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