2 * arch/arm/kernel/kprobes-test.h
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.
11 #define VERBOSE 0 /* Set to '1' for more logging of test cases */
13 #ifdef CONFIG_THUMB2_KERNEL
14 #define NORMAL_ISA "16"
16 #define NORMAL_ISA "32"
20 /* Flags used in kprobe_test_flags */
21 #define TEST_FLAG_NO_ITBLOCK (1<<0)
22 #define TEST_FLAG_FULL_ITBLOCK (1<<1)
23 #define TEST_FLAG_NARROW_INSTR (1<<2)
25 extern int kprobe_test_flags
;
26 extern int kprobe_test_cc_position
;
29 #define TEST_MEMORY_SIZE 256
33 * Test case structures.
35 * The arguments given to test cases can be one of three types.
38 * Load a register with the given value.
41 * Load a register with a pointer into the stack buffer (SP + given value).
44 * Store the given value into the stack buffer at [SP+index].
48 #define ARG_TYPE_END 0
49 #define ARG_TYPE_REG 1
50 #define ARG_TYPE_PTR 2
51 #define ARG_TYPE_MEM 3
53 #define ARG_FLAG_UNSUPPORTED 0x01
54 #define ARG_FLAG_SUPPORTED 0x02
55 #define ARG_FLAG_THUMB 0x10 /* Must be 16 so TEST_ISA can be used */
56 #define ARG_FLAG_ARM 0x20 /* Must be 32 so TEST_ISA can be used */
59 u8 type
; /* ARG_TYPE_x */
63 struct test_arg_regptr
{
64 u8 type
; /* ARG_TYPE_REG or ARG_TYPE_PTR */
71 u8 type
; /* ARG_TYPE_MEM */
78 u8 type
; /* ARG_TYPE_END */
79 u8 flags
; /* ARG_FLAG_x */
87 * Building blocks for test cases.
89 * Each test case is wrapped between TESTCASE_START and TESTCASE_END.
91 * To specify arguments for a test case the TEST_ARG_{REG,PTR,MEM} macros are
92 * used followed by a terminating TEST_ARG_END.
94 * After this, the instruction to be tested is defined with TEST_INSTRUCTION.
95 * Or for branches, TEST_BRANCH_B and TEST_BRANCH_F (branch forwards/backwards).
97 * Some specific test cases may make use of other custom constructs.
101 #define verbose(fmt, ...) pr_info(fmt, ##__VA_ARGS__)
103 #define verbose(fmt, ...)
106 #define TEST_GROUP(title) \
108 verbose(title"\n"); \
109 verbose("---------------------------------------------------------\n");
111 #define TESTCASE_START(title) \
112 __asm__ __volatile__ ( \
113 "bl __kprobes_test_case_start \n\t" \
114 /* don't use .asciz here as 'title' may be */ \
115 /* multiple strings to be concatenated. */ \
116 ".ascii "#title" \n\t" \
120 #define TEST_ARG_REG(reg, val) \
121 ".byte "__stringify(ARG_TYPE_REG)" \n\t" \
122 ".byte "#reg" \n\t" \
126 #define TEST_ARG_PTR(reg, val) \
127 ".byte "__stringify(ARG_TYPE_PTR)" \n\t" \
128 ".byte "#reg" \n\t" \
132 #define TEST_ARG_MEM(index, val) \
133 ".byte "__stringify(ARG_TYPE_MEM)" \n\t" \
134 ".byte "#index" \n\t" \
138 #define TEST_ARG_END(flags) \
139 ".byte "__stringify(ARG_TYPE_END)" \n\t" \
140 ".byte "TEST_ISA flags" \n\t" \
141 ".short 50f-0f \n\t" \
142 ".short 2f-0f \n\t" \
143 ".short 99f-0f \n\t" \
144 ".code "TEST_ISA" \n\t" \
147 #define TEST_INSTRUCTION(instruction) \
149 "1: "instruction" \n\t" \
152 #define TEST_BRANCH_F(instruction, xtra_dist) \
153 TEST_INSTRUCTION(instruction) \
154 ".if "#xtra_dist" \n\t" \
156 ".space "#xtra_dist" \n\t" \
161 #define TEST_BRANCH_B(instruction, xtra_dist) \
166 ".if "#xtra_dist" \n\t" \
167 ".space "#xtra_dist" \n\t" \
169 TEST_INSTRUCTION(instruction)
171 #define TESTCASE_END \
174 " bl __kprobes_test_case_end_"TEST_ISA" \n\t" \
175 ".code "NORMAL_ISA" \n\t" \
177 : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc" \
182 * Macros to define test cases.
184 * Those of the form TEST_{R,P,M}* can be used to define test cases
185 * which take combinations of the three basic types of arguments. E.g.
187 * TEST_R One register argument
188 * TEST_RR Two register arguments
189 * TEST_RPR A register, a pointer, then a register argument
191 * For testing instructions which may branch, there are macros TEST_BF_*
192 * and TEST_BB_* for branching forwards and backwards.
194 * TEST_SUPPORTED and TEST_UNSUPPORTED don't cause the code to be executed,
195 * the just verify that a kprobe is or is not allowed on the given instruction.
199 TESTCASE_START(code) \
201 TEST_INSTRUCTION(code) \
204 #define TEST_UNSUPPORTED(code) \
205 TESTCASE_START(code) \
206 TEST_ARG_END("|"__stringify(ARG_FLAG_UNSUPPORTED)) \
207 TEST_INSTRUCTION(code) \
210 #define TEST_SUPPORTED(code) \
211 TESTCASE_START(code) \
212 TEST_ARG_END("|"__stringify(ARG_FLAG_SUPPORTED)) \
213 TEST_INSTRUCTION(code) \
216 #define TEST_R(code1, reg, val, code2) \
217 TESTCASE_START(code1 #reg code2) \
218 TEST_ARG_REG(reg, val) \
220 TEST_INSTRUCTION(code1 #reg code2) \
223 #define TEST_RR(code1, reg1, val1, code2, reg2, val2, code3) \
224 TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
225 TEST_ARG_REG(reg1, val1) \
226 TEST_ARG_REG(reg2, val2) \
228 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
231 #define TEST_RRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
232 TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
233 TEST_ARG_REG(reg1, val1) \
234 TEST_ARG_REG(reg2, val2) \
235 TEST_ARG_REG(reg3, val3) \
237 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
240 #define TEST_RRRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4, reg4, val4) \
241 TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4) \
242 TEST_ARG_REG(reg1, val1) \
243 TEST_ARG_REG(reg2, val2) \
244 TEST_ARG_REG(reg3, val3) \
245 TEST_ARG_REG(reg4, val4) \
247 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4) \
250 #define TEST_P(code1, reg1, val1, code2) \
251 TESTCASE_START(code1 #reg1 code2) \
252 TEST_ARG_PTR(reg1, val1) \
254 TEST_INSTRUCTION(code1 #reg1 code2) \
257 #define TEST_PR(code1, reg1, val1, code2, reg2, val2, code3) \
258 TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
259 TEST_ARG_PTR(reg1, val1) \
260 TEST_ARG_REG(reg2, val2) \
262 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
265 #define TEST_RP(code1, reg1, val1, code2, reg2, val2, code3) \
266 TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
267 TEST_ARG_REG(reg1, val1) \
268 TEST_ARG_PTR(reg2, val2) \
270 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
273 #define TEST_PRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
274 TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
275 TEST_ARG_PTR(reg1, val1) \
276 TEST_ARG_REG(reg2, val2) \
277 TEST_ARG_REG(reg3, val3) \
279 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
282 #define TEST_RPR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
283 TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
284 TEST_ARG_REG(reg1, val1) \
285 TEST_ARG_PTR(reg2, val2) \
286 TEST_ARG_REG(reg3, val3) \
288 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
291 #define TEST_RRP(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
292 TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
293 TEST_ARG_REG(reg1, val1) \
294 TEST_ARG_REG(reg2, val2) \
295 TEST_ARG_PTR(reg3, val3) \
297 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
300 #define TEST_BF_P(code1, reg1, val1, code2) \
301 TESTCASE_START(code1 #reg1 code2) \
302 TEST_ARG_PTR(reg1, val1) \
304 TEST_BRANCH_F(code1 #reg1 code2, 0) \
307 #define TEST_BF_X(code, xtra_dist) \
308 TESTCASE_START(code) \
310 TEST_BRANCH_F(code, xtra_dist) \
313 #define TEST_BB_X(code, xtra_dist) \
314 TESTCASE_START(code) \
316 TEST_BRANCH_B(code, xtra_dist) \
319 #define TEST_BF_RX(code1, reg, val, code2, xtra_dist) \
320 TESTCASE_START(code1 #reg code2) \
321 TEST_ARG_REG(reg, val) \
323 TEST_BRANCH_F(code1 #reg code2, xtra_dist) \
326 #define TEST_BB_RX(code1, reg, val, code2, xtra_dist) \
327 TESTCASE_START(code1 #reg code2) \
328 TEST_ARG_REG(reg, val) \
330 TEST_BRANCH_B(code1 #reg code2, xtra_dist) \
333 #define TEST_BF(code) TEST_BF_X(code, 0)
334 #define TEST_BB(code) TEST_BB_X(code, 0)
336 #define TEST_BF_R(code1, reg, val, code2) TEST_BF_RX(code1, reg, val, code2, 0)
337 #define TEST_BB_R(code1, reg, val, code2) TEST_BB_RX(code1, reg, val, code2, 0)
339 #define TEST_BF_RR(code1, reg1, val1, code2, reg2, val2, code3) \
340 TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
341 TEST_ARG_REG(reg1, val1) \
342 TEST_ARG_REG(reg2, val2) \
344 TEST_BRANCH_F(code1 #reg1 code2 #reg2 code3, 0) \
347 #define TEST_X(code, codex) \
348 TESTCASE_START(code) \
350 TEST_INSTRUCTION(code) \
355 #define TEST_RX(code1, reg, val, code2, codex) \
356 TESTCASE_START(code1 #reg code2) \
357 TEST_ARG_REG(reg, val) \
359 TEST_INSTRUCTION(code1 __stringify(reg) code2) \
364 #define TEST_RRX(code1, reg1, val1, code2, reg2, val2, code3, codex) \
365 TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
366 TEST_ARG_REG(reg1, val1) \
367 TEST_ARG_REG(reg2, val2) \
369 TEST_INSTRUCTION(code1 __stringify(reg1) code2 __stringify(reg2) code3) \
375 /* Various values used in test cases... */
376 #define N(val) (val ^ 0xffffffff)
377 #define VAL1 0x12345678
379 #define VAL3 0xa5f801
381 #define VALM 0x456789ab
382 #define VALR 0xdeaddead
383 #define HH1 0x0123fecb
384 #define HH2 0xa9874567
387 #ifdef CONFIG_THUMB2_KERNEL
388 void kprobe_thumb16_test_cases(void);
389 void kprobe_thumb32_test_cases(void);
391 void kprobe_arm_test_cases(void);