2020-05-15 19:30:22 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stddef.h>
|
2022-01-09 20:01:12 +00:00
|
|
|
#include <stdlib.h>
|
2021-01-16 17:26:12 +00:00
|
|
|
#include <string.h>
|
2022-01-09 20:01:12 +00:00
|
|
|
#include <stdio.h>
|
2021-01-16 17:26:12 +00:00
|
|
|
#include <magenta/vmo.h>
|
2021-08-15 16:25:15 +01:00
|
|
|
#include <magenta/bootstrap.h>
|
2022-01-09 20:01:12 +00:00
|
|
|
#include <magenta/errors.h>
|
|
|
|
|
#include <magenta/misc.h>
|
2021-01-16 17:26:12 +00:00
|
|
|
#include <magenta/vmar.h>
|
2020-05-15 19:30:22 +01:00
|
|
|
|
2022-05-11 22:14:59 +01:00
|
|
|
/* 1 GiB */
|
|
|
|
|
#define HEAP_REGION_SZ 0x40000000
|
|
|
|
|
|
2020-05-15 19:30:22 +01:00
|
|
|
#define ROUND_UP(x, b) x += (b) - ((x) % (b))
|
|
|
|
|
|
|
|
|
|
static uintptr_t heap_start = 0;
|
|
|
|
|
static uintptr_t heap_end = 0;
|
2021-01-16 17:26:12 +00:00
|
|
|
static uintptr_t heap_alloc_point = 0;
|
2020-05-15 19:30:22 +01:00
|
|
|
static size_t heap_sz = 0;
|
2020-10-04 12:09:40 +01:00
|
|
|
static mx_handle_t heap_vmo = MX_NULL_HANDLE;
|
2022-05-11 22:14:59 +01:00
|
|
|
static mx_handle_t heap_vmar = MX_NULL_HANDLE;
|
2020-05-15 19:30:22 +01:00
|
|
|
|
2021-03-11 10:12:33 +00:00
|
|
|
void __crt_heap_init(size_t sz)
|
2020-05-15 19:30:22 +01:00
|
|
|
{
|
2022-05-11 22:14:59 +01:00
|
|
|
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);
|
2022-05-24 21:34:06 +01:00
|
|
|
if (status != MX_OK) {
|
2022-05-31 15:17:00 +01:00
|
|
|
fprintf(stderr, "fatal: cannot allocate heap virtual region (%s)\n", mx_status_to_string(status));
|
2022-05-24 21:34:06 +01:00
|
|
|
abort();
|
|
|
|
|
}
|
2022-05-11 22:14:59 +01:00
|
|
|
|
|
|
|
|
status = mx_vmo_create(
|
2021-03-11 10:12:33 +00:00
|
|
|
sz,
|
2021-01-16 17:26:12 +00:00
|
|
|
MX_VM_CAN_MAP_READ | MX_VM_CAN_MAP_WRITE | MX_VM_CAN_MAP_SPECIFIC,
|
|
|
|
|
&heap_vmo);
|
2022-05-24 21:34:06 +01:00
|
|
|
if (status != MX_OK) {
|
2022-05-31 15:17:00 +01:00
|
|
|
fprintf(stderr, "fatal: cannot allocate heap (%s)\n", mx_status_to_string(status));
|
2022-05-24 21:34:06 +01:00
|
|
|
abort();
|
|
|
|
|
}
|
2021-01-16 17:26:12 +00:00
|
|
|
|
2022-05-24 21:34:06 +01:00
|
|
|
status = mx_vmar_map(heap_vmar,
|
2022-05-11 22:14:59 +01:00
|
|
|
MX_VM_PERM_READ | MX_VM_PERM_WRITE | MX_VM_SPECIFIC,
|
2021-03-11 10:12:33 +00:00
|
|
|
0, heap_vmo, 0, sz,
|
2021-01-16 17:26:12 +00:00
|
|
|
&heap);
|
2022-05-24 21:34:06 +01:00
|
|
|
if (status != MX_OK) {
|
2022-05-31 15:17:00 +01:00
|
|
|
fprintf(stderr, "fatal: cannot map heap (%s)\n", mx_status_to_string(status));
|
2022-05-24 21:34:06 +01:00
|
|
|
abort();
|
|
|
|
|
}
|
2021-01-16 17:26:12 +00:00
|
|
|
|
2021-03-11 10:12:33 +00:00
|
|
|
heap_sz = sz;
|
2021-01-16 17:26:12 +00:00
|
|
|
heap_start = heap;
|
|
|
|
|
heap_end = heap + heap_sz;
|
|
|
|
|
heap_alloc_point = heap_start;
|
2021-02-19 13:54:28 +00:00
|
|
|
|
|
|
|
|
memset((void *)heap, 0x0, heap_sz);
|
2020-05-15 19:30:22 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-16 17:26:12 +00:00
|
|
|
void *__crt_heap_extend(size_t sz)
|
2020-05-15 19:30:22 +01:00
|
|
|
{
|
2021-02-19 13:54:28 +00:00
|
|
|
if (sz % MX_PAGE_SIZE) {
|
|
|
|
|
sz &= (MX_PAGE_SIZE - 1);
|
|
|
|
|
sz += MX_PAGE_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 10:12:33 +00:00
|
|
|
if (!sz) {
|
|
|
|
|
return (void *)heap_end;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-16 17:26:12 +00:00
|
|
|
if (!heap_start) {
|
|
|
|
|
__crt_heap_init(sz);
|
|
|
|
|
return (void *)heap_start;
|
2020-05-15 19:30:22 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-16 17:26:12 +00:00
|
|
|
mx_vmo_set_size(heap_vmo, heap_sz + sz);
|
|
|
|
|
|
|
|
|
|
mx_vaddr_t vmar_base, alloc_base;
|
2022-05-11 22:14:59 +01:00
|
|
|
mx_vmar_bounds(heap_vmar, &vmar_base, NULL);
|
2021-03-11 10:12:33 +00:00
|
|
|
size_t offset = heap_end - vmar_base;
|
2020-05-15 19:30:22 +01:00
|
|
|
|
2022-05-11 22:14:59 +01:00
|
|
|
mx_status_t err = mx_vmar_map(heap_vmar,
|
2021-01-16 17:26:12 +00:00
|
|
|
MX_VM_PERM_READ | MX_VM_PERM_WRITE | MX_VM_SPECIFIC,
|
|
|
|
|
offset, heap_vmo, heap_sz, sz, &alloc_base);
|
2022-01-09 20:01:12 +00:00
|
|
|
|
|
|
|
|
if (err != MX_OK) {
|
2022-05-31 15:17:00 +01:00
|
|
|
fprintf(stderr, "fatal: cannot map extended heap (%s)\n", mx_status_to_string(err));
|
2022-01-09 20:01:12 +00:00
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 15:21:57 +01:00
|
|
|
heap_sz += sz;
|
2021-01-16 17:26:12 +00:00
|
|
|
|
|
|
|
|
void *out = (void *)heap_end;
|
2021-02-19 13:54:28 +00:00
|
|
|
memset(out, 0x0, sz);
|
2020-05-15 19:30:22 +01:00
|
|
|
heap_end += sz;
|
2021-01-16 17:26:12 +00:00
|
|
|
|
|
|
|
|
return out;
|
2020-05-15 19:30:22 +01:00
|
|
|
}
|