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