summaryrefslogtreecommitdiff
path: root/src/kernel/mem/alloc.c
diff options
context:
space:
mode:
authordzwdz2022-08-15 23:03:18 +0200
committerdzwdz2022-08-15 23:03:18 +0200
commit63fd7ce362c7f1d59365045f19cf1ca87ffe2db9 (patch)
treed991957f52e74afebe88a1ec88ec0d371728160b /src/kernel/mem/alloc.c
parent3e80780859c4cbe5223ca4329bfd6176f5c2c879 (diff)
kernel: combine kmain_early and kmain
Diffstat (limited to 'src/kernel/mem/alloc.c')
-rw-r--r--src/kernel/mem/alloc.c31
1 files changed, 16 insertions, 15 deletions
diff --git a/src/kernel/mem/alloc.c b/src/kernel/mem/alloc.c
index 7270021..3ed49d6 100644
--- a/src/kernel/mem/alloc.c
+++ b/src/kernel/mem/alloc.c
@@ -36,29 +36,30 @@ static void bitmap_set(size_t i, bool v) {
else pbitmap[b] &= ~m;
}
-static void bitmap_mark(void *start, size_t len) {
+void mem_init(void *memtop) {
+ kprintf("memory %8x -> %8x\n", &_bss_end, memtop);
+ size_t pageamt = ((uintptr_t)memtop - (uintptr_t)pbitmap_start) / PAGE_SIZE;
+ pbitmap_len = pageamt / 8;
+ memset(pbitmap, 0, pbitmap_len);
+ mem_reserve(pbitmap, pbitmap_len);
+}
+
+void mem_reserve(void *addr, size_t len) {
+ kprintf("reserved %8x -> %8x\n", addr, addr + len);
+
/* align to the previous page */
- size_t off = (uintptr_t)start & PAGE_MASK;
- start -= off;
+ size_t off = (uintptr_t)addr & PAGE_MASK;
+ addr -= off;
len += off;
- size_t first = ((uintptr_t)start - (uintptr_t)pbitmap_start) / PAGE_SIZE;
+ size_t first = ((uintptr_t)addr - (uintptr_t)pbitmap_start) / PAGE_SIZE;
for (size_t i = 0; i * PAGE_SIZE < len; i++) {
if (first + i >= pbitmap_len) break;
+ if (bitmap_get(first + i))
+ panic_invalid_state();
bitmap_set(first + i, true);
}
}
-void mem_init(struct kmain_info *info) {
- size_t pageamt = ((uintptr_t)info->memtop - (uintptr_t)pbitmap_start) / PAGE_SIZE;
- pbitmap_len = pageamt / 8;
- memset(pbitmap, 0, pbitmap_len);
- bitmap_mark(pbitmap, pbitmap_len);
-
- // TODO make boot.c handle this instead
- bitmap_mark(info->init.at, info->init.size);
- bitmap_mark(info->fb.at, info->fb.size);
-}
-
void mem_debugprint(void) {
size_t total = 0;
kprintf("current kmallocs:\n");