blob: 9a79f849353e608c73d0a6463f7b58b69b47c466 [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
33// TODO: move the __get_tls() macros to functions instead.
34
Serban Constantinescue2104882013-09-26 11:37:10 +010035#if defined(__aarch64__)
Elliott Hughese286cf62025-02-27 12:38:19 -080036
Serban Constantinescue2104882013-09-26 11:37:10 +010037# define __get_tls() ({ void** __val; __asm__("mrs %0, tpidr_el0" : "=r"(__val)); __val; })
Elliott Hughese286cf62025-02-27 12:38:19 -080038
39static inline void __set_tls(void* tls) {
40 __asm__("msr tpidr_el0, %0" : : "r" (tls));
41}
42
Serban Constantinescue2104882013-09-26 11:37:10 +010043#elif defined(__arm__)
Elliott Hughese286cf62025-02-27 12:38:19 -080044
Serban Constantinescue2104882013-09-26 11:37:10 +010045# define __get_tls() ({ void** __val; __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(__val)); __val; })
Elliott Hughese286cf62025-02-27 12:38:19 -080046
47// arm32 requires a syscall.
48// By historical accident it's public API, but not in any header except this one.
49__BEGIN_DECLS
50int __set_tls(void* tls);
51__END_DECLS
52
Elliott Hughes2a0b8732013-10-08 18:50:24 -070053#elif defined(__i386__)
Elliott Hughese286cf62025-02-27 12:38:19 -080054
Serban Constantinescue2104882013-09-26 11:37:10 +010055# define __get_tls() ({ void** __val; __asm__("movl %%gs:0, %0" : "=r"(__val)); __val; })
Elliott Hughese286cf62025-02-27 12:38:19 -080056
57// x86 is really hairy, so we keep that out of line.
58__BEGIN_DECLS
59int __set_tls(void* tls);
60__END_DECLS
61
Elliott Hughesfc009dd2022-10-07 21:31:36 +000062#elif defined(__riscv)
Elliott Hughese286cf62025-02-27 12:38:19 -080063
Elliott Hughesfc009dd2022-10-07 21:31:36 +000064# define __get_tls() ({ void** __val; __asm__("mv %0, tp" : "=r"(__val)); __val; })
Elliott Hughese286cf62025-02-27 12:38:19 -080065
66static inline void __set_tls(void* tls) {
67 __asm__("mv tp, %0" : : "r"(tls));
68}
69
Elliott Hughes2a0b8732013-10-08 18:50:24 -070070#elif defined(__x86_64__)
Elliott Hughese286cf62025-02-27 12:38:19 -080071
Serban Constantinescue2104882013-09-26 11:37:10 +010072# define __get_tls() ({ void** __val; __asm__("mov %%fs:0, %0" : "=r"(__val)); __val; })
Elliott Hughese286cf62025-02-27 12:38:19 -080073
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
78int arch_prctl(int, unsigned long);
79__END_DECLS
80
81static inline int __set_tls(void* tls) {
82 return arch_prctl(ARCH_SET_FS, reinterpret_cast<unsigned long>(tls));
83}
84
Elliott Hughes2a0b8732013-10-08 18:50:24 -070085#else
86#error unsupported architecture
87#endif
88
Christopher Ferrisc5d3a432019-09-25 17:50:36 -070089#include "tls_defines.h"