|
ginnOS
The reference: libc, the libraries and the kernel
|
#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). | |
| #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 |
|
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.
|
extern |
|
extern |
|
extern |
|
extern |
|
extern |
| 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().
| 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.
| virt | virtual address (must be page-aligned). |
| phys | physical address (must be page-aligned). |
| flags | PTE flags to apply (e.g. PTE_KERNEL_RW). |
| void paging_unmap | ( | uintptr_t | virt | ) |
unmap a single 4 KiB virtual page.
clears the PTE and invalidates the TLB entry.
| virt | virtual address to unmap (must be page-aligned). |
| phys_addr_t paging_get_physical | ( | uintptr_t | virt | ) |
translate a virtual address to its mapped physical address.
walks the current kernel page directory.
| virt | virtual address to translate. |
| 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.
| pd_phys | physical address of the page directory to walk. |
| virt | virtual address to translate. |
| phys_addr_t paging_directory_address | ( | void | ) |
return the physical address of the kernel page directory.
only valid after paging_init() has been called.
| uint32_t paging_table_count | ( | void | ) |
return the number of page tables currently allocated.
only valid after paging_init() has been called.
| bool paging_is_enabled | ( | void | ) |
check whether paging is currently enabled (CR0.PG bit).
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.
| start | first virtual address of the range. |
| end | first virtual address past the range. |
| 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.
| pd_phys | physical address of the page directory to walk. |
| addr | first byte of the range. |
| len | length of the range in bytes (0 is always accessible). |
| write | true if the kernel will write to the range. |
| 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.
copies kernel entries (indices 768–1023) from the kernel page directory. user entries (indices 0–767) are left empty (not present).
| 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.
| pd_phys | physical address of the page directory to free. |
| 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.
| pd_phys | physical address of the target page directory. |
| virt | virtual address to map (page-aligned). |
| phys | physical address to map to (page-aligned). |
| flags | PTE flags. |
| 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.
| pd_phys | the 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.
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.
| dst_phys | the new directory (its user half must be empty). |
| src_phys | the directory to copy. |
| 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.
| pd_phys | physical address of the page directory. |
| virt | page-aligned user address. |
| 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.
| pd_phys | physical address of the page directory. |
| virt | page-aligned user address. |
| flags | new PTE flags (PTE_PRESENT must be set). |
| 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.
| pd_phys | physical address of the page directory to activate. |