From 7cc39e37dcc98af80a7844aa208a7b76109ca6fb Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sun, 15 Dec 2019 23:54:47 +0100 Subject: [PATCH 01/28] [BUG] Work on null vector rejection --- include/mm/mm.h | 2 -- kaleid/kernel/mm/paging.c | 60 ++----------------------------------- kaleid/kernel/sh/testcmds.c | 7 ----- 3 files changed, 3 insertions(+), 66 deletions(-) diff --git a/include/mm/mm.h b/include/mm/mm.h index 8bd9238..dcb34b1 100644 --- a/include/mm/mm.h +++ b/include/mm/mm.h @@ -160,8 +160,6 @@ extern void MmStoreGdt(void); void MmInitPaging(void); -void MmReloadPaging(void); - void MmActivatePageHandler(void); // diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 7b9a93c..c971178 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -71,6 +71,9 @@ void MmInitPaging(void) MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_READWRITE; } + // NULL VECTOR + MmPT[0] = ((ulong)(0*KPAGESIZE)); + for (volatile ulong i = 0; i < NB_4K; i++) { MmPD[i] = (ulong)(&MmPT[i*512])| MF_PRESENT | MF_READWRITE; } @@ -96,63 +99,6 @@ void MmInitPaging(void) //DebugLog("\tStack Guards at %p, %p\n", MmStackGuards[0], MmStackGuards[1]); } -// -// 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] = ((ulong)(i*KPAGESIZE)); - 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] = ((ulong)(i*KPAGESIZE)); - 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; - } - - for (volatile int i = 0; i < RAM_MAX; i++) { - MmPDP[i] = (ulong)(&MmPD[i*512])| MF_PRESENT | MF_READWRITE; - } - - MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE; - - MmLoadPML4((void *)MmPML4); - DebugLog("\tPaging tables initialized at %p, %p\n", &MmPD, &MmPT); - DebugLog("\tStack Guards at %p, %p\n", MmStackGuards[0], MmStackGuards[1]); -} - // Returns the rank of the Stack Guards void *MmGetStackGuards(char rank) { diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 50661fc..70ca0ac 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -153,12 +153,6 @@ error_t CmdPF(int argc, char **argv, char *cmdline) return EOK; } -error_t CmdReloadPage(int argc, char **argv, char *cmdline) -{ - MmReloadPaging(); - return EOK; -} - error_t CmdShell(int argc, char **argv, char *cmdline) { ShStartShell(); @@ -203,7 +197,6 @@ static Command_t testcmdtable[] = { "help", CmdHelpTest, "Show this message" }, { "div", CmdFloatDiv, "Float div. Usage : div a b. Returns a/b"}, { "pf", CmdPF, "Provoke a PF. Usage: pfault
"}, - { "rpag", CmdReloadPage, "Reload the pages directory" }, { "shell", CmdShell, "Start a new shell (nested)", }, { "stkov", CmdStackOverflow, "Provoke a stack overflow" }, { "stkun", CmdStackUnderflow, "Provoke a stack underflow" }, From 1de74e599b97a9a91ed2e8a1799011faafebcef4 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 20 Dec 2019 22:11:22 +0100 Subject: [PATCH 02/28] Now impossible to dereference a null vector in user mode --- kaleid/kernel/mm/paging.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index c971178..2f161ea 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -71,9 +71,6 @@ void MmInitPaging(void) MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_READWRITE; } - // NULL VECTOR - MmPT[0] = ((ulong)(0*KPAGESIZE)); - for (volatile ulong i = 0; i < NB_4K; i++) { MmPD[i] = (ulong)(&MmPT[i*512])| MF_PRESENT | MF_READWRITE; } From b21f82cbdbda66a40e667263378d0f54eeea8468 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 21 Dec 2019 12:40:53 +0100 Subject: [PATCH 03/28] Some buggy stuff about optimization of PIT --- include/ke/time.h | 6 +++--- kaleid/kernel/ke/pit.c | 16 ++++++++-------- kaleid/kernel/sh/testcmds.c | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/include/ke/time.h b/include/ke/time.h index b205289..1d7840f 100644 --- a/include/ke/time.h +++ b/include/ke/time.h @@ -61,9 +61,9 @@ char *KeFormatCurTime(void); void KeSetCurTime(Time_t); void KeEnablePIT(void); -void KeSleep(uint); -Timer_t *KeSetTimer(uint delay); -int KeGetTimer(Timer_t*); +void KeSleep(uint volatile); +Timer_t *KeSetTimer(uint volatile delay); +int KeGetTimer(Timer_t volatile *); //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/ke/pit.c b/kaleid/kernel/ke/pit.c index db3bfda..c20dad7 100644 --- a/kaleid/kernel/ke/pit.c +++ b/kaleid/kernel/ke/pit.c @@ -29,10 +29,10 @@ #define COUNTDONE 1 #define PIT_FREQUENCY 1000 // Hz = 1ms -static Timer_t Timer[20]; //20 concurrent sleep max -static ulong Ticks = 0; -static Time_t CurTime; -static char TimeFmtBuf[22] = { 0 }; +static Timer_t volatile Timer[20]; //20 concurrent sleep max +static ulong volatile Ticks = 0; +static Time_t volatile CurTime; +static char volatile TimeFmtBuf[22] = { 0 }; // // ISR handler for the Programmable Interval Timer @@ -78,11 +78,11 @@ void KeSleep(uint delay) timerBlock->sema = 0; } -Timer_t *KeSetTimer(uint delay) +Timer_t *KeSetTimer(uint volatile delay) { - Timer_t *timerBlock; + Timer_t volatile *timerBlock; - if ((timerBlock = (Timer_t*)KeFindTimerBlock()) == NULL) + if ((timerBlock = (Timer_t volatile*)KeFindTimerBlock()) == NULL) return NULL; timerBlock->countDown = delay * PIT_FREQUENCY / 1000; @@ -90,7 +90,7 @@ Timer_t *KeSetTimer(uint delay) return timerBlock; } -int KeGetTimer(Timer_t *timerBlock) +int KeGetTimer(Timer_t volatile *timerBlock) { if (!(timerBlock->sema)) { return 0; diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 50661fc..d2cf21f 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -55,6 +55,19 @@ error_t CmdArgs(int argc, char **argv, char *cmdline) return EOK; } +error_t CmdAtoi(int argc, char **argv, char *cmdline) +{ + int i; + + KernLog("cmdline: '%s'\nargc: %d\n", cmdline, argc); + + for (i = 0; i < argc; i++) { + KernLog("argv[%d]: '%u'\n", i, atoi(argv[i])); + } + + return EOK; +} + error_t CmdDumpATASect(int argc, char **argv, char *cmdline) { char sector[512] = {0}; @@ -199,6 +212,7 @@ error_t CmdTimerTest(int argc, char **argv, char *cmdline) static Command_t testcmdtable[] = { { "args", CmdArgs, "Print command line" }, + { "atoi", CmdAtoi, "Print command line atoised" }, { "dmpsec", CmdDumpATASect, "Dump an ATA sector on screen" }, { "help", CmdHelpTest, "Show this message" }, { "div", CmdFloatDiv, "Float div. Usage : div a b. Returns a/b"}, From 7b35afb2912e59c93e232f85b03343509bd7e6af Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 21 Dec 2019 12:45:54 +0100 Subject: [PATCH 04/28] No more optimization (for instance) for PIT ISR --- include/ke/time.h | 6 +++--- kaleid/kernel/ke/pit.c | 26 ++++++++++++++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/include/ke/time.h b/include/ke/time.h index 1d7840f..b205289 100644 --- a/include/ke/time.h +++ b/include/ke/time.h @@ -61,9 +61,9 @@ char *KeFormatCurTime(void); void KeSetCurTime(Time_t); void KeEnablePIT(void); -void KeSleep(uint volatile); -Timer_t *KeSetTimer(uint volatile delay); -int KeGetTimer(Timer_t volatile *); +void KeSleep(uint); +Timer_t *KeSetTimer(uint delay); +int KeGetTimer(Timer_t*); //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/ke/pit.c b/kaleid/kernel/ke/pit.c index c20dad7..a9d961c 100644 --- a/kaleid/kernel/ke/pit.c +++ b/kaleid/kernel/ke/pit.c @@ -29,14 +29,17 @@ #define COUNTDONE 1 #define PIT_FREQUENCY 1000 // Hz = 1ms -static Timer_t volatile Timer[20]; //20 concurrent sleep max -static ulong volatile Ticks = 0; -static Time_t volatile CurTime; -static char volatile TimeFmtBuf[22] = { 0 }; +static Timer_t Timer[20]; //20 concurrent sleep max +static ulong Ticks = 0; +static Time_t CurTime; +static char TimeFmtBuf[22] = { 0 }; // // ISR handler for the Programmable Interval Timer // + +#pragma GCC push_options +#pragma GCC optimize ("O0") static void HandlePIT(ISRFrame_t *regs) { Ticks++; @@ -52,6 +55,7 @@ static void HandlePIT(ISRFrame_t *regs) KeSendEOItoPIC(0x28); } } +#pragma GCC pop_options static Timer_t* KeFindTimerBlock(void) { @@ -63,6 +67,8 @@ static Timer_t* KeFindTimerBlock(void) return NULL; } +#pragma GCC push_options +#pragma GCC optimize ("O0") void KeSleep(uint delay) { Timer_t *timerBlock; @@ -77,20 +83,24 @@ void KeSleep(uint delay) } timerBlock->sema = 0; } +#pragma GCC pop_options -Timer_t *KeSetTimer(uint volatile delay) +#pragma GCC push_options +#pragma GCC optimize ("O0") +Timer_t *KeSetTimer(uint delay) { - Timer_t volatile *timerBlock; + Timer_t *timerBlock; - if ((timerBlock = (Timer_t volatile*)KeFindTimerBlock()) == NULL) + if ((timerBlock = (Timer_t*)KeFindTimerBlock()) == NULL) return NULL; timerBlock->countDown = delay * PIT_FREQUENCY / 1000; return timerBlock; } +#pragma GCC pop_options -int KeGetTimer(Timer_t volatile *timerBlock) +int KeGetTimer(Timer_t *timerBlock) { if (!(timerBlock->sema)) { return 0; From ca2c676df9ab786288ae5ec6779e34a4d1b80bef Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 21 Dec 2019 13:55:02 +0100 Subject: [PATCH 05/28] Working on page protection --- build/kernel.ld | 7 +++++-- kaleid/kernel/io/spkr.c | 8 ++++---- kaleid/kernel/mm/paging.c | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/build/kernel.ld b/build/kernel.ld index 0840bf2..bcc86e2 100644 --- a/build/kernel.ld +++ b/build/kernel.ld @@ -38,13 +38,16 @@ SECTIONS { .text ALIGN (0x1000) : { + _text = .; *(.text) + _text_end = .; } .data ALIGN (0x1000) : { _data = .; *(.data) + _data_end = .; } .eh_frame ALIGN (0x1000) : @@ -55,7 +58,9 @@ SECTIONS { .rodata ALIGN (0x1000) : { + _rodata = .; *(.rodata) + _rodata_end = .; } .bss ALIGN (0x1000) : @@ -70,5 +75,3 @@ SECTIONS { kernelEnd = .; } - - diff --git a/kaleid/kernel/io/spkr.c b/kaleid/kernel/io/spkr.c index dff5d21..bc0450e 100644 --- a/kaleid/kernel/io/spkr.c +++ b/kaleid/kernel/io/spkr.c @@ -100,10 +100,10 @@ void IoDoStarWars(void) //bprintf(BStdOut, "\n"); - for (uint i = 0; i < sizeof(score)/sizeof(struct Note); i++) { - IoDoTone(score[i].tone, score[i].time); - //bprintf(BStdOut, "%d ", i); - //BStdOut->flusher(BStdOut); + for (uint i = 0; i < 41; i++) { + //IoDoTone(score[i].tone, score[i].time); + bprintf(BStdOut, "%d ", score[i].time); + BStdOut->flusher(BStdOut); } IoQuietSpeaker(); diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 7b9a93c..9f25998 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -33,6 +33,13 @@ volatile pde_t MmPD[512 * RAM_MAX] __attribute__((__aligned__(KPAGESIZE)));; volatile pte_t MmPT[512 * NB_4K] __attribute__((__aligned__(KPAGESIZE)));; +extern ulong _text; +extern ulong _text_end; +extern ulong _rodata; +extern ulong _rodata_end; +extern ulong _data; +extern ulong _data_end; + ulong MmStackGuards[2] = { 0 }; // @@ -68,6 +75,24 @@ void MmInitPaging(void) continue; } + // TEXT + if ((ulong)(i*KPAGESIZE) >= (ulong)_text && (ulong)(i*KPAGESIZE) <= (ulong)_text_end) { + MmPT[i] = ((ulong)(i*KPAGESIZE)); + continue; + } + + // RODATA + if ((ulong)(i*KPAGESIZE) >= (ulong)_rodata && (ulong)(i*KPAGESIZE) <= (ulong)_rodata_end) { + MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT; + continue; + } + + // DATA + if ((ulong)(i*KPAGESIZE) >= (ulong)_data && (ulong)(i*KPAGESIZE) <= (ulong)_data_end) { + MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT; + continue; + } + MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_READWRITE; } From 50df081a8f281ba147400f70c23d221339e38a71 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 30 Dec 2019 01:22:36 +0100 Subject: [PATCH 06/28] Working on issue #76 --- kaleid/kernel/io/spkr.c | 10 +++++----- kaleid/kernel/mm/paging.c | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/kaleid/kernel/io/spkr.c b/kaleid/kernel/io/spkr.c index dff5d21..697a7a9 100644 --- a/kaleid/kernel/io/spkr.c +++ b/kaleid/kernel/io/spkr.c @@ -86,7 +86,7 @@ void IoDoStarWars(void) uint time; }; - struct Note score[] = { {440, 200}, {110, 200}, {440, 200}, {110, 200}, + const struct Note score[40] = { {440, 200}, {110, 200}, {440, 200}, {110, 200}, {440, 200}, {110, 200}, {349, 140}, {87, 100}, {523, 60}, {87, 100}, {440, 200}, {110, 200}, {349, 140}, {87, 100}, {523, 60}, {87, 100}, @@ -100,10 +100,10 @@ void IoDoStarWars(void) //bprintf(BStdOut, "\n"); - for (uint i = 0; i < sizeof(score)/sizeof(struct Note); i++) { - IoDoTone(score[i].tone, score[i].time); - //bprintf(BStdOut, "%d ", i); - //BStdOut->flusher(BStdOut); + for (uint i = 0; i < 40; i++) { + //IoDoTone(score[i].tone, score[i].time); + bprintf(BStdOut, "%d ", score[i].time); + BStdOut->flusher(BStdOut); } IoQuietSpeaker(); diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 2f161ea..2ace42a 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -68,6 +68,24 @@ void MmInitPaging(void) continue; } + // TEXT + if ((ulong)(i*KPAGESIZE) >= (ulong)&_text && (ulong)(i*KPAGESIZE) <= (ulong)&_text_end) { + MmPT[i] = ((ulong)(i*KPAGESIZE))| MF_PRESENT; + continue; + } + + // RODATA + if ((ulong)(i*KPAGESIZE) >= (ulong)&_rodata && (ulong)(i*KPAGESIZE) <= (ulong)&_rodata_end) { + MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT; + continue; + } + + // DATA + if ((ulong)(i*KPAGESIZE) >= (ulong)&_data && (ulong)(i*KPAGESIZE) <= (ulong)&_data_end) { + MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_READWRITE; + continue; + } + MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_READWRITE; } @@ -92,6 +110,10 @@ void MmInitPaging(void) MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE; MmLoadPML4((void *)MmPML4); + DebugLog("Read only : %p\n", (ulong)&_text); + DebugLog("Read only : %p\n", (ulong)&_text_end); + DebugLog("Read only : %p\n", (ulong)&_rodata); + DebugLog("Read only : %p\n", (ulong)&_rodata_end); //DebugLog("\tPaging tables initialized at %p, %p\n", &MmPD, &MmPT); //DebugLog("\tStack Guards at %p, %p\n", MmStackGuards[0], MmStackGuards[1]); } From ea95dce7492279d69b3ca05c980a6aefda4ed64d Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 31 Dec 2019 00:19:46 +0100 Subject: [PATCH 07/28] Working on issue #76 : TSS --- kaleid/kernel/io/spkr.c | 2 +- kaleid/kernel/ke/idt.c | 4 ++-- kaleid/kernel/mm/gdt.c | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/kaleid/kernel/io/spkr.c b/kaleid/kernel/io/spkr.c index 697a7a9..74488e9 100644 --- a/kaleid/kernel/io/spkr.c +++ b/kaleid/kernel/io/spkr.c @@ -98,7 +98,7 @@ void IoDoStarWars(void) {440, 200}, {110, 200}, {110, 200}, {110, 200} }; - //bprintf(BStdOut, "\n"); + bprintf(BStdOut, "Address of the score : %p\n", &score[37]); for (uint i = 0; i < 40; i++) { //IoDoTone(score[i].tone, score[i].time); diff --git a/kaleid/kernel/ke/idt.c b/kaleid/kernel/ke/idt.c index d5dc7e2..5829914 100644 --- a/kaleid/kernel/ke/idt.c +++ b/kaleid/kernel/ke/idt.c @@ -132,7 +132,7 @@ void KeSetupIDT(void) KeSetIDTGate(0x09, (ulong)isr9, codeSeg, 0x8E, 0); KeSetIDTGate(0x0A, (ulong)isr10, codeSeg, 0x8E, 0); KeSetIDTGate(0x0B, (ulong)isr11, codeSeg, 0x8E, 0); - KeSetIDTGate(0x0C, (ulong)isr12, codeSeg, 0x8E, 1); + KeSetIDTGate(0x0C, (ulong)isr12, codeSeg, 0x8E, 0); KeSetIDTGate(0x0D, (ulong)isr13, codeSeg, 0x8E, 0); KeSetIDTGate(0x0E, (ulong)isr14, codeSeg, 0x8E, 0); KeSetIDTGate(0x0F, (ulong)isr15, codeSeg, 0x8E, 0); // INTEL RESERVED @@ -154,7 +154,7 @@ void KeSetupIDT(void) KeSetIDTGate(0x1F, (ulong)isr31, codeSeg, 0x8E, 0); // INTEL RESERVED // Set IDT IRQs Gates - KeSetIDTGate(0x20, (ulong)isr32, codeSeg, 0x8E, 0); + KeSetIDTGate(0x20, (ulong)isr32, codeSeg, 0x8E, 2); KeSetIDTGate(0x21, (ulong)isr33, codeSeg, 0x8E, 0); KeSetIDTGate(0x22, (ulong)isr34, codeSeg, 0x8E, 0); KeSetIDTGate(0x23, (ulong)isr35, codeSeg, 0x8E, 0); diff --git a/kaleid/kernel/mm/gdt.c b/kaleid/kernel/mm/gdt.c index eb3c4e3..2cc0477 100644 --- a/kaleid/kernel/mm/gdt.c +++ b/kaleid/kernel/mm/gdt.c @@ -53,7 +53,8 @@ void MmInitGdt(void) tssDesc.veryHighBase = ((ulong)&tss >> 32) & 0xFFFFFFFF; tssDesc.lowLimit = sizeof(tss); - tss.ist1 = 0x0007FFFF; // RESCUE STACK, GARANTIED FREE FOR USE BY OSDEV.ORG + tss.ist1 = 0x0007FFFF; // ISR RESCUE STACK, GARANTIED FREE FOR USE BY OSDEV.ORG + tss.ist2 = 0x00EFFFFF; // ISR STACK, GARANTIED FREE FOR USE BY OSDEV.ORG tss.iomap_base = sizeof(tss); memmove(&gdt[2], &tssDesc, sizeof(TssDescriptor_t)); From 5829d7a353bd7762774531d180f7d11ecb6cc4c4 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sun, 5 Jan 2020 20:13:53 +0100 Subject: [PATCH 08/28] New TSS IST used by all ISRs --- Makefile | 29 +++++++++--- include/init/boot.h | 32 ++++++------- include/mm/mm.h | 2 +- kaleid/kernel/init/init.c | 2 +- kaleid/kernel/ke/idt.c | 96 +++++++++++++++++++-------------------- kaleid/kernel/ke/isr.asm | 13 ++++-- kaleid/kernel/ke/pit.c | 2 +- kaleid/kernel/mm/gdt.c | 12 ++--- kaleid/kernel/mm/paging.c | 8 ++-- 9 files changed, 108 insertions(+), 88 deletions(-) diff --git a/Makefile b/Makefile index 0d11f9b..e304613 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ # along with OS/K. If not, see . # #=----------------------------------------------------------------------------=# -.PHONY: all test testnokvm testnosnd test32 debug gdb installonimage dust clean OS/K run +.PHONY: all test testnokvm testnosnd test32 debug gdb ddd gdbnokvm dddnokvm installonimage dust clean OS/K run .DELETE_ON_ERROR: $(BINDIR)/kaleid .DEFAULT_GOAL := all @@ -41,7 +41,7 @@ CCNAME=x86_64-elf-gcc ASMFLAGS=-f elf64 LDFLAGS=-melf_x86_64 COPTIM=-O2 -CWARNS=-Wall -Wextra -Wno-unused-parameter -Wno-implicit-fallthrough -Werror=implicit-function-declaration -Werror=return-type +CWARNS=-Wall -Wextra -Wno-unused-parameter -Wno-implicit-fallthrough -Werror=implicit-function-declaration -Werror=return-type -Wpadded CINCLUDES=-Iinclude CFLAGS1=-nostdlib -ffreestanding -mcmodel=large -std=gnu11 -fstack-protector-all -fdump-rtl-expand CFLAGS2= -c -mno-red-zone -mno-mmx -mno-sse -mno-sse2 @@ -292,7 +292,7 @@ test32: all installonimage cpu_reset,guest_errors,pcall,int 2> $(BUILDDIR)/qemu.log & gdb: all installonimage - @setsid qemu-system-x86_64 -m $(ram) -soundhw pcspk -rtc base=localtime \ + @setsid qemu-system-x86_64 -m $(ram) -enable-kvm -rtc base=localtime \ -hda $(installdisk) -no-reboot -no-shutdown -d \ cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log & @gdb \ @@ -302,9 +302,26 @@ gdb: all installonimage -ex "break BtStartKern" \ ddd: all installonimage - @setsid qemu-system-x86_64 -m $(ram) -hda $(installdisk) -no-reboot -soundhw pcspk \ - -no-shutdown -d cpu_reset,guest_errors,pcall,int -s 2> $(BUILDDIR)/qemu.log & - @ddd + @setsid qemu-system-x86_64 -m $(ram) -enable-kvm -rtc base=localtime \ + -hda $(installdisk) -no-reboot -no-shutdown -d \ + cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log & + @ddd -n + +gdbnokvm: all installonimage + @setsid qemu-system-x86_64 -m $(ram) -rtc base=localtime \ + -hda $(installdisk) -no-reboot -no-shutdown -d \ + cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log & + @gdb \ + -ex "set arch i386:x86-64:intel" \ + -ex "target remote localhost:1234" \ + -ex "symbol-file $(BINDIR)/kaleid" \ + -ex "break BtStartKern" \ + +dddnokvm: all installonimage + @setsid qemu-system-x86_64 -m $(ram) -rtc base=localtime \ + -hda $(installdisk) -no-reboot -no-shutdown -d \ + cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log & + @ddd -n ## HD IMAGE RELATED ---------------------------------------------------------- # diff --git a/include/init/boot.h b/include/init/boot.h index 61cfa19..5594870 100644 --- a/include/init/boot.h +++ b/include/init/boot.h @@ -44,24 +44,24 @@ struct BootInfo_t { // The Bootloader infos struct { - ushort valid; - uint grubFlags; //flags - uint modulesCount; //mods_count void *modulesAddr; //mods_addr - char *grubName; //boot_loader_name void *kernelAddr; void *codeSegment; void *kernelEndAddr; void *stackEndAddr; // stack begins 16B after kernelEndAddr + uint grubFlags; //flags + uint modulesCount; //mods_count + ushort valid; + char *grubName; //boot_loader_name } btldr; // Informations about drives struct { - ushort drvValid; - ushort bufferValid; + void *bufferAddr; //drives_addr uint bootDrv; //boot_device uint bufferLength; //drives_length - void *bufferAddr; //drives_addr + ushort drvValid; + ushort bufferValid; } drives; // Informations about memory @@ -74,36 +74,36 @@ struct BootInfo_t uint upMemory; //mem_upper //GRUB provided memory map - uint mapLength; //mmap_length void *mapAddr; //mmap_addr + uint mapLength; //mmap_length uint ramSize; //The ram (init by map.c) } memory; // Informations about the video drive struct { - ushort vbeValid; - ushort fbuValid; void *vbeControl; //vbe_control_info void *vbeModeInfo; //vbe_mode_info - ushort vbeMode; //vbe_mode - ushort vbeInterfaceSeg; //vbe_interface_seg - ushort vbeInterfaceOff; //vbe_interface_off - ushort vbeInterfaceLen; //vbe_interface_len void *framebufferAddr; //framebuffer_addr uint framebufferPitch; //framebuffer_pitch uint framebufferWidth; //framebuffer_width uint framebufferHeight; //framebuffer_height + ushort vbeValid; + ushort fbuValid; + ushort vbeMode; //vbe_mode + ushort vbeInterfaceSeg; //vbe_interface_seg + ushort vbeInterfaceOff; //vbe_interface_off + ushort vbeInterfaceLen; //vbe_interface_len uchar framebufferBpp; //framebuffer_bpp uchar framebufferType; //framebuffer_type } video; // Informations about the microcode firmware (BIOS/EFI) struct { - ushort apmValid; - ushort romValid; uint apmTable; //apm_table uint romTable; //config_table + ushort apmValid; + ushort romValid; } firmware; }; diff --git a/include/mm/mm.h b/include/mm/mm.h index dcb34b1..283f262 100644 --- a/include/mm/mm.h +++ b/include/mm/mm.h @@ -108,7 +108,7 @@ struct Tss_t ushort iomap_base; uchar iomap[IOMAP_SIZE]; -} __attribute__ ((packed)); +} __attribute__ ((packed)) __attribute__((aligned(8))); diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index ceda825..cee2d63 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -64,6 +64,7 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) MmInitMemoryMap(); MmInitPaging(); MmInitHeap(); + MmInitGdt(); // Basics for interrupts KeSetupIDT(); @@ -73,7 +74,6 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) KeGetCpuInfos(); // Memory (2) - MmInitGdt(); MmActivatePageHandler(); // Drivers diff --git a/kaleid/kernel/ke/idt.c b/kaleid/kernel/ke/idt.c index 5829914..002b972 100644 --- a/kaleid/kernel/ke/idt.c +++ b/kaleid/kernel/ke/idt.c @@ -120,56 +120,56 @@ void KeSetupIDT(void) _KeIdtPtr.base = &idt; // Set IDT Exception Gates - KeSetIDTGate(0x00, (ulong)isr0, codeSeg, 0x8E, 0); - KeSetIDTGate(0x01, (ulong)isr1, codeSeg, 0x8E, 0); - KeSetIDTGate(0x02, (ulong)isr2, codeSeg, 0x8E, 0); - KeSetIDTGate(0x03, (ulong)isr3, codeSeg, 0x8E, 0); - KeSetIDTGate(0x04, (ulong)isr4, codeSeg, 0x8E, 0); - KeSetIDTGate(0x05, (ulong)isr5, codeSeg, 0x8E, 0); - KeSetIDTGate(0x06, (ulong)isr6, codeSeg, 0x8E, 0); - KeSetIDTGate(0x07, (ulong)isr7, codeSeg, 0x8E, 0); + KeSetIDTGate(0x00, (ulong)isr0, codeSeg, 0x8E, 2); + KeSetIDTGate(0x01, (ulong)isr1, codeSeg, 0x8E, 2); + KeSetIDTGate(0x02, (ulong)isr2, codeSeg, 0x8E, 2); + KeSetIDTGate(0x03, (ulong)isr3, codeSeg, 0x8E, 2); + KeSetIDTGate(0x04, (ulong)isr4, codeSeg, 0x8E, 2); + KeSetIDTGate(0x05, (ulong)isr5, codeSeg, 0x8E, 2); + KeSetIDTGate(0x06, (ulong)isr6, codeSeg, 0x8E, 2); + KeSetIDTGate(0x07, (ulong)isr7, codeSeg, 0x8E, 2); KeSetIDTGate(0x08, (ulong)isr8, codeSeg, 0x8E, 1); - KeSetIDTGate(0x09, (ulong)isr9, codeSeg, 0x8E, 0); - KeSetIDTGate(0x0A, (ulong)isr10, codeSeg, 0x8E, 0); - KeSetIDTGate(0x0B, (ulong)isr11, codeSeg, 0x8E, 0); - KeSetIDTGate(0x0C, (ulong)isr12, codeSeg, 0x8E, 0); - KeSetIDTGate(0x0D, (ulong)isr13, codeSeg, 0x8E, 0); - KeSetIDTGate(0x0E, (ulong)isr14, codeSeg, 0x8E, 0); - KeSetIDTGate(0x0F, (ulong)isr15, codeSeg, 0x8E, 0); // INTEL RESERVED - KeSetIDTGate(0x10, (ulong)isr16, codeSeg, 0x8E, 0); - KeSetIDTGate(0x11, (ulong)isr17, codeSeg, 0x8E, 0); - KeSetIDTGate(0x12, (ulong)isr18, codeSeg, 0x8E, 0); - KeSetIDTGate(0x13, (ulong)isr19, codeSeg, 0x8E, 0); - KeSetIDTGate(0x14, (ulong)isr20, codeSeg, 0x8E, 0); - KeSetIDTGate(0x15, (ulong)isr21, codeSeg, 0x8E, 0); // INTEL RESERVED - KeSetIDTGate(0x16, (ulong)isr22, codeSeg, 0x8E, 0); // INTEL RESERVED - KeSetIDTGate(0x17, (ulong)isr23, codeSeg, 0x8E, 0); // INTEL RESERVED - KeSetIDTGate(0x18, (ulong)isr24, codeSeg, 0x8E, 0); // INTEL RESERVED - KeSetIDTGate(0x19, (ulong)isr25, codeSeg, 0x8E, 0); // INTEL RESERVED - KeSetIDTGate(0x1A, (ulong)isr26, codeSeg, 0x8E, 0); // INTEL RESERVED - KeSetIDTGate(0x1B, (ulong)isr27, codeSeg, 0x8E, 0); // INTEL RESERVED - KeSetIDTGate(0x1C, (ulong)isr28, codeSeg, 0x8E, 0); // INTEL RESERVED - KeSetIDTGate(0x1D, (ulong)isr29, codeSeg, 0x8E, 0); // INTEL RESERVED - KeSetIDTGate(0x1E, (ulong)isr30, codeSeg, 0x8E, 0); - KeSetIDTGate(0x1F, (ulong)isr31, codeSeg, 0x8E, 0); // INTEL RESERVED + KeSetIDTGate(0x09, (ulong)isr9, codeSeg, 0x8E, 2); + KeSetIDTGate(0x0A, (ulong)isr10, codeSeg, 0x8E, 0); // INVALID TSS + KeSetIDTGate(0x0B, (ulong)isr11, codeSeg, 0x8E, 2); + KeSetIDTGate(0x0C, (ulong)isr12, codeSeg, 0x8E, 1); + KeSetIDTGate(0x0D, (ulong)isr13, codeSeg, 0x8E, 2); + KeSetIDTGate(0x0E, (ulong)isr14, codeSeg, 0x8E, 2); + KeSetIDTGate(0x0F, (ulong)isr15, codeSeg, 0x8E, 2); // INTEL RESERVED + KeSetIDTGate(0x10, (ulong)isr16, codeSeg, 0x8E, 2); + KeSetIDTGate(0x11, (ulong)isr17, codeSeg, 0x8E, 2); + KeSetIDTGate(0x12, (ulong)isr18, codeSeg, 0x8E, 2); + KeSetIDTGate(0x13, (ulong)isr19, codeSeg, 0x8E, 2); + KeSetIDTGate(0x14, (ulong)isr20, codeSeg, 0x8E, 2); + KeSetIDTGate(0x15, (ulong)isr21, codeSeg, 0x8E, 2); // INTEL RESERVED + KeSetIDTGate(0x16, (ulong)isr22, codeSeg, 0x8E, 2); // INTEL RESERVED + KeSetIDTGate(0x17, (ulong)isr23, codeSeg, 0x8E, 2); // INTEL RESERVED + KeSetIDTGate(0x18, (ulong)isr24, codeSeg, 0x8E, 2); // INTEL RESERVED + KeSetIDTGate(0x19, (ulong)isr25, codeSeg, 0x8E, 2); // INTEL RESERVED + KeSetIDTGate(0x1A, (ulong)isr26, codeSeg, 0x8E, 2); // INTEL RESERVED + KeSetIDTGate(0x1B, (ulong)isr27, codeSeg, 0x8E, 2); // INTEL RESERVED + KeSetIDTGate(0x1C, (ulong)isr28, codeSeg, 0x8E, 2); // INTEL RESERVED + KeSetIDTGate(0x1D, (ulong)isr29, codeSeg, 0x8E, 2); // INTEL RESERVED + KeSetIDTGate(0x1E, (ulong)isr30, codeSeg, 0x8E, 2); + KeSetIDTGate(0x1F, (ulong)isr31, codeSeg, 0x8E, 2); // INTEL RESERVED // Set IDT IRQs Gates KeSetIDTGate(0x20, (ulong)isr32, codeSeg, 0x8E, 2); - KeSetIDTGate(0x21, (ulong)isr33, codeSeg, 0x8E, 0); - KeSetIDTGate(0x22, (ulong)isr34, codeSeg, 0x8E, 0); - KeSetIDTGate(0x23, (ulong)isr35, codeSeg, 0x8E, 0); - KeSetIDTGate(0x24, (ulong)isr36, codeSeg, 0x8E, 0); - KeSetIDTGate(0x25, (ulong)isr37, codeSeg, 0x8E, 0); - KeSetIDTGate(0x26, (ulong)isr38, codeSeg, 0x8E, 0); - KeSetIDTGate(0x27, (ulong)isr39, codeSeg, 0x8E, 0); - KeSetIDTGate(0x28, (ulong)isr40, codeSeg, 0x8E, 0); - KeSetIDTGate(0x29, (ulong)isr41, codeSeg, 0x8E, 0); - KeSetIDTGate(0x2A, (ulong)isr42, codeSeg, 0x8E, 0); - KeSetIDTGate(0x2B, (ulong)isr43, codeSeg, 0x8E, 0); - KeSetIDTGate(0x2C, (ulong)isr44, codeSeg, 0x8E, 0); - KeSetIDTGate(0x2D, (ulong)isr45, codeSeg, 0x8E, 0); - KeSetIDTGate(0x2E, (ulong)isr46, codeSeg, 0x8E, 0); - KeSetIDTGate(0x2F, (ulong)isr47, codeSeg, 0x8E, 0); + KeSetIDTGate(0x21, (ulong)isr33, codeSeg, 0x8E, 2); + KeSetIDTGate(0x22, (ulong)isr34, codeSeg, 0x8E, 2); + KeSetIDTGate(0x23, (ulong)isr35, codeSeg, 0x8E, 2); + KeSetIDTGate(0x24, (ulong)isr36, codeSeg, 0x8E, 2); + KeSetIDTGate(0x25, (ulong)isr37, codeSeg, 0x8E, 2); + KeSetIDTGate(0x26, (ulong)isr38, codeSeg, 0x8E, 2); + KeSetIDTGate(0x27, (ulong)isr39, codeSeg, 0x8E, 2); + KeSetIDTGate(0x28, (ulong)isr40, codeSeg, 0x8E, 2); + KeSetIDTGate(0x29, (ulong)isr41, codeSeg, 0x8E, 2); + KeSetIDTGate(0x2A, (ulong)isr42, codeSeg, 0x8E, 2); + KeSetIDTGate(0x2B, (ulong)isr43, codeSeg, 0x8E, 2); + KeSetIDTGate(0x2C, (ulong)isr44, codeSeg, 0x8E, 2); + KeSetIDTGate(0x2D, (ulong)isr45, codeSeg, 0x8E, 2); + KeSetIDTGate(0x2E, (ulong)isr46, codeSeg, 0x8E, 2); + KeSetIDTGate(0x2F, (ulong)isr47, codeSeg, 0x8E, 2); KeIdtIsInitialized++; @@ -293,8 +293,8 @@ void KeDisableNMI(void) // void _KeHandleISR(ISRFrame_t *regs) { - if ((!regs) || (!regs->rip)) - KeStartPanic("[ISR ?] Unknown ISR Exception Abort\n"); + /* if ((!regs) || (!regs->rip)) */ + /* KeStartPanic("[ISR ?] Unknown ISR Exception Abort\n"); */ if ((regs->intNo >= 0x15) && (regs->intNo <= 0x1D)) return; // INTEL RESERVED diff --git a/kaleid/kernel/ke/isr.asm b/kaleid/kernel/ke/isr.asm index 8a92419..43b5374 100644 --- a/kaleid/kernel/ke/isr.asm +++ b/kaleid/kernel/ke/isr.asm @@ -58,11 +58,10 @@ isrPreHandler: push rax ; Check if we are switching from user mode to supervisor mode - mov rax, [rsp + 152] - and rax, 0x3000 - jz .SEnter - - swapgs ; XXX need TSS + ;mov rax, [rsp + 152] + ;and rax, 0x3000 + ;jz .SEnter + ;swapgs ; XXX need TSS .SEnter: ; Increment mask count as we configure all interrupts to mask IF @@ -95,6 +94,10 @@ isrPreHandler: iretq +Die: + hlt + jmp Die + ;; Divide Error Fault IsrWithoutErrCode 0 diff --git a/kaleid/kernel/ke/pit.c b/kaleid/kernel/ke/pit.c index a9d961c..4966c23 100644 --- a/kaleid/kernel/ke/pit.c +++ b/kaleid/kernel/ke/pit.c @@ -128,7 +128,7 @@ void KeEnablePIT(void) DebugLog("\tPIT activated with period %d ms\n", 1000/PIT_FREQUENCY); KeRestoreIRQs(flags); - KeEnableNMI(); + //XXX KeEnableNMI(); } char *KeFormatCurTime(void) diff --git a/kaleid/kernel/mm/gdt.c b/kaleid/kernel/mm/gdt.c index 2cc0477..f762336 100644 --- a/kaleid/kernel/mm/gdt.c +++ b/kaleid/kernel/mm/gdt.c @@ -25,10 +25,10 @@ #include #include -GdtPtr_t gdtPtr; -GdtEntry_t gdt[4] __attribute__((__aligned__(KPAGESIZE))); -TssDescriptor_t tssDesc __attribute__((__aligned__(KPAGESIZE))); -Tss_t tss __attribute__((__aligned__(KPAGESIZE))); +volatile GdtPtr_t gdtPtr; +volatile GdtEntry_t gdt[4] __attribute__((__aligned__(KPAGESIZE))); +volatile TssDescriptor_t tssDesc __attribute__((__aligned__(KPAGESIZE))); +volatile Tss_t tss __attribute__((__aligned__(KPAGESIZE))); void MmInitGdt(void) { @@ -53,8 +53,8 @@ void MmInitGdt(void) tssDesc.veryHighBase = ((ulong)&tss >> 32) & 0xFFFFFFFF; tssDesc.lowLimit = sizeof(tss); - tss.ist1 = 0x0007FFFF; // ISR RESCUE STACK, GARANTIED FREE FOR USE BY OSDEV.ORG - tss.ist2 = 0x00EFFFFF; // ISR STACK, GARANTIED FREE FOR USE BY OSDEV.ORG + tss.ist1 = (ulong)0x0007FFFF; // ISR RESCUE STACK, GARANTIED FREE FOR USE BY OSDEV.ORG + tss.ist2 = (ulong)0x00EFFFFF; // ISR STACK, GARANTIED FREE FOR USE BY OSDEV.ORG tss.iomap_base = sizeof(tss); memmove(&gdt[2], &tssDesc, sizeof(TssDescriptor_t)); diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 843aa87..a244181 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -117,10 +117,10 @@ void MmInitPaging(void) MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE; MmLoadPML4((void *)MmPML4); - DebugLog("Read only : %p\n", (ulong)&_text); - DebugLog("Read only : %p\n", (ulong)&_text_end); - DebugLog("Read only : %p\n", (ulong)&_rodata); - DebugLog("Read only : %p\n", (ulong)&_rodata_end); + /* DebugLog("Read only : %p\n", (ulong)&_text); */ + /* DebugLog("Read only : %p\n", (ulong)&_text_end); */ + /* DebugLog("Read only : %p\n", (ulong)&_rodata); */ + /* DebugLog("Read only : %p\n", (ulong)&_rodata_end); */ //DebugLog("\tPaging tables initialized at %p, %p\n", &MmPD, &MmPT); //DebugLog("\tStack Guards at %p, %p\n", MmStackGuards[0], MmStackGuards[1]); } From 4cbd42a19f7cc58e8b75b884ed4543fb09b1f42c Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sun, 5 Jan 2020 22:12:50 +0100 Subject: [PATCH 09/28] Write protection for rodata and text functionnal --- include/io/spkr.h | 6 ++++++ kaleid/kernel/io/spkr.c | 34 ++++++++++++++-------------------- kaleid/kernel/mm/paging.c | 13 ++++++------- kaleid/kernel/sh/testcmds.c | 25 ++++++++++++++++++++++++- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/include/io/spkr.h b/include/io/spkr.h index 760928a..e1f8514 100644 --- a/include/io/spkr.h +++ b/include/io/spkr.h @@ -38,6 +38,12 @@ void IoDoBeepNoIdt(void); void IoDoStarWars(void); +struct Note +{ + uint tone; + uint time; +}; + //----------------------------------------------------------------------------// #endif diff --git a/kaleid/kernel/io/spkr.c b/kaleid/kernel/io/spkr.c index 74488e9..c2a4fce 100644 --- a/kaleid/kernel/io/spkr.c +++ b/kaleid/kernel/io/spkr.c @@ -28,6 +28,18 @@ extern bool KeIdtIsInitialized; +const struct Note score[40] = { {440, 200}, {110, 200}, {440, 200}, {110, 200}, + {440, 200}, {110, 200}, {349, 140}, {87, 100}, + {523, 60}, {87, 100}, {440, 200}, {110, 200}, + {349, 140}, {87, 100}, {523, 60}, {87, 100}, + {440, 200}, {110, 200}, {440, 200}, {110, 200}, + {659, 200}, {110, 200}, {659, 200}, {110, 200}, + {659, 200}, {87, 200}, {698, 140}, {87, 100}, + {523, 60}, {87, 100}, {415, 200}, {87, 200}, + {349, 140}, {87, 100}, {523, 60}, {87, 100}, + {440, 200}, {110, 200}, {110, 200}, {110, 200} +}; + void IoStartSpeaker(int freq) { uchar temp; @@ -81,28 +93,10 @@ void IoDoBeepNoIdt(void) void IoDoStarWars(void) { - struct Note { - uint tone; - uint time; - }; - - const struct Note score[40] = { {440, 200}, {110, 200}, {440, 200}, {110, 200}, - {440, 200}, {110, 200}, {349, 140}, {87, 100}, - {523, 60}, {87, 100}, {440, 200}, {110, 200}, - {349, 140}, {87, 100}, {523, 60}, {87, 100}, - {440, 200}, {110, 200}, {440, 200}, {110, 200}, - {659, 200}, {110, 200}, {659, 200}, {110, 200}, - {659, 200}, {87, 200}, {698, 140}, {87, 100}, - {523, 60}, {87, 100}, {415, 200}, {87, 200}, - {349, 140}, {87, 100}, {523, 60}, {87, 100}, - {440, 200}, {110, 200}, {110, 200}, {110, 200} - }; - - bprintf(BStdOut, "Address of the score : %p\n", &score[37]); - for (uint i = 0; i < 40; i++) { //IoDoTone(score[i].tone, score[i].time); - bprintf(BStdOut, "%d ", score[i].time); + bprintf(BStdOut, "At %p : %u\n", &(score[i].tone), score[i].tone); + bprintf(BStdOut, "At %p : %u\n", &(score[i].time), score[i].time); BStdOut->flusher(BStdOut); } IoQuietSpeaker(); diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index a244181..a513d3a 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -83,17 +83,17 @@ void MmInitPaging(void) // RODATA if ((ulong)(i*KPAGESIZE) >= (ulong)&_rodata && (ulong)(i*KPAGESIZE) <= (ulong)&_rodata_end) { - MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT; + MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_WRITETHR; continue; } // DATA if ((ulong)(i*KPAGESIZE) >= (ulong)&_data && (ulong)(i*KPAGESIZE) <= (ulong)&_data_end) { - MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_READWRITE; + MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_WRITETHR | MF_READWRITE; continue; } - MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_READWRITE; + MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_WRITETHR | MF_READWRITE; } for (volatile ulong i = 0; i < NB_4K; i++) { @@ -117,10 +117,9 @@ void MmInitPaging(void) MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE; MmLoadPML4((void *)MmPML4); - /* DebugLog("Read only : %p\n", (ulong)&_text); */ - /* DebugLog("Read only : %p\n", (ulong)&_text_end); */ - /* DebugLog("Read only : %p\n", (ulong)&_rodata); */ - /* DebugLog("Read only : %p\n", (ulong)&_rodata_end); */ + MmEnableWriteProtect(); + DebugLog("\tPage RO from %p to %p\n", (ulong)&_text, (ulong)&_text_end); + DebugLog("\tPage RO from %p to %p\n", (ulong)&_rodata, (ulong)&_rodata_end); //DebugLog("\tPaging tables initialized at %p, %p\n", &MmPD, &MmPT); //DebugLog("\tStack Guards at %p, %p\n", MmStackGuards[0], MmStackGuards[1]); } diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 8b9cb66..9a8732b 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -155,11 +155,13 @@ error_t CmdHelpTest(int argc, char **argv, char *cmdline) error_t CmdPF(int argc, char **argv, char *cmdline) { - char *address = (void*)(ulong)atoi(argv[1]); + ulong *address = (ulong*)(ulong)atoi(argv[1]); KernLog("Provoking Page Fault at %#x\n", address); + KernLog("It contained %#x\n", *address); *address = 1; + KernLog("Now it contains %#x\n", *address); KernLog("No page fault : address was valid/present\n"); @@ -203,6 +205,26 @@ error_t CmdTimerTest(int argc, char **argv, char *cmdline) return EOK; } +error_t CmdprintfTest(int argc, char **argv, char *cmdline) +{ + /* bprintf(BStdOut, "INT_MIN : %u\n", INT_MIN); */ + /* BStdOut->flusher(BStdOut); */ + bprintf(BStdOut, "INT_MAX : %u\n", INT_MAX); + BStdOut->flusher(BStdOut); + + int n = - 5; + + for (int i=INT_MAX - 5; n < 10; i++) { + bprintf(BStdOut, "INT_MAX + %d : %d\n", n, i); + BStdOut->flusher(BStdOut); + n++; + } + + KernLog("Finished !\n"); + + return EOK; +} + static Command_t testcmdtable[] = { { "args", CmdArgs, "Print command line" }, @@ -215,5 +237,6 @@ static Command_t testcmdtable[] = { "stkov", CmdStackOverflow, "Provoke a stack overflow" }, { "stkun", CmdStackUnderflow, "Provoke a stack underflow" }, { "timer", CmdTimerTest, "test timer of x ms" }, + { "printf", CmdprintfTest, "test timer of x ms" }, { NULL, NULL, NULL } }; From 51552873093c7fe927561ec875467728906dbab3 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 6 Jan 2020 00:30:50 +0100 Subject: [PATCH 10/28] ISRs IST (TSS) and RO .rodata & .text pages --- kaleid/kernel/ke/pit.c | 2 +- kaleid/kernel/mm/gdt.c | 8 ++++---- kaleid/kernel/sh/testcmds.c | 21 --------------------- 3 files changed, 5 insertions(+), 26 deletions(-) diff --git a/kaleid/kernel/ke/pit.c b/kaleid/kernel/ke/pit.c index 4966c23..a9d961c 100644 --- a/kaleid/kernel/ke/pit.c +++ b/kaleid/kernel/ke/pit.c @@ -128,7 +128,7 @@ void KeEnablePIT(void) DebugLog("\tPIT activated with period %d ms\n", 1000/PIT_FREQUENCY); KeRestoreIRQs(flags); - //XXX KeEnableNMI(); + KeEnableNMI(); } char *KeFormatCurTime(void) diff --git a/kaleid/kernel/mm/gdt.c b/kaleid/kernel/mm/gdt.c index f762336..0ec7cb6 100644 --- a/kaleid/kernel/mm/gdt.c +++ b/kaleid/kernel/mm/gdt.c @@ -25,10 +25,10 @@ #include #include -volatile GdtPtr_t gdtPtr; -volatile GdtEntry_t gdt[4] __attribute__((__aligned__(KPAGESIZE))); -volatile TssDescriptor_t tssDesc __attribute__((__aligned__(KPAGESIZE))); -volatile Tss_t tss __attribute__((__aligned__(KPAGESIZE))); +GdtPtr_t gdtPtr; +GdtEntry_t gdt[4] __attribute__((__aligned__(KPAGESIZE))); +TssDescriptor_t tssDesc __attribute__((__aligned__(KPAGESIZE))); +Tss_t tss __attribute__((__aligned__(KPAGESIZE))); void MmInitGdt(void) { diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 9a8732b..716ce2a 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -205,26 +205,6 @@ error_t CmdTimerTest(int argc, char **argv, char *cmdline) return EOK; } -error_t CmdprintfTest(int argc, char **argv, char *cmdline) -{ - /* bprintf(BStdOut, "INT_MIN : %u\n", INT_MIN); */ - /* BStdOut->flusher(BStdOut); */ - bprintf(BStdOut, "INT_MAX : %u\n", INT_MAX); - BStdOut->flusher(BStdOut); - - int n = - 5; - - for (int i=INT_MAX - 5; n < 10; i++) { - bprintf(BStdOut, "INT_MAX + %d : %d\n", n, i); - BStdOut->flusher(BStdOut); - n++; - } - - KernLog("Finished !\n"); - - return EOK; -} - static Command_t testcmdtable[] = { { "args", CmdArgs, "Print command line" }, @@ -237,6 +217,5 @@ static Command_t testcmdtable[] = { "stkov", CmdStackOverflow, "Provoke a stack overflow" }, { "stkun", CmdStackUnderflow, "Provoke a stack underflow" }, { "timer", CmdTimerTest, "test timer of x ms" }, - { "printf", CmdprintfTest, "test timer of x ms" }, { NULL, NULL, NULL } }; From f882e16db702aa831f09986889b5e63997eaaed2 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 7 Jan 2020 00:44:39 +0100 Subject: [PATCH 11/28] Cleanup and misc changes --- kaleid/kernel/ke/idt.c | 40 ++++++++++++++++++------------------- kaleid/kernel/mm/gdt.c | 1 + kaleid/kernel/sh/testcmds.c | 37 ++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/kaleid/kernel/ke/idt.c b/kaleid/kernel/ke/idt.c index 002b972..c279bde 100644 --- a/kaleid/kernel/ke/idt.c +++ b/kaleid/kernel/ke/idt.c @@ -127,12 +127,12 @@ void KeSetupIDT(void) KeSetIDTGate(0x04, (ulong)isr4, codeSeg, 0x8E, 2); KeSetIDTGate(0x05, (ulong)isr5, codeSeg, 0x8E, 2); KeSetIDTGate(0x06, (ulong)isr6, codeSeg, 0x8E, 2); - KeSetIDTGate(0x07, (ulong)isr7, codeSeg, 0x8E, 2); - KeSetIDTGate(0x08, (ulong)isr8, codeSeg, 0x8E, 1); + KeSetIDTGate(0x07, (ulong)isr7, codeSeg, 0x8E, 2); // XXX device not available, useful for FPU save/restore when multitasking + KeSetIDTGate(0x08, (ulong)isr8, codeSeg, 0x8E, 1); // DOUBLE FAULT KeSetIDTGate(0x09, (ulong)isr9, codeSeg, 0x8E, 2); - KeSetIDTGate(0x0A, (ulong)isr10, codeSeg, 0x8E, 0); // INVALID TSS + KeSetIDTGate(0x0A, (ulong)isr10, codeSeg, 0x8E, 0); // INVALID TSS KeSetIDTGate(0x0B, (ulong)isr11, codeSeg, 0x8E, 2); - KeSetIDTGate(0x0C, (ulong)isr12, codeSeg, 0x8E, 1); + KeSetIDTGate(0x0C, (ulong)isr12, codeSeg, 0x8E, 1); // STACK SEGMENT FAULT KeSetIDTGate(0x0D, (ulong)isr13, codeSeg, 0x8E, 2); KeSetIDTGate(0x0E, (ulong)isr14, codeSeg, 0x8E, 2); KeSetIDTGate(0x0F, (ulong)isr15, codeSeg, 0x8E, 2); // INTEL RESERVED @@ -154,22 +154,22 @@ void KeSetupIDT(void) KeSetIDTGate(0x1F, (ulong)isr31, codeSeg, 0x8E, 2); // INTEL RESERVED // Set IDT IRQs Gates - KeSetIDTGate(0x20, (ulong)isr32, codeSeg, 0x8E, 2); - KeSetIDTGate(0x21, (ulong)isr33, codeSeg, 0x8E, 2); - KeSetIDTGate(0x22, (ulong)isr34, codeSeg, 0x8E, 2); - KeSetIDTGate(0x23, (ulong)isr35, codeSeg, 0x8E, 2); - KeSetIDTGate(0x24, (ulong)isr36, codeSeg, 0x8E, 2); - KeSetIDTGate(0x25, (ulong)isr37, codeSeg, 0x8E, 2); - KeSetIDTGate(0x26, (ulong)isr38, codeSeg, 0x8E, 2); - KeSetIDTGate(0x27, (ulong)isr39, codeSeg, 0x8E, 2); - KeSetIDTGate(0x28, (ulong)isr40, codeSeg, 0x8E, 2); - KeSetIDTGate(0x29, (ulong)isr41, codeSeg, 0x8E, 2); - KeSetIDTGate(0x2A, (ulong)isr42, codeSeg, 0x8E, 2); - KeSetIDTGate(0x2B, (ulong)isr43, codeSeg, 0x8E, 2); - KeSetIDTGate(0x2C, (ulong)isr44, codeSeg, 0x8E, 2); - KeSetIDTGate(0x2D, (ulong)isr45, codeSeg, 0x8E, 2); - KeSetIDTGate(0x2E, (ulong)isr46, codeSeg, 0x8E, 2); - KeSetIDTGate(0x2F, (ulong)isr47, codeSeg, 0x8E, 2); + KeSetIDTGate(0x20, (ulong)isr32, codeSeg, 0x8E, 3); + KeSetIDTGate(0x21, (ulong)isr33, codeSeg, 0x8E, 3); + KeSetIDTGate(0x22, (ulong)isr34, codeSeg, 0x8E, 3); + KeSetIDTGate(0x23, (ulong)isr35, codeSeg, 0x8E, 3); + KeSetIDTGate(0x24, (ulong)isr36, codeSeg, 0x8E, 3); + KeSetIDTGate(0x25, (ulong)isr37, codeSeg, 0x8E, 3); + KeSetIDTGate(0x26, (ulong)isr38, codeSeg, 0x8E, 3); + KeSetIDTGate(0x27, (ulong)isr39, codeSeg, 0x8E, 3); + KeSetIDTGate(0x28, (ulong)isr40, codeSeg, 0x8E, 3); + KeSetIDTGate(0x29, (ulong)isr41, codeSeg, 0x8E, 3); + KeSetIDTGate(0x2A, (ulong)isr42, codeSeg, 0x8E, 3); + KeSetIDTGate(0x2B, (ulong)isr43, codeSeg, 0x8E, 3); + KeSetIDTGate(0x2C, (ulong)isr44, codeSeg, 0x8E, 3); + KeSetIDTGate(0x2D, (ulong)isr45, codeSeg, 0x8E, 3); + KeSetIDTGate(0x2E, (ulong)isr46, codeSeg, 0x8E, 3); + KeSetIDTGate(0x2F, (ulong)isr47, codeSeg, 0x8E, 3); KeIdtIsInitialized++; diff --git a/kaleid/kernel/mm/gdt.c b/kaleid/kernel/mm/gdt.c index 0ec7cb6..79f9bc6 100644 --- a/kaleid/kernel/mm/gdt.c +++ b/kaleid/kernel/mm/gdt.c @@ -55,6 +55,7 @@ void MmInitGdt(void) tss.ist1 = (ulong)0x0007FFFF; // ISR RESCUE STACK, GARANTIED FREE FOR USE BY OSDEV.ORG tss.ist2 = (ulong)0x00EFFFFF; // ISR STACK, GARANTIED FREE FOR USE BY OSDEV.ORG + tss.ist3 = (ulong)0x00EF0000; // ISR STACK, GARANTIED FREE FOR USE BY OSDEV.ORG tss.iomap_base = sizeof(tss); memmove(&gdt[2], &tssDesc, sizeof(TssDescriptor_t)); diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 716ce2a..9c05d3c 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -109,6 +109,42 @@ error_t CmdDumpATASect(int argc, char **argv, char *cmdline) return EOK; } +error_t CmdDumpMem(int argc, char **argv, char *cmdline) +{ + char sector[1024] = {0}; + char *address = (char*)atol(argv[1]); + int nb = 1; //atoi(argv[2]); + int x = 0; + int step = 16; + + KernLog("Address begin: %p\n", address); + + for (int i = 0; i < 1024*nb; i++) { + sector[i] = *address++; + } + + while(x < 1024*nb) { + KernLog("%C", shcol); + for (int i = 0; i < step; i++) { + KernLog("%02x ", (uchar)sector[i+x]); + } + KernLog(" %C ", VGA_COLOR_LIGHT_BLUE); + for (int i = 0; i < step; i++) { + if (isprint(sector[i+x])) + KernLog("%c", + sector[i+x] + ); + else + KernLog("%c", 0); + } + KernLog("\n"); + x += step; + } + + KernLog("\n\n"); + return EOK; +} + error_t CmdFloatDiv(int argc, char **argv, char *cmdline) { double a = (double)atoi(argv[1]); @@ -210,6 +246,7 @@ static Command_t testcmdtable[] = { "args", CmdArgs, "Print command line" }, { "atoi", CmdAtoi, "Print command line atoised" }, { "dmpsec", CmdDumpATASect, "Dump an ATA sector on screen" }, + { "dmp", CmdDumpMem, "Dump 1MB of memory starting from addr"}, { "help", CmdHelpTest, "Show this message" }, { "div", CmdFloatDiv, "Float div. Usage : div a b. Returns a/b"}, { "pf", CmdPF, "Provoke a PF. Usage: pfault
"}, From 74d0508b679815e6ee58ba1c65cb3cee2d58237b Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 7 Jan 2020 16:56:46 +0100 Subject: [PATCH 12/28] Minor changes to stack guard --- kaleid/kernel/ke/idt.c | 18 +----------------- kaleid/kernel/mm/gdt.c | 4 ++++ kaleid/kernel/mm/paging.c | 14 +++++++++++++- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/kaleid/kernel/ke/idt.c b/kaleid/kernel/ke/idt.c index c279bde..251cf7a 100644 --- a/kaleid/kernel/ke/idt.c +++ b/kaleid/kernel/ke/idt.c @@ -341,21 +341,7 @@ static void EarlyExceptionHandler(ISRFrame_t *regs) // static void DoubleFaultHandler(ISRFrame_t *regs) { - ulong StackGuardTwo = (ulong)MmGetStackGuards(1); - - if (regs->rsp <= StackGuardTwo + 4*KB && (regs->rsp - 4*KB <= regs->cr2)) { - bprintf(BStdOut, - "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Stack Overflow\n\n" - " Double Fault Error code : %#x (%b)\n" - " Stack Guard bypassed : %#x", - - VGA_COLOR_LIGHT_RED, - regs->ErrorCode, - regs->ErrorCode, - StackGuardTwo - ); - } else { - bprintf(BStdOut, + bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Double Fault Abort\n\n" " Error code : 0x%x (%b)", @@ -364,8 +350,6 @@ static void DoubleFaultHandler(ISRFrame_t *regs) regs->ErrorCode ); - } - KeBrkDumpRegisters(regs); BStdOut->flusher(BStdOut); diff --git a/kaleid/kernel/mm/gdt.c b/kaleid/kernel/mm/gdt.c index 79f9bc6..8bd426a 100644 --- a/kaleid/kernel/mm/gdt.c +++ b/kaleid/kernel/mm/gdt.c @@ -45,6 +45,10 @@ void MmInitGdt(void) gdt[1].access = 0x98; gdt[1].flags = 0x20; + gdt[2].lowLimit = 0xFFFF; + gdt[2].access = 0x98; + gdt[2].flags = 0x20; + tssDesc.access = 0x89; tssDesc.flags = 0x40; tssDesc.lowBase = (ulong)&tss & 0xFFFF; diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index a513d3a..dbd0110 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -142,7 +142,8 @@ void *MmTranslateKPageToAddr(void *rank) static void PagingHandler(ISRFrame_t *regs) { ulong StackGuardOne = (ulong)MmGetStackGuards(0); - if (regs->cr2 >= StackGuardOne && (regs->rsp + 4*KB >= regs->cr2)) { + ulong StackGuardTwo = (ulong)MmGetStackGuards(1); + if ((regs->cr2 >= StackGuardOne) && (regs->cr2 <= StackGuardOne + 4*KB) && (regs->rsp <= regs->cr2)) { bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Stack Underflow\n\n" " Double Fault Error code : %#x (%b)\n" @@ -153,6 +154,17 @@ static void PagingHandler(ISRFrame_t *regs) regs->ErrorCode, StackGuardOne ); + } else if ((regs->cr2 >= StackGuardTwo) && (regs->cr2 <= StackGuardTwo + 4*KB) && (regs->rsp >= regs->cr2)) { + bprintf(BStdOut, + "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Stack Overflow\n\n" + " Double Fault Error code : %#x (%b)\n" + " Stack Guard bypassed : %#x", + + VGA_COLOR_LIGHT_RED, + regs->ErrorCode, + regs->ErrorCode, + StackGuardTwo + ); } else { //XXX page fault bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x%x] Irrecoverable Kernel Page Fault at %p\n\n" From 737bea025fa4a99fb2394843363bef9204cea15b Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 8 Jan 2020 00:28:10 +0100 Subject: [PATCH 13/28] Minor corrections --- include/asm.h | 5 +++++ include/mm/mm.h | 2 +- kaleid/kernel/ke/isr.asm | 9 +++++---- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/asm.h b/include/asm.h index 4694222..15f8bc5 100644 --- a/include/asm.h +++ b/include/asm.h @@ -136,6 +136,11 @@ static inline ulong KeReadStsc(void) { return ((ulong)edx << 32) + eax; } +static inline void KeFlushTlbSingle(unsigned long addr) +{ + asm volatile("invlpg (%0)" ::"r" (addr) : "memory"); +} + //------------------------------------------// // Misc. I/O // //------------------------------------------// diff --git a/include/mm/mm.h b/include/mm/mm.h index 283f262..ed0e57a 100644 --- a/include/mm/mm.h +++ b/include/mm/mm.h @@ -41,7 +41,7 @@ #define BADRAM_ZONE 5 // Invalid zone because material problem... #define MAX_ENTRIES 2048 // Max number of memory map entries #define KPAGESIZE (4 * KB) -#define UPAGESIZE (2 * MB) +#define UPAGESIZE (4 * KB) //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/ke/isr.asm b/kaleid/kernel/ke/isr.asm index 43b5374..7c07e6f 100644 --- a/kaleid/kernel/ke/isr.asm +++ b/kaleid/kernel/ke/isr.asm @@ -58,10 +58,11 @@ isrPreHandler: push rax ; Check if we are switching from user mode to supervisor mode - ;mov rax, [rsp + 152] - ;and rax, 0x3000 - ;jz .SEnter - ;swapgs ; XXX need TSS + mov rax, [rsp + 152] + and rax, 0x3000 + jz .SEnter + + swapgs ; XXX need TSS .SEnter: ; Increment mask count as we configure all interrupts to mask IF From 6785767528f35471f00574763ed2a04bbafac181 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 8 Jan 2020 21:10:24 +0100 Subject: [PATCH 14/28] Resolved #76... --- Makefile | 2 +- include/io/spkr.h | 8 ++++---- kaleid/kernel/io/spkr.c | 18 +++++++----------- kaleid/kernel/mm/paging.c | 2 +- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index e304613..05c091e 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ CCNAME=x86_64-elf-gcc ASMFLAGS=-f elf64 LDFLAGS=-melf_x86_64 COPTIM=-O2 -CWARNS=-Wall -Wextra -Wno-unused-parameter -Wno-implicit-fallthrough -Werror=implicit-function-declaration -Werror=return-type -Wpadded +CWARNS=-Wall -Wextra -Wno-unused-parameter -Wno-implicit-fallthrough -Werror=implicit-function-declaration -Werror=return-type #-Wpadded CINCLUDES=-Iinclude CFLAGS1=-nostdlib -ffreestanding -mcmodel=large -std=gnu11 -fstack-protector-all -fdump-rtl-expand CFLAGS2= -c -mno-red-zone -mno-mmx -mno-sse -mno-sse2 diff --git a/include/io/spkr.h b/include/io/spkr.h index e1f8514..be9eea5 100644 --- a/include/io/spkr.h +++ b/include/io/spkr.h @@ -31,17 +31,17 @@ //----------------------------------------------------------------------------// -void IoStartSpeaker(int); +void IoStartSpeaker(ulong); void IoDoBeep(void); -void IoDoTone(uint tone, uint time); +void IoDoTone(ulong tone, ulong time); void IoDoBeepNoIdt(void); void IoDoStarWars(void); struct Note { - uint tone; - uint time; + ulong tone; + ulong time; }; //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/io/spkr.c b/kaleid/kernel/io/spkr.c index c2a4fce..66a7f5c 100644 --- a/kaleid/kernel/io/spkr.c +++ b/kaleid/kernel/io/spkr.c @@ -28,7 +28,7 @@ extern bool KeIdtIsInitialized; -const struct Note score[40] = { {440, 200}, {110, 200}, {440, 200}, {110, 200}, +struct Note score[40] = { {440, 200}, {110, 200}, {440, 200}, {110, 200}, {440, 200}, {110, 200}, {349, 140}, {87, 100}, {523, 60}, {87, 100}, {440, 200}, {110, 200}, {349, 140}, {87, 100}, {523, 60}, {87, 100}, @@ -37,13 +37,12 @@ const struct Note score[40] = { {440, 200}, {110, 200}, {440, 200}, {110, 200}, {659, 200}, {87, 200}, {698, 140}, {87, 100}, {523, 60}, {87, 100}, {415, 200}, {87, 200}, {349, 140}, {87, 100}, {523, 60}, {87, 100}, - {440, 200}, {110, 200}, {110, 200}, {110, 200} -}; + {440, 200}, {110, 200}, {110, 200}, {110, 200} }; -void IoStartSpeaker(int freq) +void IoStartSpeaker(ulong freq) { uchar temp; - uint pitf = 1193180 / freq; + ulong pitf = 1193180 / freq; ulong flags = KePauseIRQs(); IoWriteByteOnPort(0x43, 0xB6); @@ -64,13 +63,13 @@ static inline void IoQuietSpeaker(void) KeRestoreIRQs(flags); } -void IoDoTone(uint tone, uint time) +void IoDoTone(ulong tone, ulong time) { IoStartSpeaker(tone); KeSleep(time); } -static void IoDoToneNoIdt(uint tone, uint time) +static void IoDoToneNoIdt(ulong tone, ulong time) { extern void temporize(void); IoStartSpeaker(tone); @@ -94,10 +93,7 @@ void IoDoBeepNoIdt(void) void IoDoStarWars(void) { for (uint i = 0; i < 40; i++) { - //IoDoTone(score[i].tone, score[i].time); - bprintf(BStdOut, "At %p : %u\n", &(score[i].tone), score[i].tone); - bprintf(BStdOut, "At %p : %u\n", &(score[i].time), score[i].time); - BStdOut->flusher(BStdOut); + IoDoTone(score[i].tone, score[i].time); } IoQuietSpeaker(); diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index dbd0110..1b7ff74 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -59,7 +59,7 @@ void MmInitPaging(void) // STACK GUARD PAGE if ((ulong)(i*KPAGESIZE) == (ulong)BtLoaderInfo.stackEndAddr) { MmPT[i] = ((ulong)(i*KPAGESIZE)); - MmStackGuards[0] = ((ulong)(i*KPAGESIZE)); + MmStackGuards[0] = ((ulong)(i*KPAGESIZE )); continue; } From 675063840f31ccc8a7121a3949e8be6d2a963a4c Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 9 Jan 2020 00:31:22 +0100 Subject: [PATCH 15/28] Now working on a new paging system --- build/kernel.ld | 1 + kaleid/kernel/init/init.c | 14 ++++++-------- kaleid/kernel/mm/paging.c | 1 + 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/build/kernel.ld b/build/kernel.ld index bcc86e2..7604bd8 100644 --- a/build/kernel.ld +++ b/build/kernel.ld @@ -75,3 +75,4 @@ SECTIONS { kernelEnd = .; } + diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index cee2d63..73e26d3 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -62,21 +62,19 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) // Memory MmInitMemoryMap(); - MmInitPaging(); - MmInitHeap(); MmInitGdt(); + MmInitHeap(); + MmInitPaging(); - // Basics for interrupts + // IDT KeSetupIDT(); KeEnableIRQs(); + + // Interrupt handlers + MmActivatePageHandler(); KeEnableRTC(); KeEnablePIT(); KeGetCpuInfos(); - - // Memory (2) - MmActivatePageHandler(); - - // Drivers IoEnableKeyb(); // Command line (kernel mode) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 1b7ff74..98864f7 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -22,6 +22,7 @@ enum #define RAM_MAX 32 #define NB_4K 150 // * 2 MB +#define USERSPACE 0x40000000 //----------- From 9fde358447c581efe99ee4756204e4555c6f3193 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 9 Jan 2020 18:19:49 +0100 Subject: [PATCH 16/28] New dynamic paging implementation --- include/io/vga.h | 1 + include/mm/mm.h | 15 ++- kaleid/kernel/io/vga.c | 5 + kaleid/kernel/mm/paging.c | 258 ++++++++++++++++++++++-------------- kaleid/kernel/sh/musage.c | 2 +- kaleid/kernel/sh/shell.c | 5 +- kaleid/kernel/sh/testcmds.c | 6 +- 7 files changed, 181 insertions(+), 111 deletions(-) diff --git a/include/io/vga.h b/include/io/vga.h index 319c790..11d7c50 100644 --- a/include/io/vga.h +++ b/include/io/vga.h @@ -59,6 +59,7 @@ extern const char *RtlColorNames[VGA_COLOR_WHITE+1]; #define RtlCharToColor(c) ((c) - 130) uint IoGetScroll(void); +void IoSetScroll(uint); void IoScrollUp(void); void IoScrollDown(void); diff --git a/include/mm/mm.h b/include/mm/mm.h index ed0e57a..b15294e 100644 --- a/include/mm/mm.h +++ b/include/mm/mm.h @@ -172,15 +172,18 @@ void *MmGetStackGuards(char rank); // void *MmTranslateKPageToAddr(void *rank); -// Page directory pointer offset -typedef ulong pdpe_t; - -// Page directory offset -typedef ulong pde_t; - // Page table entry typedef ulong pte_t; +// Page directory offset +typedef pte_t* pde_t; + +// Page directory pointer offset +typedef pde_t* pdpe_t; + +// Page directory L4 pointer offset +typedef pdpe_t* pml4_t; + // paging.asm void MmLoadPML4(void *); void MmEnableWriteProtect(void); diff --git a/kaleid/kernel/io/vga.c b/kaleid/kernel/io/vga.c index 6e4919f..3f46fcc 100644 --- a/kaleid/kernel/io/vga.c +++ b/kaleid/kernel/io/vga.c @@ -113,6 +113,11 @@ uint IoGetScroll(void) return bscroll; } +void IoSetScroll(uint value) +{ + bscroll = 0; +} + void IoScrollDown(void) { BLockBuf(BStdOut); diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 98864f7..2e26ec9 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -1,38 +1,18 @@ #include #include #include +#include #include #include #include #include -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, - MF_DIRTY = 1 << 6, - MF_HUGE = 1 << 7, - MF_NX = 1 << 31 -}; - -#define RAM_MAX 32 -#define NB_4K 150 - // * 2 MB #define USERSPACE 0x40000000 //----------- -volatile pdpe_t MmPML4[512] __attribute__((__aligned__(KPAGESIZE))); - -volatile pde_t MmPDP[512] __attribute__((__aligned__(KPAGESIZE))); - -volatile pde_t MmPD[512 * RAM_MAX] __attribute__((__aligned__(KPAGESIZE)));; - -volatile pte_t MmPT[512 * NB_4K] __attribute__((__aligned__(KPAGESIZE)));; +volatile pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); +volatile pml4_t MmPageMapLevel4Unmasked[512] __attribute__((__aligned__(KPAGESIZE))); extern ulong _text; extern ulong _text_end; @@ -42,6 +22,20 @@ extern ulong _data; extern ulong _data_end; ulong MmStackGuards[2] = { 0 }; +ulong MmVirtLastAddress = 0; + +enum +{ + PRESENT = 1 << 0, + READWRITE = 1 << 1, + USERMODE = 1 << 2, + WRITETHR = 1 << 3, + CACHEDIS = 1 << 4, + ACCESSED = 1 << 5, + DIRTY = 1 << 6, + HUGE = 1 << 7, + NX = 1 << 31 +}; // // Creates our new page table structure and loads it @@ -49,80 +43,129 @@ ulong MmStackGuards[2] = { 0 }; void MmInitPaging(void) { extern MemoryMap_t memoryMap; + pdpe_t *MmPDP; + pde_t *MmPD; + pte_t *MmPT; + ulong index; + ulong lastKernelAddr = (ulong)(_heap_start + _heap_max); + ulong firstDirectoryAddr = 0; + ulong lastDirectoryAddr = 0; + + // Maximum PHYSICAL address in memory ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; - memzero((void *)&MmPML4[0], sizeof(MmPML4)); - memzero((void *)&MmPDP[0], sizeof(MmPDP)); - memzero((void *)&MmPD[0], sizeof(MmPD)); - memzero((void *)&MmPT[0], sizeof(MmPT)); + // Difference between the end of kernel and the begin of userspace + ulong diffKernUsr = (ulong)USERSPACE - lastKernelAddr; - 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] = ((ulong)(i*KPAGESIZE )); - continue; - } + // Maximum VIRTUAL address in memory + MmVirtLastAddress = phRamSize + diffKernUsr; - // ENOMEM like - if ((ulong)(i*KPAGESIZE) > (ulong)phRamSize) { - break; - } + DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress); - // STACK GARD PAGE - if ((ulong)(i*KPAGESIZE) == (ulong)BtLoaderInfo.kernelEndAddr) { - MmPT[i] = ((ulong)(i*KPAGESIZE)); - MmStackGuards[1] = ((ulong)(i*KPAGESIZE)); - continue; - } + memzero((void *)&MmPageMapLevel4[0], sizeof(MmPageMapLevel4)); + memzero((void *)&MmPageMapLevel4Unmasked[0], sizeof(MmPageMapLevel4)); - // TEXT - if ((ulong)(i*KPAGESIZE) >= (ulong)&_text && (ulong)(i*KPAGESIZE) <= (ulong)&_text_end) { - MmPT[i] = ((ulong)(i*KPAGESIZE))| MF_PRESENT; - continue; - } - // RODATA - if ((ulong)(i*KPAGESIZE) >= (ulong)&_rodata && (ulong)(i*KPAGESIZE) <= (ulong)&_rodata_end) { - MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_WRITETHR; - continue; - } + for (ulong curAddrPML4 = 0; + curAddrPML4 < MmVirtLastAddress; + curAddrPML4 += ((ulong)KPAGESIZE * 0x8000000)) { + // Create an entry in PML4 each 512GB + // 0x8000000 = 512 ^ 3 - // DATA - if ((ulong)(i*KPAGESIZE) >= (ulong)&_data && (ulong)(i*KPAGESIZE) <= (ulong)&_data_end) { - MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_WRITETHR | MF_READWRITE; - continue; - } + MmPDP = (pdpe_t *)malloc(512*sizeof(pde_t)); - MmPT[i] = ((ulong)(i*KPAGESIZE)) | MF_PRESENT | MF_WRITETHR | MF_READWRITE; + if (!firstDirectoryAddr) { + firstDirectoryAddr = (ulong)MmPDP; + } + + index = (curAddrPML4 / ((ulong)KPAGESIZE * 0x8000000)) % 512; + + //DebugLog("\t\t\t\tPDP %d : %p\n", index, MmPDP); + MmPageMapLevel4[index] = (pdpe_t *)((ulong)MmPDP | PRESENT | READWRITE); + + for (ulong curAddrPDP = curAddrPML4; + curAddrPDP < (curAddrPML4 + ((ulong)KPAGESIZE * 0x8000000)) && + curAddrPDP < MmVirtLastAddress; + curAddrPDP += ((ulong)KPAGESIZE * 0x40000)) { + // Create an intry in PDP each 1GB + // 0x40000 = 512 ^ 2 + + MmPD = (pde_t *)malloc(512*sizeof(pde_t)); + + index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512; + + //DebugLog("\t\t\t\tPD %d : %p\n", index, MmPD); + MmPDP[index] = (pde_t *)((ulong)MmPD | PRESENT | READWRITE); + + for (ulong curAddrPD = curAddrPDP; + curAddrPD < (curAddrPDP + ((ulong)KPAGESIZE * 0x40000)) && + curAddrPD < MmVirtLastAddress; + curAddrPD += ((ulong)KPAGESIZE * 0x200)) { + // Create an intry in PD each 2MB + // 0x200 = 512 + + MmPT = (pte_t *)malloc(512*sizeof(pte_t)); + + index = (curAddrPD / ((ulong)KPAGESIZE * 0x200)) % 512; + + //DebugLog("\t\t\t\tPT %d : %p\n", index, MmPT); + MmPD[index] = (pte_t *)((ulong)MmPT | PRESENT | READWRITE); + + for (ulong curAddrPT = curAddrPD; + curAddrPT < (curAddrPD + ((ulong)KPAGESIZE * 0x200)) && + curAddrPT < MmVirtLastAddress; + curAddrPT += (ulong)KPAGESIZE) { + // Create an entry in PT each page of 4KB + + index = (curAddrPT / ((ulong)KPAGESIZE)) % 512; + + //DebugLog("\t\t\t\tPage %d : %p\n", index, curAddrPT); + + // STACK GUARD PAGE */ + if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) { + MmPT[index] = (ulong)curAddrPT | PRESENT; + MmStackGuards[0] = (ulong)curAddrPT; + //DebugLog("\tStack Guard at %p\n", curAddrPT); + } + else if ((ulong)curAddrPT == (ulong)BtLoaderInfo.kernelEndAddr) { + MmPT[index] = (ulong)curAddrPT | PRESENT; + MmStackGuards[1] = (ulong)curAddrPT; + //DebugLog("\tStack Guard at %p\n", curAddrPT); + } + // SECTION .TEXT PROTECTION + else if ((ulong)curAddrPT >= (ulong)&_text && (ulong)curAddrPT <= (ulong)&_text_end) { + MmPT[index] = (ulong)curAddrPT | PRESENT; + //DebugLog("\tSection .text at %p\n", curAddrPT); + } + // SECTION .DATA PROTECTION + else if ((ulong)curAddrPT >= (ulong)&_data && (ulong)curAddrPT <= (ulong)&_data_end) { + MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | READWRITE; + //DebugLog("\tSection .data at %p\n", curAddrPT); + } + // SECTION .RODATA PROTECTION + else if ((ulong)curAddrPT >= (ulong)&_rodata && (ulong)curAddrPT <= (ulong)&_rodata_end) { + MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR; + //DebugLog("\tSection .rodata at %p\n", curAddrPT); + } + else if ((ulong)curAddrPT <= lastKernelAddr) { + MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; + } + else if ((ulong)curAddrPT >= USERSPACE) { + MmPT[index] = ((ulong)curAddrPT - diffKernUsr) | READWRITE | USERMODE; // Not present for instance + + if ((ulong)curAddrPT == USERSPACE) { + DebugLog("\tMapped userspace at %p\n", curAddrPT); + } + } + } + } + } } + lastDirectoryAddr = MmPT; - 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; - } - - for (volatile int i = 0; i < RAM_MAX; i++) { - MmPDP[i] = (ulong)(&MmPD[i*512])| MF_PRESENT | MF_READWRITE; - } - - MmPML4[0] = (ulong)(&MmPDP[0])| MF_PRESENT | MF_READWRITE; - - MmLoadPML4((void *)MmPML4); - MmEnableWriteProtect(); - DebugLog("\tPage RO from %p to %p\n", (ulong)&_text, (ulong)&_text_end); - DebugLog("\tPage RO from %p to %p\n", (ulong)&_rodata, (ulong)&_rodata_end); - //DebugLog("\tPaging tables initialized at %p, %p\n", &MmPD, &MmPT); - //DebugLog("\tStack Guards at %p, %p\n", MmStackGuards[0], MmStackGuards[1]); + MmLoadPML4((void *)MmPageMapLevel4); + //MmEnableWriteProtect(); + DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr)/MB); } // Returns the rank of the Stack Guards @@ -131,12 +174,6 @@ void *MmGetStackGuards(char rank) return (void *)MmStackGuards[(int)rank]; } -// Returns an address corresponding to the PT rank -void *MmTranslateKPageToAddr(void *rank) -{ - return (void *)MmPT[(ulong)rank]; -} - // // Page fault handler // @@ -144,10 +181,10 @@ static void PagingHandler(ISRFrame_t *regs) { ulong StackGuardOne = (ulong)MmGetStackGuards(0); ulong StackGuardTwo = (ulong)MmGetStackGuards(1); - if ((regs->cr2 >= StackGuardOne) && (regs->cr2 <= StackGuardOne + 4*KB) && (regs->rsp <= regs->cr2)) { + if ((regs->cr2 >= StackGuardOne) && (regs->cr2 <= StackGuardOne + KPAGESIZE) && (regs->rsp <= regs->cr2)) { bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Stack Underflow\n\n" - " Double Fault Error code : %#x (%b)\n" + " Page Fault Error code : %#x (%b)\n" " Stack Guard bypassed : %#x", VGA_COLOR_LIGHT_RED, @@ -155,10 +192,10 @@ static void PagingHandler(ISRFrame_t *regs) regs->ErrorCode, StackGuardOne ); - } else if ((regs->cr2 >= StackGuardTwo) && (regs->cr2 <= StackGuardTwo + 4*KB) && (regs->rsp >= regs->cr2)) { + } else if ((regs->cr2 >= StackGuardTwo) && (regs->cr2 <= StackGuardTwo + KPAGESIZE) && (regs->rsp >= regs->cr2)) { bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Stack Overflow\n\n" - " Double Fault Error code : %#x (%b)\n" + " Page Fault Error code : %#x (%b)\n" " Stack Guard bypassed : %#x", VGA_COLOR_LIGHT_RED, @@ -166,13 +203,34 @@ static void PagingHandler(ISRFrame_t *regs) regs->ErrorCode, StackGuardTwo ); - } else { - //XXX page fault - bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x%x] Irrecoverable Kernel Page Fault at %p\n\n" - " Error code : 0x%x (%b)", + } else if (regs->cr2 == 0) { + bprintf(BStdOut, + "\n\n%CPANIC\n[ISR 0x8] Null vector exception !\n\n" + " Page Fault Error code : %#x (%b)\n", VGA_COLOR_LIGHT_RED, regs->intNo, + regs->ErrorCode, + regs->ErrorCode + ); + } else if (regs->cr2 >= MmVirtLastAddress || regs->cr2 <= 0) { + bprintf(BStdOut, + "\n\n%CPANIC\n[ISR 0x8] Out of bound of the address space at %p !\n\n" + " End of the address space : %p\n" + " Page Fault Error code : %#x (%b)\n", + + VGA_COLOR_LIGHT_RED, + regs->cr2, + MmVirtLastAddress, + regs->ErrorCode, + regs->ErrorCode + ); + } else { + //XXX page fault + bprintf(BStdOut, "\n\n%CPANICC\n[ISR 0x8] Irrecoverable Kernel Page Fault at %p\n\n" + " Error code : 0x%x (%b)", + + VGA_COLOR_LIGHT_RED, regs->cr2, regs->ErrorCode, regs->ErrorCode diff --git a/kaleid/kernel/sh/musage.c b/kaleid/kernel/sh/musage.c index 6de60af..b7fba04 100644 --- a/kaleid/kernel/sh/musage.c +++ b/kaleid/kernel/sh/musage.c @@ -40,7 +40,7 @@ error_t CmdMemUsage(int argc, char **argv, char *cmdline) ulong flags = KePauseIRQs(); heap_start = (size_t)_heap_start; - heap_end = (size_t)_heap_end; + heap_end = (size_t)_heap_start; heap_max = _heap_max; KeRestoreIRQs(flags); diff --git a/kaleid/kernel/sh/shell.c b/kaleid/kernel/sh/shell.c index 99fde03..c9d16ce 100644 --- a/kaleid/kernel/sh/shell.c +++ b/kaleid/kernel/sh/shell.c @@ -126,9 +126,8 @@ void ShStartShell(void) default: - while (IoGetScroll() > 0) { - IoScrollDown(); - } + IoSetScroll(1); + IoScrollDown(); *bufptr++ = (char)ch; diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 9c05d3c..a5be6fb 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -191,7 +191,11 @@ error_t CmdHelpTest(int argc, char **argv, char *cmdline) error_t CmdPF(int argc, char **argv, char *cmdline) { - ulong *address = (ulong*)(ulong)atoi(argv[1]); + ulong *address = (ulong*)(ulong)atoul(argv[1]); + + if (!address) { + address = (ulong *)0x12fdc3000 - 1 ; + } KernLog("Provoking Page Fault at %#x\n", address); From 2677c200f596c35466950c703a2c6bcf1adff603 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 9 Jan 2020 20:58:57 +0100 Subject: [PATCH 17/28] New dynamic paging implementation (2) --- include/asm.h | 2 +- include/mm/mm.h | 9 ++- kaleid/kernel/mm/paging.c | 120 ++++++++++++++++++++++++++++++------ kaleid/kernel/sh/testcmds.c | 34 +++++++++- 4 files changed, 142 insertions(+), 23 deletions(-) diff --git a/include/asm.h b/include/asm.h index 15f8bc5..936ae50 100644 --- a/include/asm.h +++ b/include/asm.h @@ -136,7 +136,7 @@ static inline ulong KeReadStsc(void) { return ((ulong)edx << 32) + eax; } -static inline void KeFlushTlbSingle(unsigned long addr) +static inline void KeFlushTlbSingle(ulong addr) { asm volatile("invlpg (%0)" ::"r" (addr) : "memory"); } diff --git a/include/mm/mm.h b/include/mm/mm.h index b15294e..5b2fc5a 100644 --- a/include/mm/mm.h +++ b/include/mm/mm.h @@ -158,6 +158,9 @@ extern void MmLoadGdt(GdtPtr_t *gdtPtr, ushort tssOffset); // extern void MmStoreGdt(void); +// +// Paging misc +// void MmInitPaging(void); void MmActivatePageHandler(void); @@ -168,9 +171,10 @@ void MmActivatePageHandler(void); void *MmGetStackGuards(char rank); // -// Translate a virtual address into physical address +// Translate a virtual address into physical address and the opposite // -void *MmTranslateKPageToAddr(void *rank); +void *MmTransVirtToPhyAddr(void*); +void *MmTransPhyToVirtAddr(void* virtualAddr); // Page table entry typedef ulong pte_t; @@ -188,7 +192,6 @@ typedef pdpe_t* pml4_t; void MmLoadPML4(void *); void MmEnableWriteProtect(void); void MmDisableWriteProtect(void); -void *MmGetStackGuards(char rank); //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 2e26ec9..87bc78f 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -7,12 +7,11 @@ #include #include -#define USERSPACE 0x40000000 +#define USERSPACE 0x80000000 //----------- volatile pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); -volatile pml4_t MmPageMapLevel4Unmasked[512] __attribute__((__aligned__(KPAGESIZE))); extern ulong _text; extern ulong _text_end; @@ -34,7 +33,7 @@ enum ACCESSED = 1 << 5, DIRTY = 1 << 6, HUGE = 1 << 7, - NX = 1 << 31 + NX = 1UL << 63 }; // @@ -43,9 +42,9 @@ enum void MmInitPaging(void) { extern MemoryMap_t memoryMap; - pdpe_t *MmPDP; - pde_t *MmPD; - pte_t *MmPT; + pdpe_t *MmPDP = NULL; + pde_t *MmPD = NULL; + pte_t *MmPT = NULL; ulong index; ulong lastKernelAddr = (ulong)(_heap_start + _heap_max); ulong firstDirectoryAddr = 0; @@ -63,8 +62,6 @@ void MmInitPaging(void) DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress); memzero((void *)&MmPageMapLevel4[0], sizeof(MmPageMapLevel4)); - memzero((void *)&MmPageMapLevel4Unmasked[0], sizeof(MmPageMapLevel4)); - for (ulong curAddrPML4 = 0; curAddrPML4 < MmVirtLastAddress; @@ -139,36 +136,123 @@ void MmInitPaging(void) } // SECTION .DATA PROTECTION else if ((ulong)curAddrPT >= (ulong)&_data && (ulong)curAddrPT <= (ulong)&_data_end) { - MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | READWRITE; + MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | READWRITE | NX; //DebugLog("\tSection .data at %p\n", curAddrPT); } // SECTION .RODATA PROTECTION else if ((ulong)curAddrPT >= (ulong)&_rodata && (ulong)curAddrPT <= (ulong)&_rodata_end) { - MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR; + MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | NX; //DebugLog("\tSection .rodata at %p\n", curAddrPT); } + // While we're inside the kernel pages else if ((ulong)curAddrPT <= lastKernelAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; - } - else if ((ulong)curAddrPT >= USERSPACE) { - MmPT[index] = ((ulong)curAddrPT - diffKernUsr) | READWRITE | USERMODE; // Not present for instance - if ((ulong)curAddrPT == USERSPACE) { - DebugLog("\tMapped userspace at %p\n", curAddrPT); + if ((ulong)curAddrPT == lastKernelAddr) { + DebugLog("\tLast page of kernel at %p\n", curAddrPT); } } + // While we're inside the userspace pages + else if ((ulong)curAddrPT >= USERSPACE) { + MmPT[index] = ((ulong)curAddrPT); // Not present for instance + + if ((ulong)curAddrPT == USERSPACE) { + DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT + diffKernUsr); + } + } + else { + MmPT[index] = 0; + } + + KeFlushTlbSingle(curAddrPT); } } } } - lastDirectoryAddr = MmPT; + lastDirectoryAddr = (ulong)MmPT; MmLoadPML4((void *)MmPageMapLevel4); - //MmEnableWriteProtect(); + + MmEnableWriteProtect(); DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr)/MB); } +// +// Translates a virtual address to its physical equivalent +// +void *MmTransVirtToPhyAddr(void* virtualAddr) +{ + ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); + + if (virtAddrPage > MmVirtLastAddress) { + KeStartPanic("MmTransVirtToPhyAddr() Out of bound of the address space !"); + } + + pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512] & ~(KPAGESIZE - 1)); + DebugLog("pdp : %p\n", pdp); + pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] & ~(KPAGESIZE - 1)); + DebugLog("pd : %p\n", pd); + pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~(KPAGESIZE - 1)); + DebugLog("pt : %p\n", pt); + + ulong page = (ulong)pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512]; + DebugLog("page : %p\n", page); + + if (page == (page & ~(KPAGESIZE - 1))) { + return NULL; + } + + return (void*)((page & ~(KPAGESIZE - 1))+ ((ulong)virtualAddr - (ulong)virtAddrPage)); +} + +void *MmTransPhyToVirtAddr(void* physicalAddr) +{ + return (void*)0; +} + +// +// Add flags to a page +// +void MmSetPage(void* virtualAddr, ulong flags) +{ + ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); + + if (virtAddrPage > MmVirtLastAddress) { + KeStartPanic("MmSetPage() Out of bound of the address space !"); + } + + pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)] & ~(KPAGESIZE - 1)); + pde_t *pd = (pde_t*)( (ulong)pdp[virtAddrPage / ((ulong)KPAGESIZE * 0x40000)] & ~(KPAGESIZE - 1)); + pte_t *pt = (pte_t*)( (ulong)pd[virtAddrPage / ((ulong)KPAGESIZE * 0x200)] & ~(KPAGESIZE - 1)); + + pt[virtAddrPage / ((ulong)KPAGESIZE)] |= flags; + + KeFlushTlbSingle(virtAddrPage); +} + +// +// Remove flags of a page +// +void MmUnsetPage(void* virtualAddr, ulong flags) +{ + ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); + + if (virtAddrPage > MmVirtLastAddress) { + KeStartPanic("MmUnsetPage() Out of bound of the address space !"); + } + + pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)] & ~(KPAGESIZE - 1)); + pde_t *pd = (pde_t*)( (ulong)pdp[virtAddrPage / ((ulong)KPAGESIZE * 0x40000)] & ~(KPAGESIZE - 1)); + pte_t *pt = (pte_t*)( (ulong)pd[virtAddrPage / ((ulong)KPAGESIZE * 0x200)] & ~(KPAGESIZE - 1)); + + pt[virtAddrPage / ((ulong)KPAGESIZE)] &= (~flags); + + KeFlushTlbSingle(virtAddrPage); +} + +// // Returns the rank of the Stack Guards +// void *MmGetStackGuards(char rank) { return (void *)MmStackGuards[(int)rank]; @@ -227,7 +311,7 @@ static void PagingHandler(ISRFrame_t *regs) ); } else { //XXX page fault - bprintf(BStdOut, "\n\n%CPANICC\n[ISR 0x8] Irrecoverable Kernel Page Fault at %p\n\n" + bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Page Fault at %p\n\n" " Error code : 0x%x (%b)", VGA_COLOR_LIGHT_RED, diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index a5be6fb..411e17e 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -189,12 +189,40 @@ error_t CmdHelpTest(int argc, char **argv, char *cmdline) return EOK; } +error_t CmdPageTranslateVirtToPhy(int argc, char **argv, char *cmdline) +{ + void *address = (void*)atoul(argv[1]); + + if (!(void*)atoul(argv[1])) { + address = (void *)0x80000001; + } + + void *translation = MmTransVirtToPhyAddr(address); + + KernLog("Translation of %p is %p\n", address, translation); + return EOK; +} + +error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline) +{ + void *address = (void*)atoul(argv[1]); + + /* if (!(void*)atoul(argv[1])) { */ + /* address = (ulong *)0x80000000; */ + /* } */ + + void *translation = MmTransPhyToVirtAddr(address); + + KernLog("Translation of %p is %p\n", address, translation); + return EOK; +} + error_t CmdPF(int argc, char **argv, char *cmdline) { ulong *address = (ulong*)(ulong)atoul(argv[1]); if (!address) { - address = (ulong *)0x12fdc3000 - 1 ; + address = (ulong *)0x80000000; } KernLog("Provoking Page Fault at %#x\n", address); @@ -253,6 +281,10 @@ static Command_t testcmdtable[] = { "dmp", CmdDumpMem, "Dump 1MB of memory starting from addr"}, { "help", CmdHelpTest, "Show this message" }, { "div", CmdFloatDiv, "Float div. Usage : div a b. Returns a/b"}, + { "transvtp", CmdPageTranslateVirtToPhy, "Translate a virtual to" + " physical address (paging)"}, + { "transptv", CmdPageTranslatePhyToVirt, "Translate a physical to" + " virtual address (paging)"}, { "pf", CmdPF, "Provoke a PF. Usage: pfault
"}, { "shell", CmdShell, "Start a new shell (nested)", }, { "stkov", CmdStackOverflow, "Provoke a stack overflow" }, From 1073397b153e9ab2b282d95d2a55b25ac5a55e18 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 9 Jan 2020 21:02:42 +0100 Subject: [PATCH 18/28] New dynamic paging implementation (2) #67 --- include/mm/mm.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/mm/mm.h b/include/mm/mm.h index 5b2fc5a..a1a6c45 100644 --- a/include/mm/mm.h +++ b/include/mm/mm.h @@ -176,6 +176,18 @@ void *MmGetStackGuards(char rank); void *MmTransVirtToPhyAddr(void*); void *MmTransPhyToVirtAddr(void* virtualAddr); +// +// Set flags to a page +// +void MmSetPage(void* virtualAddr, ulong flags); +void MmUnSetPage(void* virtualAddr, ulong flags); + +// +// Map a page +// +void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) +void MmUnmapPage(void* virtualAddr); + // Page table entry typedef ulong pte_t; From ad5b70b08abafb615e905ac445de343ff4c36857 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 9 Jan 2020 22:17:44 +0100 Subject: [PATCH 19/28] New dynamic paging implementation (3) #67 --- include/mm/mm.h | 2 +- kaleid/kernel/mm/paging.c | 37 ++++++++++++++++++++++++++---------- kaleid/kernel/sh/testcmds.c | Bin 8685 -> 8755 bytes 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/include/mm/mm.h b/include/mm/mm.h index a1a6c45..50657f1 100644 --- a/include/mm/mm.h +++ b/include/mm/mm.h @@ -185,7 +185,7 @@ void MmUnSetPage(void* virtualAddr, ulong flags); // // Map a page // -void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) +void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags); void MmUnmapPage(void* virtualAddr); // Page table entry diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 87bc78f..d3265e5 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -54,12 +54,12 @@ void MmInitPaging(void) ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; // Difference between the end of kernel and the begin of userspace - ulong diffKernUsr = (ulong)USERSPACE - lastKernelAddr; + ulong diffKernUsr = (ulong)USERSPACE - lastKernelAddr - KPAGESIZE; // Maximum VIRTUAL address in memory MmVirtLastAddress = phRamSize + diffKernUsr; - DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress); + //DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress); memzero((void *)&MmPageMapLevel4[0], sizeof(MmPageMapLevel4)); @@ -149,15 +149,15 @@ void MmInitPaging(void) MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; if ((ulong)curAddrPT == lastKernelAddr) { - DebugLog("\tLast page of kernel at %p\n", curAddrPT); + //DebugLog("\tLast page of kernel at %p\n", curAddrPT); } } // While we're inside the userspace pages else if ((ulong)curAddrPT >= USERSPACE) { - MmPT[index] = ((ulong)curAddrPT); // Not present for instance + MmPT[index] = ((ulong)curAddrPT - diffKernUsr) | PRESENT; // Not present for instance if ((ulong)curAddrPT == USERSPACE) { - DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT + diffKernUsr); + DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr); } } else { @@ -189,14 +189,14 @@ void *MmTransVirtToPhyAddr(void* virtualAddr) } pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512] & ~(KPAGESIZE - 1)); - DebugLog("pdp : %p\n", pdp); + DebugLog("pdp\t: %p\n", pdp); pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] & ~(KPAGESIZE - 1)); - DebugLog("pd : %p\n", pd); + DebugLog("pd\t: %p\n", pd); pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~(KPAGESIZE - 1)); - DebugLog("pt : %p\n", pt); + DebugLog("pt\t: %p\n", pt); ulong page = (ulong)pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512]; - DebugLog("page : %p\n", page); + DebugLog("page (with flags): %p\n", page); if (page == (page & ~(KPAGESIZE - 1))) { return NULL; @@ -311,7 +311,7 @@ static void PagingHandler(ISRFrame_t *regs) ); } else { //XXX page fault - bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Kernel Page Fault at %p\n\n" + bprintf(BStdOut, "\n\n%CPANIC\n[ISR 0x8] Irrecoverable Page Fault at %p\n\n" " Error code : 0x%x (%b)", VGA_COLOR_LIGHT_RED, @@ -321,6 +321,23 @@ static void PagingHandler(ISRFrame_t *regs) ); } + bprintf(BStdOut, "\n Description : "); + + if (regs->ErrorCode & PRESENT) { + bprintf(BStdOut, "Page-protection violation "); + } else { + bprintf(BStdOut, "Non present page "); + } + if (regs->ErrorCode & READWRITE) { + bprintf(BStdOut, "during write access "); + } else { + bprintf(BStdOut, "during read access "); + } + if (regs->ErrorCode & (1 << 3)) + bprintf(BStdOut, "from userspace "); + if (regs->ErrorCode & (1 << 4)) + bprintf(BStdOut, "after instruction fetching "); + KeBrkDumpRegisters(regs); BStdOut->flusher(BStdOut); diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 411e17e89c8edf802d203212df03d72145a16eb0..025bdcc8497bce31ebf6caeb4228a8d685baab2c 100644 GIT binary patch delta 99 zcmaFsyxC=gA*%ob8rbZ}S|~VqH9x0`U%o`^YF>$gl|o5TVqS4hVo7FRx+(t delta 51 zcmdn&^458SA*(=1Mt*6DLTXuRo Date: Thu, 9 Jan 2020 22:34:38 +0100 Subject: [PATCH 20/28] New dynamic paging implementation (3) #67 --- kaleid/kernel/mm/paging.c | 88 +++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 36 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index d3265e5..653ffb9 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -36,6 +36,8 @@ enum NX = 1UL << 63 }; +//----------- + // // Creates our new page table structure and loads it // @@ -177,26 +179,38 @@ void MmInitPaging(void) DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr)/MB); } +// +// Get a page from an address +// +static pte_t MmGetPageDescriptor(void *virtualAddr) +{ + ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); + + if (virtAddrPage > MmVirtLastAddress) { + KeStartPanic("MmSetPage() Out of bound of the address space !"); + } + + pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512] & ~(KPAGESIZE - 1)); + //DebugLog("pdp\t: %p\n", pdp); + pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] & ~(KPAGESIZE - 1)); + //DebugLog("pd\t: %p\n", pd); + pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~(KPAGESIZE - 1)); + //DebugLog("pt\t: %p\n", pt); + + ulong page = (ulong)pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512]; + //DebugLog("page (with flags): %p\n", page); + + return page; +} + + // // Translates a virtual address to its physical equivalent // void *MmTransVirtToPhyAddr(void* virtualAddr) { ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); - - if (virtAddrPage > MmVirtLastAddress) { - KeStartPanic("MmTransVirtToPhyAddr() Out of bound of the address space !"); - } - - pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512] & ~(KPAGESIZE - 1)); - DebugLog("pdp\t: %p\n", pdp); - pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] & ~(KPAGESIZE - 1)); - DebugLog("pd\t: %p\n", pd); - pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~(KPAGESIZE - 1)); - DebugLog("pt\t: %p\n", pt); - - ulong page = (ulong)pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512]; - DebugLog("page (with flags): %p\n", page); + pte_t page = MmGetPageDescriptor(virtualAddr); if (page == (page & ~(KPAGESIZE - 1))) { return NULL; @@ -215,19 +229,11 @@ void *MmTransPhyToVirtAddr(void* physicalAddr) // void MmSetPage(void* virtualAddr, ulong flags) { - ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); + pte_t page = MmGetPageDescriptor(virtualAddr); - if (virtAddrPage > MmVirtLastAddress) { - KeStartPanic("MmSetPage() Out of bound of the address space !"); - } + page |= flags; - pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)] & ~(KPAGESIZE - 1)); - pde_t *pd = (pde_t*)( (ulong)pdp[virtAddrPage / ((ulong)KPAGESIZE * 0x40000)] & ~(KPAGESIZE - 1)); - pte_t *pt = (pte_t*)( (ulong)pd[virtAddrPage / ((ulong)KPAGESIZE * 0x200)] & ~(KPAGESIZE - 1)); - - pt[virtAddrPage / ((ulong)KPAGESIZE)] |= flags; - - KeFlushTlbSingle(virtAddrPage); + KeFlushTlbSingle(page); } // @@ -235,21 +241,31 @@ void MmSetPage(void* virtualAddr, ulong flags) // void MmUnsetPage(void* virtualAddr, ulong flags) { - ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); + pte_t page = MmGetPageDescriptor(virtualAddr); - if (virtAddrPage > MmVirtLastAddress) { - KeStartPanic("MmUnsetPage() Out of bound of the address space !"); - } + page &= (~flags); - pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)] & ~(KPAGESIZE - 1)); - pde_t *pd = (pde_t*)( (ulong)pdp[virtAddrPage / ((ulong)KPAGESIZE * 0x40000)] & ~(KPAGESIZE - 1)); - pte_t *pt = (pte_t*)( (ulong)pd[virtAddrPage / ((ulong)KPAGESIZE * 0x200)] & ~(KPAGESIZE - 1)); - - pt[virtAddrPage / ((ulong)KPAGESIZE)] &= (~flags); - - KeFlushTlbSingle(virtAddrPage); + KeFlushTlbSingle(page); } +// +// Map a page in memory +// +void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) +{ + +} + +// +// Unmap a page in memory +// +void MmUnmapPage(void* virtualAddr) +{ + +} + +//----------- + // // Returns the rank of the Stack Guards // From 5ae13125bcd1f84d016aa82f0beadf37199d5be7 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 9 Jan 2020 22:42:41 +0100 Subject: [PATCH 21/28] New dynamic paging implementation (4) #67 --- kaleid/kernel/mm/paging.c | 16 ++++++++++++---- kaleid/kernel/sh/testcmds.c | Bin 8755 -> 8755 bytes 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 653ffb9..4ba948e 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -182,7 +182,7 @@ void MmInitPaging(void) // // Get a page from an address // -static pte_t MmGetPageDescriptor(void *virtualAddr) +static pte_t MmGetPageDescriptorFromVirtual(void *virtualAddr) { ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); @@ -210,7 +210,7 @@ static pte_t MmGetPageDescriptor(void *virtualAddr) void *MmTransVirtToPhyAddr(void* virtualAddr) { ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); - pte_t page = MmGetPageDescriptor(virtualAddr); + pte_t page = MmGetPageDescriptorFromVirtual(virtualAddr); if (page == (page & ~(KPAGESIZE - 1))) { return NULL; @@ -229,7 +229,7 @@ void *MmTransPhyToVirtAddr(void* physicalAddr) // void MmSetPage(void* virtualAddr, ulong flags) { - pte_t page = MmGetPageDescriptor(virtualAddr); + pte_t page = MmGetPageDescriptorFromVirtual(virtualAddr); page |= flags; @@ -241,7 +241,7 @@ void MmSetPage(void* virtualAddr, ulong flags) // void MmUnsetPage(void* virtualAddr, ulong flags) { - pte_t page = MmGetPageDescriptor(virtualAddr); + pte_t page = MmGetPageDescriptorFromVirtual(virtualAddr); page &= (~flags); @@ -253,7 +253,11 @@ void MmUnsetPage(void* virtualAddr, ulong flags) // void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) { + pte_t page = MmGetPageDescriptorFromVirtual(virtualAddr); + page = ((ulong)physicalAddr & ~(KPAGESIZE - 1)) | flags; + + KeFlushTlbSingle(page); } // @@ -261,7 +265,11 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) // void MmUnmapPage(void* virtualAddr) { + pte_t page = MmGetPageDescriptorFromVirtual(virtualAddr); + page = 0; + + KeFlushTlbSingle(page); } //----------- diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 025bdcc8497bce31ebf6caeb4228a8d685baab2c..d0421dde283c5139f95e1ffe3c2ee8e15c827adb 100644 GIT binary patch delta 29 kcmdn&ve{*WA*(=1Mt*6DLTXuRo Date: Thu, 9 Jan 2020 23:01:00 +0100 Subject: [PATCH 22/28] Dynamic paging functionnal #67 --- kaleid/kernel/mm/paging.c | 34 +++++++++++++++---------------- kaleid/kernel/sh/testcmds.c | 40 +++++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 23 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 4ba948e..c8f581a 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -182,7 +182,7 @@ void MmInitPaging(void) // // Get a page from an address // -static pte_t MmGetPageDescriptorFromVirtual(void *virtualAddr) +static pte_t *MmGetPageDescriptorFromVirtual(void *virtualAddr) { ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); @@ -197,7 +197,7 @@ static pte_t MmGetPageDescriptorFromVirtual(void *virtualAddr) pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~(KPAGESIZE - 1)); //DebugLog("pt\t: %p\n", pt); - ulong page = (ulong)pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512]; + pte_t *page = &pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512]; //DebugLog("page (with flags): %p\n", page); return page; @@ -210,13 +210,13 @@ static pte_t MmGetPageDescriptorFromVirtual(void *virtualAddr) void *MmTransVirtToPhyAddr(void* virtualAddr) { ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); - pte_t page = MmGetPageDescriptorFromVirtual(virtualAddr); + pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); - if (page == (page & ~(KPAGESIZE - 1))) { + if (*page == (*page & ~(KPAGESIZE - 1))) { return NULL; } - return (void*)((page & ~(KPAGESIZE - 1))+ ((ulong)virtualAddr - (ulong)virtAddrPage)); + return (void*)((*page & ~(KPAGESIZE - 1))+ ((ulong)virtualAddr - (ulong)virtAddrPage)); } void *MmTransPhyToVirtAddr(void* physicalAddr) @@ -229,11 +229,11 @@ void *MmTransPhyToVirtAddr(void* physicalAddr) // void MmSetPage(void* virtualAddr, ulong flags) { - pte_t page = MmGetPageDescriptorFromVirtual(virtualAddr); + pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); - page |= flags; + *page |= flags; - KeFlushTlbSingle(page); + KeFlushTlbSingle(*page); } // @@ -241,11 +241,11 @@ void MmSetPage(void* virtualAddr, ulong flags) // void MmUnsetPage(void* virtualAddr, ulong flags) { - pte_t page = MmGetPageDescriptorFromVirtual(virtualAddr); + pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); - page &= (~flags); + *page &= (~flags); - KeFlushTlbSingle(page); + KeFlushTlbSingle(*page); } // @@ -253,11 +253,11 @@ void MmUnsetPage(void* virtualAddr, ulong flags) // void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) { - pte_t page = MmGetPageDescriptorFromVirtual(virtualAddr); + pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); - page = ((ulong)physicalAddr & ~(KPAGESIZE - 1)) | flags; + *page = ((ulong)physicalAddr & ~(KPAGESIZE - 1)) | flags; - KeFlushTlbSingle(page); + KeFlushTlbSingle(*page); } // @@ -265,11 +265,11 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) // void MmUnmapPage(void* virtualAddr) { - pte_t page = MmGetPageDescriptorFromVirtual(virtualAddr); + pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); - page = 0; + *page = 0; - KeFlushTlbSingle(page); + KeFlushTlbSingle(*page); } //----------- diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index d0421dd..9fb9b11 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -204,6 +204,38 @@ error_t CmdPageTranslateVirtToPhy(int argc, char **argv, char *cmdline) return EOK; } +enum +{ + PRESENT = 1 << 0, + READWRITE = 1 << 1, + USERMODE = 1 << 2, + WRITETHR = 1 << 3, + CACHEDIS = 1 << 4, + ACCESSED = 1 << 5, + DIRTY = 1 << 6, + HUGE = 1 << 7, + NX = 1UL << 63 +}; + +error_t CmdPageMap(int argc, char **argv, char *cmdline) +{ + void *virtual = (void*)atoul(argv[1]); + void *physical = (void*)atoul(argv[2]); + + MmMapPage(virtual, physical, PRESENT | READWRITE); + + return EOK; +} + +error_t CmdPageUnmap(int argc, char **argv, char *cmdline) +{ + void *virtual = (void*)atoul(argv[1]); + + MmUnmapPage(virtual); + + return EOK; +} + error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline) { void *address = (void*)atoul(argv[1]); @@ -222,10 +254,6 @@ error_t CmdPF(int argc, char **argv, char *cmdline) { ulong *address = (ulong*)(ulong)atoul(argv[1]); - if (!address) { - address = (ulong *)0x80000000; - } - KernLog("Provoking Page Fault at %#x\n", address); KernLog("It contained %#x\n", *address); @@ -284,8 +312,8 @@ static Command_t testcmdtable[] = { "div", CmdFloatDiv, "Float div. Usage : div a b. Returns a/b"}, { "transvtp", CmdPageTranslateVirtToPhy, "Translate a virtual to" " physical address (paging)"}, - { "transptv", CmdPageTranslatePhyToVirt, "Translate a physical to" - " virtual address (paging)"}, + { "pmap", CmdPageMap, "Map a page to given physical addr" }, + { "punmap", CmdPageUnmap, "Unmap a page" }, { "pf", CmdPF, "Provoke a PF. Usage: pfault
"}, { "shell", CmdShell, "Start a new shell (nested)", }, { "stkov", CmdStackOverflow, "Provoke a stack overflow" }, From 38c0f14332f82e1c909e63065fb94212a15a94f7 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 9 Jan 2020 23:51:55 +0100 Subject: [PATCH 23/28] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 7d3b06a..93be98b 100644 --- a/README.md +++ b/README.md @@ -42,3 +42,8 @@ To compile and install, simply use at the root of this project, with XXX the ima make install installdisk=XXX ``` +#### Screenshot +![OS/K Started](https://www.os-k.eu/images/screen3.png) + + + From 0fa7d465111ac988cdbb095a473789605a01fe61 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 10 Jan 2020 12:51:23 +0100 Subject: [PATCH 24/28] minor modification to test commands --- kaleid/kernel/mm/paging.c | 2 +- kaleid/kernel/sh/testcmds.c | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index c8f581a..af127e2 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -166,7 +166,7 @@ void MmInitPaging(void) MmPT[index] = 0; } - KeFlushTlbSingle(curAddrPT); + //KeFlushTlbSingle(curAddrPT); } } } diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 9fb9b11..00a68a3 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -112,7 +112,7 @@ error_t CmdDumpATASect(int argc, char **argv, char *cmdline) error_t CmdDumpMem(int argc, char **argv, char *cmdline) { char sector[1024] = {0}; - char *address = (char*)atol(argv[1]); + char *address = (char*)strtoul(argv[1], NULL, 16); int nb = 1; //atoi(argv[2]); int x = 0; int step = 16; @@ -193,7 +193,7 @@ error_t CmdPageTranslateVirtToPhy(int argc, char **argv, char *cmdline) { void *address = (void*)atoul(argv[1]); - if (!(void*)atoul(argv[1])) { + if (!(void*)strtoul(argv[1], NULL, 16)) { KernLog("No argument : translating the userspace address\n"); address = (void *)0x80000000; } @@ -219,8 +219,8 @@ enum error_t CmdPageMap(int argc, char **argv, char *cmdline) { - void *virtual = (void*)atoul(argv[1]); - void *physical = (void*)atoul(argv[2]); + void *virtual = (void*)strtoul(argv[1], NULL, 16); + void *physical = (void*)strtoul(argv[2], NULL, 16); MmMapPage(virtual, physical, PRESENT | READWRITE); @@ -229,7 +229,7 @@ error_t CmdPageMap(int argc, char **argv, char *cmdline) error_t CmdPageUnmap(int argc, char **argv, char *cmdline) { - void *virtual = (void*)atoul(argv[1]); + void *virtual = (void*)strtoul(argv[1], NULL, 16); MmUnmapPage(virtual); @@ -238,7 +238,7 @@ error_t CmdPageUnmap(int argc, char **argv, char *cmdline) error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline) { - void *address = (void*)atoul(argv[1]); + void *address = (void*)strtoul(argv[1], NULL, 16); /* if (!(void*)atoul(argv[1])) { */ /* address = (ulong *)0x80000000; */ @@ -252,7 +252,7 @@ error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline) error_t CmdPF(int argc, char **argv, char *cmdline) { - ulong *address = (ulong*)(ulong)atoul(argv[1]); + ulong *address = (ulong*)(ulong)strtoul(argv[1], NULL, 16); KernLog("Provoking Page Fault at %#x\n", address); From 36ce243b8c04c34858db37a42ac3003de492ffe3 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 10 Jan 2020 13:36:33 +0100 Subject: [PATCH 25/28] Bug with heap ? --- kaleid/kernel/init/init.c | 8 +++++--- kaleid/kernel/ke/idt.c | 1 - kaleid/kernel/mm/paging.c | 37 +++++++++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 73e26d3..12d3792 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -63,13 +63,15 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) // Memory MmInitMemoryMap(); MmInitGdt(); - MmInitHeap(); - MmInitPaging(); - // IDT + // Interrupts KeSetupIDT(); KeEnableIRQs(); + // Memory (2) + MmInitHeap(); + MmInitPaging(); + // Interrupt handlers MmActivatePageHandler(); KeEnableRTC(); diff --git a/kaleid/kernel/ke/idt.c b/kaleid/kernel/ke/idt.c index 251cf7a..d6fc0fc 100644 --- a/kaleid/kernel/ke/idt.c +++ b/kaleid/kernel/ke/idt.c @@ -26,7 +26,6 @@ #include #include #include -#include #include IdtEntry_t idt[256] = { 0 }; diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index af127e2..0bfd06f 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -11,7 +11,8 @@ //----------- -volatile pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); +static pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); +static ulong *MmPhysicalPageTable; extern ulong _text; extern ulong _text_end; @@ -51,6 +52,7 @@ void MmInitPaging(void) ulong lastKernelAddr = (ulong)(_heap_start + _heap_max); ulong firstDirectoryAddr = 0; ulong lastDirectoryAddr = 0; + ulong phDirSize = 0; // Maximum PHYSICAL address in memory ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; @@ -64,6 +66,10 @@ void MmInitPaging(void) //DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress); memzero((void *)&MmPageMapLevel4[0], sizeof(MmPageMapLevel4)); + phDirSize = (phRamSize / KPAGESIZE)*sizeof(ulong); + + //MmPhysicalPageTable = (ulong*)malloc((phRamSize / KPAGESIZE)*sizeof(ulong)); + DebugLog("\t\tRam %u MB, pagesize %u KB, size %u MB\n", phRamSize / MB, KPAGESIZE / KB, phDirSize / MB); for (ulong curAddrPML4 = 0; curAddrPML4 < MmVirtLastAddress; @@ -157,6 +163,7 @@ void MmInitPaging(void) // While we're inside the userspace pages else if ((ulong)curAddrPT >= USERSPACE) { MmPT[index] = ((ulong)curAddrPT - diffKernUsr) | PRESENT; // Not present for instance + //MmPhysicalPageTable[(ulong)curAddrPT - diffKernUsr] = curAddrPT; if ((ulong)curAddrPT == USERSPACE) { DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr); @@ -174,9 +181,9 @@ void MmInitPaging(void) lastDirectoryAddr = (ulong)MmPT; MmLoadPML4((void *)MmPageMapLevel4); - MmEnableWriteProtect(); - DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr)/MB); + + DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB); } // @@ -221,7 +228,7 @@ void *MmTransVirtToPhyAddr(void* virtualAddr) void *MmTransPhyToVirtAddr(void* physicalAddr) { - return (void*)0; + return (void*)MmPhysicalPageTable[(ulong)physicalAddr]; } // @@ -272,6 +279,28 @@ void MmUnmapPage(void* virtualAddr) KeFlushTlbSingle(*page); } +// +// Kernel Page allocator +// +void *MmKAllocPageBlock(void *start) { + pte_t *startPage = MmGetPageDescriptorFromVirtual(start); + + //for (ulong curPage = 0; curPage < ) + + return NULL; +} + +// +// User page allocator +// +void *MmUAllocPageBlock(void *start) { + pte_t *startPage = MmGetPageDescriptorFromVirtual(start); + + //for (ulong curPage = 0; curPage < ) + + return NULL; +} + //----------- // From c6efa5c9a4eaac83ac253843a629bcad3c4a9185 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 10 Jan 2020 13:50:29 +0100 Subject: [PATCH 26/28] Bug with heap ? --- kaleid/kernel/mm/paging.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index c8f581a..008986f 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -124,27 +124,27 @@ void MmInitPaging(void) if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT; MmStackGuards[0] = (ulong)curAddrPT; - //DebugLog("\tStack Guard at %p\n", curAddrPT); + DebugLog("\tStack Guard at %p\n", curAddrPT); } else if ((ulong)curAddrPT == (ulong)BtLoaderInfo.kernelEndAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT; MmStackGuards[1] = (ulong)curAddrPT; - //DebugLog("\tStack Guard at %p\n", curAddrPT); + DebugLog("\tStack Guard at %p\n", curAddrPT); } // SECTION .TEXT PROTECTION else if ((ulong)curAddrPT >= (ulong)&_text && (ulong)curAddrPT <= (ulong)&_text_end) { MmPT[index] = (ulong)curAddrPT | PRESENT; - //DebugLog("\tSection .text at %p\n", curAddrPT); + DebugLog("\tSection .text at %p\n", curAddrPT); } // SECTION .DATA PROTECTION else if ((ulong)curAddrPT >= (ulong)&_data && (ulong)curAddrPT <= (ulong)&_data_end) { MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | READWRITE | NX; - //DebugLog("\tSection .data at %p\n", curAddrPT); + DebugLog("\tSection .data at %p\n", curAddrPT); } // SECTION .RODATA PROTECTION else if ((ulong)curAddrPT >= (ulong)&_rodata && (ulong)curAddrPT <= (ulong)&_rodata_end) { MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | NX; - //DebugLog("\tSection .rodata at %p\n", curAddrPT); + DebugLog("\tSection .rodata at %p\n", curAddrPT); } // While we're inside the kernel pages else if ((ulong)curAddrPT <= lastKernelAddr) { From 102f3f027976c2ff675cafdd03da76e59be4894a Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Fri, 10 Jan 2020 13:57:42 +0100 Subject: [PATCH 27/28] up --- kaleid/kernel/sh/musage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kaleid/kernel/sh/musage.c b/kaleid/kernel/sh/musage.c index b7fba04..6de60af 100644 --- a/kaleid/kernel/sh/musage.c +++ b/kaleid/kernel/sh/musage.c @@ -40,7 +40,7 @@ error_t CmdMemUsage(int argc, char **argv, char *cmdline) ulong flags = KePauseIRQs(); heap_start = (size_t)_heap_start; - heap_end = (size_t)_heap_start; + heap_end = (size_t)_heap_end; heap_max = _heap_max; KeRestoreIRQs(flags); From f10f53ff0865f95296a0ff75a5b1fdb2e96dda0a Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 10 Jan 2020 19:25:33 +0100 Subject: [PATCH 28/28] Bug resolved : alignment issue --- kaleid/kernel/mm/paging.c | 33 ++++++++++++++++++++++----------- kaleid/kernel/sh/testcmds.c | 2 ++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 0bfd06f..00bdf27 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -11,8 +11,8 @@ //----------- -static pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); -static ulong *MmPhysicalPageTable; +pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); +ulong *MmPhysicalPageTable; extern ulong _text; extern ulong _text_end; @@ -48,8 +48,7 @@ void MmInitPaging(void) pdpe_t *MmPDP = NULL; pde_t *MmPD = NULL; pte_t *MmPT = NULL; - ulong index; - ulong lastKernelAddr = (ulong)(_heap_start + _heap_max); + ulong index, xedni; ulong firstDirectoryAddr = 0; ulong lastDirectoryAddr = 0; ulong phDirSize = 0; @@ -58,6 +57,7 @@ void MmInitPaging(void) ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; // Difference between the end of kernel and the begin of userspace + ulong lastKernelAddr = (ulong)(_heap_start + _heap_max); ulong diffKernUsr = (ulong)USERSPACE - lastKernelAddr - KPAGESIZE; // Maximum VIRTUAL address in memory @@ -66,10 +66,10 @@ void MmInitPaging(void) //DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress); memzero((void *)&MmPageMapLevel4[0], sizeof(MmPageMapLevel4)); - phDirSize = (phRamSize / KPAGESIZE)*sizeof(ulong); + phDirSize = ((phRamSize / KPAGESIZE)*sizeof(ulong) + KPAGESIZE) & ( ~(KPAGESIZE - 1)); - //MmPhysicalPageTable = (ulong*)malloc((phRamSize / KPAGESIZE)*sizeof(ulong)); - DebugLog("\t\tRam %u MB, pagesize %u KB, size %u MB\n", phRamSize / MB, KPAGESIZE / KB, phDirSize / MB); + MmPhysicalPageTable = (ulong*)malloc(phDirSize); + //DebugLog("\t\tRam %u MB, pagesize %u KB, size %u MB\n", phRamSize / MB, KPAGESIZE / KB, phDirSize / MB); for (ulong curAddrPML4 = 0; curAddrPML4 < MmVirtLastAddress; @@ -123,38 +123,45 @@ void MmInitPaging(void) // Create an entry in PT each page of 4KB index = (curAddrPT / ((ulong)KPAGESIZE)) % 512; + xedni = (curAddrPT / ((ulong)KPAGESIZE)); //DebugLog("\t\t\t\tPage %d : %p\n", index, curAddrPT); // STACK GUARD PAGE */ if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT; + MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmStackGuards[0] = (ulong)curAddrPT; //DebugLog("\tStack Guard at %p\n", curAddrPT); } else if ((ulong)curAddrPT == (ulong)BtLoaderInfo.kernelEndAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT; + MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmStackGuards[1] = (ulong)curAddrPT; //DebugLog("\tStack Guard at %p\n", curAddrPT); } // SECTION .TEXT PROTECTION else if ((ulong)curAddrPT >= (ulong)&_text && (ulong)curAddrPT <= (ulong)&_text_end) { MmPT[index] = (ulong)curAddrPT | PRESENT; + MmPhysicalPageTable[xedni] = (ulong)curAddrPT; //DebugLog("\tSection .text at %p\n", curAddrPT); } // SECTION .DATA PROTECTION else if ((ulong)curAddrPT >= (ulong)&_data && (ulong)curAddrPT <= (ulong)&_data_end) { MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | READWRITE | NX; + MmPhysicalPageTable[xedni] = (ulong)curAddrPT; //DebugLog("\tSection .data at %p\n", curAddrPT); } // SECTION .RODATA PROTECTION else if ((ulong)curAddrPT >= (ulong)&_rodata && (ulong)curAddrPT <= (ulong)&_rodata_end) { MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | NX; + MmPhysicalPageTable[xedni] = (ulong)curAddrPT; //DebugLog("\tSection .rodata at %p\n", curAddrPT); } // While we're inside the kernel pages else if ((ulong)curAddrPT <= lastKernelAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; + MmPhysicalPageTable[xedni] = (ulong)curAddrPT; if ((ulong)curAddrPT == lastKernelAddr) { //DebugLog("\tLast page of kernel at %p\n", curAddrPT); @@ -163,7 +170,8 @@ void MmInitPaging(void) // While we're inside the userspace pages else if ((ulong)curAddrPT >= USERSPACE) { MmPT[index] = ((ulong)curAddrPT - diffKernUsr) | PRESENT; // Not present for instance - //MmPhysicalPageTable[(ulong)curAddrPT - diffKernUsr] = curAddrPT; + xedni = (((ulong)curAddrPT - diffKernUsr) / ((ulong)KPAGESIZE)); + MmPhysicalPageTable[xedni] = (ulong)curAddrPT; if ((ulong)curAddrPT == USERSPACE) { DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr); @@ -173,7 +181,7 @@ void MmInitPaging(void) MmPT[index] = 0; } - //KeFlushTlbSingle(curAddrPT); + KeFlushTlbSingle(curAddrPT); } } } @@ -181,7 +189,7 @@ void MmInitPaging(void) lastDirectoryAddr = (ulong)MmPT; MmLoadPML4((void *)MmPageMapLevel4); - MmEnableWriteProtect(); + //MmEnableWriteProtect(); DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB); } @@ -228,7 +236,10 @@ void *MmTransVirtToPhyAddr(void* virtualAddr) void *MmTransPhyToVirtAddr(void* physicalAddr) { - return (void*)MmPhysicalPageTable[(ulong)physicalAddr]; + ulong phyAddrPage = (ulong)physicalAddr & ( ~(KPAGESIZE - 1)); + return (void*)( MmPhysicalPageTable[(ulong)physicalAddr + / ((ulong)KPAGESIZE) + + ((ulong)physicalAddr - phyAddrPage) ] ); } // diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 00a68a3..f528b71 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -312,6 +312,8 @@ static Command_t testcmdtable[] = { "div", CmdFloatDiv, "Float div. Usage : div a b. Returns a/b"}, { "transvtp", CmdPageTranslateVirtToPhy, "Translate a virtual to" " physical address (paging)"}, + { "transptv", CmdPageTranslatePhyToVirt, "Translate a physical to" + " virtual address (paging)"}, { "pmap", CmdPageMap, "Map a page to given physical addr" }, { "punmap", CmdPageUnmap, "Unmap a page" }, { "pf", CmdPF, "Provoke a PF. Usage: pfault
"},