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

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_tpaging_get_main_directory (void)
 Provide access to the main page directory. More...
 
static page_directory_tpaging_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_tmem_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_tcreate_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_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...
 
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...
 

Variables

kmem_cache_tpgtbl_cache
 Cache used to store page tables. More...
 

Detailed Description

Implementation of a memory paging management.

Typedef Documentation

◆ _syscall0

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.

Enumeration Type Documentation

◆ MEMMAP_FLAGS

Flags associated with virtual memory areas.

Enumerator
MM_USER 

Area belongs to user mode (accessible by user-level processes).

MM_GLOBAL 

Area is global (not flushed from TLB on context switch).

MM_RW 

Area has read/write permissions.

MM_PRESENT 

Area is present in memory.

MM_COW 

Area is copy-on-write (used for forked processes).

MM_UPDADDR 

Update address (used for special memory mappings).

Function Documentation

◆ 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_current_directory()

static page_directory_t* paging_get_current_directory ( void  )
inlinestatic

Provide access to the current paging directory.

Returns
A pointer to the current page directory.

◆ 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_is_enabled()

static int paging_is_enabled ( void  )
inlinestatic

Returns if paging is enabled.

Returns
1 if paging is enables, 0 otherwise.

◆ paging_switch_directory()

static void paging_switch_directory ( page_directory_t dir)
inlinestatic

Switches paging directory, the pointer must be a physical address.

Parameters
dirA pointer to the new page directory.

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

◆ vm_area_compare()

static int vm_area_compare ( const list_head *  vma0,
const list_head *  vma1 
)
inlinestatic

Comparison function between virtual memory areas.

Parameters
vma0Pointer to the first vm_area_struct's list_head.
vma1Pointer to the second vm_area_struct's list_head.
Returns
1 if vma0 starts after vma1 ends, 0 otherwise.

Variable Documentation

◆ pgtbl_cache

kmem_cache_t* pgtbl_cache
extern

Cache used to store page tables.

Cache used to store page tables.