xref: /xnu-10002.61.3/osfmk/tests/xnupost.h (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
1 /*
2  * Copyright (c) 2019 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #ifndef _TESTS_XNUPOST_H
30 #define _TESTS_XNUPOST_H
31 
32 #ifndef CONFIG_XNUPOST
33 #error "Testing is not enabled if CONFIG_XNUPOST is not enabled"
34 #endif
35 
36 #include <kern/kern_types.h>
37 #include <kern/assert.h>
38 #include <tests/ktest.h>
39 
40 #define XT_CONFIG_RUN 0x0
41 #define XT_CONFIG_IGNORE 0x1
42 #define XT_CONFIG_EXPECT_PANIC 0x2
43 
44 #define XTCTL_RUN_TESTS  1
45 #define XTCTL_RESET_TESTDATA 2
46 
47 typedef enum { XT_ACTION_NONE = 0, XT_ACTION_SKIPPED, XT_ACTION_PASSED, XT_ACTION_FAILED } xnupost_test_action_t;
48 
49 typedef kern_return_t (*test_function)(void);
50 struct xnupost_test {
51 	uint16_t xt_config;
52 	uint16_t xt_test_num;
53 	kern_return_t xt_retval;
54 	kern_return_t xt_expected_retval;
55 	uint64_t xt_begin_time;
56 	uint64_t xt_end_time;
57 	xnupost_test_action_t xt_test_actions;
58 	test_function xt_func;
59 	const char * xt_name;
60 };
61 
62 typedef kern_return_t xt_panic_return_t;
63 #define XT_PANIC_UNRELATED  0x8  /* not related. continue panic */
64 #define XT_RET_W_FAIL       0x9  /* report FAILURE and return from panic */
65 #define XT_RET_W_SUCCESS    0xA  /* report SUCCESS and return from panic */
66 #define XT_PANIC_W_FAIL     0xB  /* report FAILURE and continue to panic */
67 #define XT_PANIC_W_SUCCESS  0xC  /* report SUCCESS and continue to panic */
68 
69 typedef xt_panic_return_t (*xt_panic_widget_func)(const char * panicstr, void * context, void ** outval);
70 struct xnupost_panic_widget {
71 	void * xtp_context_p;
72 	void ** xtp_outval_p;
73 	const char * xtp_func_name;
74 	xt_panic_widget_func xtp_func;
75 };
76 
77 /* for internal use only. Use T_REGISTER_* macros */
78 extern xt_panic_return_t _xt_generic_assert_check(const char * s, void * str_to_match, void ** outval);
79 kern_return_t xnupost_register_panic_widget(xt_panic_widget_func funcp, const char * funcname, void * context, void ** outval);
80 
81 #define T_REGISTER_PANIC_WIDGET(func, ctx, outval) xnupost_register_panic_widget((func), #func, (ctx), (outval))
82 #define T_REGISTER_ASSERT_CHECK(assert_str, retval) \
83 	T_REGISTER_PANIC_WIDGET(_xt_generic_assert_check, (void *)__DECONST(char *, assert_str), retval)
84 
85 typedef struct xnupost_test xnupost_test_data_t;
86 typedef struct xnupost_test * xnupost_test_t;
87 
88 extern struct xnupost_test kernel_post_tests[];
89 extern uint32_t kernel_post_tests_count;
90 extern uint32_t total_post_tests_count;
91 
92 #define XNUPOST_TEST_CONFIG_BASIC(func)                   \
93 	{                                                 \
94 	        .xt_config = XT_CONFIG_RUN,               \
95 	        .xt_test_num = 0,                         \
96 	        .xt_retval = -1,                          \
97 	        .xt_expected_retval = T_STATE_PASS,       \
98 	        .xt_begin_time = 0,                       \
99 	        .xt_end_time = 0,                         \
100 	        .xt_test_actions = 0,                     \
101 	        .xt_func = (func),                        \
102 	        .xt_name = "xnu."#func                    \
103 	}
104 
105 #define XNUPOST_TEST_CONFIG_TEST_PANIC(func)                       \
106 	{                                                          \
107 	        .xt_config = XT_CONFIG_EXPECT_PANIC,               \
108 	        .xt_test_num = 0,                                  \
109 	        .xt_retval = -1,                                   \
110 	        .xt_expected_retval = T_STATE_PASS,                \
111 	        .xt_begin_time = 0,                                \
112 	        .xt_end_time = 0,                                  \
113 	        .xt_test_actions = 0,                              \
114 	        .xt_func = (func),                                 \
115 	        .xt_name = "xnu."#func                             \
116 	}
117 
118 void xnupost_init(void);
119 /*
120  * Parse boot-args specific to POST testing and setup enabled/disabled settings
121  * returns: KERN_SUCCESS - if testing is enabled.
122  */
123 kern_return_t xnupost_parse_config(void);
124 kern_return_t xnupost_run_tests(xnupost_test_t test_list, uint32_t test_count);
125 kern_return_t xnupost_list_tests(xnupost_test_t test_list, uint32_t test_count);
126 kern_return_t xnupost_reset_tests(xnupost_test_t test_list, uint32_t test_count);
127 
128 int xnupost_export_testdata(void * outp, size_t size, uint32_t * lenp);
129 uint32_t xnupost_get_estimated_testdata_size(void);
130 
131 kern_return_t kernel_do_post(void);
132 kern_return_t xnupost_process_kdb_stop(const char * panic_s);
133 int xnupost_reset_all_tests(void);
134 
135 kern_return_t kernel_list_tests(void);
136 int bsd_do_post(void);
137 int bsd_list_tests(void);
138 
139 #endif /* _TESTS_XNUPOST_H */
140