]> git.saurik.com Git - apple/libplatform.git/blob - internal/os/internal.h
4d7a083edf6d887549fc893479237b63c951360d
[apple/libplatform.git] / internal / os / internal.h
1 /*
2 * Copyright (c) 2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21 #ifndef __OS_INTERNAL_H__
22 #define __OS_INTERNAL_H__
23
24 #define __OS_ALLOC_INDIRECT__
25
26 #include <TargetConditionals.h>
27 #include <machine/cpu_capabilities.h>
28
29 #include "os/base_private.h"
30 #include "os/semaphore_private.h"
31
32 #include <stddef.h>
33 #include <stdint.h>
34 #include <stdbool.h>
35 #include <limits.h>
36 #if defined(__arm__)
37 #include <arm/arch.h>
38 #endif
39 #include <mach/thread_switch.h>
40
41 #define likely(x) os_likely(x)
42 #define unlikely(x) os_unlikely(x)
43
44 #define __OS_CRASH__(rc, msg) ({ \
45 _os_set_crash_log_cause_and_message(rc, msg); \
46 os_prevent_tail_call_optimization(); \
47 __builtin_trap(); \
48 })
49
50 #define __LIBPLATFORM_CLIENT_CRASH__(rc, msg) \
51 __OS_CRASH__(rc, "BUG IN CLIENT OF LIBPLATFORM: " msg)
52 #define __LIBPLATFORM_INTERNAL_CRASH__(rc, msg) \
53 __OS_CRASH__(rc, "BUG IN LIBPLATFORM: " msg)
54
55
56 #define __OS_EXPOSE_INTERNALS__ 1
57 #include "os/internal/internal_shared.h"
58 #include "yield.h"
59
60 #if !VARIANT_NO_RESOLVERS
61 #if defined(_ARM_ARCH_7) && !defined(__ARM_ARCH_7S__)
62 #if OS_ATOMIC_UP
63 #define OS_VARIANT_SELECTOR up
64 #else
65 #define OS_VARIANT_SELECTOR mp
66 #endif
67 #endif
68 #if !defined(OS_VARIANT_SELECTOR) && defined(VARIANT_NO_RESOLVERS)
69 // forced up variant for no-barrier OSAtomics
70 #define OS_ATOMIC_NO_BARRIER_ONLY 1
71 #define OS_VARIANT_SELECTOR up
72 #endif
73 #if (defined(_ARM_ARCH_7) || defined(__arm64__)) && \
74 (!defined(OS_ATOMIC_WFE) && !OS_ATOMIC_UP)
75 #define OS_ATOMIC_WFE 0
76 #endif
77 #ifdef OS_ATOMIC_WFE
78 #if OS_ATOMIC_WFE
79 #define OS_LOCK_VARIANT_SELECTOR wfe
80 #else
81 #define OS_LOCK_VARIANT_SELECTOR mp
82 #endif
83 #endif
84 #endif // !VARIANT_NO_RESOLVERS
85
86 #define OS_VARIANT(f, v) OS_CONCAT(f, OS_CONCAT($VARIANT$, v))
87
88 #ifdef OS_VARIANT_SELECTOR
89 #define _OS_ATOMIC_ALIAS_PRIVATE_EXTERN(n) \
90 ".private_extern _" OS_STRINGIFY(n) "\n\t"
91 #define OS_ATOMIC_EXPORT
92 #else
93 #define _OS_ATOMIC_ALIAS_PRIVATE_EXTERN(n)
94 #define OS_ATOMIC_EXPORT OS_EXPORT
95 #endif
96 #define _OS_ATOMIC_ALIAS_GLOBL(n) \
97 ".globl _" OS_STRINGIFY(n) "\n\t"
98 #ifdef __thumb__
99 #define _OS_ATOMIC_ALIAS_THUMB(n) \
100 ".thumb_func _" OS_STRINGIFY(n) "\n\t"
101 #else
102 #define _OS_ATOMIC_ALIAS_THUMB(n)
103 #endif
104 #define _OS_ATOMIC_ALIAS_SET(n, o) \
105 ".set _" OS_STRINGIFY(n) ", _" OS_STRINGIFY(o)
106
107 #define OS_ATOMIC_ALIAS(n, o) __asm__( \
108 _OS_ATOMIC_ALIAS_PRIVATE_EXTERN(n) \
109 _OS_ATOMIC_ALIAS_GLOBL(n) \
110 _OS_ATOMIC_ALIAS_THUMB(n) \
111 _OS_ATOMIC_ALIAS_SET(n, o))
112
113 #define OS_ATOMIC_EXPORT_ALIAS(n, o) __asm__( \
114 _OS_ATOMIC_ALIAS_GLOBL(n) \
115 _OS_ATOMIC_ALIAS_THUMB(n) \
116 _OS_ATOMIC_ALIAS_SET(n, o))
117
118 #define _OS_VARIANT_RESOLVER(s, v, ...) \
119 __attribute__((visibility(OS_STRINGIFY(v)))) extern void* s(void); \
120 void* s(void) { \
121 __asm__(".symbol_resolver _" OS_STRINGIFY(s)); \
122 __VA_ARGS__ \
123 }
124
125 #define _OS_VARIANT_UPMP_RESOLVER(s, v) \
126 _OS_VARIANT_RESOLVER(s, v, \
127 uint32_t *_c = (void*)(uintptr_t)_COMM_PAGE_CPU_CAPABILITIES; \
128 if (*_c & kUP) { \
129 extern void OS_VARIANT(s, up)(void); \
130 return &OS_VARIANT(s, up); \
131 } else { \
132 extern void OS_VARIANT(s, mp)(void); \
133 return &OS_VARIANT(s, mp); \
134 })
135
136 #define OS_VARIANT_UPMP_RESOLVER(s) \
137 _OS_VARIANT_UPMP_RESOLVER(s, default)
138
139 #define OS_VARIANT_UPMP_RESOLVER_INTERNAL(s) \
140 _OS_VARIANT_UPMP_RESOLVER(s, hidden)
141
142 #endif // __OS_INTERNAL_H__