kprobes-test.c 43 KB

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