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