2019-05-14 14:39:35 +02:00
|
|
|
#include <kernel.h>
|
|
|
|
|
|
|
|
#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,
|
|
|
|
MF_HUGE = 1 << 7
|
2019-05-14 14:39:35 +02:00
|
|
|
};
|
|
|
|
|
2019-05-15 02:26:55 +02:00
|
|
|
#define RAM 8
|
2019-05-15 11:15:21 +02:00
|
|
|
#define NB_4K 2
|
2019-05-15 02:26:55 +02:00
|
|
|
|
2019-05-14 14:39:35 +02:00
|
|
|
//-----------
|
|
|
|
|
2019-05-15 02:26:55 +02:00
|
|
|
volatile pdpe_t PML4[512] __attribute__((__aligned__(4096)));
|
2019-05-14 14:39:35 +02:00
|
|
|
|
2019-05-15 02:26:55 +02:00
|
|
|
volatile pde_t PDP[512] __attribute__((__aligned__(4096)));
|
2019-05-14 14:39:35 +02:00
|
|
|
|
2019-05-15 11:15:21 +02:00
|
|
|
volatile pde_t PD[512 * RAM] __attribute__((__aligned__(4096)));
|
2019-05-14 14:39:35 +02:00
|
|
|
|
2019-05-15 11:15:21 +02:00
|
|
|
volatile pte_t PT[512 * NB_4K] __attribute__((__aligned__(4096)));
|
2019-05-14 14:39:35 +02:00
|
|
|
|
|
|
|
void MmInitPaging(void)
|
|
|
|
{
|
2019-05-15 02:26:55 +02:00
|
|
|
memzero((void *)&PML4[0], sizeof(PML4));
|
|
|
|
memzero((void *)&PDP[0], sizeof(PDP));
|
|
|
|
memzero((void *)&PD[0], sizeof(PD));
|
2019-05-15 11:15:21 +02:00
|
|
|
memzero((void *)&PT[0], sizeof(PT));
|
2019-05-15 02:26:55 +02:00
|
|
|
|
2019-05-15 11:15:21 +02:00
|
|
|
for (int i = 0; i < 512 * NB_4K; i++) {
|
|
|
|
PT[i] = ((ulong)i * 4096) | MF_PRESENT | MF_READWRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < NB_4K; i++) {
|
|
|
|
PD[i] = (ulong)(&PT[i*512])| MF_PRESENT | MF_READWRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = NB_4K; i < 512 * RAM; i++) {
|
|
|
|
PD[i] = ((ulong)i * 2048 * 1024) | MF_PRESENT | MF_READWRITE | MF_HUGE;
|
2019-05-14 14:39:35 +02:00
|
|
|
}
|
|
|
|
|
2019-05-15 02:26:55 +02:00
|
|
|
for (int i = 0; i < RAM; i++) {
|
|
|
|
PDP[i] = (ulong)(&PD[i*512])| MF_PRESENT | MF_READWRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PML4[0] = (ulong)(&PDP[0])| MF_PRESENT | MF_READWRITE;
|
2019-05-14 14:39:35 +02:00
|
|
|
|
2019-05-15 02:26:55 +02:00
|
|
|
MmLoadPML4((void *)PML4);
|
|
|
|
}
|