Implement kernel brk
This commit is contained in:
parent
4a2dc7e083
commit
8eaa8e6c8f
@ -28,7 +28,7 @@ void kmain(uint64_t multiboot_magic, void *multiboot_data)
|
||||
debug_info("Boot complete\n");
|
||||
|
||||
debug_info("Mallocing\n");
|
||||
uint64_t a = malloc(100);
|
||||
void *a = malloc(100);
|
||||
debug_info("Malloced %x\n", a);
|
||||
|
||||
PANIC("End of kernel function!");
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define KERNEL_OFFSET 0xFFFFFF8000000000
|
||||
#define KERNEL_BRK0 0xFFFFFFC000000000
|
||||
|
||||
#define PAGE_PRESENT 0x001
|
||||
#define PAGE_WRITE 0x002
|
||||
@ -46,6 +47,9 @@
|
||||
int memcmp(const void *s1, const void *s2, size_t n);
|
||||
size_t strlen(const char *s);
|
||||
|
||||
// memory/kbrk.c
|
||||
long kbrk(long brk, long, long, long, long, long);
|
||||
|
||||
// Link.ld
|
||||
extern int kernel_start, kernel_end;
|
||||
|
||||
|
8
src/kernel/include/musl-glue.h
Normal file
8
src/kernel/include/musl-glue.h
Normal file
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#define SYSCALL_BRK 12
|
||||
|
||||
typedef long (*syscall_handler)(long, long, long, long, long, long);
|
||||
|
||||
// lib/musl-glue.c
|
||||
syscall_handler set_syscall_handler(long num, syscall_handler handler);
|
@ -1,8 +1,21 @@
|
||||
#include <musl-glue.h>
|
||||
#include <debug.h>
|
||||
|
||||
syscall_handler syscall_handlers[440];
|
||||
|
||||
syscall_handler set_syscall_handler(long num, syscall_handler handler)
|
||||
{
|
||||
syscall_handler old = syscall_handlers[num];
|
||||
syscall_handlers[num] = handler;
|
||||
return old;
|
||||
}
|
||||
|
||||
long syscall0(long num)
|
||||
{
|
||||
long retval = 0;
|
||||
if(syscall_handlers[num])
|
||||
retval = syscall_handlers[num](0, 0, 0, 0, 0, 0);
|
||||
else
|
||||
PANIC("Unknown syscall: %d()\n", num);
|
||||
return retval;
|
||||
}
|
||||
@ -10,6 +23,9 @@ long syscall0(long num)
|
||||
extern long syscall1(long num, long a1)
|
||||
{
|
||||
long retval = 0;
|
||||
if(syscall_handlers[num])
|
||||
retval = syscall_handlers[num](a1, 0, 0, 0, 0, 0);
|
||||
else
|
||||
PANIC("Unknown syscall: %d(%x)\n", num, a1);
|
||||
return retval;
|
||||
}
|
||||
@ -17,6 +33,9 @@ extern long syscall1(long num, long a1)
|
||||
extern long syscall2(long num, long a1, long a2)
|
||||
{
|
||||
long retval = 0;
|
||||
if(syscall_handlers[num])
|
||||
retval = syscall_handlers[num](a1, a2, 0, 0, 0, 0);
|
||||
else
|
||||
PANIC("Unknown syscall: %d(%x, %x)\n", num, a1, a2);
|
||||
return retval;
|
||||
}
|
||||
@ -24,6 +43,9 @@ extern long syscall2(long num, long a1, long a2)
|
||||
extern long syscall3(long num, long a1, long a2, long a3)
|
||||
{
|
||||
long retval = 0;
|
||||
if(syscall_handlers[num])
|
||||
retval = syscall_handlers[num](a1, a2, a3, 0, 0, 0);
|
||||
else
|
||||
PANIC("Unknown syscall: %d(%x, %x, %x)\n", num, a1, a2, a3);
|
||||
return retval;
|
||||
}
|
||||
@ -31,6 +53,9 @@ extern long syscall3(long num, long a1, long a2, long a3)
|
||||
extern long syscall4(long num, long a1, long a2, long a3, long a4)
|
||||
{
|
||||
long retval = 0;
|
||||
if(syscall_handlers[num])
|
||||
retval = syscall_handlers[num](a1, a2, a3, a4, 0, 0);
|
||||
else
|
||||
PANIC("Unknown syscall: %d(%x, %x, %x, %x)\n", num, a1, a2, a3, a4);
|
||||
return retval;
|
||||
}
|
||||
@ -38,6 +63,9 @@ extern long syscall4(long num, long a1, long a2, long a3, long a4)
|
||||
extern long syscall5(long num, long a1, long a2, long a3, long a4, long a5)
|
||||
{
|
||||
long retval = 0;
|
||||
if(syscall_handlers[num])
|
||||
retval = syscall_handlers[num](a1, a2, a3, a4, a5, 0);
|
||||
else
|
||||
PANIC("Unknown syscall: %d(%x, %x, %x, %x, %x)\n", num, a1, a2, a3, a4, a5);
|
||||
return retval;
|
||||
}
|
||||
@ -45,6 +73,9 @@ extern long syscall5(long num, long a1, long a2, long a3, long a4, long a5)
|
||||
extern long syscall6(long num, long a1, long a2, long a3, long a4, long a5, long a6)
|
||||
{
|
||||
long retval = 0;
|
||||
if(syscall_handlers[num])
|
||||
retval = syscall_handlers[num](a1, a2, a3, a4, a5, a6);
|
||||
else
|
||||
PANIC("Unknown syscall: %d(%x, %x, %x, %x, %x, %x)\n", num, a1, a2, a3, a4, a5, a6);
|
||||
return retval;
|
||||
}
|
18
src/kernel/memory/kbrk.c
Normal file
18
src/kernel/memory/kbrk.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <memory.h>
|
||||
#include <debug.h>
|
||||
|
||||
static long _brk = KERNEL_BRK0;
|
||||
|
||||
long kbrk(long brk, long, long, long, long, long)
|
||||
{
|
||||
if(brk)
|
||||
{
|
||||
while(_brk < brk) {
|
||||
vmm_set_page(kernel_P4, _brk, pmm_alloc(), PAGE_WRITE | PAGE_PRESENT);
|
||||
_brk += PAGE_SIZE;
|
||||
}
|
||||
return _brk;
|
||||
} else {
|
||||
return _brk;
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
#include <memory.h>
|
||||
#include <debug.h>
|
||||
#include <multiboot.h>
|
||||
#include <musl-glue.h>
|
||||
|
||||
|
||||
uint64_t kernel_P4;
|
||||
@ -37,4 +38,6 @@ void memory_init()
|
||||
pmm_free(p);
|
||||
}
|
||||
}
|
||||
|
||||
set_syscall_handler(SYSCALL_BRK, &kbrk);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user