code-patching.c 14 KB

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