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

Paged memory management. More...

#include <core.h>
#include <libc/stdlib.h>
#include <libc/string.h>
#include <kernel/x86/cpu.h>
#include <kernel/interrupt/interrupt.h>
#include <kernel/mem/kmem.h>
#include <kernel/mem/pmap.h>
#include <kernel/mem/paging.h>

Go to the source code of this file.

Data Structures

struct  pf_t
 The pf structure represents a record in the page frame database. More...
 
struct  pfdb
 The pfdb describes the state of the page frame database. More...
 

Macros

#define CONTAINS_TABLE   (1 << 0)
 
#define PAGE_SHIFT   12
 
#define PAGE_SHIFT_LARGE   21
 
#define PFN_INVALID   ((uint32_t)-1)
 
#define PADDR_TO_PF(a)   ((pf_t *)(pfdb.pf + ((a) >> PAGE_SHIFT)))
 
#define PF_TO_PADDR(pf)   ((uint64_t)((pf) - pfdb.pf) << PAGE_SHIFT)
 
#define PFN_TO_PF(pfn)   ((pf_t *)((pfn) + pfdb.pf))
 
#define PF_TO_PFN(pf)   ((uint32_t)((pf) - pfdb.pf))
 
#define PTE_TO_PADDR(pte)   ((pte) & ~PGMASK_OFFSET)
 

Enumerations

Functions

 STATIC_ASSERT (sizeof(pf_t)==32,"Unexpected page frame size")
 
static void * reserve_region (const pmap_t *map, uint64_t size, uint32_t alignshift)
 Reserve an aligned region of memory managed by the memory table module. More...
 
void page_init ()
 Initialize the page frame database. More...
 
static pf_tpfalloc ()
 
static void pffree (pf_t *pf)
 
static uint64_t pgalloc ()
 
static void pgfree (uint64_t paddr)
 
static void pgfree_recurse (page_t *page, int level)
 
static void add_pte (pagetable_t *pt, uint64_t vaddr, uint64_t paddr, uint32_t pflags, uint32_t addflags)
 Add to the page table an entry mapping a virtual address to a physical address. More...
 
static uint64_t remove_pte (pagetable_t *pt, uint64_t vaddr)
 
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_in, int count)
 Allocate one or more pages contiguous in virtual memory. More...
 
void page_free (pagetable_t *pt, void *vaddr_in, int count)
 Free one or more contiguous pages from virtual memory. More...
 

Variables

static struct pfdb pfdb
 
static pagetable_t kpt
 
static pagetable_tactive_pt
 

Detailed Description

Paged memory management.

Definition in file paging.c.


Data Structure Documentation

struct pf_t

The pf structure represents a record in the page frame database.

Definition at line 45 of file paging.c.

Data Fields
uint32_t prev Index of prev pfn on available list.
uint32_t next Index of next pfn on available list.
uint16_t refcount Number of references to this page.
uint16_t sharecount Number of processes sharing page.
uint16_t flags
uint8_t type PFTYPE of page frame.
uint8_t reserved0
uint64_t reserved1
uint64_t reserved2
struct pfdb

The pfdb describes the state of the page frame database.

Definition at line 61 of file paging.c.

Data Fields
pf_t * pf Pointer to array of page frames.
uint32_t count Total number of frames in the pfdb.
uint32_t avail Available number of frames in the pfdb.
uint32_t head Index of available frame list head.
uint32_t tail Index of available frame list tail.

Macro Definition Documentation

#define CONTAINS_TABLE   (1 << 0)

Definition at line 20 of file paging.c.

Referenced by add_pte().

#define PAGE_SHIFT   12

Definition at line 23 of file paging.c.

Referenced by page_init().

#define PAGE_SHIFT_LARGE   21

Definition at line 24 of file paging.c.

Referenced by page_init().

#define PFN_INVALID   ((uint32_t)-1)

Definition at line 27 of file paging.c.

Referenced by page_init(), pfalloc(), and pffree().

#define PADDR_TO_PF (   a)    ((pf_t *)(pfdb.pf + ((a) >> PAGE_SHIFT)))

Definition at line 30 of file paging.c.

Referenced by pgfree(), and pgfree_recurse().

#define PF_TO_PADDR (   pf)    ((uint64_t)((pf) - pfdb.pf) << PAGE_SHIFT)

Definition at line 31 of file paging.c.

Referenced by pgalloc().

#define PFN_TO_PF (   pfn)    ((pf_t *)((pfn) + pfdb.pf))

Definition at line 32 of file paging.c.

Referenced by page_init(), and pfalloc().

#define PF_TO_PFN (   pf)    ((uint32_t)((pf) - pfdb.pf))

Definition at line 33 of file paging.c.

Referenced by pffree().

#define PTE_TO_PADDR (   pte)    ((pte) & ~PGMASK_OFFSET)

Definition at line 34 of file paging.c.

Referenced by pgfree_recurse().

Enumeration Type Documentation

anonymous enum
Enumerator
PFTYPE_RESERVED 
PFTYPE_AVAILABLE 
PFTYPE_ALLOCATED 

Definition at line 37 of file paging.c.

Function Documentation

STATIC_ASSERT ( sizeof(pf_t = =32,
"Unexpected page frame size"   
)
static void* reserve_region ( const pmap_t *  map,
uint64_t  size,
uint32_t  alignshift 
)
static

Reserve an aligned region of memory managed by the memory table module.

Definition at line 78 of file paging.c.

Referenced by page_init().

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().

static pf_t* pfalloc ( )
static

Definition at line 180 of file paging.c.

Referenced by pgalloc().

static void pffree ( pf_t pf)
static

Definition at line 202 of file paging.c.

Referenced by pgfree().

static uint64_t pgalloc ( )
static

Definition at line 222 of file paging.c.

Referenced by add_pte(), page_alloc(), and pagetable_create().

static void pgfree ( uint64_t  paddr)
static

Definition at line 236 of file paging.c.

Referenced by page_free(), and pgfree_recurse().

static void pgfree_recurse ( page_t page,
int  level 
)
static

Definition at line 244 of file paging.c.

Referenced by pagetable_destroy().

static void add_pte ( pagetable_t pt,
uint64_t  vaddr,
uint64_t  paddr,
uint32_t  pflags,
uint32_t  addflags 
)
static

Add to the page table an entry mapping a virtual address to a physical address.

Definition at line 276 of file paging.c.

Referenced by page_alloc().

static uint64_t remove_pte ( pagetable_t pt,
uint64_t  vaddr 
)
static

Definition at line 337 of file paging.c.

Referenced by page_free().

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().

Variable Documentation

struct pfdb pfdb
static

Definition at line 70 of file paging.c.

pagetable_t kpt
static

Definition at line 71 of file paging.c.

Referenced by page_init(), and pagetable_activate().

pagetable_t* active_pt
static

Definition at line 72 of file paging.c.