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