mapping pages

This commit is contained in:
2025-07-27 11:23:29 +03:00
parent 3d7cf62b03
commit 0e62049711

View File

@@ -1,5 +1,6 @@
#include "kernel.h"
#include "common.h"
#include <sys/types.h>
extern char __bss[], __bss_end[], __stack_top[];
extern char __free_ram[], __free_ram_end[];
@@ -265,6 +266,26 @@ void proc_b_entry(void) {
}
}
void map_page(uint32_t *table1, uint32_t vaddr, paddr_t paddr, uint32_t flags) {
if (!is_aligned(vaddr, PAGE_SIZE))
PANIC("Unaligned vaddr %x", vaddr);
if (!is_aligned(paddr, PAGE_SIZE))
PANIC("Unaligned paddr %x", paddr);
uint32_t vpn1 = (vaddr >> 22) & 0x3ff;
if ((table1[vpn1] & PAGE_V) == 0) {
// Create the first level page
uint32_t pt_paddr = alloc_pages(1);
table1[vpn1] = ((pt_paddr / PAGE_SIZE) << 10) | PAGE_V;
}
// Set the second level page table entry to map the physical page
uint32_t vpn0 = (vaddr >> 12) & 0x3ff;
uint32_t *table0 = (uint32_t *) ((table1[vpn1] >> 10) * PAGE_SIZE);
table0[vpn0] = ((paddr / PAGE_SIZE) << 10) | flags | PAGE_V;
}
void kernel_main(void) {
// printf("\n\nHello %s\n", "friend :3");
// printf("60 + 9 = %d, %x\n", 60 + 9, 0x1234abcd);