MentOS
0.8.0
The Mentoring Operating System
|
Implementation of a memory paging management. More...
Go to the source code of this file.
Classes | |
struct | page_dir_entry_t |
An entry of a page directory. More... | |
struct | page_table_entry_t |
An entry of a page table. More... | |
struct | page_table_t |
A page table. More... | |
struct | page_directory_t |
A page directory. More... | |
struct | vm_area_struct_t |
Virtual Memory Area, used to store details of a process segment. More... | |
struct | mm_struct_t |
Memory Descriptor, used to store details about the memory of a user process. More... | |
Macros | |
#define | PAGE_SHIFT 12 |
4KB pages (2^12 = 4096 bytes) | |
#define | PAGE_SIZE (1 << PAGE_SHIFT) |
Size of a page (4096 bytes). | |
#define | MAX_PHY_PFN (1UL << (32 - PAGE_SHIFT)) |
Maximum number of physical page frame numbers (PFNs). | |
#define | PROCAREA_START_ADDR 0x00000000 |
The start of the process area. | |
#define | PROCAREA_END_ADDR 0xC0000000 |
The end of the process area (and start of the kernel area). | |
#define | MAX_PAGE_TABLE_ENTRIES 1024 |
For a single page table in a 32-bit system. | |
#define | MAX_PAGE_DIR_ENTRIES 1024 |
For a page directory with 1024 entries. | |
Typedefs | |
typedef struct page_dir_entry_t | page_dir_entry_t |
An entry of a page directory. | |
typedef struct page_table_entry_t | page_table_entry_t |
An entry of a page table. | |
typedef struct page_table_t | _syscall0 = _syscall0( (aligned( (1 << 12 ) )) )= _syscall1( (aligned( (1 << 12 ) )) )= _syscall2( (aligned( (1 << 12 ) )) )= _syscall3( (aligned( (1 << 12 ) )) )= page_table_t |
A page table. More... | |
typedef struct vm_area_struct_t | vm_area_struct_t |
Virtual Memory Area, used to store details of a process segment. | |
typedef struct mm_struct_t | mm_struct_t |
Memory Descriptor, used to store details about the memory of a user process. | |
Enumerations | |
enum | MEMMAP_FLAGS { MM_USER = 0x1 , MM_GLOBAL = 0x2 , MM_RW = 0x4 , MM_PRESENT = 0x8 , MM_COW = 0x10 , MM_UPDADDR = 0x20 } |
Flags associated with virtual memory areas. More... | |
Functions | |
static int | vm_area_compare (const list_head *vma0, const list_head *vma1) |
Comparison function between virtual memory areas. More... | |
int | paging_init (boot_info_t *info) |
Initializes the paging system, sets up memory caches, page directories, and maps important memory regions. More... | |
page_directory_t * | paging_get_main_directory (void) |
Provide access to the main page directory. More... | |
static page_directory_t * | paging_get_current_directory (void) |
Provide access to the current paging directory. More... | |
static void | paging_switch_directory (page_directory_t *dir) |
Switches paging directory, the pointer must be a physical address. 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... | |
static void | paging_enable (void) |
Enables paging. | |
static int | paging_is_enabled (void) |
Returns if paging is enabled. More... | |
void | page_fault_handler (pt_regs *f) |
Handles a page fault. More... | |
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. 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... | |
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. 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... | |
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... | |
Variables | |
kmem_cache_t * | pgtbl_cache |
Cache used to store page tables. More... | |
Implementation of a memory paging management.
typedef struct page_directory_t _syscall0 = _syscall0( (aligned( (1 << 12 ) )) )= _syscall1( (aligned( (1 << 12 ) )) )= _syscall2( (aligned( (1 << 12 ) )) )= _syscall3( (aligned( (1 << 12 ) )) )= page_table_t |
A page table.
A page directory.
It contains 1024 entries which can be addressed by 10 bits (log_2(1024)).
In the two-level paging, this is the first level.
enum MEMMAP_FLAGS |
Flags associated with virtual memory areas.
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. |
|
inlinestatic |
Provide access to the current paging directory.
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. |
|
inlinestatic |
Returns if paging is enabled.
|
inlinestatic |
Switches paging directory, the pointer must be a physical address.
dir | A pointer to the new page directory. |
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. |
|
inlinestatic |
Comparison function between virtual memory areas.
vma0 | Pointer to the first vm_area_struct's list_head. |
vma1 | Pointer to the second vm_area_struct's list_head. |
|
extern |
Cache used to store page tables.
Cache used to store page tables.