CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
83 lines
3.2 KiB
C++
83 lines
3.2 KiB
C++
//===-- Implementation of tls for x86_64 ----------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "hdr/sys_mman_macros.h"
|
|
#include "src/__support/OSUtil/linux/syscall_wrappers/mmap.h"
|
|
#include "src/__support/OSUtil/linux/syscall_wrappers/munmap.h"
|
|
#include "src/__support/macros/config.h"
|
|
#include "src/string/memory_utils/inline_memcpy.h"
|
|
#include "startup/linux/do_start.h"
|
|
|
|
#include <asm/prctl.h>
|
|
#include <sys/syscall.h>
|
|
|
|
namespace LIBC_NAMESPACE_DECL {
|
|
|
|
// TODO: Also generalize this routine and handle dynamic loading properly.
|
|
void init_tls(TLSDescriptor &tls_descriptor) {
|
|
if (app.tls.size == 0) {
|
|
tls_descriptor.size = 0;
|
|
tls_descriptor.tp = 0;
|
|
return;
|
|
}
|
|
|
|
// We will assume the alignment is always a power of two.
|
|
uintptr_t tls_size = app.tls.size & -app.tls.align;
|
|
if (tls_size != app.tls.size)
|
|
tls_size += app.tls.align;
|
|
|
|
// Per the x86_64 TLS ABI, the entry pointed to by the thread pointer is the
|
|
// address of the TLS block. So, we add more size to accomodate this address
|
|
// entry.
|
|
// We also need to include space for the stack canary. The canary is at
|
|
// offset 0x28 (40) and is of size uintptr_t.
|
|
uintptr_t tls_size_with_addr = tls_size + sizeof(uintptr_t) + 40;
|
|
|
|
ErrorOr<void *> mmap_ret =
|
|
linux_syscalls::mmap(nullptr, tls_size_with_addr, PROT_READ | PROT_WRITE,
|
|
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
|
if (!mmap_ret.has_value())
|
|
syscall_impl<long>(SYS_exit, 1);
|
|
uintptr_t *tls_addr = static_cast<uintptr_t *>(mmap_ret.value());
|
|
|
|
// x86_64 TLS faces down from the thread pointer with the first entry
|
|
// pointing to the address of the first real TLS byte.
|
|
uintptr_t end_ptr = reinterpret_cast<uintptr_t>(tls_addr) + tls_size;
|
|
*reinterpret_cast<uintptr_t *>(end_ptr) = end_ptr;
|
|
|
|
inline_memcpy(reinterpret_cast<char *>(tls_addr),
|
|
reinterpret_cast<const char *>(app.tls.address),
|
|
app.tls.init_size);
|
|
uintptr_t *stack_guard_addr = reinterpret_cast<uintptr_t *>(end_ptr + 40);
|
|
// Setting the stack guard to a random value.
|
|
// We cannot call the get_random function here as the function sets errno on
|
|
// failure. Since errno is implemented via a thread local variable, we cannot
|
|
// use errno before TLS is setup.
|
|
long stack_guard_retval =
|
|
syscall_impl(SYS_getrandom, reinterpret_cast<long>(stack_guard_addr),
|
|
sizeof(uint64_t), 0);
|
|
if (stack_guard_retval < 0)
|
|
syscall_impl(SYS_exit, 1);
|
|
|
|
tls_descriptor = {tls_size_with_addr, reinterpret_cast<uintptr_t>(tls_addr),
|
|
end_ptr};
|
|
return;
|
|
}
|
|
|
|
void cleanup_tls(uintptr_t addr, uintptr_t size) {
|
|
if (size == 0)
|
|
return;
|
|
linux_syscalls::munmap(reinterpret_cast<void *>(addr), size);
|
|
}
|
|
|
|
// Sets the thread pointer to |val|. Returns true on success, false on failure.
|
|
bool set_thread_ptr(uintptr_t val) {
|
|
return syscall_impl(SYS_arch_prctl, ARCH_SET_FS, val) != -1;
|
|
}
|
|
} // namespace LIBC_NAMESPACE_DECL
|