1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
os-k/kaleid/kernel/mm/paging.c
2019-05-15 21:02:16 +02:00

110 lines
2.7 KiB
C

#include <kernel.h>
#include <init/boot.h>
#include <ex/malloc.h>
#include <mm/mm.h>
#define PAGESIZE (4 * KB)
// Page directory pointer offset
typedef ulong pdpe_t;
// Page directory offset
typedef ulong pde_t;
// Page table entry
typedef ulong 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,
MF_HUGE = 1 << 7,
MF_NX = 1 << 31
};
#define RAM_MAX 16
#define NB_4K 150
// * 2 MB
//-----------
volatile pdpe_t MmPML4[512] __attribute__((__aligned__(4096)));
volatile pde_t MmPDP[512] __attribute__((__aligned__(4096)));
volatile pde_t MmPD[512 * RAM_MAX] __attribute__((__aligned__(4096)));;
volatile pte_t MmPT[512 * NB_4K] __attribute__((__aligned__(4096)));;
volatile ulong MmStackGuards[2] = { 0 };
//
// Creates our new page table structure and loads it
void MmInitPaging(void)
{
extern MemoryMap_t memoryMap;
uint phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
memzero((void *)&MmPML4[0], sizeof(MmPML4));
memzero((void *)&MmPDP[0], sizeof(MmPDP));
memzero((void *)&MmPD[0], sizeof(MmPD));
memzero((void *)&MmPT[0], sizeof(MmPT));
for (int i = 0; i < 512 * NB_4K; i++) {
// STACK GUARD PAGE
if ((ulong)i*4096 == (ulong)BtLoaderInfo.stackEndAddr) {
MmPT[i] = ((ulong)i * 4096);
MmStackGuards[0] = i;
continue;
}
// ENOMEM like
if ((ulong)i*4096 > (ulong)phRamSize) {
MmPT[i] = ((ulong)i * 4096) | MF_READWRITE;// | MF_NX;
continue;
}
// STACK GARD PAGE
if ((ulong)i*4096 == (ulong)BtLoaderInfo.kernelEndAddr) {
MmPT[i] = ((ulong)i * 4096);
MmStackGuards[1] = i;
continue;
}
MmPT[i] = ((ulong)i * 4096) | MF_PRESENT | MF_READWRITE;
}
for (int i = 0; i < NB_4K; i++) {
MmPD[i] = (ulong)(&MmPT[i*512])| MF_PRESENT | MF_READWRITE;
}
for (int i = NB_4K; i < 512 * RAM_MAX; i++) {
// ENOMEM like
if ((ulong)i* 2048 * 1024 > (ulong)phRamSize) {
MmPD[i] = ((ulong)i * 2048 * 1024) | MF_READWRITE;// | MF_NX;
continue;
}
MmPD[i] = ((ulong)i * 2048 * 1024) | MF_PRESENT | MF_READWRITE | MF_HUGE;
}
for (int i = 0; i < RAM_MAX; i++) {
MmPDP[i] = (ulong)(&MmPD[i*512])| MF_PRESENT | MF_READWRITE;
}
MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE;
//MmLoadPML4((void *)MmPML4);
}