diff --git a/src/manager.S b/src/manager.S index 5eb6b19..f44baa0 100644 --- a/src/manager.S +++ b/src/manager.S @@ -103,44 +103,62 @@ init_compartments: /** * Function to add information for a compartment * - * @param c1 Start address - * @param c2 Size of memory region - * @param c3 Function address + * @param x0 Compartment memory size + * @param x1 Compartment executable function + * + * @return Pointer to newly allocated memory region */ .type add_compartment, "function" add_compartment: + // Store inputs and `lr` so we can call `mmap` + stp x0, x1, [sp, #-32]! + str lr, [sp, #16] + mov x1, x0 + + // Allocate memory for new compartment + mov x0, xzr // address + // length - already stored in `x1` + mov w2, #3 // prot == PROT_READ | PROT_WRITE + mov w3, #4098 // flags == MAP_PRIVATE | MAP_ANONYMOUS + mov w4, #-1 // fd + mov w5, wzr // offset + bl mmap + + // Restore memory size and function address + ldp x1, x2, [sp], #32 + ldr lr, [sp, #-16] - // Derive compartment PCC - cvtp c3, x3 - mov x4, #320 // TODO dynamic value - scbndse c3, c3, x4 - - // Derive compartment DDC - cvtp c1, x1 - scbnds c2, c1, x2 - - // Store new PCC and DDC - ldr x0, comps_addr - ldr x1, comps_cnt - mov x3, #COMP_SIZE - madd x0, x1, x2, x0 - stp c3, c2, [x0] - - // Increment counter - adr x3, comps_cnt - ldr x4, [x3] - add x4, x4, #1 - str x4, [x3] - - // Update switcher DDC - adr x2, switcher_caps - ldr c0, [x2] - gclen x1, c0 - add x1, x1, #32 - scbndse c0, c0, x1 - str c0, [x2] - - ret + // Derive compartment DDC + cvtp c0, x0 + scbnds c0, c0, x1 + + // Derive compartment PCC + cvtp c1, x2 + mov x2, #320 // TODO dynamic value + scbndse c1, c1, x2 + + // Store new PCC and DDC + ldr x2, comps_addr + ldr x3, comps_cnt + mov x4, #COMP_SIZE + madd x2, x3, x4, x2 + stp c0, c1, [x2] + + // Increment counter + adr x3, comps_cnt + ldr x4, [x3] + add x4, x4, #1 + str x4, [x3] + + // Update switcher DDC + //adr x2, switcher_caps + //ldr c0, [x2] + //gclen x1, c0 + //add x1, x1, #32 + //scbndse c0, c0, x1 + //str c0, [x2] + + ret /** * Function to delete an existing compartment data diff --git a/tests/simple_add.c b/tests/simple_add.c index 83118b6..82d0044 100644 --- a/tests/simple_add.c +++ b/tests/simple_add.c @@ -1,6 +1,7 @@ -#include "assert.h" -#include "stdint.h" -#include "stdlib.h" +#include +#include +#include +#include #include "cheriintrin.h" @@ -10,24 +11,15 @@ static_assert(COMP_SIZE == sizeof(void* __capability) * 3, "Invalid `COMP_SIZE` static_assert(COMP_OFFSET_DDC == sizeof(void* __capability) * 1, "Invalid `COMP_OFFSET_DDC` provided."); static_assert(COMP_OFFSET_STK_ADDR == sizeof(void* __capability) * 2, "Invalid `COMP_OFFSET_STK_LEN` provided."); -/******************************************************************************* - * Globals and constants - ******************************************************************************/ - -const size_t max_comp_cnt = 2; -const size_t switcher_mem_max_size = max_comp_cnt * COMP_SIZE; - /******************************************************************************* * Extern functions ******************************************************************************/ -extern void asm_call_wrapper(void*, ...); -extern void init_compartments(void*, size_t, void*); -extern void add_compartment(void*, size_t, void*); -extern int switch_compartment(); +extern void* __capability * comps_addr; +extern size_t comps_cnt; -extern void* comps_addr; -extern void* switcher_caps; +extern void* init_compartments(); +extern void* add_compartment(size_t, void*); /******************************************************************************* * Main @@ -38,19 +30,20 @@ int comp_f_fn(); int main() { - switcher_caps = malloc(sizeof(void* __capability) * 2); - comps_addr = malloc(COMP_SIZE * max_comp_cnt); + init_compartments(); + + size_t comp_size = 2000; + assert(add_compartment(comp_size, comp_f_fn) != MAP_FAILED); - void* switcher_start = malloc(switcher_mem_max_size); - void* switch_comp_addr = switch_compartment; + assert(comps_cnt == 1); - asm_call_wrapper(init_compartments, - switcher_start, switcher_mem_max_size, switch_comp_addr); + void* __capability comp_ddc = comps_addr[0]; + assert(cheri_is_valid(comp_ddc)); + assert(cheri_length_get(comp_ddc) == comp_size); - const size_t comp_f_size = 1000; - uintptr_t comp_f_start = (uintptr_t) malloc(comp_f_size); - asm_call_wrapper(add_compartment, - comp_f_start, comp_f_size, comp_f_fn); + void* __capability comp_pcc = comps_addr[1]; + assert(cheri_is_valid(comp_pcc)); + assert(cheri_address_get(comp_pcc) == (unsigned long) comp_f_fn); return 0; }