The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
paging.c File Reference
#include "paging.h"
#include "exception.h"
#include "isr.h"
#include "arch/arch.h"
#include "common/memory.h"
#include "common/stdio.h"
#include "kernel/memory/pmm.h"
#include "kernel/memory/pmm_layout.h"
#include "kernel/memory/region.h"
#include "kernel/panic.h"
#include "kernel/memory/fault.h"
#include "kernel/memory/kstack.h"
#include "kernel/assert.h"
#include "arch/x86/cpu/gdt.h"

Macros

#define KERNEL_PDE_START   PAGE_DIR_INDEX(KERNEL_VIRT_BASE)
#define PF_PRESENT   0x01u
#define PF_WRITE   0x02u
#define PF_USER   0x04u
#define PF_RESERVED_WRITE   0x08u
#define PF_INSTRUCTION   0x10u

Functions

void paging_flush (uint32_t page_directory_phys)
 32-bit (non-PAE) paging for the i686 kernel: a page directory and page tables of 1024 entries.
void paging_invalidate (uintptr_t virtual_address)
uint32_t paging_read_cr2 (void)
uint32_t paging_read_cr0 (void)
void paging_load_cr3 (uint32_t page_directory_phys)
uint32_t paging_read_cr3 (void)
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).
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_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_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).
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_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.
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

◆ KERNEL_PDE_START

#define KERNEL_PDE_START   PAGE_DIR_INDEX(KERNEL_VIRT_BASE)

◆ PF_PRESENT

#define PF_PRESENT   0x01u

◆ PF_WRITE

#define PF_WRITE   0x02u

◆ PF_USER

#define PF_USER   0x04u

◆ PF_RESERVED_WRITE

#define PF_RESERVED_WRITE   0x08u

◆ PF_INSTRUCTION

#define PF_INSTRUCTION   0x10u

Function Documentation

◆ paging_flush()

void paging_flush ( uint32_t page_directory_phys)
extern

32-bit (non-PAE) paging for the i686 kernel: a page directory and page tables of 1024 entries.

the x86_64 kernel uses paging64.c.

◆ paging_invalidate()

void paging_invalidate ( uintptr_t virtual_address)
extern

◆ paging_read_cr2()

uint32_t paging_read_cr2 ( void )
extern

◆ paging_read_cr0()

uint32_t paging_read_cr0 ( void )
extern

◆ paging_load_cr3()

void paging_load_cr3 ( uint32_t page_directory_phys)
extern

◆ paging_read_cr3()

uint32_t paging_read_cr3 ( void )
extern

◆ 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_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_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_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_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_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_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.