2 * arch/arm/kernel/kprobes-test.c
4 * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
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.
15 * The methodology used to test an ARM instruction 'test_insn' is to use
16 * inline assembler like:
19 * test_case: test_insn
22 * When the test case is run a kprobe is placed of each nop. The
23 * post-handler of the test_before probe is used to modify the saved CPU
24 * register context to that which we require for the test case. The
25 * pre-handler of the of the test_after probe saves a copy of the CPU
26 * register context. In this way we can execute test_insn with a specific
27 * register context and see the results afterwards.
29 * To actually test the kprobes instruction emulation we perform the above
30 * step a second time but with an additional kprobe on the test_case
31 * instruction itself. If the emulation is accurate then the results seen
32 * by the test_after probe will be identical to the first run which didn't
33 * have a probe on test_case.
35 * Each test case is run several times with a variety of variations in the
36 * flags value of stored in CPSR, and for Thumb code, different ITState.
38 * For instructions which can modify PC, a second test_after probe is used
42 * test_case: test_insn
48 * The test case is constructed such that test_insn branches to
49 * test_after2, or, if testing a conditional instruction, it may just
50 * continue to test_after. The probes inserted at both locations let us
51 * determine which happened. A similar approach is used for testing
52 * backwards branches...
55 * b test_done @ helps to cope with off by 1 branches
59 * test_case: test_insn
63 * The macros used to generate the assembler instructions describe above
64 * are TEST_INSTRUCTION, TEST_BRANCH_F (branch forwards) and TEST_BRANCH_B
65 * (branch backwards). In these, the local variables numbered 1, 50, 2 and
66 * 99 represent: test_before, test_case, test_after2 and test_done.
71 * Each test case is wrapped between the pair of macros TESTCASE_START and
72 * TESTCASE_END. As well as performing the inline assembler boilerplate,
73 * these call out to the kprobes_test_case_start() and
74 * kprobes_test_case_end() functions which drive the execution of the test
75 * case. The specific arguments to use for each test case are stored as
76 * inline data constructed using the various TEST_ARG_* macros. Putting
77 * this all together, a simple test case may look like:
79 * TESTCASE_START("Testing mov r0, r7")
80 * TEST_ARG_REG(7, 0x12345678) // Set r7=0x12345678
82 * TEST_INSTRUCTION("mov r0, r7")
85 * Note, in practice the single convenience macro TEST_R would be used for this
88 * The above would expand to assembler looking something like:
91 * bl __kprobes_test_case_start
92 * @ start of inline data...
93 * .ascii "mov r0, r7" @ text title for test case
105 * .byte TEST_ISA @ flags, including ISA being tested
106 * .short 50f-0f @ offset of 'test_before'
107 * .short 2f-0f @ offset of 'test_after2' (if relevent)
108 * .short 99f-0f @ offset of 'test_done'
109 * @ start of test case code...
111 * .code TEST_ISA @ switch to ISA being tested
114 * 50: nop @ location for 'test_before' probe
115 * 1: mov r0, r7 @ the test case instruction 'test_insn'
116 * nop @ location for 'test_after' probe
120 * 99: bl __kprobes_test_case_end_##TEST_ISA
123 * When the above is execute the following happens...
125 * __kprobes_test_case_start() is an assembler wrapper which sets up space
126 * for a stack buffer and calls the C function kprobes_test_case_start().
127 * This C function will do some initial processing of the inline data and
128 * setup some global state. It then inserts the test_before and test_after
129 * kprobes and returns a value which causes the assembler wrapper to jump
130 * to the start of the test case code, (local label '0').
132 * When the test case code executes, the test_before probe will be hit and
133 * test_before_post_handler will call setup_test_context(). This fills the
134 * stack buffer and CPU registers with a test pattern and then processes
135 * the test case arguments. In our example there is one TEST_ARG_REG which
136 * indicates that R7 should be loaded with the value 0x12345678.
138 * When the test_before probe ends, the test case continues and executes
139 * the "mov r0, r7" instruction. It then hits the test_after probe and the
140 * pre-handler for this (test_after_pre_handler) will save a copy of the
141 * CPU register context. This should now have R0 holding the same value as
144 * Finally we get to the call to __kprobes_test_case_end_{32,16}. This is
145 * an assembler wrapper which switches back to the ISA used by the test
146 * code and calls the C function kprobes_test_case_end().
148 * For each run through the test case, test_case_run_count is incremented
149 * by one. For even runs, kprobes_test_case_end() saves a copy of the
150 * register and stack buffer contents from the test case just run. It then
151 * inserts a kprobe on the test case instruction 'test_insn' and returns a
152 * value to cause the test case code to be re-run.
154 * For odd numbered runs, kprobes_test_case_end() compares the register and
155 * stack buffer contents to those that were saved on the previous even
156 * numbered run (the one without the kprobe on test_insn). These should be
157 * the same if the kprobe instruction simulation routine is correct.
159 * The pair of test case runs is repeated with different combinations of
160 * flag values in CPSR and, for Thumb, different ITState. This is
161 * controlled by test_context_cpsr().
163 * BUILDING TEST CASES
164 * -------------------
167 * As an aid to building test cases, the stack buffer is initialised with
168 * some special values:
170 * [SP+13*4] Contains SP+120. This can be used to test instructions
171 * which load a value into SP.
173 * [SP+15*4] When testing branching instructions using TEST_BRANCH_{F,B},
174 * this holds the target address of the branch, 'test_after2'.
175 * This can be used to test instructions which load a PC value
179 #include <linux/kernel.h>
180 #include <linux/module.h>
181 #include <linux/kprobes.h>
184 #include "kprobes-test.h"
191 static bool test_regs_ok
;
192 static int test_func_instance
;
193 static int pre_handler_called
;
194 static int post_handler_called
;
195 static int jprobe_func_called
;
196 static int kretprobe_handler_called
;
198 #define FUNC_ARG1 0x12345678
199 #define FUNC_ARG2 0xabcdef
202 #ifndef CONFIG_THUMB2_KERNEL
204 long arm_func(long r0
, long r1
);
206 static void __used __naked
__arm_kprobes_test_func(void)
208 __asm__
__volatile__ (
210 ".type arm_func, %%function \n\t"
212 "adds r0, r0, r1 \n\t"
214 ".code "NORMAL_ISA
/* Back to Thumb if necessary */
215 : : : "r0", "r1", "cc"
219 #else /* CONFIG_THUMB2_KERNEL */
221 long thumb16_func(long r0
, long r1
);
222 long thumb32even_func(long r0
, long r1
);
223 long thumb32odd_func(long r0
, long r1
);
225 static void __used __naked
__thumb_kprobes_test_funcs(void)
227 __asm__
__volatile__ (
228 ".type thumb16_func, %%function \n\t"
230 "adds.n r0, r0, r1 \n\t"
234 ".type thumb32even_func, %%function \n\t"
235 "thumb32even_func: \n\t"
236 "adds.w r0, r0, r1 \n\t"
241 ".type thumb32odd_func, %%function \n\t"
242 "thumb32odd_func: \n\t"
243 "adds.w r0, r0, r1 \n\t"
246 : : : "r0", "r1", "cc"
250 #endif /* CONFIG_THUMB2_KERNEL */
253 static int call_test_func(long (*func
)(long, long), bool check_test_regs
)
257 ++test_func_instance
;
258 test_regs_ok
= false;
260 ret
= (*func
)(FUNC_ARG1
, FUNC_ARG2
);
261 if (ret
!= FUNC_ARG1
+ FUNC_ARG2
) {
262 pr_err("FAIL: call_test_func: func returned %lx\n", ret
);
266 if (check_test_regs
&& !test_regs_ok
) {
267 pr_err("FAIL: test regs not OK\n");
274 static int __kprobes
pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
276 pre_handler_called
= test_func_instance
;
277 if (regs
->ARM_r0
== FUNC_ARG1
&& regs
->ARM_r1
== FUNC_ARG2
)
282 static void __kprobes
post_handler(struct kprobe
*p
, struct pt_regs
*regs
,
285 post_handler_called
= test_func_instance
;
286 if (regs
->ARM_r0
!= FUNC_ARG1
+ FUNC_ARG2
|| regs
->ARM_r1
!= FUNC_ARG2
)
287 test_regs_ok
= false;
290 static struct kprobe the_kprobe
= {
292 .pre_handler
= pre_handler
,
293 .post_handler
= post_handler
296 static int test_kprobe(long (*func
)(long, long))
300 the_kprobe
.addr
= (kprobe_opcode_t
*)func
;
301 ret
= register_kprobe(&the_kprobe
);
303 pr_err("FAIL: register_kprobe failed with %d\n", ret
);
307 ret
= call_test_func(func
, true);
309 unregister_kprobe(&the_kprobe
);
310 the_kprobe
.flags
= 0; /* Clear disable flag to allow reuse */
314 if (pre_handler_called
!= test_func_instance
) {
315 pr_err("FAIL: kprobe pre_handler not called\n");
318 if (post_handler_called
!= test_func_instance
) {
319 pr_err("FAIL: kprobe post_handler not called\n");
322 if (!call_test_func(func
, false))
324 if (pre_handler_called
== test_func_instance
||
325 post_handler_called
== test_func_instance
) {
326 pr_err("FAIL: probe called after unregistering\n");
333 static void __kprobes
jprobe_func(long r0
, long r1
)
335 jprobe_func_called
= test_func_instance
;
336 if (r0
== FUNC_ARG1
&& r1
== FUNC_ARG2
)
341 static struct jprobe the_jprobe
= {
342 .entry
= jprobe_func
,
345 static int test_jprobe(long (*func
)(long, long))
349 the_jprobe
.kp
.addr
= (kprobe_opcode_t
*)func
;
350 ret
= register_jprobe(&the_jprobe
);
352 pr_err("FAIL: register_jprobe failed with %d\n", ret
);
356 ret
= call_test_func(func
, true);
358 unregister_jprobe(&the_jprobe
);
359 the_jprobe
.kp
.flags
= 0; /* Clear disable flag to allow reuse */
363 if (jprobe_func_called
!= test_func_instance
) {
364 pr_err("FAIL: jprobe handler function not called\n");
367 if (!call_test_func(func
, false))
369 if (jprobe_func_called
== test_func_instance
) {
370 pr_err("FAIL: probe called after unregistering\n");
378 kretprobe_handler(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
380 kretprobe_handler_called
= test_func_instance
;
381 if (regs_return_value(regs
) == FUNC_ARG1
+ FUNC_ARG2
)
386 static struct kretprobe the_kretprobe
= {
387 .handler
= kretprobe_handler
,
390 static int test_kretprobe(long (*func
)(long, long))
394 the_kretprobe
.kp
.addr
= (kprobe_opcode_t
*)func
;
395 ret
= register_kretprobe(&the_kretprobe
);
397 pr_err("FAIL: register_kretprobe failed with %d\n", ret
);
401 ret
= call_test_func(func
, true);
403 unregister_kretprobe(&the_kretprobe
);
404 the_kretprobe
.kp
.flags
= 0; /* Clear disable flag to allow reuse */
408 if (kretprobe_handler_called
!= test_func_instance
) {
409 pr_err("FAIL: kretprobe handler not called\n");
412 if (!call_test_func(func
, false))
414 if (jprobe_func_called
== test_func_instance
) {
415 pr_err("FAIL: kretprobe called after unregistering\n");
422 static int run_api_tests(long (*func
)(long, long))
426 pr_info(" kprobe\n");
427 ret
= test_kprobe(func
);
431 pr_info(" jprobe\n");
432 ret
= test_jprobe(func
);
436 pr_info(" kretprobe\n");
437 ret
= test_kretprobe(func
);
446 * Framework for instruction set test cases
449 void __naked
__kprobes_test_case_start(void)
451 __asm__
__volatile__ (
452 "stmdb sp!, {r4-r11} \n\t"
453 "sub sp, sp, #"__stringify(TEST_MEMORY_SIZE
)"\n\t"
454 "bic r0, lr, #1 @ r0 = inline title string \n\t"
456 "bl kprobes_test_case_start \n\t"
461 #ifndef CONFIG_THUMB2_KERNEL
463 void __naked
__kprobes_test_case_end_32(void)
465 __asm__
__volatile__ (
467 "bl kprobes_test_case_end \n\t"
471 "add sp, sp, #"__stringify(TEST_MEMORY_SIZE
)"\n\t"
472 "ldmia sp!, {r4-r11} \n\t"
477 #else /* CONFIG_THUMB2_KERNEL */
479 void __naked
__kprobes_test_case_end_16(void)
481 __asm__
__volatile__ (
483 "bl kprobes_test_case_end \n\t"
487 "add sp, sp, #"__stringify(TEST_MEMORY_SIZE
)"\n\t"
488 "ldmia sp!, {r4-r11} \n\t"
493 void __naked
__kprobes_test_case_end_32(void)
495 __asm__
__volatile__ (
497 "orr lr, lr, #1 @ will return to Thumb code \n\t"
500 ".word __kprobes_test_case_end_16 \n\t"
507 int kprobe_test_flags
;
508 int kprobe_test_cc_position
;
510 static int test_try_count
;
511 static int test_pass_count
;
512 static int test_fail_count
;
514 static struct pt_regs initial_regs
;
515 static struct pt_regs expected_regs
;
516 static struct pt_regs result_regs
;
518 static u32 expected_memory
[TEST_MEMORY_SIZE
/sizeof(u32
)];
520 static const char *current_title
;
521 static struct test_arg
*current_args
;
522 static u32
*current_stack
;
523 static uintptr_t current_branch_target
;
525 static uintptr_t current_code_start
;
526 static kprobe_opcode_t current_instruction
;
529 #define TEST_CASE_PASSED -1
530 #define TEST_CASE_FAILED -2
532 static int test_case_run_count
;
533 static bool test_case_is_thumb
;
534 static int test_instance
;
537 * We ignore the state of the imprecise abort disable flag (CPSR.A) because this
538 * can change randomly as the kernel doesn't take care to preserve or initialise
539 * this across context switches. Also, with Security Extentions, the flag may
540 * not be under control of the kernel; for this reason we ignore the state of
541 * the FIQ disable flag CPSR.F as well.
543 #define PSR_IGNORE_BITS (PSR_A_BIT | PSR_F_BIT)
545 static unsigned long test_check_cc(int cc
, unsigned long cpsr
)
551 return cpsr
& PSR_Z_BIT
;
554 return (~cpsr
) & PSR_Z_BIT
;
557 return cpsr
& PSR_C_BIT
;
560 return (~cpsr
) & PSR_C_BIT
;
563 return cpsr
& PSR_N_BIT
;
566 return (~cpsr
) & PSR_N_BIT
;
569 return cpsr
& PSR_V_BIT
;
572 return (~cpsr
) & PSR_V_BIT
;
575 cpsr
&= ~(cpsr
>> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
576 return cpsr
& PSR_C_BIT
;
579 cpsr
&= ~(cpsr
>> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
580 return (~cpsr
) & PSR_C_BIT
;
583 cpsr
^= (cpsr
<< 3); /* PSR_N_BIT ^= PSR_V_BIT */
584 return (~cpsr
) & PSR_N_BIT
;
587 cpsr
^= (cpsr
<< 3); /* PSR_N_BIT ^= PSR_V_BIT */
588 return cpsr
& PSR_N_BIT
;
591 temp
= cpsr
^ (cpsr
<< 3); /* PSR_N_BIT ^= PSR_V_BIT */
592 temp
|= (cpsr
<< 1); /* PSR_N_BIT |= PSR_Z_BIT */
593 return (~temp
) & PSR_N_BIT
;
596 temp
= cpsr
^ (cpsr
<< 3); /* PSR_N_BIT ^= PSR_V_BIT */
597 temp
|= (cpsr
<< 1); /* PSR_N_BIT |= PSR_Z_BIT */
598 return temp
& PSR_N_BIT
;
601 case 0xf: /* unconditional */
608 static int is_last_scenario
;
609 static int probe_should_run
; /* 0 = no, 1 = yes, -1 = unknown */
610 static int memory_needs_checking
;
612 static unsigned long test_context_cpsr(int scenario
)
616 probe_should_run
= 1;
618 /* Default case is that we cycle through 16 combinations of flags */
619 cpsr
= (scenario
& 0xf) << 28; /* N,Z,C,V flags */
620 cpsr
|= (scenario
& 0xf) << 16; /* GE flags */
621 cpsr
|= (scenario
& 0x1) << 27; /* Toggle Q flag */
623 if (!test_case_is_thumb
) {
624 /* Testing ARM code */
625 probe_should_run
= test_check_cc(current_instruction
>> 28, cpsr
) != 0;
627 is_last_scenario
= true;
629 } else if (kprobe_test_flags
& TEST_FLAG_NO_ITBLOCK
) {
630 /* Testing Thumb code without setting ITSTATE */
631 if (kprobe_test_cc_position
) {
632 int cc
= (current_instruction
>> kprobe_test_cc_position
) & 0xf;
633 probe_should_run
= test_check_cc(cc
, cpsr
) != 0;
637 is_last_scenario
= true;
639 } else if (kprobe_test_flags
& TEST_FLAG_FULL_ITBLOCK
) {
640 /* Testing Thumb code with all combinations of ITSTATE */
641 unsigned x
= (scenario
>> 4);
642 unsigned cond_base
= x
% 7; /* ITSTATE<7:5> */
643 unsigned mask
= x
/ 7 + 2; /* ITSTATE<4:0>, bits reversed */
646 /* Finish by testing state from instruction 'itt al' */
649 if ((scenario
& 0xf) == 0xf)
650 is_last_scenario
= true;
653 cpsr
|= cond_base
<< 13; /* ITSTATE<7:5> */
654 cpsr
|= (mask
& 0x1) << 12; /* ITSTATE<4> */
655 cpsr
|= (mask
& 0x2) << 10; /* ITSTATE<3> */
656 cpsr
|= (mask
& 0x4) << 8; /* ITSTATE<2> */
657 cpsr
|= (mask
& 0x8) << 23; /* ITSTATE<1> */
658 cpsr
|= (mask
& 0x10) << 21; /* ITSTATE<0> */
660 probe_should_run
= test_check_cc((cpsr
>> 12) & 0xf, cpsr
) != 0;
663 /* Testing Thumb code with several combinations of ITSTATE */
665 case 16: /* Clear NZCV flags and 'it eq' state (false as Z=0) */
667 probe_should_run
= 0;
669 case 17: /* Set NZCV flags and 'it vc' state (false as V=1) */
671 probe_should_run
= 0;
673 case 18: /* Clear NZCV flags and 'it ls' state (true as C=0) */
676 case 19: /* Set NZCV flags and 'it cs' state (true as C=1) */
678 is_last_scenario
= true;
686 static void setup_test_context(struct pt_regs
*regs
)
688 int scenario
= test_case_run_count
>>1;
690 struct test_arg
*args
;
693 is_last_scenario
= false;
694 memory_needs_checking
= false;
696 /* Initialise test memory on stack */
697 val
= (scenario
& 1) ? VALM
: ~VALM
;
698 for (i
= 0; i
< TEST_MEMORY_SIZE
/ sizeof(current_stack
[0]); ++i
)
699 current_stack
[i
] = val
+ (i
<< 8);
700 /* Put target of branch on stack for tests which load PC from memory */
701 if (current_branch_target
)
702 current_stack
[15] = current_branch_target
;
703 /* Put a value for SP on stack for tests which load SP from memory */
704 current_stack
[13] = (u32
)current_stack
+ 120;
706 /* Initialise register values to their default state */
707 val
= (scenario
& 2) ? VALR
: ~VALR
;
708 for (i
= 0; i
< 13; ++i
)
709 regs
->uregs
[i
] = val
^ (i
<< 8);
710 regs
->ARM_lr
= val
^ (14 << 8);
711 regs
->ARM_cpsr
&= ~(APSR_MASK
| PSR_IT_MASK
);
712 regs
->ARM_cpsr
|= test_context_cpsr(scenario
);
714 /* Perform testcase specific register setup */
716 for (; args
[0].type
!= ARG_TYPE_END
; ++args
)
717 switch (args
[0].type
) {
719 struct test_arg_regptr
*arg
=
720 (struct test_arg_regptr
*)args
;
721 regs
->uregs
[arg
->reg
] = arg
->val
;
725 struct test_arg_regptr
*arg
=
726 (struct test_arg_regptr
*)args
;
727 regs
->uregs
[arg
->reg
] =
728 (unsigned long)current_stack
+ arg
->val
;
729 memory_needs_checking
= true;
733 struct test_arg_mem
*arg
= (struct test_arg_mem
*)args
;
734 current_stack
[arg
->index
] = arg
->val
;
743 struct kprobe kprobe
;
748 static void unregister_test_probe(struct test_probe
*probe
)
750 if (probe
->registered
) {
751 unregister_kprobe(&probe
->kprobe
);
752 probe
->kprobe
.flags
= 0; /* Clear disable flag to allow reuse */
754 probe
->registered
= false;
757 static int register_test_probe(struct test_probe
*probe
)
761 if (probe
->registered
)
764 ret
= register_kprobe(&probe
->kprobe
);
766 probe
->registered
= true;
773 test_before_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
775 container_of(p
, struct test_probe
, kprobe
)->hit
= test_instance
;
779 static void __kprobes
780 test_before_post_handler(struct kprobe
*p
, struct pt_regs
*regs
,
783 setup_test_context(regs
);
784 initial_regs
= *regs
;
785 initial_regs
.ARM_cpsr
&= ~PSR_IGNORE_BITS
;
789 test_case_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
791 container_of(p
, struct test_probe
, kprobe
)->hit
= test_instance
;
796 test_after_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
798 if (container_of(p
, struct test_probe
, kprobe
)->hit
== test_instance
)
799 return 0; /* Already run for this test instance */
802 result_regs
.ARM_cpsr
&= ~PSR_IGNORE_BITS
;
804 /* Undo any changes done to SP by the test case */
805 regs
->ARM_sp
= (unsigned long)current_stack
;
807 container_of(p
, struct test_probe
, kprobe
)->hit
= test_instance
;
811 static struct test_probe test_before_probe
= {
812 .kprobe
.pre_handler
= test_before_pre_handler
,
813 .kprobe
.post_handler
= test_before_post_handler
,
816 static struct test_probe test_case_probe
= {
817 .kprobe
.pre_handler
= test_case_pre_handler
,
820 static struct test_probe test_after_probe
= {
821 .kprobe
.pre_handler
= test_after_pre_handler
,
824 static struct test_probe test_after2_probe
= {
825 .kprobe
.pre_handler
= test_after_pre_handler
,
828 static void test_case_cleanup(void)
830 unregister_test_probe(&test_before_probe
);
831 unregister_test_probe(&test_case_probe
);
832 unregister_test_probe(&test_after_probe
);
833 unregister_test_probe(&test_after2_probe
);
836 static void print_registers(struct pt_regs
*regs
)
838 pr_err("r0 %08lx | r1 %08lx | r2 %08lx | r3 %08lx\n",
839 regs
->ARM_r0
, regs
->ARM_r1
, regs
->ARM_r2
, regs
->ARM_r3
);
840 pr_err("r4 %08lx | r5 %08lx | r6 %08lx | r7 %08lx\n",
841 regs
->ARM_r4
, regs
->ARM_r5
, regs
->ARM_r6
, regs
->ARM_r7
);
842 pr_err("r8 %08lx | r9 %08lx | r10 %08lx | r11 %08lx\n",
843 regs
->ARM_r8
, regs
->ARM_r9
, regs
->ARM_r10
, regs
->ARM_fp
);
844 pr_err("r12 %08lx | sp %08lx | lr %08lx | pc %08lx\n",
845 regs
->ARM_ip
, regs
->ARM_sp
, regs
->ARM_lr
, regs
->ARM_pc
);
846 pr_err("cpsr %08lx\n", regs
->ARM_cpsr
);
849 static void print_memory(u32
*mem
, size_t size
)
852 for (i
= 0; i
< size
/ sizeof(u32
); i
+= 4)
853 pr_err("%08x %08x %08x %08x\n", mem
[i
], mem
[i
+1],
857 static size_t expected_memory_size(u32
*sp
)
859 size_t size
= sizeof(expected_memory
);
860 int offset
= (uintptr_t)sp
- (uintptr_t)current_stack
;
866 static void test_case_failed(const char *message
)
870 pr_err("FAIL: %s\n", message
);
871 pr_err("FAIL: Test %s\n", current_title
);
872 pr_err("FAIL: Scenario %d\n", test_case_run_count
>> 1);
875 static unsigned long next_instruction(unsigned long pc
)
877 #ifdef CONFIG_THUMB2_KERNEL
878 if ((pc
& 1) && !is_wide_instruction(*(u16
*)(pc
- 1)))
885 static uintptr_t __used
kprobes_test_case_start(const char *title
, void *stack
)
887 struct test_arg
*args
;
888 struct test_arg_end
*end_arg
;
889 unsigned long test_code
;
891 args
= (struct test_arg
*)PTR_ALIGN(title
+ strlen(title
) + 1, 4);
893 current_title
= title
;
895 current_stack
= stack
;
899 while (args
->type
!= ARG_TYPE_END
)
901 end_arg
= (struct test_arg_end
*)args
;
903 test_code
= (unsigned long)(args
+ 1); /* Code starts after args */
905 test_case_is_thumb
= end_arg
->flags
& ARG_FLAG_THUMB
;
906 if (test_case_is_thumb
)
909 current_code_start
= test_code
;
911 current_branch_target
= 0;
912 if (end_arg
->branch_offset
!= end_arg
->end_offset
)
913 current_branch_target
= test_code
+ end_arg
->branch_offset
;
915 test_code
+= end_arg
->code_offset
;
916 test_before_probe
.kprobe
.addr
= (kprobe_opcode_t
*)test_code
;
918 test_code
= next_instruction(test_code
);
919 test_case_probe
.kprobe
.addr
= (kprobe_opcode_t
*)test_code
;
921 if (test_case_is_thumb
) {
922 u16
*p
= (u16
*)(test_code
& ~1);
923 current_instruction
= p
[0];
924 if (is_wide_instruction(current_instruction
)) {
925 current_instruction
<<= 16;
926 current_instruction
|= p
[1];
929 current_instruction
= *(u32
*)test_code
;
932 if (current_title
[0] == '.')
933 verbose("%s\n", current_title
);
935 verbose("%s\t@ %0*x\n", current_title
,
936 test_case_is_thumb
? 4 : 8,
937 current_instruction
);
939 test_code
= next_instruction(test_code
);
940 test_after_probe
.kprobe
.addr
= (kprobe_opcode_t
*)test_code
;
942 if (kprobe_test_flags
& TEST_FLAG_NARROW_INSTR
) {
943 if (!test_case_is_thumb
||
944 is_wide_instruction(current_instruction
)) {
945 test_case_failed("expected 16-bit instruction");
949 if (test_case_is_thumb
&&
950 !is_wide_instruction(current_instruction
)) {
951 test_case_failed("expected 32-bit instruction");
956 if (end_arg
->flags
& ARG_FLAG_UNSUPPORTED
) {
957 if (register_test_probe(&test_case_probe
) < 0)
959 test_case_failed("registered probe for unsupported instruction");
963 if (end_arg
->flags
& ARG_FLAG_SUPPORTED
) {
964 if (register_test_probe(&test_case_probe
) >= 0)
966 test_case_failed("couldn't register probe for supported instruction");
970 if (register_test_probe(&test_before_probe
) < 0) {
971 test_case_failed("register test_before_probe failed");
974 if (register_test_probe(&test_after_probe
) < 0) {
975 test_case_failed("register test_after_probe failed");
978 if (current_branch_target
) {
979 test_after2_probe
.kprobe
.addr
=
980 (kprobe_opcode_t
*)current_branch_target
;
981 if (register_test_probe(&test_after2_probe
) < 0) {
982 test_case_failed("register test_after2_probe failed");
987 /* Start first run of test case */
988 test_case_run_count
= 0;
990 return current_code_start
;
992 test_case_run_count
= TEST_CASE_PASSED
;
993 return (uintptr_t)test_after_probe
.kprobe
.addr
;
995 test_case_run_count
= TEST_CASE_FAILED
;
996 return (uintptr_t)test_after_probe
.kprobe
.addr
;
999 static bool check_test_results(void)
1001 size_t mem_size
= 0;
1004 if (memcmp(&expected_regs
, &result_regs
, sizeof(expected_regs
))) {
1005 test_case_failed("registers differ");
1009 if (memory_needs_checking
) {
1010 mem
= (u32
*)result_regs
.ARM_sp
;
1011 mem_size
= expected_memory_size(mem
);
1012 if (memcmp(expected_memory
, mem
, mem_size
)) {
1013 test_case_failed("test memory differs");
1021 pr_err("initial_regs:\n");
1022 print_registers(&initial_regs
);
1023 pr_err("expected_regs:\n");
1024 print_registers(&expected_regs
);
1025 pr_err("result_regs:\n");
1026 print_registers(&result_regs
);
1029 pr_err("current_stack=%p\n", current_stack
);
1030 pr_err("expected_memory:\n");
1031 print_memory(expected_memory
, mem_size
);
1032 pr_err("result_memory:\n");
1033 print_memory(mem
, mem_size
);
1039 static uintptr_t __used
kprobes_test_case_end(void)
1041 if (test_case_run_count
< 0) {
1042 if (test_case_run_count
== TEST_CASE_PASSED
)
1043 /* kprobes_test_case_start did all the needed testing */
1046 /* kprobes_test_case_start failed */
1050 if (test_before_probe
.hit
!= test_instance
) {
1051 test_case_failed("test_before_handler not run");
1055 if (test_after_probe
.hit
!= test_instance
&&
1056 test_after2_probe
.hit
!= test_instance
) {
1057 test_case_failed("test_after_handler not run");
1062 * Even numbered test runs ran without a probe on the test case so
1063 * we can gather reference results. The subsequent odd numbered run
1064 * will have the probe inserted.
1066 if ((test_case_run_count
& 1) == 0) {
1067 /* Save results from run without probe */
1068 u32
*mem
= (u32
*)result_regs
.ARM_sp
;
1069 expected_regs
= result_regs
;
1070 memcpy(expected_memory
, mem
, expected_memory_size(mem
));
1072 /* Insert probe onto test case instruction */
1073 if (register_test_probe(&test_case_probe
) < 0) {
1074 test_case_failed("register test_case_probe failed");
1078 /* Check probe ran as expected */
1079 if (probe_should_run
== 1) {
1080 if (test_case_probe
.hit
!= test_instance
) {
1081 test_case_failed("test_case_handler not run");
1084 } else if (probe_should_run
== 0) {
1085 if (test_case_probe
.hit
== test_instance
) {
1086 test_case_failed("test_case_handler ran");
1091 /* Remove probe for any subsequent reference run */
1092 unregister_test_probe(&test_case_probe
);
1094 if (!check_test_results())
1097 if (is_last_scenario
)
1101 /* Do next test run */
1102 ++test_case_run_count
;
1104 return current_code_start
;
1111 test_case_cleanup();
1117 * Top level test functions
1120 static int run_test_cases(void (*tests
)(void))
1122 pr_info(" Run test cases\n");
1129 static int __init
run_all_tests(void)
1133 pr_info("Begining kprobe tests...\n");
1135 #ifndef CONFIG_THUMB2_KERNEL
1137 pr_info("Probe ARM code\n");
1138 ret
= run_api_tests(arm_func
);
1142 #else /* CONFIG_THUMB2_KERNEL */
1144 pr_info("Probe 16-bit Thumb code\n");
1145 ret
= run_api_tests(thumb16_func
);
1149 pr_info("Probe 32-bit Thumb code, even halfword\n");
1150 ret
= run_api_tests(thumb32even_func
);
1154 pr_info("Probe 32-bit Thumb code, odd halfword\n");
1155 ret
= run_api_tests(thumb32odd_func
);
1159 pr_info("16-bit Thumb instruction simulation\n");
1160 ret
= run_test_cases(kprobe_thumb16_test_cases
);
1164 pr_info("32-bit Thumb instruction simulation\n");
1165 ret
= run_test_cases(kprobe_thumb32_test_cases
);
1170 pr_info("Total instruction simulation tests=%d, pass=%d fail=%d\n",
1171 test_try_count
, test_pass_count
, test_fail_count
);
1172 if (test_fail_count
) {
1179 pr_info("Finished kprobe tests OK\n");
1181 pr_err("kprobe tests failed\n");
1193 static void __exit
kprobe_test_exit(void)
1197 module_init(run_all_tests
)
1198 module_exit(kprobe_test_exit
)
1199 MODULE_LICENSE("GPL");
1203 late_initcall(run_all_tests
);