kprobes-test.c 42 KB

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