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