From 7f4c833ced046f88160a50e7adb1f55343e7e759 Mon Sep 17 00:00:00 2001 From: Nathan Lee Date: Fri, 28 Aug 2026 21:26:14 -0500 Subject: rework early kernel init --- kernel/common/bump.c | 54 ---------------------------------------------------- kernel/common/dtb.c | 24 ++++++++++++++++++++++- kernel/common/mem.c | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 55 deletions(-) delete mode 100644 kernel/common/bump.c (limited to 'kernel/common') diff --git a/kernel/common/bump.c b/kernel/common/bump.c deleted file mode 100644 index 1e07837..0000000 --- a/kernel/common/bump.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include - -static char eheap[EHEAP_SIZE] = {0}; -static size_t eheap_off = 0; - -struct mem_info *km_find_memory(struct dt_node *head) { - struct dt_node *child = head->children; - while (child) { - struct mem_info *location = km_find_memory(child); - if (location) { - return location; - } else { - child = child->next; - } - } - - if (strlen(head->name) != 0) { - struct dt_prop *prop_head = head->props; - while (prop_head) { - if (strcmp(prop_head->name, "device_type") == 0 - && strcmp(prop_head->value, "memory") == 0) { - size_t len = 0; - uint32_t *reg = (uint32_t *)dt_get_prop(head, "reg", &len); - if (reg && len >= 8) { - uint32_t base = btohi(reg[0]); - uint32_t size = btohi(reg[1]); - struct mem_info *ret = kemalloc(sizeof(struct mem_info)); - ret->start = (void*)base; - ret->size = (size_t)size; - - return ret; - } - } - prop_head = prop_head->next; - } - } - return NULL; -} - - -void* kemalloc(size_t size) { - size = (size + 7) & ~7; - - if (eheap_off + size > EHEAP_SIZE) { - return NULL; - } - - void *ptr = &eheap[eheap_off]; - eheap_off += size; - return ptr; -} - diff --git a/kernel/common/dtb.c b/kernel/common/dtb.c index b345db0..482359b 100644 --- a/kernel/common/dtb.c +++ b/kernel/common/dtb.c @@ -1,5 +1,5 @@ #include -#include +#include struct dt_node *dt_parse(void *fdt) { struct fdt_header *hdr = fdt; @@ -52,6 +52,28 @@ struct dt_node *dt_parse(void *fdt) { } } +struct dt_node *dt_find_device(struct dt_node *head, const char *name) { + struct dt_node *child = head->children; + while (child) { + struct dt_node *result = dt_find_device(child, name); + if (result) { + return result; + } else { + child = child->next; + } + } + + struct dt_prop *props = head->props; + while (props) { + if (strcmp(props->name, "device_type") == 0 && + strcmp(props->value, name) == 0) { + return head; + } + } + + return NULL; +} + void *dt_get_prop(struct dt_node *node, char *name, size_t *len) { for (struct dt_prop *p = node->props; p; p = p->next) { if (strcmp(p->name, name) == 0) { diff --git a/kernel/common/mem.c b/kernel/common/mem.c index fee0761..f03d0a8 100644 --- a/kernel/common/mem.c +++ b/kernel/common/mem.c @@ -1,8 +1,12 @@ #include #include +#define EHEAP_SIZE (512 * 1024) static struct page *free_list; +static char eheap[EHEAP_SIZE] = {0}; +static size_t eheap_off = 0; + static void merge_freelist() { struct page *list = free_list; @@ -16,6 +20,20 @@ static void merge_freelist() { } } + +void* kemalloc(size_t size) { + size = (size + 7) & ~7; + + if (eheap_off + size > EHEAP_SIZE) { + return NULL; + } + + void *ptr = &eheap[eheap_off]; + eheap_off += size; + return ptr; +} + + void* kmalloc(size_t size) { struct page *page = free_list; struct page *last = NULL; -- cgit v1.2.3