code-patching.c 18 KB

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