1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * arch/arm/probes/kprobes/test-core.h
5 * Copyright (C) 2011 Jon Medhurst <tixy@yxit.co.uk>.
8 #define VERBOSE 0 /* Set to '1' for more logging of test cases */
10 #ifdef CONFIG_THUMB2_KERNEL
11 #define NORMAL_ISA "16"
13 #define NORMAL_ISA "32"
17 /* Flags used in kprobe_test_flags */
18 #define TEST_FLAG_NO_ITBLOCK (1<<0)
19 #define TEST_FLAG_FULL_ITBLOCK (1<<1)
20 #define TEST_FLAG_NARROW_INSTR (1<<2)
22 extern int kprobe_test_flags
;
23 extern int kprobe_test_cc_position
;
26 #define TEST_MEMORY_SIZE 256
30 * Test case structures.
32 * The arguments given to test cases can be one of three types.
35 * Load a register with the given value.
38 * Load a register with a pointer into the stack buffer (SP + given value).
41 * Store the given value into the stack buffer at [SP+index].
45 #define ARG_TYPE_END 0
46 #define ARG_TYPE_REG 1
47 #define ARG_TYPE_PTR 2
48 #define ARG_TYPE_MEM 3
49 #define ARG_TYPE_REG_MASKED 4
51 #define ARG_FLAG_UNSUPPORTED 0x01
52 #define ARG_FLAG_SUPPORTED 0x02
53 #define ARG_FLAG_THUMB 0x10 /* Must be 16 so TEST_ISA can be used */
54 #define ARG_FLAG_ARM 0x20 /* Must be 32 so TEST_ISA can be used */
57 u8 type
; /* ARG_TYPE_x */
61 struct test_arg_regptr
{
62 u8 type
; /* ARG_TYPE_REG or ARG_TYPE_PTR or ARG_TYPE_REG_MASKED */
69 u8 type
; /* ARG_TYPE_MEM */
76 u8 type
; /* ARG_TYPE_END */
77 u8 flags
; /* ARG_FLAG_x */
85 * Building blocks for test cases.
87 * Each test case is wrapped between TESTCASE_START and TESTCASE_END.
89 * To specify arguments for a test case the TEST_ARG_{REG,PTR,MEM} macros are
90 * used followed by a terminating TEST_ARG_END.
92 * After this, the instruction to be tested is defined with TEST_INSTRUCTION.
93 * Or for branches, TEST_BRANCH_B and TEST_BRANCH_F (branch forwards/backwards).
95 * Some specific test cases may make use of other custom constructs.
99 #define verbose(fmt, ...) pr_info(fmt, ##__VA_ARGS__)
101 #define verbose(fmt, ...) no_printk(fmt, ##__VA_ARGS__)
104 #define TEST_GROUP(title) \
106 verbose(title"\n"); \
107 verbose("---------------------------------------------------------\n");
109 #define TESTCASE_START(title) \
110 __asm__ __volatile__ ( \
111 ".syntax unified \n\t" \
112 "bl __kprobes_test_case_start \n\t" \
113 ".pushsection .rodata \n\t" \
115 /* don't use .asciz here as 'title' may be */ \
116 /* multiple strings to be concatenated. */ \
117 ".ascii "#title" \n\t" \
122 #define TEST_ARG_REG(reg, val) \
123 ".byte "__stringify(ARG_TYPE_REG)" \n\t" \
124 ".byte "#reg" \n\t" \
128 #define TEST_ARG_PTR(reg, val) \
129 ".byte "__stringify(ARG_TYPE_PTR)" \n\t" \
130 ".byte "#reg" \n\t" \
134 #define TEST_ARG_MEM(index, val) \
135 ".byte "__stringify(ARG_TYPE_MEM)" \n\t" \
136 ".byte "#index" \n\t" \
140 #define TEST_ARG_REG_MASKED(reg, val) \
141 ".byte "__stringify(ARG_TYPE_REG_MASKED)" \n\t" \
142 ".byte "#reg" \n\t" \
146 #define TEST_ARG_END(flags) \
147 ".byte "__stringify(ARG_TYPE_END)" \n\t" \
148 ".byte "TEST_ISA flags" \n\t" \
149 ".short 50f-0f \n\t" \
150 ".short 2f-0f \n\t" \
151 ".short 99f-0f \n\t" \
152 ".code "TEST_ISA" \n\t" \
155 #define TEST_INSTRUCTION(instruction) \
157 "1: "instruction" \n\t" \
160 #define TEST_BRANCH_F(instruction) \
161 TEST_INSTRUCTION(instruction) \
165 #define TEST_BRANCH_B(instruction) \
170 TEST_INSTRUCTION(instruction)
172 #define TEST_BRANCH_FX(instruction, codex) \
173 TEST_INSTRUCTION(instruction) \
179 #define TEST_BRANCH_BX(instruction, codex) \
185 TEST_INSTRUCTION(instruction)
187 #define TESTCASE_END \
190 " bl __kprobes_test_case_end_"TEST_ISA" \n\t" \
191 ".code "NORMAL_ISA" \n\t" \
193 : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc" \
198 * Macros to define test cases.
200 * Those of the form TEST_{R,P,M}* can be used to define test cases
201 * which take combinations of the three basic types of arguments. E.g.
203 * TEST_R One register argument
204 * TEST_RR Two register arguments
205 * TEST_RPR A register, a pointer, then a register argument
207 * For testing instructions which may branch, there are macros TEST_BF_*
208 * and TEST_BB_* for branching forwards and backwards.
210 * TEST_SUPPORTED and TEST_UNSUPPORTED don't cause the code to be executed,
211 * the just verify that a kprobe is or is not allowed on the given instruction.
215 TESTCASE_START(code) \
217 TEST_INSTRUCTION(code) \
220 #define TEST_UNSUPPORTED(code) \
221 TESTCASE_START(code) \
222 TEST_ARG_END("|"__stringify(ARG_FLAG_UNSUPPORTED)) \
223 TEST_INSTRUCTION(code) \
226 #define TEST_SUPPORTED(code) \
227 TESTCASE_START(code) \
228 TEST_ARG_END("|"__stringify(ARG_FLAG_SUPPORTED)) \
229 TEST_INSTRUCTION(code) \
232 #define TEST_R(code1, reg, val, code2) \
233 TESTCASE_START(code1 #reg code2) \
234 TEST_ARG_REG(reg, val) \
236 TEST_INSTRUCTION(code1 #reg code2) \
239 #define TEST_RR(code1, reg1, val1, code2, reg2, val2, code3) \
240 TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
241 TEST_ARG_REG(reg1, val1) \
242 TEST_ARG_REG(reg2, val2) \
244 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
247 #define TEST_RRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
248 TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
249 TEST_ARG_REG(reg1, val1) \
250 TEST_ARG_REG(reg2, val2) \
251 TEST_ARG_REG(reg3, val3) \
253 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
256 #define TEST_RRRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4, reg4, val4) \
257 TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4) \
258 TEST_ARG_REG(reg1, val1) \
259 TEST_ARG_REG(reg2, val2) \
260 TEST_ARG_REG(reg3, val3) \
261 TEST_ARG_REG(reg4, val4) \
263 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4) \
266 #define TEST_P(code1, reg1, val1, code2) \
267 TESTCASE_START(code1 #reg1 code2) \
268 TEST_ARG_PTR(reg1, val1) \
270 TEST_INSTRUCTION(code1 #reg1 code2) \
273 #define TEST_PR(code1, reg1, val1, code2, reg2, val2, code3) \
274 TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
275 TEST_ARG_PTR(reg1, val1) \
276 TEST_ARG_REG(reg2, val2) \
278 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
281 #define TEST_RP(code1, reg1, val1, code2, reg2, val2, code3) \
282 TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
283 TEST_ARG_REG(reg1, val1) \
284 TEST_ARG_PTR(reg2, val2) \
286 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
289 #define TEST_PRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
290 TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
291 TEST_ARG_PTR(reg1, val1) \
292 TEST_ARG_REG(reg2, val2) \
293 TEST_ARG_REG(reg3, val3) \
295 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
298 #define TEST_RPR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
299 TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
300 TEST_ARG_REG(reg1, val1) \
301 TEST_ARG_PTR(reg2, val2) \
302 TEST_ARG_REG(reg3, val3) \
304 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
307 #define TEST_RRP(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
308 TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
309 TEST_ARG_REG(reg1, val1) \
310 TEST_ARG_REG(reg2, val2) \
311 TEST_ARG_PTR(reg3, val3) \
313 TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
316 #define TEST_BF_P(code1, reg1, val1, code2) \
317 TESTCASE_START(code1 #reg1 code2) \
318 TEST_ARG_PTR(reg1, val1) \
320 TEST_BRANCH_F(code1 #reg1 code2) \
323 #define TEST_BF(code) \
324 TESTCASE_START(code) \
326 TEST_BRANCH_F(code) \
329 #define TEST_BB(code) \
330 TESTCASE_START(code) \
332 TEST_BRANCH_B(code) \
335 #define TEST_BF_R(code1, reg, val, code2) \
336 TESTCASE_START(code1 #reg code2) \
337 TEST_ARG_REG(reg, val) \
339 TEST_BRANCH_F(code1 #reg code2) \
342 #define TEST_BB_R(code1, reg, val, code2) \
343 TESTCASE_START(code1 #reg code2) \
344 TEST_ARG_REG(reg, val) \
346 TEST_BRANCH_B(code1 #reg code2) \
349 #define TEST_BF_RR(code1, reg1, val1, code2, reg2, val2, code3) \
350 TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
351 TEST_ARG_REG(reg1, val1) \
352 TEST_ARG_REG(reg2, val2) \
354 TEST_BRANCH_F(code1 #reg1 code2 #reg2 code3) \
357 #define TEST_BF_X(code, codex) \
358 TESTCASE_START(code) \
360 TEST_BRANCH_FX(code, codex) \
363 #define TEST_BB_X(code, codex) \
364 TESTCASE_START(code) \
366 TEST_BRANCH_BX(code, codex) \
369 #define TEST_BF_RX(code1, reg, val, code2, codex) \
370 TESTCASE_START(code1 #reg code2) \
371 TEST_ARG_REG(reg, val) \
373 TEST_BRANCH_FX(code1 #reg code2, codex) \
376 #define TEST_X(code, codex) \
377 TESTCASE_START(code) \
379 TEST_INSTRUCTION(code) \
384 #define TEST_RX(code1, reg, val, code2, codex) \
385 TESTCASE_START(code1 #reg code2) \
386 TEST_ARG_REG(reg, val) \
388 TEST_INSTRUCTION(code1 __stringify(reg) code2) \
393 #define TEST_RRX(code1, reg1, val1, code2, reg2, val2, code3, codex) \
394 TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
395 TEST_ARG_REG(reg1, val1) \
396 TEST_ARG_REG(reg2, val2) \
398 TEST_INSTRUCTION(code1 __stringify(reg1) code2 __stringify(reg2) code3) \
403 #define TEST_RMASKED(code1, reg, mask, code2) \
404 TESTCASE_START(code1 #reg code2) \
405 TEST_ARG_REG_MASKED(reg, mask) \
407 TEST_INSTRUCTION(code1 #reg code2) \
411 * We ignore the state of the imprecise abort disable flag (CPSR.A) because this
412 * can change randomly as the kernel doesn't take care to preserve or initialise
413 * this across context switches. Also, with Security Extensions, the flag may
414 * not be under control of the kernel; for this reason we ignore the state of
415 * the FIQ disable flag CPSR.F as well.
417 #define PSR_IGNORE_BITS (PSR_A_BIT | PSR_F_BIT)
421 * Macros for defining space directives spread over multiple lines.
422 * These are required so the compiler guesses better the length of inline asm
423 * code and will spill the literal pool early enough to avoid generating PC
424 * relative loads with out of range offsets.
427 #define SPACE_0x8 TWICE(".space 4\n\t")
428 #define SPACE_0x10 TWICE(SPACE_0x8)
429 #define SPACE_0x20 TWICE(SPACE_0x10)
430 #define SPACE_0x40 TWICE(SPACE_0x20)
431 #define SPACE_0x80 TWICE(SPACE_0x40)
432 #define SPACE_0x100 TWICE(SPACE_0x80)
433 #define SPACE_0x200 TWICE(SPACE_0x100)
434 #define SPACE_0x400 TWICE(SPACE_0x200)
435 #define SPACE_0x800 TWICE(SPACE_0x400)
436 #define SPACE_0x1000 TWICE(SPACE_0x800)
439 /* Various values used in test cases... */
440 #define N(val) (val ^ 0xffffffff)
441 #define VAL1 0x12345678
443 #define VAL3 0xa5f801
445 #define VALM 0x456789ab
446 #define VALR 0xdeaddead
447 #define HH1 0x0123fecb
448 #define HH2 0xa9874567
451 #ifdef CONFIG_THUMB2_KERNEL
452 void kprobe_thumb16_test_cases(void);
453 void kprobe_thumb32_test_cases(void);
455 void kprobe_arm_test_cases(void);
458 void __kprobes_test_case_start(void);
459 void __kprobes_test_case_end_16(void);
460 void __kprobes_test_case_end_32(void);