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