page allocation
This commit is contained in:
parent
e64cf1ef72
commit
3efbcf24f4
5 changed files with 40 additions and 3 deletions
|
|
@ -10,5 +10,14 @@ void kmain() {
|
|||
kputs("-------------------");
|
||||
knewline();
|
||||
page_init();
|
||||
|
||||
kprint("Stress testing memory...\n");
|
||||
while(1) {
|
||||
void *p = page_alloc();
|
||||
if (p == NULL) {
|
||||
kpanic("Expected OOM reached!");
|
||||
}
|
||||
}
|
||||
|
||||
poweroff();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include <stddef.h>
|
||||
#include "drivers/uart.h"
|
||||
#include "memory.h"
|
||||
#include "panic.h"
|
||||
|
||||
#define PAGE_SIZE 4096
|
||||
|
||||
|
|
@ -9,13 +10,19 @@ struct Page {
|
|||
struct Page *next;
|
||||
};
|
||||
|
||||
static struct Page *free_list;
|
||||
struct HeapHeader {
|
||||
size_t size;
|
||||
int is_free;
|
||||
struct HeapHeader *next;
|
||||
};
|
||||
|
||||
extern uint8_t _bss_end[]; // named in the linker script
|
||||
static struct Page *free_list = NULL;
|
||||
|
||||
extern uint8_t _heap_start[]; // named in the linker script
|
||||
|
||||
void page_init() {
|
||||
kprint("Initialising page allocator.\n");
|
||||
uintptr_t start = ((uintptr_t)_bss_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
|
||||
uintptr_t start = ((uintptr_t)_heap_start + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
|
||||
uintptr_t end = 0x88000000; // Default QEMU RAM limit
|
||||
|
||||
for (uintptr_t addr = start; addr + PAGE_SIZE <= end; addr += PAGE_SIZE) {
|
||||
|
|
@ -30,4 +37,20 @@ void page_free(void *addr) {
|
|||
|
||||
p->next = free_list;
|
||||
free_list = p;
|
||||
}
|
||||
|
||||
void *page_alloc() { {
|
||||
if (free_list == NULL) {
|
||||
kpanic("No free pages!");
|
||||
}
|
||||
struct Page *p = free_list;
|
||||
free_list = free_list->next;
|
||||
|
||||
//zero out the page
|
||||
for (int i = 0; i < (PAGE_SIZE/8); i++) {
|
||||
((uint8_t *)p)[i] = 0;
|
||||
}
|
||||
|
||||
return (void *)p;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,5 +3,6 @@
|
|||
|
||||
void page_init();
|
||||
void page_free(void *addr);
|
||||
void *page_alloc();
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include <syscon/syscon.h>
|
||||
|
||||
void kpanic(const char *reason){
|
||||
kprint("\n!!! PANIC!!!\n");
|
||||
kprint(reason);
|
||||
poweroff();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue