xref: /xnu-8020.140.41/tests/turnstile_multihop.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions  * turnstile_multihop: Tests turnstile and multi hop priority propagation.
3*27b03b36SApple OSS Distributions  */
4*27b03b36SApple OSS Distributions 
5*27b03b36SApple OSS Distributions #ifdef T_NAMESPACE
6*27b03b36SApple OSS Distributions #undef T_NAMESPACE
7*27b03b36SApple OSS Distributions #endif
8*27b03b36SApple OSS Distributions 
9*27b03b36SApple OSS Distributions #include <darwintest.h>
10*27b03b36SApple OSS Distributions #include <darwintest_multiprocess.h>
11*27b03b36SApple OSS Distributions 
12*27b03b36SApple OSS Distributions #include <dispatch/dispatch.h>
13*27b03b36SApple OSS Distributions #include <pthread.h>
14*27b03b36SApple OSS Distributions #include <launch.h>
15*27b03b36SApple OSS Distributions #include <mach/mach.h>
16*27b03b36SApple OSS Distributions #include <mach/message.h>
17*27b03b36SApple OSS Distributions #include <mach/mach_voucher.h>
18*27b03b36SApple OSS Distributions #include <pthread/workqueue_private.h>
19*27b03b36SApple OSS Distributions #include <voucher/ipc_pthread_priority_types.h>
20*27b03b36SApple OSS Distributions #include <servers/bootstrap.h>
21*27b03b36SApple OSS Distributions #include <stdlib.h>
22*27b03b36SApple OSS Distributions #include <sys/event.h>
23*27b03b36SApple OSS Distributions #include <unistd.h>
24*27b03b36SApple OSS Distributions #include <crt_externs.h>
25*27b03b36SApple OSS Distributions #include <signal.h>
26*27b03b36SApple OSS Distributions #include <sys/types.h>
27*27b03b36SApple OSS Distributions #include <sys/sysctl.h>
28*27b03b36SApple OSS Distributions #include <libkern/OSAtomic.h>
29*27b03b36SApple OSS Distributions #include <sys/wait.h>
30*27b03b36SApple OSS Distributions 
31*27b03b36SApple OSS Distributions #include "turnstile_multihop_helper.h"
32*27b03b36SApple OSS Distributions 
33*27b03b36SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.turnstile_multihop"));
34*27b03b36SApple OSS Distributions 
35*27b03b36SApple OSS Distributions #define HELPER_TIMEOUT_SECS (3000)
36*27b03b36SApple OSS Distributions 
37*27b03b36SApple OSS Distributions struct test_msg {
38*27b03b36SApple OSS Distributions 	mach_msg_header_t header;
39*27b03b36SApple OSS Distributions 	mach_msg_body_t body;
40*27b03b36SApple OSS Distributions 	mach_msg_port_descriptor_t port_descriptor;
41*27b03b36SApple OSS Distributions };
42*27b03b36SApple OSS Distributions 
43*27b03b36SApple OSS Distributions static boolean_t spin_for_ever = false;
44*27b03b36SApple OSS Distributions 
45*27b03b36SApple OSS Distributions static boolean_t test_noimportance = false;
46*27b03b36SApple OSS Distributions 
47*27b03b36SApple OSS Distributions #define EXPECTED_MESSAGE_ID 0x100
48*27b03b36SApple OSS Distributions 
49*27b03b36SApple OSS Distributions static void
50*27b03b36SApple OSS Distributions thread_create_at_qos(qos_class_t qos, void * (*function)(void *));
51*27b03b36SApple OSS Distributions static uint64_t
52*27b03b36SApple OSS Distributions nanoseconds_to_absolutetime(uint64_t nanoseconds);
53*27b03b36SApple OSS Distributions static int
54*27b03b36SApple OSS Distributions sched_create_load_at_qos(qos_class_t qos, void **load_token);
55*27b03b36SApple OSS Distributions static int
56*27b03b36SApple OSS Distributions sched_terminate_load(void *load_token) __unused;
57*27b03b36SApple OSS Distributions static void do_work(int num);
58*27b03b36SApple OSS Distributions static void
59*27b03b36SApple OSS Distributions dispatch_sync_cancel(mach_port_t owner_thread, qos_class_t promote_qos);
60*27b03b36SApple OSS Distributions 
61*27b03b36SApple OSS Distributions static void *sched_load_thread(void *);
62*27b03b36SApple OSS Distributions 
63*27b03b36SApple OSS Distributions struct load_token_context {
64*27b03b36SApple OSS Distributions 	volatile int threads_should_exit;
65*27b03b36SApple OSS Distributions 	int thread_count;
66*27b03b36SApple OSS Distributions 	qos_class_t qos;
67*27b03b36SApple OSS Distributions 	pthread_t *threads;
68*27b03b36SApple OSS Distributions };
69*27b03b36SApple OSS Distributions 
70*27b03b36SApple OSS Distributions static struct mach_timebase_info sched_mti;
71*27b03b36SApple OSS Distributions static pthread_once_t sched_mti_once_control = PTHREAD_ONCE_INIT;
72*27b03b36SApple OSS Distributions 
73*27b03b36SApple OSS Distributions static void
sched_mti_init(void)74*27b03b36SApple OSS Distributions sched_mti_init(void)
75*27b03b36SApple OSS Distributions {
76*27b03b36SApple OSS Distributions 	mach_timebase_info(&sched_mti);
77*27b03b36SApple OSS Distributions }
78*27b03b36SApple OSS Distributions uint64_t
nanoseconds_to_absolutetime(uint64_t nanoseconds)79*27b03b36SApple OSS Distributions nanoseconds_to_absolutetime(uint64_t nanoseconds)
80*27b03b36SApple OSS Distributions {
81*27b03b36SApple OSS Distributions 	pthread_once(&sched_mti_once_control, sched_mti_init);
82*27b03b36SApple OSS Distributions 
83*27b03b36SApple OSS Distributions 	return (uint64_t)(nanoseconds * (((double)sched_mti.denom) / ((double)sched_mti.numer)));
84*27b03b36SApple OSS Distributions }
85*27b03b36SApple OSS Distributions 
86*27b03b36SApple OSS Distributions static int
sched_create_load_at_qos(qos_class_t qos,void ** load_token)87*27b03b36SApple OSS Distributions sched_create_load_at_qos(qos_class_t qos, void **load_token)
88*27b03b36SApple OSS Distributions {
89*27b03b36SApple OSS Distributions 	struct load_token_context *context = NULL;
90*27b03b36SApple OSS Distributions 	int ret;
91*27b03b36SApple OSS Distributions 	int ncpu;
92*27b03b36SApple OSS Distributions 	size_t ncpu_size = sizeof(ncpu);
93*27b03b36SApple OSS Distributions 	int nthreads;
94*27b03b36SApple OSS Distributions 	int i;
95*27b03b36SApple OSS Distributions 	pthread_attr_t attr;
96*27b03b36SApple OSS Distributions 
97*27b03b36SApple OSS Distributions 	ret = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
98*27b03b36SApple OSS Distributions 	if (ret == -1) {
99*27b03b36SApple OSS Distributions 		T_LOG("sysctlbyname(hw.ncpu)");
100*27b03b36SApple OSS Distributions 		return errno;
101*27b03b36SApple OSS Distributions 	}
102*27b03b36SApple OSS Distributions 
103*27b03b36SApple OSS Distributions 	T_QUIET; T_LOG("%s: Detected %d CPUs\n", __FUNCTION__, ncpu);
104*27b03b36SApple OSS Distributions 
105*27b03b36SApple OSS Distributions 	nthreads = ncpu;
106*27b03b36SApple OSS Distributions 	T_QUIET; T_LOG("%s: Will create %d threads\n", __FUNCTION__, nthreads);
107*27b03b36SApple OSS Distributions 
108*27b03b36SApple OSS Distributions 	ret = pthread_attr_init(&attr);
109*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "pthread_attr_init");
110*27b03b36SApple OSS Distributions 
111*27b03b36SApple OSS Distributions 	if (&pthread_attr_set_qos_class_np) {
112*27b03b36SApple OSS Distributions 		ret = pthread_attr_set_qos_class_np(&attr, qos, 0);
113*27b03b36SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "pthread_attr_set_qos_class_np");
114*27b03b36SApple OSS Distributions 	}
115*27b03b36SApple OSS Distributions 
116*27b03b36SApple OSS Distributions 	context = calloc(1, sizeof(*context));
117*27b03b36SApple OSS Distributions 	if (context == NULL) {
118*27b03b36SApple OSS Distributions 		T_QUIET; T_LOG("calloc returned error"); return ENOMEM;
119*27b03b36SApple OSS Distributions 	}
120*27b03b36SApple OSS Distributions 
121*27b03b36SApple OSS Distributions 	context->threads_should_exit = 0;
122*27b03b36SApple OSS Distributions 	context->thread_count = nthreads;
123*27b03b36SApple OSS Distributions 	context->qos = qos;
124*27b03b36SApple OSS Distributions 	context->threads = calloc((unsigned int)nthreads, sizeof(pthread_t));
125*27b03b36SApple OSS Distributions 
126*27b03b36SApple OSS Distributions 	OSMemoryBarrier();
127*27b03b36SApple OSS Distributions 
128*27b03b36SApple OSS Distributions 	for (i = 0; i < nthreads; i++) {
129*27b03b36SApple OSS Distributions 		ret = pthread_create(&context->threads[i], &attr, sched_load_thread, context);
130*27b03b36SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "pthread_create");
131*27b03b36SApple OSS Distributions 		T_QUIET; T_LOG("%s: Created thread %d (%p)\n", __FUNCTION__, i, (void *)context->threads[i]);
132*27b03b36SApple OSS Distributions 	}
133*27b03b36SApple OSS Distributions 
134*27b03b36SApple OSS Distributions 	ret = pthread_attr_destroy(&attr);
135*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "pthread_attr_destroy");
136*27b03b36SApple OSS Distributions 
137*27b03b36SApple OSS Distributions 	*load_token = context;
138*27b03b36SApple OSS Distributions 
139*27b03b36SApple OSS Distributions 	return 0;
140*27b03b36SApple OSS Distributions }
141*27b03b36SApple OSS Distributions 
142*27b03b36SApple OSS Distributions static void *
sched_load_thread(void * arg)143*27b03b36SApple OSS Distributions sched_load_thread(void *arg)
144*27b03b36SApple OSS Distributions {
145*27b03b36SApple OSS Distributions 	struct load_token_context *context = (struct load_token_context *)arg;
146*27b03b36SApple OSS Distributions 
147*27b03b36SApple OSS Distributions 	T_QUIET; T_LOG("%s: Thread started %p\n", __FUNCTION__, (void *)pthread_self());
148*27b03b36SApple OSS Distributions 
149*27b03b36SApple OSS Distributions 	while (!context->threads_should_exit) {
150*27b03b36SApple OSS Distributions 		uint64_t start = mach_absolute_time();
151*27b03b36SApple OSS Distributions 		uint64_t end = start + nanoseconds_to_absolutetime(900ULL * NSEC_PER_MSEC);
152*27b03b36SApple OSS Distributions 
153*27b03b36SApple OSS Distributions 		while ((mach_absolute_time() < end) && !context->threads_should_exit) {
154*27b03b36SApple OSS Distributions 			;
155*27b03b36SApple OSS Distributions 		}
156*27b03b36SApple OSS Distributions 	}
157*27b03b36SApple OSS Distributions 
158*27b03b36SApple OSS Distributions 	T_QUIET; T_LOG("%s: Thread terminating %p\n", __FUNCTION__, (void *)pthread_self());
159*27b03b36SApple OSS Distributions 
160*27b03b36SApple OSS Distributions 	return NULL;
161*27b03b36SApple OSS Distributions }
162*27b03b36SApple OSS Distributions 
163*27b03b36SApple OSS Distributions static int
sched_terminate_load(void * load_token)164*27b03b36SApple OSS Distributions sched_terminate_load(void *load_token)
165*27b03b36SApple OSS Distributions {
166*27b03b36SApple OSS Distributions 	int ret;
167*27b03b36SApple OSS Distributions 	int i;
168*27b03b36SApple OSS Distributions 	struct load_token_context *context = (struct load_token_context *)load_token;
169*27b03b36SApple OSS Distributions 
170*27b03b36SApple OSS Distributions 	context->threads_should_exit = 1;
171*27b03b36SApple OSS Distributions 	OSMemoryBarrier();
172*27b03b36SApple OSS Distributions 
173*27b03b36SApple OSS Distributions 	for (i = 0; i < context->thread_count; i++) {
174*27b03b36SApple OSS Distributions 		T_QUIET; T_LOG("%s: Joining thread %d (%p)\n", __FUNCTION__, i, (void *)context->threads[i]);
175*27b03b36SApple OSS Distributions 		ret = pthread_join(context->threads[i], NULL);
176*27b03b36SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "pthread_join");
177*27b03b36SApple OSS Distributions 	}
178*27b03b36SApple OSS Distributions 
179*27b03b36SApple OSS Distributions 	free(context->threads);
180*27b03b36SApple OSS Distributions 	free(context);
181*27b03b36SApple OSS Distributions 
182*27b03b36SApple OSS Distributions 	return 0;
183*27b03b36SApple OSS Distributions }
184*27b03b36SApple OSS Distributions 
185*27b03b36SApple OSS Distributions 
186*27b03b36SApple OSS Distributions // Find the first num primes, simply as a means of doing work
187*27b03b36SApple OSS Distributions static void
do_work(int num)188*27b03b36SApple OSS Distributions do_work(int num)
189*27b03b36SApple OSS Distributions {
190*27b03b36SApple OSS Distributions 	volatile int i = 3, count, c;
191*27b03b36SApple OSS Distributions 
192*27b03b36SApple OSS Distributions 	for (count = 2; count <= num;) {
193*27b03b36SApple OSS Distributions 		for (c = 2; c <= i; c++) {
194*27b03b36SApple OSS Distributions 			if (i % c == 0) {
195*27b03b36SApple OSS Distributions 				break;
196*27b03b36SApple OSS Distributions 			}
197*27b03b36SApple OSS Distributions 		}
198*27b03b36SApple OSS Distributions 		if (c == i) {
199*27b03b36SApple OSS Distributions 			count++;
200*27b03b36SApple OSS Distributions 		}
201*27b03b36SApple OSS Distributions 		i++;
202*27b03b36SApple OSS Distributions 	}
203*27b03b36SApple OSS Distributions }
204*27b03b36SApple OSS Distributions 
205*27b03b36SApple OSS Distributions #pragma mark pthread callbacks
206*27b03b36SApple OSS Distributions 
207*27b03b36SApple OSS Distributions static void
worker_cb(pthread_priority_t __unused priority)208*27b03b36SApple OSS Distributions worker_cb(pthread_priority_t __unused priority)
209*27b03b36SApple OSS Distributions {
210*27b03b36SApple OSS Distributions 	T_FAIL("a worker thread was created");
211*27b03b36SApple OSS Distributions }
212*27b03b36SApple OSS Distributions 
213*27b03b36SApple OSS Distributions static void
event_cb(void ** __unused events,int * __unused nevents)214*27b03b36SApple OSS Distributions event_cb(void ** __unused events, int * __unused nevents)
215*27b03b36SApple OSS Distributions {
216*27b03b36SApple OSS Distributions 	T_FAIL("a kevent routine was called instead of workloop");
217*27b03b36SApple OSS Distributions }
218*27b03b36SApple OSS Distributions 
219*27b03b36SApple OSS Distributions static uint32_t
get_user_promotion_basepri(void)220*27b03b36SApple OSS Distributions get_user_promotion_basepri(void)
221*27b03b36SApple OSS Distributions {
222*27b03b36SApple OSS Distributions 	mach_msg_type_number_t count = THREAD_POLICY_STATE_COUNT;
223*27b03b36SApple OSS Distributions 	struct thread_policy_state thread_policy;
224*27b03b36SApple OSS Distributions 	boolean_t get_default = FALSE;
225*27b03b36SApple OSS Distributions 	mach_port_t thread_port = pthread_mach_thread_np(pthread_self());
226*27b03b36SApple OSS Distributions 
227*27b03b36SApple OSS Distributions 	kern_return_t kr = thread_policy_get(thread_port, THREAD_POLICY_STATE,
228*27b03b36SApple OSS Distributions 	    (thread_policy_t)&thread_policy, &count, &get_default);
229*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_policy_get");
230*27b03b36SApple OSS Distributions 	return thread_policy.thps_user_promotion_basepri;
231*27b03b36SApple OSS Distributions }
232*27b03b36SApple OSS Distributions 
233*27b03b36SApple OSS Distributions static uint32_t
get_thread_base_priority(void)234*27b03b36SApple OSS Distributions get_thread_base_priority(void)
235*27b03b36SApple OSS Distributions {
236*27b03b36SApple OSS Distributions 	kern_return_t kr;
237*27b03b36SApple OSS Distributions 	mach_port_t thread_port = pthread_mach_thread_np(pthread_self());
238*27b03b36SApple OSS Distributions 
239*27b03b36SApple OSS Distributions 	policy_timeshare_info_data_t timeshare_info;
240*27b03b36SApple OSS Distributions 	mach_msg_type_number_t count = POLICY_TIMESHARE_INFO_COUNT;
241*27b03b36SApple OSS Distributions 
242*27b03b36SApple OSS Distributions 	kr = thread_info(thread_port, THREAD_SCHED_TIMESHARE_INFO,
243*27b03b36SApple OSS Distributions 	    (thread_info_t)&timeshare_info, &count);
244*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info");
245*27b03b36SApple OSS Distributions 
246*27b03b36SApple OSS Distributions 	return (uint32_t)timeshare_info.base_priority;
247*27b03b36SApple OSS Distributions }
248*27b03b36SApple OSS Distributions 
249*27b03b36SApple OSS Distributions 
250*27b03b36SApple OSS Distributions #define LISTENER_WLID  0x100
251*27b03b36SApple OSS Distributions #define CONN_WLID      0x200
252*27b03b36SApple OSS Distributions 
253*27b03b36SApple OSS Distributions static uint32_t
register_port_options(void)254*27b03b36SApple OSS Distributions register_port_options(void)
255*27b03b36SApple OSS Distributions {
256*27b03b36SApple OSS Distributions 	return MACH_RCV_MSG | MACH_RCV_LARGE | MACH_RCV_LARGE_IDENTITY |
257*27b03b36SApple OSS Distributions 	       MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_CTX) |
258*27b03b36SApple OSS Distributions 	       MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0) |
259*27b03b36SApple OSS Distributions 	       MACH_RCV_VOUCHER;
260*27b03b36SApple OSS Distributions }
261*27b03b36SApple OSS Distributions 
262*27b03b36SApple OSS Distributions static void
register_port(uint64_t wlid,mach_port_t port)263*27b03b36SApple OSS Distributions register_port(uint64_t wlid, mach_port_t port)
264*27b03b36SApple OSS Distributions {
265*27b03b36SApple OSS Distributions 	int r;
266*27b03b36SApple OSS Distributions 
267*27b03b36SApple OSS Distributions 	struct kevent_qos_s kev = {
268*27b03b36SApple OSS Distributions 		.ident  = port,
269*27b03b36SApple OSS Distributions 		.filter = EVFILT_MACHPORT,
270*27b03b36SApple OSS Distributions 		.flags  = EV_ADD | EV_UDATA_SPECIFIC | EV_DISPATCH | EV_VANISHED,
271*27b03b36SApple OSS Distributions 		.fflags = register_port_options(),
272*27b03b36SApple OSS Distributions 		.data   = 1,
273*27b03b36SApple OSS Distributions 		.qos    = (int32_t)_pthread_qos_class_encode(QOS_CLASS_MAINTENANCE, 0, 0)
274*27b03b36SApple OSS Distributions 	};
275*27b03b36SApple OSS Distributions 
276*27b03b36SApple OSS Distributions 	struct kevent_qos_s kev_err = { 0 };
277*27b03b36SApple OSS Distributions 
278*27b03b36SApple OSS Distributions 	/* Setup workloop for mach msg rcv */
279*27b03b36SApple OSS Distributions 	r = kevent_id(wlid, &kev, 1, &kev_err, 1, NULL,
280*27b03b36SApple OSS Distributions 	    NULL, KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_ERROR_EVENTS);
281*27b03b36SApple OSS Distributions 
282*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(r, "kevent_id");
283*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(r, 0, "no errors returned from kevent_id");
284*27b03b36SApple OSS Distributions }
285*27b03b36SApple OSS Distributions 
286*27b03b36SApple OSS Distributions /*
287*27b03b36SApple OSS Distributions  * Basic WL handler callback, it checks the
288*27b03b36SApple OSS Distributions  * effective Qos of the servicer thread.
289*27b03b36SApple OSS Distributions  */
290*27b03b36SApple OSS Distributions static void
workloop_cb_test_intransit(uint64_t * workloop_id,void ** eventslist,int * events)291*27b03b36SApple OSS Distributions workloop_cb_test_intransit(uint64_t *workloop_id, void **eventslist, int *events)
292*27b03b36SApple OSS Distributions {
293*27b03b36SApple OSS Distributions 	static bool got_peer;
294*27b03b36SApple OSS Distributions 
295*27b03b36SApple OSS Distributions 	struct kevent_qos_s *kev = eventslist[0];
296*27b03b36SApple OSS Distributions 	mach_msg_header_t *hdr;
297*27b03b36SApple OSS Distributions 	struct test_msg *tmsg;
298*27b03b36SApple OSS Distributions 
299*27b03b36SApple OSS Distributions 	T_LOG("Workloop handler %s called. Received message on 0x%llx",
300*27b03b36SApple OSS Distributions 	    __func__, *workloop_id);
301*27b03b36SApple OSS Distributions 
302*27b03b36SApple OSS Distributions 	/* Skip the test if we can't check Qos */
303*27b03b36SApple OSS Distributions 	if (geteuid() != 0) {
304*27b03b36SApple OSS Distributions 		T_SKIP("kevent_qos test requires root privileges to run.");
305*27b03b36SApple OSS Distributions 	}
306*27b03b36SApple OSS Distributions 
307*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(*events, 1, "should have one event");
308*27b03b36SApple OSS Distributions 
309*27b03b36SApple OSS Distributions 	T_EXPECT_REQUESTED_QOS_EQ(QOS_CLASS_MAINTENANCE, "message handler should have MT requested QoS");
310*27b03b36SApple OSS Distributions 
311*27b03b36SApple OSS Distributions 	hdr = (mach_msg_header_t *)kev->ext[0];
312*27b03b36SApple OSS Distributions 	T_ASSERT_NOTNULL(hdr, "has a message");
313*27b03b36SApple OSS Distributions 	T_ASSERT_EQ(hdr->msgh_size, (uint32_t)sizeof(struct test_msg), "of the right size");
314*27b03b36SApple OSS Distributions 	tmsg = (struct test_msg *)hdr;
315*27b03b36SApple OSS Distributions 
316*27b03b36SApple OSS Distributions 	switch (*workloop_id) {
317*27b03b36SApple OSS Distributions 	case LISTENER_WLID:
318*27b03b36SApple OSS Distributions 		T_LOG("Registering peer connection");
319*27b03b36SApple OSS Distributions 		T_QUIET; T_ASSERT_FALSE(got_peer, "Should not have seen peer yet");
320*27b03b36SApple OSS Distributions 		got_peer = true;
321*27b03b36SApple OSS Distributions 		break;
322*27b03b36SApple OSS Distributions 
323*27b03b36SApple OSS Distributions 	case CONN_WLID:
324*27b03b36SApple OSS Distributions 		T_LOG("Received message on peer");
325*27b03b36SApple OSS Distributions 		break;
326*27b03b36SApple OSS Distributions 
327*27b03b36SApple OSS Distributions 	default:
328*27b03b36SApple OSS Distributions 		T_FAIL("???");
329*27b03b36SApple OSS Distributions 	}
330*27b03b36SApple OSS Distributions 
331*27b03b36SApple OSS Distributions 	sleep(5);
332*27b03b36SApple OSS Distributions 	T_LOG("Do some CPU work.");
333*27b03b36SApple OSS Distributions 	do_work(5000);
334*27b03b36SApple OSS Distributions 
335*27b03b36SApple OSS Distributions 	/* Check if the override now is IN + 60 boost */
336*27b03b36SApple OSS Distributions 	T_EXPECT_EFFECTIVE_QOS_EQ(QOS_CLASS_USER_INITIATED,
337*27b03b36SApple OSS Distributions 	    "dispatch_source event handler QoS should be QOS_CLASS_USER_INITIATED");
338*27b03b36SApple OSS Distributions 	T_EXPECT_EQ(get_user_promotion_basepri(), 60u,
339*27b03b36SApple OSS Distributions 	    "dispatch_source event handler should be overridden at 60");
340*27b03b36SApple OSS Distributions 
341*27b03b36SApple OSS Distributions 	T_EXPECT_EQ(get_thread_base_priority(), 60u,
342*27b03b36SApple OSS Distributions 	    "dispatch_source event handler should have base pri at 60");
343*27b03b36SApple OSS Distributions 
344*27b03b36SApple OSS Distributions 	if (*workloop_id == LISTENER_WLID) {
345*27b03b36SApple OSS Distributions 		register_port(CONN_WLID, tmsg->port_descriptor.name);
346*27b03b36SApple OSS Distributions 
347*27b03b36SApple OSS Distributions 		kev->flags = EV_ADD | EV_ENABLE | EV_UDATA_SPECIFIC | EV_DISPATCH | EV_VANISHED;
348*27b03b36SApple OSS Distributions 		kev->fflags = register_port_options();
349*27b03b36SApple OSS Distributions 		kev->ext[0] = kev->ext[1] = kev->ext[2] = kev->ext[3] = 0;
350*27b03b36SApple OSS Distributions 		*events = 1;
351*27b03b36SApple OSS Distributions 	} else {
352*27b03b36SApple OSS Distributions 		/* this will unblock the waiter */
353*27b03b36SApple OSS Distributions 		mach_msg_destroy(hdr);
354*27b03b36SApple OSS Distributions 		*events = 0;
355*27b03b36SApple OSS Distributions 
356*27b03b36SApple OSS Distributions 		/*
357*27b03b36SApple OSS Distributions 		 * Destroying the message will send a send-once notification for reply port, once the
358*27b03b36SApple OSS Distributions 		 * send-once notification is consumed (by the waiting thread), only then the actual
359*27b03b36SApple OSS Distributions 		 * send-once right is destroyed and only then the push will go away.
360*27b03b36SApple OSS Distributions 		 */
361*27b03b36SApple OSS Distributions 		T_LOG("Sleeping for 5 seconds so waiting thread is unblocked\n");
362*27b03b36SApple OSS Distributions 		sleep(5);
363*27b03b36SApple OSS Distributions 
364*27b03b36SApple OSS Distributions 		/* now that the message is destroyed, the priority should be gone */
365*27b03b36SApple OSS Distributions 		T_EXPECT_EFFECTIVE_QOS_EQ(QOS_CLASS_MAINTENANCE,
366*27b03b36SApple OSS Distributions 		    "dispatch_source event handler QoS should be QOS_CLASS_MAINTENANCE after destroying message");
367*27b03b36SApple OSS Distributions 		T_EXPECT_LE(get_user_promotion_basepri(), 0u,
368*27b03b36SApple OSS Distributions 		    "dispatch_source event handler should not be overridden after destroying message");
369*27b03b36SApple OSS Distributions 		T_EXPECT_LE(get_thread_base_priority(), 4u,
370*27b03b36SApple OSS Distributions 		    "dispatch_source event handler should have base pri at 4 or less after destroying message");
371*27b03b36SApple OSS Distributions 	}
372*27b03b36SApple OSS Distributions }
373*27b03b36SApple OSS Distributions 
374*27b03b36SApple OSS Distributions static void
run_client_server(const char * server_name,const char * client_name)375*27b03b36SApple OSS Distributions run_client_server(const char *server_name, const char *client_name)
376*27b03b36SApple OSS Distributions {
377*27b03b36SApple OSS Distributions 	dt_helper_t helpers[] = {
378*27b03b36SApple OSS Distributions 		dt_launchd_helper_domain("com.apple.xnu.test.turnstile_multihop.plist",
379*27b03b36SApple OSS Distributions 	    server_name, NULL, LAUNCH_SYSTEM_DOMAIN),
380*27b03b36SApple OSS Distributions 		dt_fork_helper(client_name)
381*27b03b36SApple OSS Distributions 	};
382*27b03b36SApple OSS Distributions 	dt_run_helpers(helpers, 2, HELPER_TIMEOUT_SECS);
383*27b03b36SApple OSS Distributions }
384*27b03b36SApple OSS Distributions 
385*27b03b36SApple OSS Distributions #pragma mark Mach receive
386*27b03b36SApple OSS Distributions 
387*27b03b36SApple OSS Distributions #define TURNSTILE_MULTIHOP_SERVICE_NAME "com.apple.xnu.test.turnstile_multihop"
388*27b03b36SApple OSS Distributions 
389*27b03b36SApple OSS Distributions static mach_port_t
get_server_port(void)390*27b03b36SApple OSS Distributions get_server_port(void)
391*27b03b36SApple OSS Distributions {
392*27b03b36SApple OSS Distributions 	mach_port_t port;
393*27b03b36SApple OSS Distributions 	kern_return_t kr = bootstrap_check_in(bootstrap_port,
394*27b03b36SApple OSS Distributions 	    TURNSTILE_MULTIHOP_SERVICE_NAME, &port);
395*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "server bootstrap_check_in");
396*27b03b36SApple OSS Distributions 	return port;
397*27b03b36SApple OSS Distributions }
398*27b03b36SApple OSS Distributions 
399*27b03b36SApple OSS Distributions static void
send(mach_port_t send_port,mach_port_t reply_port,mach_port_t msg_port,mach_msg_priority_t priority,mach_msg_option_t options)400*27b03b36SApple OSS Distributions send(
401*27b03b36SApple OSS Distributions 	mach_port_t send_port,
402*27b03b36SApple OSS Distributions 	mach_port_t reply_port,
403*27b03b36SApple OSS Distributions 	mach_port_t msg_port,
404*27b03b36SApple OSS Distributions 	mach_msg_priority_t priority,
405*27b03b36SApple OSS Distributions 	mach_msg_option_t options)
406*27b03b36SApple OSS Distributions {
407*27b03b36SApple OSS Distributions 	kern_return_t ret = 0;
408*27b03b36SApple OSS Distributions 
409*27b03b36SApple OSS Distributions 	struct test_msg send_msg = {
410*27b03b36SApple OSS Distributions 		.header = {
411*27b03b36SApple OSS Distributions 			.msgh_remote_port = send_port,
412*27b03b36SApple OSS Distributions 			.msgh_local_port  = reply_port,
413*27b03b36SApple OSS Distributions 			.msgh_bits        = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND,
414*27b03b36SApple OSS Distributions 	    reply_port ? MACH_MSG_TYPE_MAKE_SEND_ONCE : 0,
415*27b03b36SApple OSS Distributions 	    MACH_MSG_TYPE_MOVE_SEND,
416*27b03b36SApple OSS Distributions 	    MACH_MSGH_BITS_COMPLEX),
417*27b03b36SApple OSS Distributions 			.msgh_id          = EXPECTED_MESSAGE_ID,
418*27b03b36SApple OSS Distributions 			.msgh_size        = sizeof(send_msg),
419*27b03b36SApple OSS Distributions 		},
420*27b03b36SApple OSS Distributions 		.body = {
421*27b03b36SApple OSS Distributions 			.msgh_descriptor_count = 1,
422*27b03b36SApple OSS Distributions 		},
423*27b03b36SApple OSS Distributions 		.port_descriptor = {
424*27b03b36SApple OSS Distributions 			.name        = msg_port,
425*27b03b36SApple OSS Distributions 			.disposition = MACH_MSG_TYPE_MOVE_RECEIVE,
426*27b03b36SApple OSS Distributions 			.type        = MACH_MSG_PORT_DESCRIPTOR,
427*27b03b36SApple OSS Distributions 		},
428*27b03b36SApple OSS Distributions 	};
429*27b03b36SApple OSS Distributions 
430*27b03b36SApple OSS Distributions 	ret = mach_msg(&(send_msg.header),
431*27b03b36SApple OSS Distributions 	    MACH_SEND_MSG |
432*27b03b36SApple OSS Distributions 	    MACH_SEND_TIMEOUT |
433*27b03b36SApple OSS Distributions 	    MACH_SEND_OVERRIDE |
434*27b03b36SApple OSS Distributions 	    (test_noimportance ? MACH_SEND_NOIMPORTANCE : 0) |
435*27b03b36SApple OSS Distributions 	    options,
436*27b03b36SApple OSS Distributions 	    send_msg.header.msgh_size,
437*27b03b36SApple OSS Distributions 	    0,
438*27b03b36SApple OSS Distributions 	    MACH_PORT_NULL,
439*27b03b36SApple OSS Distributions 	    10000,
440*27b03b36SApple OSS Distributions 	    priority);
441*27b03b36SApple OSS Distributions 
442*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "client mach_msg");
443*27b03b36SApple OSS Distributions }
444*27b03b36SApple OSS Distributions 
445*27b03b36SApple OSS Distributions static mach_msg_id_t
receive(mach_port_t rcv_port,mach_port_t notify_port)446*27b03b36SApple OSS Distributions receive(
447*27b03b36SApple OSS Distributions 	mach_port_t rcv_port,
448*27b03b36SApple OSS Distributions 	mach_port_t notify_port)
449*27b03b36SApple OSS Distributions {
450*27b03b36SApple OSS Distributions 	struct {
451*27b03b36SApple OSS Distributions 		mach_msg_header_t header;
452*27b03b36SApple OSS Distributions 		mach_msg_body_t body;
453*27b03b36SApple OSS Distributions 		mach_msg_port_descriptor_t port_descriptor;
454*27b03b36SApple OSS Distributions 	} rcv_msg = {
455*27b03b36SApple OSS Distributions 		.header =
456*27b03b36SApple OSS Distributions 		{
457*27b03b36SApple OSS Distributions 			.msgh_remote_port = MACH_PORT_NULL,
458*27b03b36SApple OSS Distributions 			.msgh_local_port  = rcv_port,
459*27b03b36SApple OSS Distributions 			.msgh_size        = sizeof(rcv_msg),
460*27b03b36SApple OSS Distributions 		},
461*27b03b36SApple OSS Distributions 	};
462*27b03b36SApple OSS Distributions 
463*27b03b36SApple OSS Distributions 	T_LOG("Client: Starting sync receive\n");
464*27b03b36SApple OSS Distributions 
465*27b03b36SApple OSS Distributions 	kern_return_t kr;
466*27b03b36SApple OSS Distributions 	kr = mach_msg(&(rcv_msg.header),
467*27b03b36SApple OSS Distributions 	    MACH_RCV_MSG |
468*27b03b36SApple OSS Distributions 	    MACH_RCV_SYNC_WAIT,
469*27b03b36SApple OSS Distributions 	    0,
470*27b03b36SApple OSS Distributions 	    rcv_msg.header.msgh_size,
471*27b03b36SApple OSS Distributions 	    rcv_port,
472*27b03b36SApple OSS Distributions 	    0,
473*27b03b36SApple OSS Distributions 	    notify_port);
474*27b03b36SApple OSS Distributions 
475*27b03b36SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_msg rcv");
476*27b03b36SApple OSS Distributions 
477*27b03b36SApple OSS Distributions 	return rcv_msg.header.msgh_id;
478*27b03b36SApple OSS Distributions }
479*27b03b36SApple OSS Distributions 
480*27b03b36SApple OSS Distributions static lock_t lock_DEF;
481*27b03b36SApple OSS Distributions static lock_t lock_IN;
482*27b03b36SApple OSS Distributions static lock_t lock_UI;
483*27b03b36SApple OSS Distributions 
484*27b03b36SApple OSS Distributions static mach_port_t main_thread_port;
485*27b03b36SApple OSS Distributions static mach_port_t def_thread_port;
486*27b03b36SApple OSS Distributions static mach_port_t in_thread_port;
487*27b03b36SApple OSS Distributions static mach_port_t ui_thread_port;
488*27b03b36SApple OSS Distributions static mach_port_t sixty_thread_port;
489*27b03b36SApple OSS Distributions 
490*27b03b36SApple OSS Distributions static uint64_t dispatch_sync_owner;
491*27b03b36SApple OSS Distributions 
492*27b03b36SApple OSS Distributions static int
get_pri(thread_t thread_port)493*27b03b36SApple OSS Distributions get_pri(thread_t thread_port)
494*27b03b36SApple OSS Distributions {
495*27b03b36SApple OSS Distributions 	kern_return_t kr;
496*27b03b36SApple OSS Distributions 
497*27b03b36SApple OSS Distributions 	thread_extended_info_data_t extended_info;
498*27b03b36SApple OSS Distributions 	mach_msg_type_number_t count = THREAD_EXTENDED_INFO_COUNT;
499*27b03b36SApple OSS Distributions 	kr = thread_info(thread_port, THREAD_EXTENDED_INFO,
500*27b03b36SApple OSS Distributions 	    (thread_info_t)&extended_info, &count);
501*27b03b36SApple OSS Distributions 
502*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info");
503*27b03b36SApple OSS Distributions 
504*27b03b36SApple OSS Distributions 	return extended_info.pth_curpri;
505*27b03b36SApple OSS Distributions }
506*27b03b36SApple OSS Distributions 
507*27b03b36SApple OSS Distributions static void
set_thread_name(const char * fn_name)508*27b03b36SApple OSS Distributions set_thread_name(const char *fn_name)
509*27b03b36SApple OSS Distributions {
510*27b03b36SApple OSS Distributions 	char name[50] = "";
511*27b03b36SApple OSS Distributions 
512*27b03b36SApple OSS Distributions 	thread_t thread_port = pthread_mach_thread_np(pthread_self());
513*27b03b36SApple OSS Distributions 
514*27b03b36SApple OSS Distributions 	int pri = get_pri(thread_port);
515*27b03b36SApple OSS Distributions 
516*27b03b36SApple OSS Distributions 	snprintf(name, sizeof(name), "%s at pri %2d", fn_name, pri);
517*27b03b36SApple OSS Distributions 	pthread_setname_np(name);
518*27b03b36SApple OSS Distributions }
519*27b03b36SApple OSS Distributions 
520*27b03b36SApple OSS Distributions static void
thread_wait_to_block(mach_port_t thread_port)521*27b03b36SApple OSS Distributions thread_wait_to_block(mach_port_t thread_port)
522*27b03b36SApple OSS Distributions {
523*27b03b36SApple OSS Distributions 	thread_extended_info_data_t extended_info;
524*27b03b36SApple OSS Distributions 	kern_return_t kr;
525*27b03b36SApple OSS Distributions 
526*27b03b36SApple OSS Distributions 	while (1) {
527*27b03b36SApple OSS Distributions 		mach_msg_type_number_t count = THREAD_EXTENDED_INFO_COUNT;
528*27b03b36SApple OSS Distributions 		kr = thread_info(thread_port, THREAD_EXTENDED_INFO,
529*27b03b36SApple OSS Distributions 		    (thread_info_t)&extended_info, &count);
530*27b03b36SApple OSS Distributions 
531*27b03b36SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info");
532*27b03b36SApple OSS Distributions 
533*27b03b36SApple OSS Distributions 		if (extended_info.pth_run_state == TH_STATE_WAITING) {
534*27b03b36SApple OSS Distributions 			T_LOG("Target thread blocked\n");
535*27b03b36SApple OSS Distributions 			break;
536*27b03b36SApple OSS Distributions 		}
537*27b03b36SApple OSS Distributions 		thread_switch(thread_port, SWITCH_OPTION_DEPRESS, 0);
538*27b03b36SApple OSS Distributions 	}
539*27b03b36SApple OSS Distributions }
540*27b03b36SApple OSS Distributions 
541*27b03b36SApple OSS Distributions static void
thread_wait_to_boost(mach_port_t thread_port,mach_port_t yield_thread,int priority)542*27b03b36SApple OSS Distributions thread_wait_to_boost(mach_port_t thread_port, mach_port_t yield_thread, int priority)
543*27b03b36SApple OSS Distributions {
544*27b03b36SApple OSS Distributions 	thread_extended_info_data_t extended_info;
545*27b03b36SApple OSS Distributions 	kern_return_t kr;
546*27b03b36SApple OSS Distributions 
547*27b03b36SApple OSS Distributions 	while (1) {
548*27b03b36SApple OSS Distributions 		mach_msg_type_number_t count = THREAD_EXTENDED_INFO_COUNT;
549*27b03b36SApple OSS Distributions 		kr = thread_info(thread_port, THREAD_EXTENDED_INFO,
550*27b03b36SApple OSS Distributions 		    (thread_info_t)&extended_info, &count);
551*27b03b36SApple OSS Distributions 
552*27b03b36SApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info");
553*27b03b36SApple OSS Distributions 
554*27b03b36SApple OSS Distributions 		if (extended_info.pth_priority >= priority) {
555*27b03b36SApple OSS Distributions 			T_LOG("Target thread boosted\n");
556*27b03b36SApple OSS Distributions 			break;
557*27b03b36SApple OSS Distributions 		}
558*27b03b36SApple OSS Distributions 		thread_switch(yield_thread, SWITCH_OPTION_DEPRESS, 0);
559*27b03b36SApple OSS Distributions 	}
560*27b03b36SApple OSS Distributions }
561*27b03b36SApple OSS Distributions 
562*27b03b36SApple OSS Distributions static void
dispatch_sync_wait(mach_port_t owner_thread,qos_class_t promote_qos)563*27b03b36SApple OSS Distributions dispatch_sync_wait(mach_port_t owner_thread, qos_class_t promote_qos)
564*27b03b36SApple OSS Distributions {
565*27b03b36SApple OSS Distributions 	struct kevent_qos_s kev_err[] = {{ 0 }};
566*27b03b36SApple OSS Distributions 	uint32_t fflags = 0;
567*27b03b36SApple OSS Distributions 	uint64_t mask = 0;
568*27b03b36SApple OSS Distributions 	uint16_t action = 0;
569*27b03b36SApple OSS Distributions 	int r;
570*27b03b36SApple OSS Distributions 
571*27b03b36SApple OSS Distributions 	action = EV_ADD | EV_DISABLE;
572*27b03b36SApple OSS Distributions 	fflags = NOTE_WL_SYNC_WAIT | NOTE_WL_DISCOVER_OWNER;
573*27b03b36SApple OSS Distributions 
574*27b03b36SApple OSS Distributions 	dispatch_sync_owner = owner_thread;
575*27b03b36SApple OSS Distributions 
576*27b03b36SApple OSS Distributions 	struct kevent_qos_s kev[] =  {{
577*27b03b36SApple OSS Distributions 					      .ident = mach_thread_self(),
578*27b03b36SApple OSS Distributions 					      .filter = EVFILT_WORKLOOP,
579*27b03b36SApple OSS Distributions 					      .flags = action,
580*27b03b36SApple OSS Distributions 					      .fflags = fflags,
581*27b03b36SApple OSS Distributions 					      .udata = (uintptr_t) &def_thread_port,
582*27b03b36SApple OSS Distributions 					      .qos = (int32_t)_pthread_qos_class_encode(promote_qos, 0, 0),
583*27b03b36SApple OSS Distributions 					      .ext[EV_EXTIDX_WL_MASK] = mask,
584*27b03b36SApple OSS Distributions 					      .ext[EV_EXTIDX_WL_VALUE] = dispatch_sync_owner,
585*27b03b36SApple OSS Distributions 					      .ext[EV_EXTIDX_WL_ADDR] = (uint64_t)&dispatch_sync_owner,
586*27b03b36SApple OSS Distributions 				      }};
587*27b03b36SApple OSS Distributions 
588*27b03b36SApple OSS Distributions 	/* Setup workloop to fake dispatch sync wait on a workloop */
589*27b03b36SApple OSS Distributions 	r = kevent_id(30, kev, 1, kev_err, 1, NULL,
590*27b03b36SApple OSS Distributions 	    NULL, KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_ERROR_EVENTS);
591*27b03b36SApple OSS Distributions 	T_QUIET; T_LOG("dispatch_sync_wait returned\n");
592*27b03b36SApple OSS Distributions }
593*27b03b36SApple OSS Distributions 
594*27b03b36SApple OSS Distributions static void
dispatch_sync_cancel(mach_port_t owner_thread,qos_class_t promote_qos)595*27b03b36SApple OSS Distributions dispatch_sync_cancel(mach_port_t owner_thread, qos_class_t promote_qos)
596*27b03b36SApple OSS Distributions {
597*27b03b36SApple OSS Distributions 	struct kevent_qos_s kev_err[] = {{ 0 }};
598*27b03b36SApple OSS Distributions 	uint32_t fflags = 0;
599*27b03b36SApple OSS Distributions 	uint64_t mask = 0;
600*27b03b36SApple OSS Distributions 	uint16_t action = 0;
601*27b03b36SApple OSS Distributions 	int r;
602*27b03b36SApple OSS Distributions 
603*27b03b36SApple OSS Distributions 	action = EV_DELETE | EV_ENABLE;
604*27b03b36SApple OSS Distributions 	fflags = NOTE_WL_SYNC_WAKE | NOTE_WL_END_OWNERSHIP;
605*27b03b36SApple OSS Distributions 
606*27b03b36SApple OSS Distributions 	dispatch_sync_owner = owner_thread;
607*27b03b36SApple OSS Distributions 
608*27b03b36SApple OSS Distributions 	struct kevent_qos_s kev[] =  {{
609*27b03b36SApple OSS Distributions 					      .ident = def_thread_port,
610*27b03b36SApple OSS Distributions 					      .filter = EVFILT_WORKLOOP,
611*27b03b36SApple OSS Distributions 					      .flags = action,
612*27b03b36SApple OSS Distributions 					      .fflags = fflags,
613*27b03b36SApple OSS Distributions 					      .udata = (uintptr_t) &def_thread_port,
614*27b03b36SApple OSS Distributions 					      .qos = (int32_t)_pthread_qos_class_encode(promote_qos, 0, 0),
615*27b03b36SApple OSS Distributions 					      .ext[EV_EXTIDX_WL_MASK] = mask,
616*27b03b36SApple OSS Distributions 					      .ext[EV_EXTIDX_WL_VALUE] = dispatch_sync_owner,
617*27b03b36SApple OSS Distributions 					      .ext[EV_EXTIDX_WL_ADDR] = (uint64_t)&dispatch_sync_owner,
618*27b03b36SApple OSS Distributions 				      }};
619*27b03b36SApple OSS Distributions 
620*27b03b36SApple OSS Distributions 	/* Setup workloop to fake dispatch sync wake on a workloop */
621*27b03b36SApple OSS Distributions 	r = kevent_id(30, kev, 1, kev_err, 1, NULL,
622*27b03b36SApple OSS Distributions 	    NULL, KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_ERROR_EVENTS);
623*27b03b36SApple OSS Distributions 	T_QUIET; T_LOG("dispatch_sync_cancel returned\n");
624*27b03b36SApple OSS Distributions }
625*27b03b36SApple OSS Distributions 
626*27b03b36SApple OSS Distributions static void *
thread_at_sixty(void * arg __unused)627*27b03b36SApple OSS Distributions thread_at_sixty(void *arg __unused)
628*27b03b36SApple OSS Distributions {
629*27b03b36SApple OSS Distributions 	int policy;
630*27b03b36SApple OSS Distributions 	struct sched_param param;
631*27b03b36SApple OSS Distributions 	int ret;
632*27b03b36SApple OSS Distributions 	void *load_token;
633*27b03b36SApple OSS Distributions 	uint64_t before_lock_time, after_lock_time;
634*27b03b36SApple OSS Distributions 
635*27b03b36SApple OSS Distributions 	sixty_thread_port = mach_thread_self();
636*27b03b36SApple OSS Distributions 
637*27b03b36SApple OSS Distributions 	set_thread_name(__FUNCTION__);
638*27b03b36SApple OSS Distributions 
639*27b03b36SApple OSS Distributions 	/* Change our priority to 60 */
640*27b03b36SApple OSS Distributions 	ret = pthread_getschedparam(pthread_self(), &policy, &param);
641*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "pthread_getschedparam");
642*27b03b36SApple OSS Distributions 
643*27b03b36SApple OSS Distributions 	param.sched_priority = 60;
644*27b03b36SApple OSS Distributions 
645*27b03b36SApple OSS Distributions 	ret = pthread_setschedparam(pthread_self(), policy, &param);
646*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "pthread_setschedparam");
647*27b03b36SApple OSS Distributions 
648*27b03b36SApple OSS Distributions 	ret = pthread_getschedparam(pthread_self(), &policy, &param);
649*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "pthread_getschedparam");
650*27b03b36SApple OSS Distributions 
651*27b03b36SApple OSS Distributions 	T_LOG("My priority is %d", param.sched_priority);
652*27b03b36SApple OSS Distributions 
653*27b03b36SApple OSS Distributions 	thread_wait_to_boost(in_thread_port, ui_thread_port, 46);
654*27b03b36SApple OSS Distributions 
655*27b03b36SApple OSS Distributions 	if (spin_for_ever) {
656*27b03b36SApple OSS Distributions 		/* Schedule load at Default */
657*27b03b36SApple OSS Distributions 		sched_create_load_at_qos(QOS_CLASS_DEFAULT, &load_token);
658*27b03b36SApple OSS Distributions 	}
659*27b03b36SApple OSS Distributions 
660*27b03b36SApple OSS Distributions 	T_LOG("Thread at priority 60 trying to acquire UI lock");
661*27b03b36SApple OSS Distributions 
662*27b03b36SApple OSS Distributions 	before_lock_time = mach_absolute_time();
663*27b03b36SApple OSS Distributions 	ull_lock(&lock_UI, 3, UL_UNFAIR_LOCK, 0);
664*27b03b36SApple OSS Distributions 	after_lock_time = mach_absolute_time();
665*27b03b36SApple OSS Distributions 
666*27b03b36SApple OSS Distributions 	T_QUIET; T_LOG("The time for priority 60 thread to acquire lock was %llu \n",
667*27b03b36SApple OSS Distributions 	    (after_lock_time - before_lock_time));
668*27b03b36SApple OSS Distributions 
669*27b03b36SApple OSS Distributions 	T_LOG("Wait for 5 seconds for the server to terminate\n");
670*27b03b36SApple OSS Distributions 	sleep(5);
671*27b03b36SApple OSS Distributions 	T_END;
672*27b03b36SApple OSS Distributions }
673*27b03b36SApple OSS Distributions 
674*27b03b36SApple OSS Distributions static void *
thread_at_ui(void * arg __unused)675*27b03b36SApple OSS Distributions thread_at_ui(void *arg __unused)
676*27b03b36SApple OSS Distributions {
677*27b03b36SApple OSS Distributions 	ui_thread_port = mach_thread_self();
678*27b03b36SApple OSS Distributions 
679*27b03b36SApple OSS Distributions 	set_thread_name(__FUNCTION__);
680*27b03b36SApple OSS Distributions 
681*27b03b36SApple OSS Distributions 	/* Grab the first ulock */
682*27b03b36SApple OSS Distributions 	ull_lock(&lock_UI, 2, UL_UNFAIR_LOCK, 0);
683*27b03b36SApple OSS Distributions 
684*27b03b36SApple OSS Distributions 	thread_wait_to_boost(def_thread_port, in_thread_port, 37);
685*27b03b36SApple OSS Distributions 	thread_create_at_qos(QOS_CLASS_USER_INTERACTIVE, thread_at_sixty);
686*27b03b36SApple OSS Distributions 
687*27b03b36SApple OSS Distributions 	T_EXPECT_GE(get_thread_base_priority(), 46u,
688*27b03b36SApple OSS Distributions 	    "thread_at_ui should have base pri 46 or greater");
689*27b03b36SApple OSS Distributions 
690*27b03b36SApple OSS Distributions 	T_LOG("Thread at UI priority trying to acquire IN lock");
691*27b03b36SApple OSS Distributions 	ull_lock(&lock_IN, 2, UL_UNFAIR_LOCK, 0);
692*27b03b36SApple OSS Distributions 	ull_unlock(&lock_UI, 2, UL_UNFAIR_LOCK, 0);
693*27b03b36SApple OSS Distributions 	return NULL;
694*27b03b36SApple OSS Distributions }
695*27b03b36SApple OSS Distributions 
696*27b03b36SApple OSS Distributions static void *
thread_at_in(void * arg __unused)697*27b03b36SApple OSS Distributions thread_at_in(void *arg __unused)
698*27b03b36SApple OSS Distributions {
699*27b03b36SApple OSS Distributions 	in_thread_port = mach_thread_self();
700*27b03b36SApple OSS Distributions 
701*27b03b36SApple OSS Distributions 	set_thread_name(__FUNCTION__);
702*27b03b36SApple OSS Distributions 
703*27b03b36SApple OSS Distributions 	/* Grab the first ulock */
704*27b03b36SApple OSS Distributions 	ull_lock(&lock_IN, 2, UL_UNFAIR_LOCK, 0);
705*27b03b36SApple OSS Distributions 
706*27b03b36SApple OSS Distributions 	T_LOG("Thread at IN priority got first lock ");
707*27b03b36SApple OSS Distributions 
708*27b03b36SApple OSS Distributions 	thread_wait_to_boost(main_thread_port, def_thread_port, 31);
709*27b03b36SApple OSS Distributions 
710*27b03b36SApple OSS Distributions 	/* Create a new thread at QOS_CLASS_USER_INTERACTIVE qos */
711*27b03b36SApple OSS Distributions 	thread_create_at_qos(QOS_CLASS_USER_INTERACTIVE, thread_at_ui);
712*27b03b36SApple OSS Distributions 
713*27b03b36SApple OSS Distributions 	T_LOG("Thread at IN priority trying to acquire default lock");
714*27b03b36SApple OSS Distributions 	ull_lock(&lock_DEF, 1, UL_UNFAIR_LOCK, 0);
715*27b03b36SApple OSS Distributions 	ull_unlock(&lock_IN, 2, UL_UNFAIR_LOCK, 0);
716*27b03b36SApple OSS Distributions 	return NULL;
717*27b03b36SApple OSS Distributions }
718*27b03b36SApple OSS Distributions 
719*27b03b36SApple OSS Distributions static void *
thread_at_default(void * arg __unused)720*27b03b36SApple OSS Distributions thread_at_default(void *arg __unused)
721*27b03b36SApple OSS Distributions {
722*27b03b36SApple OSS Distributions 	def_thread_port = mach_thread_self();
723*27b03b36SApple OSS Distributions 
724*27b03b36SApple OSS Distributions 	set_thread_name(__FUNCTION__);
725*27b03b36SApple OSS Distributions 
726*27b03b36SApple OSS Distributions 	/* Grab the first ulock */
727*27b03b36SApple OSS Distributions 	ull_lock(&lock_DEF, 1, UL_UNFAIR_LOCK, 0);
728*27b03b36SApple OSS Distributions 
729*27b03b36SApple OSS Distributions 	T_LOG("Thread at DEFAULT priority got first lock ");
730*27b03b36SApple OSS Distributions 
731*27b03b36SApple OSS Distributions 	thread_wait_to_block(main_thread_port);
732*27b03b36SApple OSS Distributions 
733*27b03b36SApple OSS Distributions 	/* Create a new thread at QOS_CLASS_USER_INITIATED qos */
734*27b03b36SApple OSS Distributions 	thread_create_at_qos(QOS_CLASS_USER_INITIATED, thread_at_in);
735*27b03b36SApple OSS Distributions 
736*27b03b36SApple OSS Distributions 	T_LOG("Thread at Default priority trying to wait on dispatch sync for maintenance thread");
737*27b03b36SApple OSS Distributions 	dispatch_sync_wait(main_thread_port, QOS_CLASS_DEFAULT);
738*27b03b36SApple OSS Distributions 	ull_unlock(&lock_DEF, 1, UL_UNFAIR_LOCK, 0);
739*27b03b36SApple OSS Distributions 	return NULL;
740*27b03b36SApple OSS Distributions }
741*27b03b36SApple OSS Distributions 
742*27b03b36SApple OSS Distributions static void *
thread_at_maintenance(void * arg __unused)743*27b03b36SApple OSS Distributions thread_at_maintenance(void *arg __unused)
744*27b03b36SApple OSS Distributions {
745*27b03b36SApple OSS Distributions 	mach_port_t service_port;
746*27b03b36SApple OSS Distributions 	mach_port_t conn_port;
747*27b03b36SApple OSS Distributions 	mach_port_t special_reply_port;
748*27b03b36SApple OSS Distributions 	mach_port_options_t opts = {
749*27b03b36SApple OSS Distributions 		.flags = MPO_INSERT_SEND_RIGHT,
750*27b03b36SApple OSS Distributions 	};
751*27b03b36SApple OSS Distributions 
752*27b03b36SApple OSS Distributions 	main_thread_port = mach_thread_self();
753*27b03b36SApple OSS Distributions 
754*27b03b36SApple OSS Distributions 	set_thread_name(__FUNCTION__);
755*27b03b36SApple OSS Distributions 
756*27b03b36SApple OSS Distributions 	kern_return_t kr = bootstrap_look_up(bootstrap_port,
757*27b03b36SApple OSS Distributions 	    TURNSTILE_MULTIHOP_SERVICE_NAME, &service_port);
758*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
759*27b03b36SApple OSS Distributions 
760*27b03b36SApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts, 0ull, &conn_port);
761*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct");
762*27b03b36SApple OSS Distributions 
763*27b03b36SApple OSS Distributions 	special_reply_port = thread_get_special_reply_port();
764*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
765*27b03b36SApple OSS Distributions 
766*27b03b36SApple OSS Distributions 	/* Become the dispatch sync owner, dispatch_sync_owner will be set in dispatch_sync_wait function */
767*27b03b36SApple OSS Distributions 
768*27b03b36SApple OSS Distributions 	/* Send a sync message */
769*27b03b36SApple OSS Distributions 	send(conn_port, special_reply_port, MACH_PORT_NULL,
770*27b03b36SApple OSS Distributions 	    mach_msg_priority_encode(0, THREAD_QOS_MAINTENANCE, 0), 0);
771*27b03b36SApple OSS Distributions 
772*27b03b36SApple OSS Distributions 	/* Send an async checkin message */
773*27b03b36SApple OSS Distributions 	send(service_port, MACH_PORT_NULL, conn_port,
774*27b03b36SApple OSS Distributions 	    mach_msg_priority_encode(0, THREAD_QOS_MAINTENANCE, 0), 0);
775*27b03b36SApple OSS Distributions 
776*27b03b36SApple OSS Distributions 	/* Create a new thread at QOS_CLASS_DEFAULT qos */
777*27b03b36SApple OSS Distributions 	thread_create_at_qos(QOS_CLASS_DEFAULT, thread_at_default);
778*27b03b36SApple OSS Distributions 
779*27b03b36SApple OSS Distributions 	/* Block on Sync IPC */
780*27b03b36SApple OSS Distributions 	mach_msg_id_t message_id = receive(special_reply_port, conn_port);
781*27b03b36SApple OSS Distributions 
782*27b03b36SApple OSS Distributions 	T_ASSERT_EQ(message_id, MACH_NOTIFY_SEND_ONCE, "got the expected send-once notification");
783*27b03b36SApple OSS Distributions 
784*27b03b36SApple OSS Distributions 	T_LOG("received reply");
785*27b03b36SApple OSS Distributions 
786*27b03b36SApple OSS Distributions 	dispatch_sync_cancel(def_thread_port, QOS_CLASS_DEFAULT);
787*27b03b36SApple OSS Distributions 	return NULL;
788*27b03b36SApple OSS Distributions }
789*27b03b36SApple OSS Distributions 
790*27b03b36SApple OSS Distributions T_HELPER_DECL(three_ulock_sync_ipc_hop,
791*27b03b36SApple OSS Distributions     "Create chain of 4 threads with 3 ulocks and 1 sync IPC at different qos")
792*27b03b36SApple OSS Distributions {
793*27b03b36SApple OSS Distributions 	thread_create_at_qos(QOS_CLASS_MAINTENANCE, thread_at_maintenance);
794*27b03b36SApple OSS Distributions 	sigsuspend(0);
795*27b03b36SApple OSS Distributions }
796*27b03b36SApple OSS Distributions 
797*27b03b36SApple OSS Distributions T_HELPER_DECL(three_ulock_sync_ipc_hop_noimportance,
798*27b03b36SApple OSS Distributions     "Create chain of 4 threads with 3 ulocks and 1 no-importance sync IPC at different qos")
799*27b03b36SApple OSS Distributions {
800*27b03b36SApple OSS Distributions 	test_noimportance = true;
801*27b03b36SApple OSS Distributions 	thread_create_at_qos(QOS_CLASS_MAINTENANCE, thread_at_maintenance);
802*27b03b36SApple OSS Distributions 	sigsuspend(0);
803*27b03b36SApple OSS Distributions }
804*27b03b36SApple OSS Distributions 
805*27b03b36SApple OSS Distributions 
806*27b03b36SApple OSS Distributions static void
thread_create_at_qos(qos_class_t qos,void * (* function)(void *))807*27b03b36SApple OSS Distributions thread_create_at_qos(qos_class_t qos, void * (*function)(void *))
808*27b03b36SApple OSS Distributions {
809*27b03b36SApple OSS Distributions 	qos_class_t qos_thread;
810*27b03b36SApple OSS Distributions 	pthread_t thread;
811*27b03b36SApple OSS Distributions 	pthread_attr_t attr;
812*27b03b36SApple OSS Distributions 	int ret;
813*27b03b36SApple OSS Distributions 
814*27b03b36SApple OSS Distributions 	ret = setpriority(PRIO_DARWIN_ROLE, 0, PRIO_DARWIN_ROLE_UI_FOCAL);
815*27b03b36SApple OSS Distributions 	if (ret != 0) {
816*27b03b36SApple OSS Distributions 		T_LOG("set priority failed\n");
817*27b03b36SApple OSS Distributions 	}
818*27b03b36SApple OSS Distributions 
819*27b03b36SApple OSS Distributions 	pthread_attr_init(&attr);
820*27b03b36SApple OSS Distributions 	pthread_attr_set_qos_class_np(&attr, qos, 0);
821*27b03b36SApple OSS Distributions 	pthread_create(&thread, &attr, function, NULL);
822*27b03b36SApple OSS Distributions 
823*27b03b36SApple OSS Distributions 	T_LOG("pthread created\n");
824*27b03b36SApple OSS Distributions 	pthread_get_qos_class_np(thread, &qos_thread, NULL);
825*27b03b36SApple OSS Distributions }
826*27b03b36SApple OSS Distributions 
827*27b03b36SApple OSS Distributions #pragma mark Mach receive - kevent_qos
828*27b03b36SApple OSS Distributions 
829*27b03b36SApple OSS Distributions T_HELPER_DECL(server_kevent_id,
830*27b03b36SApple OSS Distributions     "Reply with the QoS that a dispatch source event handler ran with")
831*27b03b36SApple OSS Distributions {
832*27b03b36SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
833*27b03b36SApple OSS Distributions 		    worker_cb, event_cb,
834*27b03b36SApple OSS Distributions 		    (pthread_workqueue_function_workloop_t)workloop_cb_test_intransit, 0, 0), NULL);
835*27b03b36SApple OSS Distributions 
836*27b03b36SApple OSS Distributions 	register_port(LISTENER_WLID, get_server_port());
837*27b03b36SApple OSS Distributions 	sigsuspend(0);
838*27b03b36SApple OSS Distributions 	T_ASSERT_FAIL("should receive a message");
839*27b03b36SApple OSS Distributions }
840*27b03b36SApple OSS Distributions 
841*27b03b36SApple OSS Distributions #define TEST_MULTIHOP(server_name, client_name, name) \
842*27b03b36SApple OSS Distributions 	T_DECL(server_kevent_id_##name, \
843*27b03b36SApple OSS Distributions 	                "Event delivery using a kevent_id", \
844*27b03b36SApple OSS Distributions 	                T_META_ASROOT(YES)) \
845*27b03b36SApple OSS Distributions 	{ \
846*27b03b36SApple OSS Distributions 	        run_client_server(server_name, client_name); \
847*27b03b36SApple OSS Distributions 	}
848*27b03b36SApple OSS Distributions 
849*27b03b36SApple OSS Distributions #define TEST_MULTIHOP_SPIN(server_name, client_name, name) \
850*27b03b36SApple OSS Distributions 	T_DECL(server_kevent_id_##name, \
851*27b03b36SApple OSS Distributions 	                "Event delivery using a kevent_id", \
852*27b03b36SApple OSS Distributions 	                T_META_ASROOT(YES), T_META_ENABLED(FALSE)) \
853*27b03b36SApple OSS Distributions 	{ \
854*27b03b36SApple OSS Distributions 	        spin_for_ever = true; \
855*27b03b36SApple OSS Distributions 	        run_client_server(server_name, client_name); \
856*27b03b36SApple OSS Distributions 	        spin_for_ever = false; \
857*27b03b36SApple OSS Distributions 	}
858*27b03b36SApple OSS Distributions 
859*27b03b36SApple OSS Distributions /*
860*27b03b36SApple OSS Distributions  * Test 1: Test multihop priority boosting with ulocks, dispatch sync and sync IPC.
861*27b03b36SApple OSS Distributions  *
862*27b03b36SApple OSS Distributions  * Create thread's at different Qos and acquire a ulock and block on next ulock/dispatch sync
863*27b03b36SApple OSS Distributions  * creating a sync chain. The last hop the chain is blocked on Sync IPC.
864*27b03b36SApple OSS Distributions  */
865*27b03b36SApple OSS Distributions TEST_MULTIHOP("server_kevent_id", "three_ulock_sync_ipc_hop", three_ulock_sync_ipc_hop)
866*27b03b36SApple OSS Distributions 
867*27b03b36SApple OSS Distributions TEST_MULTIHOP("server_kevent_id", "three_ulock_sync_ipc_hop_noimportance", three_ulock_sync_ipc_hop_noimportance)
868*27b03b36SApple OSS Distributions 
869*27b03b36SApple OSS Distributions /*
870*27b03b36SApple OSS Distributions  * Test 2: Test multihop priority boosting with ulocks, dispatch sync and sync IPC.
871*27b03b36SApple OSS Distributions  *
872*27b03b36SApple OSS Distributions  * Create thread's at different Qos and acquire a ulock and block on next ulock/dispatch sync
873*27b03b36SApple OSS Distributions  * creating a sync chain. The last hop the chain is blocked on Sync IPC.
874*27b03b36SApple OSS Distributions  * Before the last priority 60 thread blocks on ulock, it also starts spinforeverd at priority 31.
875*27b03b36SApple OSS Distributions  */
876*27b03b36SApple OSS Distributions TEST_MULTIHOP_SPIN("server_kevent_id", "three_ulock_sync_ipc_hop", three_ulock_sync_ipc_hop_spin)
877