1 /*
2 * Copyright (c) 2019-2021 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 #include <tests/ktest.h>
30 #include <tests/xnupost.h>
31 #include <kern/assert.h>
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/types.h>
35 #include <sys/kdebug.h>
36 #include <libkern/libkern.h>
37 #include <kern/kalloc.h>
38 #include <sys/cdefs.h>
39 #include <libkern/version.h>
40 #include <kern/clock.h>
41 #include <kern/kern_cdata.h>
42 #include <pexpert/pexpert.h>
43
44
45 #if !(DEVELOPMENT || DEBUG)
46 #error "Testing is not enabled on RELEASE configurations"
47 #endif
48
49 #if defined(__arm64__)
50 extern kern_return_t arm64_lock_test(void);
51 extern kern_return_t arm_cpu_capabilities_legacy_test(void);
52 extern kern_return_t pmap_test(void);
53 #endif /* defined(__arm64__) */
54 kern_return_t ipi_test(void);
55 #if defined(KERNEL_INTEGRITY_CTRR)
56 extern kern_return_t ctrr_test(void);
57 #endif
58 #if __ARM_PAN_AVAILABLE__
59 extern kern_return_t arm64_late_pan_test(void);
60 #endif
61 extern kern_return_t copyio_test(void);
62 extern kern_return_t parse_boot_arg_test(void);
63
64 struct xnupost_test bsd_post_tests[] = {
65 #ifdef __arm64__
66 XNUPOST_TEST_CONFIG_BASIC(arm64_lock_test),
67 #endif
68 #if defined(__arm64__)
69 XNUPOST_TEST_CONFIG_BASIC(arm_cpu_capabilities_legacy_test),
70 XNUPOST_TEST_CONFIG_BASIC(pmap_test),
71 #endif /* defined(__arm64__) */
72 #if defined(KERNEL_INTEGRITY_CTRR)
73 XNUPOST_TEST_CONFIG_BASIC(ctrr_test),
74 #endif
75 #if __ARM_PAN_AVAILABLE__
76 XNUPOST_TEST_CONFIG_BASIC(arm64_late_pan_test),
77 #endif
78 XNUPOST_TEST_CONFIG_BASIC(ipi_test),
79 XNUPOST_TEST_CONFIG_BASIC(copyio_test),
80 XNUPOST_TEST_CONFIG_BASIC(parse_boot_arg_test),
81 };
82
83 uint32_t bsd_post_tests_count = sizeof(bsd_post_tests) / sizeof(xnupost_test_data_t);
84
85 extern uint64_t last_loaded_timestamp; /* updated by OSKext::load() */
86 extern uint64_t kernel_post_args;
87 int
bsd_list_tests()88 bsd_list_tests()
89 {
90 if (kernel_post_args == 0) {
91 return 0;
92 }
93
94 uint64_t prev_load_time = last_loaded_timestamp;
95 int no_load_counter = 5;
96 int absolute_break_counter = 15;
97 int delay_duration_usecs = 300000; /* 0.3 second for kext loading to stabilize */
98
99 while (no_load_counter > 0) {
100 printf("bsd_list_tests:INFO waiting for %d usecs\n", delay_duration_usecs);
101 printf("bsd_list_tests: prev: %llu current: %llu\n", prev_load_time, last_loaded_timestamp);
102
103 delay(delay_duration_usecs);
104 absolute_break_counter -= 1;
105
106 if (absolute_break_counter <= 0) {
107 printf("bsd_list_tests: WARNING: Waiting beyond normal time for stabilizing kext loading\n");
108 break;
109 }
110
111 if (prev_load_time == last_loaded_timestamp) {
112 no_load_counter -= 1;
113 printf("bsd_list_tests: INFO: no new kexts loaded. remaining checks: %d\n", no_load_counter);
114 }
115
116 prev_load_time = last_loaded_timestamp;
117 }
118
119 return xnupost_list_tests(bsd_post_tests, bsd_post_tests_count);
120 }
121
122 int
bsd_do_post(void)123 bsd_do_post(void)
124 {
125 return xnupost_run_tests(bsd_post_tests, bsd_post_tests_count);
126 }
127
128 /* kcdata type definition */
129 #define XNUPOST_TNAME_MAXLEN 132
130
131 struct kcdata_subtype_descriptor kc_xnupost_test_def[] = {
132 {.kcs_flags = KCS_SUBTYPE_FLAGS_NONE, .kcs_elem_type = KC_ST_UINT16, .kcs_elem_offset = 0, .kcs_elem_size = sizeof(uint16_t), .kcs_name = "config"},
133 {.kcs_flags = KCS_SUBTYPE_FLAGS_NONE, .kcs_elem_type = KC_ST_UINT16, .kcs_elem_offset = 1 * sizeof(uint16_t), .kcs_elem_size = sizeof(uint16_t), .kcs_name = "test_num"},
134 {.kcs_flags = KCS_SUBTYPE_FLAGS_NONE, .kcs_elem_type = KC_ST_INT32, .kcs_elem_offset = 2 * sizeof(uint16_t), .kcs_elem_size = sizeof(int32_t), .kcs_name = "retval"},
135 {.kcs_flags = KCS_SUBTYPE_FLAGS_NONE, .kcs_elem_type = KC_ST_INT32, .kcs_elem_offset = 2 * sizeof(uint16_t) + sizeof(int32_t), .kcs_elem_size = sizeof(int32_t), .kcs_name = "expected_retval"},
136 {.kcs_flags = KCS_SUBTYPE_FLAGS_NONE, .kcs_elem_type = KC_ST_UINT64, .kcs_elem_offset = 2 * (sizeof(uint16_t) + sizeof(int32_t)), .kcs_elem_size = sizeof(uint64_t), .kcs_name = "begin_time"},
137 {.kcs_flags = KCS_SUBTYPE_FLAGS_NONE, .kcs_elem_type = KC_ST_UINT64, .kcs_elem_offset = 2 * (sizeof(uint16_t) + sizeof(int32_t)) + sizeof(uint64_t), .kcs_elem_size = sizeof(uint64_t), .kcs_name = "end_time"},
138 {.kcs_flags = KCS_SUBTYPE_FLAGS_ARRAY,
139 .kcs_elem_type = KC_ST_CHAR,
140 .kcs_elem_offset = 2 * (sizeof(uint16_t) + sizeof(int32_t) + sizeof(uint64_t)),
141 .kcs_elem_size = KCS_SUBTYPE_PACK_SIZE(XNUPOST_TNAME_MAXLEN * sizeof(char), sizeof(char)),
142 .kcs_name = "test_name"}
143 };
144
145 const uint32_t kc_xnupost_test_def_count = sizeof(kc_xnupost_test_def) / sizeof(struct kcdata_subtype_descriptor);
146
147 kern_return_t xnupost_copyout_test(xnupost_test_t t, mach_vm_address_t outaddr);
148
149 int
xnupost_copyout_test(xnupost_test_t t,mach_vm_address_t outaddr)150 xnupost_copyout_test(xnupost_test_t t, mach_vm_address_t outaddr)
151 {
152 /* code to copyout test config */
153 int kret = 0;
154 size_t namelen = 0;
155
156 kret = copyout(&t->xt_config, (user_addr_t)outaddr, sizeof(uint16_t));
157 if (kret) {
158 return kret;
159 }
160 outaddr += sizeof(uint16_t);
161
162 kret = copyout(&t->xt_test_num, (user_addr_t)outaddr, sizeof(uint16_t));
163 if (kret) {
164 return kret;
165 }
166 outaddr += sizeof(uint16_t);
167
168 kret = copyout(&t->xt_retval, (user_addr_t)outaddr, sizeof(uint32_t));
169 if (kret) {
170 return kret;
171 }
172 outaddr += sizeof(uint32_t);
173
174 kret = copyout(&t->xt_expected_retval, (user_addr_t)outaddr, sizeof(uint32_t));
175 if (kret) {
176 return kret;
177 }
178 outaddr += sizeof(uint32_t);
179
180 kret = copyout(&t->xt_begin_time, (user_addr_t)outaddr, sizeof(uint64_t));
181 if (kret) {
182 return kret;
183 }
184 outaddr += sizeof(uint64_t);
185
186 kret = copyout(&t->xt_end_time, (user_addr_t)outaddr, sizeof(uint64_t));
187 if (kret) {
188 return kret;
189 }
190 outaddr += sizeof(uint64_t);
191
192 namelen = strnlen(t->xt_name, XNUPOST_TNAME_MAXLEN - 1) + 1;
193 kret = copyout(t->xt_name, (user_addr_t)outaddr, namelen);
194 if (kret) {
195 return kret;
196 }
197 outaddr += namelen;
198
199 return 0;
200 }
201
202 uint32_t
xnupost_get_estimated_testdata_size(void)203 xnupost_get_estimated_testdata_size(void)
204 {
205 uint32_t total_tests = bsd_post_tests_count + kernel_post_tests_count;
206 uint32_t elem_size = kc_xnupost_test_def[kc_xnupost_test_def_count - 1].kcs_elem_offset +
207 kcs_get_elem_size(&kc_xnupost_test_def[kc_xnupost_test_def_count - 1]);
208 uint32_t retval = 1024; /* account for type definition and mach timebase */
209 retval += 1024; /* kernel version and boot-args string data */
210 retval += (total_tests * elem_size);
211
212 return retval;
213 }
214
215 int
xnupost_export_testdata(void * outp,size_t size_in,uint32_t * lenp)216 xnupost_export_testdata(void * outp, size_t size_in, uint32_t * lenp)
217 {
218 struct kcdata_descriptor kcd;
219 mach_vm_address_t user_addr = 0;
220 mach_vm_address_t tmp_entry_addr = 0;
221 kern_return_t kret = 0;
222 uint32_t i = 0;
223 char kctype_name[32] = "xnupost_test_config";
224 mach_timebase_info_data_t timebase = {0, 0};
225 uint32_t length_to_copy = 0;
226 unsigned int size = (unsigned int)size_in;
227
228 if (size_in > UINT_MAX) {
229 return ENOSPC;
230 }
231
232 #define RET_IF_OP_FAIL \
233 do { \
234 if (kret != KERN_SUCCESS) { \
235 return (kret == KERN_NO_ACCESS) ? EACCES : ((kret == KERN_RESOURCE_SHORTAGE) ? ENOMEM : EINVAL); \
236 } \
237 } while (0)
238
239 kret = kcdata_memory_static_init(&kcd, (mach_vm_address_t)outp, KCDATA_BUFFER_BEGIN_XNUPOST_CONFIG, size, KCFLAG_USE_COPYOUT);
240 RET_IF_OP_FAIL;
241
242 /* add mach timebase info */
243 clock_timebase_info(&timebase);
244 kret = kcdata_get_memory_addr(&kcd, KCDATA_TYPE_TIMEBASE, sizeof(timebase), &user_addr);
245 RET_IF_OP_FAIL;
246 kret = copyout(&timebase, (user_addr_t)user_addr, sizeof(timebase));
247 RET_IF_OP_FAIL;
248
249 /* save boot-args and osversion string */
250 length_to_copy = MIN((uint32_t)(strlen(version) + 1), OSVERSIZE);
251 kret = kcdata_get_memory_addr(&kcd, STACKSHOT_KCTYPE_OSVERSION, length_to_copy, &user_addr);
252 RET_IF_OP_FAIL;
253 kret = copyout(&version[0], (user_addr_t)user_addr, length_to_copy);
254 RET_IF_OP_FAIL;
255
256 length_to_copy = MIN((uint32_t)(strlen(PE_boot_args()) + 1), BOOT_LINE_LENGTH);
257 kret = kcdata_get_memory_addr(&kcd, STACKSHOT_KCTYPE_BOOTARGS, length_to_copy, &user_addr);
258 RET_IF_OP_FAIL;
259 kret = copyout(PE_boot_args(), (user_addr_t)user_addr, length_to_copy);
260 RET_IF_OP_FAIL;
261
262 /* add type definition to buffer */
263 kret = kcdata_add_type_definition(&kcd, XNUPOST_KCTYPE_TESTCONFIG, kctype_name, &kc_xnupost_test_def[0],
264 kc_xnupost_test_def_count);
265 RET_IF_OP_FAIL;
266
267 /* add the tests to buffer as array */
268 uint32_t total_tests = bsd_post_tests_count + kernel_post_tests_count;
269 uint32_t elem_size = kc_xnupost_test_def[kc_xnupost_test_def_count - 1].kcs_elem_offset +
270 kcs_get_elem_size(&kc_xnupost_test_def[kc_xnupost_test_def_count - 1]);
271
272 kret = kcdata_get_memory_addr_for_array(&kcd, XNUPOST_KCTYPE_TESTCONFIG, elem_size, total_tests, &user_addr);
273 RET_IF_OP_FAIL;
274
275 for (i = 0; i < bsd_post_tests_count; i++) {
276 tmp_entry_addr = (mach_vm_address_t)((uint64_t)(user_addr) + (uint64_t)(i * elem_size));
277 kret = xnupost_copyout_test(&bsd_post_tests[i], tmp_entry_addr);
278 RET_IF_OP_FAIL;
279 }
280 user_addr = (mach_vm_address_t)((uint64_t)(user_addr) + (uint64_t)(i * elem_size));
281
282 for (i = 0; i < kernel_post_tests_count; i++) {
283 tmp_entry_addr = (mach_vm_address_t)((uint64_t)(user_addr) + (uint64_t)(i * elem_size));
284 kret = xnupost_copyout_test(&kernel_post_tests[i], tmp_entry_addr);
285 RET_IF_OP_FAIL;
286 }
287
288 if (kret == KERN_SUCCESS && lenp != NULL) {
289 *lenp = (uint32_t)kcdata_memory_get_used_bytes(&kcd);
290 }
291 RET_IF_OP_FAIL;
292
293 #undef RET_IF_OP_FAIL
294 return kret;
295 }
296
297 int
xnupost_reset_all_tests(void)298 xnupost_reset_all_tests(void)
299 {
300 xnupost_reset_tests(&bsd_post_tests[0], bsd_post_tests_count);
301 xnupost_reset_tests(&kernel_post_tests[0], kernel_post_tests_count);
302 return 0;
303 }
304