]>
git.saurik.com Git - apple/xnu.git/blob - libsyscall/os/tsd.h
2 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
36 #define __TSD_THREAD_SELF 0
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
55 extern void _thread_set_tsd_base(void *tsd_base
);
57 __attribute__((always_inline
))
58 static __inline__
unsigned int
61 #if defined(__arm__) && defined(_ARM_ARCH_6)
63 __asm__("mrc p15, 0, %[p], c13, c0, 3" : [p
] "=&r" (p
));
64 return (unsigned int)(p
& 0x3ul
);
65 #elif defined(__arm64__)
67 __asm__("mrs %[p], TPIDRRO_EL0" : [p
] "=&r" (p
));
68 return (unsigned int)p
& 0x7;
69 #elif defined(__x86_64__) || defined(__i386__)
70 struct { uintptr_t p1
, p2
; } p
;
71 __asm__("sidt %[p]" : [p
] "=&m" (p
));
72 return (unsigned int)(p
.p1
& 0xfff);
74 #error _os_cpu_number not implemented on this architecture
78 #if defined(__i386__) || defined(__x86_64__)
80 #if defined(__has_attribute)
81 #if __has_attribute(address_space)
82 #define OS_GS_RELATIVE __attribute__((address_space(256)))
87 #define _os_tsd_get_base() ((void * OS_GS_RELATIVE *)0)
89 __attribute__((always_inline
))
90 static __inline__
void*
91 _os_tsd_get_direct(unsigned long slot
)
94 __asm__("mov %%gs:%1, %0" : "=r" (ret
) : "m" (*(void **)(slot
* sizeof(void *))));
98 __attribute__((always_inline
))
100 _os_tsd_set_direct(unsigned long slot
, void *val
)
102 #if defined(__i386__) && defined(__PIC__)
103 __asm__("movl %1, %%gs:%0" : "=m" (*(void **)(slot
* sizeof(void *))) : "rn" (val
));
104 #elif defined(__i386__) && !defined(__PIC__)
105 __asm__("movl %1, %%gs:%0" : "=m" (*(void **)(slot
* sizeof(void *))) : "ri" (val
));
107 __asm__("movq %1, %%gs:%0" : "=m" (*(void **)(slot
* sizeof(void *))) : "rn" (val
));
113 #elif defined(__arm__) || defined(__arm64__)
115 __attribute__((always_inline
, pure
))
116 static __inline__
void**
117 _os_tsd_get_base(void)
119 #if defined(__arm__) && defined(_ARM_ARCH_6)
121 __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r" (tsd
));
122 tsd
&= ~0x3ul
; /* lower 2-bits contain CPU number */
123 #elif defined(__arm__) && defined(_ARM_ARCH_5)
124 register uintptr_t tsd
asm ("r9");
125 #elif defined(__arm64__)
127 __asm__("mrs %0, TPIDRRO_EL0" : "=r" (tsd
));
131 return (void**)(uintptr_t)tsd
;
133 #define _os_tsd_get_base() _os_tsd_get_base()
136 #error _os_tsd_get_base not implemented on this architecture
139 #ifdef _os_tsd_get_base
140 __attribute__((always_inline
))
141 static __inline__
void*
142 _os_tsd_get_direct(unsigned long slot
)
144 return _os_tsd_get_base()[slot
];
147 __attribute__((always_inline
))
148 static __inline__
int
149 _os_tsd_set_direct(unsigned long slot
, void *val
)
151 _os_tsd_get_base()[slot
] = val
;
156 __attribute__((always_inline
, pure
))
157 static __inline__
uintptr_t
158 _os_ptr_munge_token(void)
160 return (uintptr_t)_os_tsd_get_direct(__TSD_PTR_MUNGE
);
163 __attribute__((always_inline
, pure
))
164 static __inline__
uintptr_t
165 _os_ptr_munge(uintptr_t ptr
)
167 return ptr
^ _os_ptr_munge_token();
169 #define _OS_PTR_MUNGE(_ptr) _os_ptr_munge((uintptr_t)(_ptr))
170 #define _OS_PTR_UNMUNGE(_ptr) _os_ptr_munge((uintptr_t)(_ptr))
172 #else // __ASSEMBLER__
174 #define _OS_TSD_OFFSET(_key) \
175 ((__POINTER_WIDTH__/__CHAR_BIT__)*_key)
177 #if defined(__i386__) || defined(__x86_64__)
179 #define _OS_PTR_MUNGE(_reg) \
180 xor %gs:_OS_TSD_OFFSET(__TSD_PTR_MUNGE), _reg
182 #define _OS_PTR_UNMUNGE(_reg) \
185 #elif defined(__arm__) || defined(__arm64__)
189 #define _OS_PTR_MUNGE_TOKEN(_reg, _token) \
190 mrc p15, 0, _reg, c13, c0, 3; \
191 bic _reg, _reg, #3; \
192 ldr _token, [ _reg, #_OS_TSD_OFFSET(__TSD_PTR_MUNGE) ]
194 #elif defined(__arm64__)
196 #define _OS_PTR_MUNGE_TOKEN(_reg, _token) \
197 mrs _reg, TPIDRRO_EL0 %% \
198 and _reg, _reg, #~0x7 %% \
199 ldr _token, [ _reg, #_OS_TSD_OFFSET(__TSD_PTR_MUNGE) ]
201 #endif // defined(__arm64__)
203 #define _OS_PTR_MUNGE(_regdest, _regsrc, _token) \
204 eor _regdest, _regsrc, _token
206 #define _OS_PTR_UNMUNGE(_regdest, _regsrc, _token) \
207 _OS_PTR_MUNGE(_regdest, _regsrc, _token)
209 #endif // defined(__arm__) || defined(__arm64__)
211 #endif // __ASSEMBLER__