brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · e9d23c5 Raw
76 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 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"14 15#include <sys/mman.h>16#include <sys/syscall.h>17 18namespace LIBC_NAMESPACE_DECL {19 20#ifdef SYS_mmap221static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap2;22#elif SYS_mmap23static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap;24#else25#error "mmap and mmap2 syscalls not available."26#endif27 28void init_tls(TLSDescriptor &tls_descriptor) {29  if (app.tls.size == 0) {30    tls_descriptor.size = 0;31    tls_descriptor.tp = 0;32    return;33  }34 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;42 43  uintptr_t alloc_size = size_of_pointers + padding + app.tls.size;44 45  // We cannot call the mmap function here as the functions set errno on46  // failure. Since errno is implemented via a thread local variable, we cannot47  // 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 return52  // of the mmap function and not the mmap syscall.53  if (!linux_utils::is_valid_mmap(mmap_ret_val))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;63}64 65void cleanup_tls(uintptr_t addr, uintptr_t size) {66  if (size == 0)67    return;68  syscall_impl<long>(SYS_munmap, addr, size);69}70 71bool set_thread_ptr(uintptr_t val) {72  LIBC_INLINE_ASM("mv tp, %0\n\t" : : "r"(val));73  return true;74}75} // namespace LIBC_NAMESPACE_DECL76