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.
This commit is contained in:
lordtet 2025-07-15 23:14:33 -04:00
parent e1369902f7
commit 6261af8e3a
5 changed files with 25 additions and 12 deletions

View file

@ -10,5 +10,6 @@
void outb(uint16_t port, uint8_t data); void outb(uint16_t port, uint8_t data);
uint8_t inb(uint16_t port); uint8_t inb(uint16_t port);
void cr3_reload();
#endif #endif

11
include/virtmem.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef VIRTMEM_H
#define VIRTMEM_H
#include <stdint.h>
extern uint32_t global_page_dir;
extern uint32_t kernel_pagetable;
#endif

View file

@ -1,4 +1,4 @@
global outb, inb global outb, inb, invlpg
section .text section .text
outb: outb:
mov dx, [esp+4] mov dx, [esp+4]
@ -11,3 +11,8 @@ section .text
mov dx, [esp+4] mov dx, [esp+4]
in al, dx in al, dx
ret ret
cr3_reload:
mov eax, cr3
mov cr3, eax
ret

View file

@ -21,15 +21,11 @@ unsigned int build_bitmap(mb_mmap_entry_t* mmap, int mmap_size) {
//rather important global pointer to the bitmap //rather important global pointer to the bitmap
pmem_bitmap = (&_kernel_end); pmem_bitmap = (&_kernel_end);
//some variables //some variables
int mmap_end = (uint32_t)mmap + mmap_size; int mmap_end = (uint32_t)mmap + mmap_size;
unsigned int pages_allocated = 0; unsigned int pages_allocated = 0;
uint32_t max_memory = 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. //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; for(mb_mmap_entry_t* mmap_walk = mmap;
(int)mmap_walk < mmap_end; (int)mmap_walk < mmap_end;

View file

@ -25,9 +25,9 @@ section .tss align=16 nobits
resb 104 resb 104
section .bootstrap_tables nobits align=4096 section .bootstrap_tables nobits align=4096
bootstrap_PD: global_page_dir:
resb 4096 resb 4096
bootstrap_PT: kernel_pagetable:
resb 4096 resb 4096
;Actual code. Entry point goes here! ;Actual code. Entry point goes here!
@ -60,7 +60,7 @@ section .lower_text exec
mov ecx, 0x0 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. ;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. ;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: page_alloc_loop:
;If we're lower than our kernel, skip this page. If we've passed our kernel, we're done mapping. ;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: 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. ;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! ;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. ;First identity maps, second maps to higher half.
mov dword [bootstrap_PD - 0xC0000000], bootstrap_PT - 0xC0000000 + 0x003 mov dword [global_page_dir - 0xC0000000], kernel_pagetable - 0xC0000000 + 0x003
mov dword [bootstrap_PD - 0xC0000000 + 4 * (0xC0000000 >> 22)], bootstrap_PT - 0xC0000000 + 0x003 mov dword [global_page_dir - 0xC0000000 + 4 * (0xC0000000 >> 22)], kernel_pagetable - 0xC0000000 + 0x003
;Let's set it! You do this with cr3. ;Let's set it! You do this with cr3.
mov ecx, bootstrap_PD - 0xC0000000 mov ecx, global_page_dir - 0xC0000000
mov cr3, ecx mov cr3, ecx
;Enable paging in cr0 ;Enable paging in cr0