os-k/kaleid/kernel/mm/paging.c

66 lines
1.5 KiB
C

#include <kernel.h>
#define PAGESIZE (4 * KB)
#define PAGEALIGNED __attribute__((__aligned__(4096)))
// Page directory pointer offset
typedef uint pdpe_t;
// Page directory offset
typedef uint pde_t;
// Page table entry
typedef uint pte_t;
// paging.asm
void MmLoadPML4(void *);
void MmEnableWriteProtect(void);
void MmDisableWriteProtect(void);
enum
{
MF_PRESENT = 1 << 0,
MF_READWRITE = 1 << 1,
MF_USERMODE = 1 << 2,
MF_WRITETHR = 1 << 3,
MF_CACHEDIS = 1 << 4,
MF_ACCESSED = 1 << 5,
MF_DIRTY = 1 << 6
};
//-----------
pdpe_t pml4[1024] ;
// First PDPE of our pml4
pde_t first_pdpe[1024] PAGEALIGNED;
// First PDP of first_pdpe
pte_t first_pde[1024] PAGEALIGNED;
// First PTE of first_pde
uint first_pte[1024] PAGEALIGNED;
void MmInitPaging(void)
{
size_t i;
// Set all PDPEs to kernel-mode not present
for (i = 0; i < 1024; i++) pml4[i] = MF_READWRITE;
for (i = 0; i < 1024; i++) first_pdpe[i] = MF_READWRITE;
for (i = 0; i < 1024; i++) first_pde[i] = MF_READWRITE;
// Set all pages in first_pte to kernel-mode present
for (i = 0; i < 1024; i++) {
first_pte[i] = (i * PAGESIZE) | (MF_READWRITE | MF_PRESENT);
}
// Install the first PTE
first_pde[0] = (uint)(ulong)first_pte | (MF_READWRITE | MF_PRESENT);
first_pdpe[0] = (uint)(ulong)first_pde | (MF_READWRITE | MF_PRESENT);
pml4[0] = (uint)(ulong)first_pdpe | (MF_READWRITE | MF_PRESENT);
MmLoadPML4(pml4);
}