memory allocation
This commit is contained in:
1
common.h
1
common.h
@@ -19,6 +19,7 @@ typedef uint32_t vaddr_t;
|
||||
#define va_start __builtin_va_start
|
||||
#define va_end __builtin_va_end
|
||||
#define va_arg __builtin_va_arg
|
||||
#define PAGE_SIZE 4096
|
||||
|
||||
void *memset(void *buf, char c, size_t n);
|
||||
void *memcpy(void *dst, const void *src, size_t n);
|
||||
|
24
kernel.c
24
kernel.c
@@ -2,6 +2,7 @@
|
||||
#include "common.h"
|
||||
|
||||
extern char __bss[], __bss_end[], __stack_top[];
|
||||
extern char __free_ram[], __free_ram_end[];
|
||||
|
||||
struct sbiret sbi_call(long arg0, long arg1, long arg2, long arg3, long arg4,
|
||||
long arg5, long fid, long eid) {
|
||||
@@ -22,6 +23,18 @@ struct sbiret sbi_call(long arg0, long arg1, long arg2, long arg3, long arg4,
|
||||
return (struct sbiret){.error = a0, .value = a1};
|
||||
}
|
||||
|
||||
paddr_t alloc_pages(uint32_t n) {
|
||||
static paddr_t next_paddr = (paddr_t) __free_ram;
|
||||
paddr_t paddr = next_paddr;
|
||||
next_paddr += n * PAGE_SIZE;
|
||||
|
||||
if (next_paddr > (paddr_t) __free_ram_end)
|
||||
PANIC("Out ow memowy~ :((");
|
||||
|
||||
memset((void *) paddr, 0, n * PAGE_SIZE);
|
||||
return paddr;
|
||||
}
|
||||
|
||||
void putchar(char ch) {
|
||||
sbi_call(ch, 0, 0, 0, 0, 0, 0, 1 /* Console Putchar */);
|
||||
}
|
||||
@@ -121,9 +134,16 @@ void kernel_main(void) {
|
||||
// }
|
||||
|
||||
memset(__bss, 0, (size_t) __bss_end - (size_t) __bss);
|
||||
|
||||
paddr_t paddr0 = alloc_pages(2);
|
||||
paddr_t paddr1 = alloc_pages(1);
|
||||
printf("alloc_pages test paddr0=%x\n", paddr0);
|
||||
printf("alloc_pages test paddr1=%x\n", paddr1);
|
||||
|
||||
WRITE_CSR(stvec, (uint32_t) kernel_entry);
|
||||
__asm__ __volatile__("unimp");
|
||||
PANIC("You booted successfully :3");
|
||||
|
||||
// WRITE_CSR(stvec, (uint32_t) kernel_entry);
|
||||
// __asm__ __volatile__("unimp"); // calls a unimp which triggers kernel panic
|
||||
|
||||
//
|
||||
// PANIC("BOOOO!!! GET INFECTED WITH BOOTKID YOU SKID! 0x1337h@x0r");
|
||||
|
Reference in New Issue
Block a user