diff options
author | ed <ed@FreeBSD.org> | 2016-04-06 11:11:31 +0000 |
---|---|---|
committer | ed <ed@FreeBSD.org> | 2016-04-06 11:11:31 +0000 |
commit | e55c02e6f81af885baabf454832a1908013331f5 (patch) | |
tree | b34a51eca356eac838df8dddb88435653105bae9 /sys/arm64 | |
parent | f5a4347e1c8f2e48ca0d0185b8c27fb25c428c27 (diff) | |
download | FreeBSD-src-e55c02e6f81af885baabf454832a1908013331f5.zip FreeBSD-src-e55c02e6f81af885baabf454832a1908013331f5.tar.gz |
Make CloudABI's way of doing TLS more friendly to userspace emulators.
We're currently seeing how hard it would be to run CloudABI binaries on
operating systems cannot be modified easily (Windows, Mac OS X). The
idea is that we want to just run them without any sandboxing. Now
that CloudABI executables are PIE, this is already a bit easier, but TLS
is still problematic:
- CloudABI executables want to write to the %fs, which typically
requires extra system calls by the emulator every time it needs to
switch between CloudABI's and its own TLS.
- If CloudABI executables overwrite the %fs base unconditionally, it
also becomes harder for the emulator to store a backup of the old
value of %fs. To solve this, let's no longer overwrite %fs, but just
%fs:0.
As CloudABI's C library does not use a TCB, this space can now be used
by an emulator to keep track of its internal state. The executable can
now safely overwrite %fs:0, as long as it makes sure that the TCB is
copied over to the new TLS area.
Ensure that there is an initial TLS area set up when the process starts,
only containing a bogus TCB. We don't really care about its contents on
FreeBSD.
Reviewed by: kib
Differential Revision: https://reviews.freebsd.org/D5836
Diffstat (limited to 'sys/arm64')
-rw-r--r-- | sys/arm64/cloudabi64/cloudabi64_sysvec.c | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/sys/arm64/cloudabi64/cloudabi64_sysvec.c b/sys/arm64/cloudabi64/cloudabi64_sysvec.c index a26007a..fd13db8 100644 --- a/sys/arm64/cloudabi64/cloudabi64_sysvec.c +++ b/sys/arm64/cloudabi64/cloudabi64_sysvec.c @@ -27,6 +27,7 @@ __FBSDID("$FreeBSD$"); #include <sys/param.h> +#include <sys/imgact.h> #include <sys/kernel.h> #include <sys/proc.h> #include <sys/sysent.h> @@ -46,6 +47,25 @@ __FBSDID("$FreeBSD$"); extern const char *cloudabi64_syscallnames[]; extern struct sysent cloudabi64_sysent[]; +static void +cloudabi64_proc_setregs(struct thread *td, struct image_params *imgp, + unsigned long stack) +{ + struct trapframe *regs; + + exec_setregs(td, imgp, stack); + + /* + * The stack now contains a pointer to the TCB and the auxiliary + * vector. Let x0 point to the auxiliary vector, and set + * tpidr_el0 to the TCB. + */ + regs = td->td_frame; + regs->tf_x[0] = td->td_retval[0] = + stack + roundup(sizeof(cloudabi64_tcb_t), sizeof(register_t)); + (void)cpu_set_user_tls(td, (void *)stack); +} + static int cloudabi64_fetch_syscall_args(struct thread *td, struct syscall_args *sa) { @@ -110,9 +130,9 @@ cloudabi64_schedtail(struct thread *td) } } -void +int cloudabi64_thread_setregs(struct thread *td, - const cloudabi64_threadattr_t *attr) + const cloudabi64_threadattr_t *attr, uint64_t tcb) { struct trapframe *frame; stack_t stack; @@ -130,6 +150,9 @@ cloudabi64_thread_setregs(struct thread *td, frame = td->td_frame; frame->tf_x[0] = td->td_tid; frame->tf_x[1] = attr->argument; + + /* Set up TLS. */ + return (cpu_set_user_tls(td, (void *)tcb)); } static struct sysentvec cloudabi64_elf_sysvec = { @@ -144,6 +167,7 @@ static struct sysentvec cloudabi64_elf_sysvec = { .sv_usrstack = USRSTACK, .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, .sv_copyout_strings = cloudabi64_copyout_strings, + .sv_setregs = cloudabi64_proc_setregs, .sv_flags = SV_ABI_CLOUDABI | SV_CAPSICUM | SV_LP64, .sv_set_syscall_retval = cloudabi64_set_syscall_retval, .sv_fetch_syscall_args = cloudabi64_fetch_syscall_args, |