feature-fixups.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
  3. *
  4. * Modifications for ppc64:
  5. * Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
  6. *
  7. * Copyright 2008 Michael Ellerman, IBM Corporation.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/jump_label.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <asm/cputable.h>
  20. #include <asm/code-patching.h>
  21. #include <asm/page.h>
  22. #include <asm/sections.h>
  23. #include <asm/setup.h>
  24. #include <asm/firmware.h>
  25. struct fixup_entry {
  26. unsigned long mask;
  27. unsigned long value;
  28. long start_off;
  29. long end_off;
  30. long alt_start_off;
  31. long alt_end_off;
  32. };
  33. static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
  34. {
  35. /*
  36. * We store the offset to the code as a negative offset from
  37. * the start of the alt_entry, to support the VDSO. This
  38. * routine converts that back into an actual address.
  39. */
  40. return (unsigned int *)((unsigned long)fcur + offset);
  41. }
  42. static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
  43. unsigned int *alt_start, unsigned int *alt_end)
  44. {
  45. unsigned int instr;
  46. instr = *src;
  47. if (instr_is_relative_branch(*src)) {
  48. unsigned int *target = (unsigned int *)branch_target(src);
  49. /* Branch within the section doesn't need translating */
  50. if (target < alt_start || target >= alt_end) {
  51. instr = translate_branch(dest, src);
  52. if (!instr)
  53. return 1;
  54. }
  55. }
  56. patch_instruction(dest, instr);
  57. return 0;
  58. }
  59. static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
  60. {
  61. unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
  62. start = calc_addr(fcur, fcur->start_off);
  63. end = calc_addr(fcur, fcur->end_off);
  64. alt_start = calc_addr(fcur, fcur->alt_start_off);
  65. alt_end = calc_addr(fcur, fcur->alt_end_off);
  66. if ((alt_end - alt_start) > (end - start))
  67. return 1;
  68. if ((value & fcur->mask) == fcur->value)
  69. return 0;
  70. src = alt_start;
  71. dest = start;
  72. for (; src < alt_end; src++, dest++) {
  73. if (patch_alt_instruction(src, dest, alt_start, alt_end))
  74. return 1;
  75. }
  76. for (; dest < end; dest++)
  77. patch_instruction(dest, PPC_INST_NOP);
  78. return 0;
  79. }
  80. void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
  81. {
  82. struct fixup_entry *fcur, *fend;
  83. fcur = fixup_start;
  84. fend = fixup_end;
  85. for (; fcur < fend; fcur++) {
  86. if (patch_feature_section(value, fcur)) {
  87. WARN_ON(1);
  88. printk("Unable to patch feature section at %p - %p" \
  89. " with %p - %p\n",
  90. calc_addr(fcur, fcur->start_off),
  91. calc_addr(fcur, fcur->end_off),
  92. calc_addr(fcur, fcur->alt_start_off),
  93. calc_addr(fcur, fcur->alt_end_off));
  94. }
  95. }
  96. }
  97. void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
  98. {
  99. long *start, *end;
  100. unsigned int *dest;
  101. if (!(value & CPU_FTR_LWSYNC))
  102. return ;
  103. start = fixup_start;
  104. end = fixup_end;
  105. for (; start < end; start++) {
  106. dest = (void *)start + *start;
  107. patch_instruction(dest, PPC_INST_LWSYNC);
  108. }
  109. }
  110. static void do_final_fixups(void)
  111. {
  112. #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
  113. int *src, *dest;
  114. unsigned long length;
  115. if (PHYSICAL_START == 0)
  116. return;
  117. src = (int *)(KERNELBASE + PHYSICAL_START);
  118. dest = (int *)KERNELBASE;
  119. length = (__end_interrupts - _stext) / sizeof(int);
  120. while (length--) {
  121. patch_instruction(dest, *src);
  122. src++;
  123. dest++;
  124. }
  125. #endif
  126. }
  127. static unsigned long __initdata saved_cpu_features;
  128. static unsigned int __initdata saved_mmu_features;
  129. #ifdef CONFIG_PPC64
  130. static unsigned long __initdata saved_firmware_features;
  131. #endif
  132. void __init apply_feature_fixups(void)
  133. {
  134. struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
  135. *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
  136. *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
  137. /*
  138. * Apply the CPU-specific and firmware specific fixups to kernel text
  139. * (nop out sections not relevant to this CPU or this firmware).
  140. */
  141. do_feature_fixups(spec->cpu_features,
  142. PTRRELOC(&__start___ftr_fixup),
  143. PTRRELOC(&__stop___ftr_fixup));
  144. do_feature_fixups(spec->mmu_features,
  145. PTRRELOC(&__start___mmu_ftr_fixup),
  146. PTRRELOC(&__stop___mmu_ftr_fixup));
  147. do_lwsync_fixups(spec->cpu_features,
  148. PTRRELOC(&__start___lwsync_fixup),
  149. PTRRELOC(&__stop___lwsync_fixup));
  150. #ifdef CONFIG_PPC64
  151. saved_firmware_features = powerpc_firmware_features;
  152. do_feature_fixups(powerpc_firmware_features,
  153. &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
  154. #endif
  155. do_final_fixups();
  156. }
  157. void __init setup_feature_keys(void)
  158. {
  159. /*
  160. * Initialise jump label. This causes all the cpu/mmu_has_feature()
  161. * checks to take on their correct polarity based on the current set of
  162. * CPU/MMU features.
  163. */
  164. jump_label_init();
  165. cpu_feature_keys_init();
  166. mmu_feature_keys_init();
  167. }
  168. static int __init check_features(void)
  169. {
  170. WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
  171. "CPU features changed after feature patching!\n");
  172. WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
  173. "MMU features changed after feature patching!\n");
  174. #ifdef CONFIG_PPC64
  175. WARN(saved_firmware_features != powerpc_firmware_features,
  176. "Firmware features changed after feature patching!\n");
  177. #endif
  178. return 0;
  179. }
  180. late_initcall(check_features);
  181. #ifdef CONFIG_FTR_FIXUP_SELFTEST
  182. #define check(x) \
  183. if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
  184. /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
  185. static struct fixup_entry fixup;
  186. static long calc_offset(struct fixup_entry *entry, unsigned int *p)
  187. {
  188. return (unsigned long)p - (unsigned long)entry;
  189. }
  190. static void test_basic_patching(void)
  191. {
  192. extern unsigned int ftr_fixup_test1;
  193. extern unsigned int end_ftr_fixup_test1;
  194. extern unsigned int ftr_fixup_test1_orig;
  195. extern unsigned int ftr_fixup_test1_expected;
  196. int size = &end_ftr_fixup_test1 - &ftr_fixup_test1;
  197. fixup.value = fixup.mask = 8;
  198. fixup.start_off = calc_offset(&fixup, &ftr_fixup_test1 + 1);
  199. fixup.end_off = calc_offset(&fixup, &ftr_fixup_test1 + 2);
  200. fixup.alt_start_off = fixup.alt_end_off = 0;
  201. /* Sanity check */
  202. check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
  203. /* Check we don't patch if the value matches */
  204. patch_feature_section(8, &fixup);
  205. check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
  206. /* Check we do patch if the value doesn't match */
  207. patch_feature_section(0, &fixup);
  208. check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
  209. /* Check we do patch if the mask doesn't match */
  210. memcpy(&ftr_fixup_test1, &ftr_fixup_test1_orig, size);
  211. check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_orig, size) == 0);
  212. patch_feature_section(~8, &fixup);
  213. check(memcmp(&ftr_fixup_test1, &ftr_fixup_test1_expected, size) == 0);
  214. }
  215. static void test_alternative_patching(void)
  216. {
  217. extern unsigned int ftr_fixup_test2;
  218. extern unsigned int end_ftr_fixup_test2;
  219. extern unsigned int ftr_fixup_test2_orig;
  220. extern unsigned int ftr_fixup_test2_alt;
  221. extern unsigned int ftr_fixup_test2_expected;
  222. int size = &end_ftr_fixup_test2 - &ftr_fixup_test2;
  223. fixup.value = fixup.mask = 0xF;
  224. fixup.start_off = calc_offset(&fixup, &ftr_fixup_test2 + 1);
  225. fixup.end_off = calc_offset(&fixup, &ftr_fixup_test2 + 2);
  226. fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test2_alt);
  227. fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test2_alt + 1);
  228. /* Sanity check */
  229. check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
  230. /* Check we don't patch if the value matches */
  231. patch_feature_section(0xF, &fixup);
  232. check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
  233. /* Check we do patch if the value doesn't match */
  234. patch_feature_section(0, &fixup);
  235. check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
  236. /* Check we do patch if the mask doesn't match */
  237. memcpy(&ftr_fixup_test2, &ftr_fixup_test2_orig, size);
  238. check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_orig, size) == 0);
  239. patch_feature_section(~0xF, &fixup);
  240. check(memcmp(&ftr_fixup_test2, &ftr_fixup_test2_expected, size) == 0);
  241. }
  242. static void test_alternative_case_too_big(void)
  243. {
  244. extern unsigned int ftr_fixup_test3;
  245. extern unsigned int end_ftr_fixup_test3;
  246. extern unsigned int ftr_fixup_test3_orig;
  247. extern unsigned int ftr_fixup_test3_alt;
  248. int size = &end_ftr_fixup_test3 - &ftr_fixup_test3;
  249. fixup.value = fixup.mask = 0xC;
  250. fixup.start_off = calc_offset(&fixup, &ftr_fixup_test3 + 1);
  251. fixup.end_off = calc_offset(&fixup, &ftr_fixup_test3 + 2);
  252. fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test3_alt);
  253. fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test3_alt + 2);
  254. /* Sanity check */
  255. check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
  256. /* Expect nothing to be patched, and the error returned to us */
  257. check(patch_feature_section(0xF, &fixup) == 1);
  258. check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
  259. check(patch_feature_section(0, &fixup) == 1);
  260. check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
  261. check(patch_feature_section(~0xF, &fixup) == 1);
  262. check(memcmp(&ftr_fixup_test3, &ftr_fixup_test3_orig, size) == 0);
  263. }
  264. static void test_alternative_case_too_small(void)
  265. {
  266. extern unsigned int ftr_fixup_test4;
  267. extern unsigned int end_ftr_fixup_test4;
  268. extern unsigned int ftr_fixup_test4_orig;
  269. extern unsigned int ftr_fixup_test4_alt;
  270. extern unsigned int ftr_fixup_test4_expected;
  271. int size = &end_ftr_fixup_test4 - &ftr_fixup_test4;
  272. unsigned long flag;
  273. /* Check a high-bit flag */
  274. flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
  275. fixup.value = fixup.mask = flag;
  276. fixup.start_off = calc_offset(&fixup, &ftr_fixup_test4 + 1);
  277. fixup.end_off = calc_offset(&fixup, &ftr_fixup_test4 + 5);
  278. fixup.alt_start_off = calc_offset(&fixup, &ftr_fixup_test4_alt);
  279. fixup.alt_end_off = calc_offset(&fixup, &ftr_fixup_test4_alt + 2);
  280. /* Sanity check */
  281. check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
  282. /* Check we don't patch if the value matches */
  283. patch_feature_section(flag, &fixup);
  284. check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
  285. /* Check we do patch if the value doesn't match */
  286. patch_feature_section(0, &fixup);
  287. check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
  288. /* Check we do patch if the mask doesn't match */
  289. memcpy(&ftr_fixup_test4, &ftr_fixup_test4_orig, size);
  290. check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_orig, size) == 0);
  291. patch_feature_section(~flag, &fixup);
  292. check(memcmp(&ftr_fixup_test4, &ftr_fixup_test4_expected, size) == 0);
  293. }
  294. static void test_alternative_case_with_branch(void)
  295. {
  296. extern unsigned int ftr_fixup_test5;
  297. extern unsigned int end_ftr_fixup_test5;
  298. extern unsigned int ftr_fixup_test5_expected;
  299. int size = &end_ftr_fixup_test5 - &ftr_fixup_test5;
  300. check(memcmp(&ftr_fixup_test5, &ftr_fixup_test5_expected, size) == 0);
  301. }
  302. static void test_alternative_case_with_external_branch(void)
  303. {
  304. extern unsigned int ftr_fixup_test6;
  305. extern unsigned int end_ftr_fixup_test6;
  306. extern unsigned int ftr_fixup_test6_expected;
  307. int size = &end_ftr_fixup_test6 - &ftr_fixup_test6;
  308. check(memcmp(&ftr_fixup_test6, &ftr_fixup_test6_expected, size) == 0);
  309. }
  310. static void test_cpu_macros(void)
  311. {
  312. extern u8 ftr_fixup_test_FTR_macros;
  313. extern u8 ftr_fixup_test_FTR_macros_expected;
  314. unsigned long size = &ftr_fixup_test_FTR_macros_expected -
  315. &ftr_fixup_test_FTR_macros;
  316. /* The fixups have already been done for us during boot */
  317. check(memcmp(&ftr_fixup_test_FTR_macros,
  318. &ftr_fixup_test_FTR_macros_expected, size) == 0);
  319. }
  320. static void test_fw_macros(void)
  321. {
  322. #ifdef CONFIG_PPC64
  323. extern u8 ftr_fixup_test_FW_FTR_macros;
  324. extern u8 ftr_fixup_test_FW_FTR_macros_expected;
  325. unsigned long size = &ftr_fixup_test_FW_FTR_macros_expected -
  326. &ftr_fixup_test_FW_FTR_macros;
  327. /* The fixups have already been done for us during boot */
  328. check(memcmp(&ftr_fixup_test_FW_FTR_macros,
  329. &ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
  330. #endif
  331. }
  332. static void test_lwsync_macros(void)
  333. {
  334. extern u8 lwsync_fixup_test;
  335. extern u8 end_lwsync_fixup_test;
  336. extern u8 lwsync_fixup_test_expected_LWSYNC;
  337. extern u8 lwsync_fixup_test_expected_SYNC;
  338. unsigned long size = &end_lwsync_fixup_test -
  339. &lwsync_fixup_test;
  340. /* The fixups have already been done for us during boot */
  341. if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
  342. check(memcmp(&lwsync_fixup_test,
  343. &lwsync_fixup_test_expected_LWSYNC, size) == 0);
  344. } else {
  345. check(memcmp(&lwsync_fixup_test,
  346. &lwsync_fixup_test_expected_SYNC, size) == 0);
  347. }
  348. }
  349. static int __init test_feature_fixups(void)
  350. {
  351. printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
  352. test_basic_patching();
  353. test_alternative_patching();
  354. test_alternative_case_too_big();
  355. test_alternative_case_too_small();
  356. test_alternative_case_with_branch();
  357. test_alternative_case_with_external_branch();
  358. test_cpu_macros();
  359. test_fw_macros();
  360. test_lwsync_macros();
  361. return 0;
  362. }
  363. late_initcall(test_feature_fixups);
  364. #endif /* CONFIG_FTR_FIXUP_SELFTEST */