Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Christopher Ferris | c5d3a43 | 2019-09-25 17:50:36 -0700 | [diff] [blame] | 29 | #pragma once |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 31 | #include <sys/cdefs.h> |
| 32 | |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 33 | #if defined(__aarch64__) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 34 | |
Elliott Hughes | 1728327 | 2025-03-11 11:44:14 -0700 | [diff] [blame] | 35 | static inline void** __get_tls(void) { |
Elliott Hughes | 70a9c5f | 2025-03-03 12:57:43 -0800 | [diff] [blame] | 36 | void** result; |
| 37 | __asm__("mrs %0, tpidr_el0" : "=r"(result)); |
| 38 | return result; |
| 39 | } |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 40 | |
| 41 | static inline void __set_tls(void* tls) { |
| 42 | __asm__("msr tpidr_el0, %0" : : "r" (tls)); |
| 43 | } |
| 44 | |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 45 | #elif defined(__arm__) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 46 | |
Elliott Hughes | 1728327 | 2025-03-11 11:44:14 -0700 | [diff] [blame] | 47 | static inline void** __get_tls(void) { |
Elliott Hughes | 70a9c5f | 2025-03-03 12:57:43 -0800 | [diff] [blame] | 48 | void** result; |
| 49 | __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(result)); |
| 50 | return result; |
| 51 | } |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 52 | |
Elliott Hughes | 70a9c5f | 2025-03-03 12:57:43 -0800 | [diff] [blame] | 53 | // arm32 requires a syscall to set the thread pointer. |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 54 | // By historical accident it's public API, but not in any header except this one. |
| 55 | __BEGIN_DECLS |
| 56 | int __set_tls(void* tls); |
| 57 | __END_DECLS |
| 58 | |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 59 | #elif defined(__i386__) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 60 | |
Elliott Hughes | 1728327 | 2025-03-11 11:44:14 -0700 | [diff] [blame] | 61 | static inline void** __get_tls(void) { |
Elliott Hughes | 70a9c5f | 2025-03-03 12:57:43 -0800 | [diff] [blame] | 62 | void** result; |
| 63 | __asm__("movl %%gs:0, %0" : "=r"(result)); |
| 64 | return result; |
| 65 | } |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 66 | |
| 67 | // x86 is really hairy, so we keep that out of line. |
| 68 | __BEGIN_DECLS |
| 69 | int __set_tls(void* tls); |
| 70 | __END_DECLS |
| 71 | |
Elliott Hughes | fc009dd | 2022-10-07 21:31:36 +0000 | [diff] [blame] | 72 | #elif defined(__riscv) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 73 | |
Elliott Hughes | 1728327 | 2025-03-11 11:44:14 -0700 | [diff] [blame] | 74 | static inline void** __get_tls(void) { |
Elliott Hughes | 70a9c5f | 2025-03-03 12:57:43 -0800 | [diff] [blame] | 75 | void** result; |
| 76 | __asm__("mv %0, tp" : "=r"(result)); |
| 77 | return result; |
| 78 | } |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 79 | |
| 80 | static inline void __set_tls(void* tls) { |
| 81 | __asm__("mv tp, %0" : : "r"(tls)); |
| 82 | } |
| 83 | |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 84 | #elif defined(__x86_64__) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 85 | |
Elliott Hughes | 1728327 | 2025-03-11 11:44:14 -0700 | [diff] [blame] | 86 | static inline void** __get_tls(void) { |
Elliott Hughes | 70a9c5f | 2025-03-03 12:57:43 -0800 | [diff] [blame] | 87 | void** result; |
| 88 | __asm__("mov %%fs:0, %0" : "=r"(result)); |
| 89 | return result; |
| 90 | } |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 91 | |
| 92 | // ARCH_SET_FS is not exposed via <sys/prctl.h> or <linux/prctl.h>. |
| 93 | #include <asm/prctl.h> |
| 94 | // This syscall stub is generated but it's not declared in any header. |
| 95 | __BEGIN_DECLS |
| 96 | int arch_prctl(int, unsigned long); |
| 97 | __END_DECLS |
| 98 | |
| 99 | static inline int __set_tls(void* tls) { |
| 100 | return arch_prctl(ARCH_SET_FS, reinterpret_cast<unsigned long>(tls)); |
| 101 | } |
| 102 | |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 103 | #else |
| 104 | #error unsupported architecture |
| 105 | #endif |
| 106 | |
Christopher Ferris | c5d3a43 | 2019-09-25 17:50:36 -0700 | [diff] [blame] | 107 | #include "tls_defines.h" |