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 int32_t tp_pid = -1;
39
40 #define TESTPOINT_COUNT 64
41 // array of blocked test points
42 static uint64_t tp_blocked_info[TESTPOINT_COUNT];
43
44 static LCK_GRP_DECLARE(tp_lck_grp, "testpoint lock group");
45 LCK_MTX_DECLARE(tp_mtx, &tp_lck_grp);
46
47 void
tp_block(tp_id_t testpoint)48 tp_block(tp_id_t testpoint)
49 {
50 tp_blocked_info[testpoint] = 1;
51 }
52
53 void
tp_unblock(tp_id_t other_testpoint)54 tp_unblock(tp_id_t other_testpoint)
55 {
56 tp_blocked_info[other_testpoint] = 0;
57 thread_wakeup(&tp_blocked_info);
58 }
59
60
61 void
tp_wait(tp_id_t testpoint)62 tp_wait(tp_id_t testpoint)
63 {
64 wait_result_t wr = THREAD_AWAKENED;
65 while ((tp_blocked_info[testpoint]) && wr <= 0) {
66 wr = lck_mtx_sleep(&tp_mtx, LCK_SLEEP_DEFAULT, (event_t)&tp_blocked_info, THREAD_INTERRUPTIBLE);
67 }
68 if (wr > 0) {
69 printf("tp_block(%hu) wait interrupted with error %d\n", testpoint, wr);
70 }
71 }
72
73 void
tp_relay(tp_id_t testpoint,tp_id_t other_testpoint)74 tp_relay(tp_id_t testpoint, tp_id_t other_testpoint)
75 {
76 tp_unblock(other_testpoint);
77 tp_block(testpoint);
78 tp_wait(testpoint);
79 }
80
81
82 void
tp_call(tp_id_t testpoint,tp_val_t val)83 tp_call(tp_id_t testpoint, tp_val_t val)
84 {
85 if (tp_pid != pid_from_task(current_task())) {
86 return;
87 }
88 switch (tp_scenario) {
89 case TPS_NONE:
90 break;
91 case TPS_STACKSHOT_UPCALL:
92 tp_call_stackshot_upcall(testpoint, val);
93 break;
94 case TPS_STACKSHOT_LONG_UPCALL:
95 tp_call_stackshot_long_upcall(testpoint, val);
96 break;
97 default:
98 panic("Invalid test point scenario value %llu", tp_scenario);
99 }
100 }
101
102
103 static int
testpoint_handler(int64_t testpoint,int64_t * out)104 testpoint_handler(int64_t testpoint, int64_t *out)
105 {
106 tp_sysctl_msg_t * msg = (tp_sysctl_msg_t*)&testpoint;
107 tp_call(msg->id, msg->val);
108 *out = 0;
109 return 0;
110 }
111
112 SYSCTL_TEST_REGISTER(testpoint, testpoint_handler);
113
114 static int
tp_scenario_handler(int64_t scenario,int64_t * out)115 tp_scenario_handler(int64_t scenario, int64_t *out)
116 {
117 tps_id_t new_scenario = (tps_id_t)scenario;
118
119 lck_mtx_lock(&tp_mtx);
120 if (tp_scenario != new_scenario) {
121 tp_scenario = new_scenario;
122 bzero(&tp_blocked_info, sizeof(tp_blocked_info));
123 thread_wakeup(&tp_blocked_info);
124 }
125 lck_mtx_unlock(&tp_mtx);
126
127 printf("tp_scenario=%llu\n", new_scenario);
128 *out = 0;
129 return 0;
130 }
131
132 SYSCTL_TEST_REGISTER(tp_scenario, tp_scenario_handler);
133
134 static int
tp_pid_handler(int64_t pid,int64_t * out)135 tp_pid_handler(int64_t pid, int64_t *out)
136 {
137 int32_t new_pid = (int32_t)pid;
138 lck_mtx_lock(&tp_mtx);
139 if (tp_pid != new_pid) {
140 tp_pid = new_pid;
141 tp_scenario = TPS_NONE;
142 bzero(&tp_blocked_info, sizeof(tp_blocked_info));
143 thread_wakeup(&tp_blocked_info);
144 }
145 lck_mtx_unlock(&tp_mtx);
146
147 printf("tp_pid=%u\n", tp_pid);
148 *out = 0;
149 return 0;
150 }
151
152 SYSCTL_TEST_REGISTER(tp_pid, tp_pid_handler);
153
154 #endif /* DEBUG || DEVELOPMENT */
155