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