code-patching.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * Copyright 2008 Michael Ellerman, IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/kprobes.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/init.h>
  13. #include <linux/mm.h>
  14. #include <linux/cpuhotplug.h>
  15. #include <linux/slab.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/kprobes.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/page.h>
  21. #include <asm/code-patching.h>
  22. #include <asm/setup.h>
  23. static int __patch_instruction(unsigned int *exec_addr, unsigned int instr,
  24. unsigned int *patch_addr)
  25. {
  26. int err;
  27. /* Make sure we aren't patching a freed init section */
  28. if (init_mem_is_free && init_section_contains(exec_addr, 4)) {
  29. pr_debug("Skipping init section patching addr: 0x%px\n", exec_addr);
  30. return 0;
  31. }
  32. __put_user_size(instr, patch_addr, 4, err);
  33. if (err)
  34. return err;
  35. asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
  36. "r" (exec_addr));
  37. return 0;
  38. }
  39. int raw_patch_instruction(unsigned int *addr, unsigned int instr)
  40. {
  41. return __patch_instruction(addr, instr, addr);
  42. }
  43. #ifdef CONFIG_STRICT_KERNEL_RWX
  44. static DEFINE_PER_CPU(struct vm_struct *, text_poke_area);
  45. static int text_area_cpu_up(unsigned int cpu)
  46. {
  47. struct vm_struct *area;
  48. area = get_vm_area(PAGE_SIZE, VM_ALLOC);
  49. if (!area) {
  50. WARN_ONCE(1, "Failed to create text area for cpu %d\n",
  51. cpu);
  52. return -1;
  53. }
  54. this_cpu_write(text_poke_area, area);
  55. return 0;
  56. }
  57. static int text_area_cpu_down(unsigned int cpu)
  58. {
  59. free_vm_area(this_cpu_read(text_poke_area));
  60. return 0;
  61. }
  62. /*
  63. * Run as a late init call. This allows all the boot time patching to be done
  64. * simply by patching the code, and then we're called here prior to
  65. * mark_rodata_ro(), which happens after all init calls are run. Although
  66. * BUG_ON() is rude, in this case it should only happen if ENOMEM, and we judge
  67. * it as being preferable to a kernel that will crash later when someone tries
  68. * to use patch_instruction().
  69. */
  70. static int __init setup_text_poke_area(void)
  71. {
  72. BUG_ON(!cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  73. "powerpc/text_poke:online", text_area_cpu_up,
  74. text_area_cpu_down));
  75. return 0;
  76. }
  77. late_initcall(setup_text_poke_area);
  78. /*
  79. * This can be called for kernel text or a module.
  80. */
  81. static int map_patch_area(void *addr, unsigned long text_poke_addr)
  82. {
  83. unsigned long pfn;
  84. int err;
  85. if (is_vmalloc_addr(addr))
  86. pfn = vmalloc_to_pfn(addr);
  87. else
  88. pfn = __pa_symbol(addr) >> PAGE_SHIFT;
  89. err = map_kernel_page(text_poke_addr, (pfn << PAGE_SHIFT),
  90. pgprot_val(PAGE_KERNEL));
  91. pr_devel("Mapped addr %lx with pfn %lx:%d\n", text_poke_addr, pfn, err);
  92. if (err)
  93. return -1;
  94. return 0;
  95. }
  96. static inline int unmap_patch_area(unsigned long addr)
  97. {
  98. pte_t *ptep;
  99. pmd_t *pmdp;
  100. pud_t *pudp;
  101. pgd_t *pgdp;
  102. pgdp = pgd_offset_k(addr);
  103. if (unlikely(!pgdp))
  104. return -EINVAL;
  105. pudp = pud_offset(pgdp, addr);
  106. if (unlikely(!pudp))
  107. return -EINVAL;
  108. pmdp = pmd_offset(pudp, addr);
  109. if (unlikely(!pmdp))
  110. return -EINVAL;
  111. ptep = pte_offset_kernel(pmdp, addr);
  112. if (unlikely(!ptep))
  113. return -EINVAL;
  114. pr_devel("clearing mm %p, pte %p, addr %lx\n", &init_mm, ptep, addr);
  115. /*
  116. * In hash, pte_clear flushes the tlb, in radix, we have to
  117. */
  118. pte_clear(&init_mm, addr, ptep);
  119. flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
  120. return 0;
  121. }
  122. int patch_instruction(unsigned int *addr, unsigned int instr)
  123. {
  124. int err;
  125. unsigned int *patch_addr = NULL;
  126. unsigned long flags;
  127. unsigned long text_poke_addr;
  128. unsigned long kaddr = (unsigned long)addr;
  129. /*
  130. * During early early boot patch_instruction is called
  131. * when text_poke_area is not ready, but we still need
  132. * to allow patching. We just do the plain old patching
  133. */
  134. if (!this_cpu_read(text_poke_area))
  135. return raw_patch_instruction(addr, instr);
  136. local_irq_save(flags);
  137. text_poke_addr = (unsigned long)__this_cpu_read(text_poke_area)->addr;
  138. if (map_patch_area(addr, text_poke_addr)) {
  139. err = -1;
  140. goto out;
  141. }
  142. patch_addr = (unsigned int *)(text_poke_addr) +
  143. ((kaddr & ~PAGE_MASK) / sizeof(unsigned int));
  144. __patch_instruction(addr, instr, patch_addr);
  145. err = unmap_patch_area(text_poke_addr);
  146. if (err)
  147. pr_warn("failed to unmap %lx\n", text_poke_addr);
  148. out:
  149. local_irq_restore(flags);
  150. return err;
  151. }
  152. #else /* !CONFIG_STRICT_KERNEL_RWX */
  153. int patch_instruction(unsigned int *addr, unsigned int instr)
  154. {
  155. return raw_patch_instruction(addr, instr);
  156. }
  157. #endif /* CONFIG_STRICT_KERNEL_RWX */
  158. NOKPROBE_SYMBOL(patch_instruction);
  159. int patch_branch(unsigned int *addr, unsigned long target, int flags)
  160. {
  161. return patch_instruction(addr, create_branch(addr, target, flags));
  162. }
  163. int patch_branch_site(s32 *site, unsigned long target, int flags)
  164. {
  165. unsigned int *addr;
  166. addr = (unsigned int *)((unsigned long)site + *site);
  167. return patch_instruction(addr, create_branch(addr, target, flags));
  168. }
  169. int patch_instruction_site(s32 *site, unsigned int instr)
  170. {
  171. unsigned int *addr;
  172. addr = (unsigned int *)((unsigned long)site + *site);
  173. return patch_instruction(addr, instr);
  174. }
  175. bool is_offset_in_branch_range(long offset)
  176. {
  177. /*
  178. * Powerpc branch instruction is :
  179. *
  180. * 0 6 30 31
  181. * +---------+----------------+---+---+
  182. * | opcode | LI |AA |LK |
  183. * +---------+----------------+---+---+
  184. * Where AA = 0 and LK = 0
  185. *
  186. * LI is a signed 24 bits integer. The real branch offset is computed
  187. * by: imm32 = SignExtend(LI:'0b00', 32);
  188. *
  189. * So the maximum forward branch should be:
  190. * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
  191. * The maximum backward branch should be:
  192. * (0xff800000 << 2) = 0xfe000000 = -0x2000000
  193. */
  194. return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
  195. }
  196. /*
  197. * Helper to check if a given instruction is a conditional branch
  198. * Derived from the conditional checks in analyse_instr()
  199. */
  200. bool is_conditional_branch(unsigned int instr)
  201. {
  202. unsigned int opcode = instr >> 26;
  203. if (opcode == 16) /* bc, bca, bcl, bcla */
  204. return true;
  205. if (opcode == 19) {
  206. switch ((instr >> 1) & 0x3ff) {
  207. case 16: /* bclr, bclrl */
  208. case 528: /* bcctr, bcctrl */
  209. case 560: /* bctar, bctarl */
  210. return true;
  211. }
  212. }
  213. return false;
  214. }
  215. NOKPROBE_SYMBOL(is_conditional_branch);
  216. unsigned int create_branch(const unsigned int *addr,
  217. unsigned long target, int flags)
  218. {
  219. unsigned int instruction;
  220. long offset;
  221. offset = target;
  222. if (! (flags & BRANCH_ABSOLUTE))
  223. offset = offset - (unsigned long)addr;
  224. /* Check we can represent the target in the instruction format */
  225. if (!is_offset_in_branch_range(offset))
  226. return 0;
  227. /* Mask out the flags and target, so they don't step on each other. */
  228. instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
  229. return instruction;
  230. }
  231. unsigned int create_cond_branch(const unsigned int *addr,
  232. unsigned long target, int flags)
  233. {
  234. unsigned int instruction;
  235. long offset;
  236. offset = target;
  237. if (! (flags & BRANCH_ABSOLUTE))
  238. offset = offset - (unsigned long)addr;
  239. /* Check we can represent the target in the instruction format */
  240. if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
  241. return 0;
  242. /* Mask out the flags and target, so they don't step on each other. */
  243. instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
  244. return instruction;
  245. }
  246. static unsigned int branch_opcode(unsigned int instr)
  247. {
  248. return (instr >> 26) & 0x3F;
  249. }
  250. static int instr_is_branch_iform(unsigned int instr)
  251. {
  252. return branch_opcode(instr) == 18;
  253. }
  254. static int instr_is_branch_bform(unsigned int instr)
  255. {
  256. return branch_opcode(instr) == 16;
  257. }
  258. int instr_is_relative_branch(unsigned int instr)
  259. {
  260. if (instr & BRANCH_ABSOLUTE)
  261. return 0;
  262. return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
  263. }
  264. int instr_is_relative_link_branch(unsigned int instr)
  265. {
  266. return instr_is_relative_branch(instr) && (instr & BRANCH_SET_LINK);
  267. }
  268. static unsigned long branch_iform_target(const unsigned int *instr)
  269. {
  270. signed long imm;
  271. imm = *instr & 0x3FFFFFC;
  272. /* If the top bit of the immediate value is set this is negative */
  273. if (imm & 0x2000000)
  274. imm -= 0x4000000;
  275. if ((*instr & BRANCH_ABSOLUTE) == 0)
  276. imm += (unsigned long)instr;
  277. return (unsigned long)imm;
  278. }
  279. static unsigned long branch_bform_target(const unsigned int *instr)
  280. {
  281. signed long imm;
  282. imm = *instr & 0xFFFC;
  283. /* If the top bit of the immediate value is set this is negative */
  284. if (imm & 0x8000)
  285. imm -= 0x10000;
  286. if ((*instr & BRANCH_ABSOLUTE) == 0)
  287. imm += (unsigned long)instr;
  288. return (unsigned long)imm;
  289. }
  290. unsigned long branch_target(const unsigned int *instr)
  291. {
  292. if (instr_is_branch_iform(*instr))
  293. return branch_iform_target(instr);
  294. else if (instr_is_branch_bform(*instr))
  295. return branch_bform_target(instr);
  296. return 0;
  297. }
  298. int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
  299. {
  300. if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
  301. return branch_target(instr) == addr;
  302. return 0;
  303. }
  304. unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
  305. {
  306. unsigned long target;
  307. target = branch_target(src);
  308. if (instr_is_branch_iform(*src))
  309. return create_branch(dest, target, *src);
  310. else if (instr_is_branch_bform(*src))
  311. return create_cond_branch(dest, target, *src);
  312. return 0;
  313. }
  314. #ifdef CONFIG_PPC_BOOK3E_64
  315. void __patch_exception(int exc, unsigned long addr)
  316. {
  317. extern unsigned int interrupt_base_book3e;
  318. unsigned int *ibase = &interrupt_base_book3e;
  319. /* Our exceptions vectors start with a NOP and -then- a branch
  320. * to deal with single stepping from userspace which stops on
  321. * the second instruction. Thus we need to patch the second
  322. * instruction of the exception, not the first one
  323. */
  324. patch_branch(ibase + (exc / 4) + 1, addr, 0);
  325. }
  326. #endif
  327. #ifdef CONFIG_CODE_PATCHING_SELFTEST
  328. static void __init test_trampoline(void)
  329. {
  330. asm ("nop;\n");
  331. }
  332. #define check(x) \
  333. if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
  334. static void __init test_branch_iform(void)
  335. {
  336. unsigned int instr;
  337. unsigned long addr;
  338. addr = (unsigned long)&instr;
  339. /* The simplest case, branch to self, no flags */
  340. check(instr_is_branch_iform(0x48000000));
  341. /* All bits of target set, and flags */
  342. check(instr_is_branch_iform(0x4bffffff));
  343. /* High bit of opcode set, which is wrong */
  344. check(!instr_is_branch_iform(0xcbffffff));
  345. /* Middle bits of opcode set, which is wrong */
  346. check(!instr_is_branch_iform(0x7bffffff));
  347. /* Simplest case, branch to self with link */
  348. check(instr_is_branch_iform(0x48000001));
  349. /* All bits of targets set */
  350. check(instr_is_branch_iform(0x4bfffffd));
  351. /* Some bits of targets set */
  352. check(instr_is_branch_iform(0x4bff00fd));
  353. /* Must be a valid branch to start with */
  354. check(!instr_is_branch_iform(0x7bfffffd));
  355. /* Absolute branch to 0x100 */
  356. instr = 0x48000103;
  357. check(instr_is_branch_to_addr(&instr, 0x100));
  358. /* Absolute branch to 0x420fc */
  359. instr = 0x480420ff;
  360. check(instr_is_branch_to_addr(&instr, 0x420fc));
  361. /* Maximum positive relative branch, + 20MB - 4B */
  362. instr = 0x49fffffc;
  363. check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
  364. /* Smallest negative relative branch, - 4B */
  365. instr = 0x4bfffffc;
  366. check(instr_is_branch_to_addr(&instr, addr - 4));
  367. /* Largest negative relative branch, - 32 MB */
  368. instr = 0x4a000000;
  369. check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
  370. /* Branch to self, with link */
  371. instr = create_branch(&instr, addr, BRANCH_SET_LINK);
  372. check(instr_is_branch_to_addr(&instr, addr));
  373. /* Branch to self - 0x100, with link */
  374. instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
  375. check(instr_is_branch_to_addr(&instr, addr - 0x100));
  376. /* Branch to self + 0x100, no link */
  377. instr = create_branch(&instr, addr + 0x100, 0);
  378. check(instr_is_branch_to_addr(&instr, addr + 0x100));
  379. /* Maximum relative negative offset, - 32 MB */
  380. instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
  381. check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
  382. /* Out of range relative negative offset, - 32 MB + 4*/
  383. instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
  384. check(instr == 0);
  385. /* Out of range relative positive offset, + 32 MB */
  386. instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
  387. check(instr == 0);
  388. /* Unaligned target */
  389. instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
  390. check(instr == 0);
  391. /* Check flags are masked correctly */
  392. instr = create_branch(&instr, addr, 0xFFFFFFFC);
  393. check(instr_is_branch_to_addr(&instr, addr));
  394. check(instr == 0x48000000);
  395. }
  396. static void __init test_create_function_call(void)
  397. {
  398. unsigned int *iptr;
  399. unsigned long dest;
  400. /* Check we can create a function call */
  401. iptr = (unsigned int *)ppc_function_entry(test_trampoline);
  402. dest = ppc_function_entry(test_create_function_call);
  403. patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
  404. check(instr_is_branch_to_addr(iptr, dest));
  405. }
  406. static void __init test_branch_bform(void)
  407. {
  408. unsigned long addr;
  409. unsigned int *iptr, instr, flags;
  410. iptr = &instr;
  411. addr = (unsigned long)iptr;
  412. /* The simplest case, branch to self, no flags */
  413. check(instr_is_branch_bform(0x40000000));
  414. /* All bits of target set, and flags */
  415. check(instr_is_branch_bform(0x43ffffff));
  416. /* High bit of opcode set, which is wrong */
  417. check(!instr_is_branch_bform(0xc3ffffff));
  418. /* Middle bits of opcode set, which is wrong */
  419. check(!instr_is_branch_bform(0x7bffffff));
  420. /* Absolute conditional branch to 0x100 */
  421. instr = 0x43ff0103;
  422. check(instr_is_branch_to_addr(&instr, 0x100));
  423. /* Absolute conditional branch to 0x20fc */
  424. instr = 0x43ff20ff;
  425. check(instr_is_branch_to_addr(&instr, 0x20fc));
  426. /* Maximum positive relative conditional branch, + 32 KB - 4B */
  427. instr = 0x43ff7ffc;
  428. check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
  429. /* Smallest negative relative conditional branch, - 4B */
  430. instr = 0x43fffffc;
  431. check(instr_is_branch_to_addr(&instr, addr - 4));
  432. /* Largest negative relative conditional branch, - 32 KB */
  433. instr = 0x43ff8000;
  434. check(instr_is_branch_to_addr(&instr, addr - 0x8000));
  435. /* All condition code bits set & link */
  436. flags = 0x3ff000 | BRANCH_SET_LINK;
  437. /* Branch to self */
  438. instr = create_cond_branch(iptr, addr, flags);
  439. check(instr_is_branch_to_addr(&instr, addr));
  440. /* Branch to self - 0x100 */
  441. instr = create_cond_branch(iptr, addr - 0x100, flags);
  442. check(instr_is_branch_to_addr(&instr, addr - 0x100));
  443. /* Branch to self + 0x100 */
  444. instr = create_cond_branch(iptr, addr + 0x100, flags);
  445. check(instr_is_branch_to_addr(&instr, addr + 0x100));
  446. /* Maximum relative negative offset, - 32 KB */
  447. instr = create_cond_branch(iptr, addr - 0x8000, flags);
  448. check(instr_is_branch_to_addr(&instr, addr - 0x8000));
  449. /* Out of range relative negative offset, - 32 KB + 4*/
  450. instr = create_cond_branch(iptr, addr - 0x8004, flags);
  451. check(instr == 0);
  452. /* Out of range relative positive offset, + 32 KB */
  453. instr = create_cond_branch(iptr, addr + 0x8000, flags);
  454. check(instr == 0);
  455. /* Unaligned target */
  456. instr = create_cond_branch(iptr, addr + 3, flags);
  457. check(instr == 0);
  458. /* Check flags are masked correctly */
  459. instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
  460. check(instr_is_branch_to_addr(&instr, addr));
  461. check(instr == 0x43FF0000);
  462. }
  463. static void __init test_translate_branch(void)
  464. {
  465. unsigned long addr;
  466. unsigned int *p, *q;
  467. void *buf;
  468. buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
  469. check(buf);
  470. if (!buf)
  471. return;
  472. /* Simple case, branch to self moved a little */
  473. p = buf;
  474. addr = (unsigned long)p;
  475. patch_branch(p, addr, 0);
  476. check(instr_is_branch_to_addr(p, addr));
  477. q = p + 1;
  478. patch_instruction(q, translate_branch(q, p));
  479. check(instr_is_branch_to_addr(q, addr));
  480. /* Maximum negative case, move b . to addr + 32 MB */
  481. p = buf;
  482. addr = (unsigned long)p;
  483. patch_branch(p, addr, 0);
  484. q = buf + 0x2000000;
  485. patch_instruction(q, translate_branch(q, p));
  486. check(instr_is_branch_to_addr(p, addr));
  487. check(instr_is_branch_to_addr(q, addr));
  488. check(*q == 0x4a000000);
  489. /* Maximum positive case, move x to x - 32 MB + 4 */
  490. p = buf + 0x2000000;
  491. addr = (unsigned long)p;
  492. patch_branch(p, addr, 0);
  493. q = buf + 4;
  494. patch_instruction(q, translate_branch(q, p));
  495. check(instr_is_branch_to_addr(p, addr));
  496. check(instr_is_branch_to_addr(q, addr));
  497. check(*q == 0x49fffffc);
  498. /* Jump to x + 16 MB moved to x + 20 MB */
  499. p = buf;
  500. addr = 0x1000000 + (unsigned long)buf;
  501. patch_branch(p, addr, BRANCH_SET_LINK);
  502. q = buf + 0x1400000;
  503. patch_instruction(q, translate_branch(q, p));
  504. check(instr_is_branch_to_addr(p, addr));
  505. check(instr_is_branch_to_addr(q, addr));
  506. /* Jump to x + 16 MB moved to x - 16 MB + 4 */
  507. p = buf + 0x1000000;
  508. addr = 0x2000000 + (unsigned long)buf;
  509. patch_branch(p, addr, 0);
  510. q = buf + 4;
  511. patch_instruction(q, translate_branch(q, p));
  512. check(instr_is_branch_to_addr(p, addr));
  513. check(instr_is_branch_to_addr(q, addr));
  514. /* Conditional branch tests */
  515. /* Simple case, branch to self moved a little */
  516. p = buf;
  517. addr = (unsigned long)p;
  518. patch_instruction(p, create_cond_branch(p, addr, 0));
  519. check(instr_is_branch_to_addr(p, addr));
  520. q = p + 1;
  521. patch_instruction(q, translate_branch(q, p));
  522. check(instr_is_branch_to_addr(q, addr));
  523. /* Maximum negative case, move b . to addr + 32 KB */
  524. p = buf;
  525. addr = (unsigned long)p;
  526. patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
  527. q = buf + 0x8000;
  528. patch_instruction(q, translate_branch(q, p));
  529. check(instr_is_branch_to_addr(p, addr));
  530. check(instr_is_branch_to_addr(q, addr));
  531. check(*q == 0x43ff8000);
  532. /* Maximum positive case, move x to x - 32 KB + 4 */
  533. p = buf + 0x8000;
  534. addr = (unsigned long)p;
  535. patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
  536. q = buf + 4;
  537. patch_instruction(q, translate_branch(q, p));
  538. check(instr_is_branch_to_addr(p, addr));
  539. check(instr_is_branch_to_addr(q, addr));
  540. check(*q == 0x43ff7ffc);
  541. /* Jump to x + 12 KB moved to x + 20 KB */
  542. p = buf;
  543. addr = 0x3000 + (unsigned long)buf;
  544. patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
  545. q = buf + 0x5000;
  546. patch_instruction(q, translate_branch(q, p));
  547. check(instr_is_branch_to_addr(p, addr));
  548. check(instr_is_branch_to_addr(q, addr));
  549. /* Jump to x + 8 KB moved to x - 8 KB + 4 */
  550. p = buf + 0x2000;
  551. addr = 0x4000 + (unsigned long)buf;
  552. patch_instruction(p, create_cond_branch(p, addr, 0));
  553. q = buf + 4;
  554. patch_instruction(q, translate_branch(q, p));
  555. check(instr_is_branch_to_addr(p, addr));
  556. check(instr_is_branch_to_addr(q, addr));
  557. /* Free the buffer we were using */
  558. vfree(buf);
  559. }
  560. static int __init test_code_patching(void)
  561. {
  562. printk(KERN_DEBUG "Running code patching self-tests ...\n");
  563. test_branch_iform();
  564. test_branch_bform();
  565. test_create_function_call();
  566. test_translate_branch();
  567. return 0;
  568. }
  569. late_initcall(test_code_patching);
  570. #endif /* CONFIG_CODE_PATCHING_SELFTEST */