From 8316c9f6aeeb977eaee7a442f66d071a3e1cd617 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sat, 6 Sep 2025 02:40:05 +0200 Subject: [PATCH] Remove original kalloc free-list memory allocator entirely for now --- Makefile | 1 - kern/kalloc.c | 77 --------------------------------------------------- kern/kalloc.h | 33 ---------------------- kern/start.c | 7 +++-- 4 files changed, 5 insertions(+), 113 deletions(-) delete mode 100644 kern/kalloc.c delete mode 100644 kern/kalloc.h diff --git a/Makefile b/Makefile index cc4c6a3..3fbba85 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,6 @@ quickstart: KERNEL_OBJ := \ kern/entry.o \ kern/start.o \ - kern/kalloc.o \ kern/libkern/string.o \ kern/libkern/proc.o \ kern/libkern/uart.o \ diff --git a/kern/kalloc.c b/kern/kalloc.c deleted file mode 100644 index dead607..0000000 --- a/kern/kalloc.c +++ /dev/null @@ -1,77 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -// Physical memory allocator, for user processes, -// kernel stacks, page-table pages, -// and pipe buffers. Allocates whole 4096-byte pages. - -/** Free list of physical pages. */ -void freerange(void *physaddr_start, void *physaddr_end); - -/** First address after kernel. Provided kernel.ld */ -extern char kernel_end[]; - -/** A run is a node in the free list. */ -struct Run { - struct Run *next; -}; - -/** Kernel memory allocator. */ -struct { - spinlock_t lock; - struct Run *freelist; -} kmem; - -void kalloc_init() { - spinlock_init(&kmem.lock); - freerange(kernel_end, (void *)PHYSTOP); -} - -void freerange(void *physaddr_start, void *physaddr_end) { - char *p; - p = (char *)PGROUNDUP((u64)physaddr_start); - for (; p + PGSIZE <= (char *)physaddr_end; p += PGSIZE) kfree(p); -} - -void kfree(void *pa) { - struct Run *r; - - // Assert that page is a ligned to a page boundary and that its correctly - // sized - if (((u64)pa % PGSIZE) != 0 || (char *)pa < kernel_end || (u64)pa >= PHYSTOP) - PANIC("kfree"); - - // TODO: Kconfig this - // Fill with junk to catch dangling refs. - memset(pa, 1, PGSIZE); - - r = (struct Run *)pa; - - spin_lock(&kmem.lock); - r->next = kmem.freelist; - kmem.freelist = r; - spin_unlock(&kmem.lock); -} - -void *kalloc(void) { - struct Run *r; - - spin_lock(&kmem.lock); - - r = kmem.freelist; - - if (r) - kmem.freelist = r->next; - - spin_unlock(&kmem.lock); - - if (r) - memset((char *)r, 5, PGSIZE); // fill with junk - - return (void *)r; -} diff --git a/kern/kalloc.h b/kern/kalloc.h deleted file mode 100644 index 592d814..0000000 --- a/kern/kalloc.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef KALLOC_KERNEL_H -#define KALLOC_KERNEL_H - -/** - * Kernel memory allocator - * - * Allocate one 4096-byte page of physical memory. - * Returns a pointer that the kernel can use. - * Returns 0 if the memory cannot be allocated. - * See: kalloc.c - */ -void *kalloc(void); - -/** - * Kernel memory allocator - * - * Free the page of physical memory pointed at by pa, - * which normally should have been returned by a - * call to kalloc(). (The exception is when - * initializing the allocator; see kinit above.) - * See: kalloc.c - */ -void kfree(void *); - -/** - * Initialize kernel memory allocator - * - * Called by main() on the way to the kernel's main loop. - * See: kalloc.c - */ -void kalloc_init(void); - -#endif // KALLOC_KERNEL_H diff --git a/kern/start.c b/kern/start.c index 0e4b264..c892765 100644 --- a/kern/start.c +++ b/kern/start.c @@ -1,6 +1,7 @@ +#include #include +#include #include -#include #include #include #include @@ -8,6 +9,7 @@ #include #include #include +#include #include /** @@ -39,7 +41,8 @@ void start() { if (id == 0) { /* Here we will do a bunch of initialization steps */ - kalloc_init(); + memory_sweep(heap_start, heap_end); + buddy_init(heap_start, heap_end); spinlock_init(&sl); for (int i = 0; i < banner_len; i++) uart_putc(banner[i]); __sync_synchronize();