MonkOS  v0.1
A simple 64-bit operating system (x86_64)
paging.h File Reference

Paged memory management. More...

#include <core.h>

Go to the source code of this file.

Data Structures

union  page_t
 A pagetable page record. More...
 
struct  pagetable_t
 A pagetable structure. More...
 

Macros

#define PAGE_SIZE   0x1000
 
#define PAGE_SIZE_LARGE   0x200000
 
#define PAGE_SIZE_HUGE   0x40000000
 
#define PF_PRESENT   (1 << 0)
 
#define PF_RW   (1 << 1)
 
#define PF_USER   (1 << 2)
 
#define PF_PWT   (1 << 3)
 
#define PF_PCD   (1 << 4)
 
#define PF_ACCESS   (1 << 5)
 
#define PF_DIRTY   (1 << 6)
 
#define PF_PS   (1 << 7)
 
#define PF_GLOBAL   (1 << 8)
 
#define PF_SYSTEM   (1 << 9)
 
#define PGSHIFT_PML4E   39
 
#define PGSHIFT_PDPTE   30
 
#define PGSHIFT_PDE   21
 
#define PGSHIFT_PTE   12
 
#define PGMASK_ENTRY   0x1ff
 
#define PGMASK_OFFSET   0x3ff
 
#define PML4E(a)   (((a) >> PGSHIFT_PML4E) & PGMASK_ENTRY)
 
#define PDPTE(a)   (((a) >> PGSHIFT_PDPTE) & PGMASK_ENTRY)
 
#define PDE(a)   (((a) >> PGSHIFT_PDE) & PGMASK_ENTRY)
 
#define PTE(a)   (((a) >> PGSHIFT_PTE) & PGMASK_ENTRY)
 
#define PGPTR(pte)   ((page_t *)((pte) & ~PGMASK_OFFSET))
 

Functions

void page_init ()
 Initialize the page frame database. More...
 
void pagetable_create (pagetable_t *pt, void *vaddr, uint64_t size)
 Create a new page table that can be used to associate virtual addresses with physical addresses. More...
 
void pagetable_destroy (pagetable_t *pt)
 Destroy a page table. More...
 
void pagetable_activate (pagetable_t *pt)
 Activate a page table on the CPU, so all virtual memory operations are performed relative to the page table. More...
 
void * page_alloc (pagetable_t *pt, void *vaddr, int count)
 Allocate one or more pages contiguous in virtual memory. More...
 
void page_free (pagetable_t *pt, void *vaddr, int count)
 Free one or more contiguous pages from virtual memory. More...
 

Detailed Description

Paged memory management.

Definition in file paging.h.


Data Structure Documentation

union page_t

A pagetable page record.

Contains 512 page table entries if the page holds a page table. Otherwise it contains 4096 bytes of memory.

Definition at line 54 of file paging.h.

Data Fields
uint64_t entry[PAGE_SIZE/sizeof(uint64_t)]
uint8_t memory[PAGE_SIZE]
struct pagetable_t

A pagetable structure.

Holds all the page table entries that map virtual addresses to physical addresses.

Definition at line 66 of file paging.h.

Data Fields
uint64_t proot Physical address of root page table (PML4T) entry.
uint64_t vroot Virtual address of root page table (PML4T) entry.
uint64_t vnext Virtual address to use for table's next page.
uint64_t vterm Boundary of pages used to store the table.

Macro Definition Documentation

#define PAGE_SIZE_LARGE   0x200000

Definition at line 16 of file paging.h.

Referenced by map_region(), and page_init().

#define PAGE_SIZE_HUGE   0x40000000

Definition at line 17 of file paging.h.

Referenced by map_region().

#define PF_PRESENT   (1 << 0)

Definition at line 20 of file paging.h.

Referenced by add_pte(), alloc_page(), get_pdflags(), get_ptflags(), map_table(), and page_alloc().

#define PF_RW   (1 << 1)

Definition at line 21 of file paging.h.

Referenced by add_pte(), alloc_page(), get_pdflags(), get_ptflags(), map_table(), and page_alloc().

#define PF_USER   (1 << 2)

Definition at line 22 of file paging.h.

#define PF_PWT   (1 << 3)

Definition at line 23 of file paging.h.

Referenced by get_pdflags(), and get_ptflags().

#define PF_PCD   (1 << 4)

Definition at line 24 of file paging.h.

Referenced by get_pdflags(), and get_ptflags().

#define PF_ACCESS   (1 << 5)

Definition at line 25 of file paging.h.

#define PF_DIRTY   (1 << 6)

Definition at line 26 of file paging.h.

#define PF_PS   (1 << 7)

Definition at line 27 of file paging.h.

Referenced by get_pdflags(), and is_mapped().

#define PF_GLOBAL   (1 << 8)

Definition at line 28 of file paging.h.

Referenced by get_pdflags(), and get_ptflags().

#define PF_SYSTEM   (1 << 9)

Definition at line 29 of file paging.h.

Referenced by add_pte(), alloc_page(), get_pdflags(), get_ptflags(), and pgfree_recurse().

#define PGSHIFT_PML4E   39

Definition at line 32 of file paging.h.

#define PGSHIFT_PDPTE   30

Definition at line 33 of file paging.h.

#define PGSHIFT_PDE   21

Definition at line 34 of file paging.h.

#define PGSHIFT_PTE   12

Definition at line 35 of file paging.h.

#define PGMASK_ENTRY   0x1ff

Definition at line 36 of file paging.h.

#define PGMASK_OFFSET   0x3ff

Definition at line 37 of file paging.h.

#define PML4E (   a)    (((a) >> PGSHIFT_PML4E) & PGMASK_ENTRY)
#define PDPTE (   a)    (((a) >> PGSHIFT_PDPTE) & PGMASK_ENTRY)
#define PDE (   a)    (((a) >> PGSHIFT_PDE) & PGMASK_ENTRY)
#define PTE (   a)    (((a) >> PGSHIFT_PTE) & PGMASK_ENTRY)

Definition at line 43 of file paging.h.

Referenced by add_pte(), create_page(), create_small_page(), is_mapped(), and remove_pte().

#define PGPTR (   pte)    ((page_t *)((pte) & ~PGMASK_OFFSET))

Function Documentation

void page_init ( )

Initialize the page frame database.

The page frame database manages the physical memory used by all memory pages known to the kernel.

Definition at line 107 of file paging.c.

Referenced by kmain().

void pagetable_create ( pagetable_t pt,
void *  vaddr,
uint64_t  size 
)

Create a new page table that can be used to associate virtual addresses with physical addresses.

The page table includes protected mappings for kernel memory.

Parameters
[in]ptA pointer to the pagetable structure that will hold the page table.
[in]vaddrThe virtual address within the new page table where the page table will be mapped.
[in]sizeMaximum size of the page table in bytes. Must be a multiple of PAGE_SIZE.
Returns
A handle to the created page table.

Definition at line 364 of file paging.c.

Referenced by cmd_test_heap().

void pagetable_destroy ( pagetable_t pt)

Destroy a page table.

Parameters
[in]ptA handle to the page table to destroy.

Definition at line 383 of file paging.c.

Referenced by cmd_test_heap().

void pagetable_activate ( pagetable_t pt)

Activate a page table on the CPU, so all virtual memory operations are performed relative to the page table.

Parameters
[in]ptA handle to the activated page table. Pass NULL to activate the kernel page table.

Definition at line 403 of file paging.c.

Referenced by cmd_test_heap().

void* page_alloc ( pagetable_t pt,
void *  vaddr,
int  count 
)

Allocate one or more pages contiguous in virtual memory.

Parameters
[in]ptHandle to the page table from which to allocate the page(s).
[in]vaddrThe virtual address of the first allocated page.
[in]countThe number of contiguous virtual memory pages to allocate.
Returns
A virtual memory pointer to the first page allocated.

Definition at line 415 of file paging.c.

Referenced by grow_heap(), and heap_create().

void page_free ( pagetable_t pt,
void *  vaddr,
int  count 
)

Free one or more contiguous pages from virtual memory.

Parameters
[in]ptHandle to ehte page table from which to free the page(s).
[in]vaddrThe virtual address of the first allocated page.
[in]countThe number of contiguous virtual memory pages to free.

Definition at line 425 of file paging.c.

Referenced by heap_destroy().