test-core.h 12 KB

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