1*c54f35caSApple OSS Distributions // vim:noexpandtab
2*c54f35caSApple OSS Distributions #include <stdint.h>
3*c54f35caSApple OSS Distributions #include <stdlib.h>
4*c54f35caSApple OSS Distributions #include <stdio.h>
5*c54f35caSApple OSS Distributions #include <stdbool.h>
6*c54f35caSApple OSS Distributions #include <stdarg.h>
7*c54f35caSApple OSS Distributions #include <errno.h>
8*c54f35caSApple OSS Distributions #include <string.h>
9*c54f35caSApple OSS Distributions #include <unistd.h>
10*c54f35caSApple OSS Distributions #include <sys/syscall.h>
11*c54f35caSApple OSS Distributions #include <sys/ulock.h>
12*c54f35caSApple OSS Distributions
13*c54f35caSApple OSS Distributions #include "turnstile_multihop_types.h"
14*c54f35caSApple OSS Distributions
15*c54f35caSApple OSS Distributions typedef _Atomic(u32) lock_t;
16*c54f35caSApple OSS Distributions
17*c54f35caSApple OSS Distributions __inline static void
yield(void)18*c54f35caSApple OSS Distributions yield(void)
19*c54f35caSApple OSS Distributions {
20*c54f35caSApple OSS Distributions #if !defined(__x86_64__) && !defined(__i386__)
21*c54f35caSApple OSS Distributions __asm volatile ("yield");
22*c54f35caSApple OSS Distributions #else
23*c54f35caSApple OSS Distributions __asm volatile ("pause");
24*c54f35caSApple OSS Distributions #endif
25*c54f35caSApple OSS Distributions }
26*c54f35caSApple OSS Distributions
27*c54f35caSApple OSS Distributions __inline static void
wfe(void)28*c54f35caSApple OSS Distributions wfe(void)
29*c54f35caSApple OSS Distributions {
30*c54f35caSApple OSS Distributions #if !defined(__x86_64__) && !defined(__i386__)
31*c54f35caSApple OSS Distributions __asm volatile ("wfe");
32*c54f35caSApple OSS Distributions #else
33*c54f35caSApple OSS Distributions __asm volatile ("pause");
34*c54f35caSApple OSS Distributions #endif
35*c54f35caSApple OSS Distributions }
36*c54f35caSApple OSS Distributions
37*c54f35caSApple OSS Distributions __inline static void
wfi(void)38*c54f35caSApple OSS Distributions wfi(void)
39*c54f35caSApple OSS Distributions {
40*c54f35caSApple OSS Distributions #if !defined(__x86_64__) && !defined(__i386__)
41*c54f35caSApple OSS Distributions __asm volatile ("wfi");
42*c54f35caSApple OSS Distributions #else
43*c54f35caSApple OSS Distributions __asm volatile ("pause");
44*c54f35caSApple OSS Distributions #endif
45*c54f35caSApple OSS Distributions }
46*c54f35caSApple OSS Distributions
47*c54f35caSApple OSS Distributions __inline static void
sev(void)48*c54f35caSApple OSS Distributions sev(void)
49*c54f35caSApple OSS Distributions {
50*c54f35caSApple OSS Distributions #if !defined(__x86_64__) && !defined(__i386__)
51*c54f35caSApple OSS Distributions __asm volatile ("sev");
52*c54f35caSApple OSS Distributions #endif
53*c54f35caSApple OSS Distributions }
54*c54f35caSApple OSS Distributions
55*c54f35caSApple OSS Distributions #include <os/tsd.h>
56*c54f35caSApple OSS Distributions
57*c54f35caSApple OSS Distributions #ifndef __TSD_MACH_THREAD_SELF
58*c54f35caSApple OSS Distributions #define __TSD_MACH_THREAD_SELF 3
59*c54f35caSApple OSS Distributions #endif
60*c54f35caSApple OSS Distributions
61*c54f35caSApple OSS Distributions __inline static mach_port_name_t
_os_get_self(void)62*c54f35caSApple OSS Distributions _os_get_self(void)
63*c54f35caSApple OSS Distributions {
64*c54f35caSApple OSS Distributions mach_port_name_t self = (mach_port_name_t)(uintptr_t)(void *)_os_tsd_get_direct(__TSD_MACH_THREAD_SELF);
65*c54f35caSApple OSS Distributions return self;
66*c54f35caSApple OSS Distributions }
67*c54f35caSApple OSS Distributions
68*c54f35caSApple OSS Distributions #define ULL_WAITERS 1U
69*c54f35caSApple OSS Distributions
70*c54f35caSApple OSS Distributions static uint32_t lock_no_wait[4] = { 0, 0, 0, 0};
71*c54f35caSApple OSS Distributions static uint32_t lock_wait[4] = { 0, 0, 0, 0};
72*c54f35caSApple OSS Distributions
73*c54f35caSApple OSS Distributions static mach_port_name_t main_thread_name = 0;
74*c54f35caSApple OSS Distributions
75*c54f35caSApple OSS Distributions __inline static void
ull_lock(lock_t * lock,int id,uint opcode,uint flags)76*c54f35caSApple OSS Distributions ull_lock(lock_t *lock, int id, uint opcode, uint flags)
77*c54f35caSApple OSS Distributions {
78*c54f35caSApple OSS Distributions u32 thread_id = _os_get_self() & ~0x3u;
79*c54f35caSApple OSS Distributions u32 ull_locked = (opcode == UL_UNFAIR_LOCK) ? thread_id : 4u;
80*c54f35caSApple OSS Distributions u32 mach_id = _os_get_self() >> 2;
81*c54f35caSApple OSS Distributions u32 prev;
82*c54f35caSApple OSS Distributions bool succeeded = false;
83*c54f35caSApple OSS Distributions bool waiters = false;
84*c54f35caSApple OSS Distributions bool called_wait = false;
85*c54f35caSApple OSS Distributions u32 count = 0;
86*c54f35caSApple OSS Distributions
87*c54f35caSApple OSS Distributions do {
88*c54f35caSApple OSS Distributions count++;
89*c54f35caSApple OSS Distributions if ((count % 100000) == 0) {
90*c54f35caSApple OSS Distributions printf("[%d,%d]%s>top of loop count=%d\n", id, mach_id, __FUNCTION__, count);
91*c54f35caSApple OSS Distributions }
92*c54f35caSApple OSS Distributions u32 new = waiters ? (ULL_WAITERS | ull_locked) : ull_locked;
93*c54f35caSApple OSS Distributions prev = 0;
94*c54f35caSApple OSS Distributions __c11_atomic_compare_exchange_strong(lock, &prev, new, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
95*c54f35caSApple OSS Distributions if (prev == 0) {
96*c54f35caSApple OSS Distributions /* Was unlocked, now locked */
97*c54f35caSApple OSS Distributions succeeded = true;
98*c54f35caSApple OSS Distributions break;
99*c54f35caSApple OSS Distributions }
100*c54f35caSApple OSS Distributions
101*c54f35caSApple OSS Distributions u32 value = prev;
102*c54f35caSApple OSS Distributions if (!(value & ULL_WAITERS)) {
103*c54f35caSApple OSS Distributions new = value | ULL_WAITERS;
104*c54f35caSApple OSS Distributions __c11_atomic_compare_exchange_strong(lock, &prev, new, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
105*c54f35caSApple OSS Distributions if (prev == value) {
106*c54f35caSApple OSS Distributions /* succeeded in setting ULL_WAITERS */
107*c54f35caSApple OSS Distributions value = new;
108*c54f35caSApple OSS Distributions } else if (prev & ULL_WAITERS) {
109*c54f35caSApple OSS Distributions /* Didn't succeed, but someone else already set ULL_WAITERS */
110*c54f35caSApple OSS Distributions value = prev;
111*c54f35caSApple OSS Distributions } else {
112*c54f35caSApple OSS Distributions /* Something changed under us, so try again */
113*c54f35caSApple OSS Distributions if (count % 100000 == 0) {
114*c54f35caSApple OSS Distributions printf("[%d,%d]%s>Something changed under us, prev=%d\n", id, mach_id, __FUNCTION__, prev);
115*c54f35caSApple OSS Distributions }
116*c54f35caSApple OSS Distributions continue;
117*c54f35caSApple OSS Distributions }
118*c54f35caSApple OSS Distributions }
119*c54f35caSApple OSS Distributions /* Locked with waiters indication, so block */
120*c54f35caSApple OSS Distributions int ret = __ulock_wait(flags | opcode, lock, value, 0);
121*c54f35caSApple OSS Distributions called_wait = true;
122*c54f35caSApple OSS Distributions if (ret < 0) {
123*c54f35caSApple OSS Distributions if (flags & ULF_NO_ERRNO) {
124*c54f35caSApple OSS Distributions errno = -ret;
125*c54f35caSApple OSS Distributions }
126*c54f35caSApple OSS Distributions if (errno == EFAULT) {
127*c54f35caSApple OSS Distributions continue;
128*c54f35caSApple OSS Distributions }
129*c54f35caSApple OSS Distributions printf("[%d,%d]%s>ull_wait() error: %s\n", id, mach_id, __FUNCTION__, strerror(errno));
130*c54f35caSApple OSS Distributions exit(1);
131*c54f35caSApple OSS Distributions }
132*c54f35caSApple OSS Distributions waiters = (ret > 0);
133*c54f35caSApple OSS Distributions
134*c54f35caSApple OSS Distributions if (count % 100000 == 0) {
135*c54f35caSApple OSS Distributions printf("[%d,%d]%s>bottom of loop prev=%d\n", id, mach_id, __FUNCTION__, prev);
136*c54f35caSApple OSS Distributions }
137*c54f35caSApple OSS Distributions } while (!succeeded);
138*c54f35caSApple OSS Distributions
139*c54f35caSApple OSS Distributions if (called_wait) {
140*c54f35caSApple OSS Distributions lock_wait[id]++;
141*c54f35caSApple OSS Distributions } else {
142*c54f35caSApple OSS Distributions lock_no_wait[id]++;
143*c54f35caSApple OSS Distributions }
144*c54f35caSApple OSS Distributions }
145*c54f35caSApple OSS Distributions
146*c54f35caSApple OSS Distributions static uint32_t unlock_no_waiters[4] = { 0, 0, 0, 0};
147*c54f35caSApple OSS Distributions static uint32_t unlock_waiters[4] = { 0, 0, 0, 0 };
148*c54f35caSApple OSS Distributions static uint32_t unlock_waiters_gone[4] = { 0, 0, 0, 0 };
149*c54f35caSApple OSS Distributions static uint32_t unlock_waiters_wake_thread[4] = { 0, 0, 0, 0 };
150*c54f35caSApple OSS Distributions
151*c54f35caSApple OSS Distributions __inline static void
ull_unlock(lock_t * lock,int id,uint opcode,uint flags)152*c54f35caSApple OSS Distributions ull_unlock(lock_t *lock, int id, uint opcode, uint flags)
153*c54f35caSApple OSS Distributions {
154*c54f35caSApple OSS Distributions u32 thread_id = _os_get_self() & ~0x3u;
155*c54f35caSApple OSS Distributions u32 ull_locked = (opcode == UL_UNFAIR_LOCK) ? thread_id : 4u;
156*c54f35caSApple OSS Distributions u32 mach_id = _os_get_self() >> 2;
157*c54f35caSApple OSS Distributions u32 prev = ull_locked;
158*c54f35caSApple OSS Distributions __c11_atomic_compare_exchange_strong(lock, &prev, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED);
159*c54f35caSApple OSS Distributions if (prev == ull_locked) {
160*c54f35caSApple OSS Distributions unlock_no_waiters[id]++;
161*c54f35caSApple OSS Distributions return;
162*c54f35caSApple OSS Distributions }
163*c54f35caSApple OSS Distributions
164*c54f35caSApple OSS Distributions if (prev == 0) {
165*c54f35caSApple OSS Distributions printf("%s>already unlocked\n", __FUNCTION__);
166*c54f35caSApple OSS Distributions exit(1);
167*c54f35caSApple OSS Distributions }
168*c54f35caSApple OSS Distributions
169*c54f35caSApple OSS Distributions if (prev == (ULL_WAITERS | ull_locked)) {
170*c54f35caSApple OSS Distributions /* locked with waiters */
171*c54f35caSApple OSS Distributions __c11_atomic_store(lock, 0, __ATOMIC_SEQ_CST);
172*c54f35caSApple OSS Distributions
173*c54f35caSApple OSS Distributions if ((flags & ULF_WAKE_THREAD) && (_os_get_self() == main_thread_name)) {
174*c54f35caSApple OSS Distributions flags &= ~(uint)ULF_WAKE_THREAD;
175*c54f35caSApple OSS Distributions }
176*c54f35caSApple OSS Distributions int ret = __ulock_wake((flags | opcode), lock, main_thread_name);
177*c54f35caSApple OSS Distributions if ((ret < 0) && (flags & ULF_NO_ERRNO)) {
178*c54f35caSApple OSS Distributions errno = -ret;
179*c54f35caSApple OSS Distributions }
180*c54f35caSApple OSS Distributions if ((flags & ULF_WAKE_THREAD) && (ret < 0) && (errno == EALREADY)) {
181*c54f35caSApple OSS Distributions flags &= ~(uint)ULF_WAKE_THREAD;
182*c54f35caSApple OSS Distributions ret = __ulock_wake((flags | opcode), lock, 0);
183*c54f35caSApple OSS Distributions if ((ret < 0) && (flags & ULF_NO_ERRNO)) {
184*c54f35caSApple OSS Distributions errno = -ret;
185*c54f35caSApple OSS Distributions }
186*c54f35caSApple OSS Distributions } else if ((flags & ULF_WAKE_THREAD) && (ret == 0)) {
187*c54f35caSApple OSS Distributions unlock_waiters_wake_thread[id]++;
188*c54f35caSApple OSS Distributions }
189*c54f35caSApple OSS Distributions if (ret < 0) {
190*c54f35caSApple OSS Distributions if (errno == ENOENT) {
191*c54f35caSApple OSS Distributions unlock_waiters_gone[id]++;
192*c54f35caSApple OSS Distributions } else {
193*c54f35caSApple OSS Distributions printf("[%d,%d]%s>ull_wake() error: %s\n", id, mach_id, __FUNCTION__, strerror(errno));
194*c54f35caSApple OSS Distributions exit(1);
195*c54f35caSApple OSS Distributions }
196*c54f35caSApple OSS Distributions }
197*c54f35caSApple OSS Distributions unlock_waiters[id]++;
198*c54f35caSApple OSS Distributions } else {
199*c54f35caSApple OSS Distributions printf("%s>unexpected lock value %d\n", __FUNCTION__, prev);
200*c54f35caSApple OSS Distributions exit(1);
201*c54f35caSApple OSS Distributions }
202*c54f35caSApple OSS Distributions }
203