code-patching.c 12 KB

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