blob: 3e0ef53c652c2f8ee4e7a0feb162b7b95ce4e87e [file] [log] [blame]
Elliott Hughes2a0b8732013-10-08 18:50:24 -07001/*
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 Ferrisc5d3a432019-09-25 17:50:36 -070029#pragma once
Elliott Hughes2a0b8732013-10-08 18:50:24 -070030
Elliott Hughese286cf62025-02-27 12:38:19 -080031#include <sys/cdefs.h>
32
Serban Constantinescue2104882013-09-26 11:37:10 +010033#if defined(__aarch64__)
Elliott Hughese286cf62025-02-27 12:38:19 -080034
Elliott Hughes17283272025-03-11 11:44:14 -070035static inline void** __get_tls(void) {
Elliott Hughes70a9c5f2025-03-03 12:57:43 -080036 void** result;
37 __asm__("mrs %0, tpidr_el0" : "=r"(result));
38 return result;
39}
Elliott Hughese286cf62025-02-27 12:38:19 -080040
41static inline void __set_tls(void* tls) {
42 __asm__("msr tpidr_el0, %0" : : "r" (tls));
43}
44
Serban Constantinescue2104882013-09-26 11:37:10 +010045#elif defined(__arm__)
Elliott Hughese286cf62025-02-27 12:38:19 -080046
Elliott Hughes17283272025-03-11 11:44:14 -070047static inline void** __get_tls(void) {
Elliott Hughes70a9c5f2025-03-03 12:57:43 -080048 void** result;
49 __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(result));
50 return result;
51}
Elliott Hughese286cf62025-02-27 12:38:19 -080052
Elliott Hughes70a9c5f2025-03-03 12:57:43 -080053// arm32 requires a syscall to set the thread pointer.
Elliott Hughese286cf62025-02-27 12:38:19 -080054// By historical accident it's public API, but not in any header except this one.
55__BEGIN_DECLS
56int __set_tls(void* tls);
57__END_DECLS
58
Elliott Hughes2a0b8732013-10-08 18:50:24 -070059#elif defined(__i386__)
Elliott Hughese286cf62025-02-27 12:38:19 -080060
Elliott Hughes17283272025-03-11 11:44:14 -070061static inline void** __get_tls(void) {
Elliott Hughes70a9c5f2025-03-03 12:57:43 -080062 void** result;
63 __asm__("movl %%gs:0, %0" : "=r"(result));
64 return result;
65}
Elliott Hughese286cf62025-02-27 12:38:19 -080066
67// x86 is really hairy, so we keep that out of line.
68__BEGIN_DECLS
69int __set_tls(void* tls);
70__END_DECLS
71
Elliott Hughesfc009dd2022-10-07 21:31:36 +000072#elif defined(__riscv)
Elliott Hughese286cf62025-02-27 12:38:19 -080073
Elliott Hughes17283272025-03-11 11:44:14 -070074static inline void** __get_tls(void) {
Elliott Hughes70a9c5f2025-03-03 12:57:43 -080075 void** result;
76 __asm__("mv %0, tp" : "=r"(result));
77 return result;
78}
Elliott Hughese286cf62025-02-27 12:38:19 -080079
80static inline void __set_tls(void* tls) {
81 __asm__("mv tp, %0" : : "r"(tls));
82}
83
Elliott Hughes2a0b8732013-10-08 18:50:24 -070084#elif defined(__x86_64__)
Elliott Hughese286cf62025-02-27 12:38:19 -080085
Elliott Hughes17283272025-03-11 11:44:14 -070086static inline void** __get_tls(void) {
Elliott Hughes70a9c5f2025-03-03 12:57:43 -080087 void** result;
88 __asm__("mov %%fs:0, %0" : "=r"(result));
89 return result;
90}
Elliott Hughese286cf62025-02-27 12:38:19 -080091
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
96int arch_prctl(int, unsigned long);
97__END_DECLS
98
99static inline int __set_tls(void* tls) {
100 return arch_prctl(ARCH_SET_FS, reinterpret_cast<unsigned long>(tls));
101}
102
Elliott Hughes2a0b8732013-10-08 18:50:24 -0700103#else
104#error unsupported architecture
105#endif
106
Christopher Ferrisc5d3a432019-09-25 17:50:36 -0700107#include "tls_defines.h"