kprobes-test.c 42 KB

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