summaryrefslogtreecommitdiffstats
path: root/sys/amd64
diff options
context:
space:
mode:
authored <ed@FreeBSD.org>2016-04-06 11:11:31 +0000
committered <ed@FreeBSD.org>2016-04-06 11:11:31 +0000
commite55c02e6f81af885baabf454832a1908013331f5 (patch)
treeb34a51eca356eac838df8dddb88435653105bae9 /sys/amd64
parentf5a4347e1c8f2e48ca0d0185b8c27fb25c428c27 (diff)
downloadFreeBSD-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/amd64')
-rw-r--r--sys/amd64/cloudabi64/cloudabi64_sysvec.c64
1 files changed, 60 insertions, 4 deletions
diff --git a/sys/amd64/cloudabi64/cloudabi64_sysvec.c b/sys/amd64/cloudabi64/cloudabi64_sysvec.c
index 16d3ef9..b94c0ef 100644
--- a/sys/amd64/cloudabi64/cloudabi64_sysvec.c
+++ b/sys/amd64/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>
@@ -47,6 +48,45 @@ extern const char *cloudabi64_syscallnames[];
extern struct sysent cloudabi64_sysent[];
static int
+cloudabi64_fixup_tcb(register_t **stack_base, struct image_params *imgp)
+{
+ int error;
+ register_t tcbptr;
+
+ /* Place auxiliary vector and TCB on the stack. */
+ error = cloudabi64_fixup(stack_base, imgp);
+ if (error != 0)
+ return (error);
+
+ /*
+ * On x86-64, the TCB is referred to by %fs:0. Take some space
+ * from the top of the stack to store a single element array,
+ * containing a pointer to the TCB. %fs base will point to this.
+ */
+ tcbptr = (register_t)*stack_base;
+ return (copyout(&tcbptr, --*stack_base, sizeof(tcbptr)));
+}
+
+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, the TCB itself,
+ * and the auxiliary vector. Let %rdx point to the auxiliary
+ * vector, and set %fs base to the address of the TCB.
+ */
+ regs = td->td_frame;
+ regs->tf_rdi = stack + sizeof(register_t) +
+ 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)
{
struct trapframe *frame = td->td_frame;
@@ -109,16 +149,29 @@ cloudabi64_schedtail(struct thread *td)
frame->tf_rdx = td->td_tid;
}
-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;
+ uint64_t tcbptr;
+ int error;
+
+ /*
+ * On x86-64, the TCB is referred to by %fs:0. Take some space
+ * from the top of the stack to store a single element array,
+ * containing a pointer to the TCB. %fs base will point to this.
+ */
+ tcbptr = rounddown(attr->stack + attr->stack_size - sizeof(tcbptr),
+ _Alignof(tcbptr));
+ error = copyout(&tcb, (void *)tcbptr, sizeof(tcb));
+ if (error != 0)
+ return (error);
/* Perform standard register initialization. */
stack.ss_sp = (void *)attr->stack;
- stack.ss_size = attr->stack_size;
+ stack.ss_size = tcbptr - attr->stack;
cpu_set_upcall_kse(td, (void *)attr->entry_point, NULL, &stack);
/*
@@ -129,12 +182,14 @@ cloudabi64_thread_setregs(struct thread *td,
frame = td->td_frame;
frame->tf_rdi = td->td_tid;
frame->tf_rsi = attr->argument;
+
+ return (cpu_set_user_tls(td, (void *)tcbptr));
}
static struct sysentvec cloudabi64_elf_sysvec = {
.sv_size = CLOUDABI64_SYS_MAXSYSCALL,
.sv_table = cloudabi64_sysent,
- .sv_fixup = cloudabi64_fixup,
+ .sv_fixup = cloudabi64_fixup_tcb,
.sv_name = "CloudABI ELF64",
.sv_coredump = elf64_coredump,
.sv_pagesize = PAGE_SIZE,
@@ -143,6 +198,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,
OpenPOWER on IntegriCloud