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

109 lines
2.6 KiB
C
Raw Normal View History

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