xref: /xnu-8020.121.3/libsyscall/os/tsd.h (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1 /*
2  * Copyright (c) 2012 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef OS_TSD_H
30 #define OS_TSD_H
31 
32 /* The low nine slots of the TSD are reserved for libsyscall usage. */
33 #define __TSD_RESERVED_BASE 0
34 #define __TSD_RESERVED_MAX 9
35 
36 #define __TSD_THREAD_SELF 0
37 #define __TSD_ERRNO 1
38 #define __TSD_MIG_REPLY 2
39 #define __TSD_MACH_THREAD_SELF 3
40 #define __TSD_THREAD_QOS_CLASS 4
41 #define __TSD_RETURN_TO_KERNEL 5
42 /* slot 6 is reserved for Windows/WINE compatibility reasons */
43 #define __TSD_PTR_MUNGE 7
44 #define __TSD_MACH_SPECIAL_REPLY 8
45 #define __TSD_SEMAPHORE_CACHE 9
46 
47 
48 #define __TPIDR_CPU_NUM_MASK 0x0000000000000fff
49 #define __TPIDR_CPU_NUM_SHIFT 0
50 
51 #ifndef __ASSEMBLER__
52 
53 #include <stdint.h>
54 #include <TargetConditionals.h>
55 
56 #ifdef __arm__
57 #include <arm/arch.h>
58 #endif
59 
60 extern void _thread_set_tsd_base(void *tsd_base);
61 
62 /*
63  * The implementation details of this function are not ABI and are subject to change,
64  * do not copy them in another project
65  */
66 __attribute__((always_inline))
67 static __inline__ unsigned int
_os_cpu_number(void)68 _os_cpu_number(void)
69 {
70 #if defined(__arm__)
71 	uintptr_t p;
72 	__asm__ __volatile__ ("mrc	p15, 0, %[p], c13, c0, 3" : [p] "=&r" (p));
73 	return (unsigned int)(p & 0x3ul);
74 #elif defined(__arm64__)
75 	uint64_t p;
76 	__asm__ __volatile__ ("mrs %0, TPIDR_EL0" : "=r" (p));
77 	return (p & __TPIDR_CPU_NUM_MASK) >> __TPIDR_CPU_NUM_SHIFT;
78 #elif defined(__x86_64__) || defined(__i386__)
79 	struct { uintptr_t p1, p2; } p;
80 	__asm__ __volatile__ ("sidt %[p]" : [p] "=&m" (p));
81 	return (unsigned int)(p.p1 & 0xfff);
82 #else
83 #error _os_cpu_number not implemented on this architecture
84 #endif
85 }
86 
87 #if defined(__i386__) || defined(__x86_64__)
88 
89 #if defined(__has_attribute)
90 #if __has_attribute(address_space)
91 #define OS_GS_RELATIVE  __attribute__((address_space(256)))
92 #endif
93 #endif
94 
95 #ifdef OS_GS_RELATIVE
96 #define _os_tsd_get_base() ((void * OS_GS_RELATIVE *)0)
97 #else
98 __attribute__((always_inline))
99 static __inline__ void*
_os_tsd_get_direct(unsigned long slot)100 _os_tsd_get_direct(unsigned long slot)
101 {
102 	void *ret;
103 	__asm__("mov %%gs:%1, %0" : "=r" (ret) : "m" (*(void **)(slot * sizeof(void *))));
104 	return ret;
105 }
106 
107 __attribute__((always_inline))
108 static __inline__ int
_os_tsd_set_direct(unsigned long slot,void * val)109 _os_tsd_set_direct(unsigned long slot, void *val)
110 {
111 #if defined(__i386__) && defined(__PIC__)
112 	__asm__("movl %1, %%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "rn" (val));
113 #elif defined(__i386__) && !defined(__PIC__)
114 	__asm__("movl %1, %%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "ri" (val));
115 #else
116 	__asm__("movq %1, %%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "rn" (val));
117 #endif
118 	return 0;
119 }
120 #endif
121 
122 #elif defined(__arm__) || defined(__arm64__)
123 
124 __attribute__((always_inline, const))
125 static __inline__ void**
_os_tsd_get_base(void)126 _os_tsd_get_base(void)
127 {
128 #if defined(__arm__)
129 	uintptr_t tsd;
130 	__asm__("mrc p15, 0, %0, c13, c0, 3\n"
131                 "bic %0, %0, #0x3\n" : "=r" (tsd));
132 	/* lower 2-bits contain CPU number */
133 #elif defined(__arm64__)
134 	/*
135 	 * <rdar://73762648> Do not use __builtin_arm_rsr64("TPIDRRO_EL0")
136 	 * so that the "const" attribute takes effect and repeated use
137 	 * is coalesced properly.
138 	 */
139 	uint64_t tsd;
140 	__asm__ ("mrs %0, TPIDRRO_EL0" : "=r" (tsd));
141 #endif
142 
143 	return (void**)(uintptr_t)tsd;
144 }
145 #define _os_tsd_get_base()  _os_tsd_get_base()
146 
147 #else
148 #error _os_tsd_get_base not implemented on this architecture
149 #endif
150 
151 #ifdef _os_tsd_get_base
152 __attribute__((always_inline))
153 static __inline__ void*
_os_tsd_get_direct(unsigned long slot)154 _os_tsd_get_direct(unsigned long slot)
155 {
156 	return _os_tsd_get_base()[slot];
157 }
158 
159 __attribute__((always_inline))
160 static __inline__ int
_os_tsd_set_direct(unsigned long slot,void * val)161 _os_tsd_set_direct(unsigned long slot, void *val)
162 {
163 	_os_tsd_get_base()[slot] = val;
164 	return 0;
165 }
166 #endif
167 
168 __attribute__((always_inline, const))
169 static __inline__ uintptr_t
_os_ptr_munge_token(void)170 _os_ptr_munge_token(void)
171 {
172 	return (uintptr_t)_os_tsd_get_direct(__TSD_PTR_MUNGE);
173 }
174 
175 __attribute__((always_inline, const))
176 static __inline__ uintptr_t
_os_ptr_munge(uintptr_t ptr)177 _os_ptr_munge(uintptr_t ptr)
178 {
179 	return ptr ^ _os_ptr_munge_token();
180 }
181 #define _OS_PTR_MUNGE(_ptr) _os_ptr_munge((uintptr_t)(_ptr))
182 #define _OS_PTR_UNMUNGE(_ptr) _os_ptr_munge((uintptr_t)(_ptr))
183 
184 #else // __ASSEMBLER__
185 
186 #define _OS_TSD_OFFSET(_key) \
187 	((__POINTER_WIDTH__/__CHAR_BIT__)*_key)
188 
189 #if defined(__i386__) || defined(__x86_64__)
190 
191 #define _OS_PTR_MUNGE(_reg) \
192 	xor %gs:_OS_TSD_OFFSET(__TSD_PTR_MUNGE), _reg
193 
194 #define _OS_PTR_UNMUNGE(_reg) \
195 	_OS_PTR_MUNGE(_reg)
196 
197 #elif defined(__arm__) || defined(__arm64__)
198 
199 #if defined(__arm__)
200 
201 #define _OS_PTR_MUNGE_TOKEN(_reg, _token) \
202 	mrc p15, 0, _reg, c13, c0, 3; \
203 	bic	_reg, _reg, #3; \
204 	ldr	_token, [ _reg,  #_OS_TSD_OFFSET(__TSD_PTR_MUNGE) ]
205 
206 #elif defined(__arm64__)
207 
208 #define _OS_PTR_MUNGE_TOKEN(_reg, _token) \
209 	mrs _reg, TPIDRRO_EL0 %% \
210 	ldr	_token, [ _reg,  #_OS_TSD_OFFSET(__TSD_PTR_MUNGE) ]
211 
212 #endif // defined(__arm64__)
213 
214 #define _OS_PTR_MUNGE(_regdest, _regsrc, _token) \
215 	eor _regdest, _regsrc, _token
216 
217 #define _OS_PTR_UNMUNGE(_regdest, _regsrc, _token) \
218 	_OS_PTR_MUNGE(_regdest, _regsrc, _token)
219 
220 #endif // defined(__arm__) || defined(__arm64__)
221 
222 #endif // __ASSEMBLER__
223 
224 #endif // OS_TSD_H
225