code-patching.c 13 KB

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