code-patching.c 19 KB

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