[libc] Implement sbrk to allow malloc() to work

Userspace can now allocte via malloc. This is slightly janky because it
relies on a single static handle in the library code.
This commit is contained in:
2020-09-27 17:31:23 -07:00
parent 2d44e8112b
commit ff78c951f0
4 changed files with 47 additions and 18 deletions

View File

@@ -61,27 +61,17 @@ main(int argc, const char **argv)
_syscall_system_log("main thread starting");
uintptr_t base = 0xcc0000000;
j6_handle_t vma = j6_handle_invalid;
j6_status_t result = _syscall_vma_create_map(&vma, 0x100000, base, 1);
if (result != j6_status_ok)
return result;
size_t size = 0x800000;
result = _syscall_vma_resize(vma, &size);
if (result != j6_status_ok)
return result;
if (size == 0x800000)
_syscall_system_log("main thread resized memory area");
void *base = malloc(0x1000);
if (!base)
return 1;
uint64_t *vma_ptr = reinterpret_cast<uint64_t*>(base);
for (int i = 0; i < 300; ++i)
vma_ptr[i * 512] = uint64_t(i);
vma_ptr[i] = uint64_t(i);
_syscall_system_log("main thread wrote to memory area");
result = _syscall_endpoint_create(&endp);
j6_status_t result = _syscall_endpoint_create(&endp);
if (result != j6_status_ok)
return result;
@@ -94,7 +84,7 @@ main(int argc, const char **argv)
_syscall_system_log("main thread created sub thread");
char message[] = "MAIN THREAD SUCCESSFULLY CALLED SENDRECV IF THIS IS LOWERCASE";
size = sizeof(message);
size_t size = sizeof(message);
result = _syscall_endpoint_sendrecv(endp, &size, (void*)message);
if (result != j6_status_ok)
return result;

View File

@@ -71,6 +71,7 @@ vm_space::add(uintptr_t base, vm_area *area)
//TODO: check for collisions
m_areas.sorted_insert({base, area});
area->mapper().add(this);
area->handle_retain();
return true;
}
@@ -81,6 +82,7 @@ vm_space::remove(vm_area *area)
if (a.area == area) {
m_areas.remove(a);
area->mapper().remove(this);
area->handle_release();
return true;
}
}

View File

@@ -1,5 +1,7 @@
#pragma once
#include <j6/types.h>
#ifdef __cplusplus
extern "C" {
#endif

View File

@@ -1,4 +1,39 @@
#include <stdint.h>
void *sbrk(intptr_t) __attribute__ ((weak));
#include <j6/errors.h>
#include <j6libc/syscalls.h>
//void *sbrk(intptr_t) __attribute__ ((weak));
void *sbrk(intptr_t i) { return 0; }
static j6_handle_t __core_handle = 0;
static intptr_t __core_size = 0;
static const uintptr_t __core_base = 0x1c0000000;
static const void *error_val = (void*)-1;
void *sbrk(intptr_t i)
{
if (i == 0)
return (void*)__core_base;
if (__core_size == 0) {
if (i < 0)
return (void*)-1;
j6_status_t result = _syscall_vma_create_map(&__core_handle, i, __core_base, 1);
if (result != j6_status_ok)
return (void*)-1;
__core_size = i;
return (void*)__core_base;
}
size_t new_size = __core_size + i;
j6_status_t result = _syscall_vma_resize(__core_handle, &new_size);
if (result != j6_status_ok)
return (void*)-1;
uintptr_t prev = __core_base + __core_size;
__core_size += i;
return (void*)prev;
}