page allocation
This commit is contained in:
parent
e64cf1ef72
commit
3efbcf24f4
5 changed files with 40 additions and 3 deletions
|
|
@ -30,4 +30,7 @@ SECTIONS
|
||||||
PROVIDE(_stack_bottom = .);
|
PROVIDE(_stack_bottom = .);
|
||||||
. += 4096;
|
. += 4096;
|
||||||
PROVIDE(stack_top = .);
|
PROVIDE(stack_top = .);
|
||||||
|
|
||||||
|
. = ALIGN(4K);
|
||||||
|
_heap_start =.;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,5 +10,14 @@ void kmain() {
|
||||||
kputs("-------------------");
|
kputs("-------------------");
|
||||||
knewline();
|
knewline();
|
||||||
page_init();
|
page_init();
|
||||||
|
|
||||||
|
kprint("Stress testing memory...\n");
|
||||||
|
while(1) {
|
||||||
|
void *p = page_alloc();
|
||||||
|
if (p == NULL) {
|
||||||
|
kpanic("Expected OOM reached!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
poweroff();
|
poweroff();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include "drivers/uart.h"
|
#include "drivers/uart.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
|
#include "panic.h"
|
||||||
|
|
||||||
#define PAGE_SIZE 4096
|
#define PAGE_SIZE 4096
|
||||||
|
|
||||||
|
|
@ -9,13 +10,19 @@ struct Page {
|
||||||
struct Page *next;
|
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() {
|
void page_init() {
|
||||||
kprint("Initialising page allocator.\n");
|
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
|
uintptr_t end = 0x88000000; // Default QEMU RAM limit
|
||||||
|
|
||||||
for (uintptr_t addr = start; addr + PAGE_SIZE <= end; addr += PAGE_SIZE) {
|
for (uintptr_t addr = start; addr + PAGE_SIZE <= end; addr += PAGE_SIZE) {
|
||||||
|
|
@ -31,3 +38,19 @@ void page_free(void *addr) {
|
||||||
p->next = free_list;
|
p->next = free_list;
|
||||||
free_list = p;
|
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_init();
|
||||||
void page_free(void *addr);
|
void page_free(void *addr);
|
||||||
|
void *page_alloc();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include <syscon/syscon.h>
|
#include <syscon/syscon.h>
|
||||||
|
|
||||||
void kpanic(const char *reason){
|
void kpanic(const char *reason){
|
||||||
|
kprint("\n!!! PANIC!!!\n");
|
||||||
kprint(reason);
|
kprint(reason);
|
||||||
poweroff();
|
poweroff();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue