Added basic paging.h & paging.c

This commit is contained in:
2025-03-28 11:30:43 +01:00
parent f73a6f9ccb
commit 5cee647ff0
6 changed files with 93 additions and 36 deletions
+17 -2
View File
@@ -1,7 +1,9 @@
#include <bootloader.h>
#include <multiboot.h>
#include <kernel.h>
#include <string.h>
struct boot_memmap memmap[BOOT_MAX_MMAP_ENTRIES];
struct boot_info bootloader;
// TODO:
@@ -39,8 +41,21 @@ int boot_initMB1(void* ptr, uint32_t magic) {
// mmap_* fields are valid
if (mbi->flags & MULTIBOOT_INFO_MEM_MAP) {
bootloader.memmap = mbi->mmap_addr;
bootloader.memmapLen = mbi->mmap_length;
bootloader.memmap = &memmap[0];
size_t i = 0;
multiboot_memory_map_t* entry = (multiboot_memory_map_t*)mbi->mmap_addr;
uint32_t mapLim = mbi->mmap_addr + mbi->mmap_length;
while ((uint32_t)entry < mapLim) {
bootloader.memmap[i].base = entry->addr;
bootloader.memmap[i].size = entry->len;
bootloader.memmap[i].type = (uint8_t)entry->type;
entry++;
i++;
}
bootloader.memmapLen = i;
}
// TODO: drives_*
+3 -19
View File
@@ -1,8 +1,10 @@
#include <bootloader.h>
#include <kernel.h>
#include <paging.h>
#include <serial.h>
#include <gdt.h>
#include <isr.h>
#include <pmm.h>
#include <tty.h>
// TODO: Commented out stuff
@@ -27,27 +29,9 @@ void kmain(void* ptr, uint32_t magic) {
gdt_init();
isr_init();
// pag_init();
pag_init();
debugf("Basic initialization complete!\n");
struct boot_memmap* entry;
for (
entry = (struct boot_memmap*)bootloader.memmap;
(uint64_t)entry < bootloader.memmap + bootloader.memmapLen;
entry = (struct boot_memmap*)((uint64_t)entry + entry->skip + sizeof(entry))
) {
debugf("[Region] base: %x size: %x b type: ", entry->base, entry->size);
switch (entry->type) {
case BOOT_MMAP_AVAILABLE: debugf("Available\n"); break;
case BOOT_MMAP_RESERVED: debugf("Reserved\n"); break;
case BOOT_MMAP_ACPI_RECLAIM: debugf("ACPI Reclaim\n"); break;
case BOOT_MMAP_NVS: debugf("NVS\n"); break;
case BOOT_MMAP_BADRAM: debugf("Bad RAM\n"); break;
default: debugf("Invalid type\n"); break;
}
}
panic("kmain() reached end.");
+27
View File
@@ -0,0 +1,27 @@
#include <kernel.h>
#include <string.h>
#include <paging.h>
// Invalidates the pagedir entry in the CPUs TLB cache
static inline void invalidate(uint32_t vaddr) {
__asm__ volatile ("invlpg %0" : : "m"(vaddr));
}
int pag_init(void) {
// Unmap first MB
init_pagedir[0] = 0;
invalidate(0);
/*
* Recursive table mapping
* We map the last pagetab to the pagedir itself, so it acts
* as a pagetab even though it's a pagedir. This maps it to
* the virtual address 0xFFFFF000.
*/
init_pagedir[1023] = ((uint32_t)init_pagedir - 0xC0000000) |
PAGE_FLAG_PRESENT | PAGE_FLAG_WRITE;
invalidate(0xFFFFF000);
return 0;
}
+17 -12
View File
@@ -1,22 +1,27 @@
#include <multiboot.h>
#include <bootloader.h>
#include <kernel.h>
#include <string.h>
#include <pmm.h>
// Helpers
#define SET_BLOCK(_b) memmap[(_b) / 32] |= (1 << ((_b) % 32))
#define USET_BLOCK(_b) memmap[(_b) / 32] &= ~(1 << ((_b) % 32))
#define TEST_BLOCK(_b) ((memmap[(_b) % 32] & (1 << ((_b) % 32))) ? 1 : 0)
// Bitmap & helpers
uint32_t* bitmap = NULL;
#define SET_BLOCK(_b) bitmap[(_b) / 32] |= (1 << ((_b) % 32))
#define USET_BLOCK(_b) bitmap[(_b) / 32] &= ~(1 << ((_b) % 32))
#define TEST_BLOCK(_b) ((bitmap[(_b) % 32] & (1 << ((_b) % 32))) ? 1 : 0)
// Memory manager bitmap
uint32_t* memmap = NULL;
// Status
size_t usedBlocks = 0;
size_t maxBlocks = 0;
// TODO: improve these hardcoded values
int pmm_init(void) {
memmap = NULL; // Memory map location
usedBlocks = 0;
maxBlocks = 0; // TODO
size_t totalmem = (bootloader.memLower + bootloader.memUpper) * 1024;
maxBlocks = totalmem / PMM_BS;
usedBlocks = maxBlocks;
// TODO:
bitmap = (uint32_t*)NULL;
memset(bitmap, 0, maxBlocks / PMM_BPB);
return 0;
}
@@ -27,7 +32,7 @@ int findFreeBlocks(size_t blockCount) {
uint32_t freeCount = 0;
for (size_t i = 0; i < max; i++) {
// We don't have to check, cause it's all reserved
if (memmap[i] == 0xffffffff) continue;
if (bitmap[i] == 0xffffffff) continue;
for (uint8_t j = 0; j < 32; j++) {
if (TEST_BLOCK(i * PMM_BPB + j)) freeCount++;