1//===-- Implementation of tls for aarch64 ---------------------------------===//
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 <arm_acle.h>
15#include <sys/mman.h>
16#include <sys/syscall.h>
17
18// Source documentation:
19// https://github.com/ARM-software/abi-aa/tree/main/sysvabi64
20
21namespace LIBC_NAMESPACE {
22
23#ifdef SYS_mmap2
24static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap2;
25#elif SYS_mmap
26static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap;
27#else
28#error "mmap and mmap2 syscalls not available."
29#endif
30
31void init_tls(TLSDescriptor &tls_descriptor) {
32 if (app.tls.size == 0) {
33 tls_descriptor.size = 0;
34 tls_descriptor.tp = 0;
35 return;
36 }
37
38 // aarch64 follows the variant 1 TLS layout:
39 //
40 // 1. First entry is the dynamic thread vector pointer
41 // 2. Second entry is a 8-byte reserved word.
42 // 3. Padding for alignment.
43 // 4. The TLS data from the ELF image.
44 //
45 // The thread pointer points to the first entry.
46
47 const uintptr_t size_of_pointers = 2 * sizeof(uintptr_t);
48 uintptr_t padding = 0;
49 const uintptr_t ALIGNMENT_MASK = app.tls.align - 1;
50 uintptr_t diff = size_of_pointers & ALIGNMENT_MASK;
51 if (diff != 0)
52 padding += (ALIGNMENT_MASK - diff) + 1;
53
54 uintptr_t alloc_size = size_of_pointers + padding + app.tls.size;
55
56 // We cannot call the mmap function here as the functions set errno on
57 // failure. Since errno is implemented via a thread local variable, we cannot
58 // use errno before TLS is setup.
59 long mmap_ret_val = syscall_impl<long>(number: MMAP_SYSCALL_NUMBER, ts: nullptr,
60 ts: alloc_size, PROT_READ | PROT_WRITE,
61 MAP_ANONYMOUS | MAP_PRIVATE, ts: -1, ts: 0);
62 // We cannot check the return value with MAP_FAILED as that is the return
63 // of the mmap function and not the mmap syscall.
64 if (mmap_ret_val < 0 && static_cast<uintptr_t>(mmap_ret_val) > -app.page_size)
65 syscall_impl<long>(SYS_exit, ts: 1);
66 uintptr_t thread_ptr = uintptr_t(reinterpret_cast<uintptr_t *>(mmap_ret_val));
67 uintptr_t tls_addr = thread_ptr + size_of_pointers + padding;
68 inline_memcpy(dst: reinterpret_cast<char *>(tls_addr),
69 src: reinterpret_cast<const char *>(app.tls.address),
70 count: app.tls.init_size);
71 tls_descriptor.size = alloc_size;
72 tls_descriptor.addr = thread_ptr;
73 tls_descriptor.tp = thread_ptr;
74}
75
76void cleanup_tls(uintptr_t addr, uintptr_t size) {
77 if (size == 0)
78 return;
79 syscall_impl<long>(SYS_munmap, ts: addr, ts: size);
80}
81
82bool set_thread_ptr(uintptr_t val) {
83 __arm_wsr64("tpidr_el0", val);
84 return true;
85}
86} // namespace LIBC_NAMESPACE
87

source code of libc/startup/linux/aarch64/tls.cpp