add KePanic instead of splattering _disable __halt everywhere, implement a DMA + interrupts path to the IDE driver, move the dynamic arrays from kmain.c to ke_utils.h, remove weird unicode chars

This commit is contained in:
maksw committed 2026-05-03 22:45:31 +02:00
1 parent 83d48e09bb
commit 4b57738fe3
15 files changed
+673 -219

No files matched your search

+20 -76
View File
@@ -13,65 +13,12 @@
#include <ke_irq.h>
#include <ke_syscall.h>
#include <ke_debug.h>
#include <ke_util.h>
#include <ldr_image.h>
#include <hal_api.h>
#include <io_file.h>
#include <ini_parser.h>
// dynamic arrays bitch
typedef struct {
size_t count;
size_t capacity;
} Header;
#define ARR_INIT_CAPACITY 2
#define _arr_header(arr) ((Header*)(arr) - 1)
#define _arr_maybe_grow(arr) \
do { \
if (!(arr)) { \
Header* h = MmAllocatePool(sizeof(Header) + sizeof(*(arr)) * ARR_INIT_CAPACITY); \
h->count = 0; \
h->capacity = ARR_INIT_CAPACITY; \
(arr) = (void*)(h + 1); \
} \
Header* h = _arr_header(arr); \
if (h->count >= h->capacity) { \
h->capacity *= 2; \
h = MmReallocatePool(h, sizeof(Header) + sizeof(*(arr)) * h->capacity); \
(arr) = (void*)(h + 1); \
} \
} while (0)
#define arr_push(arr, x) \
do { \
_arr_maybe_grow(arr); \
(arr)[_arr_header(arr)->count++] = (x); \
} while (0)
#define arr_push_front(arr, x) \
do { \
_arr_maybe_grow(arr); \
Header* h = _arr_header(arr); \
memmove(&(arr)[1], &(arr)[0], sizeof(*(arr)) * h->count); \
(arr)[0] = (x); \
h->count++; \
} while (0)
#define arr_remove(arr, index) \
do { \
if ((arr) && (index) < arr_len(arr)) { \
Header* h = _arr_header(arr); \
memmove(&(arr)[index], &(arr)[index + 1], sizeof(*(arr)) * (h->count - (index) - 1)); \
h->count--; \
} \
} while (0)
#define arr_len(arr) ((Header*)(arr) - 1)->count
#define arr_free(arr) MmFreePool((Header*)(arr) - 1)
typedef struct {
BOOT_INFO* bootInfo;
const HAL_API* hal;
@@ -138,17 +85,15 @@ static void InitializeDrivers(BOOT_INFO* bootInfo, const HAL_API* hal) {
s = IoOpenFile("drivers\\drivers.ini", &f);
if (s != STATUS_SUCCESS) {
DbgPrint("InitializeDrivers: Failed to open drivers.ini\n");
_disable(); while(true) __halt();
KePanic("InitializeDrivers: Failed to open drivers.ini (status %d)\n", s);
}
uint64_t size = IoGetFileSize(f) + 1;
char* mem = MmAllocatePool(size);
size_t got;
if (!KSUCCESS(IoReadFile(f, mem, size, &got))) {
DbgPrint("InitializeDrivers: Failed to read drivers.ini\n");
IoCloseFile(f);
_disable(); while(true) __halt();
KePanic("InitializeDrivers: Failed to read drivers.ini\n");
}
mem[got] = '\0';
@@ -162,16 +107,17 @@ static void InitializeDrivers(BOOT_INFO* bootInfo, const HAL_API* hal) {
// Load the driver
size_t len = strlen(runtimeKeys[i]);
char* path = MmAllocatePool(len + 10);
if (!path) {
DbgPrint("InitializeDrivers: Failed to allocate memory for driver path. How?\n");
continue;
}
// Assemble the path
strncpy(path, "drivers\\", sizeof(path) - 1);
path[sizeof(path) - 1] = '\0';
strncat(path, runtimeKeys[i], sizeof(path) - strlen(path) - 1);
// "drivers\" is 8 chars; +1 for the NUL terminator already in len.
size_t pathLen = 8 + len + 1; // len already includes the NUL from strlen+1 above
char *path = MmAllocatePool(pathLen);
if (!path) {
DbgPrint("InitializeDrivers: OOM building driver path\n");
continue;
}
memcpy(path, "drivers\\", 8);
memcpy(path + 8, runtimeKeys[i], len); // len includes the NUL
s = IoOpenFile(path, &f);
if (s != STATUS_SUCCESS) {
@@ -229,17 +175,15 @@ static void LoadAndRegisterDll(const char* filename) {
// Read the DLL from disk
s = IoOpenFile(filename, &f);
if (s != STATUS_SUCCESS) {
DbgPrint("LoadAndRegisterDll: Failed to open DLL: %s\n", filename);
return;
KePanic("LoadAndRegisterDll: Failed to open DLL: %s\n", filename);
}
uint64_t size = IoGetFileSize(f);
void* mem = MmAllocatePool(size); // Temporary pool for the raw file
size_t got;
if (!KSUCCESS(IoReadFile(f, mem, size, &got))) {
DbgPrint("LoadAndRegisterDll: Failed to read %s\n", filename);
KePanic("LoadAndRegisterDll: Failed to read %s\n", filename);
IoCloseFile(f);
return;
}
IoCloseFile(f);
@@ -248,8 +192,7 @@ static void LoadAndRegisterDll(const char* filename) {
uintptr_t mappedBase = 0;
s = LdrLoadImage(mem, size, &mappedBase, true);
if (!KSUCCESS(s)) {
DbgPrint("LoadAndRegisterDll: Failed to map %s\n", filename);
return;
KePanic("LoadAndRegisterDll: Failed to map %s\n", filename);
}
MmFreePool(mem); // Free the raw file buffer, we only need the mapped image now
@@ -428,7 +371,10 @@ extern void KernelMain(BOOT_INFO* bootInfo) {
.DebugEnabled = bootInfo->DebugEnabled
};
KSTATUS s = HalInitialize(&halParams);
if (!KSUCCESS(s)) while (true) __halt(); // there's nothing we can do.
if (!KSUCCESS(s)) {
_disable();
while (true) __halt(); // there's nothing we can do.
}
const HAL_API* hal = HalGetApi();
gKmainHal = hal;
@@ -466,9 +412,7 @@ extern void KernelMain(BOOT_INFO* bootInfo) {
PriorityNormal, &initThread);
if (!KSUCCESS(st)) {
DbgPrint("KernelMain: PANIC - Failed to create SystemInitThread! (0x%x)\n", st);
_disable();
for(;;) __halt();
KePanic("KernelMain: Failed to create SystemInitThread (status 0x%x)\n", st);
}
DbgPrint("KernelMain: Handing over to scheduler...\n");