From 6261af8e3aedbc9539410a94fd6351496e18152f Mon Sep 17 00:00:00 2001 From: lordtet Date: Tue, 15 Jul 2025 23:14:33 -0400 Subject: [PATCH] Some changes to names and a convenience ASM function asm.h now can nuke the TLB by refreshing the PD. also changed some symbols to more accurately represent what they are for, such as the kernelspace page tables or the global PD. --- include/asm.h | 1 + include/virtmem.h | 11 +++++++++++ src/asm.s | 7 ++++++- src/physmem.c | 4 ---- src/start.s | 14 +++++++------- 5 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 include/virtmem.h diff --git a/include/asm.h b/include/asm.h index 890aabe..0a3a6ea 100644 --- a/include/asm.h +++ b/include/asm.h @@ -10,5 +10,6 @@ void outb(uint16_t port, uint8_t data); uint8_t inb(uint16_t port); +void cr3_reload(); #endif diff --git a/include/virtmem.h b/include/virtmem.h new file mode 100644 index 0000000..ea445ae --- /dev/null +++ b/include/virtmem.h @@ -0,0 +1,11 @@ +#ifndef VIRTMEM_H +#define VIRTMEM_H +#include + + +extern uint32_t global_page_dir; +extern uint32_t kernel_pagetable; + + + +#endif diff --git a/src/asm.s b/src/asm.s index a6be4fc..766811c 100644 --- a/src/asm.s +++ b/src/asm.s @@ -1,4 +1,4 @@ -global outb, inb +global outb, inb, invlpg section .text outb: mov dx, [esp+4] @@ -11,3 +11,8 @@ section .text mov dx, [esp+4] in al, dx ret + + cr3_reload: + mov eax, cr3 + mov cr3, eax + ret diff --git a/src/physmem.c b/src/physmem.c index d51a08b..ea8c927 100644 --- a/src/physmem.c +++ b/src/physmem.c @@ -21,15 +21,11 @@ unsigned int build_bitmap(mb_mmap_entry_t* mmap, int mmap_size) { //rather important global pointer to the bitmap pmem_bitmap = (&_kernel_end); - - //some variables int mmap_end = (uint32_t)mmap + mmap_size; unsigned int pages_allocated = 0; uint32_t max_memory = 0; - - //How much memory do we even have? Find the highest usable mmap region, and that'll be our bitmap size. for(mb_mmap_entry_t* mmap_walk = mmap; (int)mmap_walk < mmap_end; diff --git a/src/start.s b/src/start.s index 009bce3..9b2c728 100644 --- a/src/start.s +++ b/src/start.s @@ -25,9 +25,9 @@ section .tss align=16 nobits resb 104 section .bootstrap_tables nobits align=4096 - bootstrap_PD: + global_page_dir: resb 4096 - bootstrap_PT: + kernel_pagetable: resb 4096 ;Actual code. Entry point goes here! @@ -60,7 +60,7 @@ section .lower_text exec mov ecx, 0x0 ;edi = our pointer to the current page table entry. Array of 4byte entries. It's at, of course, where we put it earlier. ;Worth noting that it's actually physically at ~1M with the kernel. The pointer, however, is virtually inclined. So we need to adjust. - mov edi, bootstrap_PT - 0xC0000000 + mov edi, kernel_pagetable - 0xC0000000 page_alloc_loop: ;If we're lower than our kernel, skip this page. If we've passed our kernel, we're done mapping. @@ -87,15 +87,15 @@ section .lower_text exec page_alloc_done: ;By now we've mapped the kernel in a page table! Let's map VGA too, we'll need that address later. - mov dword [bootstrap_PT - 0xC0000000 + 1023 * 4], 0x000B8000 | 0x27 + mov dword [kernel_pagetable - 0xC0000000 + 1023 * 4], 0x000B8000 | 0x27 ;We have our page tables mapped to the phys addr of kernel + VGA. Time to add them to our page directory! ;First identity maps, second maps to higher half. - mov dword [bootstrap_PD - 0xC0000000], bootstrap_PT - 0xC0000000 + 0x003 - mov dword [bootstrap_PD - 0xC0000000 + 4 * (0xC0000000 >> 22)], bootstrap_PT - 0xC0000000 + 0x003 + mov dword [global_page_dir - 0xC0000000], kernel_pagetable - 0xC0000000 + 0x003 + mov dword [global_page_dir - 0xC0000000 + 4 * (0xC0000000 >> 22)], kernel_pagetable - 0xC0000000 + 0x003 ;Let's set it! You do this with cr3. - mov ecx, bootstrap_PD - 0xC0000000 + mov ecx, global_page_dir - 0xC0000000 mov cr3, ecx ;Enable paging in cr0