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