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