]> git.saurik.com Git - apple/xnu.git/blame - libsyscall/os/tsd.h
xnu-3789.41.3.tar.gz
[apple/xnu.git] / libsyscall / os / tsd.h
CommitLineData
2d21ac55 1/*
39236c6e 2 * Copyright (c) 2012 Apple Inc. All rights reserved.
2d21ac55
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
2d21ac55
A
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.
39236c6e 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39236c6e 17 *
2d21ac55
A
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.
39236c6e 25 *
2d21ac55
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
39236c6e
A
28
29#ifndef OS_TSD_H
30#define OS_TSD_H
31
32#include <stdint.h>
33
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
37
38#define __TSD_THREAD_SELF 0
39#define __TSD_ERRNO 1
40#define __TSD_MIG_REPLY 2
41#define __TSD_SEMAPHORE_CACHE 9
42
43
44__attribute__((always_inline))
45static __inline__ unsigned int
46_os_cpu_number(void)
47{
48 /* Not yet implemented */
49 return 0;
50}
51
39037602
A
52#if defined(__i386__) || defined(__x86_64__)
53
54#if defined(__has_attribute)
55#if __has_attribute(address_space)
56#define OS_GS_RELATIVE __attribute__((address_space(256)))
57#endif
58#endif
59
60#ifdef OS_GS_RELATIVE
61#define _os_tsd_get_base() ((void * OS_GS_RELATIVE *)0)
62#else
39236c6e
A
63__attribute__((always_inline))
64static __inline__ void*
65_os_tsd_get_direct(unsigned long slot)
66{
67 void *ret;
39236c6e 68 __asm__("mov %%gs:%1, %0" : "=r" (ret) : "m" (*(void **)(slot * sizeof(void *))));
39236c6e
A
69 return ret;
70}
71
72__attribute__((always_inline))
73static __inline__ int
39037602 74_os_tsd_set_direct(unsigned long slot, void *val)
39236c6e
A
75{
76#if defined(__i386__) && defined(__PIC__)
77 __asm__("movl %1, %%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "rn" (val));
78#elif defined(__i386__) && !defined(__PIC__)
79 __asm__("movl %1, %%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "ri" (val));
39037602 80#else
39236c6e 81 __asm__("movq %1, %%gs:%0" : "=m" (*(void **)(slot * sizeof(void *))) : "rn" (val));
39037602
A
82#endif
83 return 0;
84}
39236c6e
A
85#endif
86
39037602
A
87#else
88#error _os_tsd_get_base not implemented on this architecture
89#endif
90
91#ifdef _os_tsd_get_base
92__attribute__((always_inline))
93static __inline__ void*
94_os_tsd_get_direct(unsigned long slot)
95{
96 return _os_tsd_get_base()[slot];
97}
98
99__attribute__((always_inline))
100static __inline__ int
101_os_tsd_set_direct(unsigned long slot, void *val)
102{
103 _os_tsd_get_base()[slot] = val;
39236c6e
A
104 return 0;
105}
39037602
A
106#endif
107
108extern void _thread_set_tsd_base(void *tsd_base);
39236c6e
A
109
110#endif