]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/xnu_quick_test/commpage_tests.c
792e78f00f6e2a4032cc3320f107ade243a28c11
[apple/xnu.git] / tools / tests / xnu_quick_test / commpage_tests.c
1 /*
2 * commpage_tests.c
3 * xnu_quick_test
4 *
5 * Copyright 2009 Apple Inc. All rights reserved.
6 *
7 */
8
9 #include "tests.h"
10 #include <unistd.h>
11 #include <stdint.h>
12 #include <err.h>
13 #include <sys/param.h>
14 #include <System/machine/cpu_capabilities.h>
15 #include <mach/mach.h>
16 #include <mach/mach_error.h>
17 #include <mach/bootstrap.h>
18
19
20 #ifdef _COMM_PAGE_ACTIVE_CPUS
21 int active_cpu_test(void);
22 #endif
23
24 int get_sys_uint64(const char *sel, uint64_t *val);
25 int get_sys_int32(const char *sel, int32_t *val);
26
27 #define getcommptr(var, commpageaddr) do { \
28 var = (typeof(var))(uintptr_t)(commpageaddr); \
29 } while(0)
30
31 /*
32 * Check some of the data in the commpage
33 * against manual sysctls
34 */
35 int commpage_data_tests( void * the_argp )
36 {
37 int ret;
38 uint64_t sys_u64;
39 int32_t sys_i32;
40
41 volatile uint64_t *comm_u64;
42 volatile uint32_t *comm_u32;
43 volatile uint16_t *comm_u16;
44 volatile uint8_t *comm_u8;
45
46
47 /* _COMM_PAGE_CPU_CAPABILITIES */
48 getcommptr(comm_u32, _COMM_PAGE_CPU_CAPABILITIES);
49
50 ret = get_sys_int32("hw.ncpu", &sys_i32);
51 if (ret) goto fail;
52
53 if (sys_i32 != ((*comm_u32 & kNumCPUs) >> kNumCPUsShift)) {
54 warnx("kNumCPUs does not match hw.ncpu");
55 ret = -1;
56 goto fail;
57 }
58
59 getcommptr(comm_u8, _COMM_PAGE_NCPUS);
60 if (sys_i32 != (*comm_u8)) {
61 warnx("_COMM_PAGE_NCPUS does not match hw.ncpu");
62 ret = -1;
63 goto fail;
64 }
65
66 ret = get_sys_int32("hw.logicalcpu", &sys_i32);
67 if (ret) goto fail;
68
69 if (sys_i32 != ((*comm_u32 & kNumCPUs) >> kNumCPUsShift)) {
70 warnx("kNumCPUs does not match hw.logicalcpu");
71 ret = -1;
72 goto fail;
73 }
74
75 /* Intel only capabilities */
76 #if defined(__i386__) || defined(__x86_64__)
77 ret = get_sys_int32("hw.optional.mmx", &sys_i32);
78 if (ret) goto fail;
79
80 if (!(sys_i32) ^ !(*comm_u32 & kHasMMX)) {
81 warnx("kHasMMX does not match hw.optional.mmx");
82 ret = -1;
83 goto fail;
84 }
85
86 ret = get_sys_int32("hw.optional.sse", &sys_i32);
87 if (ret) goto fail;
88
89 if (!(sys_i32) ^ !(*comm_u32 & kHasSSE)) {
90 warnx("kHasSSE does not match hw.optional.sse");
91 ret = -1;
92 goto fail;
93 }
94 ret = get_sys_int32("hw.optional.sse2", &sys_i32);
95 if (ret) goto fail;
96
97 if (!(sys_i32) ^ !(*comm_u32 & kHasSSE2)) {
98 warnx("kHasSSE2 does not match hw.optional.sse2");
99 ret = -1;
100 goto fail;
101 }
102
103 ret = get_sys_int32("hw.optional.sse3", &sys_i32);
104 if (ret) goto fail;
105
106 if (!(sys_i32) ^ !(*comm_u32 & kHasSSE3)) {
107 warnx("kHasSSE3 does not match hw.optional.sse3");
108 ret = -1;
109 goto fail;
110 }
111
112 ret = get_sys_int32("hw.optional.supplementalsse3", &sys_i32);
113 if (ret) goto fail;
114
115 if (!(sys_i32) ^ !(*comm_u32 & kHasSupplementalSSE3)) {
116 warnx("kHasSupplementalSSE3 does not match hw.optional.supplementalsse3");
117 ret = -1;
118 goto fail;
119 }
120
121 ret = get_sys_int32("hw.optional.sse4_1", &sys_i32);
122 if (ret) goto fail;
123
124 if (!(sys_i32) ^ !(*comm_u32 & kHasSSE4_1)) {
125 warnx("kHasSSE4_1 does not match hw.optional.sse4_1");
126 ret = -1;
127 goto fail;
128 }
129
130 ret = get_sys_int32("hw.optional.sse4_2", &sys_i32);
131 if (ret) goto fail;
132
133 if (!(sys_i32) ^ !(*comm_u32 & kHasSSE4_2)) {
134 warnx("kHasSSE4_2 does not match hw.optional.sse4_2");
135 ret = -1;
136 goto fail;
137 }
138
139 ret = get_sys_int32("hw.optional.aes", &sys_i32);
140 if (ret) goto fail;
141
142 if (!(sys_i32) ^ !(*comm_u32 & kHasAES)) {
143 warnx("kHasAES does not match hw.optional.aes");
144 ret = -1;
145 goto fail;
146 }
147
148 ret = get_sys_int32("hw.optional.x86_64", &sys_i32);
149 if (ret) goto fail;
150
151 if (!(sys_i32) ^ !(*comm_u32 & k64Bit)) {
152 warnx("k64Bit does not match hw.optional.x86_64");
153 ret = -1;
154 goto fail;
155 }
156 #endif /* __i386__ || __x86_64__ */
157
158 /* These fields are not implemented for all architectures */
159 #ifdef _COMM_PAGE_SCHED_GEN
160 uint32_t preempt_count1, preempt_count2;
161 uint64_t count;
162
163 ret = get_sys_uint64("hw.cpufrequency_max", &sys_u64);
164 if (ret) goto fail;
165
166 getcommptr(comm_u32, _COMM_PAGE_SCHED_GEN);
167 preempt_count1 = *comm_u32;
168 /* execute for around 1 quantum (10ms) */
169 for(count = MAX(10000000ULL, sys_u64/64); count > 0; count--) {
170 asm volatile("");
171 }
172 preempt_count2 = *comm_u32;
173 if (preempt_count1 >= preempt_count2) {
174 warnx("_COMM_PAGE_SCHED_GEN not incrementing (%u => %u)",
175 preempt_count1, preempt_count2);
176 ret = -1;
177 goto fail;
178 }
179 #endif /* _COMM_PAGE_SCHED_GEN */
180
181 #ifdef _COMM_PAGE_ACTIVE_CPUS
182 ret = get_sys_int32("hw.activecpu", &sys_i32);
183 if (ret) goto fail;
184
185 getcommptr(comm_u8, _COMM_PAGE_ACTIVE_CPUS);
186 if (sys_i32 != (*comm_u8)) {
187 warnx("_COMM_PAGE_ACTIVE_CPUS does not match hw.activecpu");
188 ret = -1;
189 goto fail;
190 }
191
192 ret = active_cpu_test();
193 if (ret) goto fail;
194 #endif /* _COMM_PAGE_ACTIVE_CPUS */
195
196 #ifdef _COMM_PAGE_PHYSICAL_CPUS
197 ret = get_sys_int32("hw.physicalcpu_max", &sys_i32);
198 if (ret) goto fail;
199
200 getcommptr(comm_u8, _COMM_PAGE_PHYSICAL_CPUS);
201 if (sys_i32 != (*comm_u8)) {
202 warnx("_COMM_PAGE_PHYSICAL_CPUS does not match hw.physicalcpu_max");
203 ret = -1;
204 goto fail;
205 }
206 #endif /* _COMM_PAGE_PHYSICAL_CPUS */
207
208 #ifdef _COMM_PAGE_LOGICAL_CPUS
209 ret = get_sys_int32("hw.logicalcpu_max", &sys_i32);
210 if (ret) goto fail;
211
212 getcommptr(comm_u8, _COMM_PAGE_LOGICAL_CPUS);
213 if (sys_i32 != (*comm_u8)) {
214 warnx("_COMM_PAGE_LOGICAL_CPUS does not match hw.logicalcpu_max");
215 ret = -1;
216 goto fail;
217 }
218 #endif /* _COMM_PAGE_LOGICAL_CPUS */
219
220 #if 0
221 #ifdef _COMM_PAGE_MEMORY_SIZE
222 ret = get_sys_uint64("hw.memsize", &sys_u64);
223 if (ret) goto fail;
224
225 getcommptr(comm_u64, _COMM_PAGE_MEMORY_SIZE);
226 if (sys_u64 != (*comm_u64)) {
227 warnx("_COMM_PAGE_MEMORY_SIZE does not match hw.memsize");
228 ret = -1;
229 goto fail;
230 }
231 #endif /* _COMM_PAGE_MEMORY_SIZE */
232 #endif
233
234 ret = 0;
235
236 fail:
237
238 return ret;
239 }
240
241
242 int get_sys_uint64(const char *sel, uint64_t *val)
243 {
244 size_t size = sizeof(*val);
245 int ret;
246
247 ret = sysctlbyname(sel, val, &size, NULL, 0);
248 if (ret == -1) {
249 warn("sysctlbyname(%s)", sel);
250 return ret;
251 }
252
253 // warnx("sysctlbyname(%s) => %llx", sel, *val);
254
255 return 0;
256 }
257
258 int get_sys_int32(const char *sel, int32_t *val)
259 {
260 size_t size = sizeof(*val);
261 int ret;
262
263 ret = sysctlbyname(sel, val, &size, NULL, 0);
264 if (ret == -1) {
265 warn("sysctlbyname(%s)", sel);
266 return ret;
267 }
268
269 // warnx("sysctlbyname(%s) => %x", sel, *val);
270
271 return 0;
272 }
273
274 #ifdef _COMM_PAGE_ACTIVE_CPUS
275 /*
276 * Try to find a secondary processor that we can disable,
277 * and make sure the commpage reflects that. This test
278 * will pass on UP systems, and if all secondary processors
279 * have been manually disabled
280 */
281 int active_cpu_test(void)
282 {
283 volatile uint8_t *activeaddr;
284 uint8_t original_activecpu;
285 boolean_t test_failed = FALSE;
286
287 /* Code stolen from hostinfo.c */
288 kern_return_t ret;
289 processor_t *processor_list;
290 host_name_port_t host;
291 struct processor_basic_info processor_basic_info;
292 int cpu_count;
293 int data_count;
294 int i;
295
296
297 getcommptr(activeaddr, _COMM_PAGE_ACTIVE_CPUS);
298 original_activecpu = *activeaddr;
299
300 host = mach_host_self();
301 ret = host_processors(host,
302 (processor_array_t *) &processor_list, &cpu_count);
303 if (ret != KERN_SUCCESS) {
304 mach_error("host_processors()", ret);
305 return ret;
306 }
307
308 /* skip master processor */
309 for (i = 1; i < cpu_count; i++) {
310 data_count = PROCESSOR_BASIC_INFO_COUNT;
311 ret = processor_info(processor_list[i], PROCESSOR_BASIC_INFO,
312 &host,
313 (processor_info_t) &processor_basic_info,
314 &data_count);
315 if (ret != KERN_SUCCESS) {
316 if (ret == MACH_SEND_INVALID_DEST) {
317 continue;
318 }
319 mach_error("processor_info", ret);
320 return ret;
321 }
322
323 if (processor_basic_info.running) {
324 /* found victim */
325 ret = processor_exit(processor_list[i]);
326 if (ret != KERN_SUCCESS) {
327 mach_error("processor_exit()", ret);
328 return ret;
329 }
330
331 sleep(1);
332
333 if (*activeaddr != (original_activecpu - 1)) {
334 test_failed = TRUE;
335 }
336
337 ret = processor_start(processor_list[i]);
338 if (ret != KERN_SUCCESS) {
339 mach_error("processor_exit()", ret);
340 return ret;
341 }
342
343 sleep(1);
344
345 break;
346 }
347 }
348
349 if (test_failed) {
350 warnx("_COMM_PAGE_ACTIVE_CPUS not updated after disabling a CPU");
351 return -1;
352 }
353
354 if (*activeaddr != original_activecpu) {
355 warnx("_COMM_PAGE_ACTIVE_CPUS not restored to original value");
356 return -1;
357 }
358
359 return 0;
360 }
361 #endif