Some unfinished changes + TODO chnages

This commit is contained in:
2025-03-18 20:40:29 +01:00
parent 244b83d370
commit 883cd5c1d4
11 changed files with 333 additions and 205 deletions
+47
View File
@@ -0,0 +1,47 @@
#include <bootloader.h>
#include <multiboot.h>
#include <kernel.h>
struct boot_info bootloader = { 0 };
int boot_init(int type, ...) {
va_list args;
va_start(args, type);
switch (type) {
case BOOT_UNKNOWN: return 1;
case BOOT_MB1: return boot_init_mb1(va_arg(args, void*), va_arg(args, uint32_t));
case BOOT_MB2: return 1;
case BOOT_LIMINE: return 1;
}
// We got an invalid boot protocol
return 1;
}
int boot_init_mb1(void* ptr, uint32_t magic) {
multiboot_info_t* mbi = ptr;
if (magic != 0xBADB002) return 1;
// Memory info
if (mbi->flags & MULTIBOOT_INFO_MEMORY) {
bootloader.memLower = mbi->mem_lower;
bootloader.memUpper = mbi->mem_upper;
}
// Memory map
if (mbi->flags & MULTIBOOT_INFO_MEM_MAP) {
uint32_t memmap = mbi->mmap_addr;
uint32_t offset = 0;
while (offset < mbi->mmap_length) {
multiboot_memory_map_t* entry = (multiboot_memory_map_t*)(memmap + offset);
offset += entry->size;
}
}
return 0;
}
+6 -7
View File
@@ -1,19 +1,17 @@
#include <bootloader.h>
#include <kernel.h>
#include <serial.h>
#include <pmm.h>
#include <tty.h>
#define _hlt() while (1) __asm__ volatile ("cli; hlt");
/* TODO: Commented out stuff */
__noreturn
void kmain(void* ptr, uint32_t magic) {
// Init with MultiBoot 1, this populates the global bootloader struct
//if (boot_init(BOOT_MB1, ptr, magic) != 0) panic("Unsupporred bootloader.");
if (boot_init(BOOT_MB1, ptr, magic) != 0) panic("Unsupporred bootloader.");
// Serial console for debugging
serial_init(COM1);
serial_puts(COM1, "\x1b[0H\x1b[Jkern.\n");
debugf("\x1b[H\x1b[J\x1b[0mkern. \x1b[35m(serial console)\x1b[0m\n\n");
// VGA tty thingy
tty_init();
@@ -29,8 +27,9 @@ void kmain(void* ptr, uint32_t magic) {
debugf("Basic initialization complete!\n");
/* TODO: Commented out stuff */
panic("kmain() reached end.");
// Temporary loop since panic() does not halt
while (1);
}
+91 -1
View File
@@ -1,4 +1,94 @@
#include <multiboot.h>
#include <kernel.h>
#include <pmm.h>
int pmm_init() { return 0; }
// 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)
// Memory manager bitmap
uint32_t* memmap = NULL;
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
return 0;
}
int findFreeBlocks(size_t blockCount) {
if (blockCount == 0) return -1;
uint32_t max = maxBlocks / 32;
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;
for (uint8_t j = 0; j < 32; j++) {
if (TEST_BLOCK(i * PMM_BPB + j)) freeCount++;
else freeCount = 0;
if (freeCount >= blockCount) return i * PMM_BPB + j;
}
}
// We didn't find enough free memory :(
return -1;
}
void pmm_initRegion(size_t base, size_t len) {
uint32_t align = base / PMM_BS;
uint32_t blockCount = len / PMM_BS;
while (blockCount-- > 0) {
USET_BLOCK(align);
align++;
usedBlocks--;
}
SET_BLOCK(0);
}
void pmm_deinitRegion(size_t base, size_t len) {
uint32_t align = base / PMM_BS;
uint32_t blockCount = len / PMM_BS;
while (blockCount-- > 0) {
align++;
SET_BLOCK(align); // We can't increment here, because macros -_(._.)_-
usedBlocks++;
}
SET_BLOCK(0);
}
void* pmm_alloc(size_t blockCount) {
if ((maxBlocks - usedBlocks) < blockCount) return NULL;
int startBlock = findFreeBlocks(blockCount);
if (startBlock == -1) return NULL;
for (size_t i = 0; i < blockCount; i++) {
SET_BLOCK(startBlock + i);
usedBlocks++;
}
return (void*)(startBlock * PMM_BS);
}
void pmm_free(void* ptr, size_t blockCount) {
size_t startBlock = (size_t)ptr / PMM_BS;
for (size_t i = 0; i < blockCount; i++) {
USET_BLOCK(startBlock + i);
usedBlocks--;
}
SET_BLOCK(0); // Make sure block 0 is always set
}