1 | //===-- Implementation of tls for riscv -----------------------------------===// |
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/threads/thread.h" |
11 | #include "src/string/memory_utils/inline_memcpy.h" |
12 | #include "startup/linux/do_start.h" |
13 | |
14 | #include <sys/mman.h> |
15 | #include <sys/syscall.h> |
16 | |
17 | namespace LIBC_NAMESPACE { |
18 | |
19 | #ifdef SYS_mmap2 |
20 | static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap2; |
21 | #elif SYS_mmap |
22 | static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap; |
23 | #else |
24 | #error "mmap and mmap2 syscalls not available." |
25 | #endif |
26 | |
27 | void init_tls(TLSDescriptor &tls_descriptor) { |
28 | if (app.tls.size == 0) { |
29 | tls_descriptor.size = 0; |
30 | tls_descriptor.tp = 0; |
31 | return; |
32 | } |
33 | |
34 | // riscv64 follows the variant 1 TLS layout: |
35 | const uintptr_t size_of_pointers = 2 * sizeof(uintptr_t); |
36 | uintptr_t padding = 0; |
37 | const uintptr_t ALIGNMENT_MASK = app.tls.align - 1; |
38 | uintptr_t diff = size_of_pointers & ALIGNMENT_MASK; |
39 | if (diff != 0) |
40 | padding += (ALIGNMENT_MASK - diff) + 1; |
41 | |
42 | uintptr_t alloc_size = size_of_pointers + padding + app.tls.size; |
43 | |
44 | // We cannot call the mmap function here as the functions set errno on |
45 | // failure. Since errno is implemented via a thread local variable, we cannot |
46 | // use errno before TLS is setup. |
47 | long mmap_ret_val = syscall_impl<long>(number: MMAP_SYSCALL_NUMBER, ts: nullptr, |
48 | ts: alloc_size, PROT_READ | PROT_WRITE, |
49 | MAP_ANONYMOUS | MAP_PRIVATE, ts: -1, ts: 0); |
50 | // We cannot check the return value with MAP_FAILED as that is the return |
51 | // of the mmap function and not the mmap syscall. |
52 | if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.page_size) |
53 | syscall_impl<long>(SYS_exit, ts: 1); |
54 | uintptr_t thread_ptr = uintptr_t(reinterpret_cast<uintptr_t *>(mmap_ret_val)); |
55 | uintptr_t tls_addr = thread_ptr + size_of_pointers + padding; |
56 | inline_memcpy(dst: reinterpret_cast<char *>(tls_addr), |
57 | src: reinterpret_cast<const char *>(app.tls.address), |
58 | count: app.tls.init_size); |
59 | tls_descriptor.size = alloc_size; |
60 | tls_descriptor.addr = thread_ptr; |
61 | tls_descriptor.tp = tls_addr; |
62 | } |
63 | |
64 | void cleanup_tls(uintptr_t addr, uintptr_t size) { |
65 | if (size == 0) |
66 | return; |
67 | syscall_impl<long>(SYS_munmap, ts: addr, ts: size); |
68 | } |
69 | |
70 | bool set_thread_ptr(uintptr_t val) { |
71 | LIBC_INLINE_ASM("mv tp, %0\n\t" : : "r" (val)); |
72 | return true; |
73 | } |
74 | } // namespace LIBC_NAMESPACE |
75 | |