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