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