The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
paging.h File Reference

Go to the source code of this file.

Macros

#define PDE_PRESENT   0x001u
#define PDE_READ_WRITE   0x002u
#define PDE_USER   0x004u
#define PDE_WRITE_THROUGH   0x008u
#define PDE_CACHE_DISABLE   0x010u
#define PDE_ACCESSED   0x020u
#define PDE_PAGE_SIZE   0x080u
#define PTE_PRESENT   0x001u
#define PTE_READ_WRITE   0x002u
#define PTE_USER   0x004u
#define PTE_WRITE_THROUGH   0x008u
#define PTE_CACHE_DISABLE   0x010u
#define PTE_ACCESSED   0x020u
#define PTE_DIRTY   0x040u
#define PTE_GLOBAL   0x100u
#define PTE_SHARED   0x200u
#define PTE_KERNEL_RW   (PTE_PRESENT | PTE_READ_WRITE)
#define PTE_USER_RW   (PTE_PRESENT | PTE_READ_WRITE | PTE_USER)
#define PDE_KERNEL_RW   (PDE_PRESENT | PDE_READ_WRITE)
#define PDE_USER_RW   (PDE_PRESENT | PDE_READ_WRITE | PDE_USER)
#define PAGE_DIR_INDEX(vaddr)
#define PAGE_TABLE_INDEX(vaddr)
#define PAGE_FRAME(entry)
#define PAGE_ENTRIES   1024u

Typedefs

typedef uint32_t pte_t
typedef pte_t page_directory_t[PAGE_ENTRIES]
 a page directory: array of 1024 32-bit entries.
typedef pte_t page_table_t[PAGE_ENTRIES]
 a page table: array of 1024 32-bit entries.

Functions

void paging_init (void)
 initialize the paging subsystem.
bool paging_map (uintptr_t virt, phys_addr_t phys, uint32_t flags)
 map a single 4 KiB virtual page to a physical frame.
void paging_unmap (uintptr_t virt)
 unmap a single 4 KiB virtual page.
phys_addr_t paging_get_physical (uintptr_t virt)
 translate a virtual address to its mapped physical address.
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 the active one).
phys_addr_t paging_directory_address (void)
 return the physical address of the kernel page directory.
uint32_t paging_table_count (void)
 return the number of page tables currently allocated.
bool paging_is_enabled (void)
 check whether paging is currently enabled (CR0.PG bit).
phys_addr_t paging_clone_directory (void)
 clone the kernel page directory for a new process.
void paging_free_directory (phys_addr_t pd_phys)
 free a process page directory.
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.
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).
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).
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).
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 writable, if write is set), at both the directory and the table level.
uintptr_t paging_fault_address (void)
 read CR2, the address of the most recent page fault.
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.
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.
void paging_switch_directory (phys_addr_t pd_phys)
 switch the active page directory (load CR3).

Macro Definition Documentation

◆ PDE_PRESENT

#define PDE_PRESENT   0x001u

◆ PDE_READ_WRITE

#define PDE_READ_WRITE   0x002u

◆ PDE_USER

#define PDE_USER   0x004u

◆ PDE_WRITE_THROUGH

#define PDE_WRITE_THROUGH   0x008u

◆ PDE_CACHE_DISABLE

#define PDE_CACHE_DISABLE   0x010u

◆ PDE_ACCESSED

#define PDE_ACCESSED   0x020u

◆ PDE_PAGE_SIZE

#define PDE_PAGE_SIZE   0x080u

◆ PTE_PRESENT

#define PTE_PRESENT   0x001u

◆ PTE_READ_WRITE

#define PTE_READ_WRITE   0x002u

◆ PTE_USER

#define PTE_USER   0x004u

◆ PTE_WRITE_THROUGH

#define PTE_WRITE_THROUGH   0x008u

◆ PTE_CACHE_DISABLE

#define PTE_CACHE_DISABLE   0x010u

◆ PTE_ACCESSED

#define PTE_ACCESSED   0x020u

◆ PTE_DIRTY

#define PTE_DIRTY   0x040u

◆ PTE_GLOBAL

#define PTE_GLOBAL   0x100u

◆ PTE_SHARED

#define PTE_SHARED   0x200u

◆ PTE_KERNEL_RW

#define PTE_KERNEL_RW   (PTE_PRESENT | PTE_READ_WRITE)

◆ PTE_USER_RW

#define PTE_USER_RW   (PTE_PRESENT | PTE_READ_WRITE | PTE_USER)

◆ PDE_KERNEL_RW

#define PDE_KERNEL_RW   (PDE_PRESENT | PDE_READ_WRITE)

◆ PDE_USER_RW

#define PDE_USER_RW   (PDE_PRESENT | PDE_READ_WRITE | PDE_USER)

◆ PAGE_DIR_INDEX

#define PAGE_DIR_INDEX ( vaddr)
Value:
((uint32_t)((uintptr_t)(vaddr) >> 22) & 0x3FF)
unsigned int uint32_t
Definition fb.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19

◆ PAGE_TABLE_INDEX

#define PAGE_TABLE_INDEX ( vaddr)
Value:
((uint32_t)((uintptr_t)(vaddr) >> 12) & 0x3FF)

◆ PAGE_FRAME

#define PAGE_FRAME ( entry)
Value:
#define PAGE_OFFSET_MASK
Definition page.h:15
uintptr_t phys_addr_t
physical addresses and the conversion between physical and virtual.
Definition phys.h:18

◆ PAGE_ENTRIES

#define PAGE_ENTRIES   1024u

Typedef Documentation

◆ pte_t

typedef uint32_t pte_t

◆ page_directory_t

typedef pte_t page_directory_t[PAGE_ENTRIES]

a page directory: array of 1024 32-bit entries.

each entry points to a page table (or is not present).

◆ page_table_t

typedef pte_t page_table_t[PAGE_ENTRIES]

a page table: array of 1024 32-bit entries.

each entry maps a 4 KiB virtual page to a physical frame.

Function Documentation

◆ paging_init()

void paging_init ( void )

initialize the paging subsystem.

allocates the kernel page directory, identity-maps all usable physical RAM, installs the page fault handler, loads CR3, and enables paging. must be called after heap_init() and before io_enable_interrupts().

◆ paging_map()

bool paging_map ( uintptr_t virt,
phys_addr_t phys,
uint32_t flags )

map a single 4 KiB virtual page to a physical frame.

allocates a new page table from the PMM if the relevant PDE is not present.

Parameters
virtvirtual address (must be page-aligned).
physphysical address (must be page-aligned).
flagsPTE flags to apply (e.g. PTE_KERNEL_RW).
Returns
true on success, false if page table allocation fails.

◆ paging_unmap()

void paging_unmap ( uintptr_t virt)

unmap a single 4 KiB virtual page.

clears the PTE and invalidates the TLB entry.

Parameters
virtvirtual address to unmap (must be page-aligned).

◆ paging_get_physical()

phys_addr_t paging_get_physical ( uintptr_t virt)

translate a virtual address to its mapped physical address.

walks the current kernel page directory.

Parameters
virtvirtual address to translate.
Returns
physical address, or 0 if the page is not mapped.

◆ paging_get_physical_in()

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 the active one).

walks the given directory directly, so it works for a process that is not current.

Parameters
pd_physphysical address of the page directory to walk.
virtvirtual address to translate.
Returns
physical address, or 0 if the page is not mapped.

◆ paging_directory_address()

phys_addr_t paging_directory_address ( void )

return the physical address of the kernel page directory.

only valid after paging_init() has been called.

◆ paging_table_count()

uint32_t paging_table_count ( void )

return the number of page tables currently allocated.

only valid after paging_init() has been called.

◆ paging_is_enabled()

bool paging_is_enabled ( void )

check whether paging is currently enabled (CR0.PG bit).

Returns
true if paging is enabled, false otherwise.

◆ paging_clone_directory()

phys_addr_t paging_clone_directory ( void )

clone the kernel page directory for a new process.

copies kernel entries (indices 768–1023) from the kernel page directory. user entries (indices 0–767) are left empty (not present).

Returns
physical address of the new page directory, or 0 on failure.

◆ paging_free_directory()

void paging_free_directory ( phys_addr_t pd_phys)

free a process page directory.

frees any user-space page tables (indices 0–767) and their mapped frames, then frees the directory page itself. does NOT free kernel page tables (indices 768–1023) since those are shared.

Parameters
pd_physphysical address of the page directory to free.

◆ paging_copy_user()

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.

RAM frames are shared copy-on-write: they lose write access in both directories and get one more reference each. device frames are shared as they are.

Parameters
dst_physthe new directory (its user half must be empty).
src_physthe directory to copy.
Returns
true on success. on failure dst holds a partial copy, which the caller frees with paging_free_directory.

◆ paging_count_user()

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).

device mappings are not counted.

Parameters
pd_physthe directory.
Returns
the number of 4 KiB pages.

◆ paging_map_in()

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).

used to set up process address spaces before switching to them.

Parameters
pd_physphysical address of the target page directory.
virtvirtual address to map (page-aligned).
physphysical address to map to (page-aligned).
flagsPTE flags.
Returns
true on success, false on failure.

◆ paging_reserve_tables()

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).

must run before the first paging_clone_directory(): clones copy the directory entries, so only tables that exist by then are shared by every address space, and later mappings in them are seen everywhere.

Parameters
startfirst virtual address of the range.
endfirst virtual address past the range.

◆ paging_user_range_ok()

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 writable, if write is set), at both the directory and the table level.

Parameters
pd_physphysical address of the page directory to walk.
addrfirst byte of the range.
lenlength of the range in bytes (0 is always accessible).
writetrue if the kernel will write to the range.
Returns
true if user code could make the same accesses.

◆ paging_fault_address()

uintptr_t paging_fault_address ( void )

read CR2, the address of the most recent page fault.

Returns
the faulting linear address.

◆ paging_unmap_in()

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.

Parameters
pd_physphysical address of the page directory.
virtpage-aligned user address.
Returns
the frame that was mapped there, or 0 if nothing was.

◆ paging_protect_in()

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.

Parameters
pd_physphysical address of the page directory.
virtpage-aligned user address.
flagsnew PTE flags (PTE_PRESENT must be set).
Returns
true if a mapping was there and changed.

◆ paging_switch_directory()

void paging_switch_directory ( phys_addr_t pd_phys)

switch the active page directory (load CR3).

does not modify CR0 paging must already be enabled.

Parameters
pd_physphysical address of the page directory to activate.