The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
paging.h
Go to the documentation of this file.
1#pragma once
2
3#include "common/stdint.h"
6
7// Page Directory Entry (PDE) flags
8#define PDE_PRESENT 0x001u // page table is present in memory
9#define PDE_READ_WRITE 0x002u // page table is writable
10#define PDE_USER 0x004u // page table is accessible from ring 3
11#define PDE_WRITE_THROUGH 0x008u // write-through caching policy
12#define PDE_CACHE_DISABLE 0x010u // disable caching for this page table
13#define PDE_ACCESSED 0x020u // set by CPU when any page in the table is accessed
14#define PDE_PAGE_SIZE 0x080u // 4 MiB pages (not used; we use 4 KiB)
15
16// Page Table Entry (PTE) flags
17#define PTE_PRESENT 0x001u // page is present in memory
18#define PTE_READ_WRITE 0x002u // page is writable
19#define PTE_USER 0x004u // page is accessible from ring 3
20#define PTE_WRITE_THROUGH 0x008u // write-through caching policy
21#define PTE_CACHE_DISABLE 0x010u // disable caching for this page
22#define PTE_ACCESSED 0x020u // set by CPU when the page is accessed
23#define PTE_DIRTY 0x040u // set by CPU when the page is written to
24#define PTE_GLOBAL 0x100u // page is not flushed from TLB on CR3 reload
25#define PTE_SHARED 0x200u // (a bit for the OS) shared memory: fork does not copy-on-write it
26
27// kernel read/write page: present, writable, supervisor-only.
28#define PTE_KERNEL_RW (PTE_PRESENT | PTE_READ_WRITE)
29
30// user read/write page: present, writable, user-accessible.
31#define PTE_USER_RW (PTE_PRESENT | PTE_READ_WRITE | PTE_USER)
32
33// kernel read/write page directory entry.
34#define PDE_KERNEL_RW (PDE_PRESENT | PDE_READ_WRITE)
35
36// user read/write page directory entry.
37#define PDE_USER_RW (PDE_PRESENT | PDE_READ_WRITE | PDE_USER)
38
39#ifdef __x86_64__
40
41// 4-level paging: PML4, page directory pointer table (PDPT), page
42// directory (PD) and page table (PT), 512 entries of 64 bits each. bits
43// 47-39, 38-30, 29-21 and 20-12 of an address select the entries.
44#define PAGE_ENTRIES 512u
45#define PML4_INDEX(vaddr) ((uint32_t)(((uintptr_t)(vaddr) >> 39) & 0x1FF))
46#define PDPT_INDEX(vaddr) ((uint32_t)(((uintptr_t)(vaddr) >> 30) & 0x1FF))
47#define PD_INDEX(vaddr) ((uint32_t)(((uintptr_t)(vaddr) >> 21) & 0x1FF))
48#define PT_INDEX(vaddr) ((uint32_t)(((uintptr_t)(vaddr) >> 12) & 0x1FF))
49
50// the physical address in an entry: bits 51-12 (bit 63 is NX)
51#define PAGE_FRAME(entry) ((phys_addr_t)(entry) & 0x000FFFFFFFFFF000ul)
52
53// a 2 MiB page (in a PD entry) or 1 GiB page (in a PDPT entry)
54#define PTE_HUGE 0x080u
55
56// a paging structure entry at any level
57typedef uint64_t pte_t;
58
59#else
60
61// extract the page directory index (bits 22-31) from a virtual address.
62#define PAGE_DIR_INDEX(vaddr) ((uint32_t)((uintptr_t)(vaddr) >> 22) & 0x3FF)
63
64// extract the page table index (bits 12-21) from a virtual address.
65#define PAGE_TABLE_INDEX(vaddr) ((uint32_t)((uintptr_t)(vaddr) >> 12) & 0x3FF)
66
67// extract the physical frame address from a PDE or PTE (upper 20 bits).
68#define PAGE_FRAME(entry) ((phys_addr_t)(entry) & ~(phys_addr_t)PAGE_OFFSET_MASK)
69
70// number of entries in a page directory or page table.
71#define PAGE_ENTRIES 1024u
72
73// a page directory or page table entry (32-bit, non-PAE paging)
75
76/**
77 * a page directory: array of 1024 32-bit entries.
78 * each entry points to a page table (or is not present).
79 */
81
82/**
83 * a page table: array of 1024 32-bit entries.
84 * each entry maps a 4 KiB virtual page to a physical frame.
85 */
87
88#endif // __x86_64__
89
90/**
91 * the table an entry points to, as a kernel pointer.
92 * @param entry a present entry that points to a table.
93 * @return pointer to that table.
94 */
95static inline pte_t *entry_table(pte_t entry)
96{
97 return (pte_t *)phys_to_virt(PAGE_FRAME(entry));
98}
99
100/**
101 * initialize the paging subsystem.
102 * allocates the kernel page directory, identity-maps all usable physical RAM,
103 * installs the page fault handler, loads CR3, and enables paging.
104 * must be called after heap_init() and before io_enable_interrupts().
105 */
106void paging_init(void);
107
108/**
109 * map a single 4 KiB virtual page to a physical frame.
110 * allocates a new page table from the PMM if the relevant PDE is not present.
111 * @param virt virtual address (must be page-aligned).
112 * @param phys physical address (must be page-aligned).
113 * @param flags PTE flags to apply (e.g. PTE_KERNEL_RW).
114 * @return true on success, false if page table allocation fails.
115 */
117
118/**
119 * unmap a single 4 KiB virtual page.
120 * clears the PTE and invalidates the TLB entry.
121 * @param virt virtual address to unmap (must be page-aligned).
122 */
123void paging_unmap(uintptr_t virt);
124
125/**
126 * translate a virtual address to its mapped physical address.
127 * walks the current kernel page directory.
128 * @param virt virtual address to translate.
129 * @return physical address, or 0 if the page is not mapped.
130 */
132
133/**
134 * translate a virtual address to its physical address within a specific
135 * page directory (not necessarily the active one). walks the given
136 * directory directly, so it works for a process that is not current.
137 * @param pd_phys physical address of the page directory to walk.
138 * @param virt virtual address to translate.
139 * @return physical address, or 0 if the page is not mapped.
140 */
142
143/**
144 * return the physical address of the kernel page directory.
145 * only valid after paging_init() has been called.
146 */
148
149/**
150 * return the number of page tables currently allocated.
151 * only valid after paging_init() has been called.
152 */
154
155/**
156 * check whether paging is currently enabled (CR0.PG bit).
157 * @return true if paging is enabled, false otherwise.
158 */
159bool paging_is_enabled(void);
160
161/**
162 * clone the kernel page directory for a new process.
163 * copies kernel entries (indices 768–1023) from the kernel page directory.
164 * user entries (indices 0–767) are left empty (not present).
165 * @return physical address of the new page directory, or 0 on failure.
166 */
168
169/**
170 * free a process page directory.
171 * frees any user-space page tables (indices 0–767) and their mapped frames,
172 * then frees the directory page itself.
173 * does NOT free kernel page tables (indices 768–1023) since those are shared.
174 * @param pd_phys physical address of the page directory to free.
175 */
177
178/**
179 * copy the user half of one page directory into another, empty one, for
180 * fork. RAM frames are shared copy-on-write: they lose write access in both
181 * directories and get one more reference each. device frames are shared as
182 * they are.
183 * @param dst_phys the new directory (its user half must be empty).
184 * @param src_phys the directory to copy.
185 * @return true on success. on failure dst holds a partial copy, which the
186 * caller frees with paging_free_directory.
187 */
188bool paging_copy_user(phys_addr_t dst_phys, phys_addr_t src_phys);
189
190/**
191 * count the pages of RAM mapped in the user half of a page directory (a
192 * process's resident memory). device mappings are not counted.
193 * @param pd_phys the directory.
194 * @return the number of 4 KiB pages.
195 */
197
198/**
199 * map a page in a specific page directory (not necessarily the current one).
200 * used to set up process address spaces before switching to them.
201 * @param pd_phys physical address of the target page directory.
202 * @param virt virtual address to map (page-aligned).
203 * @param phys physical address to map to (page-aligned).
204 * @param flags PTE flags.
205 * @return true on success, false on failure.
206 */
208
209/**
210 * make sure the kernel page directory has a page table for every 4 MiB in
211 * [start, end). must run before the first paging_clone_directory(): clones
212 * copy the directory entries, so only tables that exist by then are shared
213 * by every address space, and later mappings in them are seen everywhere.
214 * @param start first virtual address of the range.
215 * @param end first virtual address past the range.
216 */
218
219/**
220 * check that every page of [addr, addr + len) is mapped user-accessible in
221 * a page directory (and writable, if write is set), at both the directory
222 * and the table level.
223 * @param pd_phys physical address of the page directory to walk.
224 * @param addr first byte of the range.
225 * @param len length of the range in bytes (0 is always accessible).
226 * @param write true if the kernel will write to the range.
227 * @return true if user code could make the same accesses.
228 */
229bool paging_user_range_ok(phys_addr_t pd_phys, uintptr_t addr, size_t len, bool write);
230
231/**
232 * read CR2, the address of the most recent page fault.
233 * @return the faulting linear address.
234 */
236
237/**
238 * remove a user mapping from a page directory and drop its TLB entry.
239 * @param pd_phys physical address of the page directory.
240 * @param virt page-aligned user address.
241 * @return the frame that was mapped there, or 0 if nothing was.
242 */
244
245/**
246 * change the flags of an existing user mapping, keeping its frame, and drop
247 * its TLB entry.
248 * @param pd_phys physical address of the page directory.
249 * @param virt page-aligned user address.
250 * @param flags new PTE flags (PTE_PRESENT must be set).
251 * @return true if a mapping was there and changed.
252 */
254
255/**
256 * switch the active page directory (load CR3).
257 * does not modify CR0 paging must already be enabled.
258 * @param pd_phys physical address of the page directory to activate.
259 */
uint8_t flags
Definition idt.c:3
unsigned int uint32_t
Definition fb.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
void paging_unmap(uintptr_t virt)
unmap a single 4 KiB virtual page.
Definition paging.c:209
void paging_init(void)
initialize the paging subsystem.
Definition paging.c:134
bool paging_copy_user(phys_addr_t dst_phys, phys_addr_t src_phys)
copy the user half of one page directory into another, empty one, for fork.
Definition paging.c:478
phys_addr_t paging_get_physical_in(phys_addr_t pd_phys, uintptr_t virt)
translate a virtual address to its physical address within a specific page directory (not necessarily...
Definition paging.c:247
bool paging_map(uintptr_t virt, phys_addr_t phys, uint32_t flags)
map a single 4 KiB virtual page to a physical frame.
Definition paging.c:179
uint32_t paging_table_count(void)
return the number of page tables currently allocated.
Definition paging.c:274
phys_addr_t paging_directory_address(void)
return the physical address of the kernel page directory.
Definition paging.c:269
bool paging_is_enabled(void)
check whether paging is currently enabled (CR0.PG bit).
Definition paging.c:279
bool paging_user_range_ok(phys_addr_t pd_phys, uintptr_t addr, size_t len, bool write)
check that every page of [addr, addr + len) is mapped user-accessible in a page directory (and writab...
Definition paging.c:300
void paging_reserve_tables(uintptr_t start, uintptr_t end)
make sure the kernel page directory has a page table for every 4 MiB in [start, end).
Definition paging.c:285
bool paging_protect_in(phys_addr_t pd_phys, uintptr_t virt, uint32_t flags)
change the flags of an existing user mapping, keeping its frame, and drop its TLB entry.
Definition paging.c:570
uintptr_t paging_fault_address(void)
read CR2, the address of the most recent page fault.
Definition paging.c:339
phys_addr_t paging_clone_directory(void)
clone the kernel page directory for a new process.
Definition paging.c:344
void paging_free_directory(phys_addr_t pd_phys)
free a process page directory.
Definition paging.c:369
uint32_t paging_count_user(phys_addr_t pd_phys)
count the pages of RAM mapped in the user half of a page directory (a process's resident memory).
Definition paging.c:451
void paging_switch_directory(phys_addr_t pd_phys)
switch the active page directory (load CR3).
Definition paging.c:583
bool paging_map_in(phys_addr_t pd_phys, uintptr_t virt, phys_addr_t phys, uint32_t flags)
map a page in a specific page directory (not necessarily the current one).
Definition paging.c:414
phys_addr_t paging_unmap_in(phys_addr_t pd_phys, uintptr_t virt)
remove a user mapping from a page directory and drop its TLB entry.
Definition paging.c:553
phys_addr_t paging_get_physical(uintptr_t virt)
translate a virtual address to its mapped physical address.
Definition paging.c:226
#define PAGE_ENTRIES
Definition paging.h:71
uint32_t pte_t
Definition paging.h:74
pte_t page_directory_t[PAGE_ENTRIES]
a page directory: array of 1024 32-bit entries.
Definition paging.h:80
pte_t page_table_t[PAGE_ENTRIES]
a page table: array of 1024 32-bit entries.
Definition paging.h:86
#define PAGE_FRAME(entry)
Definition paging.h:68
uintptr_t phys_addr_t
physical addresses and the conversion between physical and virtual.
Definition phys.h:18
ssize_t write(int fd, const void *buf, size_t count)
write bytes to a file descriptor.