xref: /xnu-12377.81.4/osfmk/kern/testpoints.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2023 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions  *
15*043036a2SApple OSS Distributions  * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions  *
18*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions  * limitations under the License.
25*043036a2SApple OSS Distributions  *
26*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions  */
28*043036a2SApple OSS Distributions 
29*043036a2SApple OSS Distributions 
30*043036a2SApple OSS Distributions #if DEBUG || DEVELOPMENT
31*043036a2SApple OSS Distributions 
32*043036a2SApple OSS Distributions #include <kern/testpoints.h>
33*043036a2SApple OSS Distributions #include <kern/exclaves_test_stackshot.h>
34*043036a2SApple OSS Distributions #include <kern/thread.h>
35*043036a2SApple OSS Distributions 
36*043036a2SApple OSS Distributions // this is set by sysctl
37*043036a2SApple OSS Distributions uint64_t tp_scenario;
38*043036a2SApple OSS Distributions int32_t  tp_pid = -1;
39*043036a2SApple OSS Distributions 
40*043036a2SApple OSS Distributions #define TESTPOINT_COUNT 64
41*043036a2SApple OSS Distributions // array of blocked test points
42*043036a2SApple OSS Distributions static uint64_t tp_blocked_info[TESTPOINT_COUNT];
43*043036a2SApple OSS Distributions 
44*043036a2SApple OSS Distributions static LCK_GRP_DECLARE(tp_lck_grp, "testpoint lock group");
45*043036a2SApple OSS Distributions LCK_MTX_DECLARE(tp_mtx, &tp_lck_grp);
46*043036a2SApple OSS Distributions 
47*043036a2SApple OSS Distributions void
tp_block(tp_id_t testpoint)48*043036a2SApple OSS Distributions tp_block(tp_id_t testpoint)
49*043036a2SApple OSS Distributions {
50*043036a2SApple OSS Distributions 	tp_blocked_info[testpoint] = 1;
51*043036a2SApple OSS Distributions }
52*043036a2SApple OSS Distributions 
53*043036a2SApple OSS Distributions void
tp_unblock(tp_id_t other_testpoint)54*043036a2SApple OSS Distributions tp_unblock(tp_id_t other_testpoint)
55*043036a2SApple OSS Distributions {
56*043036a2SApple OSS Distributions 	tp_blocked_info[other_testpoint] = 0;
57*043036a2SApple OSS Distributions 	thread_wakeup(&tp_blocked_info);
58*043036a2SApple OSS Distributions }
59*043036a2SApple OSS Distributions 
60*043036a2SApple OSS Distributions 
61*043036a2SApple OSS Distributions void
tp_wait(tp_id_t testpoint)62*043036a2SApple OSS Distributions tp_wait(tp_id_t testpoint)
63*043036a2SApple OSS Distributions {
64*043036a2SApple OSS Distributions 	wait_result_t wr = THREAD_AWAKENED;
65*043036a2SApple OSS Distributions 	while ((tp_blocked_info[testpoint]) && wr <= 0) {
66*043036a2SApple OSS Distributions 		wr = lck_mtx_sleep(&tp_mtx, LCK_SLEEP_DEFAULT, (event_t)&tp_blocked_info, THREAD_INTERRUPTIBLE);
67*043036a2SApple OSS Distributions 	}
68*043036a2SApple OSS Distributions 	if (wr > 0) {
69*043036a2SApple OSS Distributions 		printf("tp_block(%hu) wait interrupted with error %d\n", testpoint, wr);
70*043036a2SApple OSS Distributions 	}
71*043036a2SApple OSS Distributions }
72*043036a2SApple OSS Distributions 
73*043036a2SApple OSS Distributions void
tp_relay(tp_id_t testpoint,tp_id_t other_testpoint)74*043036a2SApple OSS Distributions tp_relay(tp_id_t testpoint, tp_id_t other_testpoint)
75*043036a2SApple OSS Distributions {
76*043036a2SApple OSS Distributions 	tp_unblock(other_testpoint);
77*043036a2SApple OSS Distributions 	tp_block(testpoint);
78*043036a2SApple OSS Distributions 	tp_wait(testpoint);
79*043036a2SApple OSS Distributions }
80*043036a2SApple OSS Distributions 
81*043036a2SApple OSS Distributions 
82*043036a2SApple OSS Distributions void
tp_call(tp_id_t testpoint,tp_val_t val)83*043036a2SApple OSS Distributions tp_call(tp_id_t testpoint, tp_val_t val)
84*043036a2SApple OSS Distributions {
85*043036a2SApple OSS Distributions 	if (tp_pid != pid_from_task(current_task())) {
86*043036a2SApple OSS Distributions 		return;
87*043036a2SApple OSS Distributions 	}
88*043036a2SApple OSS Distributions 	switch (tp_scenario) {
89*043036a2SApple OSS Distributions 	case TPS_NONE:
90*043036a2SApple OSS Distributions 		break;
91*043036a2SApple OSS Distributions 	case TPS_STACKSHOT_UPCALL:
92*043036a2SApple OSS Distributions 		tp_call_stackshot_upcall(testpoint, val);
93*043036a2SApple OSS Distributions 		break;
94*043036a2SApple OSS Distributions 	case TPS_STACKSHOT_LONG_UPCALL:
95*043036a2SApple OSS Distributions 		tp_call_stackshot_long_upcall(testpoint, val);
96*043036a2SApple OSS Distributions 		break;
97*043036a2SApple OSS Distributions 	default:
98*043036a2SApple OSS Distributions 		panic("Invalid test point scenario value %llu", tp_scenario);
99*043036a2SApple OSS Distributions 	}
100*043036a2SApple OSS Distributions }
101*043036a2SApple OSS Distributions 
102*043036a2SApple OSS Distributions 
103*043036a2SApple OSS Distributions static int
testpoint_handler(int64_t testpoint,int64_t * out)104*043036a2SApple OSS Distributions testpoint_handler(int64_t testpoint, int64_t *out)
105*043036a2SApple OSS Distributions {
106*043036a2SApple OSS Distributions 	tp_sysctl_msg_t * msg = (tp_sysctl_msg_t*)&testpoint;
107*043036a2SApple OSS Distributions 	tp_call(msg->id, msg->val);
108*043036a2SApple OSS Distributions 	*out = 0;
109*043036a2SApple OSS Distributions 	return 0;
110*043036a2SApple OSS Distributions }
111*043036a2SApple OSS Distributions 
112*043036a2SApple OSS Distributions SYSCTL_TEST_REGISTER(testpoint, testpoint_handler);
113*043036a2SApple OSS Distributions 
114*043036a2SApple OSS Distributions static int
tp_scenario_handler(int64_t scenario,int64_t * out)115*043036a2SApple OSS Distributions tp_scenario_handler(int64_t scenario, int64_t *out)
116*043036a2SApple OSS Distributions {
117*043036a2SApple OSS Distributions 	tps_id_t new_scenario = (tps_id_t)scenario;
118*043036a2SApple OSS Distributions 
119*043036a2SApple OSS Distributions 	lck_mtx_lock(&tp_mtx);
120*043036a2SApple OSS Distributions 	if (tp_scenario != new_scenario) {
121*043036a2SApple OSS Distributions 		tp_scenario = new_scenario;
122*043036a2SApple OSS Distributions 		bzero(&tp_blocked_info, sizeof(tp_blocked_info));
123*043036a2SApple OSS Distributions 		thread_wakeup(&tp_blocked_info);
124*043036a2SApple OSS Distributions 	}
125*043036a2SApple OSS Distributions 	lck_mtx_unlock(&tp_mtx);
126*043036a2SApple OSS Distributions 
127*043036a2SApple OSS Distributions 	printf("tp_scenario=%llu\n", new_scenario);
128*043036a2SApple OSS Distributions 	*out = 0;
129*043036a2SApple OSS Distributions 	return 0;
130*043036a2SApple OSS Distributions }
131*043036a2SApple OSS Distributions 
132*043036a2SApple OSS Distributions SYSCTL_TEST_REGISTER(tp_scenario, tp_scenario_handler);
133*043036a2SApple OSS Distributions 
134*043036a2SApple OSS Distributions static int
tp_pid_handler(int64_t pid,int64_t * out)135*043036a2SApple OSS Distributions tp_pid_handler(int64_t pid, int64_t *out)
136*043036a2SApple OSS Distributions {
137*043036a2SApple OSS Distributions 	int32_t new_pid = (int32_t)pid;
138*043036a2SApple OSS Distributions 	lck_mtx_lock(&tp_mtx);
139*043036a2SApple OSS Distributions 	if (tp_pid != new_pid) {
140*043036a2SApple OSS Distributions 		tp_pid = new_pid;
141*043036a2SApple OSS Distributions 		tp_scenario = TPS_NONE;
142*043036a2SApple OSS Distributions 		bzero(&tp_blocked_info, sizeof(tp_blocked_info));
143*043036a2SApple OSS Distributions 		thread_wakeup(&tp_blocked_info);
144*043036a2SApple OSS Distributions 	}
145*043036a2SApple OSS Distributions 	lck_mtx_unlock(&tp_mtx);
146*043036a2SApple OSS Distributions 
147*043036a2SApple OSS Distributions 	printf("tp_pid=%d\n", tp_pid);
148*043036a2SApple OSS Distributions 	*out = 0;
149*043036a2SApple OSS Distributions 	return 0;
150*043036a2SApple OSS Distributions }
151*043036a2SApple OSS Distributions 
152*043036a2SApple OSS Distributions SYSCTL_TEST_REGISTER(tp_pid, tp_pid_handler);
153*043036a2SApple OSS Distributions 
154*043036a2SApple OSS Distributions #endif /* DEBUG || DEVELOPMENT */
155