code-patching.c 18 KB

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