From 586fe3bb8085eff7ac7a8bcf13e8932beaeb9b8b Mon Sep 17 00:00:00 2001 From: ChUrl Date: Mon, 4 Jul 2022 16:04:55 +0200 Subject: [PATCH] fix paging bugs in writeprotection --- c_os/kernel/Paging.cc | 10 +++++++--- c_os/kernel/Paging.h | 22 ++++++++++++++++++++++ c_os/main.cc | 16 +++++++++------- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/c_os/kernel/Paging.cc b/c_os/kernel/Paging.cc index a45633f..8cb00f2 100644 --- a/c_os/kernel/Paging.cc +++ b/c_os/kernel/Paging.cc @@ -92,7 +92,7 @@ unsigned int* pg_alloc_page() { // pruefe ob Page frei if (((*p_page) & PAGE_RESERVED) == 0) { *p_page = (*p_page | PAGE_RESERVED); - return (unsigned int*)(i << 12); + return (unsigned int*)(i << 12); // Address without flags (Offset 0) } } return 0; @@ -108,8 +108,10 @@ void pg_write_protect_page(unsigned int* p_page) { /* hier muss Code eingefügt werden */ + unsigned int* page = (unsigned int*)PAGE_TABLE + ((unsigned int)p_page >> 12); // Pagetable entry + unsigned int mask = PAGE_WRITEABLE; // fill to 32bit - *p_page = *p_page & ~mask; // set writable to 0 + *page = *page & ~mask; // set writable to 0 } /***************************************************************************** @@ -121,8 +123,10 @@ void pg_notpresent_page(unsigned int* p_page) { /* hier muss Code eingefügt werden */ + unsigned int* page = (unsigned int*)PAGE_TABLE + ((unsigned int)p_page >> 12); // Pagetable entry + unsigned int mask = PAGE_PRESENT; - *p_page = *p_page & ~mask; // set present to 0 + *page = *page & ~mask; // set present to 0 } /***************************************************************************** diff --git a/c_os/kernel/Paging.h b/c_os/kernel/Paging.h index 3d48591..0ad467f 100644 --- a/c_os/kernel/Paging.h +++ b/c_os/kernel/Paging.h @@ -52,6 +52,28 @@ #ifndef __Paging_include__ #define __Paging_include__ +// Bits fuer Eintraege in der Page-Table +#define PAGE_PRESENT 0x001 +#define PAGE_WRITEABLE 0x002 +#define PAGE_BIGSIZE 0x080 +#define PAGE_RESERVED 0x800 // Bit 11 ist frei fuer das OS + +// Adresse des Page-Directory (benoetigt 4 KB) +#define PAGE_DIRECTORY 0x200000 + +// Adresse der Page-Table (benoetigt 4 KB) +#define PAGE_TABLE 0x201000 + +// Start- und End-Adresse der 4 KB Seiten die durch die Page-Table adressiert werden +#define FST_ALLOCABLE_PAGE 0x202000 +#define LST_ALLOCABLE_PAGE 0x2FF000 + +// Externe Funktionen in startup.asm +extern "C" { + void paging_on(unsigned int* p_pdir); // Paging einschalten + void invalidate_tlb_entry(unsigned int* ptr); // Page in TLB invalid. +} + // ativiert paging extern void pg_init(); diff --git a/c_os/main.cc b/c_os/main.cc index d213b14..18f9da8 100755 --- a/c_os/main.cc +++ b/c_os/main.cc @@ -56,27 +56,29 @@ int main() { pg_init(); // Trigger Bluescreen - // kout << "Trigger Bluescreen, if you can read this it didn't work" << endl; + kout << "Trigger Bluescreen, if you can read this it didn't work" << endl; // BlueScreen 1 // asm("int $3"); // BlueScreen 2 - // unsigned int* page = pg_alloc_page(); - // *page = 42; // Should work - // pg_write_protect_page(page); - // *page = 42; // should trigger BlueScreen + unsigned int* page = pg_alloc_page(); + *page = 42; + pg_write_protect_page(page); + invalidate_tlb_entry(page); // If we don't invalidate after first access the write protection + // won't work as no lookup is performed (address in tlb) + *page = 42; // We map logical to physical 1:1 so no need to do any lookup + // If tlb is invalidated this access produces a pagefault // Demo threads anlegen // scheduler.ready(new HelloWorldThread()); - scheduler.ready(new CoopThreadDemo()); + // scheduler.ready(new CoopThreadDemo()); // scheduler.ready(new VBEdemo()); // Switch to VESA graphics mode // Scheduler starten (schedule() erzeugt den Idle-Thread) scheduler.schedule(); // TODO: Use templates for queue so threads don't have to be casted down from chain - // TODO: Move scrollback control to thread // Scheduler doesn't return return 0;