Fix braces + init_paging args

This commit is contained in:
2026-03-11 19:58:00 +01:00
parent 9d409317e2
commit 8e2a612d88
29 changed files with 147 additions and 229 deletions

View File

@@ -39,8 +39,7 @@ static uint64_t* alloc_page_table()
{
uint64_t* virt = (uint64_t*)PHYS_TO_VIRT(pmm_alloc());
for (size_t i=0; i<512; i++)
{
for (size_t i=0; i<512; i++) {
virt[i] = 0;
}
return virt;
@@ -70,32 +69,26 @@ void paging_map_page(uint64_t* root_table, uint64_t virt, uint64_t phys, uint64_
// PML4
// If the entry at index is not present, allocate enough space for it
// then populate the entry with correct addr + flags
if (!(root_table[pml4_i] & PTE_PRESENT))
{
if (!(root_table[pml4_i] & PTE_PRESENT)) {
pdpt = alloc_page_table();
root_table[pml4_i] = VIRT_TO_PHYS(pdpt) | PTE_PRESENT | PTE_WRITABLE;
}
else {
} else {
pdpt = (uint64_t *)PHYS_TO_VIRT(root_table[pml4_i] & PTE_ADDR_MASK);
}
// PDPT: same here
if (!(pdpt[pdpt_i] & PTE_PRESENT))
{
if (!(pdpt[pdpt_i] & PTE_PRESENT)) {
pd = alloc_page_table();
pdpt[pdpt_i] = VIRT_TO_PHYS(pd) | PTE_PRESENT | PTE_WRITABLE;
}
else {
} else {
pd = (uint64_t *)PHYS_TO_VIRT(pdpt[pdpt_i] & PTE_ADDR_MASK);
}
// PD: and here
if (!(pd[pd_i] & PTE_PRESENT))
{
if (!(pd[pd_i] & PTE_PRESENT)) {
pt = alloc_page_table();
pd[pd_i] = VIRT_TO_PHYS(pt) | PTE_PRESENT | PTE_WRITABLE;
}
else {
} else {
pt = (uint64_t *)PHYS_TO_VIRT(pd[pd_i] & PTE_ADDR_MASK);
}
@@ -109,9 +102,7 @@ void paging_map_page(uint64_t* root_table, uint64_t virt, uint64_t phys, uint64_
uint64_t kernel_phys_base;
uint64_t kernel_virt_base;
extern struct boot_context boot_ctx;
void paging_init()
void paging_init(struct boot_context boot_ctx)
{
// We should map the kernel, GDT, IDT, stack, framebuffer.
// Optionally we could map ACPI tables (we can find them in the Limine memmap)
@@ -129,31 +120,25 @@ void paging_init()
// Find max physical address from limine memmap
uint64_t max_phys = 0;
for (uint64_t i=0; i<boot_ctx.mmap->entry_count; i++)
{
for (uint64_t i=0; i<boot_ctx.mmap->entry_count; i++) {
struct limine_memmap_entry* entry = boot_ctx.mmap->entries[i];
if (entry->length == 0)
{
if (entry->length == 0) {
continue;
}
uint64_t top = entry->base + entry->length;
if (top > max_phys)
{
if (top > max_phys) {
max_phys = top;
}
}
// 4GB
if (max_phys > 0x100000000)
{
DEBUG("WARNING: max_phys capped to 4GB (0x100000000) (from max_phys=%p)", max_phys);
max_phys = 0x100000000;
if (max_phys > PAGING_MAX_PHYS) {
DEBUG("WARNING: max_phys capped to 4GB (%x) (from max_phys=%p)", PAGING_MAX_PHYS, max_phys);
max_phys = PAGING_MAX_PHYS;
}
// HHDM map up to max_phys or 4GB, whichever is smaller, using given offset
for (uint64_t i=0; i<max_phys; i += PAGE_SIZE)
{
//paging_kmap_page(i+hhdm_off, i, PTE_WRITABLE);
// HHDM map up to max_phys or PAGING_MAX_PHYS, whichever is smaller, using given offset
for (uint64_t i=0; i<max_phys; i += PAGE_SIZE) {
paging_map_page(kernel_pml4, i+hhdm_off, i, PTE_WRITABLE | PTE_PRESENT);
page_count++;
}
@@ -163,9 +148,7 @@ void paging_init()
// SOME DAY when we want a safer kernel we should map .text as Read/Exec
// .rodata as Read and .data as Read/Write
// For now who gives a shit, let's RWX all kernel
for (uint64_t i = 0; i < KERNEL_SIZE; i += PAGE_SIZE)
{
//paging_kmap_page(kernel_virt_base+i, kernel_phys_base+i, PTE_WRITABLE);
for (uint64_t i = 0; i < KERNEL_SIZE; i += PAGE_SIZE) {
paging_map_page(kernel_pml4, kernel_virt_base+i, kernel_phys_base+i, PTE_WRITABLE);
page_count++;
}
@@ -178,9 +161,7 @@ void paging_init()
uint64_t fb_pages = (fb_size + PAGE_SIZE-1)/PAGE_SIZE;
// Map the framebuffer (with cache-disable & write-through)
for (uint64_t i=0; i<fb_pages; i++)
{
//paging_kmap_page(fb_virt+i*PAGE_SIZE, fb_phys+i*PAGE_SIZE, PTE_WRITABLE | PTE_PCD | PTE_PWT);
for (uint64_t i=0; i<fb_pages; i++) {
paging_map_page(kernel_pml4, fb_virt+i*PAGE_SIZE, fb_phys+i*PAGE_SIZE, PTE_WRITABLE | PTE_PCD | PTE_PWT);
page_count++;
}

View File

@@ -12,8 +12,9 @@
#include <stdint.h>
#include <limine.h>
#include "mem/heap/kheap.h"
#include <kernel.h>
void paging_init();
void paging_init(struct boot_context boot_ctx);
void paging_map_page(uint64_t* root_table, uint64_t virt, uint64_t phys, uint64_t flags);
// To swap root page tables

View File

@@ -38,12 +38,10 @@ static void pmm_find_biggest_usable_region(struct limine_memmap_response* memmap
uint64_t offset = hhdm->offset;
DEBUG("Usable Memory:");
for (size_t i=0; i<memmap->entry_count; i++)
{
for (size_t i=0; i<memmap->entry_count; i++) {
struct limine_memmap_entry* entry = memmap->entries[i];
if (entry->type == LIMINE_MEMMAP_USABLE)
{
if (entry->type == LIMINE_MEMMAP_USABLE) {
DEBUG("0x%p-0x%p mapped at 0x%p-0x%p", entry->base, entry->base+entry->length,
entry->base+offset, entry->base+entry->length+offset);
if (entry->length > length_max)
@@ -66,8 +64,7 @@ static uintptr_t g_freelist = 0;
uintptr_t pmm_alloc()
{
if (!g_freelist)
{
if (!g_freelist) {
panic(NULL, "PMM is out of memory!");
}
uintptr_t addr = g_freelist;
@@ -89,19 +86,17 @@ static void pmm_init_freelist()
uint64_t end = ALIGN_DOWN(biggest_entry->base + biggest_entry->length, PAGE_SIZE);
uint64_t page_count=0;
for (uint64_t addr = base; addr < end; addr += PAGE_SIZE)
{
for (uint64_t addr = base; addr < end; addr += PAGE_SIZE) {
pmm_free(addr);
//DEBUG("page %u lives at phys 0x%p (virt 0x%p)", page_count, addr, PHYS_TO_VIRT(addr));
page_count++;
}
DEBUG("%u frames in freelist, available for use (%u bytes)", page_count, page_count*PAGE_SIZE);
}
void pmm_init(struct limine_memmap_response* memmap, struct limine_hhdm_response* hhdm)
void pmm_init(struct boot_context boot_ctx)
{
hhdm_off = hhdm->offset;
pmm_find_biggest_usable_region(memmap, hhdm);
hhdm_off = boot_ctx.hhdm->offset;
pmm_find_biggest_usable_region(boot_ctx.mmap, boot_ctx.hhdm);
// Now we have biggest USABLE region,
// so to populate the free list we just iterate through it

View File

@@ -8,8 +8,9 @@
#define PAGING_PMM_H
#include <limine.h>
#include <kernel.h>
void pmm_init(struct limine_memmap_response* memmap, struct limine_hhdm_response* hhdm);
void pmm_init(struct boot_context boot_ctx);
void pmm_free(uintptr_t addr);
uintptr_t pmm_alloc();

View File

@@ -16,8 +16,7 @@ Flags here aren't x86 flags, they are platform-agnostic
kernel-defined flags.
*/
struct vm_object
{
struct vm_object {
uintptr_t base;
size_t length;
size_t flags;