xref: /xnu-10063.121.3/osfmk/kern/testpoints.h (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 #ifndef _TESTPOINTS_H_
30*2c2f96dcSApple OSS Distributions #define _TESTPOINTS_H_
31*2c2f96dcSApple OSS Distributions 
32*2c2f96dcSApple OSS Distributions #ifdef KERNEL
33*2c2f96dcSApple OSS Distributions #include <kern/kern_types.h>
34*2c2f96dcSApple OSS Distributions #include <kern/assert.h>
35*2c2f96dcSApple OSS Distributions #include <kern/locks.h>
36*2c2f96dcSApple OSS Distributions #else /* KERNEL */
37*2c2f96dcSApple OSS Distributions #include <sys/types.h>
38*2c2f96dcSApple OSS Distributions #include <assert.h>
39*2c2f96dcSApple OSS Distributions #endif /* KERNEL */
40*2c2f96dcSApple OSS Distributions 
41*2c2f96dcSApple OSS Distributions #include <sys/cdefs.h>
42*2c2f96dcSApple OSS Distributions 
43*2c2f96dcSApple OSS Distributions /* Testpoints are intended as a generic mechanism which would allow developers
44*2c2f96dcSApple OSS Distributions  * to call a test code from various places of source code by inserting one line
45*2c2f96dcSApple OSS Distributions  * only. What will happen when testpoint is called is determined by current
46*2c2f96dcSApple OSS Distributions  * test scenario which can be set via sysctl. */
47*2c2f96dcSApple OSS Distributions 
48*2c2f96dcSApple OSS Distributions /* Testpoints scenarios */
49*2c2f96dcSApple OSS Distributions __enum_decl(tps_id_t, uint64_t, {
50*2c2f96dcSApple OSS Distributions 	TPS_NONE,                       // nothing enabled
51*2c2f96dcSApple OSS Distributions 	TPS_STACKSHOT_UPCALL,           // c-hello-exclaves thread is going to hang in upcall during stackshot and tries to return as soon as it can
52*2c2f96dcSApple OSS Distributions 	TPS_STACKSHOT_LONG_UPCALL,      // c-hello-exclaves thread is going to hang in upcall during stackshot and returns when stackshot is completely done
53*2c2f96dcSApple OSS Distributions });
54*2c2f96dcSApple OSS Distributions 
55*2c2f96dcSApple OSS Distributions /* Testpoint definitions */
56*2c2f96dcSApple OSS Distributions __enum_decl(tp_id_t, uint16_t, {
57*2c2f96dcSApple OSS Distributions 	TP_BLOCK_START_STACKSHOT,       // this action will mark stackshot blocked
58*2c2f96dcSApple OSS Distributions 	TP_WAIT_START_STACKSHOT,        // stackshot thread is waiting here until test thread is in upcall
59*2c2f96dcSApple OSS Distributions 	TP_UPCALL,                      // upcall handler, unblocks stackshot and waits
60*2c2f96dcSApple OSS Distributions 	TP_START_COLLECTION,            // just before exclave threads collection starts, unblocks upcall and waits
61*2c2f96dcSApple OSS Distributions 	TP_AST,                         // before going back to exclaves, unblocks exclaves collection
62*2c2f96dcSApple OSS Distributions 	TP_STACKSHOT_DONE,              // stackshot is done
63*2c2f96dcSApple OSS Distributions 	TESTPOINT_COUNT,
64*2c2f96dcSApple OSS Distributions });
65*2c2f96dcSApple OSS Distributions 
66*2c2f96dcSApple OSS Distributions typedef uint32_t tp_val_t;
67*2c2f96dcSApple OSS Distributions 
68*2c2f96dcSApple OSS Distributions // must fit 64 bits
69*2c2f96dcSApple OSS Distributions typedef struct tp_sysctl_msg {
70*2c2f96dcSApple OSS Distributions 	uint16_t id;
71*2c2f96dcSApple OSS Distributions 	uint16_t _unused;
72*2c2f96dcSApple OSS Distributions 	uint32_t val;
73*2c2f96dcSApple OSS Distributions } tp_sysctl_msg_t;
74*2c2f96dcSApple OSS Distributions 
75*2c2f96dcSApple OSS Distributions static_assert(sizeof(uint64_t) == sizeof(tp_sysctl_msg_t), "tp_sysctl_msg_t does have 64 bits");
76*2c2f96dcSApple OSS Distributions 
77*2c2f96dcSApple OSS Distributions 
78*2c2f96dcSApple OSS Distributions #if DEBUG || DEVELOPMENT || TESTPOINTS
79*2c2f96dcSApple OSS Distributions 
80*2c2f96dcSApple OSS Distributions /* Action is determined by current scenario */
81*2c2f96dcSApple OSS Distributions void
82*2c2f96dcSApple OSS Distributions tp_call(tp_id_t testpoint, tp_val_t val);
83*2c2f96dcSApple OSS Distributions 
84*2c2f96dcSApple OSS Distributions #define TESTPOINT(x) tp_call(x, 0);
85*2c2f96dcSApple OSS Distributions 
86*2c2f96dcSApple OSS Distributions #else /* DEBUG || DEVELOPMENT || TESTPOINTS */
87*2c2f96dcSApple OSS Distributions 
88*2c2f96dcSApple OSS Distributions #define TESTPOINT(x) ;
89*2c2f96dcSApple OSS Distributions 
90*2c2f96dcSApple OSS Distributions #endif /* DEBUG || DEVELOPMENT || TESTPOINTS */
91*2c2f96dcSApple OSS Distributions 
92*2c2f96dcSApple OSS Distributions #if (DEBUG || DEVELOPMENT) && KERNEL
93*2c2f96dcSApple OSS Distributions 
94*2c2f96dcSApple OSS Distributions /* Below functions are intended for kernel tests implementations. They are not
95*2c2f96dcSApple OSS Distributions  * thread safe and should be protected by tp_mtx. See exclaves_test_stackshot.c
96*2c2f96dcSApple OSS Distributions  * for sample code. */
97*2c2f96dcSApple OSS Distributions 
98*2c2f96dcSApple OSS Distributions extern lck_mtx_t tp_mtx;
99*2c2f96dcSApple OSS Distributions 
100*2c2f96dcSApple OSS Distributions /* Set given testpoint value to 1. */
101*2c2f96dcSApple OSS Distributions void
102*2c2f96dcSApple OSS Distributions tp_block(tp_id_t testpoint);
103*2c2f96dcSApple OSS Distributions 
104*2c2f96dcSApple OSS Distributions /* Set given testpoint value to 0 and wakeup waiting threads. */
105*2c2f96dcSApple OSS Distributions void
106*2c2f96dcSApple OSS Distributions tp_unblock(tp_id_t other_testpoint);
107*2c2f96dcSApple OSS Distributions 
108*2c2f96dcSApple OSS Distributions /* Wait until given testpoint value is zero. */
109*2c2f96dcSApple OSS Distributions void
110*2c2f96dcSApple OSS Distributions tp_wait(tp_id_t testpoint);
111*2c2f96dcSApple OSS Distributions 
112*2c2f96dcSApple OSS Distributions /* Unblock other testpoint, then block and wait. */
113*2c2f96dcSApple OSS Distributions void
114*2c2f96dcSApple OSS Distributions tp_relay(tp_id_t testpoint, tp_id_t other_testpoint);
115*2c2f96dcSApple OSS Distributions 
116*2c2f96dcSApple OSS Distributions #endif /* (DEBUG || DEVELOPMENT) && KERNEL */
117*2c2f96dcSApple OSS Distributions 
118*2c2f96dcSApple OSS Distributions #endif /* _TESTPOINTS_H_ */
119