The manual
ginnOS
The reference: libc, the libraries and the kernel
Toggle main menu visibility
Loading...
Searching...
No Matches
phys.h
Go to the documentation of this file.
1
#pragma once
2
3
/**
4
* physical addresses and the conversion between physical and virtual.
5
*
6
* the kernel runs in the higher half and reaches RAM through a direct map:
7
* physical address P is at virtual P + KERNEL_VIRT_BASE (up to
8
* PHYS_MAP_SIZE). device memory outside RAM is mapped with ioremap instead
9
* (kernel/memory/mmio.h), and kernel stacks live in their own region, so
10
* virt_to_phys only works on direct-mapped pointers (heap, PMM frames,
11
* the kernel image).
12
*/
13
14
#include "
common/stdint.h
"
15
#include "
kernel/memory/memory_layout.h
"
16
17
// a physical address (pointer-width for now; PAE or x86_64 may widen it)
18
typedef
uintptr_t
phys_addr_t
;
19
20
/**
21
* pointer through which the kernel reaches a physical address.
22
* @param phys the physical address.
23
* @return the kernel virtual address of that byte.
24
*/
25
static
inline
void
*phys_to_virt(
phys_addr_t
phys)
26
{
27
return
(
void
*)(phys +
KERNEL_VIRT_BASE
);
28
}
29
30
#ifdef __x86_64__
31
32
/* the physical address of the first byte of the kernel image, from limine
33
* (arch/x86/boot64/limine_boot.c). the image is mapped at
34
* KERNEL_IMAGE_BASE, not in the direct map. */
35
extern
phys_addr_t
kernel_image_phys;
36
37
/**
38
* physical address behind a kernel pointer into RAM: the direct map or the
39
* kernel image (not the kernel stack region or other remapped memory).
40
* @param virt a kernel pointer into directly mapped RAM or the kernel image.
41
* @return its physical address.
42
*/
43
static
inline
phys_addr_t
virt_to_phys(
const
void
*virt)
44
{
45
uintptr_t
addr = (
uintptr_t
)virt;
46
if
(addr >= KERNEL_IMAGE_BASE)
47
{
48
return
addr - KERNEL_IMAGE_BASE + kernel_image_phys;
49
}
50
return
addr -
KERNEL_VIRT_BASE
;
51
}
52
53
#else
54
55
/**
56
* physical address behind a kernel pointer into RAM (not the kernel stack
57
* region or other remapped memory).
58
* @param virt a kernel pointer into directly mapped RAM.
59
* @return its physical address.
60
*/
61
static
inline
phys_addr_t
virt_to_phys(
const
void
*virt)
62
{
63
return
(
phys_addr_t
)virt -
KERNEL_VIRT_BASE
;
64
}
65
66
#endif
// __x86_64__
stdint.h
uintptr_t
__UINTPTR_TYPE__ uintptr_t
Definition
stdint.h:19
memory_layout.h
KERNEL_VIRT_BASE
#define KERNEL_VIRT_BASE
Definition
memory_layout.h:113
phys_addr_t
uintptr_t phys_addr_t
physical addresses and the conversion between physical and virtual.
Definition
phys.h:18
src
kernel
memory
phys.h
Generated by
1.18.0