Skip to content

Commit

Permalink
c18n: Use superpages to store trampolines
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgao committed Apr 22, 2024
1 parent 5637264 commit c73231e
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions libexec/rtld-elf/rtld_c18n.c
Original file line number Diff line number Diff line change
Expand Up @@ -1023,16 +1023,12 @@ struct tramp_pg {
_Alignas(_Alignof(void *)) char trampolines[];
};

/*
* 64K is the largest size such that any capability-aligned proper
* sub-range can be exactly bounded by a Morello capability.
*/
#define C18N_TRAMPOLINE_PAGE_SIZE 64 * 1024
static size_t tramp_pg_size;

static struct tramp_pg *
tramp_pg_new(struct tramp_pg *next)
{
size_t capacity = C18N_TRAMPOLINE_PAGE_SIZE;
size_t capacity = tramp_pg_size;
struct tramp_pg *pg;

pg = mmap(NULL, capacity, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON,
Expand Down Expand Up @@ -1068,7 +1064,7 @@ tramp_pg_push(struct tramp_pg *pg, size_t len)
*/
&size, n_size, memory_order_relaxed, memory_order_relaxed));

tramp = cheri_setbounds(tramp, len);
tramp = cheri_setboundsexact(tramp, len);
assert(cheri_gettag(tramp));

return (tramp);
Expand Down Expand Up @@ -1603,10 +1599,17 @@ c18n_init(Obj_Entry *obj_rtld, Elf_Auxinfo *aux_info[])
}
}

/*
* Trampoline pages should be large to minimise pressure on the TLB, but not too
* large. 4MB is a reasonable threshold.
*/
#define MAX_TRAMP_PG_SIZE (4 * 1024 * 1024)

void
c18n_init2(void)
{
uintptr_t sealer;
int n = npagesizes;

/*
* Allocate otypes for RTLD use
Expand Down Expand Up @@ -1642,8 +1645,14 @@ c18n_init2(void)
expand_tramp_table(9);

/*
* Create the first trampoline page.
* Find a suitable page size and create the first trampoline page.
*/
while (n-- > 0)
if (pagesizes[n] <= MAX_TRAMP_PG_SIZE) {
tramp_pg_size = pagesizes[n];
break;
}
assert(tramp_pg_size > 0);
atomic_store_explicit(&tramp_pgs.head, tramp_pg_new(NULL),
memory_order_relaxed);
}
Expand Down

0 comments on commit c73231e

Please sign in to comment.