code-patching.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 <asm/page.h>
  15. #include <asm/code-patching.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/kprobes.h>
  18. int patch_instruction(unsigned int *addr, unsigned int instr)
  19. {
  20. int err;
  21. __put_user_size(instr, addr, 4, err);
  22. if (err)
  23. return err;
  24. asm ("dcbst 0, %0; sync; icbi 0,%0; sync; isync" : : "r" (addr));
  25. return 0;
  26. }
  27. int patch_branch(unsigned int *addr, unsigned long target, int flags)
  28. {
  29. return patch_instruction(addr, create_branch(addr, target, flags));
  30. }
  31. bool is_offset_in_branch_range(long offset)
  32. {
  33. /*
  34. * Powerpc branch instruction is :
  35. *
  36. * 0 6 30 31
  37. * +---------+----------------+---+---+
  38. * | opcode | LI |AA |LK |
  39. * +---------+----------------+---+---+
  40. * Where AA = 0 and LK = 0
  41. *
  42. * LI is a signed 24 bits integer. The real branch offset is computed
  43. * by: imm32 = SignExtend(LI:'0b00', 32);
  44. *
  45. * So the maximum forward branch should be:
  46. * (0x007fffff << 2) = 0x01fffffc = 0x1fffffc
  47. * The maximum backward branch should be:
  48. * (0xff800000 << 2) = 0xfe000000 = -0x2000000
  49. */
  50. return (offset >= -0x2000000 && offset <= 0x1fffffc && !(offset & 0x3));
  51. }
  52. /*
  53. * Helper to check if a given instruction is a conditional branch
  54. * Derived from the conditional checks in analyse_instr()
  55. */
  56. bool is_conditional_branch(unsigned int instr)
  57. {
  58. unsigned int opcode = instr >> 26;
  59. if (opcode == 16) /* bc, bca, bcl, bcla */
  60. return true;
  61. if (opcode == 19) {
  62. switch ((instr >> 1) & 0x3ff) {
  63. case 16: /* bclr, bclrl */
  64. case 528: /* bcctr, bcctrl */
  65. case 560: /* bctar, bctarl */
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. NOKPROBE_SYMBOL(is_conditional_branch);
  72. unsigned int create_branch(const unsigned int *addr,
  73. unsigned long target, int flags)
  74. {
  75. unsigned int instruction;
  76. long offset;
  77. offset = target;
  78. if (! (flags & BRANCH_ABSOLUTE))
  79. offset = offset - (unsigned long)addr;
  80. /* Check we can represent the target in the instruction format */
  81. if (!is_offset_in_branch_range(offset))
  82. return 0;
  83. /* Mask out the flags and target, so they don't step on each other. */
  84. instruction = 0x48000000 | (flags & 0x3) | (offset & 0x03FFFFFC);
  85. return instruction;
  86. }
  87. unsigned int create_cond_branch(const unsigned int *addr,
  88. unsigned long target, int flags)
  89. {
  90. unsigned int instruction;
  91. long offset;
  92. offset = target;
  93. if (! (flags & BRANCH_ABSOLUTE))
  94. offset = offset - (unsigned long)addr;
  95. /* Check we can represent the target in the instruction format */
  96. if (offset < -0x8000 || offset > 0x7FFF || offset & 0x3)
  97. return 0;
  98. /* Mask out the flags and target, so they don't step on each other. */
  99. instruction = 0x40000000 | (flags & 0x3FF0003) | (offset & 0xFFFC);
  100. return instruction;
  101. }
  102. static unsigned int branch_opcode(unsigned int instr)
  103. {
  104. return (instr >> 26) & 0x3F;
  105. }
  106. static int instr_is_branch_iform(unsigned int instr)
  107. {
  108. return branch_opcode(instr) == 18;
  109. }
  110. static int instr_is_branch_bform(unsigned int instr)
  111. {
  112. return branch_opcode(instr) == 16;
  113. }
  114. int instr_is_relative_branch(unsigned int instr)
  115. {
  116. if (instr & BRANCH_ABSOLUTE)
  117. return 0;
  118. return instr_is_branch_iform(instr) || instr_is_branch_bform(instr);
  119. }
  120. static unsigned long branch_iform_target(const unsigned int *instr)
  121. {
  122. signed long imm;
  123. imm = *instr & 0x3FFFFFC;
  124. /* If the top bit of the immediate value is set this is negative */
  125. if (imm & 0x2000000)
  126. imm -= 0x4000000;
  127. if ((*instr & BRANCH_ABSOLUTE) == 0)
  128. imm += (unsigned long)instr;
  129. return (unsigned long)imm;
  130. }
  131. static unsigned long branch_bform_target(const unsigned int *instr)
  132. {
  133. signed long imm;
  134. imm = *instr & 0xFFFC;
  135. /* If the top bit of the immediate value is set this is negative */
  136. if (imm & 0x8000)
  137. imm -= 0x10000;
  138. if ((*instr & BRANCH_ABSOLUTE) == 0)
  139. imm += (unsigned long)instr;
  140. return (unsigned long)imm;
  141. }
  142. unsigned long branch_target(const unsigned int *instr)
  143. {
  144. if (instr_is_branch_iform(*instr))
  145. return branch_iform_target(instr);
  146. else if (instr_is_branch_bform(*instr))
  147. return branch_bform_target(instr);
  148. return 0;
  149. }
  150. int instr_is_branch_to_addr(const unsigned int *instr, unsigned long addr)
  151. {
  152. if (instr_is_branch_iform(*instr) || instr_is_branch_bform(*instr))
  153. return branch_target(instr) == addr;
  154. return 0;
  155. }
  156. unsigned int translate_branch(const unsigned int *dest, const unsigned int *src)
  157. {
  158. unsigned long target;
  159. target = branch_target(src);
  160. if (instr_is_branch_iform(*src))
  161. return create_branch(dest, target, *src);
  162. else if (instr_is_branch_bform(*src))
  163. return create_cond_branch(dest, target, *src);
  164. return 0;
  165. }
  166. #ifdef CONFIG_PPC_BOOK3E_64
  167. void __patch_exception(int exc, unsigned long addr)
  168. {
  169. extern unsigned int interrupt_base_book3e;
  170. unsigned int *ibase = &interrupt_base_book3e;
  171. /* Our exceptions vectors start with a NOP and -then- a branch
  172. * to deal with single stepping from userspace which stops on
  173. * the second instruction. Thus we need to patch the second
  174. * instruction of the exception, not the first one
  175. */
  176. patch_branch(ibase + (exc / 4) + 1, addr, 0);
  177. }
  178. #endif
  179. #ifdef CONFIG_CODE_PATCHING_SELFTEST
  180. static void __init test_trampoline(void)
  181. {
  182. asm ("nop;\n");
  183. }
  184. #define check(x) \
  185. if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
  186. static void __init test_branch_iform(void)
  187. {
  188. unsigned int instr;
  189. unsigned long addr;
  190. addr = (unsigned long)&instr;
  191. /* The simplest case, branch to self, no flags */
  192. check(instr_is_branch_iform(0x48000000));
  193. /* All bits of target set, and flags */
  194. check(instr_is_branch_iform(0x4bffffff));
  195. /* High bit of opcode set, which is wrong */
  196. check(!instr_is_branch_iform(0xcbffffff));
  197. /* Middle bits of opcode set, which is wrong */
  198. check(!instr_is_branch_iform(0x7bffffff));
  199. /* Simplest case, branch to self with link */
  200. check(instr_is_branch_iform(0x48000001));
  201. /* All bits of targets set */
  202. check(instr_is_branch_iform(0x4bfffffd));
  203. /* Some bits of targets set */
  204. check(instr_is_branch_iform(0x4bff00fd));
  205. /* Must be a valid branch to start with */
  206. check(!instr_is_branch_iform(0x7bfffffd));
  207. /* Absolute branch to 0x100 */
  208. instr = 0x48000103;
  209. check(instr_is_branch_to_addr(&instr, 0x100));
  210. /* Absolute branch to 0x420fc */
  211. instr = 0x480420ff;
  212. check(instr_is_branch_to_addr(&instr, 0x420fc));
  213. /* Maximum positive relative branch, + 20MB - 4B */
  214. instr = 0x49fffffc;
  215. check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
  216. /* Smallest negative relative branch, - 4B */
  217. instr = 0x4bfffffc;
  218. check(instr_is_branch_to_addr(&instr, addr - 4));
  219. /* Largest negative relative branch, - 32 MB */
  220. instr = 0x4a000000;
  221. check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
  222. /* Branch to self, with link */
  223. instr = create_branch(&instr, addr, BRANCH_SET_LINK);
  224. check(instr_is_branch_to_addr(&instr, addr));
  225. /* Branch to self - 0x100, with link */
  226. instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
  227. check(instr_is_branch_to_addr(&instr, addr - 0x100));
  228. /* Branch to self + 0x100, no link */
  229. instr = create_branch(&instr, addr + 0x100, 0);
  230. check(instr_is_branch_to_addr(&instr, addr + 0x100));
  231. /* Maximum relative negative offset, - 32 MB */
  232. instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
  233. check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
  234. /* Out of range relative negative offset, - 32 MB + 4*/
  235. instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
  236. check(instr == 0);
  237. /* Out of range relative positive offset, + 32 MB */
  238. instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
  239. check(instr == 0);
  240. /* Unaligned target */
  241. instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
  242. check(instr == 0);
  243. /* Check flags are masked correctly */
  244. instr = create_branch(&instr, addr, 0xFFFFFFFC);
  245. check(instr_is_branch_to_addr(&instr, addr));
  246. check(instr == 0x48000000);
  247. }
  248. static void __init test_create_function_call(void)
  249. {
  250. unsigned int *iptr;
  251. unsigned long dest;
  252. /* Check we can create a function call */
  253. iptr = (unsigned int *)ppc_function_entry(test_trampoline);
  254. dest = ppc_function_entry(test_create_function_call);
  255. patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
  256. check(instr_is_branch_to_addr(iptr, dest));
  257. }
  258. static void __init test_branch_bform(void)
  259. {
  260. unsigned long addr;
  261. unsigned int *iptr, instr, flags;
  262. iptr = &instr;
  263. addr = (unsigned long)iptr;
  264. /* The simplest case, branch to self, no flags */
  265. check(instr_is_branch_bform(0x40000000));
  266. /* All bits of target set, and flags */
  267. check(instr_is_branch_bform(0x43ffffff));
  268. /* High bit of opcode set, which is wrong */
  269. check(!instr_is_branch_bform(0xc3ffffff));
  270. /* Middle bits of opcode set, which is wrong */
  271. check(!instr_is_branch_bform(0x7bffffff));
  272. /* Absolute conditional branch to 0x100 */
  273. instr = 0x43ff0103;
  274. check(instr_is_branch_to_addr(&instr, 0x100));
  275. /* Absolute conditional branch to 0x20fc */
  276. instr = 0x43ff20ff;
  277. check(instr_is_branch_to_addr(&instr, 0x20fc));
  278. /* Maximum positive relative conditional branch, + 32 KB - 4B */
  279. instr = 0x43ff7ffc;
  280. check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
  281. /* Smallest negative relative conditional branch, - 4B */
  282. instr = 0x43fffffc;
  283. check(instr_is_branch_to_addr(&instr, addr - 4));
  284. /* Largest negative relative conditional branch, - 32 KB */
  285. instr = 0x43ff8000;
  286. check(instr_is_branch_to_addr(&instr, addr - 0x8000));
  287. /* All condition code bits set & link */
  288. flags = 0x3ff000 | BRANCH_SET_LINK;
  289. /* Branch to self */
  290. instr = create_cond_branch(iptr, addr, flags);
  291. check(instr_is_branch_to_addr(&instr, addr));
  292. /* Branch to self - 0x100 */
  293. instr = create_cond_branch(iptr, addr - 0x100, flags);
  294. check(instr_is_branch_to_addr(&instr, addr - 0x100));
  295. /* Branch to self + 0x100 */
  296. instr = create_cond_branch(iptr, addr + 0x100, flags);
  297. check(instr_is_branch_to_addr(&instr, addr + 0x100));
  298. /* Maximum relative negative offset, - 32 KB */
  299. instr = create_cond_branch(iptr, addr - 0x8000, flags);
  300. check(instr_is_branch_to_addr(&instr, addr - 0x8000));
  301. /* Out of range relative negative offset, - 32 KB + 4*/
  302. instr = create_cond_branch(iptr, addr - 0x8004, flags);
  303. check(instr == 0);
  304. /* Out of range relative positive offset, + 32 KB */
  305. instr = create_cond_branch(iptr, addr + 0x8000, flags);
  306. check(instr == 0);
  307. /* Unaligned target */
  308. instr = create_cond_branch(iptr, addr + 3, flags);
  309. check(instr == 0);
  310. /* Check flags are masked correctly */
  311. instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
  312. check(instr_is_branch_to_addr(&instr, addr));
  313. check(instr == 0x43FF0000);
  314. }
  315. static void __init test_translate_branch(void)
  316. {
  317. unsigned long addr;
  318. unsigned int *p, *q;
  319. void *buf;
  320. buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
  321. check(buf);
  322. if (!buf)
  323. return;
  324. /* Simple case, branch to self moved a little */
  325. p = buf;
  326. addr = (unsigned long)p;
  327. patch_branch(p, addr, 0);
  328. check(instr_is_branch_to_addr(p, addr));
  329. q = p + 1;
  330. patch_instruction(q, translate_branch(q, p));
  331. check(instr_is_branch_to_addr(q, addr));
  332. /* Maximum negative case, move b . to addr + 32 MB */
  333. p = buf;
  334. addr = (unsigned long)p;
  335. patch_branch(p, addr, 0);
  336. q = buf + 0x2000000;
  337. patch_instruction(q, translate_branch(q, p));
  338. check(instr_is_branch_to_addr(p, addr));
  339. check(instr_is_branch_to_addr(q, addr));
  340. check(*q == 0x4a000000);
  341. /* Maximum positive case, move x to x - 32 MB + 4 */
  342. p = buf + 0x2000000;
  343. addr = (unsigned long)p;
  344. patch_branch(p, addr, 0);
  345. q = buf + 4;
  346. patch_instruction(q, translate_branch(q, p));
  347. check(instr_is_branch_to_addr(p, addr));
  348. check(instr_is_branch_to_addr(q, addr));
  349. check(*q == 0x49fffffc);
  350. /* Jump to x + 16 MB moved to x + 20 MB */
  351. p = buf;
  352. addr = 0x1000000 + (unsigned long)buf;
  353. patch_branch(p, addr, BRANCH_SET_LINK);
  354. q = buf + 0x1400000;
  355. patch_instruction(q, translate_branch(q, p));
  356. check(instr_is_branch_to_addr(p, addr));
  357. check(instr_is_branch_to_addr(q, addr));
  358. /* Jump to x + 16 MB moved to x - 16 MB + 4 */
  359. p = buf + 0x1000000;
  360. addr = 0x2000000 + (unsigned long)buf;
  361. patch_branch(p, addr, 0);
  362. q = buf + 4;
  363. patch_instruction(q, translate_branch(q, p));
  364. check(instr_is_branch_to_addr(p, addr));
  365. check(instr_is_branch_to_addr(q, addr));
  366. /* Conditional branch tests */
  367. /* Simple case, branch to self moved a little */
  368. p = buf;
  369. addr = (unsigned long)p;
  370. patch_instruction(p, create_cond_branch(p, addr, 0));
  371. check(instr_is_branch_to_addr(p, addr));
  372. q = p + 1;
  373. patch_instruction(q, translate_branch(q, p));
  374. check(instr_is_branch_to_addr(q, addr));
  375. /* Maximum negative case, move b . to addr + 32 KB */
  376. p = buf;
  377. addr = (unsigned long)p;
  378. patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
  379. q = buf + 0x8000;
  380. patch_instruction(q, translate_branch(q, p));
  381. check(instr_is_branch_to_addr(p, addr));
  382. check(instr_is_branch_to_addr(q, addr));
  383. check(*q == 0x43ff8000);
  384. /* Maximum positive case, move x to x - 32 KB + 4 */
  385. p = buf + 0x8000;
  386. addr = (unsigned long)p;
  387. patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
  388. q = buf + 4;
  389. patch_instruction(q, translate_branch(q, p));
  390. check(instr_is_branch_to_addr(p, addr));
  391. check(instr_is_branch_to_addr(q, addr));
  392. check(*q == 0x43ff7ffc);
  393. /* Jump to x + 12 KB moved to x + 20 KB */
  394. p = buf;
  395. addr = 0x3000 + (unsigned long)buf;
  396. patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
  397. q = buf + 0x5000;
  398. patch_instruction(q, translate_branch(q, p));
  399. check(instr_is_branch_to_addr(p, addr));
  400. check(instr_is_branch_to_addr(q, addr));
  401. /* Jump to x + 8 KB moved to x - 8 KB + 4 */
  402. p = buf + 0x2000;
  403. addr = 0x4000 + (unsigned long)buf;
  404. patch_instruction(p, create_cond_branch(p, addr, 0));
  405. q = buf + 4;
  406. patch_instruction(q, translate_branch(q, p));
  407. check(instr_is_branch_to_addr(p, addr));
  408. check(instr_is_branch_to_addr(q, addr));
  409. /* Free the buffer we were using */
  410. vfree(buf);
  411. }
  412. static int __init test_code_patching(void)
  413. {
  414. printk(KERN_DEBUG "Running code patching self-tests ...\n");
  415. test_branch_iform();
  416. test_branch_bform();
  417. test_create_function_call();
  418. test_translate_branch();
  419. return 0;
  420. }
  421. late_initcall(test_code_patching);
  422. #endif /* CONFIG_CODE_PATCHING_SELFTEST */