code-patching.c 14 KB

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