code-patching.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. static int do_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. static int do_patch_instruction(unsigned int *addr, unsigned int instr)
  149. {
  150. return raw_patch_instruction(addr, instr);
  151. }
  152. #endif /* CONFIG_STRICT_KERNEL_RWX */
  153. int patch_instruction(unsigned int *addr, unsigned int instr)
  154. {
  155. /* Make sure we aren't patching a freed init section */
  156. if (init_mem_is_free && init_section_contains(addr, 4)) {
  157. pr_debug("Skipping init section patching addr: 0x%px\n", addr);
  158. return 0;
  159. }
  160. return do_patch_instruction(addr, instr);
  161. }
  162. NOKPROBE_SYMBOL(patch_instruction);
  163. int patch_branch(unsigned int *addr, unsigned long target, int flags)
  164. {
  165. return patch_instruction(addr, create_branch(addr, target, flags));
  166. }
  167. int patch_branch_site(s32 *site, unsigned long target, int flags)
  168. {
  169. unsigned int *addr;
  170. addr = (unsigned int *)((unsigned long)site + *site);
  171. return patch_instruction(addr, create_branch(addr, target, flags));
  172. }
  173. int patch_instruction_site(s32 *site, unsigned int instr)
  174. {
  175. unsigned int *addr;
  176. addr = (unsigned int *)((unsigned long)site + *site);
  177. return patch_instruction(addr, instr);
  178. }
  179. bool is_offset_in_branch_range(long offset)
  180. {
  181. /*
  182. * Powerpc branch instruction is :
  183. *
  184. * 0 6 30 31
  185. * +---------+----------------+---+---+
  186. * | opcode | LI |AA |LK |
  187. * +---------+----------------+---+---+
  188. * Where AA = 0 and LK = 0
  189. *
  190. * LI is a signed 24 bits integer. The real branch offset is computed
  191. * by: imm32 = SignExtend(LI:'0b00', 32);
  192. *
  193. * So the maximum forward branch should be:
  194. * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
  195. * The maximum backward branch should be:
  196. * (0xff800000 << 2) = 0xfe000000 = -0x2000000
  197. */
  198. return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
  199. }
  200. /*
  201. * Helper to check if a given instruction is a conditional branch
  202. * Derived from the conditional checks in analyse_instr()
  203. */
  204. bool is_conditional_branch(unsigned int instr)
  205. {
  206. unsigned int opcode = instr >> 26;
  207. if (opcode == 16) /* bc, bca, bcl, bcla */
  208. return true;
  209. if (opcode == 19) {
  210. switch ((instr >> 1) & 0x3ff) {
  211. case 16: /* bclr, bclrl */
  212. case 528: /* bcctr, bcctrl */
  213. case 560: /* bctar, bctarl */
  214. return true;
  215. }
  216. }
  217. return false;
  218. }
  219. NOKPROBE_SYMBOL(is_conditional_branch);
  220. unsigned int create_branch(const unsigned int *addr,
  221. unsigned long target, int flags)
  222. {
  223. unsigned int instruction;
  224. long offset;
  225. offset = target;
  226. if (! (flags & BRANCH_ABSOLUTE))
  227. offset = offset - (unsigned long)addr;
  228. /* Check we can represent the target in the instruction format */
  229. if (!is_offset_in_branch_range(offset))
  230. return 0;
  231. /* Mask out the flags and target, so they don't step on each other. */
  232. instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
  233. return instruction;
  234. }
  235. unsigned int create_cond_branch(const unsigned int *addr,
  236. unsigned long target, int flags)
  237. {
  238. unsigned int instruction;
  239. long offset;
  240. offset = target;
  241. if (! (flags & BRANCH_ABSOLUTE))
  242. offset = offset - (unsigned long)addr;
  243. /* Check we can represent the target in the instruction format */
  244. if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
  245. return 0;
  246. /* Mask out the flags and target, so they don't step on each other. */
  247. instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
  248. return instruction;
  249. }
  250. static unsigned int branch_opcode(unsigned int instr)
  251. {
  252. return (instr >> 26) & 0x3F;
  253. }
  254. static int instr_is_branch_iform(unsigned int instr)
  255. {
  256. return branch_opcode(instr) == 18;
  257. }
  258. static int instr_is_branch_bform(unsigned int instr)
  259. {
  260. return branch_opcode(instr) == 16;
  261. }
  262. int instr_is_relative_branch(unsigned int instr)
  263. {
  264. if (instr & BRANCH_ABSOLUTE)
  265. return 0;
  266. return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
  267. }
  268. int instr_is_relative_link_branch(unsigned int instr)
  269. {
  270. return instr_is_relative_branch(instr) && (instr & BRANCH_SET_LINK);
  271. }
  272. static unsigned long branch_iform_target(const unsigned int *instr)
  273. {
  274. signed long imm;
  275. imm = *instr & 0x3FFFFFC;
  276. /* If the top bit of the immediate value is set this is negative */
  277. if (imm & 0x2000000)
  278. imm -= 0x4000000;
  279. if ((*instr & BRANCH_ABSOLUTE) == 0)
  280. imm += (unsigned long)instr;
  281. return (unsigned long)imm;
  282. }
  283. static unsigned long branch_bform_target(const unsigned int *instr)
  284. {
  285. signed long imm;
  286. imm = *instr & 0xFFFC;
  287. /* If the top bit of the immediate value is set this is negative */
  288. if (imm & 0x8000)
  289. imm -= 0x10000;
  290. if ((*instr & BRANCH_ABSOLUTE) == 0)
  291. imm += (unsigned long)instr;
  292. return (unsigned long)imm;
  293. }
  294. unsigned long branch_target(const unsigned int *instr)
  295. {
  296. if (instr_is_branch_iform(*instr))
  297. return branch_iform_target(instr);
  298. else if (instr_is_branch_bform(*instr))
  299. return branch_bform_target(instr);
  300. return 0;
  301. }
  302. int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
  303. {
  304. if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
  305. return branch_target(instr) == addr;
  306. return 0;
  307. }
  308. unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
  309. {
  310. unsigned long target;
  311. target = branch_target(src);
  312. if (instr_is_branch_iform(*src))
  313. return create_branch(dest, target, *src);
  314. else if (instr_is_branch_bform(*src))
  315. return create_cond_branch(dest, target, *src);
  316. return 0;
  317. }
  318. #ifdef CONFIG_PPC_BOOK3E_64
  319. void __patch_exception(int exc, unsigned long addr)
  320. {
  321. extern unsigned int interrupt_base_book3e;
  322. unsigned int *ibase = &interrupt_base_book3e;
  323. /* Our exceptions vectors start with a NOP and -then- a branch
  324. * to deal with single stepping from userspace which stops on
  325. * the second instruction. Thus we need to patch the second
  326. * instruction of the exception, not the first one
  327. */
  328. patch_branch(ibase + (exc / 4) + 1, addr, 0);
  329. }
  330. #endif
  331. #ifdef CONFIG_CODE_PATCHING_SELFTEST
  332. static void __init test_trampoline(void)
  333. {
  334. asm ("nop;\n");
  335. }
  336. #define check(x) \
  337. if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
  338. static void __init test_branch_iform(void)
  339. {
  340. unsigned int instr;
  341. unsigned long addr;
  342. addr = (unsigned long)&instr;
  343. /* The simplest case, branch to self, no flags */
  344. check(instr_is_branch_iform(0x48000000));
  345. /* All bits of target set, and flags */
  346. check(instr_is_branch_iform(0x4bffffff));
  347. /* High bit of opcode set, which is wrong */
  348. check(!instr_is_branch_iform(0xcbffffff));
  349. /* Middle bits of opcode set, which is wrong */
  350. check(!instr_is_branch_iform(0x7bffffff));
  351. /* Simplest case, branch to self with link */
  352. check(instr_is_branch_iform(0x48000001));
  353. /* All bits of targets set */
  354. check(instr_is_branch_iform(0x4bfffffd));
  355. /* Some bits of targets set */
  356. check(instr_is_branch_iform(0x4bff00fd));
  357. /* Must be a valid branch to start with */
  358. check(!instr_is_branch_iform(0x7bfffffd));
  359. /* Absolute branch to 0x100 */
  360. instr = 0x48000103;
  361. check(instr_is_branch_to_addr(&instr, 0x100));
  362. /* Absolute branch to 0x420fc */
  363. instr = 0x480420ff;
  364. check(instr_is_branch_to_addr(&instr, 0x420fc));
  365. /* Maximum positive relative branch, + 20MB - 4B */
  366. instr = 0x49fffffc;
  367. check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
  368. /* Smallest negative relative branch, - 4B */
  369. instr = 0x4bfffffc;
  370. check(instr_is_branch_to_addr(&instr, addr - 4));
  371. /* Largest negative relative branch, - 32 MB */
  372. instr = 0x4a000000;
  373. check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
  374. /* Branch to self, with link */
  375. instr = create_branch(&instr, addr, BRANCH_SET_LINK);
  376. check(instr_is_branch_to_addr(&instr, addr));
  377. /* Branch to self - 0x100, with link */
  378. instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
  379. check(instr_is_branch_to_addr(&instr, addr - 0x100));
  380. /* Branch to self + 0x100, no link */
  381. instr = create_branch(&instr, addr + 0x100, 0);
  382. check(instr_is_branch_to_addr(&instr, addr + 0x100));
  383. /* Maximum relative negative offset, - 32 MB */
  384. instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
  385. check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
  386. /* Out of range relative negative offset, - 32 MB + 4*/
  387. instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
  388. check(instr == 0);
  389. /* Out of range relative positive offset, + 32 MB */
  390. instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
  391. check(instr == 0);
  392. /* Unaligned target */
  393. instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
  394. check(instr == 0);
  395. /* Check flags are masked correctly */
  396. instr = create_branch(&instr, addr, 0xFFFFFFFC);
  397. check(instr_is_branch_to_addr(&instr, addr));
  398. check(instr == 0x48000000);
  399. }
  400. static void __init test_create_function_call(void)
  401. {
  402. unsigned int *iptr;
  403. unsigned long dest;
  404. /* Check we can create a function call */
  405. iptr = (unsigned int *)ppc_function_entry(test_trampoline);
  406. dest = ppc_function_entry(test_create_function_call);
  407. patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
  408. check(instr_is_branch_to_addr(iptr, dest));
  409. }
  410. static void __init test_branch_bform(void)
  411. {
  412. unsigned long addr;
  413. unsigned int *iptr, instr, flags;
  414. iptr = &instr;
  415. addr = (unsigned long)iptr;
  416. /* The simplest case, branch to self, no flags */
  417. check(instr_is_branch_bform(0x40000000));
  418. /* All bits of target set, and flags */
  419. check(instr_is_branch_bform(0x43ffffff));
  420. /* High bit of opcode set, which is wrong */
  421. check(!instr_is_branch_bform(0xc3ffffff));
  422. /* Middle bits of opcode set, which is wrong */
  423. check(!instr_is_branch_bform(0x7bffffff));
  424. /* Absolute conditional branch to 0x100 */
  425. instr = 0x43ff0103;
  426. check(instr_is_branch_to_addr(&instr, 0x100));
  427. /* Absolute conditional branch to 0x20fc */
  428. instr = 0x43ff20ff;
  429. check(instr_is_branch_to_addr(&instr, 0x20fc));
  430. /* Maximum positive relative conditional branch, + 32 KB - 4B */
  431. instr = 0x43ff7ffc;
  432. check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
  433. /* Smallest negative relative conditional branch, - 4B */
  434. instr = 0x43fffffc;
  435. check(instr_is_branch_to_addr(&instr, addr - 4));
  436. /* Largest negative relative conditional branch, - 32 KB */
  437. instr = 0x43ff8000;
  438. check(instr_is_branch_to_addr(&instr, addr - 0x8000));
  439. /* All condition code bits set & link */
  440. flags = 0x3ff000 | BRANCH_SET_LINK;
  441. /* Branch to self */
  442. instr = create_cond_branch(iptr, addr, flags);
  443. check(instr_is_branch_to_addr(&instr, addr));
  444. /* Branch to self - 0x100 */
  445. instr = create_cond_branch(iptr, addr - 0x100, flags);
  446. check(instr_is_branch_to_addr(&instr, addr - 0x100));
  447. /* Branch to self + 0x100 */
  448. instr = create_cond_branch(iptr, addr + 0x100, flags);
  449. check(instr_is_branch_to_addr(&instr, addr + 0x100));
  450. /* Maximum relative negative offset, - 32 KB */
  451. instr = create_cond_branch(iptr, addr - 0x8000, flags);
  452. check(instr_is_branch_to_addr(&instr, addr - 0x8000));
  453. /* Out of range relative negative offset, - 32 KB + 4*/
  454. instr = create_cond_branch(iptr, addr - 0x8004, flags);
  455. check(instr == 0);
  456. /* Out of range relative positive offset, + 32 KB */
  457. instr = create_cond_branch(iptr, addr + 0x8000, flags);
  458. check(instr == 0);
  459. /* Unaligned target */
  460. instr = create_cond_branch(iptr, addr + 3, flags);
  461. check(instr == 0);
  462. /* Check flags are masked correctly */
  463. instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
  464. check(instr_is_branch_to_addr(&instr, addr));
  465. check(instr == 0x43FF0000);
  466. }
  467. static void __init test_translate_branch(void)
  468. {
  469. unsigned long addr;
  470. unsigned int *p, *q;
  471. void *buf;
  472. buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
  473. check(buf);
  474. if (!buf)
  475. return;
  476. /* Simple case, branch to self moved a little */
  477. p = buf;
  478. addr = (unsigned long)p;
  479. patch_branch(p, addr, 0);
  480. check(instr_is_branch_to_addr(p, addr));
  481. q = p + 1;
  482. patch_instruction(q, translate_branch(q, p));
  483. check(instr_is_branch_to_addr(q, addr));
  484. /* Maximum negative case, move b . to addr + 32 MB */
  485. p = buf;
  486. addr = (unsigned long)p;
  487. patch_branch(p, addr, 0);
  488. q = buf + 0x2000000;
  489. patch_instruction(q, translate_branch(q, p));
  490. check(instr_is_branch_to_addr(p, addr));
  491. check(instr_is_branch_to_addr(q, addr));
  492. check(*q == 0x4a000000);
  493. /* Maximum positive case, move x to x - 32 MB + 4 */
  494. p = buf + 0x2000000;
  495. addr = (unsigned long)p;
  496. patch_branch(p, addr, 0);
  497. q = buf + 4;
  498. patch_instruction(q, translate_branch(q, p));
  499. check(instr_is_branch_to_addr(p, addr));
  500. check(instr_is_branch_to_addr(q, addr));
  501. check(*q == 0x49fffffc);
  502. /* Jump to x + 16 MB moved to x + 20 MB */
  503. p = buf;
  504. addr = 0x1000000 + (unsigned long)buf;
  505. patch_branch(p, addr, BRANCH_SET_LINK);
  506. q = buf + 0x1400000;
  507. patch_instruction(q, translate_branch(q, p));
  508. check(instr_is_branch_to_addr(p, addr));
  509. check(instr_is_branch_to_addr(q, addr));
  510. /* Jump to x + 16 MB moved to x - 16 MB + 4 */
  511. p = buf + 0x1000000;
  512. addr = 0x2000000 + (unsigned long)buf;
  513. patch_branch(p, addr, 0);
  514. q = buf + 4;
  515. patch_instruction(q, translate_branch(q, p));
  516. check(instr_is_branch_to_addr(p, addr));
  517. check(instr_is_branch_to_addr(q, addr));
  518. /* Conditional branch tests */
  519. /* Simple case, branch to self moved a little */
  520. p = buf;
  521. addr = (unsigned long)p;
  522. patch_instruction(p, create_cond_branch(p, addr, 0));
  523. check(instr_is_branch_to_addr(p, addr));
  524. q = p + 1;
  525. patch_instruction(q, translate_branch(q, p));
  526. check(instr_is_branch_to_addr(q, addr));
  527. /* Maximum negative case, move b . to addr + 32 KB */
  528. p = buf;
  529. addr = (unsigned long)p;
  530. patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
  531. q = buf + 0x8000;
  532. patch_instruction(q, translate_branch(q, p));
  533. check(instr_is_branch_to_addr(p, addr));
  534. check(instr_is_branch_to_addr(q, addr));
  535. check(*q == 0x43ff8000);
  536. /* Maximum positive case, move x to x - 32 KB + 4 */
  537. p = buf + 0x8000;
  538. addr = (unsigned long)p;
  539. patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
  540. q = buf + 4;
  541. patch_instruction(q, translate_branch(q, p));
  542. check(instr_is_branch_to_addr(p, addr));
  543. check(instr_is_branch_to_addr(q, addr));
  544. check(*q == 0x43ff7ffc);
  545. /* Jump to x + 12 KB moved to x + 20 KB */
  546. p = buf;
  547. addr = 0x3000 + (unsigned long)buf;
  548. patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
  549. q = buf + 0x5000;
  550. patch_instruction(q, translate_branch(q, p));
  551. check(instr_is_branch_to_addr(p, addr));
  552. check(instr_is_branch_to_addr(q, addr));
  553. /* Jump to x + 8 KB moved to x - 8 KB + 4 */
  554. p = buf + 0x2000;
  555. addr = 0x4000 + (unsigned long)buf;
  556. patch_instruction(p, create_cond_branch(p, addr, 0));
  557. q = buf + 4;
  558. patch_instruction(q, translate_branch(q, p));
  559. check(instr_is_branch_to_addr(p, addr));
  560. check(instr_is_branch_to_addr(q, addr));
  561. /* Free the buffer we were using */
  562. vfree(buf);
  563. }
  564. static int __init test_code_patching(void)
  565. {
  566. printk(KERN_DEBUG "Running code patching self-tests ...\n");
  567. test_branch_iform();
  568. test_branch_bform();
  569. test_create_function_call();
  570. test_translate_branch();
  571. return 0;
  572. }
  573. late_initcall(test_code_patching);
  574. #endif /* CONFIG_CODE_PATCHING_SELFTEST */