]>
Commit | Line | Data |
---|---|---|
9eed1797 JM |
1 | /* |
2 | * arch/arm/kernel/kprobes-test.c | |
3 | * | |
4 | * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
08aab447 JM |
11 | /* |
12 | * This file contains test code for ARM kprobes. | |
13 | * | |
14 | * The top level function run_all_tests() executes tests for all of the | |
15 | * supported instruction sets: ARM, 16-bit Thumb, and 32-bit Thumb. These tests | |
16 | * fall into two categories; run_api_tests() checks basic functionality of the | |
17 | * kprobes API, and run_test_cases() is a comprehensive test for kprobes | |
18 | * instruction decoding and simulation. | |
19 | * | |
20 | * run_test_cases() first checks the kprobes decoding table for self consistency | |
21 | * (using table_test()) then executes a series of test cases for each of the CPU | |
22 | * instruction forms. coverage_start() and coverage_end() are used to verify | |
23 | * that these test cases cover all of the possible combinations of instructions | |
24 | * described by the kprobes decoding tables. | |
25 | * | |
26 | * The individual test cases are in kprobes-test-arm.c and kprobes-test-thumb.c | |
27 | * which use the macros defined in kprobes-test.h. The rest of this | |
28 | * documentation will describe the operation of the framework used by these | |
29 | * test cases. | |
30 | */ | |
31 | ||
a43bc69b JM |
32 | /* |
33 | * TESTING METHODOLOGY | |
34 | * ------------------- | |
35 | * | |
36 | * The methodology used to test an ARM instruction 'test_insn' is to use | |
37 | * inline assembler like: | |
38 | * | |
39 | * test_before: nop | |
40 | * test_case: test_insn | |
41 | * test_after: nop | |
42 | * | |
43 | * When the test case is run a kprobe is placed of each nop. The | |
44 | * post-handler of the test_before probe is used to modify the saved CPU | |
45 | * register context to that which we require for the test case. The | |
46 | * pre-handler of the of the test_after probe saves a copy of the CPU | |
47 | * register context. In this way we can execute test_insn with a specific | |
48 | * register context and see the results afterwards. | |
49 | * | |
50 | * To actually test the kprobes instruction emulation we perform the above | |
51 | * step a second time but with an additional kprobe on the test_case | |
52 | * instruction itself. If the emulation is accurate then the results seen | |
53 | * by the test_after probe will be identical to the first run which didn't | |
54 | * have a probe on test_case. | |
55 | * | |
56 | * Each test case is run several times with a variety of variations in the | |
57 | * flags value of stored in CPSR, and for Thumb code, different ITState. | |
58 | * | |
59 | * For instructions which can modify PC, a second test_after probe is used | |
60 | * like this: | |
61 | * | |
62 | * test_before: nop | |
63 | * test_case: test_insn | |
64 | * test_after: nop | |
65 | * b test_done | |
66 | * test_after2: nop | |
67 | * test_done: | |
68 | * | |
69 | * The test case is constructed such that test_insn branches to | |
70 | * test_after2, or, if testing a conditional instruction, it may just | |
71 | * continue to test_after. The probes inserted at both locations let us | |
72 | * determine which happened. A similar approach is used for testing | |
73 | * backwards branches... | |
74 | * | |
75 | * b test_before | |
76 | * b test_done @ helps to cope with off by 1 branches | |
77 | * test_after2: nop | |
78 | * b test_done | |
79 | * test_before: nop | |
80 | * test_case: test_insn | |
81 | * test_after: nop | |
82 | * test_done: | |
83 | * | |
84 | * The macros used to generate the assembler instructions describe above | |
85 | * are TEST_INSTRUCTION, TEST_BRANCH_F (branch forwards) and TEST_BRANCH_B | |
86 | * (branch backwards). In these, the local variables numbered 1, 50, 2 and | |
87 | * 99 represent: test_before, test_case, test_after2 and test_done. | |
88 | * | |
89 | * FRAMEWORK | |
90 | * --------- | |
91 | * | |
92 | * Each test case is wrapped between the pair of macros TESTCASE_START and | |
93 | * TESTCASE_END. As well as performing the inline assembler boilerplate, | |
94 | * these call out to the kprobes_test_case_start() and | |
95 | * kprobes_test_case_end() functions which drive the execution of the test | |
96 | * case. The specific arguments to use for each test case are stored as | |
97 | * inline data constructed using the various TEST_ARG_* macros. Putting | |
98 | * this all together, a simple test case may look like: | |
99 | * | |
100 | * TESTCASE_START("Testing mov r0, r7") | |
101 | * TEST_ARG_REG(7, 0x12345678) // Set r7=0x12345678 | |
102 | * TEST_ARG_END("") | |
103 | * TEST_INSTRUCTION("mov r0, r7") | |
104 | * TESTCASE_END | |
105 | * | |
106 | * Note, in practice the single convenience macro TEST_R would be used for this | |
107 | * instead. | |
108 | * | |
109 | * The above would expand to assembler looking something like: | |
110 | * | |
111 | * @ TESTCASE_START | |
112 | * bl __kprobes_test_case_start | |
113 | * @ start of inline data... | |
114 | * .ascii "mov r0, r7" @ text title for test case | |
115 | * .byte 0 | |
41b5368f | 116 | * .align 2, 0 |
a43bc69b JM |
117 | * |
118 | * @ TEST_ARG_REG | |
119 | * .byte ARG_TYPE_REG | |
120 | * .byte 7 | |
121 | * .short 0 | |
122 | * .word 0x1234567 | |
123 | * | |
124 | * @ TEST_ARG_END | |
125 | * .byte ARG_TYPE_END | |
126 | * .byte TEST_ISA @ flags, including ISA being tested | |
127 | * .short 50f-0f @ offset of 'test_before' | |
128 | * .short 2f-0f @ offset of 'test_after2' (if relevent) | |
129 | * .short 99f-0f @ offset of 'test_done' | |
130 | * @ start of test case code... | |
131 | * 0: | |
132 | * .code TEST_ISA @ switch to ISA being tested | |
133 | * | |
134 | * @ TEST_INSTRUCTION | |
135 | * 50: nop @ location for 'test_before' probe | |
136 | * 1: mov r0, r7 @ the test case instruction 'test_insn' | |
137 | * nop @ location for 'test_after' probe | |
138 | * | |
139 | * // TESTCASE_END | |
140 | * 2: | |
141 | * 99: bl __kprobes_test_case_end_##TEST_ISA | |
142 | * .code NONMAL_ISA | |
143 | * | |
144 | * When the above is execute the following happens... | |
145 | * | |
146 | * __kprobes_test_case_start() is an assembler wrapper which sets up space | |
147 | * for a stack buffer and calls the C function kprobes_test_case_start(). | |
148 | * This C function will do some initial processing of the inline data and | |
149 | * setup some global state. It then inserts the test_before and test_after | |
150 | * kprobes and returns a value which causes the assembler wrapper to jump | |
151 | * to the start of the test case code, (local label '0'). | |
152 | * | |
153 | * When the test case code executes, the test_before probe will be hit and | |
154 | * test_before_post_handler will call setup_test_context(). This fills the | |
155 | * stack buffer and CPU registers with a test pattern and then processes | |
156 | * the test case arguments. In our example there is one TEST_ARG_REG which | |
157 | * indicates that R7 should be loaded with the value 0x12345678. | |
158 | * | |
159 | * When the test_before probe ends, the test case continues and executes | |
160 | * the "mov r0, r7" instruction. It then hits the test_after probe and the | |
161 | * pre-handler for this (test_after_pre_handler) will save a copy of the | |
162 | * CPU register context. This should now have R0 holding the same value as | |
163 | * R7. | |
164 | * | |
165 | * Finally we get to the call to __kprobes_test_case_end_{32,16}. This is | |
166 | * an assembler wrapper which switches back to the ISA used by the test | |
167 | * code and calls the C function kprobes_test_case_end(). | |
168 | * | |
169 | * For each run through the test case, test_case_run_count is incremented | |
170 | * by one. For even runs, kprobes_test_case_end() saves a copy of the | |
171 | * register and stack buffer contents from the test case just run. It then | |
172 | * inserts a kprobe on the test case instruction 'test_insn' and returns a | |
173 | * value to cause the test case code to be re-run. | |
174 | * | |
175 | * For odd numbered runs, kprobes_test_case_end() compares the register and | |
176 | * stack buffer contents to those that were saved on the previous even | |
177 | * numbered run (the one without the kprobe on test_insn). These should be | |
178 | * the same if the kprobe instruction simulation routine is correct. | |
179 | * | |
180 | * The pair of test case runs is repeated with different combinations of | |
181 | * flag values in CPSR and, for Thumb, different ITState. This is | |
182 | * controlled by test_context_cpsr(). | |
183 | * | |
184 | * BUILDING TEST CASES | |
185 | * ------------------- | |
186 | * | |
187 | * | |
188 | * As an aid to building test cases, the stack buffer is initialised with | |
189 | * some special values: | |
190 | * | |
191 | * [SP+13*4] Contains SP+120. This can be used to test instructions | |
192 | * which load a value into SP. | |
193 | * | |
194 | * [SP+15*4] When testing branching instructions using TEST_BRANCH_{F,B}, | |
195 | * this holds the target address of the branch, 'test_after2'. | |
196 | * This can be used to test instructions which load a PC value | |
197 | * from memory. | |
198 | */ | |
199 | ||
9eed1797 JM |
200 | #include <linux/kernel.h> |
201 | #include <linux/module.h> | |
963780df | 202 | #include <linux/slab.h> |
9eed1797 | 203 | #include <linux/kprobes.h> |
21254ebc DL |
204 | #include <linux/errno.h> |
205 | #include <linux/stddef.h> | |
206 | #include <linux/bug.h> | |
c41584dd LL |
207 | #include <asm/opcodes.h> |
208 | ||
9eed1797 | 209 | #include "kprobes.h" |
47e190fa DL |
210 | #include "probes-arm.h" |
211 | #include "probes-thumb.h" | |
a43bc69b | 212 | #include "kprobes-test.h" |
9eed1797 JM |
213 | |
214 | ||
ce5af3ba JM |
215 | #define BENCHMARKING 1 |
216 | ||
217 | ||
9eed1797 JM |
218 | /* |
219 | * Test basic API | |
220 | */ | |
221 | ||
222 | static bool test_regs_ok; | |
223 | static int test_func_instance; | |
224 | static int pre_handler_called; | |
225 | static int post_handler_called; | |
226 | static int jprobe_func_called; | |
227 | static int kretprobe_handler_called; | |
48f7bc86 | 228 | static int tests_failed; |
9eed1797 JM |
229 | |
230 | #define FUNC_ARG1 0x12345678 | |
231 | #define FUNC_ARG2 0xabcdef | |
232 | ||
233 | ||
234 | #ifndef CONFIG_THUMB2_KERNEL | |
235 | ||
236 | long arm_func(long r0, long r1); | |
237 | ||
238 | static void __used __naked __arm_kprobes_test_func(void) | |
239 | { | |
240 | __asm__ __volatile__ ( | |
241 | ".arm \n\t" | |
242 | ".type arm_func, %%function \n\t" | |
243 | "arm_func: \n\t" | |
244 | "adds r0, r0, r1 \n\t" | |
245 | "bx lr \n\t" | |
246 | ".code "NORMAL_ISA /* Back to Thumb if necessary */ | |
247 | : : : "r0", "r1", "cc" | |
248 | ); | |
249 | } | |
250 | ||
251 | #else /* CONFIG_THUMB2_KERNEL */ | |
252 | ||
253 | long thumb16_func(long r0, long r1); | |
254 | long thumb32even_func(long r0, long r1); | |
255 | long thumb32odd_func(long r0, long r1); | |
256 | ||
257 | static void __used __naked __thumb_kprobes_test_funcs(void) | |
258 | { | |
259 | __asm__ __volatile__ ( | |
260 | ".type thumb16_func, %%function \n\t" | |
261 | "thumb16_func: \n\t" | |
262 | "adds.n r0, r0, r1 \n\t" | |
263 | "bx lr \n\t" | |
264 | ||
265 | ".align \n\t" | |
266 | ".type thumb32even_func, %%function \n\t" | |
267 | "thumb32even_func: \n\t" | |
268 | "adds.w r0, r0, r1 \n\t" | |
269 | "bx lr \n\t" | |
270 | ||
271 | ".align \n\t" | |
272 | "nop.n \n\t" | |
273 | ".type thumb32odd_func, %%function \n\t" | |
274 | "thumb32odd_func: \n\t" | |
275 | "adds.w r0, r0, r1 \n\t" | |
276 | "bx lr \n\t" | |
277 | ||
278 | : : : "r0", "r1", "cc" | |
279 | ); | |
280 | } | |
281 | ||
282 | #endif /* CONFIG_THUMB2_KERNEL */ | |
283 | ||
284 | ||
285 | static int call_test_func(long (*func)(long, long), bool check_test_regs) | |
286 | { | |
287 | long ret; | |
288 | ||
289 | ++test_func_instance; | |
290 | test_regs_ok = false; | |
291 | ||
292 | ret = (*func)(FUNC_ARG1, FUNC_ARG2); | |
293 | if (ret != FUNC_ARG1 + FUNC_ARG2) { | |
294 | pr_err("FAIL: call_test_func: func returned %lx\n", ret); | |
295 | return false; | |
296 | } | |
297 | ||
298 | if (check_test_regs && !test_regs_ok) { | |
299 | pr_err("FAIL: test regs not OK\n"); | |
300 | return false; | |
301 | } | |
302 | ||
303 | return true; | |
304 | } | |
305 | ||
306 | static int __kprobes pre_handler(struct kprobe *p, struct pt_regs *regs) | |
307 | { | |
308 | pre_handler_called = test_func_instance; | |
309 | if (regs->ARM_r0 == FUNC_ARG1 && regs->ARM_r1 == FUNC_ARG2) | |
310 | test_regs_ok = true; | |
311 | return 0; | |
312 | } | |
313 | ||
314 | static void __kprobes post_handler(struct kprobe *p, struct pt_regs *regs, | |
315 | unsigned long flags) | |
316 | { | |
317 | post_handler_called = test_func_instance; | |
318 | if (regs->ARM_r0 != FUNC_ARG1 + FUNC_ARG2 || regs->ARM_r1 != FUNC_ARG2) | |
319 | test_regs_ok = false; | |
320 | } | |
321 | ||
322 | static struct kprobe the_kprobe = { | |
323 | .addr = 0, | |
324 | .pre_handler = pre_handler, | |
325 | .post_handler = post_handler | |
326 | }; | |
327 | ||
328 | static int test_kprobe(long (*func)(long, long)) | |
329 | { | |
330 | int ret; | |
331 | ||
332 | the_kprobe.addr = (kprobe_opcode_t *)func; | |
333 | ret = register_kprobe(&the_kprobe); | |
334 | if (ret < 0) { | |
335 | pr_err("FAIL: register_kprobe failed with %d\n", ret); | |
336 | return ret; | |
337 | } | |
338 | ||
339 | ret = call_test_func(func, true); | |
340 | ||
341 | unregister_kprobe(&the_kprobe); | |
342 | the_kprobe.flags = 0; /* Clear disable flag to allow reuse */ | |
343 | ||
344 | if (!ret) | |
345 | return -EINVAL; | |
346 | if (pre_handler_called != test_func_instance) { | |
347 | pr_err("FAIL: kprobe pre_handler not called\n"); | |
348 | return -EINVAL; | |
349 | } | |
350 | if (post_handler_called != test_func_instance) { | |
351 | pr_err("FAIL: kprobe post_handler not called\n"); | |
352 | return -EINVAL; | |
353 | } | |
354 | if (!call_test_func(func, false)) | |
355 | return -EINVAL; | |
356 | if (pre_handler_called == test_func_instance || | |
357 | post_handler_called == test_func_instance) { | |
358 | pr_err("FAIL: probe called after unregistering\n"); | |
359 | return -EINVAL; | |
360 | } | |
361 | ||
362 | return 0; | |
363 | } | |
364 | ||
365 | static void __kprobes jprobe_func(long r0, long r1) | |
366 | { | |
367 | jprobe_func_called = test_func_instance; | |
368 | if (r0 == FUNC_ARG1 && r1 == FUNC_ARG2) | |
369 | test_regs_ok = true; | |
370 | jprobe_return(); | |
371 | } | |
372 | ||
373 | static struct jprobe the_jprobe = { | |
374 | .entry = jprobe_func, | |
375 | }; | |
376 | ||
377 | static int test_jprobe(long (*func)(long, long)) | |
378 | { | |
379 | int ret; | |
380 | ||
381 | the_jprobe.kp.addr = (kprobe_opcode_t *)func; | |
382 | ret = register_jprobe(&the_jprobe); | |
383 | if (ret < 0) { | |
384 | pr_err("FAIL: register_jprobe failed with %d\n", ret); | |
385 | return ret; | |
386 | } | |
387 | ||
388 | ret = call_test_func(func, true); | |
389 | ||
390 | unregister_jprobe(&the_jprobe); | |
391 | the_jprobe.kp.flags = 0; /* Clear disable flag to allow reuse */ | |
392 | ||
393 | if (!ret) | |
394 | return -EINVAL; | |
395 | if (jprobe_func_called != test_func_instance) { | |
396 | pr_err("FAIL: jprobe handler function not called\n"); | |
397 | return -EINVAL; | |
398 | } | |
399 | if (!call_test_func(func, false)) | |
400 | return -EINVAL; | |
401 | if (jprobe_func_called == test_func_instance) { | |
402 | pr_err("FAIL: probe called after unregistering\n"); | |
403 | return -EINVAL; | |
404 | } | |
405 | ||
406 | return 0; | |
407 | } | |
408 | ||
409 | static int __kprobes | |
410 | kretprobe_handler(struct kretprobe_instance *ri, struct pt_regs *regs) | |
411 | { | |
412 | kretprobe_handler_called = test_func_instance; | |
413 | if (regs_return_value(regs) == FUNC_ARG1 + FUNC_ARG2) | |
414 | test_regs_ok = true; | |
415 | return 0; | |
416 | } | |
417 | ||
418 | static struct kretprobe the_kretprobe = { | |
419 | .handler = kretprobe_handler, | |
420 | }; | |
421 | ||
422 | static int test_kretprobe(long (*func)(long, long)) | |
423 | { | |
424 | int ret; | |
425 | ||
426 | the_kretprobe.kp.addr = (kprobe_opcode_t *)func; | |
427 | ret = register_kretprobe(&the_kretprobe); | |
428 | if (ret < 0) { | |
429 | pr_err("FAIL: register_kretprobe failed with %d\n", ret); | |
430 | return ret; | |
431 | } | |
432 | ||
433 | ret = call_test_func(func, true); | |
434 | ||
435 | unregister_kretprobe(&the_kretprobe); | |
436 | the_kretprobe.kp.flags = 0; /* Clear disable flag to allow reuse */ | |
437 | ||
438 | if (!ret) | |
439 | return -EINVAL; | |
440 | if (kretprobe_handler_called != test_func_instance) { | |
441 | pr_err("FAIL: kretprobe handler not called\n"); | |
442 | return -EINVAL; | |
443 | } | |
444 | if (!call_test_func(func, false)) | |
445 | return -EINVAL; | |
446 | if (jprobe_func_called == test_func_instance) { | |
447 | pr_err("FAIL: kretprobe called after unregistering\n"); | |
448 | return -EINVAL; | |
449 | } | |
450 | ||
451 | return 0; | |
452 | } | |
453 | ||
454 | static int run_api_tests(long (*func)(long, long)) | |
455 | { | |
456 | int ret; | |
457 | ||
458 | pr_info(" kprobe\n"); | |
459 | ret = test_kprobe(func); | |
460 | if (ret < 0) | |
461 | return ret; | |
462 | ||
463 | pr_info(" jprobe\n"); | |
464 | ret = test_jprobe(func); | |
48f7bc86 JM |
465 | #if defined(CONFIG_THUMB2_KERNEL) && !defined(MODULE) |
466 | if (ret == -EINVAL) { | |
467 | pr_err("FAIL: Known longtime bug with jprobe on Thumb kernels\n"); | |
468 | tests_failed = ret; | |
469 | ret = 0; | |
470 | } | |
471 | #endif | |
9eed1797 JM |
472 | if (ret < 0) |
473 | return ret; | |
474 | ||
475 | pr_info(" kretprobe\n"); | |
476 | ret = test_kretprobe(func); | |
477 | if (ret < 0) | |
478 | return ret; | |
479 | ||
480 | return 0; | |
481 | } | |
482 | ||
483 | ||
ce5af3ba JM |
484 | /* |
485 | * Benchmarking | |
486 | */ | |
487 | ||
488 | #if BENCHMARKING | |
489 | ||
490 | static void __naked benchmark_nop(void) | |
491 | { | |
492 | __asm__ __volatile__ ( | |
493 | "nop \n\t" | |
494 | "bx lr" | |
495 | ); | |
496 | } | |
497 | ||
498 | #ifdef CONFIG_THUMB2_KERNEL | |
499 | #define wide ".w" | |
500 | #else | |
501 | #define wide | |
502 | #endif | |
503 | ||
504 | static void __naked benchmark_pushpop1(void) | |
505 | { | |
506 | __asm__ __volatile__ ( | |
507 | "stmdb"wide" sp!, {r3-r11,lr} \n\t" | |
508 | "ldmia"wide" sp!, {r3-r11,pc}" | |
509 | ); | |
510 | } | |
511 | ||
512 | static void __naked benchmark_pushpop2(void) | |
513 | { | |
514 | __asm__ __volatile__ ( | |
515 | "stmdb"wide" sp!, {r0-r8,lr} \n\t" | |
516 | "ldmia"wide" sp!, {r0-r8,pc}" | |
517 | ); | |
518 | } | |
519 | ||
520 | static void __naked benchmark_pushpop3(void) | |
521 | { | |
522 | __asm__ __volatile__ ( | |
523 | "stmdb"wide" sp!, {r4,lr} \n\t" | |
524 | "ldmia"wide" sp!, {r4,pc}" | |
525 | ); | |
526 | } | |
527 | ||
528 | static void __naked benchmark_pushpop4(void) | |
529 | { | |
530 | __asm__ __volatile__ ( | |
531 | "stmdb"wide" sp!, {r0,lr} \n\t" | |
532 | "ldmia"wide" sp!, {r0,pc}" | |
533 | ); | |
534 | } | |
535 | ||
536 | ||
537 | #ifdef CONFIG_THUMB2_KERNEL | |
538 | ||
539 | static void __naked benchmark_pushpop_thumb(void) | |
540 | { | |
541 | __asm__ __volatile__ ( | |
542 | "push.n {r0-r7,lr} \n\t" | |
543 | "pop.n {r0-r7,pc}" | |
544 | ); | |
545 | } | |
546 | ||
547 | #endif | |
548 | ||
549 | static int __kprobes | |
550 | benchmark_pre_handler(struct kprobe *p, struct pt_regs *regs) | |
551 | { | |
552 | return 0; | |
553 | } | |
554 | ||
555 | static int benchmark(void(*fn)(void)) | |
556 | { | |
557 | unsigned n, i, t, t0; | |
558 | ||
559 | for (n = 1000; ; n *= 2) { | |
560 | t0 = sched_clock(); | |
561 | for (i = n; i > 0; --i) | |
562 | fn(); | |
563 | t = sched_clock() - t0; | |
564 | if (t >= 250000000) | |
565 | break; /* Stop once we took more than 0.25 seconds */ | |
566 | } | |
567 | return t / n; /* Time for one iteration in nanoseconds */ | |
568 | }; | |
569 | ||
570 | static int kprobe_benchmark(void(*fn)(void), unsigned offset) | |
571 | { | |
572 | struct kprobe k = { | |
573 | .addr = (kprobe_opcode_t *)((uintptr_t)fn + offset), | |
574 | .pre_handler = benchmark_pre_handler, | |
575 | }; | |
576 | ||
577 | int ret = register_kprobe(&k); | |
578 | if (ret < 0) { | |
579 | pr_err("FAIL: register_kprobe failed with %d\n", ret); | |
580 | return ret; | |
581 | } | |
582 | ||
583 | ret = benchmark(fn); | |
584 | ||
585 | unregister_kprobe(&k); | |
586 | return ret; | |
587 | }; | |
588 | ||
589 | struct benchmarks { | |
590 | void (*fn)(void); | |
591 | unsigned offset; | |
592 | const char *title; | |
593 | }; | |
594 | ||
595 | static int run_benchmarks(void) | |
596 | { | |
597 | int ret; | |
598 | struct benchmarks list[] = { | |
599 | {&benchmark_nop, 0, "nop"}, | |
600 | /* | |
601 | * benchmark_pushpop{1,3} will have the optimised | |
602 | * instruction emulation, whilst benchmark_pushpop{2,4} will | |
603 | * be the equivalent unoptimised instructions. | |
604 | */ | |
605 | {&benchmark_pushpop1, 0, "stmdb sp!, {r3-r11,lr}"}, | |
606 | {&benchmark_pushpop1, 4, "ldmia sp!, {r3-r11,pc}"}, | |
607 | {&benchmark_pushpop2, 0, "stmdb sp!, {r0-r8,lr}"}, | |
608 | {&benchmark_pushpop2, 4, "ldmia sp!, {r0-r8,pc}"}, | |
609 | {&benchmark_pushpop3, 0, "stmdb sp!, {r4,lr}"}, | |
610 | {&benchmark_pushpop3, 4, "ldmia sp!, {r4,pc}"}, | |
611 | {&benchmark_pushpop4, 0, "stmdb sp!, {r0,lr}"}, | |
612 | {&benchmark_pushpop4, 4, "ldmia sp!, {r0,pc}"}, | |
613 | #ifdef CONFIG_THUMB2_KERNEL | |
614 | {&benchmark_pushpop_thumb, 0, "push.n {r0-r7,lr}"}, | |
615 | {&benchmark_pushpop_thumb, 2, "pop.n {r0-r7,pc}"}, | |
616 | #endif | |
617 | {0} | |
618 | }; | |
619 | ||
620 | struct benchmarks *b; | |
621 | for (b = list; b->fn; ++b) { | |
622 | ret = kprobe_benchmark(b->fn, b->offset); | |
623 | if (ret < 0) | |
624 | return ret; | |
625 | pr_info(" %dns for kprobe %s\n", ret, b->title); | |
626 | } | |
627 | ||
628 | pr_info("\n"); | |
629 | return 0; | |
630 | } | |
631 | ||
632 | #endif /* BENCHMARKING */ | |
633 | ||
634 | ||
68f360e7 JM |
635 | /* |
636 | * Decoding table self-consistency tests | |
637 | */ | |
638 | ||
639 | static const int decode_struct_sizes[NUM_DECODE_TYPES] = { | |
640 | [DECODE_TYPE_TABLE] = sizeof(struct decode_table), | |
641 | [DECODE_TYPE_CUSTOM] = sizeof(struct decode_custom), | |
642 | [DECODE_TYPE_SIMULATE] = sizeof(struct decode_simulate), | |
643 | [DECODE_TYPE_EMULATE] = sizeof(struct decode_emulate), | |
644 | [DECODE_TYPE_OR] = sizeof(struct decode_or), | |
645 | [DECODE_TYPE_REJECT] = sizeof(struct decode_reject) | |
646 | }; | |
647 | ||
648 | static int table_iter(const union decode_item *table, | |
649 | int (*fn)(const struct decode_header *, void *), | |
650 | void *args) | |
651 | { | |
652 | const struct decode_header *h = (struct decode_header *)table; | |
653 | int result; | |
654 | ||
655 | for (;;) { | |
656 | enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK; | |
657 | ||
658 | if (type == DECODE_TYPE_END) | |
659 | return 0; | |
660 | ||
661 | result = fn(h, args); | |
662 | if (result) | |
663 | return result; | |
664 | ||
665 | h = (struct decode_header *) | |
666 | ((uintptr_t)h + decode_struct_sizes[type]); | |
667 | ||
668 | } | |
669 | } | |
670 | ||
671 | static int table_test_fail(const struct decode_header *h, const char* message) | |
672 | { | |
673 | ||
674 | pr_err("FAIL: kprobes test failure \"%s\" (mask %08x, value %08x)\n", | |
675 | message, h->mask.bits, h->value.bits); | |
676 | return -EINVAL; | |
677 | } | |
678 | ||
679 | struct table_test_args { | |
680 | const union decode_item *root_table; | |
681 | u32 parent_mask; | |
682 | u32 parent_value; | |
683 | }; | |
684 | ||
685 | static int table_test_fn(const struct decode_header *h, void *args) | |
686 | { | |
687 | struct table_test_args *a = (struct table_test_args *)args; | |
688 | enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK; | |
689 | ||
690 | if (h->value.bits & ~h->mask.bits) | |
691 | return table_test_fail(h, "Match value has bits not in mask"); | |
692 | ||
693 | if ((h->mask.bits & a->parent_mask) != a->parent_mask) | |
694 | return table_test_fail(h, "Mask has bits not in parent mask"); | |
695 | ||
696 | if ((h->value.bits ^ a->parent_value) & a->parent_mask) | |
697 | return table_test_fail(h, "Value is inconsistent with parent"); | |
698 | ||
699 | if (type == DECODE_TYPE_TABLE) { | |
700 | struct decode_table *d = (struct decode_table *)h; | |
701 | struct table_test_args args2 = *a; | |
702 | args2.parent_mask = h->mask.bits; | |
703 | args2.parent_value = h->value.bits; | |
704 | return table_iter(d->table.table, table_test_fn, &args2); | |
705 | } | |
706 | ||
707 | return 0; | |
708 | } | |
709 | ||
710 | static int table_test(const union decode_item *table) | |
711 | { | |
712 | struct table_test_args args = { | |
713 | .root_table = table, | |
714 | .parent_mask = 0, | |
715 | .parent_value = 0 | |
716 | }; | |
717 | return table_iter(args.root_table, table_test_fn, &args); | |
718 | } | |
719 | ||
720 | ||
963780df JM |
721 | /* |
722 | * Decoding table test coverage analysis | |
723 | * | |
724 | * coverage_start() builds a coverage_table which contains a list of | |
725 | * coverage_entry's to match each entry in the specified kprobes instruction | |
726 | * decoding table. | |
727 | * | |
728 | * When test cases are run, coverage_add() is called to process each case. | |
729 | * This looks up the corresponding entry in the coverage_table and sets it as | |
730 | * being matched, as well as clearing the regs flag appropriate for the test. | |
731 | * | |
732 | * After all test cases have been run, coverage_end() is called to check that | |
733 | * all entries in coverage_table have been matched and that all regs flags are | |
734 | * cleared. I.e. that all possible combinations of instructions described by | |
735 | * the kprobes decoding tables have had a test case executed for them. | |
736 | */ | |
737 | ||
738 | bool coverage_fail; | |
739 | ||
740 | #define MAX_COVERAGE_ENTRIES 256 | |
741 | ||
742 | struct coverage_entry { | |
743 | const struct decode_header *header; | |
744 | unsigned regs; | |
745 | unsigned nesting; | |
746 | char matched; | |
747 | }; | |
748 | ||
749 | struct coverage_table { | |
750 | struct coverage_entry *base; | |
751 | unsigned num_entries; | |
752 | unsigned nesting; | |
753 | }; | |
754 | ||
755 | struct coverage_table coverage; | |
756 | ||
757 | #define COVERAGE_ANY_REG (1<<0) | |
758 | #define COVERAGE_SP (1<<1) | |
759 | #define COVERAGE_PC (1<<2) | |
760 | #define COVERAGE_PCWB (1<<3) | |
761 | ||
762 | static const char coverage_register_lookup[16] = { | |
763 | [REG_TYPE_ANY] = COVERAGE_ANY_REG | COVERAGE_SP | COVERAGE_PC, | |
764 | [REG_TYPE_SAMEAS16] = COVERAGE_ANY_REG, | |
765 | [REG_TYPE_SP] = COVERAGE_SP, | |
766 | [REG_TYPE_PC] = COVERAGE_PC, | |
767 | [REG_TYPE_NOSP] = COVERAGE_ANY_REG | COVERAGE_SP, | |
768 | [REG_TYPE_NOSPPC] = COVERAGE_ANY_REG | COVERAGE_SP | COVERAGE_PC, | |
769 | [REG_TYPE_NOPC] = COVERAGE_ANY_REG | COVERAGE_PC, | |
770 | [REG_TYPE_NOPCWB] = COVERAGE_ANY_REG | COVERAGE_PC | COVERAGE_PCWB, | |
771 | [REG_TYPE_NOPCX] = COVERAGE_ANY_REG, | |
772 | [REG_TYPE_NOSPPCX] = COVERAGE_ANY_REG | COVERAGE_SP, | |
773 | }; | |
774 | ||
775 | unsigned coverage_start_registers(const struct decode_header *h) | |
776 | { | |
777 | unsigned regs = 0; | |
778 | int i; | |
779 | for (i = 0; i < 20; i += 4) { | |
780 | int r = (h->type_regs.bits >> (DECODE_TYPE_BITS + i)) & 0xf; | |
781 | regs |= coverage_register_lookup[r] << i; | |
782 | } | |
783 | return regs; | |
784 | } | |
785 | ||
786 | static int coverage_start_fn(const struct decode_header *h, void *args) | |
787 | { | |
788 | struct coverage_table *coverage = (struct coverage_table *)args; | |
789 | enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK; | |
790 | struct coverage_entry *entry = coverage->base + coverage->num_entries; | |
791 | ||
792 | if (coverage->num_entries == MAX_COVERAGE_ENTRIES - 1) { | |
793 | pr_err("FAIL: Out of space for test coverage data"); | |
794 | return -ENOMEM; | |
795 | } | |
796 | ||
797 | ++coverage->num_entries; | |
798 | ||
799 | entry->header = h; | |
800 | entry->regs = coverage_start_registers(h); | |
801 | entry->nesting = coverage->nesting; | |
802 | entry->matched = false; | |
803 | ||
804 | if (type == DECODE_TYPE_TABLE) { | |
805 | struct decode_table *d = (struct decode_table *)h; | |
806 | int ret; | |
807 | ++coverage->nesting; | |
808 | ret = table_iter(d->table.table, coverage_start_fn, coverage); | |
809 | --coverage->nesting; | |
810 | return ret; | |
811 | } | |
812 | ||
813 | return 0; | |
814 | } | |
815 | ||
816 | static int coverage_start(const union decode_item *table) | |
817 | { | |
818 | coverage.base = kmalloc(MAX_COVERAGE_ENTRIES * | |
819 | sizeof(struct coverage_entry), GFP_KERNEL); | |
820 | coverage.num_entries = 0; | |
821 | coverage.nesting = 0; | |
822 | return table_iter(table, coverage_start_fn, &coverage); | |
823 | } | |
824 | ||
825 | static void | |
826 | coverage_add_registers(struct coverage_entry *entry, kprobe_opcode_t insn) | |
827 | { | |
828 | int regs = entry->header->type_regs.bits >> DECODE_TYPE_BITS; | |
829 | int i; | |
830 | for (i = 0; i < 20; i += 4) { | |
831 | enum decode_reg_type reg_type = (regs >> i) & 0xf; | |
832 | int reg = (insn >> i) & 0xf; | |
833 | int flag; | |
834 | ||
835 | if (!reg_type) | |
836 | continue; | |
837 | ||
838 | if (reg == 13) | |
839 | flag = COVERAGE_SP; | |
840 | else if (reg == 15) | |
841 | flag = COVERAGE_PC; | |
842 | else | |
843 | flag = COVERAGE_ANY_REG; | |
844 | entry->regs &= ~(flag << i); | |
845 | ||
846 | switch (reg_type) { | |
847 | ||
848 | case REG_TYPE_NONE: | |
849 | case REG_TYPE_ANY: | |
850 | case REG_TYPE_SAMEAS16: | |
851 | break; | |
852 | ||
853 | case REG_TYPE_SP: | |
854 | if (reg != 13) | |
855 | return; | |
856 | break; | |
857 | ||
858 | case REG_TYPE_PC: | |
859 | if (reg != 15) | |
860 | return; | |
861 | break; | |
862 | ||
863 | case REG_TYPE_NOSP: | |
864 | if (reg == 13) | |
865 | return; | |
866 | break; | |
867 | ||
868 | case REG_TYPE_NOSPPC: | |
869 | case REG_TYPE_NOSPPCX: | |
870 | if (reg == 13 || reg == 15) | |
871 | return; | |
872 | break; | |
873 | ||
874 | case REG_TYPE_NOPCWB: | |
875 | if (!is_writeback(insn)) | |
876 | break; | |
877 | if (reg == 15) { | |
878 | entry->regs &= ~(COVERAGE_PCWB << i); | |
879 | return; | |
880 | } | |
881 | break; | |
882 | ||
883 | case REG_TYPE_NOPC: | |
884 | case REG_TYPE_NOPCX: | |
885 | if (reg == 15) | |
886 | return; | |
887 | break; | |
888 | } | |
889 | ||
890 | } | |
891 | } | |
892 | ||
893 | static void coverage_add(kprobe_opcode_t insn) | |
894 | { | |
895 | struct coverage_entry *entry = coverage.base; | |
896 | struct coverage_entry *end = coverage.base + coverage.num_entries; | |
897 | bool matched = false; | |
898 | unsigned nesting = 0; | |
899 | ||
900 | for (; entry < end; ++entry) { | |
901 | const struct decode_header *h = entry->header; | |
902 | enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK; | |
903 | ||
904 | if (entry->nesting > nesting) | |
905 | continue; /* Skip sub-table we didn't match */ | |
906 | ||
907 | if (entry->nesting < nesting) | |
908 | break; /* End of sub-table we were scanning */ | |
909 | ||
910 | if (!matched) { | |
911 | if ((insn & h->mask.bits) != h->value.bits) | |
912 | continue; | |
913 | entry->matched = true; | |
914 | } | |
915 | ||
916 | switch (type) { | |
917 | ||
918 | case DECODE_TYPE_TABLE: | |
919 | ++nesting; | |
920 | break; | |
921 | ||
922 | case DECODE_TYPE_CUSTOM: | |
923 | case DECODE_TYPE_SIMULATE: | |
924 | case DECODE_TYPE_EMULATE: | |
925 | coverage_add_registers(entry, insn); | |
926 | return; | |
927 | ||
928 | case DECODE_TYPE_OR: | |
929 | matched = true; | |
930 | break; | |
931 | ||
932 | case DECODE_TYPE_REJECT: | |
933 | default: | |
934 | return; | |
935 | } | |
936 | ||
937 | } | |
938 | } | |
939 | ||
940 | static void coverage_end(void) | |
941 | { | |
942 | struct coverage_entry *entry = coverage.base; | |
943 | struct coverage_entry *end = coverage.base + coverage.num_entries; | |
944 | ||
945 | for (; entry < end; ++entry) { | |
946 | u32 mask = entry->header->mask.bits; | |
947 | u32 value = entry->header->value.bits; | |
948 | ||
949 | if (entry->regs) { | |
950 | pr_err("FAIL: Register test coverage missing for %08x %08x (%05x)\n", | |
951 | mask, value, entry->regs); | |
952 | coverage_fail = true; | |
953 | } | |
954 | if (!entry->matched) { | |
955 | pr_err("FAIL: Test coverage entry missing for %08x %08x\n", | |
956 | mask, value); | |
957 | coverage_fail = true; | |
958 | } | |
959 | } | |
960 | ||
961 | kfree(coverage.base); | |
962 | } | |
963 | ||
964 | ||
a43bc69b JM |
965 | /* |
966 | * Framework for instruction set test cases | |
967 | */ | |
968 | ||
969 | void __naked __kprobes_test_case_start(void) | |
970 | { | |
971 | __asm__ __volatile__ ( | |
972 | "stmdb sp!, {r4-r11} \n\t" | |
973 | "sub sp, sp, #"__stringify(TEST_MEMORY_SIZE)"\n\t" | |
974 | "bic r0, lr, #1 @ r0 = inline title string \n\t" | |
975 | "mov r1, sp \n\t" | |
976 | "bl kprobes_test_case_start \n\t" | |
977 | "bx r0 \n\t" | |
978 | ); | |
979 | } | |
980 | ||
981 | #ifndef CONFIG_THUMB2_KERNEL | |
982 | ||
983 | void __naked __kprobes_test_case_end_32(void) | |
984 | { | |
985 | __asm__ __volatile__ ( | |
986 | "mov r4, lr \n\t" | |
987 | "bl kprobes_test_case_end \n\t" | |
988 | "cmp r0, #0 \n\t" | |
989 | "movne pc, r0 \n\t" | |
990 | "mov r0, r4 \n\t" | |
991 | "add sp, sp, #"__stringify(TEST_MEMORY_SIZE)"\n\t" | |
992 | "ldmia sp!, {r4-r11} \n\t" | |
993 | "mov pc, r0 \n\t" | |
994 | ); | |
995 | } | |
996 | ||
997 | #else /* CONFIG_THUMB2_KERNEL */ | |
998 | ||
999 | void __naked __kprobes_test_case_end_16(void) | |
1000 | { | |
1001 | __asm__ __volatile__ ( | |
1002 | "mov r4, lr \n\t" | |
1003 | "bl kprobes_test_case_end \n\t" | |
1004 | "cmp r0, #0 \n\t" | |
1005 | "bxne r0 \n\t" | |
1006 | "mov r0, r4 \n\t" | |
1007 | "add sp, sp, #"__stringify(TEST_MEMORY_SIZE)"\n\t" | |
1008 | "ldmia sp!, {r4-r11} \n\t" | |
1009 | "bx r0 \n\t" | |
1010 | ); | |
1011 | } | |
1012 | ||
1013 | void __naked __kprobes_test_case_end_32(void) | |
1014 | { | |
1015 | __asm__ __volatile__ ( | |
1016 | ".arm \n\t" | |
1017 | "orr lr, lr, #1 @ will return to Thumb code \n\t" | |
1018 | "ldr pc, 1f \n\t" | |
1019 | "1: \n\t" | |
1020 | ".word __kprobes_test_case_end_16 \n\t" | |
1021 | ); | |
1022 | } | |
1023 | ||
1024 | #endif | |
1025 | ||
1026 | ||
1027 | int kprobe_test_flags; | |
1028 | int kprobe_test_cc_position; | |
1029 | ||
1030 | static int test_try_count; | |
1031 | static int test_pass_count; | |
1032 | static int test_fail_count; | |
1033 | ||
1034 | static struct pt_regs initial_regs; | |
1035 | static struct pt_regs expected_regs; | |
1036 | static struct pt_regs result_regs; | |
1037 | ||
1038 | static u32 expected_memory[TEST_MEMORY_SIZE/sizeof(u32)]; | |
1039 | ||
1040 | static const char *current_title; | |
1041 | static struct test_arg *current_args; | |
1042 | static u32 *current_stack; | |
1043 | static uintptr_t current_branch_target; | |
1044 | ||
1045 | static uintptr_t current_code_start; | |
1046 | static kprobe_opcode_t current_instruction; | |
1047 | ||
1048 | ||
1049 | #define TEST_CASE_PASSED -1 | |
1050 | #define TEST_CASE_FAILED -2 | |
1051 | ||
1052 | static int test_case_run_count; | |
1053 | static bool test_case_is_thumb; | |
1054 | static int test_instance; | |
1055 | ||
1056 | /* | |
1057 | * We ignore the state of the imprecise abort disable flag (CPSR.A) because this | |
1058 | * can change randomly as the kernel doesn't take care to preserve or initialise | |
1059 | * this across context switches. Also, with Security Extentions, the flag may | |
1060 | * not be under control of the kernel; for this reason we ignore the state of | |
1061 | * the FIQ disable flag CPSR.F as well. | |
1062 | */ | |
1063 | #define PSR_IGNORE_BITS (PSR_A_BIT | PSR_F_BIT) | |
1064 | ||
1065 | static unsigned long test_check_cc(int cc, unsigned long cpsr) | |
1066 | { | |
c41584dd | 1067 | int ret = arm_check_condition(cc << 28, cpsr); |
a43bc69b | 1068 | |
c41584dd | 1069 | return (ret != ARM_OPCODE_CONDTEST_FAIL); |
a43bc69b JM |
1070 | } |
1071 | ||
1072 | static int is_last_scenario; | |
1073 | static int probe_should_run; /* 0 = no, 1 = yes, -1 = unknown */ | |
1074 | static int memory_needs_checking; | |
1075 | ||
1076 | static unsigned long test_context_cpsr(int scenario) | |
1077 | { | |
1078 | unsigned long cpsr; | |
1079 | ||
1080 | probe_should_run = 1; | |
1081 | ||
1082 | /* Default case is that we cycle through 16 combinations of flags */ | |
1083 | cpsr = (scenario & 0xf) << 28; /* N,Z,C,V flags */ | |
1084 | cpsr |= (scenario & 0xf) << 16; /* GE flags */ | |
1085 | cpsr |= (scenario & 0x1) << 27; /* Toggle Q flag */ | |
1086 | ||
1087 | if (!test_case_is_thumb) { | |
1088 | /* Testing ARM code */ | |
c41584dd LL |
1089 | int cc = current_instruction >> 28; |
1090 | ||
1091 | probe_should_run = test_check_cc(cc, cpsr) != 0; | |
a43bc69b JM |
1092 | if (scenario == 15) |
1093 | is_last_scenario = true; | |
1094 | ||
1095 | } else if (kprobe_test_flags & TEST_FLAG_NO_ITBLOCK) { | |
1096 | /* Testing Thumb code without setting ITSTATE */ | |
1097 | if (kprobe_test_cc_position) { | |
1098 | int cc = (current_instruction >> kprobe_test_cc_position) & 0xf; | |
1099 | probe_should_run = test_check_cc(cc, cpsr) != 0; | |
1100 | } | |
1101 | ||
1102 | if (scenario == 15) | |
1103 | is_last_scenario = true; | |
1104 | ||
1105 | } else if (kprobe_test_flags & TEST_FLAG_FULL_ITBLOCK) { | |
1106 | /* Testing Thumb code with all combinations of ITSTATE */ | |
1107 | unsigned x = (scenario >> 4); | |
1108 | unsigned cond_base = x % 7; /* ITSTATE<7:5> */ | |
1109 | unsigned mask = x / 7 + 2; /* ITSTATE<4:0>, bits reversed */ | |
1110 | ||
1111 | if (mask > 0x1f) { | |
1112 | /* Finish by testing state from instruction 'itt al' */ | |
1113 | cond_base = 7; | |
1114 | mask = 0x4; | |
1115 | if ((scenario & 0xf) == 0xf) | |
1116 | is_last_scenario = true; | |
1117 | } | |
1118 | ||
1119 | cpsr |= cond_base << 13; /* ITSTATE<7:5> */ | |
1120 | cpsr |= (mask & 0x1) << 12; /* ITSTATE<4> */ | |
1121 | cpsr |= (mask & 0x2) << 10; /* ITSTATE<3> */ | |
1122 | cpsr |= (mask & 0x4) << 8; /* ITSTATE<2> */ | |
1123 | cpsr |= (mask & 0x8) << 23; /* ITSTATE<1> */ | |
1124 | cpsr |= (mask & 0x10) << 21; /* ITSTATE<0> */ | |
1125 | ||
1126 | probe_should_run = test_check_cc((cpsr >> 12) & 0xf, cpsr) != 0; | |
1127 | ||
1128 | } else { | |
1129 | /* Testing Thumb code with several combinations of ITSTATE */ | |
1130 | switch (scenario) { | |
1131 | case 16: /* Clear NZCV flags and 'it eq' state (false as Z=0) */ | |
1132 | cpsr = 0x00000800; | |
1133 | probe_should_run = 0; | |
1134 | break; | |
1135 | case 17: /* Set NZCV flags and 'it vc' state (false as V=1) */ | |
1136 | cpsr = 0xf0007800; | |
1137 | probe_should_run = 0; | |
1138 | break; | |
1139 | case 18: /* Clear NZCV flags and 'it ls' state (true as C=0) */ | |
1140 | cpsr = 0x00009800; | |
1141 | break; | |
1142 | case 19: /* Set NZCV flags and 'it cs' state (true as C=1) */ | |
1143 | cpsr = 0xf0002800; | |
1144 | is_last_scenario = true; | |
1145 | break; | |
1146 | } | |
1147 | } | |
1148 | ||
1149 | return cpsr; | |
1150 | } | |
1151 | ||
1152 | static void setup_test_context(struct pt_regs *regs) | |
1153 | { | |
1154 | int scenario = test_case_run_count>>1; | |
1155 | unsigned long val; | |
1156 | struct test_arg *args; | |
1157 | int i; | |
1158 | ||
1159 | is_last_scenario = false; | |
1160 | memory_needs_checking = false; | |
1161 | ||
1162 | /* Initialise test memory on stack */ | |
1163 | val = (scenario & 1) ? VALM : ~VALM; | |
1164 | for (i = 0; i < TEST_MEMORY_SIZE / sizeof(current_stack[0]); ++i) | |
1165 | current_stack[i] = val + (i << 8); | |
1166 | /* Put target of branch on stack for tests which load PC from memory */ | |
1167 | if (current_branch_target) | |
1168 | current_stack[15] = current_branch_target; | |
1169 | /* Put a value for SP on stack for tests which load SP from memory */ | |
1170 | current_stack[13] = (u32)current_stack + 120; | |
1171 | ||
1172 | /* Initialise register values to their default state */ | |
1173 | val = (scenario & 2) ? VALR : ~VALR; | |
1174 | for (i = 0; i < 13; ++i) | |
1175 | regs->uregs[i] = val ^ (i << 8); | |
1176 | regs->ARM_lr = val ^ (14 << 8); | |
1177 | regs->ARM_cpsr &= ~(APSR_MASK | PSR_IT_MASK); | |
1178 | regs->ARM_cpsr |= test_context_cpsr(scenario); | |
1179 | ||
1180 | /* Perform testcase specific register setup */ | |
1181 | args = current_args; | |
1182 | for (; args[0].type != ARG_TYPE_END; ++args) | |
1183 | switch (args[0].type) { | |
1184 | case ARG_TYPE_REG: { | |
1185 | struct test_arg_regptr *arg = | |
1186 | (struct test_arg_regptr *)args; | |
1187 | regs->uregs[arg->reg] = arg->val; | |
1188 | break; | |
1189 | } | |
1190 | case ARG_TYPE_PTR: { | |
1191 | struct test_arg_regptr *arg = | |
1192 | (struct test_arg_regptr *)args; | |
1193 | regs->uregs[arg->reg] = | |
1194 | (unsigned long)current_stack + arg->val; | |
1195 | memory_needs_checking = true; | |
1196 | break; | |
1197 | } | |
1198 | case ARG_TYPE_MEM: { | |
1199 | struct test_arg_mem *arg = (struct test_arg_mem *)args; | |
1200 | current_stack[arg->index] = arg->val; | |
1201 | break; | |
1202 | } | |
1203 | default: | |
1204 | break; | |
1205 | } | |
1206 | } | |
1207 | ||
1208 | struct test_probe { | |
1209 | struct kprobe kprobe; | |
1210 | bool registered; | |
1211 | int hit; | |
1212 | }; | |
1213 | ||
1214 | static void unregister_test_probe(struct test_probe *probe) | |
1215 | { | |
1216 | if (probe->registered) { | |
1217 | unregister_kprobe(&probe->kprobe); | |
1218 | probe->kprobe.flags = 0; /* Clear disable flag to allow reuse */ | |
1219 | } | |
1220 | probe->registered = false; | |
1221 | } | |
1222 | ||
1223 | static int register_test_probe(struct test_probe *probe) | |
1224 | { | |
1225 | int ret; | |
1226 | ||
1227 | if (probe->registered) | |
1228 | BUG(); | |
1229 | ||
1230 | ret = register_kprobe(&probe->kprobe); | |
1231 | if (ret >= 0) { | |
1232 | probe->registered = true; | |
1233 | probe->hit = -1; | |
1234 | } | |
1235 | return ret; | |
1236 | } | |
1237 | ||
1238 | static int __kprobes | |
1239 | test_before_pre_handler(struct kprobe *p, struct pt_regs *regs) | |
1240 | { | |
1241 | container_of(p, struct test_probe, kprobe)->hit = test_instance; | |
1242 | return 0; | |
1243 | } | |
1244 | ||
1245 | static void __kprobes | |
1246 | test_before_post_handler(struct kprobe *p, struct pt_regs *regs, | |
1247 | unsigned long flags) | |
1248 | { | |
1249 | setup_test_context(regs); | |
1250 | initial_regs = *regs; | |
1251 | initial_regs.ARM_cpsr &= ~PSR_IGNORE_BITS; | |
1252 | } | |
1253 | ||
1254 | static int __kprobes | |
1255 | test_case_pre_handler(struct kprobe *p, struct pt_regs *regs) | |
1256 | { | |
1257 | container_of(p, struct test_probe, kprobe)->hit = test_instance; | |
1258 | return 0; | |
1259 | } | |
1260 | ||
1261 | static int __kprobes | |
1262 | test_after_pre_handler(struct kprobe *p, struct pt_regs *regs) | |
1263 | { | |
1264 | if (container_of(p, struct test_probe, kprobe)->hit == test_instance) | |
1265 | return 0; /* Already run for this test instance */ | |
1266 | ||
1267 | result_regs = *regs; | |
1268 | result_regs.ARM_cpsr &= ~PSR_IGNORE_BITS; | |
1269 | ||
1270 | /* Undo any changes done to SP by the test case */ | |
1271 | regs->ARM_sp = (unsigned long)current_stack; | |
1272 | ||
1273 | container_of(p, struct test_probe, kprobe)->hit = test_instance; | |
1274 | return 0; | |
1275 | } | |
1276 | ||
1277 | static struct test_probe test_before_probe = { | |
1278 | .kprobe.pre_handler = test_before_pre_handler, | |
1279 | .kprobe.post_handler = test_before_post_handler, | |
1280 | }; | |
1281 | ||
1282 | static struct test_probe test_case_probe = { | |
1283 | .kprobe.pre_handler = test_case_pre_handler, | |
1284 | }; | |
1285 | ||
1286 | static struct test_probe test_after_probe = { | |
1287 | .kprobe.pre_handler = test_after_pre_handler, | |
1288 | }; | |
1289 | ||
1290 | static struct test_probe test_after2_probe = { | |
1291 | .kprobe.pre_handler = test_after_pre_handler, | |
1292 | }; | |
1293 | ||
1294 | static void test_case_cleanup(void) | |
1295 | { | |
1296 | unregister_test_probe(&test_before_probe); | |
1297 | unregister_test_probe(&test_case_probe); | |
1298 | unregister_test_probe(&test_after_probe); | |
1299 | unregister_test_probe(&test_after2_probe); | |
1300 | } | |
1301 | ||
1302 | static void print_registers(struct pt_regs *regs) | |
1303 | { | |
1304 | pr_err("r0 %08lx | r1 %08lx | r2 %08lx | r3 %08lx\n", | |
1305 | regs->ARM_r0, regs->ARM_r1, regs->ARM_r2, regs->ARM_r3); | |
1306 | pr_err("r4 %08lx | r5 %08lx | r6 %08lx | r7 %08lx\n", | |
1307 | regs->ARM_r4, regs->ARM_r5, regs->ARM_r6, regs->ARM_r7); | |
1308 | pr_err("r8 %08lx | r9 %08lx | r10 %08lx | r11 %08lx\n", | |
1309 | regs->ARM_r8, regs->ARM_r9, regs->ARM_r10, regs->ARM_fp); | |
1310 | pr_err("r12 %08lx | sp %08lx | lr %08lx | pc %08lx\n", | |
1311 | regs->ARM_ip, regs->ARM_sp, regs->ARM_lr, regs->ARM_pc); | |
1312 | pr_err("cpsr %08lx\n", regs->ARM_cpsr); | |
1313 | } | |
1314 | ||
1315 | static void print_memory(u32 *mem, size_t size) | |
1316 | { | |
1317 | int i; | |
1318 | for (i = 0; i < size / sizeof(u32); i += 4) | |
1319 | pr_err("%08x %08x %08x %08x\n", mem[i], mem[i+1], | |
1320 | mem[i+2], mem[i+3]); | |
1321 | } | |
1322 | ||
1323 | static size_t expected_memory_size(u32 *sp) | |
1324 | { | |
1325 | size_t size = sizeof(expected_memory); | |
1326 | int offset = (uintptr_t)sp - (uintptr_t)current_stack; | |
1327 | if (offset > 0) | |
1328 | size -= offset; | |
1329 | return size; | |
1330 | } | |
1331 | ||
1332 | static void test_case_failed(const char *message) | |
1333 | { | |
1334 | test_case_cleanup(); | |
1335 | ||
1336 | pr_err("FAIL: %s\n", message); | |
1337 | pr_err("FAIL: Test %s\n", current_title); | |
1338 | pr_err("FAIL: Scenario %d\n", test_case_run_count >> 1); | |
1339 | } | |
1340 | ||
1341 | static unsigned long next_instruction(unsigned long pc) | |
1342 | { | |
1343 | #ifdef CONFIG_THUMB2_KERNEL | |
4712e17a BD |
1344 | if ((pc & 1) && |
1345 | !is_wide_instruction(__mem_to_opcode_thumb16(*(u16 *)(pc - 1)))) | |
a43bc69b JM |
1346 | return pc + 2; |
1347 | else | |
1348 | #endif | |
1349 | return pc + 4; | |
1350 | } | |
1351 | ||
1352 | static uintptr_t __used kprobes_test_case_start(const char *title, void *stack) | |
1353 | { | |
1354 | struct test_arg *args; | |
1355 | struct test_arg_end *end_arg; | |
1356 | unsigned long test_code; | |
1357 | ||
1358 | args = (struct test_arg *)PTR_ALIGN(title + strlen(title) + 1, 4); | |
1359 | ||
1360 | current_title = title; | |
1361 | current_args = args; | |
1362 | current_stack = stack; | |
1363 | ||
1364 | ++test_try_count; | |
1365 | ||
1366 | while (args->type != ARG_TYPE_END) | |
1367 | ++args; | |
1368 | end_arg = (struct test_arg_end *)args; | |
1369 | ||
1370 | test_code = (unsigned long)(args + 1); /* Code starts after args */ | |
1371 | ||
1372 | test_case_is_thumb = end_arg->flags & ARG_FLAG_THUMB; | |
1373 | if (test_case_is_thumb) | |
1374 | test_code |= 1; | |
1375 | ||
1376 | current_code_start = test_code; | |
1377 | ||
1378 | current_branch_target = 0; | |
1379 | if (end_arg->branch_offset != end_arg->end_offset) | |
1380 | current_branch_target = test_code + end_arg->branch_offset; | |
1381 | ||
1382 | test_code += end_arg->code_offset; | |
1383 | test_before_probe.kprobe.addr = (kprobe_opcode_t *)test_code; | |
1384 | ||
1385 | test_code = next_instruction(test_code); | |
1386 | test_case_probe.kprobe.addr = (kprobe_opcode_t *)test_code; | |
1387 | ||
1388 | if (test_case_is_thumb) { | |
1389 | u16 *p = (u16 *)(test_code & ~1); | |
4712e17a | 1390 | current_instruction = __mem_to_opcode_thumb16(p[0]); |
a43bc69b | 1391 | if (is_wide_instruction(current_instruction)) { |
4712e17a BD |
1392 | u16 instr2 = __mem_to_opcode_thumb16(p[1]); |
1393 | current_instruction = __opcode_thumb32_compose(current_instruction, instr2); | |
a43bc69b JM |
1394 | } |
1395 | } else { | |
4712e17a | 1396 | current_instruction = __mem_to_opcode_arm(*(u32 *)test_code); |
a43bc69b JM |
1397 | } |
1398 | ||
1399 | if (current_title[0] == '.') | |
1400 | verbose("%s\n", current_title); | |
1401 | else | |
1402 | verbose("%s\t@ %0*x\n", current_title, | |
1403 | test_case_is_thumb ? 4 : 8, | |
1404 | current_instruction); | |
1405 | ||
1406 | test_code = next_instruction(test_code); | |
1407 | test_after_probe.kprobe.addr = (kprobe_opcode_t *)test_code; | |
1408 | ||
1409 | if (kprobe_test_flags & TEST_FLAG_NARROW_INSTR) { | |
1410 | if (!test_case_is_thumb || | |
1411 | is_wide_instruction(current_instruction)) { | |
1412 | test_case_failed("expected 16-bit instruction"); | |
1413 | goto fail; | |
1414 | } | |
1415 | } else { | |
1416 | if (test_case_is_thumb && | |
1417 | !is_wide_instruction(current_instruction)) { | |
1418 | test_case_failed("expected 32-bit instruction"); | |
1419 | goto fail; | |
1420 | } | |
1421 | } | |
1422 | ||
963780df JM |
1423 | coverage_add(current_instruction); |
1424 | ||
a43bc69b JM |
1425 | if (end_arg->flags & ARG_FLAG_UNSUPPORTED) { |
1426 | if (register_test_probe(&test_case_probe) < 0) | |
1427 | goto pass; | |
1428 | test_case_failed("registered probe for unsupported instruction"); | |
1429 | goto fail; | |
1430 | } | |
1431 | ||
1432 | if (end_arg->flags & ARG_FLAG_SUPPORTED) { | |
1433 | if (register_test_probe(&test_case_probe) >= 0) | |
1434 | goto pass; | |
1435 | test_case_failed("couldn't register probe for supported instruction"); | |
1436 | goto fail; | |
1437 | } | |
1438 | ||
1439 | if (register_test_probe(&test_before_probe) < 0) { | |
1440 | test_case_failed("register test_before_probe failed"); | |
1441 | goto fail; | |
1442 | } | |
1443 | if (register_test_probe(&test_after_probe) < 0) { | |
1444 | test_case_failed("register test_after_probe failed"); | |
1445 | goto fail; | |
1446 | } | |
1447 | if (current_branch_target) { | |
1448 | test_after2_probe.kprobe.addr = | |
1449 | (kprobe_opcode_t *)current_branch_target; | |
1450 | if (register_test_probe(&test_after2_probe) < 0) { | |
1451 | test_case_failed("register test_after2_probe failed"); | |
1452 | goto fail; | |
1453 | } | |
1454 | } | |
1455 | ||
1456 | /* Start first run of test case */ | |
1457 | test_case_run_count = 0; | |
1458 | ++test_instance; | |
1459 | return current_code_start; | |
1460 | pass: | |
1461 | test_case_run_count = TEST_CASE_PASSED; | |
1462 | return (uintptr_t)test_after_probe.kprobe.addr; | |
1463 | fail: | |
1464 | test_case_run_count = TEST_CASE_FAILED; | |
1465 | return (uintptr_t)test_after_probe.kprobe.addr; | |
1466 | } | |
1467 | ||
1468 | static bool check_test_results(void) | |
1469 | { | |
1470 | size_t mem_size = 0; | |
1471 | u32 *mem = 0; | |
1472 | ||
1473 | if (memcmp(&expected_regs, &result_regs, sizeof(expected_regs))) { | |
1474 | test_case_failed("registers differ"); | |
1475 | goto fail; | |
1476 | } | |
1477 | ||
1478 | if (memory_needs_checking) { | |
1479 | mem = (u32 *)result_regs.ARM_sp; | |
1480 | mem_size = expected_memory_size(mem); | |
1481 | if (memcmp(expected_memory, mem, mem_size)) { | |
1482 | test_case_failed("test memory differs"); | |
1483 | goto fail; | |
1484 | } | |
1485 | } | |
1486 | ||
1487 | return true; | |
1488 | ||
1489 | fail: | |
1490 | pr_err("initial_regs:\n"); | |
1491 | print_registers(&initial_regs); | |
1492 | pr_err("expected_regs:\n"); | |
1493 | print_registers(&expected_regs); | |
1494 | pr_err("result_regs:\n"); | |
1495 | print_registers(&result_regs); | |
1496 | ||
1497 | if (mem) { | |
1498 | pr_err("current_stack=%p\n", current_stack); | |
1499 | pr_err("expected_memory:\n"); | |
1500 | print_memory(expected_memory, mem_size); | |
1501 | pr_err("result_memory:\n"); | |
1502 | print_memory(mem, mem_size); | |
1503 | } | |
1504 | ||
1505 | return false; | |
1506 | } | |
1507 | ||
1508 | static uintptr_t __used kprobes_test_case_end(void) | |
1509 | { | |
1510 | if (test_case_run_count < 0) { | |
1511 | if (test_case_run_count == TEST_CASE_PASSED) | |
1512 | /* kprobes_test_case_start did all the needed testing */ | |
1513 | goto pass; | |
1514 | else | |
1515 | /* kprobes_test_case_start failed */ | |
1516 | goto fail; | |
1517 | } | |
1518 | ||
1519 | if (test_before_probe.hit != test_instance) { | |
1520 | test_case_failed("test_before_handler not run"); | |
1521 | goto fail; | |
1522 | } | |
1523 | ||
1524 | if (test_after_probe.hit != test_instance && | |
1525 | test_after2_probe.hit != test_instance) { | |
1526 | test_case_failed("test_after_handler not run"); | |
1527 | goto fail; | |
1528 | } | |
1529 | ||
1530 | /* | |
1531 | * Even numbered test runs ran without a probe on the test case so | |
1532 | * we can gather reference results. The subsequent odd numbered run | |
1533 | * will have the probe inserted. | |
1534 | */ | |
1535 | if ((test_case_run_count & 1) == 0) { | |
1536 | /* Save results from run without probe */ | |
1537 | u32 *mem = (u32 *)result_regs.ARM_sp; | |
1538 | expected_regs = result_regs; | |
1539 | memcpy(expected_memory, mem, expected_memory_size(mem)); | |
1540 | ||
1541 | /* Insert probe onto test case instruction */ | |
1542 | if (register_test_probe(&test_case_probe) < 0) { | |
1543 | test_case_failed("register test_case_probe failed"); | |
1544 | goto fail; | |
1545 | } | |
1546 | } else { | |
1547 | /* Check probe ran as expected */ | |
1548 | if (probe_should_run == 1) { | |
1549 | if (test_case_probe.hit != test_instance) { | |
1550 | test_case_failed("test_case_handler not run"); | |
1551 | goto fail; | |
1552 | } | |
1553 | } else if (probe_should_run == 0) { | |
1554 | if (test_case_probe.hit == test_instance) { | |
1555 | test_case_failed("test_case_handler ran"); | |
1556 | goto fail; | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | /* Remove probe for any subsequent reference run */ | |
1561 | unregister_test_probe(&test_case_probe); | |
1562 | ||
1563 | if (!check_test_results()) | |
1564 | goto fail; | |
1565 | ||
1566 | if (is_last_scenario) | |
1567 | goto pass; | |
1568 | } | |
1569 | ||
1570 | /* Do next test run */ | |
1571 | ++test_case_run_count; | |
1572 | ++test_instance; | |
1573 | return current_code_start; | |
1574 | fail: | |
1575 | ++test_fail_count; | |
1576 | goto end; | |
1577 | pass: | |
1578 | ++test_pass_count; | |
1579 | end: | |
1580 | test_case_cleanup(); | |
1581 | return 0; | |
1582 | } | |
1583 | ||
1584 | ||
9eed1797 JM |
1585 | /* |
1586 | * Top level test functions | |
1587 | */ | |
1588 | ||
68f360e7 | 1589 | static int run_test_cases(void (*tests)(void), const union decode_item *table) |
c7054aad | 1590 | { |
68f360e7 JM |
1591 | int ret; |
1592 | ||
1593 | pr_info(" Check decoding tables\n"); | |
1594 | ret = table_test(table); | |
1595 | if (ret) | |
1596 | return ret; | |
1597 | ||
c7054aad | 1598 | pr_info(" Run test cases\n"); |
963780df JM |
1599 | ret = coverage_start(table); |
1600 | if (ret) | |
1601 | return ret; | |
1602 | ||
c7054aad JM |
1603 | tests(); |
1604 | ||
963780df | 1605 | coverage_end(); |
c7054aad JM |
1606 | return 0; |
1607 | } | |
1608 | ||
1609 | ||
9eed1797 JM |
1610 | static int __init run_all_tests(void) |
1611 | { | |
1612 | int ret = 0; | |
1613 | ||
744627e9 | 1614 | pr_info("Beginning kprobe tests...\n"); |
9eed1797 JM |
1615 | |
1616 | #ifndef CONFIG_THUMB2_KERNEL | |
1617 | ||
1618 | pr_info("Probe ARM code\n"); | |
1619 | ret = run_api_tests(arm_func); | |
1620 | if (ret) | |
1621 | goto out; | |
1622 | ||
c0cc6df1 | 1623 | pr_info("ARM instruction simulation\n"); |
47e190fa | 1624 | ret = run_test_cases(kprobe_arm_test_cases, probes_decode_arm_table); |
c0cc6df1 JM |
1625 | if (ret) |
1626 | goto out; | |
1627 | ||
9eed1797 JM |
1628 | #else /* CONFIG_THUMB2_KERNEL */ |
1629 | ||
1630 | pr_info("Probe 16-bit Thumb code\n"); | |
1631 | ret = run_api_tests(thumb16_func); | |
1632 | if (ret) | |
1633 | goto out; | |
1634 | ||
1635 | pr_info("Probe 32-bit Thumb code, even halfword\n"); | |
1636 | ret = run_api_tests(thumb32even_func); | |
1637 | if (ret) | |
1638 | goto out; | |
1639 | ||
1640 | pr_info("Probe 32-bit Thumb code, odd halfword\n"); | |
1641 | ret = run_api_tests(thumb32odd_func); | |
1642 | if (ret) | |
1643 | goto out; | |
1644 | ||
c7054aad | 1645 | pr_info("16-bit Thumb instruction simulation\n"); |
68f360e7 | 1646 | ret = run_test_cases(kprobe_thumb16_test_cases, |
47e190fa | 1647 | probes_decode_thumb16_table); |
c7054aad JM |
1648 | if (ret) |
1649 | goto out; | |
1650 | ||
1651 | pr_info("32-bit Thumb instruction simulation\n"); | |
68f360e7 | 1652 | ret = run_test_cases(kprobe_thumb32_test_cases, |
47e190fa | 1653 | probes_decode_thumb32_table); |
c7054aad JM |
1654 | if (ret) |
1655 | goto out; | |
9eed1797 JM |
1656 | #endif |
1657 | ||
c7054aad JM |
1658 | pr_info("Total instruction simulation tests=%d, pass=%d fail=%d\n", |
1659 | test_try_count, test_pass_count, test_fail_count); | |
1660 | if (test_fail_count) { | |
1661 | ret = -EINVAL; | |
1662 | goto out; | |
1663 | } | |
1664 | ||
ce5af3ba JM |
1665 | #if BENCHMARKING |
1666 | pr_info("Benchmarks\n"); | |
1667 | ret = run_benchmarks(); | |
1668 | if (ret) | |
1669 | goto out; | |
1670 | #endif | |
1671 | ||
963780df JM |
1672 | #if __LINUX_ARM_ARCH__ >= 7 |
1673 | /* We are able to run all test cases so coverage should be complete */ | |
1674 | if (coverage_fail) { | |
1675 | pr_err("FAIL: Test coverage checks failed\n"); | |
1676 | ret = -EINVAL; | |
1677 | goto out; | |
1678 | } | |
1679 | #endif | |
1680 | ||
9eed1797 | 1681 | out: |
48f7bc86 JM |
1682 | if (ret == 0) |
1683 | ret = tests_failed; | |
9eed1797 JM |
1684 | if (ret == 0) |
1685 | pr_info("Finished kprobe tests OK\n"); | |
1686 | else | |
1687 | pr_err("kprobe tests failed\n"); | |
1688 | ||
1689 | return ret; | |
1690 | } | |
1691 | ||
1692 | ||
1693 | /* | |
1694 | * Module setup | |
1695 | */ | |
1696 | ||
1697 | #ifdef MODULE | |
1698 | ||
1699 | static void __exit kprobe_test_exit(void) | |
1700 | { | |
1701 | } | |
1702 | ||
1703 | module_init(run_all_tests) | |
1704 | module_exit(kprobe_test_exit) | |
1705 | MODULE_LICENSE("GPL"); | |
1706 | ||
1707 | #else /* !MODULE */ | |
1708 | ||
1709 | late_initcall(run_all_tests); | |
1710 | ||
1711 | #endif |