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