Compare commits

...

2 commits

Author SHA1 Message Date
6261af8e3a 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.
2025-07-15 23:14:33 -04:00
e1369902f7 started fixing physmem. page fault when allocating. 2025-07-15 17:21:50 -04:00
6 changed files with 28 additions and 15 deletions

View file

@ -10,5 +10,6 @@
void outb(uint16_t port, uint8_t data);
uint8_t inb(uint16_t port);
void cr3_reload();
#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
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

View file

@ -40,6 +40,8 @@ void kern_main(uint32_t multiboot_magic, mb_info_t* multiboot_info)
//AND OUR DEFAULT OUTPUT IS:
default_output = default_COM;
printf(default_output, "Output schemes loaded!\n");
printf(default_output, "currently executing in kern_main() at %X\n",kern_main);
printf(default_output, "Entry eax:%X\n", multiboot_magic);
if(multiboot_magic != 0x2BADB002) {
println(default_output, "Bootloader not multiboot1 compliant! Needed for mmap, etc. Can't work without it, kthxbye!");
@ -58,10 +60,8 @@ void kern_main(uint32_t multiboot_magic, mb_info_t* multiboot_info)
return;
}
/* This is broken while i implement virtual memory/paging
unsigned int pages_allocated = build_bitmap((mb_mmap_entry_t*)multiboot_info->mmap_addr, multiboot_info->mmap_length);
printf(default_output, "Available mem:%d Pages | %dK", pages_allocated, pages_allocated * 4);
*/
}

View file

@ -19,17 +19,13 @@ void free_page(void* addr) {
unsigned int build_bitmap(mb_mmap_entry_t* mmap, int mmap_size) {
//rather important global pointer to the bitmap
pmem_bitmap = &_kernel_end;
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;

View file

@ -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