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 | |
| 33 | // TODO: move the __get_tls() macros to functions instead. |
| 34 | |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 35 | #if defined(__aarch64__) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 36 | |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 37 | # define __get_tls() ({ void** __val; __asm__("mrs %0, tpidr_el0" : "=r"(__val)); __val; }) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 38 | |
| 39 | static inline void __set_tls(void* tls) { |
| 40 | __asm__("msr tpidr_el0, %0" : : "r" (tls)); |
| 41 | } |
| 42 | |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 43 | #elif defined(__arm__) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 44 | |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 45 | # define __get_tls() ({ void** __val; __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(__val)); __val; }) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 46 | |
| 47 | // arm32 requires a syscall. |
| 48 | // By historical accident it's public API, but not in any header except this one. |
| 49 | __BEGIN_DECLS |
| 50 | int __set_tls(void* tls); |
| 51 | __END_DECLS |
| 52 | |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 53 | #elif defined(__i386__) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 54 | |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 55 | # define __get_tls() ({ void** __val; __asm__("movl %%gs:0, %0" : "=r"(__val)); __val; }) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 56 | |
| 57 | // x86 is really hairy, so we keep that out of line. |
| 58 | __BEGIN_DECLS |
| 59 | int __set_tls(void* tls); |
| 60 | __END_DECLS |
| 61 | |
Elliott Hughes | fc009dd | 2022-10-07 21:31:36 +0000 | [diff] [blame] | 62 | #elif defined(__riscv) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 63 | |
Elliott Hughes | fc009dd | 2022-10-07 21:31:36 +0000 | [diff] [blame] | 64 | # define __get_tls() ({ void** __val; __asm__("mv %0, tp" : "=r"(__val)); __val; }) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 65 | |
| 66 | static inline void __set_tls(void* tls) { |
| 67 | __asm__("mv tp, %0" : : "r"(tls)); |
| 68 | } |
| 69 | |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 70 | #elif defined(__x86_64__) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 71 | |
Serban Constantinescu | e210488 | 2013-09-26 11:37:10 +0100 | [diff] [blame] | 72 | # define __get_tls() ({ void** __val; __asm__("mov %%fs:0, %0" : "=r"(__val)); __val; }) |
Elliott Hughes | e286cf6 | 2025-02-27 12:38:19 -0800 | [diff] [blame] | 73 | |
| 74 | // ARCH_SET_FS is not exposed via <sys/prctl.h> or <linux/prctl.h>. |
| 75 | #include <asm/prctl.h> |
| 76 | // This syscall stub is generated but it's not declared in any header. |
| 77 | __BEGIN_DECLS |
| 78 | int arch_prctl(int, unsigned long); |
| 79 | __END_DECLS |
| 80 | |
| 81 | static inline int __set_tls(void* tls) { |
| 82 | return arch_prctl(ARCH_SET_FS, reinterpret_cast<unsigned long>(tls)); |
| 83 | } |
| 84 | |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 85 | #else |
| 86 | #error unsupported architecture |
| 87 | #endif |
| 88 | |
Christopher Ferris | c5d3a43 | 2019-09-25 17:50:36 -0700 | [diff] [blame] | 89 | #include "tls_defines.h" |