xref: /llvm-project/libc/startup/linux/x86_64/tls.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1 //===-- Implementation of tls for x86_64 ----------------------------------===//
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 "src/__support/OSUtil/syscall.h"
10 #include "src/__support/macros/config.h"
11 #include "src/string/memory_utils/inline_memcpy.h"
12 #include "startup/linux/do_start.h"
13 
14 #include <asm/prctl.h>
15 #include <sys/mman.h>
16 #include <sys/syscall.h>
17 
18 namespace LIBC_NAMESPACE_DECL {
19 
20 #ifdef SYS_mmap2
21 static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap2;
22 #elif SYS_mmap
23 static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap;
24 #else
25 #error "mmap and mmap2 syscalls not available."
26 #endif
27 
28 // TODO: Also generalize this routine and handle dynamic loading properly.
29 void init_tls(TLSDescriptor &tls_descriptor) {
30   if (app.tls.size == 0) {
31     tls_descriptor.size = 0;
32     tls_descriptor.tp = 0;
33     return;
34   }
35 
36   // We will assume the alignment is always a power of two.
37   uintptr_t tls_size = app.tls.size & -app.tls.align;
38   if (tls_size != app.tls.size)
39     tls_size += app.tls.align;
40 
41   // Per the x86_64 TLS ABI, the entry pointed to by the thread pointer is the
42   // address of the TLS block. So, we add more size to accomodate this address
43   // entry.
44   // We also need to include space for the stack canary. The canary is at
45   // offset 0x28 (40) and is of size uintptr_t.
46   uintptr_t tls_size_with_addr = tls_size + sizeof(uintptr_t) + 40;
47 
48   // We cannot call the mmap function here as the functions set errno on
49   // failure. Since errno is implemented via a thread local variable, we cannot
50   // use errno before TLS is setup.
51   long mmap_retval = syscall_impl<long>(
52       MMAP_SYSCALL_NUMBER, nullptr, tls_size_with_addr, PROT_READ | PROT_WRITE,
53       MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
54   // We cannot check the return value with MAP_FAILED as that is the return
55   // of the mmap function and not the mmap syscall.
56   if (mmap_retval < 0 && static_cast<uintptr_t>(mmap_retval) > -app.page_size)
57     syscall_impl<long>(SYS_exit, 1);
58   uintptr_t *tls_addr = reinterpret_cast<uintptr_t *>(mmap_retval);
59 
60   // x86_64 TLS faces down from the thread pointer with the first entry
61   // pointing to the address of the first real TLS byte.
62   uintptr_t end_ptr = reinterpret_cast<uintptr_t>(tls_addr) + tls_size;
63   *reinterpret_cast<uintptr_t *>(end_ptr) = end_ptr;
64 
65   inline_memcpy(reinterpret_cast<char *>(tls_addr),
66                 reinterpret_cast<const char *>(app.tls.address),
67                 app.tls.init_size);
68   uintptr_t *stack_guard_addr = reinterpret_cast<uintptr_t *>(end_ptr + 40);
69   // Setting the stack guard to a random value.
70   // We cannot call the get_random function here as the function sets errno on
71   // failure. Since errno is implemented via a thread local variable, we cannot
72   // use errno before TLS is setup.
73   long stack_guard_retval =
74       syscall_impl(SYS_getrandom, reinterpret_cast<long>(stack_guard_addr),
75                    sizeof(uint64_t), 0);
76   if (stack_guard_retval < 0)
77     syscall_impl(SYS_exit, 1);
78 
79   tls_descriptor = {tls_size_with_addr, reinterpret_cast<uintptr_t>(tls_addr),
80                     end_ptr};
81   return;
82 }
83 
84 void cleanup_tls(uintptr_t addr, uintptr_t size) {
85   if (size == 0)
86     return;
87   syscall_impl<long>(SYS_munmap, addr, size);
88 }
89 
90 // Sets the thread pointer to |val|. Returns true on success, false on failure.
91 bool set_thread_ptr(uintptr_t val) {
92   return syscall_impl(SYS_arch_prctl, ARCH_SET_FS, val) != -1;
93 }
94 } // namespace LIBC_NAMESPACE_DECL
95