1 //===-- Linux implementation of the thrd_create function ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "config/linux/syscall.h" // For syscall function.
10 #include "include/errno.h"        // For E* error values.
11 #include "include/sys/mman.h"     // For PROT_* and MAP_* definitions.
12 #include "include/sys/syscall.h"  // For syscall numbers.
13 #include "include/threads.h"      // For thrd_* type definitions.
14 #include "src/__support/common.h"
15 #include "src/errno/llvmlibc_errno.h"
16 #include "src/sys/mman/mmap.h"
17 #include "src/sys/mman/munmap.h"
18 #include "src/threads/linux/thread_utils.h"
19 
20 #include <linux/futex.h> // For futex operations.
21 #include <linux/sched.h> // For CLONE_* flags.
22 #include <stdint.h>
23 
24 namespace __llvm_libc {
25 
26 struct StartArgs {
27   thrd_t *thread;
28   thrd_start_t func;
29   void *arg;
30 };
31 
start_thread()32 static __attribute__((noinline)) void start_thread() {
33   StartArgs *start_args = reinterpret_cast<StartArgs *>(get_start_args_addr());
34   __llvm_libc::syscall(SYS_exit, start_args->thread->__retval =
35                                      start_args->func(start_args->arg));
36 }
37 
LLVM_LIBC_ENTRYPOINT(thrd_create)38 int LLVM_LIBC_ENTRYPOINT(thrd_create)(thrd_t *thread, thrd_start_t func,
39                                       void *arg) {
40   unsigned clone_flags =
41       CLONE_VM        // Share the memory space with the parent.
42       | CLONE_FS      // Share the file system with the parent.
43       | CLONE_FILES   // Share the files with the parent.
44       | CLONE_SIGHAND // Share the signal handlers with the parent.
45       | CLONE_THREAD  // Same thread group as the parent.
46       | CLONE_SYSVSEM // Share a single list of System V semaphore adjustment
47                       // values
48       | CLONE_PARENT_SETTID   // Set child thread ID in |ptid| of the parent.
49       | CLONE_CHILD_CLEARTID; // Let the kernel clear the tid address and futex
50                               // wake the joining thread.
51   // TODO: Add the CLONE_SETTLS flag and setup the TLS area correctly when
52   // making the clone syscall.
53 
54   void *stack = __llvm_libc::mmap(nullptr, ThreadParams::DefaultStackSize,
55                                   PROT_READ | PROT_WRITE,
56                                   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
57   if (stack == MAP_FAILED)
58     return llvmlibc_errno == ENOMEM ? thrd_nomem : thrd_error;
59 
60   thread->__stack = stack;
61   thread->__stack_size = ThreadParams::DefaultStackSize;
62   thread->__retval = -1;
63   FutexData *clear_tid_address =
64       reinterpret_cast<FutexData *>(thread->__clear_tid);
65   *clear_tid_address = ThreadParams::ClearTIDValue;
66 
67   // When the new thread is spawned by the kernel, the new thread gets the
68   // stack we pass to the clone syscall. However, this stack is empty and does
69   // not have any local vars present in this function. Hence, one cannot
70   // pass arguments to the thread start function, or use any local vars from
71   // here. So, we pack them into the new stack from where the thread can sniff
72   // them out.
73   uintptr_t adjusted_stack = reinterpret_cast<uintptr_t>(stack) +
74                              ThreadParams::DefaultStackSize - sizeof(StartArgs);
75   StartArgs *start_args = reinterpret_cast<StartArgs *>(adjusted_stack);
76   start_args->thread = thread;
77   start_args->func = func;
78   start_args->arg = arg;
79 
80   // TODO: The arguments to the clone syscall below is correct for x86_64
81   // but it might differ for other architectures. So, make this call
82   // architecture independent. May be implement a glibc like wrapper for clone
83   // and use it here.
84   long clone_result =
85       __llvm_libc::syscall(SYS_clone, clone_flags, adjusted_stack,
86                            &thread->__tid, clear_tid_address, 0);
87 
88   if (clone_result == 0) {
89     start_thread();
90   } else if (clone_result < 0) {
91     int error_val = -clone_result;
92     return error_val == ENOMEM ? thrd_nomem : thrd_error;
93   }
94 
95   return thrd_success;
96 }
97 
98 } // namespace __llvm_libc
99