Working static paging

This commit is contained in:
Adrien Bourmault 2019-05-15 15:55:57 +02:00
parent aaf25bd7e9
commit 2c7967f772
6 changed files with 84 additions and 47 deletions

View File

@ -54,7 +54,7 @@ MB_header:
;;MULTIBOOT POINT ENTRY FOR GRUB -------------------------------------------- ;;
MB_start:
mov esp, KERNEL_STACK ; Setup the stack
mov esp, kernelEnd + 16*1024 ; Setup the stack
push 0 ; Reset EFLAGS
popf
mov [mbInfo], ebx

View File

@ -80,6 +80,8 @@ Go64:
mov ecx, 0xC0000080 ; Address of MSR
rdmsr ; Read MSR
or eax, 1 << 8 ; LME = 1. (Long Mode Enable)
or eax, 1 << 11 ; NXE = 1 (No execute bit)
wrmsr ; Write MSR
;; Enable paging
@ -129,16 +131,16 @@ InitStack:
push rdi
;; Begin address to fill and length
mov qword [newStackEnd], KERNEL_STACK
mov qword [newKernelEnd], kernelEnd
mov rdi, kernelEnd
mov qword [kernelEnd], qword 0xbad0bad
mov rdi, kernelEnd + 16
mov rcx, (KERNEL_STACK - (kernelEnd + 16)) ; The Stack can begin at
; kernelEnd + 16 in order to not overwrite the
; kernel by pushing values (grows downward)
;; Alignes it to 4096o / FUTURE PAGE FRAME
shr rdi, 12
shl rdi, 12
add rdi, 0x1000
;; XXX : align the stack to 16bytes
;; Passing info to kernel
mov qword [newKernelEnd], rdi
mov rcx, KERNEL_STACK ; counter
;; If bit 0 is on, fill one byte
sar rcx, 1 ; Shift bit 0 into CY
@ -151,12 +153,16 @@ InitStack:
stosw
;; We are dword aligned and if bit 2 was on fill another dword
sar rcx, 1 ; Shift bit 2 into CY
jnc $ + 3
sar rcx, 1 ; Shift bit 2 into CY
jnc $ + 3
stosd
;; RCX now equals the number of qwords to fill
repnz stosq ; Finish by writing RCX qwords.
repnz stosq ; Finish by writing RCX qwords.
;; New info for the kernel
mov qword [newStackEnd], rdi
mov rsp, rdi
pop rdi
pop rcx

View File

@ -30,7 +30,7 @@ global newKernelEnd
global newStackEnd
[section .text]
KERNEL_STACK equ (kernelEnd + 16) + 16 * 1024 ; 16KB of stack
KERNEL_STACK equ 16 * 1024 ; 16KB of stack
newKernelEnd dq 0x0
newStackEnd dq 0x0

View File

@ -141,8 +141,8 @@ void *MmGetFirstAvailZone(void *start) {
void *current = 0;
// Because the kernel is the kernel
if ((ulong)start < (ulong)BtLoaderInfo.stackEndAddr+16) {
return MmGetFirstAvailZone(BtLoaderInfo.stackEndAddr+16);
if ((ulong)start < (ulong)BtLoaderInfo.stackEndAddr+4096) {
return MmGetFirstAvailZone(BtLoaderInfo.stackEndAddr+4096);
}
// Search the zone where the start address is

View File

@ -1,4 +1,5 @@
#include <kernel.h>
#include <init/boot.h>
#define PAGESIZE (4 * KB)
@ -26,7 +27,8 @@ enum
MF_CACHEDIS = 1 << 4,
MF_ACCESSED = 1 << 5,
MF_DIRTY = 1 << 6,
MF_HUGE = 1 << 7
MF_HUGE = 1 << 7,
MF_NX = 1 << 31
};
#define RAM 8
@ -34,38 +36,67 @@ enum
//-----------
volatile pdpe_t PML4[512] __attribute__((__aligned__(4096)));
volatile pdpe_t MmPML4[512] __attribute__((__aligned__(4096)));
volatile pde_t PDP[512] __attribute__((__aligned__(4096)));
volatile pde_t MmPDP[512] __attribute__((__aligned__(4096)));
volatile pde_t PD[512 * RAM] __attribute__((__aligned__(4096)));
volatile pde_t MmPD[512 * RAM] __attribute__((__aligned__(4096)));
volatile pte_t PT[512 * NB_4K] __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)
{
memzero((void *)&PML4[0], sizeof(PML4));
memzero((void *)&PDP[0], sizeof(PDP));
memzero((void *)&PD[0], sizeof(PD));
memzero((void *)&PT[0], sizeof(PT));
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++) {
PT[i] = ((ulong)i * 4096) | MF_PRESENT | MF_READWRITE;
// STACK GUARD PAGE
if ((ulong)i*4096 == (ulong)BtLoaderInfo.stackEndAddr) {
MmPT[i] = ((ulong)i * 4096) | MF_PRESENT;
MmStackGuards[0] = i;
continue;
}
// STACK PAGES
if (
(ulong)i*4096 < (ulong)BtLoaderInfo.stackEndAddr &&
(ulong)i*4096 > (ulong)BtLoaderInfo.kernelEndAddr
) {
MmPT[i] = ((ulong)i * 4096) | MF_PRESENT | MF_READWRITE;// | MF_NX;
continue;
}
// STACK GARD PAGE
if ((ulong)i*4096 == (ulong)BtLoaderInfo.kernelEndAddr) {
MmPT[i] = ((ulong)i * 4096) | MF_PRESENT;
MmStackGuards[1] = i;
continue;
}
MmPT[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;
MmPD[i] = (ulong)(&MmPT[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;
MmPD[i] = ((ulong)i * 2048 * 1024) | MF_PRESENT | MF_READWRITE | MF_HUGE;
}
for (int i = 0; i < RAM; i++) {
PDP[i] = (ulong)(&PD[i*512])| MF_PRESENT | MF_READWRITE;
MmPDP[i] = (ulong)(&MmPD[i*512])| MF_PRESENT | MF_READWRITE;
}
PML4[0] = (ulong)(&PDP[0])| MF_PRESENT | MF_READWRITE;
MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE;
MmLoadPML4((void *)PML4);
MmLoadPML4((void *)MmPML4);
}

View File

@ -146,7 +146,7 @@ error_t CmdMemUsage(int argc, char **argv, char *cmdline)
KernLog("Kernel stack\n");
KernLog("\tstarts at:\t\t%p (%4luMB + %4luKB + %4luB)\n",
BtLoaderInfo.kernelEndAddr,
BtLoaderInfo.stackEndAddr,
_ADDR_TO_MB((size_t)BtLoaderInfo.stackEndAddr),
_ADDR_TO_KB((size_t)BtLoaderInfo.stackEndAddr),
_ADDR_TO_B((size_t)BtLoaderInfo.stackEndAddr));
@ -229,9 +229,9 @@ error_t CmdPF(int argc, char **argv, char *cmdline)
}
extern void KeStartShell(void);
error_t CmdShell(int argc, char **argv, char *cmdline)
error_t CmdStackOverflow(int argc, char **argv, char *cmdline)
{
KeStartShell();
CmdStackOverflow(0, 0, 0);
return EOK;
}
@ -239,20 +239,20 @@ error_t CmdShell(int argc, char **argv, char *cmdline)
Command_t cmdtable[] =
{
{ "beep", CmdBeep, "Make a beep" },
{ "cls", CmdClear, "Clears standard output" },
{ "date", CmdDate, "Print date" },
{ "die", CmdDie, "Die painfully" },
{ "exit", CmdQuit, "Initiate shutdown" },
{ "help", CmdHelp, "Show this message" },
{ "march", CmdStarWars, "Play the Imperial March"},
{ "mmap", CmdMemMap, "Show memory map" },
{ "musage", CmdMemUsage, "Show memory statistics" },
{ "pfault", CmdPF, "Provokes a PF" },
{ "pstest", CmdPsTest, "Scheduler test routine" },
{ "quit", CmdQuit, "Alias for 'exit'" },
{ "shell", CmdShell, "New shell instance" },
{ "time", CmdTime, "Print time" },
{ "beep", CmdBeep, "Make a beep" },
{ "cls", CmdClear, "Clears standard output" },
{ "date", CmdDate, "Print date" },
{ "die", CmdDie, "Die painfully" },
{ "exit", CmdQuit, "Initiate shutdown" },
{ "help", CmdHelp, "Show this message" },
{ "march", CmdStarWars, "Play the Imperial March"},
{ "mmap", CmdMemMap, "Show memory map" },
{ "musage", CmdMemUsage, "Show memory statistics" },
{ "pfault", CmdPF, "Provoke a PF" },
{ "pstest", CmdPsTest, "Scheduler test routine" },
{ "quit", CmdQuit, "Alias for 'exit'" },
{ "so", CmdStackOverflow, "Provoke a stack overflow" },
{ "time", CmdTime, "Print time" },
{ NULL, NULL, NULL }
};