kprobes-test.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. ".pushsection .rodata \n\t" \
  95. "10: \n\t" \
  96. /* don't use .asciz here as 'title' may be */ \
  97. /* multiple strings to be concatenated. */ \
  98. ".ascii "#title" \n\t" \
  99. ".byte 0 \n\t" \
  100. ".popsection \n\t" \
  101. ".word 10b \n\t"
  102. #define TEST_ARG_REG(reg, val) \
  103. ".byte "__stringify(ARG_TYPE_REG)" \n\t" \
  104. ".byte "#reg" \n\t" \
  105. ".short 0 \n\t" \
  106. ".word "#val" \n\t"
  107. #define TEST_ARG_PTR(reg, val) \
  108. ".byte "__stringify(ARG_TYPE_PTR)" \n\t" \
  109. ".byte "#reg" \n\t" \
  110. ".short 0 \n\t" \
  111. ".word "#val" \n\t"
  112. #define TEST_ARG_MEM(index, val) \
  113. ".byte "__stringify(ARG_TYPE_MEM)" \n\t" \
  114. ".byte "#index" \n\t" \
  115. ".short 0 \n\t" \
  116. ".word "#val" \n\t"
  117. #define TEST_ARG_END(flags) \
  118. ".byte "__stringify(ARG_TYPE_END)" \n\t" \
  119. ".byte "TEST_ISA flags" \n\t" \
  120. ".short 50f-0f \n\t" \
  121. ".short 2f-0f \n\t" \
  122. ".short 99f-0f \n\t" \
  123. ".code "TEST_ISA" \n\t" \
  124. "0: \n\t"
  125. #define TEST_INSTRUCTION(instruction) \
  126. "50: nop \n\t" \
  127. "1: "instruction" \n\t" \
  128. " nop \n\t"
  129. #define TEST_BRANCH_F(instruction) \
  130. TEST_INSTRUCTION(instruction) \
  131. " b 99f \n\t" \
  132. "2: nop \n\t"
  133. #define TEST_BRANCH_B(instruction) \
  134. " b 50f \n\t" \
  135. " b 99f \n\t" \
  136. "2: nop \n\t" \
  137. " b 99f \n\t" \
  138. TEST_INSTRUCTION(instruction)
  139. #define TEST_BRANCH_FX(instruction, codex) \
  140. TEST_INSTRUCTION(instruction) \
  141. " b 99f \n\t" \
  142. codex" \n\t" \
  143. " b 99f \n\t" \
  144. "2: nop \n\t"
  145. #define TEST_BRANCH_BX(instruction, codex) \
  146. " b 50f \n\t" \
  147. " b 99f \n\t" \
  148. "2: nop \n\t" \
  149. " b 99f \n\t" \
  150. codex" \n\t" \
  151. TEST_INSTRUCTION(instruction)
  152. #define TESTCASE_END \
  153. "2: \n\t" \
  154. "99: \n\t" \
  155. " bl __kprobes_test_case_end_"TEST_ISA" \n\t" \
  156. ".code "NORMAL_ISA" \n\t" \
  157. : : \
  158. : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc" \
  159. );
  160. /*
  161. * Macros to define test cases.
  162. *
  163. * Those of the form TEST_{R,P,M}* can be used to define test cases
  164. * which take combinations of the three basic types of arguments. E.g.
  165. *
  166. * TEST_R One register argument
  167. * TEST_RR Two register arguments
  168. * TEST_RPR A register, a pointer, then a register argument
  169. *
  170. * For testing instructions which may branch, there are macros TEST_BF_*
  171. * and TEST_BB_* for branching forwards and backwards.
  172. *
  173. * TEST_SUPPORTED and TEST_UNSUPPORTED don't cause the code to be executed,
  174. * the just verify that a kprobe is or is not allowed on the given instruction.
  175. */
  176. #define TEST(code) \
  177. TESTCASE_START(code) \
  178. TEST_ARG_END("") \
  179. TEST_INSTRUCTION(code) \
  180. TESTCASE_END
  181. #define TEST_UNSUPPORTED(code) \
  182. TESTCASE_START(code) \
  183. TEST_ARG_END("|"__stringify(ARG_FLAG_UNSUPPORTED)) \
  184. TEST_INSTRUCTION(code) \
  185. TESTCASE_END
  186. #define TEST_SUPPORTED(code) \
  187. TESTCASE_START(code) \
  188. TEST_ARG_END("|"__stringify(ARG_FLAG_SUPPORTED)) \
  189. TEST_INSTRUCTION(code) \
  190. TESTCASE_END
  191. #define TEST_R(code1, reg, val, code2) \
  192. TESTCASE_START(code1 #reg code2) \
  193. TEST_ARG_REG(reg, val) \
  194. TEST_ARG_END("") \
  195. TEST_INSTRUCTION(code1 #reg code2) \
  196. TESTCASE_END
  197. #define TEST_RR(code1, reg1, val1, code2, reg2, val2, code3) \
  198. TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
  199. TEST_ARG_REG(reg1, val1) \
  200. TEST_ARG_REG(reg2, val2) \
  201. TEST_ARG_END("") \
  202. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
  203. TESTCASE_END
  204. #define TEST_RRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
  205. TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  206. TEST_ARG_REG(reg1, val1) \
  207. TEST_ARG_REG(reg2, val2) \
  208. TEST_ARG_REG(reg3, val3) \
  209. TEST_ARG_END("") \
  210. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  211. TESTCASE_END
  212. #define TEST_RRRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4, reg4, val4) \
  213. TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4) \
  214. TEST_ARG_REG(reg1, val1) \
  215. TEST_ARG_REG(reg2, val2) \
  216. TEST_ARG_REG(reg3, val3) \
  217. TEST_ARG_REG(reg4, val4) \
  218. TEST_ARG_END("") \
  219. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4) \
  220. TESTCASE_END
  221. #define TEST_P(code1, reg1, val1, code2) \
  222. TESTCASE_START(code1 #reg1 code2) \
  223. TEST_ARG_PTR(reg1, val1) \
  224. TEST_ARG_END("") \
  225. TEST_INSTRUCTION(code1 #reg1 code2) \
  226. TESTCASE_END
  227. #define TEST_PR(code1, reg1, val1, code2, reg2, val2, code3) \
  228. TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
  229. TEST_ARG_PTR(reg1, val1) \
  230. TEST_ARG_REG(reg2, val2) \
  231. TEST_ARG_END("") \
  232. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
  233. TESTCASE_END
  234. #define TEST_RP(code1, reg1, val1, code2, reg2, val2, code3) \
  235. TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
  236. TEST_ARG_REG(reg1, val1) \
  237. TEST_ARG_PTR(reg2, val2) \
  238. TEST_ARG_END("") \
  239. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
  240. TESTCASE_END
  241. #define TEST_PRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
  242. TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  243. TEST_ARG_PTR(reg1, val1) \
  244. TEST_ARG_REG(reg2, val2) \
  245. TEST_ARG_REG(reg3, val3) \
  246. TEST_ARG_END("") \
  247. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  248. TESTCASE_END
  249. #define TEST_RPR(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_PTR(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. #define TEST_RRP(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
  258. TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  259. TEST_ARG_REG(reg1, val1) \
  260. TEST_ARG_REG(reg2, val2) \
  261. TEST_ARG_PTR(reg3, val3) \
  262. TEST_ARG_END("") \
  263. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  264. TESTCASE_END
  265. #define TEST_BF_P(code1, reg1, val1, code2) \
  266. TESTCASE_START(code1 #reg1 code2) \
  267. TEST_ARG_PTR(reg1, val1) \
  268. TEST_ARG_END("") \
  269. TEST_BRANCH_F(code1 #reg1 code2) \
  270. TESTCASE_END
  271. #define TEST_BF(code) \
  272. TESTCASE_START(code) \
  273. TEST_ARG_END("") \
  274. TEST_BRANCH_F(code) \
  275. TESTCASE_END
  276. #define TEST_BB(code) \
  277. TESTCASE_START(code) \
  278. TEST_ARG_END("") \
  279. TEST_BRANCH_B(code) \
  280. TESTCASE_END
  281. #define TEST_BF_R(code1, reg, val, code2) \
  282. TESTCASE_START(code1 #reg code2) \
  283. TEST_ARG_REG(reg, val) \
  284. TEST_ARG_END("") \
  285. TEST_BRANCH_F(code1 #reg code2) \
  286. TESTCASE_END
  287. #define TEST_BB_R(code1, reg, val, code2) \
  288. TESTCASE_START(code1 #reg code2) \
  289. TEST_ARG_REG(reg, val) \
  290. TEST_ARG_END("") \
  291. TEST_BRANCH_B(code1 #reg code2) \
  292. TESTCASE_END
  293. #define TEST_BF_RR(code1, reg1, val1, code2, reg2, val2, code3) \
  294. TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
  295. TEST_ARG_REG(reg1, val1) \
  296. TEST_ARG_REG(reg2, val2) \
  297. TEST_ARG_END("") \
  298. TEST_BRANCH_F(code1 #reg1 code2 #reg2 code3) \
  299. TESTCASE_END
  300. #define TEST_BF_X(code, codex) \
  301. TESTCASE_START(code) \
  302. TEST_ARG_END("") \
  303. TEST_BRANCH_FX(code, codex) \
  304. TESTCASE_END
  305. #define TEST_BB_X(code, codex) \
  306. TESTCASE_START(code) \
  307. TEST_ARG_END("") \
  308. TEST_BRANCH_BX(code, codex) \
  309. TESTCASE_END
  310. #define TEST_BF_RX(code1, reg, val, code2, codex) \
  311. TESTCASE_START(code1 #reg code2) \
  312. TEST_ARG_REG(reg, val) \
  313. TEST_ARG_END("") \
  314. TEST_BRANCH_FX(code1 #reg code2, codex) \
  315. TESTCASE_END
  316. #define TEST_X(code, codex) \
  317. TESTCASE_START(code) \
  318. TEST_ARG_END("") \
  319. TEST_INSTRUCTION(code) \
  320. " b 99f \n\t" \
  321. " "codex" \n\t" \
  322. TESTCASE_END
  323. #define TEST_RX(code1, reg, val, code2, codex) \
  324. TESTCASE_START(code1 #reg code2) \
  325. TEST_ARG_REG(reg, val) \
  326. TEST_ARG_END("") \
  327. TEST_INSTRUCTION(code1 __stringify(reg) code2) \
  328. " b 99f \n\t" \
  329. " "codex" \n\t" \
  330. TESTCASE_END
  331. #define TEST_RRX(code1, reg1, val1, code2, reg2, val2, code3, codex) \
  332. TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
  333. TEST_ARG_REG(reg1, val1) \
  334. TEST_ARG_REG(reg2, val2) \
  335. TEST_ARG_END("") \
  336. TEST_INSTRUCTION(code1 __stringify(reg1) code2 __stringify(reg2) code3) \
  337. " b 99f \n\t" \
  338. " "codex" \n\t" \
  339. TESTCASE_END
  340. /*
  341. * Macros for defining space directives spread over multiple lines.
  342. * These are required so the compiler guesses better the length of inline asm
  343. * code and will spill the literal pool early enough to avoid generating PC
  344. * relative loads with out of range offsets.
  345. */
  346. #define TWICE(x) x x
  347. #define SPACE_0x8 TWICE(".space 4\n\t")
  348. #define SPACE_0x10 TWICE(SPACE_0x8)
  349. #define SPACE_0x20 TWICE(SPACE_0x10)
  350. #define SPACE_0x40 TWICE(SPACE_0x20)
  351. #define SPACE_0x80 TWICE(SPACE_0x40)
  352. #define SPACE_0x100 TWICE(SPACE_0x80)
  353. #define SPACE_0x200 TWICE(SPACE_0x100)
  354. #define SPACE_0x400 TWICE(SPACE_0x200)
  355. #define SPACE_0x800 TWICE(SPACE_0x400)
  356. #define SPACE_0x1000 TWICE(SPACE_0x800)
  357. /* Various values used in test cases... */
  358. #define N(val) (val ^ 0xffffffff)
  359. #define VAL1 0x12345678
  360. #define VAL2 N(VAL1)
  361. #define VAL3 0xa5f801
  362. #define VAL4 N(VAL3)
  363. #define VALM 0x456789ab
  364. #define VALR 0xdeaddead
  365. #define HH1 0x0123fecb
  366. #define HH2 0xa9874567
  367. #ifdef CONFIG_THUMB2_KERNEL
  368. void kprobe_thumb16_test_cases(void);
  369. void kprobe_thumb32_test_cases(void);
  370. #else
  371. void kprobe_arm_test_cases(void);
  372. #endif