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