1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
This commit is contained in:
Julian Barathieu 2019-05-18 22:14:08 +02:00
commit 58b896ea2a
11 changed files with 200 additions and 64 deletions

View File

@ -110,8 +110,28 @@ struct CpuInfo_t
}; };
struct ISRFrame_t { struct ISRFrame_t {
/* The register file */ /* The registers */
ulong regs[20]; ulong efer;
ulong cr0;
ulong cr2;
ulong cr3;
ulong cr4;
ulong cr8;
ulong r15;
ulong r14;
ulong r13;
ulong r12;
ulong r11;
ulong r10;
ulong r9;
ulong r8;
ulong rbp;
ulong rdi;
ulong rsi;
ulong rdx;
ulong rcx;
ulong rbx;
ulong rax;
/* The error code and interrupt id */ /* The error code and interrupt id */
ulong intNo; ulong intNo;

View File

@ -132,7 +132,7 @@ void MmInitGdt(void);
// //
// Loads the descriptor table // Loads the descriptor table
// //
extern void MmLoadGdt(ulong ds, ulong cs, ulong tr); extern void MmLoadGdt();
// //

View File

@ -110,7 +110,7 @@ void BtInitBootInfo(multiboot_info_t *mbi, void *codeSeg)
void BtDoSanityChecks(uint mbMagic) { void BtDoSanityChecks(uint mbMagic) {
if (!(mbMagic == MULTIBOOT_BOOTLOADER_MAGIC)) 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", DebugLog("\tKernel successfully loaded at %p\n",
BtLoaderInfo.kernelAddr); BtLoaderInfo.kernelAddr);

View File

@ -26,6 +26,7 @@
#include <io/vga.h> #include <io/vga.h>
void MmInitPaging(void); void MmInitPaging(void);
void MmActivatePageHandler(void);
// //
// Entry point of the Kaleid kernel // Entry point of the Kaleid kernel
@ -49,6 +50,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg)
// Memory // Memory
MmInitMemoryMap(); MmInitMemoryMap();
//MmInitGdt();
MmInitPaging(); MmInitPaging();
MmInitHeap(); MmInitHeap();
@ -59,6 +61,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg)
// Start drivers // Start drivers
KeEnableRTC(); KeEnableRTC();
IoEnableKeyb(); IoEnableKeyb();
MmActivatePageHandler();
KernLog("%CThis %Cis %Ca %CColor %Ctest%C...%C\n", KernLog("%CThis %Cis %Ca %CColor %Ctest%C...%C\n",
VGA_COLOR_LIGHT_BLUE, VGA_COLOR_LIGHT_BLUE,

View File

@ -175,7 +175,7 @@ void KeSetupIDT(void)
// Load IDT // Load IDT
KeLoadIDT(); KeLoadIDT();
DebugLog("\tInterrupt table initialized\n"); DebugLog("\tInterrupt table initialized at %p\n", _KeIdtPtr.base);
} }
// //
@ -277,43 +277,50 @@ static void EarlyExceptionHandler(ISRFrame_t *regs)
{ {
KeStartPanic("[ISR 0x%x] Irrecoverable Kernel %s\n\n" KeStartPanic("[ISR 0x%x] Irrecoverable Kernel %s\n\n"
" Error code : 0x%x (%b)\n\n" " Error code : 0x%x (%b)\n\n"
" RIP: %#016lx CS: %#016lx RSP: %#016lx\n"
" SS: %#016lx RAX: %#016lx RBX: %#016lx\n" " RIP: %#016lx RSP: %#016lx RBP: %#016lx\n\n"
" RCX: %#016lx RDX: %#016lx RSI: %#016lx\n"
" RDI: %#016lx RBP: %#016lx R8: %#016lx\n" " SS: %#016lx CS: %#016lx CR0: %#016lx\n"
" R9: %#016lx R10: %#016lx R11: %#016lx\n" " CR2: %#016lx CR3: %#016lx CR4: %#016lx\n"
" R12: %#016lx R13: %#016lx R14: %#016lx\n" " CR8: %#016lx EFE: %#016lx \n\n"
" R15: %#016lx CR0: %#016lx CR2: %#016lx\n"
" CR3: %#016lx CR4: %#016lx CR8: %#016lx\n" " RAX: %#016lx RBX: %#016lx RCX: %#016lx\n"
" RDX: %#016lx RSI: %#016lx RDI: %#016lx\n"
" R8: %#016lx R9: %#016lx R10: %#016lx\n"
" R11: %#016lx R12: %#016lx R13: %#016lx\n"
" R14: %#016lx R15: %#016lx \n\n"
" RFLAGS: %#022b (%#06x)", " RFLAGS: %#022b (%#06x)",
regs->intNo, regs->intNo,
ExceptionsChar[regs->intNo], ExceptionsChar[regs->intNo],
regs->ErrorCode, regs->ErrorCode,
regs->ErrorCode, regs->ErrorCode,
regs->rip, regs->rip,
regs->cs,
regs->rsp, regs->rsp,
regs->rbp,
regs->ss, regs->ss,
regs->regs[5], regs->cs,
regs->regs[6], regs->cr0,
regs->regs[7], regs->cr2,
regs->regs[8], regs->cr3,
regs->regs[9], regs->cr4,
regs->regs[10], regs->cr8,
regs->regs[11], regs->efer,
regs->regs[12], regs->rax,
regs->regs[13], regs->rbx,
regs->regs[14], regs->rcx,
regs->regs[15], regs->rdx,
regs->regs[16], regs->rsi,
regs->regs[17], regs->rdi,
regs->regs[18], regs->r8,
regs->regs[19], regs->r9,
regs->regs[0], regs->r10,
regs->regs[1], regs->r11,
regs->regs[2], regs->r12,
regs->regs[3], regs->r13,
regs->regs[4], regs->r14,
regs->r15,
regs->rflags, regs->rflags,
regs->rflags regs->rflags
); );

View File

@ -53,6 +53,9 @@ isrPreHandler:
push rax push rax
mov rax, cr0 mov rax, cr0
push rax push rax
mov rcx, 0xC0000080
rdmsr
push rax
; Check if we are switching from user mode to supervisor mode ; Check if we are switching from user mode to supervisor mode
mov rax, [rsp + 152] mov rax, [rsp + 152]
@ -84,7 +87,7 @@ isrPreHandler:
.SExit: .SExit:
; pop the control registers ; pop the control registers
add rsp, 40 add rsp, 48
popAll popAll
; pop the error code and interrupt id ; pop the error code and interrupt id

View File

@ -33,24 +33,13 @@ global MmStoreGdt
;; Loads the GDT ;; Loads the GDT
;; ;;
MmLoadGdt: MmLoadGdt:
;; Loading the gdt via the gdtPtr pointer ;; Loading the gdt via the gdtPtr pointer
lgdt [rel gdtPtr] lgdt [gdtPtr]
;; Reloading the segment registers
mov ax, si ; data segment
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov cs, di ; code segment
;; We must far jump because we changed the GDT ;; We must far jump because we changed the GDT
lea rax, [rel .next] lea rax, [.next]
push rax push rax
ret ret
.next: .next:
;ltr dx ; the TSS ;ltr dx ; the TSS
ret ret

View File

@ -76,7 +76,7 @@ void MmInitGdt(void)
/* SetTssEntry(2, (ulong)&tssEntry, sizeof(TssEntry_t)); */ /* SetTssEntry(2, (ulong)&tssEntry, sizeof(TssEntry_t)); */
/* MmLoadGdt(0x8, 0x0, 2 << 3); */ MmLoadGdt();
} }

View File

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

View File

@ -2,8 +2,10 @@
#include <init/boot.h> #include <init/boot.h>
#include <ex/malloc.h> #include <ex/malloc.h>
#include <mm/mm.h> #include <mm/mm.h>
#include <ke/idt.h>
#define PAGESIZE (4 * KB) #define KPAGESIZE (4 * KB)
#define UPAGESIZE (2 * MB)
// Page directory pointer offset // Page directory pointer offset
@ -39,18 +41,19 @@ enum
//----------- //-----------
volatile pdpe_t MmPML4[512] __attribute__((__aligned__(4096))); volatile pdpe_t MmPML4[512] __attribute__((__aligned__(KPAGESIZE)));
volatile pde_t MmPDP[512] __attribute__((__aligned__(4096))); volatile pde_t MmPDP[512] __attribute__((__aligned__(KPAGESIZE)));
volatile pde_t MmPD[512 * RAM_MAX] __attribute__((__aligned__(4096)));; volatile pde_t MmPD[512 * RAM_MAX] __attribute__((__aligned__(KPAGESIZE)));;
volatile pte_t MmPT[512 * NB_4K] __attribute__((__aligned__(4096)));; volatile pte_t MmPT[512 * NB_4K] __attribute__((__aligned__(KPAGESIZE)));;
volatile ulong MmStackGuards[2] = { 0 }; volatile ulong MmStackGuards[2] = { 0 };
// //
// Creates our new page table structure and loads it // Creates our new page table structure and loads it
//
void MmInitPaging(void) void MmInitPaging(void)
{ {
extern MemoryMap_t memoryMap; extern MemoryMap_t memoryMap;
@ -63,25 +66,25 @@ void MmInitPaging(void)
for (volatile ulong i = 0; i < 512 * NB_4K; i++) { for (volatile ulong i = 0; i < 512 * NB_4K; i++) {
// STACK GUARD PAGE // STACK GUARD PAGE
if ((ulong)(i*4096) == (ulong)BtLoaderInfo.stackEndAddr) { if ((ulong)(i*KPAGESIZE) == (ulong)BtLoaderInfo.stackEndAddr) {
MmPT[i] = ((ulong)(i*4096)); MmPT[i] = ((ulong)(i*KPAGESIZE));
MmStackGuards[0] = i; MmStackGuards[0] = i;
continue; continue;
} }
// ENOMEM like // ENOMEM like
if ((ulong)(i*4096) > (ulong)phRamSize) { if ((ulong)(i*KPAGESIZE) > (ulong)phRamSize) {
break; break;
} }
// STACK GARD PAGE // STACK GARD PAGE
if ((ulong)(i*4096) == (ulong)BtLoaderInfo.kernelEndAddr) { if ((ulong)(i*KPAGESIZE) == (ulong)BtLoaderInfo.kernelEndAddr) {
MmPT[i] = ((ulong)(i*4096)); MmPT[i] = ((ulong)(i*KPAGESIZE));
MmStackGuards[1] = i; MmStackGuards[1] = i;
continue; continue;
} }
MmPT[i] = ((ulong)(i*4096)) | MF_PRESENT | MF_READWRITE; MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_READWRITE;
} }
for (volatile ulong i = 0; i < NB_4K; i++) { for (volatile ulong i = 0; i < NB_4K; i++) {
@ -90,12 +93,12 @@ void MmInitPaging(void)
for (volatile ulong i = NB_4K; i < 512 * RAM_MAX; i++) { for (volatile ulong i = NB_4K; i < 512 * RAM_MAX; i++) {
// ENOMEM like // ENOMEM like
if ((ulong)(i* 2048 * 1024) > (ulong)phRamSize) { if ((ulong)(i* UPAGESIZE) > (ulong)phRamSize) {
break; break;
} }
MmPD[i] = 0; MmPD[i] = 0;
MmPD[i] = ((ulong)(i* 2048 * 1024)) | MF_PRESENT | MF_READWRITE | MF_HUGE; MmPD[i] = ((ulong)(i* UPAGESIZE)) | MF_PRESENT | MF_READWRITE | MF_HUGE;
} }
for (volatile int i = 0; i < RAM_MAX; i++) { for (volatile int i = 0; i < RAM_MAX; i++) {
@ -105,4 +108,114 @@ void MmInitPaging(void)
MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE; MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE;
MmLoadPML4((void *)MmPML4); MmLoadPML4((void *)MmPML4);
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 RSP: %#016lx RBP: %#016lx\n\n"
" SS: %#016lx CS: %#016lx CR0: %#016lx\n"
" CR2: %#016lx CR3: %#016lx CR4: %#016lx\n"
" CR8: %#016lx EFE: %#016lx \n\n"
" RAX: %#016lx RBX: %#016lx RCX: %#016lx\n"
" RDX: %#016lx RSI: %#016lx RDI: %#016lx\n"
" R8: %#016lx R9: %#016lx R10: %#016lx\n"
" R11: %#016lx R12: %#016lx R13: %#016lx\n"
" R14: %#016lx R15: %#016lx \n\n"
" RFLAGS: %#022b (%#06x)",
regs->intNo,
regs->cr2,
regs->ErrorCode,
regs->ErrorCode,
regs->rip,
regs->rsp,
regs->rbp,
regs->ss,
regs->cs,
regs->cr0,
regs->cr2,
regs->cr3,
regs->cr4,
regs->cr8,
regs->efer,
regs->rax,
regs->rbx,
regs->rcx,
regs->rdx,
regs->rsi,
regs->rdi,
regs->r8,
regs->r9,
regs->r10,
regs->r11,
regs->r12,
regs->r13,
regs->r14,
regs->r15,
regs->rflags,
regs->rflags
);
}
void MmActivatePageHandler(void)
{
KeRegisterISR(PagingHandler, 0xe);
} }

View File

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