Remove original kalloc free-list memory allocator entirely for now

This commit is contained in:
Imbus 2025-09-06 02:40:05 +02:00
parent 90c63ab41e
commit 8316c9f6ae
4 changed files with 5 additions and 113 deletions

View file

@ -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 \

View file

@ -1,77 +0,0 @@
#include <kalloc.h>
#include <memory.h>
#include <panic.h>
#include <riscv.h>
#include <spinlock.h>
#include <stdint.h>
#include <string.h>
// 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;
}

View file

@ -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

View file

@ -1,6 +1,7 @@
#include <assert.h>
#include <banner.h>
#include <buddy.h>
#include <config.h>
#include <kalloc.h>
#include <memory.h>
#include <panic.h>
#include <proc.h>
@ -8,6 +9,7 @@
#include <spinlock.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <uart.h>
/**
@ -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();