• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 #pragma once
30 
31 #include <sys/cdefs.h>
32 
33 #if defined(__aarch64__)
34 
__get_tls(void)35 static inline void** __get_tls(void) {
36   void** result;
37   __asm__("mrs %0, tpidr_el0" : "=r"(result));
38   return result;
39 }
40 
__set_tls(void * tls)41 static inline void __set_tls(void* tls) {
42   __asm__("msr tpidr_el0, %0" : : "r" (tls));
43 }
44 
45 #elif defined(__arm__)
46 
__get_tls(void)47 static inline void** __get_tls(void) {
48   void** result;
49   __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(result));
50   return result;
51 }
52 
53 // arm32 requires a syscall to set the thread pointer.
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 
59 #elif defined(__i386__)
60 
61 static inline void** __get_tls(void) {
62   void** result;
63   __asm__("movl %%gs:0, %0" : "=r"(result));
64   return result;
65 }
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 
72 #elif defined(__riscv)
73 
74 static inline void** __get_tls(void) {
75   void** result;
76   __asm__("mv %0, tp" : "=r"(result));
77   return result;
78 }
79 
80 static inline void __set_tls(void* tls) {
81   __asm__("mv tp, %0" : : "r"(tls));
82 }
83 
84 #elif defined(__x86_64__)
85 
86 static inline void** __get_tls(void) {
87   void** result;
88   __asm__("mov %%fs:0, %0" : "=r"(result));
89   return result;
90 }
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 
103 #else
104 #error unsupported architecture
105 #endif
106 
107 #include "tls_defines.h"
108