]>
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@
34 /* The low nine slots of the TSD are reserved for libsyscall usage. */
35 #define __TSD_RESERVED_BASE 0
36 #define __TSD_RESERVED_MAX 9
38 #define __TSD_THREAD_SELF 0
40 #define __TSD_MIG_REPLY 2
41 #define __TSD_MACH_THREAD_SELF 3
42 #define __TSD_THREAD_QOS_CLASS 4
43 #define __TSD_RETURN_TO_KERNEL 5
44 /* slot 6 is reserved for Windows/WINE compatibility reasons */
45 #define __TSD_SEMAPHORE_CACHE 9
51 __attribute__((always_inline
))
52 static __inline__
unsigned int
55 #if defined(__arm__) && defined(_ARM_ARCH_6)
57 __asm__("mrc p15, 0, %[p], c13, c0, 3" : [p
] "=&r" (p
));
58 return (unsigned int)(p
& 0x3ul
);
59 #elif defined(__arm64__)
61 __asm__("mrs %[p], TPIDRRO_EL0" : [p
] "=&r" (p
));
62 return (unsigned int)p
& 0x7;
63 #elif defined(__x86_64__) || defined(__i386__)
64 struct { uintptr_t p1
, p2
; } p
;
65 __asm__("sidt %[p]" : [p
] "=&m" (p
));
66 return (unsigned int)(p
.p1
& 0xfff);
68 #error _os_cpu_number not implemented on this architecture
72 #if defined(__i386__) || defined(__x86_64__)
74 #if defined(__has_attribute)
75 #if __has_attribute(address_space)
76 #define OS_GS_RELATIVE __attribute__((address_space(256)))
81 #define _os_tsd_get_base() ((void * OS_GS_RELATIVE *)0)
83 __attribute__((always_inline
))
84 static __inline__
void*
85 _os_tsd_get_direct(unsigned long slot
)
88 __asm__("mov %%gs:%1, %0" : "=r" (ret
) : "m" (*(void **)(slot
* sizeof(void *))));
92 __attribute__((always_inline
))
94 _os_tsd_set_direct(unsigned long slot
, void *val
)
96 #if defined(__i386__) && defined(__PIC__)
97 __asm__("movl %1, %%gs:%0" : "=m" (*(void **)(slot
* sizeof(void *))) : "rn" (val
));
98 #elif defined(__i386__) && !defined(__PIC__)
99 __asm__("movl %1, %%gs:%0" : "=m" (*(void **)(slot
* sizeof(void *))) : "ri" (val
));
101 __asm__("movq %1, %%gs:%0" : "=m" (*(void **)(slot
* sizeof(void *))) : "rn" (val
));
107 #elif defined(__arm__) || defined(__arm64__)
109 __attribute__((always_inline
, pure
))
110 static __inline__
void**
111 _os_tsd_get_base(void)
113 #if defined(__arm__) && defined(_ARM_ARCH_6)
115 __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r" (tsd
));
116 tsd
&= ~0x3ul
; /* lower 2-bits contain CPU number */
117 #elif defined(__arm__) && defined(_ARM_ARCH_5)
118 register uintptr_t tsd
asm ("r9");
119 #elif defined(__arm64__)
121 __asm__("mrs %0, TPIDRRO_EL0" : "=r" (tsd
));
125 return (void**)(uintptr_t)tsd
;
127 #define _os_tsd_get_base() _os_tsd_get_base()
130 #error _os_tsd_get_base not implemented on this architecture
133 #ifdef _os_tsd_get_base
134 __attribute__((always_inline
))
135 static __inline__
void*
136 _os_tsd_get_direct(unsigned long slot
)
138 return _os_tsd_get_base()[slot
];
141 __attribute__((always_inline
))
142 static __inline__
int
143 _os_tsd_set_direct(unsigned long slot
, void *val
)
145 _os_tsd_get_base()[slot
] = val
;
150 extern void _thread_set_tsd_base(void *tsd_base
);