1 GiB of virtual memory is now reserved for the heap on startup

This commit is contained in:
2022-05-11 22:14:59 +01:00
parent f3e2312d35
commit b796aa0f62

View File

@@ -9,6 +9,9 @@
#include <magenta/misc.h> #include <magenta/misc.h>
#include <magenta/vmar.h> #include <magenta/vmar.h>
/* 1 GiB */
#define HEAP_REGION_SZ 0x40000000
#define ROUND_UP(x, b) x += (b) - ((x) % (b)) #define ROUND_UP(x, b) x += (b) - ((x) % (b))
static uintptr_t heap_start = 0; static uintptr_t heap_start = 0;
@@ -16,17 +19,24 @@ static uintptr_t heap_end = 0;
static uintptr_t heap_alloc_point = 0; static uintptr_t heap_alloc_point = 0;
static size_t heap_sz = 0; static size_t heap_sz = 0;
static mx_handle_t heap_vmo = MX_NULL_HANDLE; static mx_handle_t heap_vmo = MX_NULL_HANDLE;
static mx_handle_t heap_vmar = MX_NULL_HANDLE;
void __crt_heap_init(size_t sz) void __crt_heap_init(size_t sz)
{ {
mx_status_t status = mx_vmo_create( mx_handle_t root_vmar = mx_bootstrap_handle_get(MX_B_VMAR_ROOT);
mx_vaddr_t heap = 0;
mx_status_t status = mx_vmar_allocate(root_vmar,
MX_VM_CAN_MAP_READ | MX_VM_CAN_MAP_WRITE | MX_VM_CAN_MAP_SPECIFIC,
0, HEAP_REGION_SZ, &heap_vmar, &heap);
status = mx_vmo_create(
sz, sz,
MX_VM_CAN_MAP_READ | MX_VM_CAN_MAP_WRITE | MX_VM_CAN_MAP_SPECIFIC, MX_VM_CAN_MAP_READ | MX_VM_CAN_MAP_WRITE | MX_VM_CAN_MAP_SPECIFIC,
&heap_vmo); &heap_vmo);
mx_vaddr_t heap = 0; mx_vmar_map(heap_vmar,
mx_vmar_map(mx_bootstrap_handle_get(MX_B_VMAR_ROOT), MX_VM_PERM_READ | MX_VM_PERM_WRITE | MX_VM_SPECIFIC,
MX_VM_PERM_READ | MX_VM_PERM_WRITE,
0, heap_vmo, 0, sz, 0, heap_vmo, 0, sz,
&heap); &heap);
@@ -57,10 +67,10 @@ void *__crt_heap_extend(size_t sz)
mx_vmo_set_size(heap_vmo, heap_sz + sz); mx_vmo_set_size(heap_vmo, heap_sz + sz);
mx_vaddr_t vmar_base, alloc_base; mx_vaddr_t vmar_base, alloc_base;
mx_vmar_bounds(mx_bootstrap_handle_get(MX_B_VMAR_ROOT), &vmar_base, NULL); mx_vmar_bounds(heap_vmar, &vmar_base, NULL);
size_t offset = heap_end - vmar_base; size_t offset = heap_end - vmar_base;
mx_status_t err = mx_vmar_map(mx_bootstrap_handle_get(MX_B_VMAR_ROOT), mx_status_t err = mx_vmar_map(heap_vmar,
MX_VM_PERM_READ | MX_VM_PERM_WRITE | MX_VM_SPECIFIC, MX_VM_PERM_READ | MX_VM_PERM_WRITE | MX_VM_SPECIFIC,
offset, heap_vmo, heap_sz, sz, &alloc_base); offset, heap_vmo, heap_sz, sz, &alloc_base);