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 May 8, 2024
1 parent 885b0e5 commit 2fafb2c
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 @@ -973,16 +973,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 @@ -1014,7 +1010,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 @@ -1510,10 +1506,17 @@ c18n_init(Obj_Entry *obj_rtld)
}
}

/*
* 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 @@ -1549,8 +1552,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 2fafb2c

Please sign in to comment.