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_t * | pfalloc () |
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_t * | active_pt |
Paged memory management.
Definition in file paging.c.
struct pf_t |
The pf structure represents a record in the page frame database.
struct pfdb |
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. |
#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 32 of file paging.c.
Referenced by page_init(), and pfalloc().
#define PF_TO_PFN | ( | pf | ) | ((uint32_t)((pf) - pfdb.pf)) |
#define PTE_TO_PADDR | ( | pte | ) | ((pte) & ~PGMASK_OFFSET) |
Definition at line 34 of file paging.c.
Referenced by pgfree_recurse().
anonymous enum |
STATIC_ASSERT | ( | sizeof(pf_t) | = =32 , |
"Unexpected page frame size" | |||
) |
|
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 | ( | ) |
|
static |
|
static |
Definition at line 222 of file paging.c.
Referenced by add_pte(), page_alloc(), and pagetable_create().
|
static |
Definition at line 236 of file paging.c.
Referenced by page_free(), and pgfree_recurse().
|
static |
Definition at line 244 of file paging.c.
Referenced by pagetable_destroy().
|
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 |
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.
[in] | pt | A pointer to the pagetable structure that will hold the page table. |
[in] | vaddr | The virtual address within the new page table where the page table will be mapped. |
[in] | size | Maximum size of the page table in bytes. Must be a multiple of PAGE_SIZE. |
Definition at line 364 of file paging.c.
Referenced by cmd_test_heap().
void pagetable_destroy | ( | pagetable_t * | pt | ) |
Destroy a page table.
[in] | pt | A 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.
[in] | pt | A 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.
[in] | pt | Handle to the page table from which to allocate the page(s). |
[in] | vaddr | The virtual address of the first allocated page. |
[in] | count | The number of contiguous virtual memory pages to allocate. |
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.
[in] | pt | Handle to ehte page table from which to free the page(s). |
[in] | vaddr | The virtual address of the first allocated page. |
[in] | count | The number of contiguous virtual memory pages to free. |
Definition at line 425 of file paging.c.
Referenced by heap_destroy().
|
static |
Definition at line 71 of file paging.c.
Referenced by page_init(), and pagetable_activate().
|
static |