code-patching.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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_CODE_PATCHING_SELFTEST
  124. static void __init test_trampoline(void)
  125. {
  126. asm ("nop;\n");
  127. }
  128. #define check(x) \
  129. if (!(x)) printk("code-patching: test failed at line %d\n", __LINE__);
  130. static void __init test_branch_iform(void)
  131. {
  132. unsigned int instr;
  133. unsigned long addr;
  134. addr = (unsigned long)&instr;
  135. /* The simplest case, branch to self, no flags */
  136. check(instr_is_branch_iform(0x48000000));
  137. /* All bits of target set, and flags */
  138. check(instr_is_branch_iform(0x4bffffff));
  139. /* High bit of opcode set, which is wrong */
  140. check(!instr_is_branch_iform(0xcbffffff));
  141. /* Middle bits of opcode set, which is wrong */
  142. check(!instr_is_branch_iform(0x7bffffff));
  143. /* Simplest case, branch to self with link */
  144. check(instr_is_branch_iform(0x48000001));
  145. /* All bits of targets set */
  146. check(instr_is_branch_iform(0x4bfffffd));
  147. /* Some bits of targets set */
  148. check(instr_is_branch_iform(0x4bff00fd));
  149. /* Must be a valid branch to start with */
  150. check(!instr_is_branch_iform(0x7bfffffd));
  151. /* Absolute branch to 0x100 */
  152. instr = 0x48000103;
  153. check(instr_is_branch_to_addr(&instr, 0x100));
  154. /* Absolute branch to 0x420fc */
  155. instr = 0x480420ff;
  156. check(instr_is_branch_to_addr(&instr, 0x420fc));
  157. /* Maximum positive relative branch, + 20MB - 4B */
  158. instr = 0x49fffffc;
  159. check(instr_is_branch_to_addr(&instr, addr + 0x1FFFFFC));
  160. /* Smallest negative relative branch, - 4B */
  161. instr = 0x4bfffffc;
  162. check(instr_is_branch_to_addr(&instr, addr - 4));
  163. /* Largest negative relative branch, - 32 MB */
  164. instr = 0x4a000000;
  165. check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
  166. /* Branch to self, with link */
  167. instr = create_branch(&instr, addr, BRANCH_SET_LINK);
  168. check(instr_is_branch_to_addr(&instr, addr));
  169. /* Branch to self - 0x100, with link */
  170. instr = create_branch(&instr, addr - 0x100, BRANCH_SET_LINK);
  171. check(instr_is_branch_to_addr(&instr, addr - 0x100));
  172. /* Branch to self + 0x100, no link */
  173. instr = create_branch(&instr, addr + 0x100, 0);
  174. check(instr_is_branch_to_addr(&instr, addr + 0x100));
  175. /* Maximum relative negative offset, - 32 MB */
  176. instr = create_branch(&instr, addr - 0x2000000, BRANCH_SET_LINK);
  177. check(instr_is_branch_to_addr(&instr, addr - 0x2000000));
  178. /* Out of range relative negative offset, - 32 MB + 4*/
  179. instr = create_branch(&instr, addr - 0x2000004, BRANCH_SET_LINK);
  180. check(instr == 0);
  181. /* Out of range relative positive offset, + 32 MB */
  182. instr = create_branch(&instr, addr + 0x2000000, BRANCH_SET_LINK);
  183. check(instr == 0);
  184. /* Unaligned target */
  185. instr = create_branch(&instr, addr + 3, BRANCH_SET_LINK);
  186. check(instr == 0);
  187. /* Check flags are masked correctly */
  188. instr = create_branch(&instr, addr, 0xFFFFFFFC);
  189. check(instr_is_branch_to_addr(&instr, addr));
  190. check(instr == 0x48000000);
  191. }
  192. static void __init test_create_function_call(void)
  193. {
  194. unsigned int *iptr;
  195. unsigned long dest;
  196. /* Check we can create a function call */
  197. iptr = (unsigned int *)ppc_function_entry(test_trampoline);
  198. dest = ppc_function_entry(test_create_function_call);
  199. patch_instruction(iptr, create_branch(iptr, dest, BRANCH_SET_LINK));
  200. check(instr_is_branch_to_addr(iptr, dest));
  201. }
  202. static void __init test_branch_bform(void)
  203. {
  204. unsigned long addr;
  205. unsigned int *iptr, instr, flags;
  206. iptr = &instr;
  207. addr = (unsigned long)iptr;
  208. /* The simplest case, branch to self, no flags */
  209. check(instr_is_branch_bform(0x40000000));
  210. /* All bits of target set, and flags */
  211. check(instr_is_branch_bform(0x43ffffff));
  212. /* High bit of opcode set, which is wrong */
  213. check(!instr_is_branch_bform(0xc3ffffff));
  214. /* Middle bits of opcode set, which is wrong */
  215. check(!instr_is_branch_bform(0x7bffffff));
  216. /* Absolute conditional branch to 0x100 */
  217. instr = 0x43ff0103;
  218. check(instr_is_branch_to_addr(&instr, 0x100));
  219. /* Absolute conditional branch to 0x20fc */
  220. instr = 0x43ff20ff;
  221. check(instr_is_branch_to_addr(&instr, 0x20fc));
  222. /* Maximum positive relative conditional branch, + 32 KB - 4B */
  223. instr = 0x43ff7ffc;
  224. check(instr_is_branch_to_addr(&instr, addr + 0x7FFC));
  225. /* Smallest negative relative conditional branch, - 4B */
  226. instr = 0x43fffffc;
  227. check(instr_is_branch_to_addr(&instr, addr - 4));
  228. /* Largest negative relative conditional branch, - 32 KB */
  229. instr = 0x43ff8000;
  230. check(instr_is_branch_to_addr(&instr, addr - 0x8000));
  231. /* All condition code bits set & link */
  232. flags = 0x3ff000 | BRANCH_SET_LINK;
  233. /* Branch to self */
  234. instr = create_cond_branch(iptr, addr, flags);
  235. check(instr_is_branch_to_addr(&instr, addr));
  236. /* Branch to self - 0x100 */
  237. instr = create_cond_branch(iptr, addr - 0x100, flags);
  238. check(instr_is_branch_to_addr(&instr, addr - 0x100));
  239. /* Branch to self + 0x100 */
  240. instr = create_cond_branch(iptr, addr + 0x100, flags);
  241. check(instr_is_branch_to_addr(&instr, addr + 0x100));
  242. /* Maximum relative negative offset, - 32 KB */
  243. instr = create_cond_branch(iptr, addr - 0x8000, flags);
  244. check(instr_is_branch_to_addr(&instr, addr - 0x8000));
  245. /* Out of range relative negative offset, - 32 KB + 4*/
  246. instr = create_cond_branch(iptr, addr - 0x8004, flags);
  247. check(instr == 0);
  248. /* Out of range relative positive offset, + 32 KB */
  249. instr = create_cond_branch(iptr, addr + 0x8000, flags);
  250. check(instr == 0);
  251. /* Unaligned target */
  252. instr = create_cond_branch(iptr, addr + 3, flags);
  253. check(instr == 0);
  254. /* Check flags are masked correctly */
  255. instr = create_cond_branch(iptr, addr, 0xFFFFFFFC);
  256. check(instr_is_branch_to_addr(&instr, addr));
  257. check(instr == 0x43FF0000);
  258. }
  259. static void __init test_translate_branch(void)
  260. {
  261. unsigned long addr;
  262. unsigned int *p, *q;
  263. void *buf;
  264. buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
  265. check(buf);
  266. if (!buf)
  267. return;
  268. /* Simple case, branch to self moved a little */
  269. p = buf;
  270. addr = (unsigned long)p;
  271. patch_branch(p, addr, 0);
  272. check(instr_is_branch_to_addr(p, addr));
  273. q = p + 1;
  274. patch_instruction(q, translate_branch(q, p));
  275. check(instr_is_branch_to_addr(q, addr));
  276. /* Maximum negative case, move b . to addr + 32 MB */
  277. p = buf;
  278. addr = (unsigned long)p;
  279. patch_branch(p, addr, 0);
  280. q = buf + 0x2000000;
  281. patch_instruction(q, translate_branch(q, p));
  282. check(instr_is_branch_to_addr(p, addr));
  283. check(instr_is_branch_to_addr(q, addr));
  284. check(*q == 0x4a000000);
  285. /* Maximum positive case, move x to x - 32 MB + 4 */
  286. p = buf + 0x2000000;
  287. addr = (unsigned long)p;
  288. patch_branch(p, addr, 0);
  289. q = buf + 4;
  290. patch_instruction(q, translate_branch(q, p));
  291. check(instr_is_branch_to_addr(p, addr));
  292. check(instr_is_branch_to_addr(q, addr));
  293. check(*q == 0x49fffffc);
  294. /* Jump to x + 16 MB moved to x + 20 MB */
  295. p = buf;
  296. addr = 0x1000000 + (unsigned long)buf;
  297. patch_branch(p, addr, BRANCH_SET_LINK);
  298. q = buf + 0x1400000;
  299. patch_instruction(q, translate_branch(q, p));
  300. check(instr_is_branch_to_addr(p, addr));
  301. check(instr_is_branch_to_addr(q, addr));
  302. /* Jump to x + 16 MB moved to x - 16 MB + 4 */
  303. p = buf + 0x1000000;
  304. addr = 0x2000000 + (unsigned long)buf;
  305. patch_branch(p, addr, 0);
  306. q = buf + 4;
  307. patch_instruction(q, translate_branch(q, p));
  308. check(instr_is_branch_to_addr(p, addr));
  309. check(instr_is_branch_to_addr(q, addr));
  310. /* Conditional branch tests */
  311. /* Simple case, branch to self moved a little */
  312. p = buf;
  313. addr = (unsigned long)p;
  314. patch_instruction(p, create_cond_branch(p, addr, 0));
  315. check(instr_is_branch_to_addr(p, addr));
  316. q = p + 1;
  317. patch_instruction(q, translate_branch(q, p));
  318. check(instr_is_branch_to_addr(q, addr));
  319. /* Maximum negative case, move b . to addr + 32 KB */
  320. p = buf;
  321. addr = (unsigned long)p;
  322. patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
  323. q = buf + 0x8000;
  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 == 0x43ff8000);
  328. /* Maximum positive case, move x to x - 32 KB + 4 */
  329. p = buf + 0x8000;
  330. addr = (unsigned long)p;
  331. patch_instruction(p, create_cond_branch(p, addr, 0xFFFFFFFC));
  332. q = buf + 4;
  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. check(*q == 0x43ff7ffc);
  337. /* Jump to x + 12 KB moved to x + 20 KB */
  338. p = buf;
  339. addr = 0x3000 + (unsigned long)buf;
  340. patch_instruction(p, create_cond_branch(p, addr, BRANCH_SET_LINK));
  341. q = buf + 0x5000;
  342. patch_instruction(q, translate_branch(q, p));
  343. check(instr_is_branch_to_addr(p, addr));
  344. check(instr_is_branch_to_addr(q, addr));
  345. /* Jump to x + 8 KB moved to x - 8 KB + 4 */
  346. p = buf + 0x2000;
  347. addr = 0x4000 + (unsigned long)buf;
  348. patch_instruction(p, create_cond_branch(p, addr, 0));
  349. q = buf + 4;
  350. patch_instruction(q, translate_branch(q, p));
  351. check(instr_is_branch_to_addr(p, addr));
  352. check(instr_is_branch_to_addr(q, addr));
  353. /* Free the buffer we were using */
  354. vfree(buf);
  355. }
  356. static int __init test_code_patching(void)
  357. {
  358. printk(KERN_DEBUG "Running code patching self-tests ...\n");
  359. test_branch_iform();
  360. test_branch_bform();
  361. test_create_function_call();
  362. test_translate_branch();
  363. return 0;
  364. }
  365. late_initcall(test_code_patching);
  366. #endif /* CONFIG_CODE_PATCHING_SELFTEST */