Some stuff

This commit is contained in:
Adrien Bourmault 2019-05-18 13:39:58 +02:00
parent a389517877
commit 6ee78caa4d
5 changed files with 111 additions and 5 deletions

View File

@ -110,7 +110,7 @@ void BtInitBootInfo(multiboot_info_t *mbi, void *codeSeg)
void BtDoSanityChecks(uint mbMagic) {
if (!(mbMagic == MULTIBOOT_BOOTLOADER_MAGIC))
KeStartPanic("\tMagic number %x is incorrect\n", mbMagic);
KeStartPanic("Magic number %x is incorrect\n", mbMagic);
DebugLog("\tKernel successfully loaded at %p\n",
BtLoaderInfo.kernelAddr);

View File

@ -25,6 +25,7 @@
#include "init.h"
void MmInitPaging(void);
void MmActivatePageHandler(void);
//
// Entry point of the Kaleid kernel
@ -58,6 +59,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg)
// Start drivers
KeEnableRTC();
IoEnableKeyb();
MmActivatePageHandler();
KeStartShell();

View File

@ -41,7 +41,7 @@ void MmInitMemoryMap(void)
rc = InitMemoryMap();
if (rc)
KeStartPanic("\tThe memory map failed to initialize.\nError : %s",
KeStartPanic("Failed to initialize the memory map\nError : %s",
strerror(rc) );
}

View File

@ -2,6 +2,7 @@
#include <init/boot.h>
#include <ex/malloc.h>
#include <mm/mm.h>
#include <ke/idt.h>
#define KPAGESIZE (4 * KB)
#define UPAGESIZE (2 * MB)
@ -52,6 +53,7 @@ volatile ulong MmStackGuards[2] = { 0 };
//
// Creates our new page table structure and loads it
//
void MmInitPaging(void)
{
extern MemoryMap_t memoryMap;
@ -106,5 +108,107 @@ void MmInitPaging(void)
MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE;
MmLoadPML4((void *)MmPML4);
DebugLog("\tPaging table initialized at %p, %p, %p, %p\n", &MmPML4, &MmPDP, &MmPD, &MmPT);
DebugLog("\tPaging tables initialized at %p, %p\n", &MmPD, &MmPT);
}
//
// Reloads the page tables
//
void MmReloadPaging(void)
{
extern MemoryMap_t memoryMap;
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
for (volatile ulong i = 0; i < 512 * NB_4K; i++) {
// STACK GUARD PAGE
if ((ulong)(i*KPAGESIZE) == (ulong)BtLoaderInfo.stackEndAddr) {
MmPT[i] = ((ulong)(i*KPAGESIZE));
MmStackGuards[0] = i;
continue;
}
// ENOMEM like
if ((ulong)(i*KPAGESIZE) > (ulong)phRamSize) {
break;
}
// STACK GARD PAGE
if ((ulong)(i*KPAGESIZE) == (ulong)BtLoaderInfo.kernelEndAddr) {
MmPT[i] = ((ulong)(i*KPAGESIZE));
MmStackGuards[1] = i;
continue;
}
MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_READWRITE;
}
for (volatile ulong i = 0; i < NB_4K; i++) {
MmPD[i] = (ulong)(&MmPT[i*512])| MF_PRESENT | MF_READWRITE;
}
for (volatile ulong i = NB_4K; i < 512 * RAM_MAX; i++) {
// ENOMEM like
if ((ulong)(i* UPAGESIZE) > (ulong)phRamSize) {
break;
}
MmPD[i] = 0;
MmPD[i] = ((ulong)(i* UPAGESIZE)) | MF_PRESENT | MF_READWRITE | MF_HUGE;
}
DebugLog("Paging tables reloaded at %p, %p\n", &MmPD, &MmPT);
}
//
// Page fault handler
//
static void PagingHandler(ISRFrame_t *regs)
{
KeStartPanic("[ISR 0x%x] Irrecoverable Kernel Page Fault at %p\n\n"
" Error code : 0x%x (%b)\n\n"
" RIP: %#016lx CS: %#016lx RSP: %#016lx\n"
" SS: %#016lx RAX: %#016lx RBX: %#016lx\n"
" RCX: %#016lx RDX: %#016lx RSI: %#016lx\n"
" RDI: %#016lx RBP: %#016lx R8: %#016lx\n"
" R9: %#016lx R10: %#016lx R11: %#016lx\n"
" R12: %#016lx R13: %#016lx R14: %#016lx\n"
" R15: %#016lx CR0: %#016lx CR2: %#016lx\n"
" CR3: %#016lx CR4: %#016lx CR8: %#016lx\n"
" RFLAGS: %#022b (%#06x)",
regs->intNo,
regs->regs[1],
regs->ErrorCode,
regs->ErrorCode,
regs->rip,
regs->cs,
regs->rsp,
regs->ss,
regs->regs[5],
regs->regs[6],
regs->regs[7],
regs->regs[8],
regs->regs[9],
regs->regs[10],
regs->regs[11],
regs->regs[12],
regs->regs[13],
regs->regs[14],
regs->regs[15],
regs->regs[16],
regs->regs[17],
regs->regs[18],
regs->regs[19],
regs->regs[0],
regs->regs[1],
regs->regs[2],
regs->regs[3],
regs->regs[4],
regs->rflags,
regs->rflags
);
}
void MmActivatePageHandler(void)
{
KeRegisterISR(PagingHandler, 0xe);
}

View File

@ -240,11 +240,11 @@ error_t CmdPF(int argc, char **argv, char *cmdline)
return EOK;
}
void MmInitPaging(void);
void MmReloadPaging(void);
error_t CmdReloadPage(int argc, char **argv, char *cmdline)
{
MmInitPaging();
MmReloadPaging();
return EOK;
}