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