kprobes-test.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. /*
  2. * arch/arm/kernel/kprobes-test.c
  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. * This file contains test code for ARM kprobes.
  12. *
  13. * The top level function run_all_tests() executes tests for all of the
  14. * supported instruction sets: ARM, 16-bit Thumb, and 32-bit Thumb. These tests
  15. * fall into two categories; run_api_tests() checks basic functionality of the
  16. * kprobes API, and run_test_cases() is a comprehensive test for kprobes
  17. * instruction decoding and simulation.
  18. *
  19. * run_test_cases() first checks the kprobes decoding table for self consistency
  20. * (using table_test()) then executes a series of test cases for each of the CPU
  21. * instruction forms. coverage_start() and coverage_end() are used to verify
  22. * that these test cases cover all of the possible combinations of instructions
  23. * described by the kprobes decoding tables.
  24. *
  25. * The individual test cases are in kprobes-test-arm.c and kprobes-test-thumb.c
  26. * which use the macros defined in kprobes-test.h. The rest of this
  27. * documentation will describe the operation of the framework used by these
  28. * test cases.
  29. */
  30. /*
  31. * TESTING METHODOLOGY
  32. * -------------------
  33. *
  34. * The methodology used to test an ARM instruction 'test_insn' is to use
  35. * inline assembler like:
  36. *
  37. * test_before: nop
  38. * test_case: test_insn
  39. * test_after: nop
  40. *
  41. * When the test case is run a kprobe is placed of each nop. The
  42. * post-handler of the test_before probe is used to modify the saved CPU
  43. * register context to that which we require for the test case. The
  44. * pre-handler of the of the test_after probe saves a copy of the CPU
  45. * register context. In this way we can execute test_insn with a specific
  46. * register context and see the results afterwards.
  47. *
  48. * To actually test the kprobes instruction emulation we perform the above
  49. * step a second time but with an additional kprobe on the test_case
  50. * instruction itself. If the emulation is accurate then the results seen
  51. * by the test_after probe will be identical to the first run which didn't
  52. * have a probe on test_case.
  53. *
  54. * Each test case is run several times with a variety of variations in the
  55. * flags value of stored in CPSR, and for Thumb code, different ITState.
  56. *
  57. * For instructions which can modify PC, a second test_after probe is used
  58. * like this:
  59. *
  60. * test_before: nop
  61. * test_case: test_insn
  62. * test_after: nop
  63. * b test_done
  64. * test_after2: nop
  65. * test_done:
  66. *
  67. * The test case is constructed such that test_insn branches to
  68. * test_after2, or, if testing a conditional instruction, it may just
  69. * continue to test_after. The probes inserted at both locations let us
  70. * determine which happened. A similar approach is used for testing
  71. * backwards branches...
  72. *
  73. * b test_before
  74. * b test_done @ helps to cope with off by 1 branches
  75. * test_after2: nop
  76. * b test_done
  77. * test_before: nop
  78. * test_case: test_insn
  79. * test_after: nop
  80. * test_done:
  81. *
  82. * The macros used to generate the assembler instructions describe above
  83. * are TEST_INSTRUCTION, TEST_BRANCH_F (branch forwards) and TEST_BRANCH_B
  84. * (branch backwards). In these, the local variables numbered 1, 50, 2 and
  85. * 99 represent: test_before, test_case, test_after2 and test_done.
  86. *
  87. * FRAMEWORK
  88. * ---------
  89. *
  90. * Each test case is wrapped between the pair of macros TESTCASE_START and
  91. * TESTCASE_END. As well as performing the inline assembler boilerplate,
  92. * these call out to the kprobes_test_case_start() and
  93. * kprobes_test_case_end() functions which drive the execution of the test
  94. * case. The specific arguments to use for each test case are stored as
  95. * inline data constructed using the various TEST_ARG_* macros. Putting
  96. * this all together, a simple test case may look like:
  97. *
  98. * TESTCASE_START("Testing mov r0, r7")
  99. * TEST_ARG_REG(7, 0x12345678) // Set r7=0x12345678
  100. * TEST_ARG_END("")
  101. * TEST_INSTRUCTION("mov r0, r7")
  102. * TESTCASE_END
  103. *
  104. * Note, in practice the single convenience macro TEST_R would be used for this
  105. * instead.
  106. *
  107. * The above would expand to assembler looking something like:
  108. *
  109. * @ TESTCASE_START
  110. * bl __kprobes_test_case_start
  111. * @ start of inline data...
  112. * .ascii "mov r0, r7" @ text title for test case
  113. * .byte 0
  114. * .align 2, 0
  115. *
  116. * @ TEST_ARG_REG
  117. * .byte ARG_TYPE_REG
  118. * .byte 7
  119. * .short 0
  120. * .word 0x1234567
  121. *
  122. * @ TEST_ARG_END
  123. * .byte ARG_TYPE_END
  124. * .byte TEST_ISA @ flags, including ISA being tested
  125. * .short 50f-0f @ offset of 'test_before'
  126. * .short 2f-0f @ offset of 'test_after2' (if relevent)
  127. * .short 99f-0f @ offset of 'test_done'
  128. * @ start of test case code...
  129. * 0:
  130. * .code TEST_ISA @ switch to ISA being tested
  131. *
  132. * @ TEST_INSTRUCTION
  133. * 50: nop @ location for 'test_before' probe
  134. * 1: mov r0, r7 @ the test case instruction 'test_insn'
  135. * nop @ location for 'test_after' probe
  136. *
  137. * // TESTCASE_END
  138. * 2:
  139. * 99: bl __kprobes_test_case_end_##TEST_ISA
  140. * .code NONMAL_ISA
  141. *
  142. * When the above is execute the following happens...
  143. *
  144. * __kprobes_test_case_start() is an assembler wrapper which sets up space
  145. * for a stack buffer and calls the C function kprobes_test_case_start().
  146. * This C function will do some initial processing of the inline data and
  147. * setup some global state. It then inserts the test_before and test_after
  148. * kprobes and returns a value which causes the assembler wrapper to jump
  149. * to the start of the test case code, (local label '0').
  150. *
  151. * When the test case code executes, the test_before probe will be hit and
  152. * test_before_post_handler will call setup_test_context(). This fills the
  153. * stack buffer and CPU registers with a test pattern and then processes
  154. * the test case arguments. In our example there is one TEST_ARG_REG which
  155. * indicates that R7 should be loaded with the value 0x12345678.
  156. *
  157. * When the test_before probe ends, the test case continues and executes
  158. * the "mov r0, r7" instruction. It then hits the test_after probe and the
  159. * pre-handler for this (test_after_pre_handler) will save a copy of the
  160. * CPU register context. This should now have R0 holding the same value as
  161. * R7.
  162. *
  163. * Finally we get to the call to __kprobes_test_case_end_{32,16}. This is
  164. * an assembler wrapper which switches back to the ISA used by the test
  165. * code and calls the C function kprobes_test_case_end().
  166. *
  167. * For each run through the test case, test_case_run_count is incremented
  168. * by one. For even runs, kprobes_test_case_end() saves a copy of the
  169. * register and stack buffer contents from the test case just run. It then
  170. * inserts a kprobe on the test case instruction 'test_insn' and returns a
  171. * value to cause the test case code to be re-run.
  172. *
  173. * For odd numbered runs, kprobes_test_case_end() compares the register and
  174. * stack buffer contents to those that were saved on the previous even
  175. * numbered run (the one without the kprobe on test_insn). These should be
  176. * the same if the kprobe instruction simulation routine is correct.
  177. *
  178. * The pair of test case runs is repeated with different combinations of
  179. * flag values in CPSR and, for Thumb, different ITState. This is
  180. * controlled by test_context_cpsr().
  181. *
  182. * BUILDING TEST CASES
  183. * -------------------
  184. *
  185. *
  186. * As an aid to building test cases, the stack buffer is initialised with
  187. * some special values:
  188. *
  189. * [SP+13*4] Contains SP+120. This can be used to test instructions
  190. * which load a value into SP.
  191. *
  192. * [SP+15*4] When testing branching instructions using TEST_BRANCH_{F,B},
  193. * this holds the target address of the branch, 'test_after2'.
  194. * This can be used to test instructions which load a PC value
  195. * from memory.
  196. */
  197. #include <linux/kernel.h>
  198. #include <linux/module.h>
  199. #include <linux/slab.h>
  200. #include <linux/kprobes.h>
  201. #include <linux/errno.h>
  202. #include <linux/stddef.h>
  203. #include <linux/bug.h>
  204. #include <asm/opcodes.h>
  205. #include "kprobes.h"
  206. #include "probes-arm.h"
  207. #include "probes-thumb.h"
  208. #include "kprobes-test.h"
  209. #define BENCHMARKING 1
  210. /*
  211. * Test basic API
  212. */
  213. static bool test_regs_ok;
  214. static int test_func_instance;
  215. static int pre_handler_called;
  216. static int post_handler_called;
  217. static int jprobe_func_called;
  218. static int kretprobe_handler_called;
  219. #define FUNC_ARG1 0x12345678
  220. #define FUNC_ARG2 0xabcdef
  221. #ifndef CONFIG_THUMB2_KERNEL
  222. long arm_func(long r0, long r1);
  223. static void __used __naked __arm_kprobes_test_func(void)
  224. {
  225. __asm__ __volatile__ (
  226. ".arm \n\t"
  227. ".type arm_func, %%function \n\t"
  228. "arm_func: \n\t"
  229. "adds r0, r0, r1 \n\t"
  230. "bx lr \n\t"
  231. ".code "NORMAL_ISA /* Back to Thumb if necessary */
  232. : : : "r0", "r1", "cc"
  233. );
  234. }
  235. #else /* CONFIG_THUMB2_KERNEL */
  236. long thumb16_func(long r0, long r1);
  237. long thumb32even_func(long r0, long r1);
  238. long thumb32odd_func(long r0, long r1);
  239. static void __used __naked __thumb_kprobes_test_funcs(void)
  240. {
  241. __asm__ __volatile__ (
  242. ".type thumb16_func, %%function \n\t"
  243. "thumb16_func: \n\t"
  244. "adds.n r0, r0, r1 \n\t"
  245. "bx lr \n\t"
  246. ".align \n\t"
  247. ".type thumb32even_func, %%function \n\t"
  248. "thumb32even_func: \n\t"
  249. "adds.w r0, r0, r1 \n\t"
  250. "bx lr \n\t"
  251. ".align \n\t"
  252. "nop.n \n\t"
  253. ".type thumb32odd_func, %%function \n\t"
  254. "thumb32odd_func: \n\t"
  255. "adds.w r0, r0, r1 \n\t"
  256. "bx lr \n\t"
  257. : : : "r0", "r1", "cc"
  258. );
  259. }
  260. #endif /* CONFIG_THUMB2_KERNEL */
  261. static int call_test_func(long (*func)(long, long), bool check_test_regs)
  262. {
  263. long ret;
  264. ++test_func_instance;
  265. test_regs_ok = false;
  266. ret = (*func)(FUNC_ARG1, FUNC_ARG2);
  267. if (ret != FUNC_ARG1 + FUNC_ARG2) {
  268. pr_err("FAIL: call_test_func: func returned %lx\n", ret);
  269. return false;
  270. }
  271. if (check_test_regs && !test_regs_ok) {
  272. pr_err("FAIL: test regs not OK\n");
  273. return false;
  274. }
  275. return true;
  276. }
  277. static int __kprobes pre_handler(struct kprobe *p, struct pt_regs *regs)
  278. {
  279. pre_handler_called = test_func_instance;
  280. if (regs->ARM_r0 == FUNC_ARG1 && regs->ARM_r1 == FUNC_ARG2)
  281. test_regs_ok = true;
  282. return 0;
  283. }
  284. static void __kprobes post_handler(struct kprobe *p, struct pt_regs *regs,
  285. unsigned long flags)
  286. {
  287. post_handler_called = test_func_instance;
  288. if (regs->ARM_r0 != FUNC_ARG1 + FUNC_ARG2 || regs->ARM_r1 != FUNC_ARG2)
  289. test_regs_ok = false;
  290. }
  291. static struct kprobe the_kprobe = {
  292. .addr = 0,
  293. .pre_handler = pre_handler,
  294. .post_handler = post_handler
  295. };
  296. static int test_kprobe(long (*func)(long, long))
  297. {
  298. int ret;
  299. the_kprobe.addr = (kprobe_opcode_t *)func;
  300. ret = register_kprobe(&the_kprobe);
  301. if (ret < 0) {
  302. pr_err("FAIL: register_kprobe failed with %d\n", ret);
  303. return ret;
  304. }
  305. ret = call_test_func(func, true);
  306. unregister_kprobe(&the_kprobe);
  307. the_kprobe.flags = 0; /* Clear disable flag to allow reuse */
  308. if (!ret)
  309. return -EINVAL;
  310. if (pre_handler_called != test_func_instance) {
  311. pr_err("FAIL: kprobe pre_handler not called\n");
  312. return -EINVAL;
  313. }
  314. if (post_handler_called != test_func_instance) {
  315. pr_err("FAIL: kprobe post_handler not called\n");
  316. return -EINVAL;
  317. }
  318. if (!call_test_func(func, false))
  319. return -EINVAL;
  320. if (pre_handler_called == test_func_instance ||
  321. post_handler_called == test_func_instance) {
  322. pr_err("FAIL: probe called after unregistering\n");
  323. return -EINVAL;
  324. }
  325. return 0;
  326. }
  327. static void __kprobes jprobe_func(long r0, long r1)
  328. {
  329. jprobe_func_called = test_func_instance;
  330. if (r0 == FUNC_ARG1 && r1 == FUNC_ARG2)
  331. test_regs_ok = true;
  332. jprobe_return();
  333. }
  334. static struct jprobe the_jprobe = {
  335. .entry = jprobe_func,
  336. };
  337. static int test_jprobe(long (*func)(long, long))
  338. {
  339. int ret;
  340. the_jprobe.kp.addr = (kprobe_opcode_t *)func;
  341. ret = register_jprobe(&the_jprobe);
  342. if (ret < 0) {
  343. pr_err("FAIL: register_jprobe failed with %d\n", ret);
  344. return ret;
  345. }
  346. ret = call_test_func(func, true);
  347. unregister_jprobe(&the_jprobe);
  348. the_jprobe.kp.flags = 0; /* Clear disable flag to allow reuse */
  349. if (!ret)
  350. return -EINVAL;
  351. if (jprobe_func_called != test_func_instance) {
  352. pr_err("FAIL: jprobe handler function not called\n");
  353. return -EINVAL;
  354. }
  355. if (!call_test_func(func, false))
  356. return -EINVAL;
  357. if (jprobe_func_called == test_func_instance) {
  358. pr_err("FAIL: probe called after unregistering\n");
  359. return -EINVAL;
  360. }
  361. return 0;
  362. }
  363. static int __kprobes
  364. kretprobe_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
  365. {
  366. kretprobe_handler_called = test_func_instance;
  367. if (regs_return_value(regs) == FUNC_ARG1 + FUNC_ARG2)
  368. test_regs_ok = true;
  369. return 0;
  370. }
  371. static struct kretprobe the_kretprobe = {
  372. .handler = kretprobe_handler,
  373. };
  374. static int test_kretprobe(long (*func)(long, long))
  375. {
  376. int ret;
  377. the_kretprobe.kp.addr = (kprobe_opcode_t *)func;
  378. ret = register_kretprobe(&the_kretprobe);
  379. if (ret < 0) {
  380. pr_err("FAIL: register_kretprobe failed with %d\n", ret);
  381. return ret;
  382. }
  383. ret = call_test_func(func, true);
  384. unregister_kretprobe(&the_kretprobe);
  385. the_kretprobe.kp.flags = 0; /* Clear disable flag to allow reuse */
  386. if (!ret)
  387. return -EINVAL;
  388. if (kretprobe_handler_called != test_func_instance) {
  389. pr_err("FAIL: kretprobe handler not called\n");
  390. return -EINVAL;
  391. }
  392. if (!call_test_func(func, false))
  393. return -EINVAL;
  394. if (jprobe_func_called == test_func_instance) {
  395. pr_err("FAIL: kretprobe called after unregistering\n");
  396. return -EINVAL;
  397. }
  398. return 0;
  399. }
  400. static int run_api_tests(long (*func)(long, long))
  401. {
  402. int ret;
  403. pr_info(" kprobe\n");
  404. ret = test_kprobe(func);
  405. if (ret < 0)
  406. return ret;
  407. pr_info(" jprobe\n");
  408. ret = test_jprobe(func);
  409. if (ret < 0)
  410. return ret;
  411. pr_info(" kretprobe\n");
  412. ret = test_kretprobe(func);
  413. if (ret < 0)
  414. return ret;
  415. return 0;
  416. }
  417. /*
  418. * Benchmarking
  419. */
  420. #if BENCHMARKING
  421. static void __naked benchmark_nop(void)
  422. {
  423. __asm__ __volatile__ (
  424. "nop \n\t"
  425. "bx lr"
  426. );
  427. }
  428. #ifdef CONFIG_THUMB2_KERNEL
  429. #define wide ".w"
  430. #else
  431. #define wide
  432. #endif
  433. static void __naked benchmark_pushpop1(void)
  434. {
  435. __asm__ __volatile__ (
  436. "stmdb"wide" sp!, {r3-r11,lr} \n\t"
  437. "ldmia"wide" sp!, {r3-r11,pc}"
  438. );
  439. }
  440. static void __naked benchmark_pushpop2(void)
  441. {
  442. __asm__ __volatile__ (
  443. "stmdb"wide" sp!, {r0-r8,lr} \n\t"
  444. "ldmia"wide" sp!, {r0-r8,pc}"
  445. );
  446. }
  447. static void __naked benchmark_pushpop3(void)
  448. {
  449. __asm__ __volatile__ (
  450. "stmdb"wide" sp!, {r4,lr} \n\t"
  451. "ldmia"wide" sp!, {r4,pc}"
  452. );
  453. }
  454. static void __naked benchmark_pushpop4(void)
  455. {
  456. __asm__ __volatile__ (
  457. "stmdb"wide" sp!, {r0,lr} \n\t"
  458. "ldmia"wide" sp!, {r0,pc}"
  459. );
  460. }
  461. #ifdef CONFIG_THUMB2_KERNEL
  462. static void __naked benchmark_pushpop_thumb(void)
  463. {
  464. __asm__ __volatile__ (
  465. "push.n {r0-r7,lr} \n\t"
  466. "pop.n {r0-r7,pc}"
  467. );
  468. }
  469. #endif
  470. static int __kprobes
  471. benchmark_pre_handler(struct kprobe *p, struct pt_regs *regs)
  472. {
  473. return 0;
  474. }
  475. static int benchmark(void(*fn)(void))
  476. {
  477. unsigned n, i, t, t0;
  478. for (n = 1000; ; n *= 2) {
  479. t0 = sched_clock();
  480. for (i = n; i > 0; --i)
  481. fn();
  482. t = sched_clock() - t0;
  483. if (t >= 250000000)
  484. break; /* Stop once we took more than 0.25 seconds */
  485. }
  486. return t / n; /* Time for one iteration in nanoseconds */
  487. };
  488. static int kprobe_benchmark(void(*fn)(void), unsigned offset)
  489. {
  490. struct kprobe k = {
  491. .addr = (kprobe_opcode_t *)((uintptr_t)fn + offset),
  492. .pre_handler = benchmark_pre_handler,
  493. };
  494. int ret = register_kprobe(&k);
  495. if (ret < 0) {
  496. pr_err("FAIL: register_kprobe failed with %d\n", ret);
  497. return ret;
  498. }
  499. ret = benchmark(fn);
  500. unregister_kprobe(&k);
  501. return ret;
  502. };
  503. struct benchmarks {
  504. void (*fn)(void);
  505. unsigned offset;
  506. const char *title;
  507. };
  508. static int run_benchmarks(void)
  509. {
  510. int ret;
  511. struct benchmarks list[] = {
  512. {&benchmark_nop, 0, "nop"},
  513. /*
  514. * benchmark_pushpop{1,3} will have the optimised
  515. * instruction emulation, whilst benchmark_pushpop{2,4} will
  516. * be the equivalent unoptimised instructions.
  517. */
  518. {&benchmark_pushpop1, 0, "stmdb sp!, {r3-r11,lr}"},
  519. {&benchmark_pushpop1, 4, "ldmia sp!, {r3-r11,pc}"},
  520. {&benchmark_pushpop2, 0, "stmdb sp!, {r0-r8,lr}"},
  521. {&benchmark_pushpop2, 4, "ldmia sp!, {r0-r8,pc}"},
  522. {&benchmark_pushpop3, 0, "stmdb sp!, {r4,lr}"},
  523. {&benchmark_pushpop3, 4, "ldmia sp!, {r4,pc}"},
  524. {&benchmark_pushpop4, 0, "stmdb sp!, {r0,lr}"},
  525. {&benchmark_pushpop4, 4, "ldmia sp!, {r0,pc}"},
  526. #ifdef CONFIG_THUMB2_KERNEL
  527. {&benchmark_pushpop_thumb, 0, "push.n {r0-r7,lr}"},
  528. {&benchmark_pushpop_thumb, 2, "pop.n {r0-r7,pc}"},
  529. #endif
  530. {0}
  531. };
  532. struct benchmarks *b;
  533. for (b = list; b->fn; ++b) {
  534. ret = kprobe_benchmark(b->fn, b->offset);
  535. if (ret < 0)
  536. return ret;
  537. pr_info(" %dns for kprobe %s\n", ret, b->title);
  538. }
  539. pr_info("\n");
  540. return 0;
  541. }
  542. #endif /* BENCHMARKING */
  543. /*
  544. * Decoding table self-consistency tests
  545. */
  546. static const int decode_struct_sizes[NUM_DECODE_TYPES] = {
  547. [DECODE_TYPE_TABLE] = sizeof(struct decode_table),
  548. [DECODE_TYPE_CUSTOM] = sizeof(struct decode_custom),
  549. [DECODE_TYPE_SIMULATE] = sizeof(struct decode_simulate),
  550. [DECODE_TYPE_EMULATE] = sizeof(struct decode_emulate),
  551. [DECODE_TYPE_OR] = sizeof(struct decode_or),
  552. [DECODE_TYPE_REJECT] = sizeof(struct decode_reject)
  553. };
  554. static int table_iter(const union decode_item *table,
  555. int (*fn)(const struct decode_header *, void *),
  556. void *args)
  557. {
  558. const struct decode_header *h = (struct decode_header *)table;
  559. int result;
  560. for (;;) {
  561. enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
  562. if (type == DECODE_TYPE_END)
  563. return 0;
  564. result = fn(h, args);
  565. if (result)
  566. return result;
  567. h = (struct decode_header *)
  568. ((uintptr_t)h + decode_struct_sizes[type]);
  569. }
  570. }
  571. static int table_test_fail(const struct decode_header *h, const char* message)
  572. {
  573. pr_err("FAIL: kprobes test failure \"%s\" (mask %08x, value %08x)\n",
  574. message, h->mask.bits, h->value.bits);
  575. return -EINVAL;
  576. }
  577. struct table_test_args {
  578. const union decode_item *root_table;
  579. u32 parent_mask;
  580. u32 parent_value;
  581. };
  582. static int table_test_fn(const struct decode_header *h, void *args)
  583. {
  584. struct table_test_args *a = (struct table_test_args *)args;
  585. enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
  586. if (h->value.bits & ~h->mask.bits)
  587. return table_test_fail(h, "Match value has bits not in mask");
  588. if ((h->mask.bits & a->parent_mask) != a->parent_mask)
  589. return table_test_fail(h, "Mask has bits not in parent mask");
  590. if ((h->value.bits ^ a->parent_value) & a->parent_mask)
  591. return table_test_fail(h, "Value is inconsistent with parent");
  592. if (type == DECODE_TYPE_TABLE) {
  593. struct decode_table *d = (struct decode_table *)h;
  594. struct table_test_args args2 = *a;
  595. args2.parent_mask = h->mask.bits;
  596. args2.parent_value = h->value.bits;
  597. return table_iter(d->table.table, table_test_fn, &args2);
  598. }
  599. return 0;
  600. }
  601. static int table_test(const union decode_item *table)
  602. {
  603. struct table_test_args args = {
  604. .root_table = table,
  605. .parent_mask = 0,
  606. .parent_value = 0
  607. };
  608. return table_iter(args.root_table, table_test_fn, &args);
  609. }
  610. /*
  611. * Decoding table test coverage analysis
  612. *
  613. * coverage_start() builds a coverage_table which contains a list of
  614. * coverage_entry's to match each entry in the specified kprobes instruction
  615. * decoding table.
  616. *
  617. * When test cases are run, coverage_add() is called to process each case.
  618. * This looks up the corresponding entry in the coverage_table and sets it as
  619. * being matched, as well as clearing the regs flag appropriate for the test.
  620. *
  621. * After all test cases have been run, coverage_end() is called to check that
  622. * all entries in coverage_table have been matched and that all regs flags are
  623. * cleared. I.e. that all possible combinations of instructions described by
  624. * the kprobes decoding tables have had a test case executed for them.
  625. */
  626. bool coverage_fail;
  627. #define MAX_COVERAGE_ENTRIES 256
  628. struct coverage_entry {
  629. const struct decode_header *header;
  630. unsigned regs;
  631. unsigned nesting;
  632. char matched;
  633. };
  634. struct coverage_table {
  635. struct coverage_entry *base;
  636. unsigned num_entries;
  637. unsigned nesting;
  638. };
  639. struct coverage_table coverage;
  640. #define COVERAGE_ANY_REG (1<<0)
  641. #define COVERAGE_SP (1<<1)
  642. #define COVERAGE_PC (1<<2)
  643. #define COVERAGE_PCWB (1<<3)
  644. static const char coverage_register_lookup[16] = {
  645. [REG_TYPE_ANY] = COVERAGE_ANY_REG | COVERAGE_SP | COVERAGE_PC,
  646. [REG_TYPE_SAMEAS16] = COVERAGE_ANY_REG,
  647. [REG_TYPE_SP] = COVERAGE_SP,
  648. [REG_TYPE_PC] = COVERAGE_PC,
  649. [REG_TYPE_NOSP] = COVERAGE_ANY_REG | COVERAGE_SP,
  650. [REG_TYPE_NOSPPC] = COVERAGE_ANY_REG | COVERAGE_SP | COVERAGE_PC,
  651. [REG_TYPE_NOPC] = COVERAGE_ANY_REG | COVERAGE_PC,
  652. [REG_TYPE_NOPCWB] = COVERAGE_ANY_REG | COVERAGE_PC | COVERAGE_PCWB,
  653. [REG_TYPE_NOPCX] = COVERAGE_ANY_REG,
  654. [REG_TYPE_NOSPPCX] = COVERAGE_ANY_REG | COVERAGE_SP,
  655. };
  656. unsigned coverage_start_registers(const struct decode_header *h)
  657. {
  658. unsigned regs = 0;
  659. int i;
  660. for (i = 0; i < 20; i += 4) {
  661. int r = (h->type_regs.bits >> (DECODE_TYPE_BITS + i)) & 0xf;
  662. regs |= coverage_register_lookup[r] << i;
  663. }
  664. return regs;
  665. }
  666. static int coverage_start_fn(const struct decode_header *h, void *args)
  667. {
  668. struct coverage_table *coverage = (struct coverage_table *)args;
  669. enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
  670. struct coverage_entry *entry = coverage->base + coverage->num_entries;
  671. if (coverage->num_entries == MAX_COVERAGE_ENTRIES - 1) {
  672. pr_err("FAIL: Out of space for test coverage data");
  673. return -ENOMEM;
  674. }
  675. ++coverage->num_entries;
  676. entry->header = h;
  677. entry->regs = coverage_start_registers(h);
  678. entry->nesting = coverage->nesting;
  679. entry->matched = false;
  680. if (type == DECODE_TYPE_TABLE) {
  681. struct decode_table *d = (struct decode_table *)h;
  682. int ret;
  683. ++coverage->nesting;
  684. ret = table_iter(d->table.table, coverage_start_fn, coverage);
  685. --coverage->nesting;
  686. return ret;
  687. }
  688. return 0;
  689. }
  690. static int coverage_start(const union decode_item *table)
  691. {
  692. coverage.base = kmalloc(MAX_COVERAGE_ENTRIES *
  693. sizeof(struct coverage_entry), GFP_KERNEL);
  694. coverage.num_entries = 0;
  695. coverage.nesting = 0;
  696. return table_iter(table, coverage_start_fn, &coverage);
  697. }
  698. static void
  699. coverage_add_registers(struct coverage_entry *entry, kprobe_opcode_t insn)
  700. {
  701. int regs = entry->header->type_regs.bits >> DECODE_TYPE_BITS;
  702. int i;
  703. for (i = 0; i < 20; i += 4) {
  704. enum decode_reg_type reg_type = (regs >> i) & 0xf;
  705. int reg = (insn >> i) & 0xf;
  706. int flag;
  707. if (!reg_type)
  708. continue;
  709. if (reg == 13)
  710. flag = COVERAGE_SP;
  711. else if (reg == 15)
  712. flag = COVERAGE_PC;
  713. else
  714. flag = COVERAGE_ANY_REG;
  715. entry->regs &= ~(flag << i);
  716. switch (reg_type) {
  717. case REG_TYPE_NONE:
  718. case REG_TYPE_ANY:
  719. case REG_TYPE_SAMEAS16:
  720. break;
  721. case REG_TYPE_SP:
  722. if (reg != 13)
  723. return;
  724. break;
  725. case REG_TYPE_PC:
  726. if (reg != 15)
  727. return;
  728. break;
  729. case REG_TYPE_NOSP:
  730. if (reg == 13)
  731. return;
  732. break;
  733. case REG_TYPE_NOSPPC:
  734. case REG_TYPE_NOSPPCX:
  735. if (reg == 13 || reg == 15)
  736. return;
  737. break;
  738. case REG_TYPE_NOPCWB:
  739. if (!is_writeback(insn))
  740. break;
  741. if (reg == 15) {
  742. entry->regs &= ~(COVERAGE_PCWB << i);
  743. return;
  744. }
  745. break;
  746. case REG_TYPE_NOPC:
  747. case REG_TYPE_NOPCX:
  748. if (reg == 15)
  749. return;
  750. break;
  751. }
  752. }
  753. }
  754. static void coverage_add(kprobe_opcode_t insn)
  755. {
  756. struct coverage_entry *entry = coverage.base;
  757. struct coverage_entry *end = coverage.base + coverage.num_entries;
  758. bool matched = false;
  759. unsigned nesting = 0;
  760. for (; entry < end; ++entry) {
  761. const struct decode_header *h = entry->header;
  762. enum decode_type type = h->type_regs.bits & DECODE_TYPE_MASK;
  763. if (entry->nesting > nesting)
  764. continue; /* Skip sub-table we didn't match */
  765. if (entry->nesting < nesting)
  766. break; /* End of sub-table we were scanning */
  767. if (!matched) {
  768. if ((insn & h->mask.bits) != h->value.bits)
  769. continue;
  770. entry->matched = true;
  771. }
  772. switch (type) {
  773. case DECODE_TYPE_TABLE:
  774. ++nesting;
  775. break;
  776. case DECODE_TYPE_CUSTOM:
  777. case DECODE_TYPE_SIMULATE:
  778. case DECODE_TYPE_EMULATE:
  779. coverage_add_registers(entry, insn);
  780. return;
  781. case DECODE_TYPE_OR:
  782. matched = true;
  783. break;
  784. case DECODE_TYPE_REJECT:
  785. default:
  786. return;
  787. }
  788. }
  789. }
  790. static void coverage_end(void)
  791. {
  792. struct coverage_entry *entry = coverage.base;
  793. struct coverage_entry *end = coverage.base + coverage.num_entries;
  794. for (; entry < end; ++entry) {
  795. u32 mask = entry->header->mask.bits;
  796. u32 value = entry->header->value.bits;
  797. if (entry->regs) {
  798. pr_err("FAIL: Register test coverage missing for %08x %08x (%05x)\n",
  799. mask, value, entry->regs);
  800. coverage_fail = true;
  801. }
  802. if (!entry->matched) {
  803. pr_err("FAIL: Test coverage entry missing for %08x %08x\n",
  804. mask, value);
  805. coverage_fail = true;
  806. }
  807. }
  808. kfree(coverage.base);
  809. }
  810. /*
  811. * Framework for instruction set test cases
  812. */
  813. void __naked __kprobes_test_case_start(void)
  814. {
  815. __asm__ __volatile__ (
  816. "stmdb sp!, {r4-r11} \n\t"
  817. "sub sp, sp, #"__stringify(TEST_MEMORY_SIZE)"\n\t"
  818. "bic r0, lr, #1 @ r0 = inline title string \n\t"
  819. "mov r1, sp \n\t"
  820. "bl kprobes_test_case_start \n\t"
  821. "bx r0 \n\t"
  822. );
  823. }
  824. #ifndef CONFIG_THUMB2_KERNEL
  825. void __naked __kprobes_test_case_end_32(void)
  826. {
  827. __asm__ __volatile__ (
  828. "mov r4, lr \n\t"
  829. "bl kprobes_test_case_end \n\t"
  830. "cmp r0, #0 \n\t"
  831. "movne pc, r0 \n\t"
  832. "mov r0, r4 \n\t"
  833. "add sp, sp, #"__stringify(TEST_MEMORY_SIZE)"\n\t"
  834. "ldmia sp!, {r4-r11} \n\t"
  835. "mov pc, r0 \n\t"
  836. );
  837. }
  838. #else /* CONFIG_THUMB2_KERNEL */
  839. void __naked __kprobes_test_case_end_16(void)
  840. {
  841. __asm__ __volatile__ (
  842. "mov r4, lr \n\t"
  843. "bl kprobes_test_case_end \n\t"
  844. "cmp r0, #0 \n\t"
  845. "bxne r0 \n\t"
  846. "mov r0, r4 \n\t"
  847. "add sp, sp, #"__stringify(TEST_MEMORY_SIZE)"\n\t"
  848. "ldmia sp!, {r4-r11} \n\t"
  849. "bx r0 \n\t"
  850. );
  851. }
  852. void __naked __kprobes_test_case_end_32(void)
  853. {
  854. __asm__ __volatile__ (
  855. ".arm \n\t"
  856. "orr lr, lr, #1 @ will return to Thumb code \n\t"
  857. "ldr pc, 1f \n\t"
  858. "1: \n\t"
  859. ".word __kprobes_test_case_end_16 \n\t"
  860. );
  861. }
  862. #endif
  863. int kprobe_test_flags;
  864. int kprobe_test_cc_position;
  865. static int test_try_count;
  866. static int test_pass_count;
  867. static int test_fail_count;
  868. static struct pt_regs initial_regs;
  869. static struct pt_regs expected_regs;
  870. static struct pt_regs result_regs;
  871. static u32 expected_memory[TEST_MEMORY_SIZE/sizeof(u32)];
  872. static const char *current_title;
  873. static struct test_arg *current_args;
  874. static u32 *current_stack;
  875. static uintptr_t current_branch_target;
  876. static uintptr_t current_code_start;
  877. static kprobe_opcode_t current_instruction;
  878. #define TEST_CASE_PASSED -1
  879. #define TEST_CASE_FAILED -2
  880. static int test_case_run_count;
  881. static bool test_case_is_thumb;
  882. static int test_instance;
  883. /*
  884. * We ignore the state of the imprecise abort disable flag (CPSR.A) because this
  885. * can change randomly as the kernel doesn't take care to preserve or initialise
  886. * this across context switches. Also, with Security Extentions, the flag may
  887. * not be under control of the kernel; for this reason we ignore the state of
  888. * the FIQ disable flag CPSR.F as well.
  889. */
  890. #define PSR_IGNORE_BITS (PSR_A_BIT | PSR_F_BIT)
  891. static unsigned long test_check_cc(int cc, unsigned long cpsr)
  892. {
  893. int ret = arm_check_condition(cc << 28, cpsr);
  894. return (ret != ARM_OPCODE_CONDTEST_FAIL);
  895. }
  896. static int is_last_scenario;
  897. static int probe_should_run; /* 0 = no, 1 = yes, -1 = unknown */
  898. static int memory_needs_checking;
  899. static unsigned long test_context_cpsr(int scenario)
  900. {
  901. unsigned long cpsr;
  902. probe_should_run = 1;
  903. /* Default case is that we cycle through 16 combinations of flags */
  904. cpsr = (scenario & 0xf) << 28; /* N,Z,C,V flags */
  905. cpsr |= (scenario & 0xf) << 16; /* GE flags */
  906. cpsr |= (scenario & 0x1) << 27; /* Toggle Q flag */
  907. if (!test_case_is_thumb) {
  908. /* Testing ARM code */
  909. int cc = current_instruction >> 28;
  910. probe_should_run = test_check_cc(cc, cpsr) != 0;
  911. if (scenario == 15)
  912. is_last_scenario = true;
  913. } else if (kprobe_test_flags & TEST_FLAG_NO_ITBLOCK) {
  914. /* Testing Thumb code without setting ITSTATE */
  915. if (kprobe_test_cc_position) {
  916. int cc = (current_instruction >> kprobe_test_cc_position) & 0xf;
  917. probe_should_run = test_check_cc(cc, cpsr) != 0;
  918. }
  919. if (scenario == 15)
  920. is_last_scenario = true;
  921. } else if (kprobe_test_flags & TEST_FLAG_FULL_ITBLOCK) {
  922. /* Testing Thumb code with all combinations of ITSTATE */
  923. unsigned x = (scenario >> 4);
  924. unsigned cond_base = x % 7; /* ITSTATE<7:5> */
  925. unsigned mask = x / 7 + 2; /* ITSTATE<4:0>, bits reversed */
  926. if (mask > 0x1f) {
  927. /* Finish by testing state from instruction 'itt al' */
  928. cond_base = 7;
  929. mask = 0x4;
  930. if ((scenario & 0xf) == 0xf)
  931. is_last_scenario = true;
  932. }
  933. cpsr |= cond_base << 13; /* ITSTATE<7:5> */
  934. cpsr |= (mask & 0x1) << 12; /* ITSTATE<4> */
  935. cpsr |= (mask & 0x2) << 10; /* ITSTATE<3> */
  936. cpsr |= (mask & 0x4) << 8; /* ITSTATE<2> */
  937. cpsr |= (mask & 0x8) << 23; /* ITSTATE<1> */
  938. cpsr |= (mask & 0x10) << 21; /* ITSTATE<0> */
  939. probe_should_run = test_check_cc((cpsr >> 12) & 0xf, cpsr) != 0;
  940. } else {
  941. /* Testing Thumb code with several combinations of ITSTATE */
  942. switch (scenario) {
  943. case 16: /* Clear NZCV flags and 'it eq' state (false as Z=0) */
  944. cpsr = 0x00000800;
  945. probe_should_run = 0;
  946. break;
  947. case 17: /* Set NZCV flags and 'it vc' state (false as V=1) */
  948. cpsr = 0xf0007800;
  949. probe_should_run = 0;
  950. break;
  951. case 18: /* Clear NZCV flags and 'it ls' state (true as C=0) */
  952. cpsr = 0x00009800;
  953. break;
  954. case 19: /* Set NZCV flags and 'it cs' state (true as C=1) */
  955. cpsr = 0xf0002800;
  956. is_last_scenario = true;
  957. break;
  958. }
  959. }
  960. return cpsr;
  961. }
  962. static void setup_test_context(struct pt_regs *regs)
  963. {
  964. int scenario = test_case_run_count>>1;
  965. unsigned long val;
  966. struct test_arg *args;
  967. int i;
  968. is_last_scenario = false;
  969. memory_needs_checking = false;
  970. /* Initialise test memory on stack */
  971. val = (scenario & 1) ? VALM : ~VALM;
  972. for (i = 0; i < TEST_MEMORY_SIZE / sizeof(current_stack[0]); ++i)
  973. current_stack[i] = val + (i << 8);
  974. /* Put target of branch on stack for tests which load PC from memory */
  975. if (current_branch_target)
  976. current_stack[15] = current_branch_target;
  977. /* Put a value for SP on stack for tests which load SP from memory */
  978. current_stack[13] = (u32)current_stack + 120;
  979. /* Initialise register values to their default state */
  980. val = (scenario & 2) ? VALR : ~VALR;
  981. for (i = 0; i < 13; ++i)
  982. regs->uregs[i] = val ^ (i << 8);
  983. regs->ARM_lr = val ^ (14 << 8);
  984. regs->ARM_cpsr &= ~(APSR_MASK | PSR_IT_MASK);
  985. regs->ARM_cpsr |= test_context_cpsr(scenario);
  986. /* Perform testcase specific register setup */
  987. args = current_args;
  988. for (; args[0].type != ARG_TYPE_END; ++args)
  989. switch (args[0].type) {
  990. case ARG_TYPE_REG: {
  991. struct test_arg_regptr *arg =
  992. (struct test_arg_regptr *)args;
  993. regs->uregs[arg->reg] = arg->val;
  994. break;
  995. }
  996. case ARG_TYPE_PTR: {
  997. struct test_arg_regptr *arg =
  998. (struct test_arg_regptr *)args;
  999. regs->uregs[arg->reg] =
  1000. (unsigned long)current_stack + arg->val;
  1001. memory_needs_checking = true;
  1002. break;
  1003. }
  1004. case ARG_TYPE_MEM: {
  1005. struct test_arg_mem *arg = (struct test_arg_mem *)args;
  1006. current_stack[arg->index] = arg->val;
  1007. break;
  1008. }
  1009. default:
  1010. break;
  1011. }
  1012. }
  1013. struct test_probe {
  1014. struct kprobe kprobe;
  1015. bool registered;
  1016. int hit;
  1017. };
  1018. static void unregister_test_probe(struct test_probe *probe)
  1019. {
  1020. if (probe->registered) {
  1021. unregister_kprobe(&probe->kprobe);
  1022. probe->kprobe.flags = 0; /* Clear disable flag to allow reuse */
  1023. }
  1024. probe->registered = false;
  1025. }
  1026. static int register_test_probe(struct test_probe *probe)
  1027. {
  1028. int ret;
  1029. if (probe->registered)
  1030. BUG();
  1031. ret = register_kprobe(&probe->kprobe);
  1032. if (ret >= 0) {
  1033. probe->registered = true;
  1034. probe->hit = -1;
  1035. }
  1036. return ret;
  1037. }
  1038. static int __kprobes
  1039. test_before_pre_handler(struct kprobe *p, struct pt_regs *regs)
  1040. {
  1041. container_of(p, struct test_probe, kprobe)->hit = test_instance;
  1042. return 0;
  1043. }
  1044. static void __kprobes
  1045. test_before_post_handler(struct kprobe *p, struct pt_regs *regs,
  1046. unsigned long flags)
  1047. {
  1048. setup_test_context(regs);
  1049. initial_regs = *regs;
  1050. initial_regs.ARM_cpsr &= ~PSR_IGNORE_BITS;
  1051. }
  1052. static int __kprobes
  1053. test_case_pre_handler(struct kprobe *p, struct pt_regs *regs)
  1054. {
  1055. container_of(p, struct test_probe, kprobe)->hit = test_instance;
  1056. return 0;
  1057. }
  1058. static int __kprobes
  1059. test_after_pre_handler(struct kprobe *p, struct pt_regs *regs)
  1060. {
  1061. if (container_of(p, struct test_probe, kprobe)->hit == test_instance)
  1062. return 0; /* Already run for this test instance */
  1063. result_regs = *regs;
  1064. result_regs.ARM_cpsr &= ~PSR_IGNORE_BITS;
  1065. /* Undo any changes done to SP by the test case */
  1066. regs->ARM_sp = (unsigned long)current_stack;
  1067. container_of(p, struct test_probe, kprobe)->hit = test_instance;
  1068. return 0;
  1069. }
  1070. static struct test_probe test_before_probe = {
  1071. .kprobe.pre_handler = test_before_pre_handler,
  1072. .kprobe.post_handler = test_before_post_handler,
  1073. };
  1074. static struct test_probe test_case_probe = {
  1075. .kprobe.pre_handler = test_case_pre_handler,
  1076. };
  1077. static struct test_probe test_after_probe = {
  1078. .kprobe.pre_handler = test_after_pre_handler,
  1079. };
  1080. static struct test_probe test_after2_probe = {
  1081. .kprobe.pre_handler = test_after_pre_handler,
  1082. };
  1083. static void test_case_cleanup(void)
  1084. {
  1085. unregister_test_probe(&test_before_probe);
  1086. unregister_test_probe(&test_case_probe);
  1087. unregister_test_probe(&test_after_probe);
  1088. unregister_test_probe(&test_after2_probe);
  1089. }
  1090. static void print_registers(struct pt_regs *regs)
  1091. {
  1092. pr_err("r0 %08lx | r1 %08lx | r2 %08lx | r3 %08lx\n",
  1093. regs->ARM_r0, regs->ARM_r1, regs->ARM_r2, regs->ARM_r3);
  1094. pr_err("r4 %08lx | r5 %08lx | r6 %08lx | r7 %08lx\n",
  1095. regs->ARM_r4, regs->ARM_r5, regs->ARM_r6, regs->ARM_r7);
  1096. pr_err("r8 %08lx | r9 %08lx | r10 %08lx | r11 %08lx\n",
  1097. regs->ARM_r8, regs->ARM_r9, regs->ARM_r10, regs->ARM_fp);
  1098. pr_err("r12 %08lx | sp %08lx | lr %08lx | pc %08lx\n",
  1099. regs->ARM_ip, regs->ARM_sp, regs->ARM_lr, regs->ARM_pc);
  1100. pr_err("cpsr %08lx\n", regs->ARM_cpsr);
  1101. }
  1102. static void print_memory(u32 *mem, size_t size)
  1103. {
  1104. int i;
  1105. for (i = 0; i < size / sizeof(u32); i += 4)
  1106. pr_err("%08x %08x %08x %08x\n", mem[i], mem[i+1],
  1107. mem[i+2], mem[i+3]);
  1108. }
  1109. static size_t expected_memory_size(u32 *sp)
  1110. {
  1111. size_t size = sizeof(expected_memory);
  1112. int offset = (uintptr_t)sp - (uintptr_t)current_stack;
  1113. if (offset > 0)
  1114. size -= offset;
  1115. return size;
  1116. }
  1117. static void test_case_failed(const char *message)
  1118. {
  1119. test_case_cleanup();
  1120. pr_err("FAIL: %s\n", message);
  1121. pr_err("FAIL: Test %s\n", current_title);
  1122. pr_err("FAIL: Scenario %d\n", test_case_run_count >> 1);
  1123. }
  1124. static unsigned long next_instruction(unsigned long pc)
  1125. {
  1126. #ifdef CONFIG_THUMB2_KERNEL
  1127. if ((pc & 1) &&
  1128. !is_wide_instruction(__mem_to_opcode_thumb16(*(u16 *)(pc - 1))))
  1129. return pc + 2;
  1130. else
  1131. #endif
  1132. return pc + 4;
  1133. }
  1134. static uintptr_t __used kprobes_test_case_start(const char *title, void *stack)
  1135. {
  1136. struct test_arg *args;
  1137. struct test_arg_end *end_arg;
  1138. unsigned long test_code;
  1139. args = (struct test_arg *)PTR_ALIGN(title + strlen(title) + 1, 4);
  1140. current_title = title;
  1141. current_args = args;
  1142. current_stack = stack;
  1143. ++test_try_count;
  1144. while (args->type != ARG_TYPE_END)
  1145. ++args;
  1146. end_arg = (struct test_arg_end *)args;
  1147. test_code = (unsigned long)(args + 1); /* Code starts after args */
  1148. test_case_is_thumb = end_arg->flags & ARG_FLAG_THUMB;
  1149. if (test_case_is_thumb)
  1150. test_code |= 1;
  1151. current_code_start = test_code;
  1152. current_branch_target = 0;
  1153. if (end_arg->branch_offset != end_arg->end_offset)
  1154. current_branch_target = test_code + end_arg->branch_offset;
  1155. test_code += end_arg->code_offset;
  1156. test_before_probe.kprobe.addr = (kprobe_opcode_t *)test_code;
  1157. test_code = next_instruction(test_code);
  1158. test_case_probe.kprobe.addr = (kprobe_opcode_t *)test_code;
  1159. if (test_case_is_thumb) {
  1160. u16 *p = (u16 *)(test_code & ~1);
  1161. current_instruction = __mem_to_opcode_thumb16(p[0]);
  1162. if (is_wide_instruction(current_instruction)) {
  1163. u16 instr2 = __mem_to_opcode_thumb16(p[1]);
  1164. current_instruction = __opcode_thumb32_compose(current_instruction, instr2);
  1165. }
  1166. } else {
  1167. current_instruction = __mem_to_opcode_arm(*(u32 *)test_code);
  1168. }
  1169. if (current_title[0] == '.')
  1170. verbose("%s\n", current_title);
  1171. else
  1172. verbose("%s\t@ %0*x\n", current_title,
  1173. test_case_is_thumb ? 4 : 8,
  1174. current_instruction);
  1175. test_code = next_instruction(test_code);
  1176. test_after_probe.kprobe.addr = (kprobe_opcode_t *)test_code;
  1177. if (kprobe_test_flags & TEST_FLAG_NARROW_INSTR) {
  1178. if (!test_case_is_thumb ||
  1179. is_wide_instruction(current_instruction)) {
  1180. test_case_failed("expected 16-bit instruction");
  1181. goto fail;
  1182. }
  1183. } else {
  1184. if (test_case_is_thumb &&
  1185. !is_wide_instruction(current_instruction)) {
  1186. test_case_failed("expected 32-bit instruction");
  1187. goto fail;
  1188. }
  1189. }
  1190. coverage_add(current_instruction);
  1191. if (end_arg->flags & ARG_FLAG_UNSUPPORTED) {
  1192. if (register_test_probe(&test_case_probe) < 0)
  1193. goto pass;
  1194. test_case_failed("registered probe for unsupported instruction");
  1195. goto fail;
  1196. }
  1197. if (end_arg->flags & ARG_FLAG_SUPPORTED) {
  1198. if (register_test_probe(&test_case_probe) >= 0)
  1199. goto pass;
  1200. test_case_failed("couldn't register probe for supported instruction");
  1201. goto fail;
  1202. }
  1203. if (register_test_probe(&test_before_probe) < 0) {
  1204. test_case_failed("register test_before_probe failed");
  1205. goto fail;
  1206. }
  1207. if (register_test_probe(&test_after_probe) < 0) {
  1208. test_case_failed("register test_after_probe failed");
  1209. goto fail;
  1210. }
  1211. if (current_branch_target) {
  1212. test_after2_probe.kprobe.addr =
  1213. (kprobe_opcode_t *)current_branch_target;
  1214. if (register_test_probe(&test_after2_probe) < 0) {
  1215. test_case_failed("register test_after2_probe failed");
  1216. goto fail;
  1217. }
  1218. }
  1219. /* Start first run of test case */
  1220. test_case_run_count = 0;
  1221. ++test_instance;
  1222. return current_code_start;
  1223. pass:
  1224. test_case_run_count = TEST_CASE_PASSED;
  1225. return (uintptr_t)test_after_probe.kprobe.addr;
  1226. fail:
  1227. test_case_run_count = TEST_CASE_FAILED;
  1228. return (uintptr_t)test_after_probe.kprobe.addr;
  1229. }
  1230. static bool check_test_results(void)
  1231. {
  1232. size_t mem_size = 0;
  1233. u32 *mem = 0;
  1234. if (memcmp(&expected_regs, &result_regs, sizeof(expected_regs))) {
  1235. test_case_failed("registers differ");
  1236. goto fail;
  1237. }
  1238. if (memory_needs_checking) {
  1239. mem = (u32 *)result_regs.ARM_sp;
  1240. mem_size = expected_memory_size(mem);
  1241. if (memcmp(expected_memory, mem, mem_size)) {
  1242. test_case_failed("test memory differs");
  1243. goto fail;
  1244. }
  1245. }
  1246. return true;
  1247. fail:
  1248. pr_err("initial_regs:\n");
  1249. print_registers(&initial_regs);
  1250. pr_err("expected_regs:\n");
  1251. print_registers(&expected_regs);
  1252. pr_err("result_regs:\n");
  1253. print_registers(&result_regs);
  1254. if (mem) {
  1255. pr_err("current_stack=%p\n", current_stack);
  1256. pr_err("expected_memory:\n");
  1257. print_memory(expected_memory, mem_size);
  1258. pr_err("result_memory:\n");
  1259. print_memory(mem, mem_size);
  1260. }
  1261. return false;
  1262. }
  1263. static uintptr_t __used kprobes_test_case_end(void)
  1264. {
  1265. if (test_case_run_count < 0) {
  1266. if (test_case_run_count == TEST_CASE_PASSED)
  1267. /* kprobes_test_case_start did all the needed testing */
  1268. goto pass;
  1269. else
  1270. /* kprobes_test_case_start failed */
  1271. goto fail;
  1272. }
  1273. if (test_before_probe.hit != test_instance) {
  1274. test_case_failed("test_before_handler not run");
  1275. goto fail;
  1276. }
  1277. if (test_after_probe.hit != test_instance &&
  1278. test_after2_probe.hit != test_instance) {
  1279. test_case_failed("test_after_handler not run");
  1280. goto fail;
  1281. }
  1282. /*
  1283. * Even numbered test runs ran without a probe on the test case so
  1284. * we can gather reference results. The subsequent odd numbered run
  1285. * will have the probe inserted.
  1286. */
  1287. if ((test_case_run_count & 1) == 0) {
  1288. /* Save results from run without probe */
  1289. u32 *mem = (u32 *)result_regs.ARM_sp;
  1290. expected_regs = result_regs;
  1291. memcpy(expected_memory, mem, expected_memory_size(mem));
  1292. /* Insert probe onto test case instruction */
  1293. if (register_test_probe(&test_case_probe) < 0) {
  1294. test_case_failed("register test_case_probe failed");
  1295. goto fail;
  1296. }
  1297. } else {
  1298. /* Check probe ran as expected */
  1299. if (probe_should_run == 1) {
  1300. if (test_case_probe.hit != test_instance) {
  1301. test_case_failed("test_case_handler not run");
  1302. goto fail;
  1303. }
  1304. } else if (probe_should_run == 0) {
  1305. if (test_case_probe.hit == test_instance) {
  1306. test_case_failed("test_case_handler ran");
  1307. goto fail;
  1308. }
  1309. }
  1310. /* Remove probe for any subsequent reference run */
  1311. unregister_test_probe(&test_case_probe);
  1312. if (!check_test_results())
  1313. goto fail;
  1314. if (is_last_scenario)
  1315. goto pass;
  1316. }
  1317. /* Do next test run */
  1318. ++test_case_run_count;
  1319. ++test_instance;
  1320. return current_code_start;
  1321. fail:
  1322. ++test_fail_count;
  1323. goto end;
  1324. pass:
  1325. ++test_pass_count;
  1326. end:
  1327. test_case_cleanup();
  1328. return 0;
  1329. }
  1330. /*
  1331. * Top level test functions
  1332. */
  1333. static int run_test_cases(void (*tests)(void), const union decode_item *table)
  1334. {
  1335. int ret;
  1336. pr_info(" Check decoding tables\n");
  1337. ret = table_test(table);
  1338. if (ret)
  1339. return ret;
  1340. pr_info(" Run test cases\n");
  1341. ret = coverage_start(table);
  1342. if (ret)
  1343. return ret;
  1344. tests();
  1345. coverage_end();
  1346. return 0;
  1347. }
  1348. static int __init run_all_tests(void)
  1349. {
  1350. int ret = 0;
  1351. pr_info("Beginning kprobe tests...\n");
  1352. #ifndef CONFIG_THUMB2_KERNEL
  1353. pr_info("Probe ARM code\n");
  1354. ret = run_api_tests(arm_func);
  1355. if (ret)
  1356. goto out;
  1357. pr_info("ARM instruction simulation\n");
  1358. ret = run_test_cases(kprobe_arm_test_cases, probes_decode_arm_table);
  1359. if (ret)
  1360. goto out;
  1361. #else /* CONFIG_THUMB2_KERNEL */
  1362. pr_info("Probe 16-bit Thumb code\n");
  1363. ret = run_api_tests(thumb16_func);
  1364. if (ret)
  1365. goto out;
  1366. pr_info("Probe 32-bit Thumb code, even halfword\n");
  1367. ret = run_api_tests(thumb32even_func);
  1368. if (ret)
  1369. goto out;
  1370. pr_info("Probe 32-bit Thumb code, odd halfword\n");
  1371. ret = run_api_tests(thumb32odd_func);
  1372. if (ret)
  1373. goto out;
  1374. pr_info("16-bit Thumb instruction simulation\n");
  1375. ret = run_test_cases(kprobe_thumb16_test_cases,
  1376. probes_decode_thumb16_table);
  1377. if (ret)
  1378. goto out;
  1379. pr_info("32-bit Thumb instruction simulation\n");
  1380. ret = run_test_cases(kprobe_thumb32_test_cases,
  1381. probes_decode_thumb32_table);
  1382. if (ret)
  1383. goto out;
  1384. #endif
  1385. pr_info("Total instruction simulation tests=%d, pass=%d fail=%d\n",
  1386. test_try_count, test_pass_count, test_fail_count);
  1387. if (test_fail_count) {
  1388. ret = -EINVAL;
  1389. goto out;
  1390. }
  1391. #if BENCHMARKING
  1392. pr_info("Benchmarks\n");
  1393. ret = run_benchmarks();
  1394. if (ret)
  1395. goto out;
  1396. #endif
  1397. #if __LINUX_ARM_ARCH__ >= 7
  1398. /* We are able to run all test cases so coverage should be complete */
  1399. if (coverage_fail) {
  1400. pr_err("FAIL: Test coverage checks failed\n");
  1401. ret = -EINVAL;
  1402. goto out;
  1403. }
  1404. #endif
  1405. out:
  1406. if (ret == 0)
  1407. pr_info("Finished kprobe tests OK\n");
  1408. else
  1409. pr_err("kprobe tests failed\n");
  1410. return ret;
  1411. }
  1412. /*
  1413. * Module setup
  1414. */
  1415. #ifdef MODULE
  1416. static void __exit kprobe_test_exit(void)
  1417. {
  1418. }
  1419. module_init(run_all_tests)
  1420. module_exit(kprobe_test_exit)
  1421. MODULE_LICENSE("GPL");
  1422. #else /* !MODULE */
  1423. late_initcall(run_all_tests);
  1424. #endif