xref: /xnu-11215.1.10/tests/kevent_qos.c (revision 8d741a5de7ff4191bf97d57b9f54c2f6d4a15585)
1*8d741a5dSApple OSS Distributions /*
2*8d741a5dSApple OSS Distributions  * kevent_qos: Tests Synchronous IPC QOS override.
3*8d741a5dSApple OSS Distributions  */
4*8d741a5dSApple OSS Distributions 
5*8d741a5dSApple OSS Distributions #ifdef T_NAMESPACE
6*8d741a5dSApple OSS Distributions #undef T_NAMESPACE
7*8d741a5dSApple OSS Distributions #endif
8*8d741a5dSApple OSS Distributions 
9*8d741a5dSApple OSS Distributions #include <darwintest.h>
10*8d741a5dSApple OSS Distributions #include <darwintest_multiprocess.h>
11*8d741a5dSApple OSS Distributions 
12*8d741a5dSApple OSS Distributions #include <dispatch/dispatch.h>
13*8d741a5dSApple OSS Distributions #include <pthread.h>
14*8d741a5dSApple OSS Distributions #include <launch.h>
15*8d741a5dSApple OSS Distributions #include <mach/mach.h>
16*8d741a5dSApple OSS Distributions #include <mach/message.h>
17*8d741a5dSApple OSS Distributions #include <mach/mach_voucher.h>
18*8d741a5dSApple OSS Distributions #include <pthread/workqueue_private.h>
19*8d741a5dSApple OSS Distributions #include <voucher/ipc_pthread_priority_types.h>
20*8d741a5dSApple OSS Distributions #include <servers/bootstrap.h>
21*8d741a5dSApple OSS Distributions #include <stdlib.h>
22*8d741a5dSApple OSS Distributions #include <sys/event.h>
23*8d741a5dSApple OSS Distributions #include <unistd.h>
24*8d741a5dSApple OSS Distributions #include <crt_externs.h>
25*8d741a5dSApple OSS Distributions #include <mach/mach_port.h>
26*8d741a5dSApple OSS Distributions #include <mach/mach_sync_ipc.h>
27*8d741a5dSApple OSS Distributions 
28*8d741a5dSApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.kevent_qos"),
29*8d741a5dSApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
30*8d741a5dSApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("kevent"));
31*8d741a5dSApple OSS Distributions 
32*8d741a5dSApple OSS Distributions #define ARRAYLEN(arr) (sizeof(arr) / sizeof(arr[0]))
33*8d741a5dSApple OSS Distributions 
34*8d741a5dSApple OSS Distributions #define INTERMITTENT_TIMEOUT_SEC (3)
35*8d741a5dSApple OSS Distributions #define RECV_TIMEOUT_SECS   (4)
36*8d741a5dSApple OSS Distributions #define SEND_TIMEOUT_SECS   (6)
37*8d741a5dSApple OSS Distributions #define HELPER_TIMEOUT_SECS (15)
38*8d741a5dSApple OSS Distributions 
39*8d741a5dSApple OSS Distributions #define ENV_VAR_QOS (3)
40*8d741a5dSApple OSS Distributions static const char *qos_env[ENV_VAR_QOS] = {"XNU_TEST_QOS_BO", "XNU_TEST_QOS_QO", "XNU_TEST_QOS_AO"};
41*8d741a5dSApple OSS Distributions static const char *qos_name_env[ENV_VAR_QOS] = {"XNU_TEST_QOS_NAME_BO", "XNU_TEST_QOS_NAME_QO", "XNU_TEST_QOS_NAME_AO"};
42*8d741a5dSApple OSS Distributions 
43*8d741a5dSApple OSS Distributions #define ENV_VAR_FUNCTION (1)
44*8d741a5dSApple OSS Distributions static const char *wl_function_name = "XNU_TEST_WL_FUNCTION";
45*8d741a5dSApple OSS Distributions 
46*8d741a5dSApple OSS Distributions static qos_class_t g_expected_qos[ENV_VAR_QOS];
47*8d741a5dSApple OSS Distributions static const char *g_expected_qos_name[ENV_VAR_QOS];
48*8d741a5dSApple OSS Distributions 
49*8d741a5dSApple OSS Distributions #define ENV_QOS_BEFORE_OVERRIDE (0)
50*8d741a5dSApple OSS Distributions #define ENV_QOS_QUEUE_OVERRIDE  (1)
51*8d741a5dSApple OSS Distributions #define ENV_QOS_AFTER_OVERRIDE  (2)
52*8d741a5dSApple OSS Distributions 
53*8d741a5dSApple OSS Distributions #define USR_THROTTLE_LEVEL_TIER0 0
54*8d741a5dSApple OSS Distributions #define USR_THROTTLE_LEVEL_TIER1 1
55*8d741a5dSApple OSS Distributions 
56*8d741a5dSApple OSS Distributions struct test_msg {
57*8d741a5dSApple OSS Distributions 	mach_msg_header_t header;
58*8d741a5dSApple OSS Distributions 	mach_msg_body_t body;
59*8d741a5dSApple OSS Distributions 	mach_msg_port_descriptor_t port_descriptor;
60*8d741a5dSApple OSS Distributions 	mach_msg_option_t opts;
61*8d741a5dSApple OSS Distributions 	mach_msg_priority_t qos;
62*8d741a5dSApple OSS Distributions };
63*8d741a5dSApple OSS Distributions 
64*8d741a5dSApple OSS Distributions #pragma mark pthread callbacks
65*8d741a5dSApple OSS Distributions 
66*8d741a5dSApple OSS Distributions static pthread_t
67*8d741a5dSApple OSS Distributions thread_create_at_qos(qos_class_t qos, void * (*function)(void *));
68*8d741a5dSApple OSS Distributions static void
69*8d741a5dSApple OSS Distributions send(mach_port_t send_port, mach_port_t reply_port, mach_port_t msg_port, qos_class_t qos, mach_msg_option_t options);
70*8d741a5dSApple OSS Distributions static void
71*8d741a5dSApple OSS Distributions enable_kevent(uint64_t *workloop_id, unsigned long long port);
72*8d741a5dSApple OSS Distributions static void
73*8d741a5dSApple OSS Distributions populate_kevent(struct kevent_qos_s *kev, unsigned long long port);
74*8d741a5dSApple OSS Distributions 
75*8d741a5dSApple OSS Distributions static void
worker_cb(pthread_priority_t __unused priority)76*8d741a5dSApple OSS Distributions worker_cb(pthread_priority_t __unused priority)
77*8d741a5dSApple OSS Distributions {
78*8d741a5dSApple OSS Distributions 	T_FAIL("a worker thread was created");
79*8d741a5dSApple OSS Distributions }
80*8d741a5dSApple OSS Distributions 
81*8d741a5dSApple OSS Distributions static void
event_cb(void ** __unused events,int * __unused nevents)82*8d741a5dSApple OSS Distributions event_cb(void ** __unused events, int * __unused nevents)
83*8d741a5dSApple OSS Distributions {
84*8d741a5dSApple OSS Distributions 	T_FAIL("a kevent routine was called instead of workloop");
85*8d741a5dSApple OSS Distributions }
86*8d741a5dSApple OSS Distributions 
87*8d741a5dSApple OSS Distributions static uint32_t
get_user_promotion_basepri(void)88*8d741a5dSApple OSS Distributions get_user_promotion_basepri(void)
89*8d741a5dSApple OSS Distributions {
90*8d741a5dSApple OSS Distributions 	mach_msg_type_number_t count = THREAD_POLICY_STATE_COUNT;
91*8d741a5dSApple OSS Distributions 	struct thread_policy_state thread_policy;
92*8d741a5dSApple OSS Distributions 	boolean_t get_default = FALSE;
93*8d741a5dSApple OSS Distributions 	mach_port_t thread_port = pthread_mach_thread_np(pthread_self());
94*8d741a5dSApple OSS Distributions 
95*8d741a5dSApple OSS Distributions 	kern_return_t kr = thread_policy_get(thread_port, THREAD_POLICY_STATE,
96*8d741a5dSApple OSS Distributions 	    (thread_policy_t)&thread_policy, &count, &get_default);
97*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_policy_get");
98*8d741a5dSApple OSS Distributions 	return thread_policy.thps_user_promotion_basepri;
99*8d741a5dSApple OSS Distributions }
100*8d741a5dSApple OSS Distributions 
101*8d741a5dSApple OSS Distributions static uint32_t
get_thread_iotier(void)102*8d741a5dSApple OSS Distributions get_thread_iotier(void)
103*8d741a5dSApple OSS Distributions {
104*8d741a5dSApple OSS Distributions 	mach_msg_type_number_t count = THREAD_POLICY_STATE_COUNT;
105*8d741a5dSApple OSS Distributions 	struct thread_policy_state thread_policy;
106*8d741a5dSApple OSS Distributions 	boolean_t get_default = FALSE;
107*8d741a5dSApple OSS Distributions 	mach_port_t thread_port = pthread_mach_thread_np(pthread_self());
108*8d741a5dSApple OSS Distributions 
109*8d741a5dSApple OSS Distributions 	kern_return_t kr = thread_policy_get(thread_port, THREAD_POLICY_STATE,
110*8d741a5dSApple OSS Distributions 	    (thread_policy_t)&thread_policy, &count, &get_default);
111*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_policy_get");
112*8d741a5dSApple OSS Distributions 	return (thread_policy.effective >> POLICY_EFF_IO_TIER_SHIFT) & POLICY_EFF_IO_TIER_MASK;
113*8d741a5dSApple OSS Distributions }
114*8d741a5dSApple OSS Distributions 
115*8d741a5dSApple OSS Distributions #define EXPECT_QOS_EQ(qos, ...) do { \
116*8d741a5dSApple OSS Distributions 	        if ((qos) == QOS_CLASS_USER_INTERACTIVE) { \
117*8d741a5dSApple OSS Distributions 	                T_EXPECT_EFFECTIVE_QOS_EQ(QOS_CLASS_USER_INITIATED, __VA_ARGS__); \
118*8d741a5dSApple OSS Distributions 	                T_EXPECT_EQ(47u, get_user_promotion_basepri(), __VA_ARGS__); \
119*8d741a5dSApple OSS Distributions 	        } else { \
120*8d741a5dSApple OSS Distributions 	                T_EXPECT_EFFECTIVE_QOS_EQ(qos, __VA_ARGS__); \
121*8d741a5dSApple OSS Distributions 	        } \
122*8d741a5dSApple OSS Distributions 	} while (0)
123*8d741a5dSApple OSS Distributions 
124*8d741a5dSApple OSS Distributions #define EXPECT_TEST_MSG(_ke)  do { \
125*8d741a5dSApple OSS Distributions 	        struct kevent_qos_s *ke = _ke; \
126*8d741a5dSApple OSS Distributions 	        mach_msg_header_t *hdr = (mach_msg_header_t *)ke->ext[0]; \
127*8d741a5dSApple OSS Distributions 	        T_ASSERT_NOTNULL(hdr, "has a message"); \
128*8d741a5dSApple OSS Distributions 	        T_ASSERT_EQ(hdr->msgh_size, (uint32_t)sizeof(struct test_msg), "of the right size"); \
129*8d741a5dSApple OSS Distributions 	        struct test_msg *tmsg = (struct test_msg *)hdr; \
130*8d741a5dSApple OSS Distributions 	        if (tmsg->opts & MACH_SEND_PROPAGATE_QOS) { \
131*8d741a5dSApple OSS Distributions 	                T_EXPECT_EQ(tmsg->qos, ((uint32_t)(ke->ext[2] >> 32)), \
132*8d741a5dSApple OSS Distributions 	                                "propagation works"); \
133*8d741a5dSApple OSS Distributions 	        } \
134*8d741a5dSApple OSS Distributions 	} while (0)
135*8d741a5dSApple OSS Distributions 
136*8d741a5dSApple OSS Distributions /*
137*8d741a5dSApple OSS Distributions  * Basic WL handler callback, it sleeps for n seconds and then checks the
138*8d741a5dSApple OSS Distributions  * effective Qos of the servicer thread.
139*8d741a5dSApple OSS Distributions  */
140*8d741a5dSApple OSS Distributions static void
workloop_cb_test_intransit(uint64_t * workloop_id __unused,void ** eventslist,int * events)141*8d741a5dSApple OSS Distributions workloop_cb_test_intransit(uint64_t *workloop_id __unused, void **eventslist, int *events)
142*8d741a5dSApple OSS Distributions {
143*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_intransit called. "
144*8d741a5dSApple OSS Distributions 	    "Will wait for %d seconds to make sure client enqueues the sync msg \n",
145*8d741a5dSApple OSS Distributions 	    2 * RECV_TIMEOUT_SECS);
146*8d741a5dSApple OSS Distributions 
147*8d741a5dSApple OSS Distributions 	EXPECT_TEST_MSG(*eventslist);
148*8d741a5dSApple OSS Distributions 
149*8d741a5dSApple OSS Distributions 	/* Wait for the client to send the high priority message to override the qos */
150*8d741a5dSApple OSS Distributions 	sleep(2 * RECV_TIMEOUT_SECS);
151*8d741a5dSApple OSS Distributions 
152*8d741a5dSApple OSS Distributions 	/* Skip the test if we can't check Qos */
153*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
154*8d741a5dSApple OSS Distributions 
155*8d741a5dSApple OSS Distributions 	/* The effective Qos should be the one expected after override */
156*8d741a5dSApple OSS Distributions 	EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
157*8d741a5dSApple OSS Distributions 	    "dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
158*8d741a5dSApple OSS Distributions 
159*8d741a5dSApple OSS Distributions 	*events = 0;
160*8d741a5dSApple OSS Distributions 	T_END;
161*8d741a5dSApple OSS Distributions }
162*8d741a5dSApple OSS Distributions 
163*8d741a5dSApple OSS Distributions /*
164*8d741a5dSApple OSS Distributions  * WL handler which checks if the servicer thread has correct Qos.
165*8d741a5dSApple OSS Distributions  */
166*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send(uint64_t * workloop_id __unused,void ** eventslist,int * events)167*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send(uint64_t *workloop_id __unused, void **eventslist, int *events)
168*8d741a5dSApple OSS Distributions {
169*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_sync_send called");
170*8d741a5dSApple OSS Distributions 
171*8d741a5dSApple OSS Distributions 	EXPECT_TEST_MSG(*eventslist);
172*8d741a5dSApple OSS Distributions 
173*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
174*8d741a5dSApple OSS Distributions 
175*8d741a5dSApple OSS Distributions 	/* The effective Qos should be the one expected after override */
176*8d741a5dSApple OSS Distributions 	EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
177*8d741a5dSApple OSS Distributions 	    "dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
178*8d741a5dSApple OSS Distributions 
179*8d741a5dSApple OSS Distributions 	*events = 0;
180*8d741a5dSApple OSS Distributions 	T_END;
181*8d741a5dSApple OSS Distributions }
182*8d741a5dSApple OSS Distributions 
183*8d741a5dSApple OSS Distributions /*
184*8d741a5dSApple OSS Distributions  * WL handler which checks if the servicer thread has correct Qos.
185*8d741a5dSApple OSS Distributions  */
186*8d741a5dSApple OSS Distributions static void
workloop_cb_test_kernel_sync_send(uint64_t * workloop_id __unused,void ** eventslist,int * events)187*8d741a5dSApple OSS Distributions workloop_cb_test_kernel_sync_send(uint64_t *workloop_id __unused, void **eventslist, int *events)
188*8d741a5dSApple OSS Distributions {
189*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_kernel_sync_send called");
190*8d741a5dSApple OSS Distributions 
191*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
192*8d741a5dSApple OSS Distributions 
193*8d741a5dSApple OSS Distributions 	/* Wait for the kernel upcall to block on receive */
194*8d741a5dSApple OSS Distributions 	sleep(2 * RECV_TIMEOUT_SECS);
195*8d741a5dSApple OSS Distributions 
196*8d741a5dSApple OSS Distributions 	/* The effective Qos should be the one expected after override */
197*8d741a5dSApple OSS Distributions 	EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
198*8d741a5dSApple OSS Distributions 	    "dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
199*8d741a5dSApple OSS Distributions 
200*8d741a5dSApple OSS Distributions 	*events = 0;
201*8d741a5dSApple OSS Distributions 	T_END;
202*8d741a5dSApple OSS Distributions }
203*8d741a5dSApple OSS Distributions 
204*8d741a5dSApple OSS Distributions /*
205*8d741a5dSApple OSS Distributions  * WL handler which checks if the servicer thread has correct Qos.
206*8d741a5dSApple OSS Distributions  */
207*8d741a5dSApple OSS Distributions static void
workloop_cb_test_kernel_async_send(uint64_t * workloop_id __unused,void ** eventslist,int * events)208*8d741a5dSApple OSS Distributions workloop_cb_test_kernel_async_send(uint64_t *workloop_id __unused, void **eventslist, int *events)
209*8d741a5dSApple OSS Distributions {
210*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_kernel_sync_send called");
211*8d741a5dSApple OSS Distributions 
212*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
213*8d741a5dSApple OSS Distributions 
214*8d741a5dSApple OSS Distributions 	/* The effective Qos should be the one expected after override */
215*8d741a5dSApple OSS Distributions 	EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
216*8d741a5dSApple OSS Distributions 	    "dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
217*8d741a5dSApple OSS Distributions 
218*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(get_thread_iotier(), USR_THROTTLE_LEVEL_TIER0, "Thread IOTier has correct override");
219*8d741a5dSApple OSS Distributions 
220*8d741a5dSApple OSS Distributions 	*events = 0;
221*8d741a5dSApple OSS Distributions 	T_END;
222*8d741a5dSApple OSS Distributions }
223*8d741a5dSApple OSS Distributions 
224*8d741a5dSApple OSS Distributions /*
225*8d741a5dSApple OSS Distributions  * WL handler which checks the overridden Qos and then enables the knote and checks
226*8d741a5dSApple OSS Distributions  * for the Qos again if that dropped the sync ipc override.
227*8d741a5dSApple OSS Distributions  */
228*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_and_enable(uint64_t * workloop_id,struct kevent_qos_s ** eventslist,int * events)229*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_and_enable(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
230*8d741a5dSApple OSS Distributions {
231*8d741a5dSApple OSS Distributions 	unsigned override_priority;
232*8d741a5dSApple OSS Distributions 	unsigned reenable_priority;
233*8d741a5dSApple OSS Distributions 
234*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_sync_send_and_enable called");
235*8d741a5dSApple OSS Distributions 
236*8d741a5dSApple OSS Distributions 	EXPECT_TEST_MSG(*eventslist);
237*8d741a5dSApple OSS Distributions 
238*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
239*8d741a5dSApple OSS Distributions 
240*8d741a5dSApple OSS Distributions 	/* The effective Qos should be the one expected after override */
241*8d741a5dSApple OSS Distributions 	EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
242*8d741a5dSApple OSS Distributions 	    "dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
243*8d741a5dSApple OSS Distributions 
244*8d741a5dSApple OSS Distributions 	/* Snapshot the current override priority */
245*8d741a5dSApple OSS Distributions 	override_priority = get_user_promotion_basepri();
246*8d741a5dSApple OSS Distributions 
247*8d741a5dSApple OSS Distributions 	/* Enable the knote */
248*8d741a5dSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
249*8d741a5dSApple OSS Distributions 	enable_kevent(workloop_id, kev->ident);
250*8d741a5dSApple OSS Distributions 
251*8d741a5dSApple OSS Distributions 	/*
252*8d741a5dSApple OSS Distributions 	 * Check if the override has been dropped, check for priority instead of qos since
253*8d741a5dSApple OSS Distributions 	 * there will be async qos push.
254*8d741a5dSApple OSS Distributions 	 */
255*8d741a5dSApple OSS Distributions 	reenable_priority = get_user_promotion_basepri();
256*8d741a5dSApple OSS Distributions 	T_EXPECT_LT(reenable_priority, override_priority,
257*8d741a5dSApple OSS Distributions 	    "thread's current override priority %d should be less than override priority prior to enabling knote %d",
258*8d741a5dSApple OSS Distributions 	    reenable_priority, override_priority);
259*8d741a5dSApple OSS Distributions 
260*8d741a5dSApple OSS Distributions 	*events = 0;
261*8d741a5dSApple OSS Distributions 	T_END;
262*8d741a5dSApple OSS Distributions }
263*8d741a5dSApple OSS Distributions 
264*8d741a5dSApple OSS Distributions /*
265*8d741a5dSApple OSS Distributions  * WL handler which checks the overridden Qos and then handoffs the IPC,
266*8d741a5dSApple OSS Distributions  * enables the knote and checks for the Qos again that it hasn't dropped the sync ipc override.
267*8d741a5dSApple OSS Distributions  */
268*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_and_enable_handoff(uint64_t * workloop_id,struct kevent_qos_s ** eventslist,int * events)269*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_and_enable_handoff(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
270*8d741a5dSApple OSS Distributions {
271*8d741a5dSApple OSS Distributions 	unsigned override_priority;
272*8d741a5dSApple OSS Distributions 	int error;
273*8d741a5dSApple OSS Distributions 
274*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_sync_send_and_enable_handoff called");
275*8d741a5dSApple OSS Distributions 
276*8d741a5dSApple OSS Distributions 	EXPECT_TEST_MSG(*eventslist);
277*8d741a5dSApple OSS Distributions 
278*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
279*8d741a5dSApple OSS Distributions 
280*8d741a5dSApple OSS Distributions 	/* The effective Qos should be the one expected after override */
281*8d741a5dSApple OSS Distributions 	EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
282*8d741a5dSApple OSS Distributions 	    "dispatch_source event handler QoS should be %s",
283*8d741a5dSApple OSS Distributions 	    g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
284*8d741a5dSApple OSS Distributions 
285*8d741a5dSApple OSS Distributions 	/* Snapshot the current override priority */
286*8d741a5dSApple OSS Distributions 	override_priority = get_user_promotion_basepri();
287*8d741a5dSApple OSS Distributions 
288*8d741a5dSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
289*8d741a5dSApple OSS Distributions 	mach_msg_header_t *hdr = (mach_msg_header_t *)kev->ext[0];
290*8d741a5dSApple OSS Distributions 
291*8d741a5dSApple OSS Distributions 	/* handoff the IPC */
292*8d741a5dSApple OSS Distributions 	struct kevent_qos_s handoff_kev = {
293*8d741a5dSApple OSS Distributions 		.filter = EVFILT_WORKLOOP,
294*8d741a5dSApple OSS Distributions 		.ident = hdr->msgh_remote_port,
295*8d741a5dSApple OSS Distributions 		.flags = EV_ADD | EV_DISABLE,
296*8d741a5dSApple OSS Distributions 		.fflags = 0x80000000,
297*8d741a5dSApple OSS Distributions 	};
298*8d741a5dSApple OSS Distributions 
299*8d741a5dSApple OSS Distributions 	error = kevent_id(*workloop_id, &handoff_kev, 1, &handoff_kev, 1, NULL,
300*8d741a5dSApple OSS Distributions 	    NULL, KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_ERROR_EVENTS | KEVENT_FLAG_DYNAMIC_KQ_MUST_EXIST);
301*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(error, "kevent_id");
302*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(0, error, "Handed off the sync IPC");
303*8d741a5dSApple OSS Distributions 
304*8d741a5dSApple OSS Distributions 	/* Enable the knote */
305*8d741a5dSApple OSS Distributions 	enable_kevent(workloop_id, kev->ident);
306*8d741a5dSApple OSS Distributions 
307*8d741a5dSApple OSS Distributions 	/*
308*8d741a5dSApple OSS Distributions 	 * Check if the override has not been dropped.
309*8d741a5dSApple OSS Distributions 	 */
310*8d741a5dSApple OSS Distributions 	EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
311*8d741a5dSApple OSS Distributions 	    "dispatch_source event handler QoS should still be %s",
312*8d741a5dSApple OSS Distributions 	    g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
313*8d741a5dSApple OSS Distributions 
314*8d741a5dSApple OSS Distributions 	*events = 0;
315*8d741a5dSApple OSS Distributions 	T_END;
316*8d741a5dSApple OSS Distributions }
317*8d741a5dSApple OSS Distributions 
318*8d741a5dSApple OSS Distributions /*
319*8d741a5dSApple OSS Distributions  * WL handler receives the first message and checks sync ipc override, then enables the knote
320*8d741a5dSApple OSS Distributions  * and receives 2nd message and checks it sync ipc override.
321*8d741a5dSApple OSS Distributions  */
322*8d741a5dSApple OSS Distributions static int send_two_sync_handler_called = 0;
323*8d741a5dSApple OSS Distributions static void
workloop_cb_test_send_two_sync(uint64_t * workloop_id __unused,struct kevent_qos_s ** eventslist,int * events)324*8d741a5dSApple OSS Distributions workloop_cb_test_send_two_sync(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
325*8d741a5dSApple OSS Distributions {
326*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_send_two_sync called for %d time", send_two_sync_handler_called + 1);
327*8d741a5dSApple OSS Distributions 
328*8d741a5dSApple OSS Distributions 	EXPECT_TEST_MSG(*eventslist);
329*8d741a5dSApple OSS Distributions 
330*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
331*8d741a5dSApple OSS Distributions 
332*8d741a5dSApple OSS Distributions 	T_LOG("Number of events received is %d\n", *events);
333*8d741a5dSApple OSS Distributions 
334*8d741a5dSApple OSS Distributions 	if (send_two_sync_handler_called == 0) {
335*8d741a5dSApple OSS Distributions 		/* The effective Qos should be the one expected after override */
336*8d741a5dSApple OSS Distributions 		EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
337*8d741a5dSApple OSS Distributions 		    "dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
338*8d741a5dSApple OSS Distributions 
339*8d741a5dSApple OSS Distributions 		/* Enable the knote to get 2nd message */
340*8d741a5dSApple OSS Distributions 		struct kevent_qos_s *kev = *eventslist;
341*8d741a5dSApple OSS Distributions 		uint64_t port = kev->ident;
342*8d741a5dSApple OSS Distributions 		populate_kevent(kev, port);
343*8d741a5dSApple OSS Distributions 		*events = 1;
344*8d741a5dSApple OSS Distributions 	} else {
345*8d741a5dSApple OSS Distributions 		EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_BEFORE_OVERRIDE],
346*8d741a5dSApple OSS Distributions 		    "dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_BEFORE_OVERRIDE]);
347*8d741a5dSApple OSS Distributions 		*events = 0;
348*8d741a5dSApple OSS Distributions 		T_END;
349*8d741a5dSApple OSS Distributions 	}
350*8d741a5dSApple OSS Distributions 	send_two_sync_handler_called++;
351*8d741a5dSApple OSS Distributions }
352*8d741a5dSApple OSS Distributions 
353*8d741a5dSApple OSS Distributions /*
354*8d741a5dSApple OSS Distributions  * Checks the sync ipc override and then waits for client to destroy the
355*8d741a5dSApple OSS Distributions  * special reply port and checks if that removes the sync ipc override.
356*8d741a5dSApple OSS Distributions  */
357*8d741a5dSApple OSS Distributions static boolean_t two_send_and_destroy_test_passed = FALSE;
358*8d741a5dSApple OSS Distributions static int two_send_and_destroy_handler = 0;
359*8d741a5dSApple OSS Distributions static void
workloop_cb_test_two_send_and_destroy(uint64_t * workloop_id __unused,struct kevent_qos_s ** eventslist __unused,int * events)360*8d741a5dSApple OSS Distributions workloop_cb_test_two_send_and_destroy(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist __unused, int *events)
361*8d741a5dSApple OSS Distributions {
362*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_two_send_and_destroy called %d times", two_send_and_destroy_handler + 1);
363*8d741a5dSApple OSS Distributions 
364*8d741a5dSApple OSS Distributions 	EXPECT_TEST_MSG(*eventslist);
365*8d741a5dSApple OSS Distributions 
366*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
367*8d741a5dSApple OSS Distributions 
368*8d741a5dSApple OSS Distributions 	if (two_send_and_destroy_handler == 0) {
369*8d741a5dSApple OSS Distributions 		/* Sleep to make sure the mqueue gets full */
370*8d741a5dSApple OSS Distributions 		sleep(RECV_TIMEOUT_SECS);
371*8d741a5dSApple OSS Distributions 
372*8d741a5dSApple OSS Distributions 		/* The effective Qos should be the one expected after override */
373*8d741a5dSApple OSS Distributions 		EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
374*8d741a5dSApple OSS Distributions 		    "dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
375*8d741a5dSApple OSS Distributions 
376*8d741a5dSApple OSS Distributions 		sleep(SEND_TIMEOUT_SECS);
377*8d741a5dSApple OSS Distributions 
378*8d741a5dSApple OSS Distributions 		/* Special reply port should have been destroyed, check Qos again */
379*8d741a5dSApple OSS Distributions 		EXPECT_QOS_EQ(g_expected_qos[ENV_QOS_BEFORE_OVERRIDE],
380*8d741a5dSApple OSS Distributions 		    "dispatch_source event handler QoS should be %s", g_expected_qos_name[ENV_QOS_BEFORE_OVERRIDE]);
381*8d741a5dSApple OSS Distributions 
382*8d741a5dSApple OSS Distributions 		two_send_and_destroy_test_passed = TRUE;
383*8d741a5dSApple OSS Distributions 	} else {
384*8d741a5dSApple OSS Distributions 		if (two_send_and_destroy_test_passed) {
385*8d741a5dSApple OSS Distributions 			T_END;
386*8d741a5dSApple OSS Distributions 		}
387*8d741a5dSApple OSS Distributions 	}
388*8d741a5dSApple OSS Distributions 
389*8d741a5dSApple OSS Distributions 	/* Enable the knote to get next message */
390*8d741a5dSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
391*8d741a5dSApple OSS Distributions 	uint64_t port = kev->ident;
392*8d741a5dSApple OSS Distributions 	populate_kevent(kev, port);
393*8d741a5dSApple OSS Distributions 	*events = 1;
394*8d741a5dSApple OSS Distributions 	two_send_and_destroy_handler++;
395*8d741a5dSApple OSS Distributions 	T_LOG("Handler returning \n");
396*8d741a5dSApple OSS Distributions }
397*8d741a5dSApple OSS Distributions 
398*8d741a5dSApple OSS Distributions static mach_port_type_t
get_reply_port(struct kevent_qos_s * kev)399*8d741a5dSApple OSS Distributions get_reply_port(struct kevent_qos_s *kev)
400*8d741a5dSApple OSS Distributions {
401*8d741a5dSApple OSS Distributions 	mach_msg_header_t *hdr;
402*8d741a5dSApple OSS Distributions 	mach_port_t reply_port;
403*8d741a5dSApple OSS Distributions 	mach_port_type_t type;
404*8d741a5dSApple OSS Distributions 	kern_return_t kr;
405*8d741a5dSApple OSS Distributions 
406*8d741a5dSApple OSS Distributions 	hdr = (void*)kev->ext[0];
407*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(hdr, "msg hdr");
408*8d741a5dSApple OSS Distributions 
409*8d741a5dSApple OSS Distributions 	reply_port = hdr->msgh_remote_port;
410*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(reply_port), "reply port valid");
411*8d741a5dSApple OSS Distributions 	kr = mach_port_type(mach_task_self(), reply_port, &type);
412*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_type");
413*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(type & MACH_PORT_TYPE_SEND_ONCE, "send once received");
414*8d741a5dSApple OSS Distributions 
415*8d741a5dSApple OSS Distributions 	return reply_port;
416*8d741a5dSApple OSS Distributions }
417*8d741a5dSApple OSS Distributions 
418*8d741a5dSApple OSS Distributions static void
send_reply(mach_port_t reply_port)419*8d741a5dSApple OSS Distributions send_reply(mach_port_t reply_port)
420*8d741a5dSApple OSS Distributions {
421*8d741a5dSApple OSS Distributions 	kern_return_t kr;
422*8d741a5dSApple OSS Distributions 
423*8d741a5dSApple OSS Distributions 	struct {
424*8d741a5dSApple OSS Distributions 		mach_msg_header_t header;
425*8d741a5dSApple OSS Distributions 	} send_msg = {
426*8d741a5dSApple OSS Distributions 		.header = {
427*8d741a5dSApple OSS Distributions 			.msgh_remote_port = reply_port,
428*8d741a5dSApple OSS Distributions 			.msgh_local_port  = MACH_PORT_NULL,
429*8d741a5dSApple OSS Distributions 			.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0),
430*8d741a5dSApple OSS Distributions 			.msgh_id          = 0x100,
431*8d741a5dSApple OSS Distributions 			.msgh_size        = sizeof(send_msg),
432*8d741a5dSApple OSS Distributions 		},
433*8d741a5dSApple OSS Distributions 	};
434*8d741a5dSApple OSS Distributions 
435*8d741a5dSApple OSS Distributions 	kr = mach_msg(&(send_msg.header),
436*8d741a5dSApple OSS Distributions 	    MACH_SEND_MSG,
437*8d741a5dSApple OSS Distributions 	    send_msg.header.msgh_size,
438*8d741a5dSApple OSS Distributions 	    0,
439*8d741a5dSApple OSS Distributions 	    MACH_PORT_NULL,
440*8d741a5dSApple OSS Distributions 	    0,
441*8d741a5dSApple OSS Distributions 	    0);
442*8d741a5dSApple OSS Distributions 
443*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "server mach_msg");
444*8d741a5dSApple OSS Distributions }
445*8d741a5dSApple OSS Distributions 
446*8d741a5dSApple OSS Distributions static void
populate_kevent(struct kevent_qos_s * kev,unsigned long long port)447*8d741a5dSApple OSS Distributions populate_kevent(struct kevent_qos_s *kev, unsigned long long port)
448*8d741a5dSApple OSS Distributions {
449*8d741a5dSApple OSS Distributions 	memset(kev, 0, sizeof(struct kevent_qos_s));
450*8d741a5dSApple OSS Distributions 	kev->ident = port;
451*8d741a5dSApple OSS Distributions 	kev->filter = EVFILT_MACHPORT;
452*8d741a5dSApple OSS Distributions 	kev->flags = EV_ADD | EV_ENABLE | EV_UDATA_SPECIFIC | EV_DISPATCH | EV_VANISHED;
453*8d741a5dSApple OSS Distributions 	kev->fflags = (MACH_RCV_MSG | MACH_RCV_VOUCHER | MACH_RCV_LARGE | MACH_RCV_LARGE_IDENTITY |
454*8d741a5dSApple OSS Distributions 	    MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AV) |
455*8d741a5dSApple OSS Distributions 	    MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0));
456*8d741a5dSApple OSS Distributions 	kev->data = 1;
457*8d741a5dSApple OSS Distributions }
458*8d741a5dSApple OSS Distributions 
459*8d741a5dSApple OSS Distributions static void
enable_kevent(uint64_t * workloop_id,unsigned long long port)460*8d741a5dSApple OSS Distributions enable_kevent(uint64_t *workloop_id, unsigned long long port)
461*8d741a5dSApple OSS Distributions {
462*8d741a5dSApple OSS Distributions 	struct kevent_qos_s kev;
463*8d741a5dSApple OSS Distributions 	int error;
464*8d741a5dSApple OSS Distributions 
465*8d741a5dSApple OSS Distributions 	populate_kevent(&kev, port);
466*8d741a5dSApple OSS Distributions 	struct kevent_qos_s kev_err[] = {{ 0 }};
467*8d741a5dSApple OSS Distributions 
468*8d741a5dSApple OSS Distributions 	error = kevent_id(*workloop_id, &kev, 1, kev_err, 1, NULL,
469*8d741a5dSApple OSS Distributions 	    NULL, KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_ERROR_EVENTS | KEVENT_FLAG_DYNAMIC_KQ_MUST_EXIST);
470*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(error, "kevent_id");
471*8d741a5dSApple OSS Distributions }
472*8d741a5dSApple OSS Distributions 
473*8d741a5dSApple OSS Distributions /*
474*8d741a5dSApple OSS Distributions  * WL handler which sends a msg to the client from handler.
475*8d741a5dSApple OSS Distributions  */
476*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_reply(uint64_t * workloop_id __unused,struct kevent_qos_s ** eventslist,int * events)477*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_reply(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
478*8d741a5dSApple OSS Distributions {
479*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_sync_send_reply called");
480*8d741a5dSApple OSS Distributions 
481*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
482*8d741a5dSApple OSS Distributions 
483*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
484*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
485*8d741a5dSApple OSS Distributions 
486*8d741a5dSApple OSS Distributions 	/* send reply */
487*8d741a5dSApple OSS Distributions 	send_reply(get_reply_port(*eventslist));
488*8d741a5dSApple OSS Distributions 
489*8d741a5dSApple OSS Distributions 	*events = 0;
490*8d741a5dSApple OSS Distributions }
491*8d741a5dSApple OSS Distributions 
492*8d741a5dSApple OSS Distributions /*
493*8d741a5dSApple OSS Distributions  * WL handler which deallocates reply port.
494*8d741a5dSApple OSS Distributions  */
495*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_deallocate(uint64_t * workloop_id __unused,struct kevent_qos_s ** eventslist,int * events)496*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_deallocate(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
497*8d741a5dSApple OSS Distributions {
498*8d741a5dSApple OSS Distributions 	mach_port_t reply_port;
499*8d741a5dSApple OSS Distributions 	kern_return_t kr;
500*8d741a5dSApple OSS Distributions 
501*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_sync_send_deallocate called");
502*8d741a5dSApple OSS Distributions 
503*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
504*8d741a5dSApple OSS Distributions 
505*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
506*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
507*8d741a5dSApple OSS Distributions 
508*8d741a5dSApple OSS Distributions 	reply_port = get_reply_port(*eventslist);
509*8d741a5dSApple OSS Distributions 
510*8d741a5dSApple OSS Distributions 	/* deallocate port */
511*8d741a5dSApple OSS Distributions 	kr = mach_port_deallocate(mach_task_self(), reply_port);
512*8d741a5dSApple OSS Distributions 
513*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_deallocate");
514*8d741a5dSApple OSS Distributions 
515*8d741a5dSApple OSS Distributions 	*events = 0;
516*8d741a5dSApple OSS Distributions 
517*8d741a5dSApple OSS Distributions 	T_LOG("Handler returning \n");
518*8d741a5dSApple OSS Distributions }
519*8d741a5dSApple OSS Distributions 
520*8d741a5dSApple OSS Distributions 
521*8d741a5dSApple OSS Distributions /*
522*8d741a5dSApple OSS Distributions  * WL handler which sends a msg to the client before enabling the event from handler.
523*8d741a5dSApple OSS Distributions  */
524*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_reply_kevent(uint64_t * workloop_id,struct kevent_qos_s ** eventslist,int * events)525*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_reply_kevent(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
526*8d741a5dSApple OSS Distributions {
527*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_sync_send_reply_kevent called");
528*8d741a5dSApple OSS Distributions 
529*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
530*8d741a5dSApple OSS Distributions 
531*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
532*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(((*eventslist)->filter), EVFILT_MACHPORT, "received EVFILT_MACHPORT");
533*8d741a5dSApple OSS Distributions 
534*8d741a5dSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
535*8d741a5dSApple OSS Distributions 
536*8d741a5dSApple OSS Distributions 	/* send reply */
537*8d741a5dSApple OSS Distributions 	send_reply(get_reply_port(kev));
538*8d741a5dSApple OSS Distributions 
539*8d741a5dSApple OSS Distributions 	/* Enable the knote */
540*8d741a5dSApple OSS Distributions 	enable_kevent(workloop_id, kev->ident);
541*8d741a5dSApple OSS Distributions 
542*8d741a5dSApple OSS Distributions 	*events = 0;
543*8d741a5dSApple OSS Distributions 
544*8d741a5dSApple OSS Distributions 	T_LOG("Handler returning \n");
545*8d741a5dSApple OSS Distributions }
546*8d741a5dSApple OSS Distributions 
547*8d741a5dSApple OSS Distributions /*
548*8d741a5dSApple OSS Distributions  * WL handler which sends a msg to the client before enabling the event from pthread.
549*8d741a5dSApple OSS Distributions  */
550*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_reply_kevent_pthread(uint64_t * workloop_id __unused,struct kevent_qos_s ** eventslist,int * events)551*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_reply_kevent_pthread(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
552*8d741a5dSApple OSS Distributions {
553*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_sync_send_reply_kevent_pthread called");
554*8d741a5dSApple OSS Distributions 
555*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
556*8d741a5dSApple OSS Distributions 
557*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
558*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
559*8d741a5dSApple OSS Distributions 
560*8d741a5dSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
561*8d741a5dSApple OSS Distributions 
562*8d741a5dSApple OSS Distributions 	/* send reply */
563*8d741a5dSApple OSS Distributions 	send_reply(get_reply_port(kev));
564*8d741a5dSApple OSS Distributions 
565*8d741a5dSApple OSS Distributions 	populate_kevent(kev, kev->ident);
566*8d741a5dSApple OSS Distributions 
567*8d741a5dSApple OSS Distributions 	*events = 1;
568*8d741a5dSApple OSS Distributions 
569*8d741a5dSApple OSS Distributions 	T_LOG("Handler returning \n");
570*8d741a5dSApple OSS Distributions }
571*8d741a5dSApple OSS Distributions 
572*8d741a5dSApple OSS Distributions /*
573*8d741a5dSApple OSS Distributions  * WL handler which sends a msg to the client after reenabling the event.
574*8d741a5dSApple OSS Distributions  */
575*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_kevent_reply(uint64_t * workloop_id,struct kevent_qos_s ** eventslist,int * events)576*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_kevent_reply(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
577*8d741a5dSApple OSS Distributions {
578*8d741a5dSApple OSS Distributions 	T_LOG("workloop handler workloop_cb_test_sync_send_kevent_reply called");
579*8d741a5dSApple OSS Distributions 
580*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
581*8d741a5dSApple OSS Distributions 
582*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
583*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
584*8d741a5dSApple OSS Distributions 
585*8d741a5dSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
586*8d741a5dSApple OSS Distributions 	mach_port_t reply_port = get_reply_port(*eventslist);
587*8d741a5dSApple OSS Distributions 
588*8d741a5dSApple OSS Distributions 	/* Enable the knote */
589*8d741a5dSApple OSS Distributions 	enable_kevent(workloop_id, kev->ident);
590*8d741a5dSApple OSS Distributions 
591*8d741a5dSApple OSS Distributions 	/* send reply */
592*8d741a5dSApple OSS Distributions 	send_reply(reply_port);
593*8d741a5dSApple OSS Distributions 
594*8d741a5dSApple OSS Distributions 	*events = 0;
595*8d741a5dSApple OSS Distributions 
596*8d741a5dSApple OSS Distributions 	T_LOG("Handler returning \n");
597*8d741a5dSApple OSS Distributions }
598*8d741a5dSApple OSS Distributions 
599*8d741a5dSApple OSS Distributions /*
600*8d741a5dSApple OSS Distributions  * WL handler that does nothing.
601*8d741a5dSApple OSS Distributions  */
602*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_do_nothing(uint64_t * workloop_id __unused,struct kevent_qos_s ** eventslist,int * events)603*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_do_nothing(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
604*8d741a5dSApple OSS Distributions {
605*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_sync_send_do_nothing called");
606*8d741a5dSApple OSS Distributions 
607*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
608*8d741a5dSApple OSS Distributions 
609*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
610*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
611*8d741a5dSApple OSS Distributions 
612*8d741a5dSApple OSS Distributions 	/* do nothing */
613*8d741a5dSApple OSS Distributions 
614*8d741a5dSApple OSS Distributions 	*events = 0;
615*8d741a5dSApple OSS Distributions 
616*8d741a5dSApple OSS Distributions 	T_LOG("Handler returning \n");
617*8d741a5dSApple OSS Distributions }
618*8d741a5dSApple OSS Distributions 
619*8d741a5dSApple OSS Distributions /*
620*8d741a5dSApple OSS Distributions  * WL handler that returns the event to reenable.
621*8d741a5dSApple OSS Distributions  */
622*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_do_nothing_kevent_pthread(uint64_t * workloop_id __unused,struct kevent_qos_s ** eventslist,int * events)623*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_do_nothing_kevent_pthread(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
624*8d741a5dSApple OSS Distributions {
625*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_sync_send_do_nothing_kevent_pthread called");
626*8d741a5dSApple OSS Distributions 
627*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
628*8d741a5dSApple OSS Distributions 
629*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
630*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
631*8d741a5dSApple OSS Distributions 
632*8d741a5dSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
633*8d741a5dSApple OSS Distributions 	populate_kevent(kev, kev->ident);
634*8d741a5dSApple OSS Distributions 
635*8d741a5dSApple OSS Distributions 	*events = 1;
636*8d741a5dSApple OSS Distributions 
637*8d741a5dSApple OSS Distributions 	T_LOG("handler returning \n");
638*8d741a5dSApple OSS Distributions }
639*8d741a5dSApple OSS Distributions 
640*8d741a5dSApple OSS Distributions /*
641*8d741a5dSApple OSS Distributions  * WL handler that exits.
642*8d741a5dSApple OSS Distributions  */
643*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_do_nothing_exit(uint64_t * workloop_id __unused,struct kevent_qos_s ** eventslist,__unused int * events)644*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_do_nothing_exit(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, __unused int *events)
645*8d741a5dSApple OSS Distributions {
646*8d741a5dSApple OSS Distributions 	T_LOG("workloop handler workloop_cb_test_sync_send_do_nothing_exit called");
647*8d741a5dSApple OSS Distributions 
648*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
649*8d741a5dSApple OSS Distributions 
650*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
651*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
652*8d741a5dSApple OSS Distributions 
653*8d741a5dSApple OSS Distributions 	/* call exit */
654*8d741a5dSApple OSS Distributions 	exit(0);
655*8d741a5dSApple OSS Distributions }
656*8d741a5dSApple OSS Distributions 
657*8d741a5dSApple OSS Distributions /*
658*8d741a5dSApple OSS Distributions  * WL handler which:
659*8d741a5dSApple OSS Distributions  * first sync sends a msg to the client and reenables kevent after
660*8d741a5dSApple OSS Distributions  * second sync sends a msg and reenables kevent after.
661*8d741a5dSApple OSS Distributions  */
662*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_reply_kevent_reply_kevent(uint64_t * workloop_id __unused,struct kevent_qos_s ** eventslist,int * events)663*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_reply_kevent_reply_kevent(uint64_t *workloop_id __unused, struct kevent_qos_s **eventslist, int *events)
664*8d741a5dSApple OSS Distributions {
665*8d741a5dSApple OSS Distributions 	T_LOG("Workloop handler workloop_cb_test_sync_send_reply_kevent_reply_kevent called");
666*8d741a5dSApple OSS Distributions 
667*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
668*8d741a5dSApple OSS Distributions 
669*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
670*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
671*8d741a5dSApple OSS Distributions 
672*8d741a5dSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
673*8d741a5dSApple OSS Distributions 
674*8d741a5dSApple OSS Distributions 	/* send reply */
675*8d741a5dSApple OSS Distributions 	send_reply(get_reply_port(kev));
676*8d741a5dSApple OSS Distributions 
677*8d741a5dSApple OSS Distributions 	populate_kevent(kev, kev->ident);
678*8d741a5dSApple OSS Distributions 
679*8d741a5dSApple OSS Distributions 	*events = 1;
680*8d741a5dSApple OSS Distributions 
681*8d741a5dSApple OSS Distributions 	T_LOG("Handler returning \n");
682*8d741a5dSApple OSS Distributions }
683*8d741a5dSApple OSS Distributions 
684*8d741a5dSApple OSS Distributions /*
685*8d741a5dSApple OSS Distributions  * WL handler which:
686*8d741a5dSApple OSS Distributions  * first sync reenables kevent and after sends a msg
687*8d741a5dSApple OSS Distributions  * second sync sends a msg and reenables kevent after.
688*8d741a5dSApple OSS Distributions  */
689*8d741a5dSApple OSS Distributions static int workloop_cb_test_sync_send_kevent_reply_reply_kevent_handler_called = 0;
690*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_kevent_reply_reply_kevent(uint64_t * workloop_id,struct kevent_qos_s ** eventslist,int * events)691*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_kevent_reply_reply_kevent(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
692*8d741a5dSApple OSS Distributions {
693*8d741a5dSApple OSS Distributions 	T_LOG("workloop handler workloop_cb_test_sync_send_kevent_reply_reply_kevent called");
694*8d741a5dSApple OSS Distributions 
695*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
696*8d741a5dSApple OSS Distributions 
697*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
698*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
699*8d741a5dSApple OSS Distributions 
700*8d741a5dSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
701*8d741a5dSApple OSS Distributions 	mach_port_t reply_port = get_reply_port(kev);
702*8d741a5dSApple OSS Distributions 
703*8d741a5dSApple OSS Distributions 	if (workloop_cb_test_sync_send_kevent_reply_reply_kevent_handler_called == 0) {
704*8d741a5dSApple OSS Distributions 		workloop_cb_test_sync_send_kevent_reply_reply_kevent_handler_called = 1;
705*8d741a5dSApple OSS Distributions 
706*8d741a5dSApple OSS Distributions 		/* Enable the knote */
707*8d741a5dSApple OSS Distributions 		enable_kevent(workloop_id, kev->ident);
708*8d741a5dSApple OSS Distributions 
709*8d741a5dSApple OSS Distributions 		/* send reply */
710*8d741a5dSApple OSS Distributions 		send_reply(reply_port);
711*8d741a5dSApple OSS Distributions 
712*8d741a5dSApple OSS Distributions 		*events = 0;
713*8d741a5dSApple OSS Distributions 	} else {
714*8d741a5dSApple OSS Distributions 		/* send reply */
715*8d741a5dSApple OSS Distributions 		send_reply(reply_port);
716*8d741a5dSApple OSS Distributions 
717*8d741a5dSApple OSS Distributions 		/* Enable the knote */
718*8d741a5dSApple OSS Distributions 		enable_kevent(workloop_id, kev->ident);
719*8d741a5dSApple OSS Distributions 
720*8d741a5dSApple OSS Distributions 		*events = 0;
721*8d741a5dSApple OSS Distributions 	}
722*8d741a5dSApple OSS Distributions 
723*8d741a5dSApple OSS Distributions 	T_LOG("Handler returning \n");
724*8d741a5dSApple OSS Distributions }
725*8d741a5dSApple OSS Distributions 
726*8d741a5dSApple OSS Distributions /*
727*8d741a5dSApple OSS Distributions  * WL handler which:
728*8d741a5dSApple OSS Distributions  * first sync reenables kevent and after sends a msg
729*8d741a5dSApple OSS Distributions  * second sync reenables kevent and after sends a msg
730*8d741a5dSApple OSS Distributions  */
731*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_kevent_reply_kevent_reply(uint64_t * workloop_id,struct kevent_qos_s ** eventslist,int * events)732*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_kevent_reply_kevent_reply(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
733*8d741a5dSApple OSS Distributions {
734*8d741a5dSApple OSS Distributions 	T_LOG("workloop handler workloop_cb_test_sync_send_kevent_reply_kevent_reply called");
735*8d741a5dSApple OSS Distributions 
736*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
737*8d741a5dSApple OSS Distributions 
738*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
739*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
740*8d741a5dSApple OSS Distributions 
741*8d741a5dSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
742*8d741a5dSApple OSS Distributions 	mach_port_t reply_port = get_reply_port(kev);
743*8d741a5dSApple OSS Distributions 
744*8d741a5dSApple OSS Distributions 	/* Enable the knote */
745*8d741a5dSApple OSS Distributions 	enable_kevent(workloop_id, kev->ident);
746*8d741a5dSApple OSS Distributions 
747*8d741a5dSApple OSS Distributions 	/* send reply */
748*8d741a5dSApple OSS Distributions 	send_reply(reply_port);
749*8d741a5dSApple OSS Distributions 
750*8d741a5dSApple OSS Distributions 	*events = 0;
751*8d741a5dSApple OSS Distributions 	T_LOG("Handler returning \n");
752*8d741a5dSApple OSS Distributions }
753*8d741a5dSApple OSS Distributions 
754*8d741a5dSApple OSS Distributions /*
755*8d741a5dSApple OSS Distributions  * WL handler which:
756*8d741a5dSApple OSS Distributions  * first sync ends a msg and reenables kevent after
757*8d741a5dSApple OSS Distributions  * second sync reenables kevent and sends a msg after
758*8d741a5dSApple OSS Distributions  */
759*8d741a5dSApple OSS Distributions static int workloop_cb_test_sync_send_reply_kevent_kevent_reply_handler_called = 0;
760*8d741a5dSApple OSS Distributions static void
workloop_cb_test_sync_send_reply_kevent_kevent_reply(uint64_t * workloop_id,struct kevent_qos_s ** eventslist,int * events)761*8d741a5dSApple OSS Distributions workloop_cb_test_sync_send_reply_kevent_kevent_reply(uint64_t *workloop_id, struct kevent_qos_s **eventslist, int *events)
762*8d741a5dSApple OSS Distributions {
763*8d741a5dSApple OSS Distributions 	T_LOG("workloop handler workloop_cb_test_sync_send_reply_kevent_kevent_reply called");
764*8d741a5dSApple OSS Distributions 
765*8d741a5dSApple OSS Distributions 	T_ASSERT_EQ(geteuid(), 0, "kevent_qos test requires root privileges to run.");
766*8d741a5dSApple OSS Distributions 
767*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT(*events, 1, "events received");
768*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_INT((*eventslist)->filter, EVFILT_MACHPORT, "received EVFILT_MACHPORT");
769*8d741a5dSApple OSS Distributions 
770*8d741a5dSApple OSS Distributions 	struct kevent_qos_s *kev = *eventslist;
771*8d741a5dSApple OSS Distributions 	mach_port_t reply_port = get_reply_port(kev);
772*8d741a5dSApple OSS Distributions 
773*8d741a5dSApple OSS Distributions 	if (workloop_cb_test_sync_send_reply_kevent_kevent_reply_handler_called == 0) {
774*8d741a5dSApple OSS Distributions 		workloop_cb_test_sync_send_reply_kevent_kevent_reply_handler_called = 1;
775*8d741a5dSApple OSS Distributions 
776*8d741a5dSApple OSS Distributions 		/* send reply */
777*8d741a5dSApple OSS Distributions 		send_reply(reply_port);
778*8d741a5dSApple OSS Distributions 
779*8d741a5dSApple OSS Distributions 		populate_kevent(kev, kev->ident);
780*8d741a5dSApple OSS Distributions 
781*8d741a5dSApple OSS Distributions 		*events = 1;
782*8d741a5dSApple OSS Distributions 	} else {
783*8d741a5dSApple OSS Distributions 		/* Enable the knote */
784*8d741a5dSApple OSS Distributions 		enable_kevent(workloop_id, kev->ident);
785*8d741a5dSApple OSS Distributions 		/* send reply */
786*8d741a5dSApple OSS Distributions 		send_reply(reply_port);
787*8d741a5dSApple OSS Distributions 
788*8d741a5dSApple OSS Distributions 		*events = 0;
789*8d741a5dSApple OSS Distributions 	}
790*8d741a5dSApple OSS Distributions 
791*8d741a5dSApple OSS Distributions 	T_LOG("Handler returning \n");
792*8d741a5dSApple OSS Distributions }
793*8d741a5dSApple OSS Distributions #pragma mark Mach receive
794*8d741a5dSApple OSS Distributions 
795*8d741a5dSApple OSS Distributions #define KEVENT_QOS_SERVICE_NAME "com.apple.xnu.test.kevent_qos"
796*8d741a5dSApple OSS Distributions 
797*8d741a5dSApple OSS Distributions static mach_port_t
get_server_port(void)798*8d741a5dSApple OSS Distributions get_server_port(void)
799*8d741a5dSApple OSS Distributions {
800*8d741a5dSApple OSS Distributions 	mach_port_t port;
801*8d741a5dSApple OSS Distributions 	kern_return_t kr = bootstrap_check_in(bootstrap_port,
802*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &port);
803*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "server bootstrap_check_in");
804*8d741a5dSApple OSS Distributions 	return port;
805*8d741a5dSApple OSS Distributions }
806*8d741a5dSApple OSS Distributions 
807*8d741a5dSApple OSS Distributions static void
env_set_qos(char ** env,qos_class_t qos[],const char * qos_name[],const char * wl_function)808*8d741a5dSApple OSS Distributions env_set_qos(char **env, qos_class_t qos[], const char *qos_name[], const char *wl_function)
809*8d741a5dSApple OSS Distributions {
810*8d741a5dSApple OSS Distributions 	int i;
811*8d741a5dSApple OSS Distributions 	char *qos_str, *qos_name_str;
812*8d741a5dSApple OSS Distributions 	for (i = 0; i < ENV_VAR_QOS; i++) {
813*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(asprintf(&qos_str, "%s=%d", qos_env[i], qos[i]),
814*8d741a5dSApple OSS Distributions 		    NULL);
815*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(
816*8d741a5dSApple OSS Distributions 			asprintf(&qos_name_str, "%s=%s", qos_name_env[i], qos_name[i]), NULL);
817*8d741a5dSApple OSS Distributions 		env[2 * i] = qos_str;
818*8d741a5dSApple OSS Distributions 		env[2 * i + 1] = qos_name_str;
819*8d741a5dSApple OSS Distributions 	}
820*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(asprintf(&env[2 * i], "%s=%s", wl_function_name, wl_function),
821*8d741a5dSApple OSS Distributions 	    NULL);
822*8d741a5dSApple OSS Distributions 	env[2 * i + 1] = NULL;
823*8d741a5dSApple OSS Distributions }
824*8d741a5dSApple OSS Distributions 
825*8d741a5dSApple OSS Distributions static void
environ_get_qos(qos_class_t qos[],const char * qos_name[],const char ** wl_function)826*8d741a5dSApple OSS Distributions environ_get_qos(qos_class_t qos[], const char *qos_name[], const char **wl_function)
827*8d741a5dSApple OSS Distributions {
828*8d741a5dSApple OSS Distributions 	char *qos_str;
829*8d741a5dSApple OSS Distributions 	char *qos_end;
830*8d741a5dSApple OSS Distributions 	int i;
831*8d741a5dSApple OSS Distributions 
832*8d741a5dSApple OSS Distributions 	for (i = 0; i < ENV_VAR_QOS; i++) {
833*8d741a5dSApple OSS Distributions 		qos_str = getenv(qos_env[i]);
834*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_NOTNULL(qos_str, "getenv(%s)", qos_env[i]);
835*8d741a5dSApple OSS Distributions 
836*8d741a5dSApple OSS Distributions 		unsigned long qos_l = strtoul(qos_str, &qos_end, 10);
837*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(*qos_end, '\0', "getenv(%s) = '%s' should be an "
838*8d741a5dSApple OSS Distributions 		    "integer", qos_env[i], qos_str);
839*8d741a5dSApple OSS Distributions 
840*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_LT(qos_l, (unsigned long)100, "getenv(%s) = '%s' should "
841*8d741a5dSApple OSS Distributions 		    "be less than 100", qos_env[i], qos_str);
842*8d741a5dSApple OSS Distributions 
843*8d741a5dSApple OSS Distributions 		qos[i] = (qos_class_t)qos_l;
844*8d741a5dSApple OSS Distributions 		qos_name[i] = getenv(qos_name_env[i]);
845*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_NOTNULL(qos_name[i], "getenv(%s)", qos_name_env[i]);
846*8d741a5dSApple OSS Distributions 	}
847*8d741a5dSApple OSS Distributions 	*wl_function = getenv(wl_function_name);
848*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(*wl_function, "getenv(%s)", wl_function_name);
849*8d741a5dSApple OSS Distributions }
850*8d741a5dSApple OSS Distributions 
851*8d741a5dSApple OSS Distributions static inline thread_qos_t
thread_qos_from_qos_class(qos_class_t cls)852*8d741a5dSApple OSS Distributions thread_qos_from_qos_class(qos_class_t cls)
853*8d741a5dSApple OSS Distributions {
854*8d741a5dSApple OSS Distributions 	switch ((unsigned int)cls) {
855*8d741a5dSApple OSS Distributions 	case QOS_CLASS_USER_INTERACTIVE: return THREAD_QOS_USER_INTERACTIVE;
856*8d741a5dSApple OSS Distributions 	case QOS_CLASS_USER_INITIATED:   return THREAD_QOS_USER_INITIATED;
857*8d741a5dSApple OSS Distributions 	case QOS_CLASS_DEFAULT:          return THREAD_QOS_DEFAULT;
858*8d741a5dSApple OSS Distributions 	case QOS_CLASS_UTILITY:          return THREAD_QOS_UTILITY;
859*8d741a5dSApple OSS Distributions 	case QOS_CLASS_BACKGROUND:       return THREAD_QOS_BACKGROUND;
860*8d741a5dSApple OSS Distributions 	case QOS_CLASS_MAINTENANCE:      return THREAD_QOS_MAINTENANCE;
861*8d741a5dSApple OSS Distributions 	default: return THREAD_QOS_UNSPECIFIED;
862*8d741a5dSApple OSS Distributions 	}
863*8d741a5dSApple OSS Distributions }
864*8d741a5dSApple OSS Distributions 
865*8d741a5dSApple OSS Distributions static void
send(mach_port_t send_port,mach_port_t reply_port,mach_port_t msg_port,qos_class_t qos,mach_msg_option_t options)866*8d741a5dSApple OSS Distributions send(
867*8d741a5dSApple OSS Distributions 	mach_port_t send_port,
868*8d741a5dSApple OSS Distributions 	mach_port_t reply_port,
869*8d741a5dSApple OSS Distributions 	mach_port_t msg_port,
870*8d741a5dSApple OSS Distributions 	qos_class_t qos,
871*8d741a5dSApple OSS Distributions 	mach_msg_option_t options)
872*8d741a5dSApple OSS Distributions {
873*8d741a5dSApple OSS Distributions 	kern_return_t ret = 0;
874*8d741a5dSApple OSS Distributions 	mach_msg_priority_t priority = (uint32_t)_pthread_qos_class_encode(qos, 0, 0);
875*8d741a5dSApple OSS Distributions 
876*8d741a5dSApple OSS Distributions 	struct test_msg send_msg = {
877*8d741a5dSApple OSS Distributions 		.header = {
878*8d741a5dSApple OSS Distributions 			.msgh_remote_port = send_port,
879*8d741a5dSApple OSS Distributions 			.msgh_local_port  = reply_port,
880*8d741a5dSApple OSS Distributions 			.msgh_bits        = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND,
881*8d741a5dSApple OSS Distributions 	    reply_port ? MACH_MSG_TYPE_MAKE_SEND_ONCE : 0, 0,
882*8d741a5dSApple OSS Distributions 	    MACH_MSGH_BITS_COMPLEX),
883*8d741a5dSApple OSS Distributions 			.msgh_id          = 0x100,
884*8d741a5dSApple OSS Distributions 			.msgh_size        = sizeof(send_msg),
885*8d741a5dSApple OSS Distributions 		},
886*8d741a5dSApple OSS Distributions 		.body = {
887*8d741a5dSApple OSS Distributions 			.msgh_descriptor_count = 1,
888*8d741a5dSApple OSS Distributions 		},
889*8d741a5dSApple OSS Distributions 		.port_descriptor = {
890*8d741a5dSApple OSS Distributions 			.name        = msg_port,
891*8d741a5dSApple OSS Distributions 			.disposition = MACH_MSG_TYPE_MOVE_RECEIVE,
892*8d741a5dSApple OSS Distributions 			.type        = MACH_MSG_PORT_DESCRIPTOR,
893*8d741a5dSApple OSS Distributions 		},
894*8d741a5dSApple OSS Distributions 		.opts = options,
895*8d741a5dSApple OSS Distributions 	};
896*8d741a5dSApple OSS Distributions 
897*8d741a5dSApple OSS Distributions 	if (msg_port == MACH_PORT_NULL) {
898*8d741a5dSApple OSS Distributions 		send_msg.body.msgh_descriptor_count = 0;
899*8d741a5dSApple OSS Distributions 	}
900*8d741a5dSApple OSS Distributions 
901*8d741a5dSApple OSS Distributions 	send_msg.qos = priority;
902*8d741a5dSApple OSS Distributions 	priority = mach_msg_priority_encode(0, thread_qos_from_qos_class(qos), 0);
903*8d741a5dSApple OSS Distributions 
904*8d741a5dSApple OSS Distributions 	mach_msg_option_t send_opts = options;
905*8d741a5dSApple OSS Distributions 	send_opts |= MACH_SEND_MSG | MACH_SEND_TIMEOUT | MACH_SEND_OVERRIDE;
906*8d741a5dSApple OSS Distributions 
907*8d741a5dSApple OSS Distributions 	ret = mach_msg(&send_msg.header, send_opts, send_msg.header.msgh_size,
908*8d741a5dSApple OSS Distributions 	    0, MACH_PORT_NULL, 10000, priority);
909*8d741a5dSApple OSS Distributions 
910*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "client mach_msg");
911*8d741a5dSApple OSS Distributions }
912*8d741a5dSApple OSS Distributions 
913*8d741a5dSApple OSS Distributions static kern_return_t
receive(mach_port_t rcv_port,mach_port_t notify_port)914*8d741a5dSApple OSS Distributions receive(
915*8d741a5dSApple OSS Distributions 	mach_port_t rcv_port,
916*8d741a5dSApple OSS Distributions 	mach_port_t notify_port)
917*8d741a5dSApple OSS Distributions {
918*8d741a5dSApple OSS Distributions 	kern_return_t ret = 0;
919*8d741a5dSApple OSS Distributions 
920*8d741a5dSApple OSS Distributions 	struct test_msg rcv_msg = {
921*8d741a5dSApple OSS Distributions 		.header = {
922*8d741a5dSApple OSS Distributions 			.msgh_remote_port = MACH_PORT_NULL,
923*8d741a5dSApple OSS Distributions 			.msgh_local_port  = rcv_port,
924*8d741a5dSApple OSS Distributions 			.msgh_size        = sizeof(rcv_msg),
925*8d741a5dSApple OSS Distributions 		},
926*8d741a5dSApple OSS Distributions 	};
927*8d741a5dSApple OSS Distributions 
928*8d741a5dSApple OSS Distributions 	T_LOG("Client: Starting sync receive\n");
929*8d741a5dSApple OSS Distributions 
930*8d741a5dSApple OSS Distributions 	ret = mach_msg(&(rcv_msg.header),
931*8d741a5dSApple OSS Distributions 	    MACH_RCV_MSG |
932*8d741a5dSApple OSS Distributions 	    MACH_RCV_TIMEOUT |
933*8d741a5dSApple OSS Distributions 	    MACH_RCV_SYNC_WAIT,
934*8d741a5dSApple OSS Distributions 	    0,
935*8d741a5dSApple OSS Distributions 	    rcv_msg.header.msgh_size,
936*8d741a5dSApple OSS Distributions 	    rcv_port,
937*8d741a5dSApple OSS Distributions 	    SEND_TIMEOUT_SECS * 1000,
938*8d741a5dSApple OSS Distributions 	    notify_port);
939*8d741a5dSApple OSS Distributions 
940*8d741a5dSApple OSS Distributions 	return ret;
941*8d741a5dSApple OSS Distributions }
942*8d741a5dSApple OSS Distributions 
943*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_get_special_reply_port,
944*8d741a5dSApple OSS Distributions     "Test get_special_reply_port and it's corner cases.")
945*8d741a5dSApple OSS Distributions {
946*8d741a5dSApple OSS Distributions 	mach_port_t special_reply_port;
947*8d741a5dSApple OSS Distributions 	mach_port_t new_special_reply_port;
948*8d741a5dSApple OSS Distributions 
949*8d741a5dSApple OSS Distributions 	special_reply_port = thread_get_special_reply_port();
950*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
951*8d741a5dSApple OSS Distributions 
952*8d741a5dSApple OSS Distributions 	new_special_reply_port = thread_get_special_reply_port();
953*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(new_special_reply_port), "get_thread_special_reply_port");
954*8d741a5dSApple OSS Distributions 
955*8d741a5dSApple OSS Distributions 	mach_port_destroy(mach_task_self(), special_reply_port);
956*8d741a5dSApple OSS Distributions 	mach_port_destroy(mach_task_self(), new_special_reply_port);
957*8d741a5dSApple OSS Distributions 
958*8d741a5dSApple OSS Distributions 	new_special_reply_port = thread_get_special_reply_port();
959*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(new_special_reply_port), "get_thread_special_reply_port");
960*8d741a5dSApple OSS Distributions 
961*8d741a5dSApple OSS Distributions 	T_END;
962*8d741a5dSApple OSS Distributions }
963*8d741a5dSApple OSS Distributions 
964*8d741a5dSApple OSS Distributions static void *
qos_client_send_to_intransit(void * arg __unused)965*8d741a5dSApple OSS Distributions qos_client_send_to_intransit(void *arg __unused)
966*8d741a5dSApple OSS Distributions {
967*8d741a5dSApple OSS Distributions 	mach_port_t qos_send_port;
968*8d741a5dSApple OSS Distributions 	mach_port_t msg_port;
969*8d741a5dSApple OSS Distributions 	mach_port_t special_reply_port;
970*8d741a5dSApple OSS Distributions 
971*8d741a5dSApple OSS Distributions 	kern_return_t kr = bootstrap_look_up(bootstrap_port,
972*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &qos_send_port);
973*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
974*8d741a5dSApple OSS Distributions 
975*8d741a5dSApple OSS Distributions 	special_reply_port = thread_get_special_reply_port();
976*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
977*8d741a5dSApple OSS Distributions 
978*8d741a5dSApple OSS Distributions 	/* Create a rcv right to send in a msg */
979*8d741a5dSApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(),
980*8d741a5dSApple OSS Distributions 	    MACH_PORT_RIGHT_RECEIVE,
981*8d741a5dSApple OSS Distributions 	    &msg_port);
982*8d741a5dSApple OSS Distributions 
983*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client mach_port_allocate");
984*8d741a5dSApple OSS Distributions 
985*8d741a5dSApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(),
986*8d741a5dSApple OSS Distributions 	    msg_port,
987*8d741a5dSApple OSS Distributions 	    msg_port,
988*8d741a5dSApple OSS Distributions 	    MACH_MSG_TYPE_MAKE_SEND);
989*8d741a5dSApple OSS Distributions 
990*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client mach_port_insert_right");
991*8d741a5dSApple OSS Distributions 
992*8d741a5dSApple OSS Distributions 	/* Send an empty msg on the port to fire the WL thread */
993*8d741a5dSApple OSS Distributions 	send(qos_send_port, MACH_PORT_NULL, MACH_PORT_NULL,
994*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_BEFORE_OVERRIDE], 0);
995*8d741a5dSApple OSS Distributions 
996*8d741a5dSApple OSS Distributions 	/* Sleep 3 seconds for the server to start */
997*8d741a5dSApple OSS Distributions 	sleep(3);
998*8d741a5dSApple OSS Distributions 
999*8d741a5dSApple OSS Distributions 	/* Send the message with msg port as in-transit port, this msg will not be dequeued */
1000*8d741a5dSApple OSS Distributions 	send(qos_send_port, MACH_PORT_NULL, msg_port,
1001*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_BEFORE_OVERRIDE], 0);
1002*8d741a5dSApple OSS Distributions 
1003*8d741a5dSApple OSS Distributions 	/* Send 5 messages to msg port to make sure the port is full */
1004*8d741a5dSApple OSS Distributions 	for (int i = 0; i < 5; i++) {
1005*8d741a5dSApple OSS Distributions 		send(msg_port, MACH_PORT_NULL, MACH_PORT_NULL,
1006*8d741a5dSApple OSS Distributions 		    g_expected_qos[ENV_QOS_BEFORE_OVERRIDE], 0);
1007*8d741a5dSApple OSS Distributions 	}
1008*8d741a5dSApple OSS Distributions 
1009*8d741a5dSApple OSS Distributions 	T_LOG("Sent 5 msgs, now trying to send sync ipc message, which will block with a timeout\n");
1010*8d741a5dSApple OSS Distributions 	/* Send the message to the in-transit port, it should block and override the rcv's workloop */
1011*8d741a5dSApple OSS Distributions 	send(msg_port, special_reply_port, MACH_PORT_NULL,
1012*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_AFTER_OVERRIDE], 0);
1013*8d741a5dSApple OSS Distributions 	T_LOG("Client done sending messages, now waiting for server to end the test");
1014*8d741a5dSApple OSS Distributions 
1015*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1016*8d741a5dSApple OSS Distributions 	return NULL;
1017*8d741a5dSApple OSS Distributions }
1018*8d741a5dSApple OSS Distributions 
1019*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_send_to_intransit_with_thr_pri,
1020*8d741a5dSApple OSS Distributions     "Send synchronous messages from a pri thread to an intransit port")
1021*8d741a5dSApple OSS Distributions {
1022*8d741a5dSApple OSS Distributions 	thread_create_at_qos(g_expected_qos[ENV_QOS_AFTER_OVERRIDE], qos_client_send_to_intransit);
1023*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1024*8d741a5dSApple OSS Distributions }
1025*8d741a5dSApple OSS Distributions 
1026*8d741a5dSApple OSS Distributions static pthread_t
thread_create_at_qos(qos_class_t qos,void * (* function)(void *))1027*8d741a5dSApple OSS Distributions thread_create_at_qos(qos_class_t qos, void * (*function)(void *))
1028*8d741a5dSApple OSS Distributions {
1029*8d741a5dSApple OSS Distributions 	qos_class_t qos_thread;
1030*8d741a5dSApple OSS Distributions 	pthread_t thread;
1031*8d741a5dSApple OSS Distributions 	pthread_attr_t attr;
1032*8d741a5dSApple OSS Distributions 	int ret;
1033*8d741a5dSApple OSS Distributions 
1034*8d741a5dSApple OSS Distributions 	ret = setpriority(PRIO_DARWIN_ROLE, 0, PRIO_DARWIN_ROLE_UI_FOCAL);
1035*8d741a5dSApple OSS Distributions 	if (ret != 0) {
1036*8d741a5dSApple OSS Distributions 		T_LOG("set priority failed\n");
1037*8d741a5dSApple OSS Distributions 	}
1038*8d741a5dSApple OSS Distributions 
1039*8d741a5dSApple OSS Distributions 	pthread_attr_init(&attr);
1040*8d741a5dSApple OSS Distributions 	pthread_attr_set_qos_class_np(&attr, qos, 0);
1041*8d741a5dSApple OSS Distributions 	pthread_create(&thread, &attr, function, NULL);
1042*8d741a5dSApple OSS Distributions 
1043*8d741a5dSApple OSS Distributions 	T_LOG("pthread created\n");
1044*8d741a5dSApple OSS Distributions 	pthread_get_qos_class_np(thread, &qos_thread, NULL);
1045*8d741a5dSApple OSS Distributions 	T_EXPECT_EQ(qos_thread, (qos_class_t)qos, NULL);
1046*8d741a5dSApple OSS Distributions 	return thread;
1047*8d741a5dSApple OSS Distributions }
1048*8d741a5dSApple OSS Distributions 
1049*8d741a5dSApple OSS Distributions static void *
qos_send_and_sync_rcv(void * arg __unused)1050*8d741a5dSApple OSS Distributions qos_send_and_sync_rcv(void *arg __unused)
1051*8d741a5dSApple OSS Distributions {
1052*8d741a5dSApple OSS Distributions 	mach_port_t qos_send_port;
1053*8d741a5dSApple OSS Distributions 	mach_port_t special_reply_port;
1054*8d741a5dSApple OSS Distributions 
1055*8d741a5dSApple OSS Distributions 	T_LOG("Client: from created thread\n");
1056*8d741a5dSApple OSS Distributions 	T_EXPECT_EFFECTIVE_QOS_EQ(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
1057*8d741a5dSApple OSS Distributions 	    "pthread QoS should be %s", g_expected_qos_name[ENV_QOS_AFTER_OVERRIDE]);
1058*8d741a5dSApple OSS Distributions 
1059*8d741a5dSApple OSS Distributions 	kern_return_t kr = bootstrap_look_up(bootstrap_port,
1060*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &qos_send_port);
1061*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
1062*8d741a5dSApple OSS Distributions 
1063*8d741a5dSApple OSS Distributions 	special_reply_port = thread_get_special_reply_port();
1064*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
1065*8d741a5dSApple OSS Distributions 
1066*8d741a5dSApple OSS Distributions 	/* enqueue two messages to make sure that mqueue is not empty */
1067*8d741a5dSApple OSS Distributions 	send(qos_send_port, MACH_PORT_NULL, MACH_PORT_NULL,
1068*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_QUEUE_OVERRIDE], 0);
1069*8d741a5dSApple OSS Distributions 
1070*8d741a5dSApple OSS Distributions 	send(qos_send_port, MACH_PORT_NULL, MACH_PORT_NULL,
1071*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_QUEUE_OVERRIDE], 0);
1072*8d741a5dSApple OSS Distributions 
1073*8d741a5dSApple OSS Distributions 	sleep(SEND_TIMEOUT_SECS);
1074*8d741a5dSApple OSS Distributions 
1075*8d741a5dSApple OSS Distributions 	/* sync wait on msg port */
1076*8d741a5dSApple OSS Distributions 	receive(special_reply_port, qos_send_port);
1077*8d741a5dSApple OSS Distributions 
1078*8d741a5dSApple OSS Distributions 	T_LOG("Client done doing sync rcv, now waiting for server to end the test");
1079*8d741a5dSApple OSS Distributions 	sleep(SEND_TIMEOUT_SECS);
1080*8d741a5dSApple OSS Distributions 
1081*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1082*8d741a5dSApple OSS Distributions 	return NULL;
1083*8d741a5dSApple OSS Distributions }
1084*8d741a5dSApple OSS Distributions 
1085*8d741a5dSApple OSS Distributions static void *
qos_sync_rcv(void * arg __unused)1086*8d741a5dSApple OSS Distributions qos_sync_rcv(void *arg __unused)
1087*8d741a5dSApple OSS Distributions {
1088*8d741a5dSApple OSS Distributions 	mach_port_t qos_send_port;
1089*8d741a5dSApple OSS Distributions 	mach_port_t special_reply_port;
1090*8d741a5dSApple OSS Distributions 
1091*8d741a5dSApple OSS Distributions 	T_LOG("Client: from created thread\n");
1092*8d741a5dSApple OSS Distributions 
1093*8d741a5dSApple OSS Distributions 	kern_return_t kr = bootstrap_look_up(bootstrap_port,
1094*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &qos_send_port);
1095*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
1096*8d741a5dSApple OSS Distributions 
1097*8d741a5dSApple OSS Distributions 	special_reply_port = thread_get_special_reply_port();
1098*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
1099*8d741a5dSApple OSS Distributions 
1100*8d741a5dSApple OSS Distributions 	/* enqueue two messages to make sure that mqueue is not empty */
1101*8d741a5dSApple OSS Distributions 	send(qos_send_port, MACH_PORT_NULL, MACH_PORT_NULL,
1102*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_QUEUE_OVERRIDE], 0);
1103*8d741a5dSApple OSS Distributions 
1104*8d741a5dSApple OSS Distributions 	sleep(RECV_TIMEOUT_SECS);
1105*8d741a5dSApple OSS Distributions 
1106*8d741a5dSApple OSS Distributions 	/* sync wait on msg port */
1107*8d741a5dSApple OSS Distributions 	receive(special_reply_port, qos_send_port);
1108*8d741a5dSApple OSS Distributions 
1109*8d741a5dSApple OSS Distributions 	T_LOG("Client done doing sync rcv, now waiting for server to end the test");
1110*8d741a5dSApple OSS Distributions 	sleep(SEND_TIMEOUT_SECS);
1111*8d741a5dSApple OSS Distributions 
1112*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1113*8d741a5dSApple OSS Distributions 	return NULL;
1114*8d741a5dSApple OSS Distributions }
1115*8d741a5dSApple OSS Distributions 
1116*8d741a5dSApple OSS Distributions static void
thread_wait_to_block(mach_port_t thread_port)1117*8d741a5dSApple OSS Distributions thread_wait_to_block(mach_port_t thread_port)
1118*8d741a5dSApple OSS Distributions {
1119*8d741a5dSApple OSS Distributions 	thread_extended_info_data_t extended_info;
1120*8d741a5dSApple OSS Distributions 	kern_return_t kr;
1121*8d741a5dSApple OSS Distributions 
1122*8d741a5dSApple OSS Distributions 	while (1) {
1123*8d741a5dSApple OSS Distributions 		mach_msg_type_number_t count = THREAD_EXTENDED_INFO_COUNT;
1124*8d741a5dSApple OSS Distributions 		kr = thread_info(thread_port, THREAD_EXTENDED_INFO,
1125*8d741a5dSApple OSS Distributions 		    (thread_info_t)&extended_info, &count);
1126*8d741a5dSApple OSS Distributions 
1127*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "thread_info");
1128*8d741a5dSApple OSS Distributions 
1129*8d741a5dSApple OSS Distributions 		if (extended_info.pth_run_state == TH_STATE_WAITING) {
1130*8d741a5dSApple OSS Distributions 			T_LOG("Target thread blocked\n");
1131*8d741a5dSApple OSS Distributions 			break;
1132*8d741a5dSApple OSS Distributions 		}
1133*8d741a5dSApple OSS Distributions 		thread_switch(thread_port, SWITCH_OPTION_DEPRESS, 0);
1134*8d741a5dSApple OSS Distributions 	}
1135*8d741a5dSApple OSS Distributions }
1136*8d741a5dSApple OSS Distributions 
1137*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_send_sync_and_sync_rcv,
1138*8d741a5dSApple OSS Distributions     "Send messages and syncronously wait for rcv")
1139*8d741a5dSApple OSS Distributions {
1140*8d741a5dSApple OSS Distributions 	thread_create_at_qos(g_expected_qos[ENV_QOS_AFTER_OVERRIDE], qos_send_and_sync_rcv);
1141*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1142*8d741a5dSApple OSS Distributions }
1143*8d741a5dSApple OSS Distributions 
1144*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_sync_rcv_qos_change,
1145*8d741a5dSApple OSS Distributions     "Send messages and syncronously wait for rcv and change qos of waiting thread")
1146*8d741a5dSApple OSS Distributions {
1147*8d741a5dSApple OSS Distributions 	pthread_t rcv_thread;
1148*8d741a5dSApple OSS Distributions 
1149*8d741a5dSApple OSS Distributions 	rcv_thread = thread_create_at_qos(g_expected_qos[ENV_QOS_BEFORE_OVERRIDE], qos_sync_rcv);
1150*8d741a5dSApple OSS Distributions 
1151*8d741a5dSApple OSS Distributions 	T_LOG("Waiting for %d seconds before changing qos of rcv thread", SEND_TIMEOUT_SECS);
1152*8d741a5dSApple OSS Distributions 	sleep(SEND_TIMEOUT_SECS);
1153*8d741a5dSApple OSS Distributions 
1154*8d741a5dSApple OSS Distributions 	/* Wait for the thread to block */
1155*8d741a5dSApple OSS Distributions 	thread_wait_to_block(pthread_mach_thread_np(rcv_thread));
1156*8d741a5dSApple OSS Distributions 
1157*8d741a5dSApple OSS Distributions 	/* Update the rcv thread's qos */
1158*8d741a5dSApple OSS Distributions 	pthread_override_qos_class_start_np(rcv_thread, g_expected_qos[ENV_QOS_AFTER_OVERRIDE], 0);
1159*8d741a5dSApple OSS Distributions 
1160*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1161*8d741a5dSApple OSS Distributions }
1162*8d741a5dSApple OSS Distributions 
1163*8d741a5dSApple OSS Distributions static void *
qos_client_send_sync_msg_and_test_link(void * arg)1164*8d741a5dSApple OSS Distributions qos_client_send_sync_msg_and_test_link(void *arg)
1165*8d741a5dSApple OSS Distributions {
1166*8d741a5dSApple OSS Distributions 	mach_port_t qos_send_port;
1167*8d741a5dSApple OSS Distributions 	mach_port_t special_reply_port;
1168*8d741a5dSApple OSS Distributions 	boolean_t in_effect = FALSE;
1169*8d741a5dSApple OSS Distributions 	kern_return_t kr;
1170*8d741a5dSApple OSS Distributions 	unsigned long expected_result = (unsigned long) arg;
1171*8d741a5dSApple OSS Distributions 
1172*8d741a5dSApple OSS Distributions 	kr = bootstrap_look_up(bootstrap_port,
1173*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &qos_send_port);
1174*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
1175*8d741a5dSApple OSS Distributions 
1176*8d741a5dSApple OSS Distributions 	/* start monitoring sync ipc link */
1177*8d741a5dSApple OSS Distributions 	kr = mach_sync_ipc_link_monitoring_start(&special_reply_port);
1178*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_sync_ipc_link_monitoring_start");
1179*8d741a5dSApple OSS Distributions 
1180*8d741a5dSApple OSS Distributions 	/* Send the message to msg port */
1181*8d741a5dSApple OSS Distributions 	send(qos_send_port, special_reply_port, MACH_PORT_NULL,
1182*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_AFTER_OVERRIDE], 0);
1183*8d741a5dSApple OSS Distributions 
1184*8d741a5dSApple OSS Distributions 	/*
1185*8d741a5dSApple OSS Distributions 	 * wait for the reply
1186*8d741a5dSApple OSS Distributions 	 * some tests do not send a msg back so the receive
1187*8d741a5dSApple OSS Distributions 	 * might fail
1188*8d741a5dSApple OSS Distributions 	 */
1189*8d741a5dSApple OSS Distributions 	receive(special_reply_port, qos_send_port);
1190*8d741a5dSApple OSS Distributions 
1191*8d741a5dSApple OSS Distributions 	/* stop monitoring link */
1192*8d741a5dSApple OSS Distributions 	kr = mach_sync_ipc_link_monitoring_stop(special_reply_port, &in_effect);
1193*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_sync_ipc_link_monitoring_stop");
1194*8d741a5dSApple OSS Distributions 
1195*8d741a5dSApple OSS Distributions 	if (!in_effect) {
1196*8d741a5dSApple OSS Distributions 		T_LOG("Link was broken");
1197*8d741a5dSApple OSS Distributions 	} else {
1198*8d741a5dSApple OSS Distributions 		T_LOG("Link correct");
1199*8d741a5dSApple OSS Distributions 	}
1200*8d741a5dSApple OSS Distributions 
1201*8d741a5dSApple OSS Distributions 	if (expected_result == 1) {
1202*8d741a5dSApple OSS Distributions 		T_ASSERT_TRUE(in_effect, "special reply port link after rcv");
1203*8d741a5dSApple OSS Distributions 	} else {
1204*8d741a5dSApple OSS Distributions 		T_ASSERT_FALSE(in_effect, "special reply port link after rcv");
1205*8d741a5dSApple OSS Distributions 	}
1206*8d741a5dSApple OSS Distributions 	T_END;
1207*8d741a5dSApple OSS Distributions }
1208*8d741a5dSApple OSS Distributions 
1209*8d741a5dSApple OSS Distributions static void *
qos_client_send_2sync_msg_and_test_link(void * arg)1210*8d741a5dSApple OSS Distributions qos_client_send_2sync_msg_and_test_link(void *arg)
1211*8d741a5dSApple OSS Distributions {
1212*8d741a5dSApple OSS Distributions 	mach_port_t qos_send_port;
1213*8d741a5dSApple OSS Distributions 	mach_port_t special_reply_port;
1214*8d741a5dSApple OSS Distributions 	boolean_t in_effect = FALSE;
1215*8d741a5dSApple OSS Distributions 	kern_return_t kr;
1216*8d741a5dSApple OSS Distributions 	unsigned long expected_result = (unsigned long) arg;
1217*8d741a5dSApple OSS Distributions 
1218*8d741a5dSApple OSS Distributions 	kr = bootstrap_look_up(bootstrap_port,
1219*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &qos_send_port);
1220*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
1221*8d741a5dSApple OSS Distributions 
1222*8d741a5dSApple OSS Distributions 	/* start monitoring sync ipc link */
1223*8d741a5dSApple OSS Distributions 	kr = mach_sync_ipc_link_monitoring_start(&special_reply_port);
1224*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_sync_ipc_link_monitoring_start");
1225*8d741a5dSApple OSS Distributions 
1226*8d741a5dSApple OSS Distributions 	/* Send the first message to msg port */
1227*8d741a5dSApple OSS Distributions 	send(qos_send_port, special_reply_port, MACH_PORT_NULL,
1228*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_AFTER_OVERRIDE], 0);
1229*8d741a5dSApple OSS Distributions 
1230*8d741a5dSApple OSS Distributions 	/* wait for the reply */
1231*8d741a5dSApple OSS Distributions 	kr = receive(special_reply_port, qos_send_port);
1232*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "receive");
1233*8d741a5dSApple OSS Distributions 
1234*8d741a5dSApple OSS Distributions 	/* Send the second message to msg port */
1235*8d741a5dSApple OSS Distributions 	send(qos_send_port, special_reply_port, MACH_PORT_NULL,
1236*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_AFTER_OVERRIDE], 0);
1237*8d741a5dSApple OSS Distributions 
1238*8d741a5dSApple OSS Distributions 	/* wait for the reply */
1239*8d741a5dSApple OSS Distributions 	kr = receive(special_reply_port, qos_send_port);
1240*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "receive");
1241*8d741a5dSApple OSS Distributions 
1242*8d741a5dSApple OSS Distributions 	/* stop monitoring link */
1243*8d741a5dSApple OSS Distributions 	kr = mach_sync_ipc_link_monitoring_stop(special_reply_port, &in_effect);
1244*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_sync_ipc_link_monitoring_stop");
1245*8d741a5dSApple OSS Distributions 
1246*8d741a5dSApple OSS Distributions 	if (!in_effect) {
1247*8d741a5dSApple OSS Distributions 		T_LOG("Link was broken");
1248*8d741a5dSApple OSS Distributions 	} else {
1249*8d741a5dSApple OSS Distributions 		T_LOG("Link correct");
1250*8d741a5dSApple OSS Distributions 	}
1251*8d741a5dSApple OSS Distributions 
1252*8d741a5dSApple OSS Distributions 	if (expected_result == 1) {
1253*8d741a5dSApple OSS Distributions 		T_ASSERT_TRUE(in_effect, "special reply port link after rcv");
1254*8d741a5dSApple OSS Distributions 	} else {
1255*8d741a5dSApple OSS Distributions 		T_ASSERT_FALSE(in_effect, "special reply port link after rcv");
1256*8d741a5dSApple OSS Distributions 	}
1257*8d741a5dSApple OSS Distributions 	T_END;
1258*8d741a5dSApple OSS Distributions }
1259*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_send_sync_msg_with_link_check_correct_server,
1260*8d741a5dSApple OSS Distributions     "Send sync message, wait for reply and check sync ipc link")
1261*8d741a5dSApple OSS Distributions {
1262*8d741a5dSApple OSS Distributions 	pthread_t thread;
1263*8d741a5dSApple OSS Distributions 	pthread_attr_t attr;
1264*8d741a5dSApple OSS Distributions 	unsigned long expected_result = 1;
1265*8d741a5dSApple OSS Distributions 
1266*8d741a5dSApple OSS Distributions 	pthread_attr_init(&attr);
1267*8d741a5dSApple OSS Distributions 	pthread_create(&thread, &attr, qos_client_send_sync_msg_and_test_link, (void *)expected_result);
1268*8d741a5dSApple OSS Distributions 
1269*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1270*8d741a5dSApple OSS Distributions }
1271*8d741a5dSApple OSS Distributions 
1272*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_send_sync_msg_with_link_check_incorrect_server,
1273*8d741a5dSApple OSS Distributions     "Send sync message, wait for reply and check sync ipc link")
1274*8d741a5dSApple OSS Distributions {
1275*8d741a5dSApple OSS Distributions 	pthread_t thread;
1276*8d741a5dSApple OSS Distributions 	pthread_attr_t attr;
1277*8d741a5dSApple OSS Distributions 	unsigned long expected_result = 0;
1278*8d741a5dSApple OSS Distributions 
1279*8d741a5dSApple OSS Distributions 	pthread_attr_init(&attr);
1280*8d741a5dSApple OSS Distributions 	pthread_create(&thread, &attr, qos_client_send_sync_msg_and_test_link, (void *)expected_result);
1281*8d741a5dSApple OSS Distributions 
1282*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1283*8d741a5dSApple OSS Distributions }
1284*8d741a5dSApple OSS Distributions 
1285*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_send_2sync_msg_with_link_check_correct_server,
1286*8d741a5dSApple OSS Distributions     "Send sync message, wait for reply and check sync ipc link")
1287*8d741a5dSApple OSS Distributions {
1288*8d741a5dSApple OSS Distributions 	pthread_t thread;
1289*8d741a5dSApple OSS Distributions 	pthread_attr_t attr;
1290*8d741a5dSApple OSS Distributions 	unsigned long expected_result = 1;
1291*8d741a5dSApple OSS Distributions 
1292*8d741a5dSApple OSS Distributions 	pthread_attr_init(&attr);
1293*8d741a5dSApple OSS Distributions 	pthread_create(&thread, &attr, qos_client_send_2sync_msg_and_test_link, (void *)expected_result);
1294*8d741a5dSApple OSS Distributions 
1295*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1296*8d741a5dSApple OSS Distributions }
1297*8d741a5dSApple OSS Distributions 
1298*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_send_2sync_msg_with_link_check_incorrect_server,
1299*8d741a5dSApple OSS Distributions     "Send sync message, wait for reply and check sync ipc link")
1300*8d741a5dSApple OSS Distributions {
1301*8d741a5dSApple OSS Distributions 	pthread_t thread;
1302*8d741a5dSApple OSS Distributions 	pthread_attr_t attr;
1303*8d741a5dSApple OSS Distributions 	unsigned long expected_result = 0;
1304*8d741a5dSApple OSS Distributions 
1305*8d741a5dSApple OSS Distributions 	pthread_attr_init(&attr);
1306*8d741a5dSApple OSS Distributions 	pthread_create(&thread, &attr, qos_client_send_2sync_msg_and_test_link, (void *)expected_result);
1307*8d741a5dSApple OSS Distributions 
1308*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1309*8d741a5dSApple OSS Distributions }
1310*8d741a5dSApple OSS Distributions 
1311*8d741a5dSApple OSS Distributions static void *
qos_client_send_sync_msg(void * arg __unused)1312*8d741a5dSApple OSS Distributions qos_client_send_sync_msg(void *arg __unused)
1313*8d741a5dSApple OSS Distributions {
1314*8d741a5dSApple OSS Distributions 	mach_port_t qos_send_port;
1315*8d741a5dSApple OSS Distributions 	mach_port_t special_reply_port;
1316*8d741a5dSApple OSS Distributions 
1317*8d741a5dSApple OSS Distributions 	kern_return_t kr = bootstrap_look_up(bootstrap_port,
1318*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &qos_send_port);
1319*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
1320*8d741a5dSApple OSS Distributions 
1321*8d741a5dSApple OSS Distributions 	special_reply_port = thread_get_special_reply_port();
1322*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
1323*8d741a5dSApple OSS Distributions 
1324*8d741a5dSApple OSS Distributions 	/* Send the message to msg port */
1325*8d741a5dSApple OSS Distributions 	send(qos_send_port, special_reply_port, MACH_PORT_NULL,
1326*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_AFTER_OVERRIDE], 0);
1327*8d741a5dSApple OSS Distributions 
1328*8d741a5dSApple OSS Distributions 	/* wait for the reply */
1329*8d741a5dSApple OSS Distributions 	receive(special_reply_port, qos_send_port);
1330*8d741a5dSApple OSS Distributions 
1331*8d741a5dSApple OSS Distributions 	T_LOG("Client done sending messages, now waiting for server to end the test");
1332*8d741a5dSApple OSS Distributions 	sleep(2 * SEND_TIMEOUT_SECS);
1333*8d741a5dSApple OSS Distributions 
1334*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1335*8d741a5dSApple OSS Distributions 	return NULL;
1336*8d741a5dSApple OSS Distributions }
1337*8d741a5dSApple OSS Distributions 
1338*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_send_sync_msg_with_pri,
1339*8d741a5dSApple OSS Distributions     "Send sync message and wait for reply")
1340*8d741a5dSApple OSS Distributions {
1341*8d741a5dSApple OSS Distributions 	thread_create_at_qos(g_expected_qos[ENV_QOS_AFTER_OVERRIDE], qos_client_send_sync_msg);
1342*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1343*8d741a5dSApple OSS Distributions }
1344*8d741a5dSApple OSS Distributions 
1345*8d741a5dSApple OSS Distributions static void *
qos_client_kernel_upcall_send_sync_msg(void * arg __unused)1346*8d741a5dSApple OSS Distributions qos_client_kernel_upcall_send_sync_msg(void *arg __unused)
1347*8d741a5dSApple OSS Distributions {
1348*8d741a5dSApple OSS Distributions 	mach_port_t qos_send_port;
1349*8d741a5dSApple OSS Distributions 
1350*8d741a5dSApple OSS Distributions 	kern_return_t kr = bootstrap_look_up(bootstrap_port,
1351*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &qos_send_port);
1352*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
1353*8d741a5dSApple OSS Distributions 
1354*8d741a5dSApple OSS Distributions 	/* Call task_test_sync_upcall to perform a sync kernel upcall */
1355*8d741a5dSApple OSS Distributions 	kr = task_test_sync_upcall(mach_task_self(), qos_send_port);
1356*8d741a5dSApple OSS Distributions 	if (kr == KERN_NOT_SUPPORTED) {
1357*8d741a5dSApple OSS Distributions 		T_QUIET; T_SKIP("QOS Client Kernel Upcall test called on release kernel, skipping\n");
1358*8d741a5dSApple OSS Distributions 	}
1359*8d741a5dSApple OSS Distributions 
1360*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_test_sync_upcall");
1361*8d741a5dSApple OSS Distributions 
1362*8d741a5dSApple OSS Distributions 	T_LOG("Client done doing upcall, now waiting for server to end the test");
1363*8d741a5dSApple OSS Distributions 	sleep(2 * SEND_TIMEOUT_SECS);
1364*8d741a5dSApple OSS Distributions 
1365*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1366*8d741a5dSApple OSS Distributions 	return NULL;
1367*8d741a5dSApple OSS Distributions }
1368*8d741a5dSApple OSS Distributions 
1369*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_send_kernel_upcall_sync_msg_with_pri,
1370*8d741a5dSApple OSS Distributions     "Send Kernel upcall sync message and wait for reply")
1371*8d741a5dSApple OSS Distributions {
1372*8d741a5dSApple OSS Distributions 	thread_create_at_qos(g_expected_qos[ENV_QOS_AFTER_OVERRIDE], qos_client_kernel_upcall_send_sync_msg);
1373*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1374*8d741a5dSApple OSS Distributions }
1375*8d741a5dSApple OSS Distributions 
1376*8d741a5dSApple OSS Distributions static void *
qos_client_kernel_upcall_send_async_msg(void * arg __unused)1377*8d741a5dSApple OSS Distributions qos_client_kernel_upcall_send_async_msg(void *arg __unused)
1378*8d741a5dSApple OSS Distributions {
1379*8d741a5dSApple OSS Distributions 	mach_port_t qos_send_port;
1380*8d741a5dSApple OSS Distributions 
1381*8d741a5dSApple OSS Distributions 	kern_return_t kr = bootstrap_look_up(bootstrap_port,
1382*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &qos_send_port);
1383*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
1384*8d741a5dSApple OSS Distributions 
1385*8d741a5dSApple OSS Distributions 	/* Call task_test_async_upcall_propagation to perform an async kernel upcall */
1386*8d741a5dSApple OSS Distributions 	kr = task_test_async_upcall_propagation(mach_task_self(), qos_send_port, THREAD_QOS_UTILITY, USR_THROTTLE_LEVEL_TIER0);
1387*8d741a5dSApple OSS Distributions 	if (kr == KERN_NOT_SUPPORTED) {
1388*8d741a5dSApple OSS Distributions 		T_QUIET; T_SKIP("QOS Client Kernel Upcall test called on release kernel, skipping\n");
1389*8d741a5dSApple OSS Distributions 	}
1390*8d741a5dSApple OSS Distributions 
1391*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "task_test_async_upcall_propagation");
1392*8d741a5dSApple OSS Distributions 
1393*8d741a5dSApple OSS Distributions 	T_LOG("Client done doing upcall, now waiting for server to end the test");
1394*8d741a5dSApple OSS Distributions 	sleep(2 * SEND_TIMEOUT_SECS);
1395*8d741a5dSApple OSS Distributions 
1396*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1397*8d741a5dSApple OSS Distributions 	return NULL;
1398*8d741a5dSApple OSS Distributions }
1399*8d741a5dSApple OSS Distributions 
1400*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_iotier_client_send_kernel_upcall_async_msg,
1401*8d741a5dSApple OSS Distributions     "Send Kernel upcall async message")
1402*8d741a5dSApple OSS Distributions {
1403*8d741a5dSApple OSS Distributions 	thread_create_at_qos(g_expected_qos[ENV_QOS_AFTER_OVERRIDE], qos_client_kernel_upcall_send_async_msg);
1404*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1405*8d741a5dSApple OSS Distributions }
1406*8d741a5dSApple OSS Distributions 
1407*8d741a5dSApple OSS Distributions static void *
qos_client_send_two_sync_msg_high_qos(void * arg __unused)1408*8d741a5dSApple OSS Distributions qos_client_send_two_sync_msg_high_qos(void *arg __unused)
1409*8d741a5dSApple OSS Distributions {
1410*8d741a5dSApple OSS Distributions 	mach_port_t qos_send_port;
1411*8d741a5dSApple OSS Distributions 	mach_port_t special_reply_port;
1412*8d741a5dSApple OSS Distributions 
1413*8d741a5dSApple OSS Distributions 	kern_return_t kr = bootstrap_look_up(bootstrap_port,
1414*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &qos_send_port);
1415*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
1416*8d741a5dSApple OSS Distributions 
1417*8d741a5dSApple OSS Distributions 	special_reply_port = thread_get_special_reply_port();
1418*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
1419*8d741a5dSApple OSS Distributions 
1420*8d741a5dSApple OSS Distributions 	/* Send the message to msg port */
1421*8d741a5dSApple OSS Distributions 	send(qos_send_port, special_reply_port, MACH_PORT_NULL,
1422*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_AFTER_OVERRIDE], 0);
1423*8d741a5dSApple OSS Distributions 
1424*8d741a5dSApple OSS Distributions 	/* wait for the reply */
1425*8d741a5dSApple OSS Distributions 	receive(special_reply_port, qos_send_port);
1426*8d741a5dSApple OSS Distributions 
1427*8d741a5dSApple OSS Distributions 	T_LOG("Client done sending messages, now waiting for server to end the test");
1428*8d741a5dSApple OSS Distributions 	sleep(SEND_TIMEOUT_SECS);
1429*8d741a5dSApple OSS Distributions 
1430*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1431*8d741a5dSApple OSS Distributions 	return NULL;
1432*8d741a5dSApple OSS Distributions }
1433*8d741a5dSApple OSS Distributions 
1434*8d741a5dSApple OSS Distributions static void *
qos_client_send_two_sync_msg_low_qos(void * arg __unused)1435*8d741a5dSApple OSS Distributions qos_client_send_two_sync_msg_low_qos(void *arg __unused)
1436*8d741a5dSApple OSS Distributions {
1437*8d741a5dSApple OSS Distributions 	mach_port_t qos_send_port;
1438*8d741a5dSApple OSS Distributions 	mach_port_t special_reply_port;
1439*8d741a5dSApple OSS Distributions 
1440*8d741a5dSApple OSS Distributions 	kern_return_t kr = bootstrap_look_up(bootstrap_port,
1441*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &qos_send_port);
1442*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
1443*8d741a5dSApple OSS Distributions 
1444*8d741a5dSApple OSS Distributions 	special_reply_port = thread_get_special_reply_port();
1445*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
1446*8d741a5dSApple OSS Distributions 
1447*8d741a5dSApple OSS Distributions 	/* Send the message to msg port */
1448*8d741a5dSApple OSS Distributions 	send(qos_send_port, special_reply_port, MACH_PORT_NULL,
1449*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_BEFORE_OVERRIDE], 0);
1450*8d741a5dSApple OSS Distributions 
1451*8d741a5dSApple OSS Distributions 	/* wait for the reply */
1452*8d741a5dSApple OSS Distributions 	receive(special_reply_port, qos_send_port);
1453*8d741a5dSApple OSS Distributions 
1454*8d741a5dSApple OSS Distributions 	T_LOG("Client done sending messages, now waiting for server to end the test");
1455*8d741a5dSApple OSS Distributions 	sleep(SEND_TIMEOUT_SECS);
1456*8d741a5dSApple OSS Distributions 
1457*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1458*8d741a5dSApple OSS Distributions 	return NULL;
1459*8d741a5dSApple OSS Distributions }
1460*8d741a5dSApple OSS Distributions 
1461*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_send_two_sync_msg_with_thr_pri,
1462*8d741a5dSApple OSS Distributions     "Send messages sync msgs from 2 threads at given thread pri")
1463*8d741a5dSApple OSS Distributions {
1464*8d741a5dSApple OSS Distributions 	thread_create_at_qos(g_expected_qos[ENV_QOS_AFTER_OVERRIDE], qos_client_send_two_sync_msg_high_qos);
1465*8d741a5dSApple OSS Distributions 	sleep(INTERMITTENT_TIMEOUT_SEC);
1466*8d741a5dSApple OSS Distributions 	thread_create_at_qos(g_expected_qos[ENV_QOS_BEFORE_OVERRIDE], qos_client_send_two_sync_msg_low_qos);
1467*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1468*8d741a5dSApple OSS Distributions }
1469*8d741a5dSApple OSS Distributions 
1470*8d741a5dSApple OSS Distributions static mach_port_t other_thread_reply_port = MACH_PORT_NULL;
1471*8d741a5dSApple OSS Distributions static void *
qos_client_destroy_other_threads_port(void * arg __unused)1472*8d741a5dSApple OSS Distributions qos_client_destroy_other_threads_port(void *arg __unused)
1473*8d741a5dSApple OSS Distributions {
1474*8d741a5dSApple OSS Distributions 	T_LOG("Waiting 6 seconds before destroying other thread's reply port");
1475*8d741a5dSApple OSS Distributions 	sleep(SEND_TIMEOUT_SECS);
1476*8d741a5dSApple OSS Distributions 
1477*8d741a5dSApple OSS Distributions 	T_LOG("Destroying other thread's special reply port ");
1478*8d741a5dSApple OSS Distributions 	mach_port_destroy(mach_task_self(), other_thread_reply_port);
1479*8d741a5dSApple OSS Distributions 
1480*8d741a5dSApple OSS Distributions 	T_LOG("Other thread done destroying ");
1481*8d741a5dSApple OSS Distributions 	sleep(3 * SEND_TIMEOUT_SECS);
1482*8d741a5dSApple OSS Distributions 
1483*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1484*8d741a5dSApple OSS Distributions 	return NULL;
1485*8d741a5dSApple OSS Distributions }
1486*8d741a5dSApple OSS Distributions 
1487*8d741a5dSApple OSS Distributions static void *
qos_client_create_sepcial_reply_and_spawn_thread(void * arg __unused)1488*8d741a5dSApple OSS Distributions qos_client_create_sepcial_reply_and_spawn_thread(void *arg __unused)
1489*8d741a5dSApple OSS Distributions {
1490*8d741a5dSApple OSS Distributions 	mach_port_t qos_send_port;
1491*8d741a5dSApple OSS Distributions 	mach_port_t special_reply_port;
1492*8d741a5dSApple OSS Distributions 
1493*8d741a5dSApple OSS Distributions 	kern_return_t kr = bootstrap_look_up(bootstrap_port,
1494*8d741a5dSApple OSS Distributions 	    KEVENT_QOS_SERVICE_NAME, &qos_send_port);
1495*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
1496*8d741a5dSApple OSS Distributions 
1497*8d741a5dSApple OSS Distributions 	special_reply_port = thread_get_special_reply_port();
1498*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(special_reply_port), "get_thread_special_reply_port");
1499*8d741a5dSApple OSS Distributions 
1500*8d741a5dSApple OSS Distributions 	other_thread_reply_port = special_reply_port;
1501*8d741a5dSApple OSS Distributions 
1502*8d741a5dSApple OSS Distributions 	/* Send an async message */
1503*8d741a5dSApple OSS Distributions 	send(qos_send_port, MACH_PORT_NULL, MACH_PORT_NULL,
1504*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_BEFORE_OVERRIDE], 0);
1505*8d741a5dSApple OSS Distributions 
1506*8d741a5dSApple OSS Distributions 	/* Send the sync ipc message */
1507*8d741a5dSApple OSS Distributions 	send(qos_send_port, special_reply_port, MACH_PORT_NULL,
1508*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_BEFORE_OVERRIDE], 0);
1509*8d741a5dSApple OSS Distributions 
1510*8d741a5dSApple OSS Distributions 	/* Create a new thread to send the sync message on our special reply port */
1511*8d741a5dSApple OSS Distributions 	thread_create_at_qos(g_expected_qos[ENV_QOS_AFTER_OVERRIDE], qos_client_destroy_other_threads_port);
1512*8d741a5dSApple OSS Distributions 
1513*8d741a5dSApple OSS Distributions 	/* Client starting to receive message */
1514*8d741a5dSApple OSS Distributions 	receive(special_reply_port, qos_send_port);
1515*8d741a5dSApple OSS Distributions 
1516*8d741a5dSApple OSS Distributions 	sleep(3 * SEND_TIMEOUT_SECS);
1517*8d741a5dSApple OSS Distributions 
1518*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1519*8d741a5dSApple OSS Distributions 	return NULL;
1520*8d741a5dSApple OSS Distributions }
1521*8d741a5dSApple OSS Distributions 
1522*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_send_two_msg_and_destroy,
1523*8d741a5dSApple OSS Distributions     "Send a message with another threads special reply port while that thread destroys the port")
1524*8d741a5dSApple OSS Distributions {
1525*8d741a5dSApple OSS Distributions 	thread_create_at_qos(g_expected_qos[ENV_QOS_AFTER_OVERRIDE], qos_client_create_sepcial_reply_and_spawn_thread);
1526*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1527*8d741a5dSApple OSS Distributions }
1528*8d741a5dSApple OSS Distributions 
1529*8d741a5dSApple OSS Distributions static mach_port_t send_complex_connection_port = MACH_PORT_NULL;
1530*8d741a5dSApple OSS Distributions 
1531*8d741a5dSApple OSS Distributions static void *
qos_client_send_complex_msg_to_service_port(void * arg __unused)1532*8d741a5dSApple OSS Distributions qos_client_send_complex_msg_to_service_port(void *arg __unused)
1533*8d741a5dSApple OSS Distributions {
1534*8d741a5dSApple OSS Distributions 	mach_port_t svc_port, tsr_port, conn_port;
1535*8d741a5dSApple OSS Distributions 	kern_return_t kr;
1536*8d741a5dSApple OSS Distributions 
1537*8d741a5dSApple OSS Distributions 	kr = bootstrap_look_up(bootstrap_port, KEVENT_QOS_SERVICE_NAME, &svc_port);
1538*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "client bootstrap_look_up");
1539*8d741a5dSApple OSS Distributions 
1540*8d741a5dSApple OSS Distributions 	tsr_port = thread_get_special_reply_port();
1541*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(tsr_port), "get_thread_special_reply_port");
1542*8d741a5dSApple OSS Distributions 
1543*8d741a5dSApple OSS Distributions 	conn_port = send_complex_connection_port;
1544*8d741a5dSApple OSS Distributions 
1545*8d741a5dSApple OSS Distributions 	T_LOG("Sending to the service port with a sync IPC");
1546*8d741a5dSApple OSS Distributions 	send(svc_port, tsr_port, conn_port,
1547*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_BEFORE_OVERRIDE],
1548*8d741a5dSApple OSS Distributions 	    MACH_SEND_PROPAGATE_QOS);
1549*8d741a5dSApple OSS Distributions 
1550*8d741a5dSApple OSS Distributions 	receive(tsr_port, svc_port);
1551*8d741a5dSApple OSS Distributions 
1552*8d741a5dSApple OSS Distributions 	sleep(3 * SEND_TIMEOUT_SECS);
1553*8d741a5dSApple OSS Distributions 
1554*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1555*8d741a5dSApple OSS Distributions 	return NULL;
1556*8d741a5dSApple OSS Distributions }
1557*8d741a5dSApple OSS Distributions 
1558*8d741a5dSApple OSS Distributions static void *
qos_client_send_to_connection_then_service_port(void * arg __unused)1559*8d741a5dSApple OSS Distributions qos_client_send_to_connection_then_service_port(void *arg __unused)
1560*8d741a5dSApple OSS Distributions {
1561*8d741a5dSApple OSS Distributions 	mach_port_t tsr_port, conn_port;
1562*8d741a5dSApple OSS Distributions 	mach_port_options_t opts = {
1563*8d741a5dSApple OSS Distributions 		.flags = MPO_INSERT_SEND_RIGHT,
1564*8d741a5dSApple OSS Distributions 	};
1565*8d741a5dSApple OSS Distributions 	kern_return_t kr;
1566*8d741a5dSApple OSS Distributions 
1567*8d741a5dSApple OSS Distributions 	kr = mach_port_construct(mach_task_self(), &opts, 0ull, &conn_port);
1568*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_port_construct");
1569*8d741a5dSApple OSS Distributions 	send_complex_connection_port = conn_port;
1570*8d741a5dSApple OSS Distributions 
1571*8d741a5dSApple OSS Distributions 	tsr_port = thread_get_special_reply_port();
1572*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(MACH_PORT_VALID(tsr_port), "get_thread_special_reply_port");
1573*8d741a5dSApple OSS Distributions 
1574*8d741a5dSApple OSS Distributions 	T_LOG("Sending to the connection port with a sync IPC");
1575*8d741a5dSApple OSS Distributions 	send(conn_port, tsr_port, MACH_PORT_NULL,
1576*8d741a5dSApple OSS Distributions 	    g_expected_qos[ENV_QOS_BEFORE_OVERRIDE],
1577*8d741a5dSApple OSS Distributions 	    MACH_SEND_PROPAGATE_QOS);
1578*8d741a5dSApple OSS Distributions 
1579*8d741a5dSApple OSS Distributions 	thread_create_at_qos(g_expected_qos[ENV_QOS_AFTER_OVERRIDE],
1580*8d741a5dSApple OSS Distributions 	    qos_client_send_complex_msg_to_service_port);
1581*8d741a5dSApple OSS Distributions 
1582*8d741a5dSApple OSS Distributions 	receive(tsr_port, conn_port);
1583*8d741a5dSApple OSS Distributions 
1584*8d741a5dSApple OSS Distributions 	sleep(3 * SEND_TIMEOUT_SECS);
1585*8d741a5dSApple OSS Distributions 
1586*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("client timed out");
1587*8d741a5dSApple OSS Distributions 	return NULL;
1588*8d741a5dSApple OSS Distributions }
1589*8d741a5dSApple OSS Distributions 
1590*8d741a5dSApple OSS Distributions T_HELPER_DECL(qos_client_send_complex_msg_with_pri,
1591*8d741a5dSApple OSS Distributions     "Send a message with several ports causing links")
1592*8d741a5dSApple OSS Distributions {
1593*8d741a5dSApple OSS Distributions 	thread_create_at_qos(g_expected_qos[ENV_QOS_BEFORE_OVERRIDE],
1594*8d741a5dSApple OSS Distributions 	    qos_client_send_to_connection_then_service_port);
1595*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1596*8d741a5dSApple OSS Distributions }
1597*8d741a5dSApple OSS Distributions 
1598*8d741a5dSApple OSS Distributions static void
run_client_server(const char * server_name,const char * client_name,qos_class_t qos[],const char * qos_name[],const char * wl_function)1599*8d741a5dSApple OSS Distributions run_client_server(const char *server_name, const char *client_name, qos_class_t qos[],
1600*8d741a5dSApple OSS Distributions     const char *qos_name[], const char *wl_function)
1601*8d741a5dSApple OSS Distributions {
1602*8d741a5dSApple OSS Distributions 	char *env[2 * ENV_VAR_QOS + ENV_VAR_FUNCTION + 1];
1603*8d741a5dSApple OSS Distributions 	env_set_qos(env, qos, qos_name, wl_function);
1604*8d741a5dSApple OSS Distributions 
1605*8d741a5dSApple OSS Distributions 	for (int i = 0; i < ENV_VAR_QOS; i++) {
1606*8d741a5dSApple OSS Distributions 		g_expected_qos[i] = qos[i];
1607*8d741a5dSApple OSS Distributions 		g_expected_qos_name[i] = qos_name[i];
1608*8d741a5dSApple OSS Distributions 	}
1609*8d741a5dSApple OSS Distributions 
1610*8d741a5dSApple OSS Distributions 	dt_helper_t helpers[] = {
1611*8d741a5dSApple OSS Distributions 		dt_launchd_helper_domain("com.apple.xnu.test.kevent_qos.plist",
1612*8d741a5dSApple OSS Distributions 	    server_name, env, LAUNCH_SYSTEM_DOMAIN),
1613*8d741a5dSApple OSS Distributions 		dt_fork_helper(client_name)
1614*8d741a5dSApple OSS Distributions 	};
1615*8d741a5dSApple OSS Distributions 	dt_run_helpers(helpers, 2, HELPER_TIMEOUT_SECS);
1616*8d741a5dSApple OSS Distributions }
1617*8d741a5dSApple OSS Distributions 
1618*8d741a5dSApple OSS Distributions #pragma mark Mach receive - kevent_qos
1619*8d741a5dSApple OSS Distributions 
1620*8d741a5dSApple OSS Distributions static void
expect_kevent_id_recv(mach_port_t port,qos_class_t qos[],const char * qos_name[],const char * wl_function)1621*8d741a5dSApple OSS Distributions expect_kevent_id_recv(mach_port_t port, qos_class_t qos[], const char *qos_name[], const char *wl_function)
1622*8d741a5dSApple OSS Distributions {
1623*8d741a5dSApple OSS Distributions 	int r;
1624*8d741a5dSApple OSS Distributions 
1625*8d741a5dSApple OSS Distributions 	/* Qos expected by workloop thread */
1626*8d741a5dSApple OSS Distributions 	for (int i = 0; i < ENV_VAR_QOS; i++) {
1627*8d741a5dSApple OSS Distributions 		g_expected_qos[i] = qos[i];
1628*8d741a5dSApple OSS Distributions 		g_expected_qos_name[i] = qos_name[i];
1629*8d741a5dSApple OSS Distributions 	}
1630*8d741a5dSApple OSS Distributions 
1631*8d741a5dSApple OSS Distributions 	if (strcmp(wl_function, "workloop_cb_test_intransit") == 0) {
1632*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1633*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1634*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_intransit, 0, 0), NULL);
1635*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send") == 0) {
1636*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1637*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1638*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send, 0, 0), NULL);
1639*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_and_enable") == 0) {
1640*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1641*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1642*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_and_enable, 0, 0), NULL);
1643*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_and_enable_handoff") == 0) {
1644*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1645*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1646*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_and_enable_handoff, 0, 0), NULL);
1647*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_send_two_sync") == 0) {
1648*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1649*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1650*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_send_two_sync, 0, 0), NULL);
1651*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_two_send_and_destroy") == 0) {
1652*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1653*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1654*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_two_send_and_destroy, 0, 0), NULL);
1655*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_reply") == 0) {
1656*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1657*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1658*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_reply, 0, 0), NULL);
1659*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_deallocate") == 0) {
1660*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1661*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1662*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_deallocate, 0, 0), NULL);
1663*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_reply_kevent") == 0) {
1664*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1665*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1666*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_reply_kevent, 0, 0), NULL);
1667*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_reply_kevent_pthread") == 0) {
1668*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1669*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1670*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_reply_kevent_pthread, 0, 0), NULL);
1671*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_kevent_reply") == 0) {
1672*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1673*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1674*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_kevent_reply, 0, 0), NULL);
1675*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_do_nothing") == 0) {
1676*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1677*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1678*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_do_nothing, 0, 0), NULL);
1679*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_do_nothing_kevent_pthread") == 0) {
1680*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1681*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1682*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_do_nothing_kevent_pthread, 0, 0), NULL);
1683*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_do_nothing_exit") == 0) {
1684*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1685*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1686*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_do_nothing_exit, 0, 0), NULL);
1687*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_reply_kevent_reply_kevent") == 0) {
1688*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1689*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1690*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_reply_kevent_reply_kevent, 0, 0), NULL);
1691*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_kevent_reply_reply_kevent") == 0) {
1692*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1693*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1694*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_kevent_reply_reply_kevent, 0, 0), NULL);
1695*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_kevent_reply_kevent_reply") == 0) {
1696*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1697*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1698*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_kevent_reply_kevent_reply, 0, 0), NULL);
1699*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_sync_send_reply_kevent_kevent_reply") == 0) {
1700*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1701*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1702*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_sync_send_reply_kevent_kevent_reply, 0, 0), NULL);
1703*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_kernel_sync_send") == 0) {
1704*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1705*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1706*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_kernel_sync_send, 0, 0), NULL);
1707*8d741a5dSApple OSS Distributions 	} else if (strcmp(wl_function, "workloop_cb_test_kernel_async_send") == 0) {
1708*8d741a5dSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(_pthread_workqueue_init_with_workloop(
1709*8d741a5dSApple OSS Distributions 			    worker_cb, event_cb,
1710*8d741a5dSApple OSS Distributions 			    (pthread_workqueue_function_workloop_t)workloop_cb_test_kernel_async_send, 0, 0), NULL);
1711*8d741a5dSApple OSS Distributions 	} else {
1712*8d741a5dSApple OSS Distributions 		T_ASSERT_FAIL("no workloop function specified \n");
1713*8d741a5dSApple OSS Distributions 	}
1714*8d741a5dSApple OSS Distributions 
1715*8d741a5dSApple OSS Distributions 	struct kevent_qos_s kev = {
1716*8d741a5dSApple OSS Distributions 		.ident = port,
1717*8d741a5dSApple OSS Distributions 		.filter = EVFILT_MACHPORT,
1718*8d741a5dSApple OSS Distributions 		.flags = EV_ADD | EV_UDATA_SPECIFIC | EV_DISPATCH | EV_VANISHED,
1719*8d741a5dSApple OSS Distributions 		.fflags = (MACH_RCV_MSG | MACH_RCV_VOUCHER | MACH_RCV_LARGE | MACH_RCV_LARGE_IDENTITY |
1720*8d741a5dSApple OSS Distributions 	    MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AV) |
1721*8d741a5dSApple OSS Distributions 	    MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0)),
1722*8d741a5dSApple OSS Distributions 		.data = 1,
1723*8d741a5dSApple OSS Distributions 		.qos = (int32_t)_pthread_qos_class_encode(qos[ENV_QOS_QUEUE_OVERRIDE], 0, 0)
1724*8d741a5dSApple OSS Distributions 	};
1725*8d741a5dSApple OSS Distributions 
1726*8d741a5dSApple OSS Distributions 	struct kevent_qos_s kev_err = { 0 };
1727*8d741a5dSApple OSS Distributions 
1728*8d741a5dSApple OSS Distributions 	/* Setup workloop for mach msg rcv */
1729*8d741a5dSApple OSS Distributions 	r = kevent_id(25, &kev, 1, &kev_err, 1, NULL,
1730*8d741a5dSApple OSS Distributions 	    NULL, KEVENT_FLAG_WORKLOOP | KEVENT_FLAG_ERROR_EVENTS);
1731*8d741a5dSApple OSS Distributions 
1732*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(r, "kevent_id");
1733*8d741a5dSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(r, 0, "no errors returned from kevent_id");
1734*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1735*8d741a5dSApple OSS Distributions }
1736*8d741a5dSApple OSS Distributions 
1737*8d741a5dSApple OSS Distributions T_HELPER_DECL(server_kevent_id,
1738*8d741a5dSApple OSS Distributions     "Reply with the QoS that a dispatch source event handler ran with")
1739*8d741a5dSApple OSS Distributions {
1740*8d741a5dSApple OSS Distributions 	qos_class_t qos[ENV_VAR_QOS];
1741*8d741a5dSApple OSS Distributions 	const char *qos_name[ENV_VAR_QOS];
1742*8d741a5dSApple OSS Distributions 	const char *wl_function;
1743*8d741a5dSApple OSS Distributions 	environ_get_qos(qos, qos_name, &wl_function);
1744*8d741a5dSApple OSS Distributions 
1745*8d741a5dSApple OSS Distributions 	expect_kevent_id_recv(get_server_port(), qos, qos_name, wl_function);
1746*8d741a5dSApple OSS Distributions 	sleep(HELPER_TIMEOUT_SECS);
1747*8d741a5dSApple OSS Distributions 	T_ASSERT_FAIL("should receive a message within %d seconds",
1748*8d741a5dSApple OSS Distributions 	    RECV_TIMEOUT_SECS);
1749*8d741a5dSApple OSS Distributions }
1750*8d741a5dSApple OSS Distributions 
1751*8d741a5dSApple OSS Distributions static void *
special_reply_port_thread(void * ctxt)1752*8d741a5dSApple OSS Distributions special_reply_port_thread(void *ctxt)
1753*8d741a5dSApple OSS Distributions {
1754*8d741a5dSApple OSS Distributions 	kern_return_t ret;
1755*8d741a5dSApple OSS Distributions 	mach_port_t rcv_port = *(mach_port_t *)ctxt;
1756*8d741a5dSApple OSS Distributions 	struct test_msg rcv_msg = {
1757*8d741a5dSApple OSS Distributions 		.header = {
1758*8d741a5dSApple OSS Distributions 			.msgh_remote_port = MACH_PORT_NULL,
1759*8d741a5dSApple OSS Distributions 			.msgh_local_port  = rcv_port,
1760*8d741a5dSApple OSS Distributions 			.msgh_size        = sizeof(rcv_msg),
1761*8d741a5dSApple OSS Distributions 		},
1762*8d741a5dSApple OSS Distributions 	};
1763*8d741a5dSApple OSS Distributions 
1764*8d741a5dSApple OSS Distributions 	ret = mach_msg(&rcv_msg.header, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0,
1765*8d741a5dSApple OSS Distributions 	    rcv_msg.header.msgh_size, rcv_port, 1000, MACH_PORT_NULL);
1766*8d741a5dSApple OSS Distributions 
1767*8d741a5dSApple OSS Distributions 	T_EXPECT_EQ(ret, MACH_RCV_TIMED_OUT, "receive should not panic");
1768*8d741a5dSApple OSS Distributions 
1769*8d741a5dSApple OSS Distributions 	*(mach_port_t *)ctxt = MACH_PORT_NULL;
1770*8d741a5dSApple OSS Distributions 
1771*8d741a5dSApple OSS Distributions 	sleep(1); // give some time to pthread_exit
1772*8d741a5dSApple OSS Distributions 
1773*8d741a5dSApple OSS Distributions 	ret = mach_msg(&rcv_msg.header, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0,
1774*8d741a5dSApple OSS Distributions 	    rcv_msg.header.msgh_size, rcv_port, 1000, MACH_PORT_NULL);
1775*8d741a5dSApple OSS Distributions 
1776*8d741a5dSApple OSS Distributions 	T_EXPECT_EQ(ret, MACH_RCV_TIMED_OUT, "receive should not panic");
1777*8d741a5dSApple OSS Distributions 
1778*8d741a5dSApple OSS Distributions 	T_END;
1779*8d741a5dSApple OSS Distributions }
1780*8d741a5dSApple OSS Distributions 
1781*8d741a5dSApple OSS Distributions T_DECL(special_reply_port, "basic special reply port robustness checks",
1782*8d741a5dSApple OSS Distributions     T_META_RUN_CONCURRENTLY(true), T_META_TAG_VM_PREFERRED)
1783*8d741a5dSApple OSS Distributions {
1784*8d741a5dSApple OSS Distributions 	pthread_t thread;
1785*8d741a5dSApple OSS Distributions 	mach_port_t srp = thread_get_special_reply_port();
1786*8d741a5dSApple OSS Distributions 
1787*8d741a5dSApple OSS Distributions 	pthread_create(&thread, NULL, special_reply_port_thread, &srp);
1788*8d741a5dSApple OSS Distributions 
1789*8d741a5dSApple OSS Distributions 	while (srp) {
1790*8d741a5dSApple OSS Distributions 		usleep(1000);
1791*8d741a5dSApple OSS Distributions 	}
1792*8d741a5dSApple OSS Distributions 
1793*8d741a5dSApple OSS Distributions 	pthread_exit(NULL);
1794*8d741a5dSApple OSS Distributions }
1795*8d741a5dSApple OSS Distributions 
1796*8d741a5dSApple OSS Distributions #define TEST_QOS(server_name, client_name, name, wl_function_name, qos_bo, qos_bo_name, qos_qo, qos_qo_name, qos_ao, qos_ao_name) \
1797*8d741a5dSApple OSS Distributions 	T_DECL(server_kevent_id_##name, \
1798*8d741a5dSApple OSS Distributions 	                "Event delivery at " qos_ao_name " QoS using a kevent_id", \
1799*8d741a5dSApple OSS Distributions 	                T_META_ASROOT(YES), T_META_TAG_VM_PREFERRED) \
1800*8d741a5dSApple OSS Distributions 	{ \
1801*8d741a5dSApple OSS Distributions 	        qos_class_t qos_array[ENV_VAR_QOS] = {qos_bo, qos_qo, qos_ao};  \
1802*8d741a5dSApple OSS Distributions 	        const char *qos_name_array[ENV_VAR_QOS] = {qos_bo_name, qos_qo_name, qos_ao_name}; \
1803*8d741a5dSApple OSS Distributions 	        run_client_server(server_name, client_name, qos_array, qos_name_array, wl_function_name); \
1804*8d741a5dSApple OSS Distributions 	}
1805*8d741a5dSApple OSS Distributions /*
1806*8d741a5dSApple OSS Distributions  * Test 1: Test special reply port SPI
1807*8d741a5dSApple OSS Distributions  *
1808*8d741a5dSApple OSS Distributions  * Create thread special reply port and check any subsequent calls to
1809*8d741a5dSApple OSS Distributions  * the same should return MACH_PORT_NULL, unless the reply port is destroyed.
1810*8d741a5dSApple OSS Distributions  */
1811*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_get_special_reply_port", special_reply_port, "workloop_cb_test_intransit",
1812*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1813*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1814*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
1815*8d741a5dSApple OSS Distributions 
1816*8d741a5dSApple OSS Distributions /*
1817*8d741a5dSApple OSS Distributions  * Test 2: Test sync ipc send to an in-transit port
1818*8d741a5dSApple OSS Distributions  *
1819*8d741a5dSApple OSS Distributions  * Send a sync ipc message (at IN qos) to an in-transit port enqueued in a port
1820*8d741a5dSApple OSS Distributions  * attached to a workloop. Test that the servicer of the workloop gets
1821*8d741a5dSApple OSS Distributions  * sync ipc override.
1822*8d741a5dSApple OSS Distributions  */
1823*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_to_intransit_with_thr_pri", transit_IN, "workloop_cb_test_intransit",
1824*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1825*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
1826*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INITIATED, "user initiated")
1827*8d741a5dSApple OSS Distributions 
1828*8d741a5dSApple OSS Distributions /*
1829*8d741a5dSApple OSS Distributions  * Test 3: Test sync ipc send to an in-transit port
1830*8d741a5dSApple OSS Distributions  *
1831*8d741a5dSApple OSS Distributions  * Send a sync ipc message (at UI qos) to an in-transit port enqueued in a port
1832*8d741a5dSApple OSS Distributions  * attached to a workloop. Test that the servicer of the workloop gets
1833*8d741a5dSApple OSS Distributions  * sync ipc override.
1834*8d741a5dSApple OSS Distributions  */
1835*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_to_intransit_with_thr_pri", transit_UI, "workloop_cb_test_intransit",
1836*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INITIATED, "user initiated",
1837*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
1838*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INTERACTIVE, "user initiated with 47 basepri promotion")
1839*8d741a5dSApple OSS Distributions 
1840*8d741a5dSApple OSS Distributions /*
1841*8d741a5dSApple OSS Distributions  * Test 4: Test starting a sync rcv overrides the servicer
1842*8d741a5dSApple OSS Distributions  *
1843*8d741a5dSApple OSS Distributions  * Send an async message to a port and then start waiting on
1844*8d741a5dSApple OSS Distributions  * the port in mach msg rcv (at IN qos) with sync wait and test if the
1845*8d741a5dSApple OSS Distributions  * servicer of the workloop gets sync ipc override.
1846*8d741a5dSApple OSS Distributions  */
1847*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_and_sync_rcv", rcv_IN, "workloop_cb_test_intransit",
1848*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1849*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
1850*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INITIATED, "user initiated")
1851*8d741a5dSApple OSS Distributions 
1852*8d741a5dSApple OSS Distributions /*
1853*8d741a5dSApple OSS Distributions  * Test 5: Test starting a sync rcv overrides the servicer
1854*8d741a5dSApple OSS Distributions  *
1855*8d741a5dSApple OSS Distributions  * Send an async message to a port and then start waiting on
1856*8d741a5dSApple OSS Distributions  * the port in mach msg rcv (at UI qos) with sync wait and test if the
1857*8d741a5dSApple OSS Distributions  * servicer of the workloop gets sync ipc override.
1858*8d741a5dSApple OSS Distributions  */
1859*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_and_sync_rcv", rcv_UI, "workloop_cb_test_intransit",
1860*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1861*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
1862*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INTERACTIVE, "user interactive with 47 basepri promotion")
1863*8d741a5dSApple OSS Distributions 
1864*8d741a5dSApple OSS Distributions /*
1865*8d741a5dSApple OSS Distributions  * Test 6: test sending sync ipc message (at IN qos) to port will override the servicer
1866*8d741a5dSApple OSS Distributions  *
1867*8d741a5dSApple OSS Distributions  * Send a message with sync ipc override to a port and check if the servicer
1868*8d741a5dSApple OSS Distributions  * of the workloop on other side gets sync ipc override.
1869*8d741a5dSApple OSS Distributions  */
1870*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_msg_with_pri", send_sync_IN, "workloop_cb_test_sync_send",
1871*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1872*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
1873*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INITIATED, "user initiated")
1874*8d741a5dSApple OSS Distributions 
1875*8d741a5dSApple OSS Distributions /*
1876*8d741a5dSApple OSS Distributions  * Test 7: test sending sync ipc message (at UI qos) to port will override the servicer
1877*8d741a5dSApple OSS Distributions  *
1878*8d741a5dSApple OSS Distributions  * Send a message with sync ipc override to a port and check if the servicer
1879*8d741a5dSApple OSS Distributions  * of the workloop on other side gets sync ipc override.
1880*8d741a5dSApple OSS Distributions  */
1881*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_msg_with_pri", send_sync_UI, "workloop_cb_test_sync_send",
1882*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
1883*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
1884*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INTERACTIVE, "user initiated with 47 basepri promotion")
1885*8d741a5dSApple OSS Distributions 
1886*8d741a5dSApple OSS Distributions /*
1887*8d741a5dSApple OSS Distributions  * Test 8: test enabling a knote in workloop handler will drop the sync ipc override of delivered message
1888*8d741a5dSApple OSS Distributions  *
1889*8d741a5dSApple OSS Distributions  * Send a sync ipc message to port and check the servicer of the workloop
1890*8d741a5dSApple OSS Distributions  * on other side gets sync ipc override and once the handler enables the knote,
1891*8d741a5dSApple OSS Distributions  * that sync ipc override is dropped.
1892*8d741a5dSApple OSS Distributions  */
1893*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_msg_with_pri", send_sync_UI_and_enable, "workloop_cb_test_sync_send_and_enable",
1894*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1895*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1896*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INTERACTIVE, "user initiated with 47 basepri promotion")
1897*8d741a5dSApple OSS Distributions 
1898*8d741a5dSApple OSS Distributions /*
1899*8d741a5dSApple OSS Distributions  * Test 9: test returning to begin processing drops sync ipc override of delivered message
1900*8d741a5dSApple OSS Distributions  *
1901*8d741a5dSApple OSS Distributions  * Send a sync ipc message and check if enabling the knote clears the override of
1902*8d741a5dSApple OSS Distributions  * the delivered message, but should still have the override of an enqueued message.
1903*8d741a5dSApple OSS Distributions  */
1904*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_two_sync_msg_with_thr_pri", send_two_sync_UI, "workloop_cb_test_send_two_sync",
1905*8d741a5dSApple OSS Distributions     QOS_CLASS_BACKGROUND, "background",
1906*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
1907*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INTERACTIVE, "user initiated with 47 basepri promotion")
1908*8d741a5dSApple OSS Distributions 
1909*8d741a5dSApple OSS Distributions /*
1910*8d741a5dSApple OSS Distributions  * Test 10: test destroying the special reply port drops the override
1911*8d741a5dSApple OSS Distributions  *
1912*8d741a5dSApple OSS Distributions  * Send an async messages and a sync ipc message, the workloop handler
1913*8d741a5dSApple OSS Distributions  * should get a sync ipc override, now test if destroying the special
1914*8d741a5dSApple OSS Distributions  * reply port drops the sync ipc override on the servicer.
1915*8d741a5dSApple OSS Distributions  */
1916*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_two_msg_and_destroy", send_two_UI_and_destroy, "workloop_cb_test_two_send_and_destroy",
1917*8d741a5dSApple OSS Distributions     QOS_CLASS_BACKGROUND, "background",
1918*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
1919*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INTERACTIVE, "user initiated with 47 basepri promotion")
1920*8d741a5dSApple OSS Distributions 
1921*8d741a5dSApple OSS Distributions /*
1922*8d741a5dSApple OSS Distributions  * Test 11: test sending two ports with chaining
1923*8d741a5dSApple OSS Distributions  *
1924*8d741a5dSApple OSS Distributions  * Send a sync IPC to a connection port, which itself is embedded in a message
1925*8d741a5dSApple OSS Distributions  * sent as a sync IPC to a service port.
1926*8d741a5dSApple OSS Distributions  */
1927*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_complex_msg_with_pri", send_complex_sync_UI_and_enable, "workloop_cb_test_sync_send_and_enable",
1928*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INITIATED, "user initiated",
1929*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INITIATED, "user initiated",
1930*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INTERACTIVE, "user initiated with 47 basepri promotion")
1931*8d741a5dSApple OSS Distributions 
1932*8d741a5dSApple OSS Distributions /*
1933*8d741a5dSApple OSS Distributions  * Test 12: test sending two ports with chaining
1934*8d741a5dSApple OSS Distributions  *
1935*8d741a5dSApple OSS Distributions  * Send a sync IPC to a connection port, which itself is embedded in a message
1936*8d741a5dSApple OSS Distributions  * sent as a sync IPC to a service port.
1937*8d741a5dSApple OSS Distributions  */
1938*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_complex_msg_with_pri", send_complex_sync_UI_and_enable_and_handoff, "workloop_cb_test_sync_send_and_enable_handoff",
1939*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INITIATED, "user initiated",
1940*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INITIATED, "user initiated",
1941*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INTERACTIVE, "user initiated with 47 basepri promotion")
1942*8d741a5dSApple OSS Distributions 
1943*8d741a5dSApple OSS Distributions /*
1944*8d741a5dSApple OSS Distributions  * Test 13: test changing qos of a thread to trigger turnstile push
1945*8d741a5dSApple OSS Distributions  *
1946*8d741a5dSApple OSS Distributions  * Send a sync IPC to a service port and change the qos of the blocked thread
1947*8d741a5dSApple OSS Distributions  * to verify that changing qos triggers a turnstile push.
1948*8d741a5dSApple OSS Distributions  */
1949*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_sync_rcv_qos_change", qos_change_to_IN, "workloop_cb_test_intransit",
1950*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1951*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
1952*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INITIATED, "user initiated")
1953*8d741a5dSApple OSS Distributions 
1954*8d741a5dSApple OSS Distributions /*
1955*8d741a5dSApple OSS Distributions  * Test 14 - 21
1956*8d741a5dSApple OSS Distributions  *
1957*8d741a5dSApple OSS Distributions  * Test single sync ipc link with server that breaks/preserves the link in different ways.
1958*8d741a5dSApple OSS Distributions  */
1959*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_msg_with_link_check_correct_server", send_sync_link_correct_server_s, "workloop_cb_test_sync_send_reply",
1960*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1961*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1962*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
1963*8d741a5dSApple OSS Distributions 
1964*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_msg_with_link_check_correct_server", send_sync_link_correct_server_d, "workloop_cb_test_sync_send_deallocate",
1965*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1966*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1967*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
1968*8d741a5dSApple OSS Distributions 
1969*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_msg_with_link_check_correct_server", send_sync_link_correct_server_sk, "workloop_cb_test_sync_send_reply_kevent",
1970*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1971*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1972*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
1973*8d741a5dSApple OSS Distributions 
1974*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_msg_with_link_check_correct_server", send_sync_link_correct_server_skp, "workloop_cb_test_sync_send_reply_kevent_pthread",
1975*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1976*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1977*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
1978*8d741a5dSApple OSS Distributions 
1979*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_msg_with_link_check_incorrect_server", send_sync_link_incorrect_server_ks, "workloop_cb_test_sync_send_kevent_reply",
1980*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1981*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1982*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
1983*8d741a5dSApple OSS Distributions 
1984*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_msg_with_link_check_correct_server", send_sync_link_correct_server_n, "workloop_cb_test_sync_send_do_nothing",
1985*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1986*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1987*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
1988*8d741a5dSApple OSS Distributions 
1989*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_msg_with_link_check_incorrect_server", send_sync_link_incorrect_server_kp, "workloop_cb_test_sync_send_do_nothing_kevent_pthread",
1990*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1991*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1992*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
1993*8d741a5dSApple OSS Distributions 
1994*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_sync_msg_with_link_check_correct_server", send_sync_link_correct_server_e, "workloop_cb_test_sync_send_do_nothing_exit",
1995*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1996*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
1997*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
1998*8d741a5dSApple OSS Distributions 
1999*8d741a5dSApple OSS Distributions /*
2000*8d741a5dSApple OSS Distributions  * Test 22 - 25
2001*8d741a5dSApple OSS Distributions  *
2002*8d741a5dSApple OSS Distributions  * Test sequential sync ipc link with server that breaks/preserves the link.
2003*8d741a5dSApple OSS Distributions  */
2004*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_2sync_msg_with_link_check_correct_server", send_2sync_link_correct_server_sksk, "workloop_cb_test_sync_send_reply_kevent_reply_kevent",
2005*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
2006*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
2007*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
2008*8d741a5dSApple OSS Distributions 
2009*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_2sync_msg_with_link_check_incorrect_server", send_2sync_link_incorrect_server_kssk, "workloop_cb_test_sync_send_kevent_reply_reply_kevent",
2010*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
2011*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
2012*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
2013*8d741a5dSApple OSS Distributions 
2014*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_2sync_msg_with_link_check_incorrect_server", send_2sync_link_incorrect_server_ksks, "workloop_cb_test_sync_send_kevent_reply_kevent_reply",
2015*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
2016*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
2017*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
2018*8d741a5dSApple OSS Distributions 
2019*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_2sync_msg_with_link_check_incorrect_server", send_2sync_link_incorrect_server_skks, "workloop_cb_test_sync_send_reply_kevent_kevent_reply",
2020*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
2021*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default",
2022*8d741a5dSApple OSS Distributions     QOS_CLASS_DEFAULT, "default")
2023*8d741a5dSApple OSS Distributions 
2024*8d741a5dSApple OSS Distributions /*
2025*8d741a5dSApple OSS Distributions  * Test 26: test sending sync ipc from kernel (at IN qos) to port will override the servicer
2026*8d741a5dSApple OSS Distributions  *
2027*8d741a5dSApple OSS Distributions  * Do a kernel upcall at IN qos to a port and check if the servicer
2028*8d741a5dSApple OSS Distributions  * of the workloop on other side gets sync ipc override.
2029*8d741a5dSApple OSS Distributions  */
2030*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_client_send_kernel_upcall_sync_msg_with_pri", kernel_send_sync_IN, "workloop_cb_test_kernel_sync_send",
2031*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
2032*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
2033*8d741a5dSApple OSS Distributions     QOS_CLASS_USER_INITIATED, "user initiated")
2034*8d741a5dSApple OSS Distributions 
2035*8d741a5dSApple OSS Distributions /*
2036*8d741a5dSApple OSS Distributions  * Test 27: test sending async ipc from kernel (at IN qos and Tier 0 iotier) to port will override the servicer
2037*8d741a5dSApple OSS Distributions  *
2038*8d741a5dSApple OSS Distributions  * Do a kernel upcall at IN qos and Tier 0 iotier to a port and check if
2039*8d741a5dSApple OSS Distributions  * the servicer of the workloop on the other side gets the ipc override.
2040*8d741a5dSApple OSS Distributions  */
2041*8d741a5dSApple OSS Distributions TEST_QOS("server_kevent_id", "qos_iotier_client_send_kernel_upcall_async_msg", kernel_send_async_IN, "workloop_cb_test_kernel_async_send",
2042*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
2043*8d741a5dSApple OSS Distributions     QOS_CLASS_MAINTENANCE, "maintenance",
2044*8d741a5dSApple OSS Distributions     QOS_CLASS_UTILITY, "utility")
2045