test-code-patching.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2008 Michael Ellerman, IBM Corporation.
  4. */
  5. #include <linux/vmalloc.h>
  6. #include <linux/init.h>
  7. #include <asm/code-patching.h>
  8. static int __init instr_is_branch_to_addr(const u32 *instr, unsigned long addr)
  9. {
  10. if (instr_is_branch_iform(ppc_inst_read(instr)) ||
  11. instr_is_branch_bform(ppc_inst_read(instr)))
  12. return branch_target(instr) == addr;
  13. return 0;
  14. }
  15. static void __init test_trampoline(void)
  16. {
  17. asm ("nop;nop;\n");
  18. }
  19. #define check(x) do { \
  20. if (!(x)) \
  21. pr_err("code-patching: test failed at line %d\n", __LINE__); \
  22. } while (0)
  23. static void __init test_branch_iform(void)
  24. {
  25. int err;
  26. ppc_inst_t instr;
  27. u32 tmp[2];
  28. u32 *iptr = tmp;
  29. unsigned long addr = (unsigned long)tmp;
  30. /* The simplest case, branch to self, no flags */
  31. check(instr_is_branch_iform(ppc_inst(0x48000000)));
  32. /* All bits of target set, and flags */
  33. check(instr_is_branch_iform(ppc_inst(0x4bffffff)));
  34. /* High bit of opcode set, which is wrong */
  35. check(!instr_is_branch_iform(ppc_inst(0xcbffffff)));
  36. /* Middle bits of opcode set, which is wrong */
  37. check(!instr_is_branch_iform(ppc_inst(0x7bffffff)));
  38. /* Simplest case, branch to self with link */
  39. check(instr_is_branch_iform(ppc_inst(0x48000001)));
  40. /* All bits of targets set */
  41. check(instr_is_branch_iform(ppc_inst(0x4bfffffd)));
  42. /* Some bits of targets set */
  43. check(instr_is_branch_iform(ppc_inst(0x4bff00fd)));
  44. /* Must be a valid branch to start with */
  45. check(!instr_is_branch_iform(ppc_inst(0x7bfffffd)));
  46. /* Absolute branch to 0x100 */
  47. ppc_inst_write(iptr, ppc_inst(0x48000103));
  48. check(instr_is_branch_to_addr(iptr, 0x100));
  49. /* Absolute branch to 0x420fc */
  50. ppc_inst_write(iptr, ppc_inst(0x480420ff));
  51. check(instr_is_branch_to_addr(iptr, 0x420fc));
  52. /* Maximum positive relative branch, + 20MB - 4B */
  53. ppc_inst_write(iptr, ppc_inst(0x49fffffc));
  54. check(instr_is_branch_to_addr(iptr, addr + 0x1FFFFFC));
  55. /* Smallest negative relative branch, - 4B */
  56. ppc_inst_write(iptr, ppc_inst(0x4bfffffc));
  57. check(instr_is_branch_to_addr(iptr, addr - 4));
  58. /* Largest negative relative branch, - 32 MB */
  59. ppc_inst_write(iptr, ppc_inst(0x4a000000));
  60. check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
  61. /* Branch to self, with link */
  62. err = create_branch(&instr, iptr, addr, BRANCH_SET_LINK);
  63. ppc_inst_write(iptr, instr);
  64. check(instr_is_branch_to_addr(iptr, addr));
  65. /* Branch to self - 0x100, with link */
  66. err = create_branch(&instr, iptr, addr - 0x100, BRANCH_SET_LINK);
  67. ppc_inst_write(iptr, instr);
  68. check(instr_is_branch_to_addr(iptr, addr - 0x100));
  69. /* Branch to self + 0x100, no link */
  70. err = create_branch(&instr, iptr, addr + 0x100, 0);
  71. ppc_inst_write(iptr, instr);
  72. check(instr_is_branch_to_addr(iptr, addr + 0x100));
  73. /* Maximum relative negative offset, - 32 MB */
  74. err = create_branch(&instr, iptr, addr - 0x2000000, BRANCH_SET_LINK);
  75. ppc_inst_write(iptr, instr);
  76. check(instr_is_branch_to_addr(iptr, addr - 0x2000000));
  77. /* Out of range relative negative offset, - 32 MB + 4*/
  78. err = create_branch(&instr, iptr, addr - 0x2000004, BRANCH_SET_LINK);
  79. check(err);
  80. /* Out of range relative positive offset, + 32 MB */
  81. err = create_branch(&instr, iptr, addr + 0x2000000, BRANCH_SET_LINK);
  82. check(err);
  83. /* Unaligned target */
  84. err = create_branch(&instr, iptr, addr + 3, BRANCH_SET_LINK);
  85. check(err);
  86. /* Check flags are masked correctly */
  87. err = create_branch(&instr, iptr, addr, 0xFFFFFFFC);
  88. ppc_inst_write(iptr, instr);
  89. check(instr_is_branch_to_addr(iptr, addr));
  90. check(ppc_inst_equal(instr, ppc_inst(0x48000000)));
  91. }
  92. static void __init test_create_function_call(void)
  93. {
  94. u32 *iptr;
  95. unsigned long dest;
  96. ppc_inst_t instr;
  97. /* Check we can create a function call */
  98. iptr = (u32 *)ppc_function_entry(test_trampoline);
  99. dest = ppc_function_entry(test_create_function_call);
  100. create_branch(&instr, iptr, dest, BRANCH_SET_LINK);
  101. patch_instruction(iptr, instr);
  102. check(instr_is_branch_to_addr(iptr, dest));
  103. }
  104. static void __init test_branch_bform(void)
  105. {
  106. int err;
  107. unsigned long addr;
  108. ppc_inst_t instr;
  109. u32 tmp[2];
  110. u32 *iptr = tmp;
  111. unsigned int flags;
  112. addr = (unsigned long)iptr;
  113. /* The simplest case, branch to self, no flags */
  114. check(instr_is_branch_bform(ppc_inst(0x40000000)));
  115. /* All bits of target set, and flags */
  116. check(instr_is_branch_bform(ppc_inst(0x43ffffff)));
  117. /* High bit of opcode set, which is wrong */
  118. check(!instr_is_branch_bform(ppc_inst(0xc3ffffff)));
  119. /* Middle bits of opcode set, which is wrong */
  120. check(!instr_is_branch_bform(ppc_inst(0x7bffffff)));
  121. /* Absolute conditional branch to 0x100 */
  122. ppc_inst_write(iptr, ppc_inst(0x43ff0103));
  123. check(instr_is_branch_to_addr(iptr, 0x100));
  124. /* Absolute conditional branch to 0x20fc */
  125. ppc_inst_write(iptr, ppc_inst(0x43ff20ff));
  126. check(instr_is_branch_to_addr(iptr, 0x20fc));
  127. /* Maximum positive relative conditional branch, + 32 KB - 4B */
  128. ppc_inst_write(iptr, ppc_inst(0x43ff7ffc));
  129. check(instr_is_branch_to_addr(iptr, addr + 0x7FFC));
  130. /* Smallest negative relative conditional branch, - 4B */
  131. ppc_inst_write(iptr, ppc_inst(0x43fffffc));
  132. check(instr_is_branch_to_addr(iptr, addr - 4));
  133. /* Largest negative relative conditional branch, - 32 KB */
  134. ppc_inst_write(iptr, ppc_inst(0x43ff8000));
  135. check(instr_is_branch_to_addr(iptr, addr - 0x8000));
  136. /* All condition code bits set & link */
  137. flags = 0x3ff000 | BRANCH_SET_LINK;
  138. /* Branch to self */
  139. err = create_cond_branch(&instr, iptr, addr, flags);
  140. ppc_inst_write(iptr, instr);
  141. check(instr_is_branch_to_addr(iptr, addr));
  142. /* Branch to self - 0x100 */
  143. err = create_cond_branch(&instr, iptr, addr - 0x100, flags);
  144. ppc_inst_write(iptr, instr);
  145. check(instr_is_branch_to_addr(iptr, addr - 0x100));
  146. /* Branch to self + 0x100 */
  147. err = create_cond_branch(&instr, iptr, addr + 0x100, flags);
  148. ppc_inst_write(iptr, instr);
  149. check(instr_is_branch_to_addr(iptr, addr + 0x100));
  150. /* Maximum relative negative offset, - 32 KB */
  151. err = create_cond_branch(&instr, iptr, addr - 0x8000, flags);
  152. ppc_inst_write(iptr, instr);
  153. check(instr_is_branch_to_addr(iptr, addr - 0x8000));
  154. /* Out of range relative negative offset, - 32 KB + 4*/
  155. err = create_cond_branch(&instr, iptr, addr - 0x8004, flags);
  156. check(err);
  157. /* Out of range relative positive offset, + 32 KB */
  158. err = create_cond_branch(&instr, iptr, addr + 0x8000, flags);
  159. check(err);
  160. /* Unaligned target */
  161. err = create_cond_branch(&instr, iptr, addr + 3, flags);
  162. check(err);
  163. /* Check flags are masked correctly */
  164. err = create_cond_branch(&instr, iptr, addr, 0xFFFFFFFC);
  165. ppc_inst_write(iptr, instr);
  166. check(instr_is_branch_to_addr(iptr, addr));
  167. check(ppc_inst_equal(instr, ppc_inst(0x43FF0000)));
  168. }
  169. static void __init test_translate_branch(void)
  170. {
  171. unsigned long addr;
  172. void *p, *q;
  173. ppc_inst_t instr;
  174. void *buf;
  175. buf = vmalloc(PAGE_ALIGN(0x2000000 + 1));
  176. check(buf);
  177. if (!buf)
  178. return;
  179. /* Simple case, branch to self moved a little */
  180. p = buf;
  181. addr = (unsigned long)p;
  182. create_branch(&instr, p, addr, 0);
  183. ppc_inst_write(p, instr);
  184. check(instr_is_branch_to_addr(p, addr));
  185. q = p + 4;
  186. translate_branch(&instr, q, p);
  187. ppc_inst_write(q, instr);
  188. check(instr_is_branch_to_addr(q, addr));
  189. /* Maximum negative case, move b . to addr + 32 MB */
  190. p = buf;
  191. addr = (unsigned long)p;
  192. create_branch(&instr, p, addr, 0);
  193. ppc_inst_write(p, instr);
  194. q = buf + 0x2000000;
  195. translate_branch(&instr, q, p);
  196. ppc_inst_write(q, instr);
  197. check(instr_is_branch_to_addr(p, addr));
  198. check(instr_is_branch_to_addr(q, addr));
  199. check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x4a000000)));
  200. /* Maximum positive case, move x to x - 32 MB + 4 */
  201. p = buf + 0x2000000;
  202. addr = (unsigned long)p;
  203. create_branch(&instr, p, addr, 0);
  204. ppc_inst_write(p, instr);
  205. q = buf + 4;
  206. translate_branch(&instr, q, p);
  207. ppc_inst_write(q, instr);
  208. check(instr_is_branch_to_addr(p, addr));
  209. check(instr_is_branch_to_addr(q, addr));
  210. check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x49fffffc)));
  211. /* Jump to x + 16 MB moved to x + 20 MB */
  212. p = buf;
  213. addr = 0x1000000 + (unsigned long)buf;
  214. create_branch(&instr, p, addr, BRANCH_SET_LINK);
  215. ppc_inst_write(p, instr);
  216. q = buf + 0x1400000;
  217. translate_branch(&instr, q, p);
  218. ppc_inst_write(q, instr);
  219. check(instr_is_branch_to_addr(p, addr));
  220. check(instr_is_branch_to_addr(q, addr));
  221. /* Jump to x + 16 MB moved to x - 16 MB + 4 */
  222. p = buf + 0x1000000;
  223. addr = 0x2000000 + (unsigned long)buf;
  224. create_branch(&instr, p, addr, 0);
  225. ppc_inst_write(p, instr);
  226. q = buf + 4;
  227. translate_branch(&instr, q, p);
  228. ppc_inst_write(q, instr);
  229. check(instr_is_branch_to_addr(p, addr));
  230. check(instr_is_branch_to_addr(q, addr));
  231. /* Conditional branch tests */
  232. /* Simple case, branch to self moved a little */
  233. p = buf;
  234. addr = (unsigned long)p;
  235. create_cond_branch(&instr, p, addr, 0);
  236. ppc_inst_write(p, instr);
  237. check(instr_is_branch_to_addr(p, addr));
  238. q = buf + 4;
  239. translate_branch(&instr, q, p);
  240. ppc_inst_write(q, instr);
  241. check(instr_is_branch_to_addr(q, addr));
  242. /* Maximum negative case, move b . to addr + 32 KB */
  243. p = buf;
  244. addr = (unsigned long)p;
  245. create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
  246. ppc_inst_write(p, instr);
  247. q = buf + 0x8000;
  248. translate_branch(&instr, q, p);
  249. ppc_inst_write(q, instr);
  250. check(instr_is_branch_to_addr(p, addr));
  251. check(instr_is_branch_to_addr(q, addr));
  252. check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff8000)));
  253. /* Maximum positive case, move x to x - 32 KB + 4 */
  254. p = buf + 0x8000;
  255. addr = (unsigned long)p;
  256. create_cond_branch(&instr, p, addr, 0xFFFFFFFC);
  257. ppc_inst_write(p, instr);
  258. q = buf + 4;
  259. translate_branch(&instr, q, p);
  260. ppc_inst_write(q, instr);
  261. check(instr_is_branch_to_addr(p, addr));
  262. check(instr_is_branch_to_addr(q, addr));
  263. check(ppc_inst_equal(ppc_inst_read(q), ppc_inst(0x43ff7ffc)));
  264. /* Jump to x + 12 KB moved to x + 20 KB */
  265. p = buf;
  266. addr = 0x3000 + (unsigned long)buf;
  267. create_cond_branch(&instr, p, addr, BRANCH_SET_LINK);
  268. ppc_inst_write(p, instr);
  269. q = buf + 0x5000;
  270. translate_branch(&instr, q, p);
  271. ppc_inst_write(q, instr);
  272. check(instr_is_branch_to_addr(p, addr));
  273. check(instr_is_branch_to_addr(q, addr));
  274. /* Jump to x + 8 KB moved to x - 8 KB + 4 */
  275. p = buf + 0x2000;
  276. addr = 0x4000 + (unsigned long)buf;
  277. create_cond_branch(&instr, p, addr, 0);
  278. ppc_inst_write(p, instr);
  279. q = buf + 4;
  280. translate_branch(&instr, q, p);
  281. ppc_inst_write(q, instr);
  282. check(instr_is_branch_to_addr(p, addr));
  283. check(instr_is_branch_to_addr(q, addr));
  284. /* Free the buffer we were using */
  285. vfree(buf);
  286. }
  287. static void __init test_prefixed_patching(void)
  288. {
  289. u32 *iptr = (u32 *)ppc_function_entry(test_trampoline);
  290. u32 expected[2] = {OP_PREFIX << 26, 0};
  291. ppc_inst_t inst = ppc_inst_prefix(OP_PREFIX << 26, 0);
  292. if (!IS_ENABLED(CONFIG_PPC64))
  293. return;
  294. patch_instruction(iptr, inst);
  295. check(!memcmp(iptr, expected, sizeof(expected)));
  296. }
  297. static int __init test_code_patching(void)
  298. {
  299. pr_info("Running code patching self-tests ...\n");
  300. test_branch_iform();
  301. test_branch_bform();
  302. test_create_function_call();
  303. test_translate_branch();
  304. test_prefixed_patching();
  305. return 0;
  306. }
  307. late_initcall(test_code_patching);