• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * arch/arm/probes/kprobes/test-core.h
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 
11 #define VERBOSE 0 /* Set to '1' for more logging of test cases */
12 
13 #ifdef CONFIG_THUMB2_KERNEL
14 #define NORMAL_ISA "16"
15 #else
16 #define NORMAL_ISA "32"
17 #endif
18 
19 
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)
24 
25 extern int kprobe_test_flags;
26 extern int kprobe_test_cc_position;
27 
28 
29 #define TEST_MEMORY_SIZE 256
30 
31 
32 /*
33  * Test case structures.
34  *
35  * The arguments given to test cases can be one of three types.
36  *
37  *   ARG_TYPE_REG
38  *	Load a register with the given value.
39  *
40  *   ARG_TYPE_PTR
41  *	Load a register with a pointer into the stack buffer (SP + given value).
42  *
43  *   ARG_TYPE_MEM
44  *	Store the given value into the stack buffer at [SP+index].
45  *
46  */
47 
48 #define	ARG_TYPE_END		0
49 #define	ARG_TYPE_REG		1
50 #define	ARG_TYPE_PTR		2
51 #define	ARG_TYPE_MEM		3
52 #define	ARG_TYPE_REG_MASKED	4
53 
54 #define ARG_FLAG_UNSUPPORTED	0x01
55 #define ARG_FLAG_SUPPORTED	0x02
56 #define ARG_FLAG_THUMB		0x10	/* Must be 16 so TEST_ISA can be used */
57 #define ARG_FLAG_ARM		0x20	/* Must be 32 so TEST_ISA can be used */
58 
59 struct test_arg {
60 	u8	type;		/* ARG_TYPE_x */
61 	u8	_padding[7];
62 };
63 
64 struct test_arg_regptr {
65 	u8	type;		/* ARG_TYPE_REG or ARG_TYPE_PTR or ARG_TYPE_REG_MASKED */
66 	u8	reg;
67 	u8	_padding[2];
68 	u32	val;
69 };
70 
71 struct test_arg_mem {
72 	u8	type;		/* ARG_TYPE_MEM */
73 	u8	index;
74 	u8	_padding[2];
75 	u32	val;
76 };
77 
78 struct test_arg_end {
79 	u8	type;		/* ARG_TYPE_END */
80 	u8	flags;		/* ARG_FLAG_x */
81 	u16	code_offset;
82 	u16	branch_offset;
83 	u16	end_offset;
84 };
85 
86 
87 /*
88  * Building blocks for test cases.
89  *
90  * Each test case is wrapped between TESTCASE_START and TESTCASE_END.
91  *
92  * To specify arguments for a test case the TEST_ARG_{REG,PTR,MEM} macros are
93  * used followed by a terminating TEST_ARG_END.
94  *
95  * After this, the instruction to be tested is defined with TEST_INSTRUCTION.
96  * Or for branches, TEST_BRANCH_B and TEST_BRANCH_F (branch forwards/backwards).
97  *
98  * Some specific test cases may make use of other custom constructs.
99  */
100 
101 #if VERBOSE
102 #define verbose(fmt, ...) pr_info(fmt, ##__VA_ARGS__)
103 #else
104 #define verbose(fmt, ...)
105 #endif
106 
107 #define TEST_GROUP(title)					\
108 	verbose("\n");						\
109 	verbose(title"\n");					\
110 	verbose("---------------------------------------------------------\n");
111 
112 #define TESTCASE_START(title)					\
113 	__asm__ __volatile__ (					\
114 	"bl	__kprobes_test_case_start		\n\t"	\
115 	".pushsection .rodata				\n\t"	\
116 	"10:						\n\t"	\
117 	/* don't use .asciz here as 'title' may be */		\
118 	/* multiple strings to be concatenated.  */		\
119 	".ascii "#title"				\n\t"	\
120 	".byte	0					\n\t"	\
121 	".popsection					\n\t"	\
122 	".word	10b					\n\t"
123 
124 #define	TEST_ARG_REG(reg, val)					\
125 	".byte	"__stringify(ARG_TYPE_REG)"		\n\t"	\
126 	".byte	"#reg"					\n\t"	\
127 	".short	0					\n\t"	\
128 	".word	"#val"					\n\t"
129 
130 #define	TEST_ARG_PTR(reg, val)					\
131 	".byte	"__stringify(ARG_TYPE_PTR)"		\n\t"	\
132 	".byte	"#reg"					\n\t"	\
133 	".short	0					\n\t"	\
134 	".word	"#val"					\n\t"
135 
136 #define	TEST_ARG_MEM(index, val)				\
137 	".byte	"__stringify(ARG_TYPE_MEM)"		\n\t"	\
138 	".byte	"#index"				\n\t"	\
139 	".short	0					\n\t"	\
140 	".word	"#val"					\n\t"
141 
142 #define	TEST_ARG_REG_MASKED(reg, val)				\
143 	".byte	"__stringify(ARG_TYPE_REG_MASKED)"	\n\t"	\
144 	".byte	"#reg"					\n\t"	\
145 	".short	0					\n\t"	\
146 	".word	"#val"					\n\t"
147 
148 #define	TEST_ARG_END(flags)					\
149 	".byte	"__stringify(ARG_TYPE_END)"		\n\t"	\
150 	".byte	"TEST_ISA flags"			\n\t"	\
151 	".short	50f-0f					\n\t"	\
152 	".short	2f-0f					\n\t"	\
153 	".short	99f-0f					\n\t"	\
154 	".code "TEST_ISA"				\n\t"	\
155 	"0:						\n\t"
156 
157 #define TEST_INSTRUCTION(instruction)				\
158 	"50:	nop					\n\t"	\
159 	"1:	"instruction"				\n\t"	\
160 	"	nop					\n\t"
161 
162 #define TEST_BRANCH_F(instruction)				\
163 	TEST_INSTRUCTION(instruction)				\
164 	"	b	99f				\n\t"	\
165 	"2:	nop					\n\t"
166 
167 #define TEST_BRANCH_B(instruction)				\
168 	"	b	50f				\n\t"	\
169 	"	b	99f				\n\t"	\
170 	"2:	nop					\n\t"	\
171 	"	b	99f				\n\t"	\
172 	TEST_INSTRUCTION(instruction)
173 
174 #define TEST_BRANCH_FX(instruction, codex)			\
175 	TEST_INSTRUCTION(instruction)				\
176 	"	b	99f				\n\t"	\
177 	codex"						\n\t"	\
178 	"	b	99f				\n\t"	\
179 	"2:	nop					\n\t"
180 
181 #define TEST_BRANCH_BX(instruction, codex)			\
182 	"	b	50f				\n\t"	\
183 	"	b	99f				\n\t"	\
184 	"2:	nop					\n\t"	\
185 	"	b	99f				\n\t"	\
186 	codex"						\n\t"	\
187 	TEST_INSTRUCTION(instruction)
188 
189 #define TESTCASE_END						\
190 	"2:						\n\t"	\
191 	"99:						\n\t"	\
192 	"	bl __kprobes_test_case_end_"TEST_ISA"	\n\t"	\
193 	".code "NORMAL_ISA"				\n\t"	\
194 	: :							\
195 	: "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc"	\
196 	);
197 
198 
199 /*
200  * Macros to define test cases.
201  *
202  * Those of the form TEST_{R,P,M}* can be used to define test cases
203  * which take combinations of the three basic types of arguments. E.g.
204  *
205  *   TEST_R	One register argument
206  *   TEST_RR	Two register arguments
207  *   TEST_RPR	A register, a pointer, then a register argument
208  *
209  * For testing instructions which may branch, there are macros TEST_BF_*
210  * and TEST_BB_* for branching forwards and backwards.
211  *
212  * TEST_SUPPORTED and TEST_UNSUPPORTED don't cause the code to be executed,
213  * the just verify that a kprobe is or is not allowed on the given instruction.
214  */
215 
216 #define TEST(code)				\
217 	TESTCASE_START(code)			\
218 	TEST_ARG_END("")			\
219 	TEST_INSTRUCTION(code)			\
220 	TESTCASE_END
221 
222 #define TEST_UNSUPPORTED(code)					\
223 	TESTCASE_START(code)					\
224 	TEST_ARG_END("|"__stringify(ARG_FLAG_UNSUPPORTED))	\
225 	TEST_INSTRUCTION(code)					\
226 	TESTCASE_END
227 
228 #define TEST_SUPPORTED(code)					\
229 	TESTCASE_START(code)					\
230 	TEST_ARG_END("|"__stringify(ARG_FLAG_SUPPORTED))	\
231 	TEST_INSTRUCTION(code)					\
232 	TESTCASE_END
233 
234 #define TEST_R(code1, reg, val, code2)			\
235 	TESTCASE_START(code1 #reg code2)		\
236 	TEST_ARG_REG(reg, val)				\
237 	TEST_ARG_END("")				\
238 	TEST_INSTRUCTION(code1 #reg code2)		\
239 	TESTCASE_END
240 
241 #define TEST_RR(code1, reg1, val1, code2, reg2, val2, code3)	\
242 	TESTCASE_START(code1 #reg1 code2 #reg2 code3)		\
243 	TEST_ARG_REG(reg1, val1)				\
244 	TEST_ARG_REG(reg2, val2)				\
245 	TEST_ARG_END("")					\
246 	TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3)		\
247 	TESTCASE_END
248 
249 #define TEST_RRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
250 	TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)		\
251 	TEST_ARG_REG(reg1, val1)						\
252 	TEST_ARG_REG(reg2, val2)						\
253 	TEST_ARG_REG(reg3, val3)						\
254 	TEST_ARG_END("")							\
255 	TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)		\
256 	TESTCASE_END
257 
258 #define TEST_RRRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4, reg4, val4)	\
259 	TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4)		\
260 	TEST_ARG_REG(reg1, val1)						\
261 	TEST_ARG_REG(reg2, val2)						\
262 	TEST_ARG_REG(reg3, val3)						\
263 	TEST_ARG_REG(reg4, val4)						\
264 	TEST_ARG_END("")							\
265 	TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4)	\
266 	TESTCASE_END
267 
268 #define TEST_P(code1, reg1, val1, code2)	\
269 	TESTCASE_START(code1 #reg1 code2)	\
270 	TEST_ARG_PTR(reg1, val1)		\
271 	TEST_ARG_END("")			\
272 	TEST_INSTRUCTION(code1 #reg1 code2)	\
273 	TESTCASE_END
274 
275 #define TEST_PR(code1, reg1, val1, code2, reg2, val2, code3)	\
276 	TESTCASE_START(code1 #reg1 code2 #reg2 code3)		\
277 	TEST_ARG_PTR(reg1, val1)				\
278 	TEST_ARG_REG(reg2, val2)				\
279 	TEST_ARG_END("")					\
280 	TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3)		\
281 	TESTCASE_END
282 
283 #define TEST_RP(code1, reg1, val1, code2, reg2, val2, code3)	\
284 	TESTCASE_START(code1 #reg1 code2 #reg2 code3)		\
285 	TEST_ARG_REG(reg1, val1)				\
286 	TEST_ARG_PTR(reg2, val2)				\
287 	TEST_ARG_END("")					\
288 	TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3)		\
289 	TESTCASE_END
290 
291 #define TEST_PRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
292 	TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)		\
293 	TEST_ARG_PTR(reg1, val1)						\
294 	TEST_ARG_REG(reg2, val2)						\
295 	TEST_ARG_REG(reg3, val3)						\
296 	TEST_ARG_END("")							\
297 	TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)		\
298 	TESTCASE_END
299 
300 #define TEST_RPR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
301 	TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)		\
302 	TEST_ARG_REG(reg1, val1)						\
303 	TEST_ARG_PTR(reg2, val2)						\
304 	TEST_ARG_REG(reg3, val3)						\
305 	TEST_ARG_END("")							\
306 	TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)		\
307 	TESTCASE_END
308 
309 #define TEST_RRP(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
310 	TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4)		\
311 	TEST_ARG_REG(reg1, val1)						\
312 	TEST_ARG_REG(reg2, val2)						\
313 	TEST_ARG_PTR(reg3, val3)						\
314 	TEST_ARG_END("")							\
315 	TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4)		\
316 	TESTCASE_END
317 
318 #define TEST_BF_P(code1, reg1, val1, code2)	\
319 	TESTCASE_START(code1 #reg1 code2)	\
320 	TEST_ARG_PTR(reg1, val1)		\
321 	TEST_ARG_END("")			\
322 	TEST_BRANCH_F(code1 #reg1 code2)	\
323 	TESTCASE_END
324 
325 #define TEST_BF(code)				\
326 	TESTCASE_START(code)			\
327 	TEST_ARG_END("")			\
328 	TEST_BRANCH_F(code)			\
329 	TESTCASE_END
330 
331 #define TEST_BB(code)				\
332 	TESTCASE_START(code)			\
333 	TEST_ARG_END("")			\
334 	TEST_BRANCH_B(code)			\
335 	TESTCASE_END
336 
337 #define TEST_BF_R(code1, reg, val, code2)	\
338 	TESTCASE_START(code1 #reg code2)	\
339 	TEST_ARG_REG(reg, val)			\
340 	TEST_ARG_END("")			\
341 	TEST_BRANCH_F(code1 #reg code2)		\
342 	TESTCASE_END
343 
344 #define TEST_BB_R(code1, reg, val, code2)	\
345 	TESTCASE_START(code1 #reg code2)	\
346 	TEST_ARG_REG(reg, val)			\
347 	TEST_ARG_END("")			\
348 	TEST_BRANCH_B(code1 #reg code2)		\
349 	TESTCASE_END
350 
351 #define TEST_BF_RR(code1, reg1, val1, code2, reg2, val2, code3)	\
352 	TESTCASE_START(code1 #reg1 code2 #reg2 code3)		\
353 	TEST_ARG_REG(reg1, val1)				\
354 	TEST_ARG_REG(reg2, val2)				\
355 	TEST_ARG_END("")					\
356 	TEST_BRANCH_F(code1 #reg1 code2 #reg2 code3)		\
357 	TESTCASE_END
358 
359 #define TEST_BF_X(code, codex)			\
360 	TESTCASE_START(code)			\
361 	TEST_ARG_END("")			\
362 	TEST_BRANCH_FX(code, codex)		\
363 	TESTCASE_END
364 
365 #define TEST_BB_X(code, codex)			\
366 	TESTCASE_START(code)			\
367 	TEST_ARG_END("")			\
368 	TEST_BRANCH_BX(code, codex)		\
369 	TESTCASE_END
370 
371 #define TEST_BF_RX(code1, reg, val, code2, codex)	\
372 	TESTCASE_START(code1 #reg code2)		\
373 	TEST_ARG_REG(reg, val)				\
374 	TEST_ARG_END("")				\
375 	TEST_BRANCH_FX(code1 #reg code2, codex)		\
376 	TESTCASE_END
377 
378 #define TEST_X(code, codex)			\
379 	TESTCASE_START(code)			\
380 	TEST_ARG_END("")			\
381 	TEST_INSTRUCTION(code)			\
382 	"	b	99f		\n\t"	\
383 	"	"codex"			\n\t"	\
384 	TESTCASE_END
385 
386 #define TEST_RX(code1, reg, val, code2, codex)		\
387 	TESTCASE_START(code1 #reg code2)		\
388 	TEST_ARG_REG(reg, val)				\
389 	TEST_ARG_END("")				\
390 	TEST_INSTRUCTION(code1 __stringify(reg) code2)	\
391 	"	b	99f		\n\t"		\
392 	"	"codex"			\n\t"		\
393 	TESTCASE_END
394 
395 #define TEST_RRX(code1, reg1, val1, code2, reg2, val2, code3, codex)		\
396 	TESTCASE_START(code1 #reg1 code2 #reg2 code3)				\
397 	TEST_ARG_REG(reg1, val1)						\
398 	TEST_ARG_REG(reg2, val2)						\
399 	TEST_ARG_END("")							\
400 	TEST_INSTRUCTION(code1 __stringify(reg1) code2 __stringify(reg2) code3)	\
401 	"	b	99f		\n\t"					\
402 	"	"codex"			\n\t"					\
403 	TESTCASE_END
404 
405 #define TEST_RMASKED(code1, reg, mask, code2)		\
406 	TESTCASE_START(code1 #reg code2)		\
407 	TEST_ARG_REG_MASKED(reg, mask)			\
408 	TEST_ARG_END("")				\
409 	TEST_INSTRUCTION(code1 #reg code2)		\
410 	TESTCASE_END
411 
412 /*
413  * We ignore the state of the imprecise abort disable flag (CPSR.A) because this
414  * can change randomly as the kernel doesn't take care to preserve or initialise
415  * this across context switches. Also, with Security Extensions, the flag may
416  * not be under control of the kernel; for this reason we ignore the state of
417  * the FIQ disable flag CPSR.F as well.
418  */
419 #define PSR_IGNORE_BITS (PSR_A_BIT | PSR_F_BIT)
420 
421 
422 /*
423  * Macros for defining space directives spread over multiple lines.
424  * These are required so the compiler guesses better the length of inline asm
425  * code and will spill the literal pool early enough to avoid generating PC
426  * relative loads with out of range offsets.
427  */
428 #define TWICE(x)	x x
429 #define SPACE_0x8	TWICE(".space 4\n\t")
430 #define SPACE_0x10	TWICE(SPACE_0x8)
431 #define SPACE_0x20	TWICE(SPACE_0x10)
432 #define SPACE_0x40	TWICE(SPACE_0x20)
433 #define SPACE_0x80	TWICE(SPACE_0x40)
434 #define SPACE_0x100	TWICE(SPACE_0x80)
435 #define SPACE_0x200	TWICE(SPACE_0x100)
436 #define SPACE_0x400	TWICE(SPACE_0x200)
437 #define SPACE_0x800	TWICE(SPACE_0x400)
438 #define SPACE_0x1000	TWICE(SPACE_0x800)
439 
440 
441 /* Various values used in test cases... */
442 #define N(val)	(val ^ 0xffffffff)
443 #define VAL1	0x12345678
444 #define VAL2	N(VAL1)
445 #define VAL3	0xa5f801
446 #define VAL4	N(VAL3)
447 #define VALM	0x456789ab
448 #define VALR	0xdeaddead
449 #define HH1	0x0123fecb
450 #define HH2	0xa9874567
451 
452 
453 #ifdef CONFIG_THUMB2_KERNEL
454 void kprobe_thumb16_test_cases(void);
455 void kprobe_thumb32_test_cases(void);
456 #else
457 void kprobe_arm_test_cases(void);
458 #endif
459