code-patching.c 14 KB

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