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