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