kprobes-test.c 42 KB

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