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