kprobes-test.h 12 KB

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