MentOS  0.8.0
The Mentoring Operating System
Classes | Macros | Typedefs | Functions | Variables
paging.c File Reference

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_tpaging_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_tcreate_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_tfind_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_tmem_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_tcreate_blank_process_image (size_t stack_size)
 Creates the main memory descriptor. More...
 
mm_struct_tclone_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_tmm_cache
 Cache for storing mm_struct.
 
kmem_cache_tvm_area_cache
 Cache for storing vm_area_struct.
 
kmem_cache_tpgdir_cache
 Cache for storing page directories.
 
kmem_cache_tpgtbl_cache
 Cache for storing page tables. More...
 
static mm_struct_tmain_mm
 The mm_struct of the kernel.
 

Detailed Description

Implementation of a memory paging management.

Function Documentation

◆ __init_pagedir()

static void __init_pagedir ( page_directory_t pdir)
static

Initializes the page directory.

Parameters
pdirthe page directory to initialize.

◆ __init_pagetable()

static void __init_pagetable ( page_table_t ptable)
static

Initializes the page table.

Parameters
ptablethe page table to initialize.

◆ __mem_pg_entry_alloc()

static page_table_t* __mem_pg_entry_alloc ( page_dir_entry_t entry,
uint32_t  flags 
)
static

Allocates memory for a page table entry.

If the page table is not present, allocates a new one and sets flags accordingly.

Parameters
entryThe page directory entry for which memory is being allocated.
flagsThe flags that control the allocation, such as permissions and attributes.
Returns
A pointer to the allocated page table, or NULL if allocation fails.

◆ __page_fault_panic()

static void __page_fault_panic ( pt_regs f,
uint32_t  addr 
)
static

Prints stack frame data and calls kernel_panic.

Parameters
fThe interrupt stack frame.
addrThe faulting address.

◆ __page_handle_cow()

static int __page_handle_cow ( page_table_entry_t entry)
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.

Parameters
entryThe page table entry to manage.
Returns
0 on success, 1 on error.

◆ __pg_iter_has_next()

static int __pg_iter_has_next ( page_iterator_t iter)
static

Checks if the iterator has a next entry.

Parameters
iterThe iterator to check.
Returns
Returns 1 if the iterator can continue the iteration; otherwise, returns 0.

◆ __pg_iter_init()

static int __pg_iter_init ( page_iterator_t iter,
page_directory_t pgd,
uint32_t  addr_start,
uint32_t  size,
uint32_t  flags 
)
static

Initialize a page iterator.

Parameters
iterThe iterator to initialize.
pgdThe page directory to iterate.
addr_startThe starting address.
sizeThe total amount we want to iterate.
flagsAllocation flags.
Returns
0 on success, -1 on error.

◆ __pg_iter_next()

static pg_iter_entry_t __pg_iter_next ( page_iterator_t iter)
static

Moves the iterator to the next entry.

Parameters
iterThe iterator to advance.
Returns
The current entry after moving to the next entry.

◆ __set_pg_entry_frame()

static int __set_pg_entry_frame ( page_dir_entry_t entry,
page_table_t table 
)
inlinestatic

Sets the frame attribute of a page directory entry based on the page table's physical address.

Parameters
entryThe page directory entry to modify.
tableThe page table whose frame is being set in the directory entry.
Returns
0 on success, -1 on failure.

◆ __set_pg_table_flags()

static void __set_pg_table_flags ( page_table_entry_t table,
uint32_t  flags 
)
inlinestatic

Sets the given page table flags.

Parameters
tablethe page table.
flagsthe flags to set.

◆ clone_process_image()

mm_struct_t* clone_process_image ( mm_struct_t mmp)

Create a Memory Descriptor.

Parameters
mmpThe memory map to clone
Returns
The Memory Descriptor created.

◆ clone_vm_area()

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.

Parameters
mmthe memory descriptor which will contain the new segment.
areathe area to clone
cowwhether to use copy-on-write or just copy everything.
gfpflagsthe Get Free Pages flags.
Returns
Zero on success.

◆ create_blank_process_image()

mm_struct_t* create_blank_process_image ( size_t  stack_size)

Creates the main memory descriptor.

Parameters
stack_sizeThe size of the stack in byte.
Returns
The Memory Descriptor created.

◆ create_vm_area()

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.

Parameters
mmThe memory descriptor which will contain the new segment.
virt_startThe virtual address to map to.
sizeThe size of the segment.
pgflagsThe flags for the new memory area.
gfpflagsThe Get Free Pages flags.
Returns
The newly created virtual memory area descriptor.

◆ destroy_process_image()

int destroy_process_image ( mm_struct_t mm)

Free Memory Descriptor with all the memory segment contained.

Parameters
mmThe Memory Descriptor to free.
Returns
Returns -1 on error, otherwise 0.

◆ destroy_vm_area()

int destroy_vm_area ( mm_struct_t mm,
vm_area_struct_t area 
)

Destroys a virtual memory area.

Parameters
mmthe memory descriptor from which we will destroy the area.
areathe are we want to destroy.
Returns
0 if the area was destroyed, or -1 if the operation failed.

◆ find_free_vm_area()

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.

Parameters
mmthe memory descriptor which should contain the new area.
lengththe size of the empty spot.
vm_startwhere we save the starting address for the new area.
Returns
0 on success, -1 on error, or 1 if no free area is found.

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

Parameters
mmthe memory descriptor which should contain the area.
vm_startthe starting address of the area we are looking for.
Returns
a pointer to the area if we found it, NULL otherwise.

◆ is_current_pgd()

int is_current_pgd ( page_directory_t pgd)

Checks if the given page directory is the current one.

Parameters
pgdA pointer to the page directory to check.
Returns
1 if the given page directory is the current one, 0 otherwise.

◆ is_valid_vm_area()

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.

Parameters
mmthe memory descriptor which we use to check the range.
vm_startthe starting address of the area.
vm_endthe ending address of the area.
Returns
1 if it's valid, 0 if it's occupied, -1 if it's outside the memory.

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

Parameters
src_pgdThe source page directory.
dst_pgdThe dest page directory.
src_startThe source virtual address for the clone.
dst_startThe destination virtual address for the clone.
sizeThe size of the segment.
flagsThe flags for the new dst memory range.
Returns
0 on success, -1 on failure.

◆ mem_upd_vm_area()

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.

Parameters
pgdThe page directory to update.
virt_startThe starting virtual address to update.
phy_startThe starting physical address to map to the virtual addresses.
sizeThe size of the memory area to update.
flagsFlags to set for the page table entries.
Returns
0 on success, or -1 on failure.

◆ mem_virtual_to_page()

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.

Parameters
pgdirThe page directory.
virt_startThe starting virtual address to map.
sizePointer to a size_t variable to store the size of the mapped memory.
Returns
A pointer to the physical page corresponding to the virtual address, or NULL on error.

◆ page_fault_handler()

void page_fault_handler ( pt_regs f)

Handles a page fault.

Parameters
fThe interrupt stack frame.

◆ paging_flush_tlb_single()

void paging_flush_tlb_single ( unsigned long  addr)

Invalidate a single tlb page (the one that maps the specified virtual address)

Parameters
addrThe address of the page table.

◆ paging_get_main_directory()

page_directory_t* paging_get_main_directory ( void  )

Provide access to the main page directory.

Returns
A pointer to the main page directory, or NULL if main_mm is not initialized.

◆ paging_init()

int paging_init ( boot_info_t info)

Initializes the paging system, sets up memory caches, page directories, and maps important memory regions.

Parameters
infoPointer to the boot information structure, containing kernel addresses and other details.
Returns
0 on success, -1 on error.

◆ paging_switch_directory_va()

int paging_switch_directory_va ( page_directory_t dir)

Switches paging directory, the pointer can be a lowmem address.

Parameters
dirA pointer to the new page directory.
Returns
Returns 0 on success, or -1 if an error occurs.

Variable Documentation

◆ pgtbl_cache

kmem_cache_t* pgtbl_cache

Cache for storing page tables.

Cache used to store page tables.