path: root/sys/compat/cloudabi64
diff options
authorEd Schouten <ed@FreeBSD.org>2016-04-06 11:11:31 +0000
committerEd Schouten <ed@FreeBSD.org>2016-04-06 11:11:31 +0000
commitab8357507077525f79ba675f5b4daf505ab3fa01 (patch)
treeb34a51eca356eac838df8dddb88435653105bae9 /sys/compat/cloudabi64
parent1c7c13aa0e7ad180c67fb2dc24964febe4a44bab (diff)
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
Notes: svn path=/head/; revision=297613
Diffstat (limited to 'sys/compat/cloudabi64')
3 files changed, 17 insertions, 5 deletions
diff --git a/sys/compat/cloudabi64/cloudabi64_module.c b/sys/compat/cloudabi64/cloudabi64_module.c
index de890bc5c652..246a887105cb 100644
--- a/sys/compat/cloudabi64/cloudabi64_module.c
+++ b/sys/compat/cloudabi64/cloudabi64_module.c
@@ -112,7 +112,13 @@ cloudabi64_fixup(register_t **stack_base, struct image_params *imgp)
{ .a_type = CLOUDABI_AT_NULL },
*stack_base -= howmany(sizeof(auxv), sizeof(register_t));
- return (copyout(auxv, *stack_base, sizeof(auxv)));
+ error = copyout(auxv, *stack_base, sizeof(auxv));
+ if (error != 0)
+ return (error);
+ /* Reserve space for storing the TCB. */
+ *stack_base -= howmany(sizeof(cloudabi64_tcb_t), sizeof(register_t));
+ return (0);
static int
diff --git a/sys/compat/cloudabi64/cloudabi64_thread.c b/sys/compat/cloudabi64/cloudabi64_thread.c
index 51961f8ad29b..429ad3398be6 100644
--- a/sys/compat/cloudabi64/cloudabi64_thread.c
+++ b/sys/compat/cloudabi64/cloudabi64_thread.c
@@ -37,6 +37,7 @@ __FBSDID("$FreeBSD$");
struct thread_create_args {
cloudabi64_threadattr_t attr;
+ uint64_t tcb;
lwpid_t tid;
@@ -49,8 +50,7 @@ initialize_thread(struct thread *td, void *thunk)
args->tid = td->td_tid;
/* Set up initial register contents. */
- cloudabi64_thread_setregs(td, &args->attr);
- return (0);
+ return (cloudabi64_thread_setregs(td, &args->attr, args->tcb));
@@ -63,6 +63,12 @@ cloudabi64_sys_thread_create(struct thread *td,
error = copyin(uap->attr, &args.attr, sizeof(args.attr));
if (error != 0)
return (error);
+ /* Remove some space on the top of the stack for the TCB. */
+ args.tcb = rounddown(args.attr.stack + args.attr.stack_size -
+ sizeof(cloudabi64_tcb_t), _Alignof(cloudabi64_tcb_t));
+ args.attr.stack_size = args.tcb - args.attr.stack;
error = thread_create(td, NULL, initialize_thread, &args);
if (error != 0)
return (error);
diff --git a/sys/compat/cloudabi64/cloudabi64_util.h b/sys/compat/cloudabi64/cloudabi64_util.h
index dcec70ece5f4..29a90d779821 100644
--- a/sys/compat/cloudabi64/cloudabi64_util.h
+++ b/sys/compat/cloudabi64/cloudabi64_util.h
@@ -42,7 +42,7 @@ extern Elf64_Brandinfo cloudabi64_brand;
register_t *cloudabi64_copyout_strings(struct image_params *);
int cloudabi64_fixup(register_t **, struct image_params *);
-void cloudabi64_thread_setregs(struct thread *,
- const cloudabi64_threadattr_t *);
+int cloudabi64_thread_setregs(struct thread *,
+ const cloudabi64_threadattr_t *, uint64_t);