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