[libc++][Android] Allow testing libc++ with clang-r536225 (#116149)
[llvm-project.git] / libc / startup / linux / riscv / tls.cpp
blob04d44e6ca882e297672d36904db483da0016a026
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 //===----------------------------------------------------------------------===//
9 #include "src/__support/OSUtil/syscall.h"
10 #include "src/__support/macros/config.h"
11 #include "src/__support/threads/thread.h"
12 #include "src/string/memory_utils/inline_memcpy.h"
13 #include "startup/linux/do_start.h"
15 #include <sys/mman.h>
16 #include <sys/syscall.h>
18 namespace LIBC_NAMESPACE_DECL {
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
28 void init_tls(TLSDescriptor &tls_descriptor) {
29 if (app.tls.size == 0) {
30 tls_descriptor.size = 0;
31 tls_descriptor.tp = 0;
32 return;
35 // riscv64 follows the variant 1 TLS layout:
36 const uintptr_t size_of_pointers = 2 * sizeof(uintptr_t);
37 uintptr_t padding = 0;
38 const uintptr_t ALIGNMENT_MASK = app.tls.align - 1;
39 uintptr_t diff = size_of_pointers & ALIGNMENT_MASK;
40 if (diff != 0)
41 padding += (ALIGNMENT_MASK - diff) + 1;
43 uintptr_t alloc_size = size_of_pointers + padding + app.tls.size;
45 // We cannot call the mmap function here as the functions set errno on
46 // failure. Since errno is implemented via a thread local variable, we cannot
47 // use errno before TLS is setup.
48 long mmap_ret_val = syscall_impl<long>(MMAP_SYSCALL_NUMBER, nullptr,
49 alloc_size, PROT_READ | PROT_WRITE,
50 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
51 // We cannot check the return value with MAP_FAILED as that is the return
52 // of the mmap function and not the mmap syscall.
53 if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.page_size)
54 syscall_impl<long>(SYS_exit, 1);
55 uintptr_t thread_ptr = uintptr_t(reinterpret_cast<uintptr_t *>(mmap_ret_val));
56 uintptr_t tls_addr = thread_ptr + size_of_pointers + padding;
57 inline_memcpy(reinterpret_cast<char *>(tls_addr),
58 reinterpret_cast<const char *>(app.tls.address),
59 app.tls.init_size);
60 tls_descriptor.size = alloc_size;
61 tls_descriptor.addr = thread_ptr;
62 tls_descriptor.tp = tls_addr;
65 void cleanup_tls(uintptr_t addr, uintptr_t size) {
66 if (size == 0)
67 return;
68 syscall_impl<long>(SYS_munmap, addr, size);
71 bool set_thread_ptr(uintptr_t val) {
72 LIBC_INLINE_ASM("mv tp, %0\n\t" : : "r"(val));
73 return true;
75 } // namespace LIBC_NAMESPACE_DECL