code-patching.c 12 KB

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