From: Dragos Iulian Argint Date: Sun, 14 Aug 2022 22:13:29 +0000 (+0300) Subject: pthread_create: TLS alignment and cleanup X-Git-Tag: RELEASE-0.11.0~24 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=523f201d1685b272a166f99e7d1580f727f1d06a;p=unikraft%2Flibs%2Fmusl.git pthread_create: TLS alignment and cleanup This patch replaces 2 function calls from musl with 2 custom ones and introduces an additional size when allocating the map for a thread, `libc.tls_align`. It is needed because Unikraft demands that the tls be aligned. Signed-off-by: Dragos Iulian Argint Reviewed-by: Florin Postolache Reviewed-by: Simon Kuenzer Reviewed-by: Cezar Craciunoiu Reviewed-by: Razvan Deaconescu Reviewed-by: Robert Kuban Approved-by: Simon Kuenzer Tested-by: Unikraft CI GitHub-Closes: #9 --- diff --git a/patches/0014-Change-some-things-in-pthread_create.c.patch b/patches/0014-Change-some-things-in-pthread_create.c.patch new file mode 100644 index 0000000..fe3318d --- /dev/null +++ b/patches/0014-Change-some-things-in-pthread_create.c.patch @@ -0,0 +1,89 @@ +From cab8414305bdbb07a9adfa72682d110fd7365da9 Mon Sep 17 00:00:00 2001 +From: Dragos Iulian Argint +Date: Sun, 14 Aug 2022 20:14:33 +0300 +Subject: [PATCH] Change some things in pthread_create.c + +This patch replaces 2 function calls from musl with +2 custom ones and introduces an additional size +when allocating the map for a thread, `libc.tls_align`. +It is needed because Unikraft demands that the tls be aligned. + +Signed-off-by: Dragos Iulian Argint +--- + src/thread/pthread_create.c | 28 +++++++++++++++++++++------- + 1 file changed, 21 insertions(+), 7 deletions(-) + +diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c +index 439ee36..d7d5da9 100644 +--- a/src/thread/pthread_create.c ++++ b/src/thread/pthread_create.c +@@ -10,6 +10,11 @@ void *__mmap(void *, size_t, int, int, int, off_t); + int __munmap(void *, size_t); + int __mprotect(void *, size_t, int); + ++/* ++ * Unikraft version of `__unmapself()`. See `__uk_unmapself.c` ++ */ ++void __uk_unmapself(void *base, size_t size); ++ + static void dummy_0() + { + } +@@ -108,9 +113,15 @@ _Noreturn void __pthread_exit(void *result) + * explicitly wait for vmlock holders first. */ + __vm_wait(); + +- /* The following call unmaps the thread's stack mapping +- * and then exits without touching the stack. */ +- __unmapself(self->map_base, self->map_size); ++ /* ++ * In the original code `__unampself()` was called here. ++ * That call unmaps the thread's stack mapping and then ++ * exits without touching the stack. However, we cannot ++ * do this since we don't have a kernel stack. We use ++ * a different approach and let the `idle` thread do the ++ * cleaning. Please refer to the `__uk_unampself.c` file. ++ */ ++ __uk_unmapself(self->map_base, self->map_size); + } + + for (;;) __syscall(SYS_exit, 0); +@@ -181,7 +192,10 @@ static void init_file_lock(FILE *f) + if (f && f->lock<0) f->lock = 0; + } + +-void *__copy_tls(unsigned char *); ++/* ++ * Unikraft version of `__copy_tls()` ++ */ ++void *__uk_copy_tls(unsigned char *); + + int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict attrp, void *(*entry)(void *), void *restrict arg) + { +@@ -237,7 +251,7 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att + } else { + guard = ROUND(attr._a_guardsize); + size = guard + ROUND(attr._a_stacksize +- + libc.tls_size + __pthread_tsd_size); ++ + libc.tls_size + libc.tls_align + __pthread_tsd_size); + } + + if (!tsd) { +@@ -255,12 +269,12 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att + } + tsd = map + size - __pthread_tsd_size; + if (!stack) { +- stack = tsd - libc.tls_size; ++ stack = tsd - libc.tls_size - libc.tls_align; + stack_limit = map + guard; + } + } + +- new = __copy_tls(tsd - libc.tls_size); ++ new = __uk_copy_tls(tsd - libc.tls_size); + new->map_base = map; + new->map_size = size; + new->stack = stack; +-- +2.17.1 +