xref: /xnu-11215.1.10/osfmk/kern/testpoints.h (revision 8d741a5de7ff4191bf97d57b9f54c2f6d4a15585)
1*8d741a5dSApple OSS Distributions /*
2*8d741a5dSApple OSS Distributions  * Copyright (c) 2023 Apple Inc. All rights reserved.
3*8d741a5dSApple OSS Distributions  *
4*8d741a5dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*8d741a5dSApple OSS Distributions  *
6*8d741a5dSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*8d741a5dSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*8d741a5dSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*8d741a5dSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*8d741a5dSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*8d741a5dSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*8d741a5dSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*8d741a5dSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*8d741a5dSApple OSS Distributions  *
15*8d741a5dSApple OSS Distributions  * Please obtain a copy of the License at
16*8d741a5dSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*8d741a5dSApple OSS Distributions  *
18*8d741a5dSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*8d741a5dSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*8d741a5dSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*8d741a5dSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*8d741a5dSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*8d741a5dSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*8d741a5dSApple OSS Distributions  * limitations under the License.
25*8d741a5dSApple OSS Distributions  *
26*8d741a5dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*8d741a5dSApple OSS Distributions  */
28*8d741a5dSApple OSS Distributions 
29*8d741a5dSApple OSS Distributions #ifndef _TESTPOINTS_H_
30*8d741a5dSApple OSS Distributions #define _TESTPOINTS_H_
31*8d741a5dSApple OSS Distributions 
32*8d741a5dSApple OSS Distributions #ifdef KERNEL
33*8d741a5dSApple OSS Distributions #include <kern/kern_types.h>
34*8d741a5dSApple OSS Distributions #include <kern/assert.h>
35*8d741a5dSApple OSS Distributions #include <kern/locks.h>
36*8d741a5dSApple OSS Distributions #else /* KERNEL */
37*8d741a5dSApple OSS Distributions #include <sys/types.h>
38*8d741a5dSApple OSS Distributions #include <assert.h>
39*8d741a5dSApple OSS Distributions #endif /* KERNEL */
40*8d741a5dSApple OSS Distributions 
41*8d741a5dSApple OSS Distributions #include <sys/cdefs.h>
42*8d741a5dSApple OSS Distributions 
43*8d741a5dSApple OSS Distributions /* Testpoints are intended as a generic mechanism which would allow developers
44*8d741a5dSApple OSS Distributions  * to call a test code from various places of source code by inserting one line
45*8d741a5dSApple OSS Distributions  * only. What will happen when testpoint is called is determined by current
46*8d741a5dSApple OSS Distributions  * test scenario which can be set via sysctl. */
47*8d741a5dSApple OSS Distributions 
48*8d741a5dSApple OSS Distributions /* Testpoints scenarios */
49*8d741a5dSApple OSS Distributions __enum_decl(tps_id_t, uint64_t, {
50*8d741a5dSApple OSS Distributions 	TPS_NONE,                       // nothing enabled
51*8d741a5dSApple 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*8d741a5dSApple 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*8d741a5dSApple OSS Distributions });
54*8d741a5dSApple OSS Distributions 
55*8d741a5dSApple OSS Distributions /* Testpoint definitions */
56*8d741a5dSApple OSS Distributions __enum_decl(tp_id_t, uint16_t, {
57*8d741a5dSApple OSS Distributions 	TP_BLOCK_START_STACKSHOT,       // this action will mark stackshot blocked
58*8d741a5dSApple OSS Distributions 	TP_WAIT_START_STACKSHOT,        // stackshot thread is waiting here until test thread is in upcall
59*8d741a5dSApple OSS Distributions 	TP_UPCALL,                      // upcall handler, unblocks stackshot and waits
60*8d741a5dSApple OSS Distributions 	TP_START_COLLECTION,            // just before exclave threads collection starts, unblocks upcall and waits
61*8d741a5dSApple OSS Distributions 	TP_AST,                         // before going back to exclaves, unblocks exclaves collection
62*8d741a5dSApple OSS Distributions 	TP_STACKSHOT_DONE,              // stackshot is done
63*8d741a5dSApple OSS Distributions 	TESTPOINT_COUNT,
64*8d741a5dSApple OSS Distributions });
65*8d741a5dSApple OSS Distributions 
66*8d741a5dSApple OSS Distributions typedef uint32_t tp_val_t;
67*8d741a5dSApple OSS Distributions 
68*8d741a5dSApple OSS Distributions // must fit 64 bits
69*8d741a5dSApple OSS Distributions typedef struct tp_sysctl_msg {
70*8d741a5dSApple OSS Distributions 	uint16_t id;
71*8d741a5dSApple OSS Distributions 	uint16_t _unused;
72*8d741a5dSApple OSS Distributions 	uint32_t val;
73*8d741a5dSApple OSS Distributions } tp_sysctl_msg_t;
74*8d741a5dSApple OSS Distributions 
75*8d741a5dSApple OSS Distributions static_assert(sizeof(uint64_t) == sizeof(tp_sysctl_msg_t), "tp_sysctl_msg_t does have 64 bits");
76*8d741a5dSApple OSS Distributions 
77*8d741a5dSApple OSS Distributions 
78*8d741a5dSApple OSS Distributions #if DEBUG || DEVELOPMENT || TESTPOINTS
79*8d741a5dSApple OSS Distributions 
80*8d741a5dSApple OSS Distributions /* Action is determined by current scenario */
81*8d741a5dSApple OSS Distributions void
82*8d741a5dSApple OSS Distributions tp_call(tp_id_t testpoint, tp_val_t val);
83*8d741a5dSApple OSS Distributions 
84*8d741a5dSApple OSS Distributions #define TESTPOINT(x) tp_call(x, 0);
85*8d741a5dSApple OSS Distributions 
86*8d741a5dSApple OSS Distributions #else /* DEBUG || DEVELOPMENT || TESTPOINTS */
87*8d741a5dSApple OSS Distributions 
88*8d741a5dSApple OSS Distributions #define TESTPOINT(x) ;
89*8d741a5dSApple OSS Distributions 
90*8d741a5dSApple OSS Distributions #endif /* DEBUG || DEVELOPMENT || TESTPOINTS */
91*8d741a5dSApple OSS Distributions 
92*8d741a5dSApple OSS Distributions #if (DEBUG || DEVELOPMENT) && KERNEL
93*8d741a5dSApple OSS Distributions 
94*8d741a5dSApple OSS Distributions /* Below functions are intended for kernel tests implementations. They are not
95*8d741a5dSApple OSS Distributions  * thread safe and should be protected by tp_mtx. See exclaves_test_stackshot.c
96*8d741a5dSApple OSS Distributions  * for sample code. */
97*8d741a5dSApple OSS Distributions 
98*8d741a5dSApple OSS Distributions extern lck_mtx_t tp_mtx;
99*8d741a5dSApple OSS Distributions 
100*8d741a5dSApple OSS Distributions /* Set given testpoint value to 1. */
101*8d741a5dSApple OSS Distributions void
102*8d741a5dSApple OSS Distributions tp_block(tp_id_t testpoint);
103*8d741a5dSApple OSS Distributions 
104*8d741a5dSApple OSS Distributions /* Set given testpoint value to 0 and wakeup waiting threads. */
105*8d741a5dSApple OSS Distributions void
106*8d741a5dSApple OSS Distributions tp_unblock(tp_id_t other_testpoint);
107*8d741a5dSApple OSS Distributions 
108*8d741a5dSApple OSS Distributions /* Wait until given testpoint value is zero. */
109*8d741a5dSApple OSS Distributions void
110*8d741a5dSApple OSS Distributions tp_wait(tp_id_t testpoint);
111*8d741a5dSApple OSS Distributions 
112*8d741a5dSApple OSS Distributions /* Unblock other testpoint, then block and wait. */
113*8d741a5dSApple OSS Distributions void
114*8d741a5dSApple OSS Distributions tp_relay(tp_id_t testpoint, tp_id_t other_testpoint);
115*8d741a5dSApple OSS Distributions 
116*8d741a5dSApple OSS Distributions #endif /* (DEBUG || DEVELOPMENT) && KERNEL */
117*8d741a5dSApple OSS Distributions 
118*8d741a5dSApple OSS Distributions #endif /* _TESTPOINTS_H_ */
119