1/*
2 * Copyright 2020, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#include <platsupport/io.h>
14#include <vka/vka.h>
15#include <simple/simple.h>
16#include <simple/simple_helpers.h>
17#include <allocman/vka.h>
18#include <allocman/bootstrap.h>
19#include <sel4utils/vspace.h>
20#include <camkes.h>
21
22
23static allocman_t *allocman;
24static char allocator_mempool[0x800000];
25static simple_t camkes_simple;
26static vka_t vka;
27static vspace_t vspace;
28static sel4utils_alloc_data_t vspace_data;
29
30void camkes_make_simple(simple_t *simple);
31
32
33
34static USED SECTION("_dynamic_init") struct {} dummy_dynamic_init_module;
35
36extern camkes_dynamic_module_init_fn_t __start__dynamic_init[];
37extern camkes_dynamic_module_init_fn_t __stop__dynamic_init[];
38
39static int init_system(ps_io_ops_t *io_ops)
40{
41    int error;
42
43    /* Camkes adds nothing to our address space, so this array is empty */
44    void *existing_frames[] = {
45        NULL
46    };
47    camkes_make_simple(&camkes_simple);
48
49    /* Initialize allocator */
50    allocman = bootstrap_use_current_1level(
51                   simple_get_cnode(&camkes_simple),
52                   simple_get_cnode_size_bits(&camkes_simple),
53                   simple_last_valid_cap(&camkes_simple) + 1,
54                   BIT(simple_get_cnode_size_bits(&camkes_simple)),
55                   sizeof(allocator_mempool), allocator_mempool
56               );
57    assert(allocman);
58    error = allocman_add_simple_untypeds(allocman, &camkes_simple);
59    allocman_make_vka(&vka, allocman);
60
61    /* Initialize the vspace */
62    error = sel4utils_bootstrap_vspace(&vspace, &vspace_data,
63                                       simple_get_init_cap(&camkes_simple, seL4_CapInitThreadPD), &vka, NULL, NULL, existing_frames);
64    assert(!error);
65
66
67    for (camkes_dynamic_module_init_fn_t * init_fun = __start__dynamic_init; init_fun < __stop__dynamic_init; init_fun++) {
68        error = (*init_fun)(io_ops, &vka, &camkes_simple, &vspace);
69        if (error) {
70            ZF_LOGE("Failed to initialize %p", init_fun);
71        }
72
73    }
74
75    return 0;
76}
77
78
79CAMKES_ENV_INIT_MODULE_DEFINE(dynamic_untyped_allocators_init, init_system);
80