MentOS
0.8.0
The Mentoring Operating System
|
Implementation of a memory paging management. More...
Classes | |
struct | page_iterator_s |
Structure for iterating page directory entries. More... | |
struct | pg_iter_entry_s |
Structure for iterating page table entries. More... | |
Macros | |
#define | __DEBUG_HEADER__ "[PAGING]" |
Change header. | |
#define | __DEBUG_LEVEL__ LOGLEVEL_NOTICE |
Set log level. | |
#define | ERR_PRESENT 0x01 |
Page not present. | |
#define | ERR_RW 0x02 |
Page is read only. | |
#define | ERR_USER 0x04 |
Page is privileged. | |
#define | ERR_RESERVED 0x08 |
Overwrote reserved bit. | |
#define | ERR_INST 0x10 |
Instruction fetch. | |
Typedefs | |
typedef struct page_iterator_s | page_iterator_t |
Structure for iterating page directory entries. | |
typedef struct pg_iter_entry_s | pg_iter_entry_t |
Structure for iterating page table entries. | |
Functions | |
page_directory_t * | paging_get_main_directory (void) |
Provide access to the main page directory. More... | |
int | is_current_pgd (page_directory_t *pgd) |
Checks if the given page directory is the current one. More... | |
int | paging_switch_directory_va (page_directory_t *dir) |
Switches paging directory, the pointer can be a lowmem address. More... | |
void | paging_flush_tlb_single (unsigned long addr) |
Invalidate a single tlb page (the one that maps the specified virtual address) More... | |
vm_area_struct_t * | create_vm_area (mm_struct_t *mm, uint32_t vm_start, size_t size, uint32_t pgflags, uint32_t gfpflags) |
Create a virtual memory area. More... | |
uint32_t | clone_vm_area (mm_struct_t *mm, vm_area_struct_t *area, int cow, uint32_t gfpflags) |
Clone a virtual memory area, using copy on write if specified. More... | |
int | destroy_vm_area (mm_struct_t *mm, vm_area_struct_t *area) |
Destroys a virtual memory area. More... | |
vm_area_struct_t * | find_vm_area (mm_struct_t *mm, uint32_t vm_start) |
Searches for the virtual memory area at the given address. More... | |
int | is_valid_vm_area (mm_struct_t *mm, uintptr_t vm_start, uintptr_t vm_end) |
Checks if the given virtual memory area range is valid. More... | |
int | find_free_vm_area (mm_struct_t *mm, size_t length, uintptr_t *vm_start) |
Searches for an empty spot for a new virtual memory area. More... | |
static void | __init_pagedir (page_directory_t *pdir) |
Initializes the page directory. More... | |
static void | __init_pagetable (page_table_t *ptable) |
Initializes the page table. More... | |
int | paging_init (boot_info_t *info) |
Initializes the paging system, sets up memory caches, page directories, and maps important memory regions. More... | |
static void | __set_pg_table_flags (page_table_entry_t *table, uint32_t flags) |
Sets the given page table flags. More... | |
static void | __page_fault_panic (pt_regs *f, uint32_t addr) |
Prints stack frame data and calls kernel_panic. More... | |
static int | __page_handle_cow (page_table_entry_t *entry) |
Handles the Copy-On-Write (COW) mechanism for a page table entry. If the page is marked as COW, it allocates a new page and updates the entry. More... | |
static page_table_t * | __mem_pg_entry_alloc (page_dir_entry_t *entry, uint32_t flags) |
Allocates memory for a page table entry. More... | |
static int | __set_pg_entry_frame (page_dir_entry_t *entry, page_table_t *table) |
Sets the frame attribute of a page directory entry based on the page table's physical address. More... | |
void | page_fault_handler (pt_regs *f) |
Handles a page fault. More... | |
static int | __pg_iter_init (page_iterator_t *iter, page_directory_t *pgd, uint32_t addr_start, uint32_t size, uint32_t flags) |
Initialize a page iterator. More... | |
static int | __pg_iter_has_next (page_iterator_t *iter) |
Checks if the iterator has a next entry. More... | |
static pg_iter_entry_t | __pg_iter_next (page_iterator_t *iter) |
Moves the iterator to the next entry. More... | |
page_t * | mem_virtual_to_page (page_directory_t *pgd, uint32_t virt_start, size_t *size) |
Maps a virtual address to a corresponding physical page. More... | |
int | mem_upd_vm_area (page_directory_t *pgd, uint32_t virt_start, uint32_t phy_start, size_t size, uint32_t flags) |
Updates the virtual memory area in a page directory. More... | |
int | mem_clone_vm_area (page_directory_t *src_pgd, page_directory_t *dst_pgd, uint32_t src_start, uint32_t dst_start, size_t size, uint32_t flags) |
Clones a range of pages between two distinct page tables. More... | |
mm_struct_t * | create_blank_process_image (size_t stack_size) |
Creates the main memory descriptor. More... | |
mm_struct_t * | clone_process_image (mm_struct_t *mmp) |
Create a Memory Descriptor. More... | |
int | destroy_process_image (mm_struct_t *mm) |
Free Memory Descriptor with all the memory segment contained. More... | |
void * | sys_mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset) |
int | sys_munmap (void *addr, size_t length) |
Variables | |
kmem_cache_t * | mm_cache |
Cache for storing mm_struct. | |
kmem_cache_t * | vm_area_cache |
Cache for storing vm_area_struct. | |
kmem_cache_t * | pgdir_cache |
Cache for storing page directories. | |
kmem_cache_t * | pgtbl_cache |
Cache for storing page tables. More... | |
static mm_struct_t * | main_mm |
The mm_struct of the kernel. | |
Implementation of a memory paging management.
|
static |
Initializes the page directory.
pdir | the page directory to initialize. |
|
static |
Initializes the page table.
ptable | the page table to initialize. |
|
static |
Allocates memory for a page table entry.
If the page table is not present, allocates a new one and sets flags accordingly.
entry | The page directory entry for which memory is being allocated. |
flags | The flags that control the allocation, such as permissions and attributes. |
Prints stack frame data and calls kernel_panic.
f | The interrupt stack frame. |
addr | The faulting address. |
|
static |
Handles the Copy-On-Write (COW) mechanism for a page table entry. If the page is marked as COW, it allocates a new page and updates the entry.
entry | The page table entry to manage. |
|
static |
Checks if the iterator has a next entry.
iter | The iterator to check. |
|
static |
Initialize a page iterator.
iter | The iterator to initialize. |
pgd | The page directory to iterate. |
addr_start | The starting address. |
size | The total amount we want to iterate. |
flags | Allocation flags. |
|
static |
Moves the iterator to the next entry.
iter | The iterator to advance. |
|
inlinestatic |
Sets the frame attribute of a page directory entry based on the page table's physical address.
entry | The page directory entry to modify. |
table | The page table whose frame is being set in the directory entry. |
|
inlinestatic |
Sets the given page table flags.
table | the page table. |
flags | the flags to set. |
mm_struct_t* clone_process_image | ( | mm_struct_t * | mmp | ) |
Create a Memory Descriptor.
mmp | The memory map to clone |
uint32_t clone_vm_area | ( | mm_struct_t * | mm, |
vm_area_struct_t * | area, | ||
int | cow, | ||
uint32_t | gfpflags | ||
) |
Clone a virtual memory area, using copy on write if specified.
mm | the memory descriptor which will contain the new segment. |
area | the area to clone |
cow | whether to use copy-on-write or just copy everything. |
gfpflags | the Get Free Pages flags. |
mm_struct_t* create_blank_process_image | ( | size_t | stack_size | ) |
Creates the main memory descriptor.
stack_size | The size of the stack in byte. |
vm_area_struct_t* create_vm_area | ( | mm_struct_t * | mm, |
uint32_t | virt_start, | ||
size_t | size, | ||
uint32_t | pgflags, | ||
uint32_t | gfpflags | ||
) |
Create a virtual memory area.
mm | The memory descriptor which will contain the new segment. |
virt_start | The virtual address to map to. |
size | The size of the segment. |
pgflags | The flags for the new memory area. |
gfpflags | The Get Free Pages flags. |
int destroy_process_image | ( | mm_struct_t * | mm | ) |
Free Memory Descriptor with all the memory segment contained.
mm | The Memory Descriptor to free. |
int destroy_vm_area | ( | mm_struct_t * | mm, |
vm_area_struct_t * | area | ||
) |
Destroys a virtual memory area.
mm | the memory descriptor from which we will destroy the area. |
area | the are we want to destroy. |
int find_free_vm_area | ( | mm_struct_t * | mm, |
size_t | length, | ||
uintptr_t * | vm_start | ||
) |
Searches for an empty spot for a new virtual memory area.
mm | the memory descriptor which should contain the new area. |
length | the size of the empty spot. |
vm_start | where we save the starting address for the new area. |
vm_area_struct_t* find_vm_area | ( | mm_struct_t * | mm, |
uint32_t | vm_start | ||
) |
Searches for the virtual memory area at the given address.
mm | the memory descriptor which should contain the area. |
vm_start | the starting address of the area we are looking for. |
int is_current_pgd | ( | page_directory_t * | pgd | ) |
Checks if the given page directory is the current one.
pgd | A pointer to the page directory to check. |
int is_valid_vm_area | ( | mm_struct_t * | mm, |
uintptr_t | vm_start, | ||
uintptr_t | vm_end | ||
) |
Checks if the given virtual memory area range is valid.
mm | the memory descriptor which we use to check the range. |
vm_start | the starting address of the area. |
vm_end | the ending address of the area. |
int mem_clone_vm_area | ( | page_directory_t * | src_pgd, |
page_directory_t * | dst_pgd, | ||
uint32_t | src_start, | ||
uint32_t | dst_start, | ||
size_t | size, | ||
uint32_t | flags | ||
) |
Clones a range of pages between two distinct page tables.
src_pgd | The source page directory. |
dst_pgd | The dest page directory. |
src_start | The source virtual address for the clone. |
dst_start | The destination virtual address for the clone. |
size | The size of the segment. |
flags | The flags for the new dst memory range. |
int mem_upd_vm_area | ( | page_directory_t * | pgd, |
uint32_t | virt_start, | ||
uint32_t | phy_start, | ||
size_t | size, | ||
uint32_t | flags | ||
) |
Updates the virtual memory area in a page directory.
pgd | The page directory to update. |
virt_start | The starting virtual address to update. |
phy_start | The starting physical address to map to the virtual addresses. |
size | The size of the memory area to update. |
flags | Flags to set for the page table entries. |
page_t* mem_virtual_to_page | ( | page_directory_t * | pgdir, |
uint32_t | virt_start, | ||
size_t * | size | ||
) |
Maps a virtual address to a corresponding physical page.
pgdir | The page directory. |
virt_start | The starting virtual address to map. |
size | Pointer to a size_t variable to store the size of the mapped memory. |
void page_fault_handler | ( | pt_regs * | f | ) |
Handles a page fault.
f | The interrupt stack frame. |
void paging_flush_tlb_single | ( | unsigned long | addr | ) |
Invalidate a single tlb page (the one that maps the specified virtual address)
addr | The address of the page table. |
page_directory_t* paging_get_main_directory | ( | void | ) |
Provide access to the main page directory.
int paging_init | ( | boot_info_t * | info | ) |
Initializes the paging system, sets up memory caches, page directories, and maps important memory regions.
info | Pointer to the boot information structure, containing kernel addresses and other details. |
int paging_switch_directory_va | ( | page_directory_t * | dir | ) |
Switches paging directory, the pointer can be a lowmem address.
dir | A pointer to the new page directory. |
kmem_cache_t* pgtbl_cache |
Cache for storing page tables.
Cache used to store page tables.