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