kprobes-test.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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, xtra_dist) \
  127. TEST_INSTRUCTION(instruction) \
  128. ".if "#xtra_dist" \n\t" \
  129. " b 99f \n\t" \
  130. ".space "#xtra_dist" \n\t" \
  131. ".endif \n\t" \
  132. " b 99f \n\t" \
  133. "2: nop \n\t"
  134. #define TEST_BRANCH_B(instruction, xtra_dist) \
  135. " b 50f \n\t" \
  136. " b 99f \n\t" \
  137. "2: nop \n\t" \
  138. " b 99f \n\t" \
  139. ".if "#xtra_dist" \n\t" \
  140. ".space "#xtra_dist" \n\t" \
  141. ".endif \n\t" \
  142. TEST_INSTRUCTION(instruction)
  143. #define TESTCASE_END \
  144. "2: \n\t" \
  145. "99: \n\t" \
  146. " bl __kprobes_test_case_end_"TEST_ISA" \n\t" \
  147. ".code "NORMAL_ISA" \n\t" \
  148. : : \
  149. : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc" \
  150. );
  151. /*
  152. * Macros to define test cases.
  153. *
  154. * Those of the form TEST_{R,P,M}* can be used to define test cases
  155. * which take combinations of the three basic types of arguments. E.g.
  156. *
  157. * TEST_R One register argument
  158. * TEST_RR Two register arguments
  159. * TEST_RPR A register, a pointer, then a register argument
  160. *
  161. * For testing instructions which may branch, there are macros TEST_BF_*
  162. * and TEST_BB_* for branching forwards and backwards.
  163. *
  164. * TEST_SUPPORTED and TEST_UNSUPPORTED don't cause the code to be executed,
  165. * the just verify that a kprobe is or is not allowed on the given instruction.
  166. */
  167. #define TEST(code) \
  168. TESTCASE_START(code) \
  169. TEST_ARG_END("") \
  170. TEST_INSTRUCTION(code) \
  171. TESTCASE_END
  172. #define TEST_UNSUPPORTED(code) \
  173. TESTCASE_START(code) \
  174. TEST_ARG_END("|"__stringify(ARG_FLAG_UNSUPPORTED)) \
  175. TEST_INSTRUCTION(code) \
  176. TESTCASE_END
  177. #define TEST_SUPPORTED(code) \
  178. TESTCASE_START(code) \
  179. TEST_ARG_END("|"__stringify(ARG_FLAG_SUPPORTED)) \
  180. TEST_INSTRUCTION(code) \
  181. TESTCASE_END
  182. #define TEST_R(code1, reg, val, code2) \
  183. TESTCASE_START(code1 #reg code2) \
  184. TEST_ARG_REG(reg, val) \
  185. TEST_ARG_END("") \
  186. TEST_INSTRUCTION(code1 #reg code2) \
  187. TESTCASE_END
  188. #define TEST_RR(code1, reg1, val1, code2, reg2, val2, code3) \
  189. TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
  190. TEST_ARG_REG(reg1, val1) \
  191. TEST_ARG_REG(reg2, val2) \
  192. TEST_ARG_END("") \
  193. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
  194. TESTCASE_END
  195. #define TEST_RRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
  196. TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  197. TEST_ARG_REG(reg1, val1) \
  198. TEST_ARG_REG(reg2, val2) \
  199. TEST_ARG_REG(reg3, val3) \
  200. TEST_ARG_END("") \
  201. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  202. TESTCASE_END
  203. #define TEST_RRRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4, reg4, val4) \
  204. TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4) \
  205. TEST_ARG_REG(reg1, val1) \
  206. TEST_ARG_REG(reg2, val2) \
  207. TEST_ARG_REG(reg3, val3) \
  208. TEST_ARG_REG(reg4, val4) \
  209. TEST_ARG_END("") \
  210. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4 #reg4) \
  211. TESTCASE_END
  212. #define TEST_P(code1, reg1, val1, code2) \
  213. TESTCASE_START(code1 #reg1 code2) \
  214. TEST_ARG_PTR(reg1, val1) \
  215. TEST_ARG_END("") \
  216. TEST_INSTRUCTION(code1 #reg1 code2) \
  217. TESTCASE_END
  218. #define TEST_PR(code1, reg1, val1, code2, reg2, val2, code3) \
  219. TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
  220. TEST_ARG_PTR(reg1, val1) \
  221. TEST_ARG_REG(reg2, val2) \
  222. TEST_ARG_END("") \
  223. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
  224. TESTCASE_END
  225. #define TEST_RP(code1, reg1, val1, code2, reg2, val2, code3) \
  226. TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
  227. TEST_ARG_REG(reg1, val1) \
  228. TEST_ARG_PTR(reg2, val2) \
  229. TEST_ARG_END("") \
  230. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3) \
  231. TESTCASE_END
  232. #define TEST_PRR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
  233. TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  234. TEST_ARG_PTR(reg1, val1) \
  235. TEST_ARG_REG(reg2, val2) \
  236. TEST_ARG_REG(reg3, val3) \
  237. TEST_ARG_END("") \
  238. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  239. TESTCASE_END
  240. #define TEST_RPR(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
  241. TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  242. TEST_ARG_REG(reg1, val1) \
  243. TEST_ARG_PTR(reg2, val2) \
  244. TEST_ARG_REG(reg3, val3) \
  245. TEST_ARG_END("") \
  246. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  247. TESTCASE_END
  248. #define TEST_RRP(code1, reg1, val1, code2, reg2, val2, code3, reg3, val3, code4)\
  249. TESTCASE_START(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  250. TEST_ARG_REG(reg1, val1) \
  251. TEST_ARG_REG(reg2, val2) \
  252. TEST_ARG_PTR(reg3, val3) \
  253. TEST_ARG_END("") \
  254. TEST_INSTRUCTION(code1 #reg1 code2 #reg2 code3 #reg3 code4) \
  255. TESTCASE_END
  256. #define TEST_BF_P(code1, reg1, val1, code2) \
  257. TESTCASE_START(code1 #reg1 code2) \
  258. TEST_ARG_PTR(reg1, val1) \
  259. TEST_ARG_END("") \
  260. TEST_BRANCH_F(code1 #reg1 code2, 0) \
  261. TESTCASE_END
  262. #define TEST_BF_X(code, xtra_dist) \
  263. TESTCASE_START(code) \
  264. TEST_ARG_END("") \
  265. TEST_BRANCH_F(code, xtra_dist) \
  266. TESTCASE_END
  267. #define TEST_BB_X(code, xtra_dist) \
  268. TESTCASE_START(code) \
  269. TEST_ARG_END("") \
  270. TEST_BRANCH_B(code, xtra_dist) \
  271. TESTCASE_END
  272. #define TEST_BF_RX(code1, reg, val, code2, xtra_dist) \
  273. TESTCASE_START(code1 #reg code2) \
  274. TEST_ARG_REG(reg, val) \
  275. TEST_ARG_END("") \
  276. TEST_BRANCH_F(code1 #reg code2, xtra_dist) \
  277. TESTCASE_END
  278. #define TEST_BB_RX(code1, reg, val, code2, xtra_dist) \
  279. TESTCASE_START(code1 #reg code2) \
  280. TEST_ARG_REG(reg, val) \
  281. TEST_ARG_END("") \
  282. TEST_BRANCH_B(code1 #reg code2, xtra_dist) \
  283. TESTCASE_END
  284. #define TEST_BF(code) TEST_BF_X(code, 0)
  285. #define TEST_BB(code) TEST_BB_X(code, 0)
  286. #define TEST_BF_R(code1, reg, val, code2) TEST_BF_RX(code1, reg, val, code2, 0)
  287. #define TEST_BB_R(code1, reg, val, code2) TEST_BB_RX(code1, reg, val, code2, 0)
  288. #define TEST_BF_RR(code1, reg1, val1, code2, reg2, val2, code3) \
  289. TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
  290. TEST_ARG_REG(reg1, val1) \
  291. TEST_ARG_REG(reg2, val2) \
  292. TEST_ARG_END("") \
  293. TEST_BRANCH_F(code1 #reg1 code2 #reg2 code3, 0) \
  294. TESTCASE_END
  295. #define TEST_X(code, codex) \
  296. TESTCASE_START(code) \
  297. TEST_ARG_END("") \
  298. TEST_INSTRUCTION(code) \
  299. " b 99f \n\t" \
  300. " "codex" \n\t" \
  301. TESTCASE_END
  302. #define TEST_RX(code1, reg, val, code2, codex) \
  303. TESTCASE_START(code1 #reg code2) \
  304. TEST_ARG_REG(reg, val) \
  305. TEST_ARG_END("") \
  306. TEST_INSTRUCTION(code1 __stringify(reg) code2) \
  307. " b 99f \n\t" \
  308. " "codex" \n\t" \
  309. TESTCASE_END
  310. #define TEST_RRX(code1, reg1, val1, code2, reg2, val2, code3, codex) \
  311. TESTCASE_START(code1 #reg1 code2 #reg2 code3) \
  312. TEST_ARG_REG(reg1, val1) \
  313. TEST_ARG_REG(reg2, val2) \
  314. TEST_ARG_END("") \
  315. TEST_INSTRUCTION(code1 __stringify(reg1) code2 __stringify(reg2) code3) \
  316. " b 99f \n\t" \
  317. " "codex" \n\t" \
  318. TESTCASE_END
  319. /* Various values used in test cases... */
  320. #define N(val) (val ^ 0xffffffff)
  321. #define VAL1 0x12345678
  322. #define VAL2 N(VAL1)
  323. #define VAL3 0xa5f801
  324. #define VAL4 N(VAL3)
  325. #define VALM 0x456789ab
  326. #define VALR 0xdeaddead
  327. #define HH1 0x0123fecb
  328. #define HH2 0xa9874567
  329. #ifdef CONFIG_THUMB2_KERNEL
  330. void kprobe_thumb16_test_cases(void);
  331. void kprobe_thumb32_test_cases(void);
  332. #else
  333. void kprobe_arm_test_cases(void);
  334. #endif