Linux Audio

Check our new training course

In-person Linux kernel drivers training

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