feature-fixups.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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 <linux/sched/mm.h>
  20. #include <asm/cputable.h>
  21. #include <asm/code-patching.h>
  22. #include <asm/page.h>
  23. #include <asm/sections.h>
  24. #include <asm/setup.h>
  25. #include <asm/security_features.h>
  26. #include <asm/firmware.h>
  27. struct fixup_entry {
  28. unsigned long mask;
  29. unsigned long value;
  30. long start_off;
  31. long end_off;
  32. long alt_start_off;
  33. long alt_end_off;
  34. };
  35. static unsigned int *calc_addr(struct fixup_entry *fcur, long offset)
  36. {
  37. /*
  38. * We store the offset to the code as a negative offset from
  39. * the start of the alt_entry, to support the VDSO. This
  40. * routine converts that back into an actual address.
  41. */
  42. return (unsigned int *)((unsigned long)fcur + offset);
  43. }
  44. static int patch_alt_instruction(unsigned int *src, unsigned int *dest,
  45. unsigned int *alt_start, unsigned int *alt_end)
  46. {
  47. unsigned int instr;
  48. instr = *src;
  49. if (instr_is_relative_branch(*src)) {
  50. unsigned int *target = (unsigned int *)branch_target(src);
  51. /* Branch within the section doesn't need translating */
  52. if (target < alt_start || target > alt_end) {
  53. instr = translate_branch(dest, src);
  54. if (!instr)
  55. return 1;
  56. }
  57. }
  58. raw_patch_instruction(dest, instr);
  59. return 0;
  60. }
  61. static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
  62. {
  63. unsigned int *start, *end, *alt_start, *alt_end, *src, *dest;
  64. start = calc_addr(fcur, fcur->start_off);
  65. end = calc_addr(fcur, fcur->end_off);
  66. alt_start = calc_addr(fcur, fcur->alt_start_off);
  67. alt_end = calc_addr(fcur, fcur->alt_end_off);
  68. if ((alt_end - alt_start) > (end - start))
  69. return 1;
  70. if ((value & fcur->mask) == fcur->value)
  71. return 0;
  72. src = alt_start;
  73. dest = start;
  74. for (; src < alt_end; src++, dest++) {
  75. if (patch_alt_instruction(src, dest, alt_start, alt_end))
  76. return 1;
  77. }
  78. for (; dest < end; dest++)
  79. raw_patch_instruction(dest, PPC_INST_NOP);
  80. return 0;
  81. }
  82. void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
  83. {
  84. struct fixup_entry *fcur, *fend;
  85. fcur = fixup_start;
  86. fend = fixup_end;
  87. for (; fcur < fend; fcur++) {
  88. if (patch_feature_section(value, fcur)) {
  89. WARN_ON(1);
  90. printk("Unable to patch feature section at %p - %p" \
  91. " with %p - %p\n",
  92. calc_addr(fcur, fcur->start_off),
  93. calc_addr(fcur, fcur->end_off),
  94. calc_addr(fcur, fcur->alt_start_off),
  95. calc_addr(fcur, fcur->alt_end_off));
  96. }
  97. }
  98. }
  99. #ifdef CONFIG_PPC_BOOK3S_64
  100. void do_stf_entry_barrier_fixups(enum stf_barrier_type types)
  101. {
  102. unsigned int instrs[3], *dest;
  103. long *start, *end;
  104. int i;
  105. start = PTRRELOC(&__start___stf_entry_barrier_fixup),
  106. end = PTRRELOC(&__stop___stf_entry_barrier_fixup);
  107. instrs[0] = 0x60000000; /* nop */
  108. instrs[1] = 0x60000000; /* nop */
  109. instrs[2] = 0x60000000; /* nop */
  110. i = 0;
  111. if (types & STF_BARRIER_FALLBACK) {
  112. instrs[i++] = 0x7d4802a6; /* mflr r10 */
  113. instrs[i++] = 0x60000000; /* branch patched below */
  114. instrs[i++] = 0x7d4803a6; /* mtlr r10 */
  115. } else if (types & STF_BARRIER_EIEIO) {
  116. instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
  117. } else if (types & STF_BARRIER_SYNC_ORI) {
  118. instrs[i++] = 0x7c0004ac; /* hwsync */
  119. instrs[i++] = 0xe94d0000; /* ld r10,0(r13) */
  120. instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
  121. }
  122. for (i = 0; start < end; start++, i++) {
  123. dest = (void *)start + *start;
  124. pr_devel("patching dest %lx\n", (unsigned long)dest);
  125. patch_instruction(dest, instrs[0]);
  126. if (types & STF_BARRIER_FALLBACK)
  127. patch_branch(dest + 1, (unsigned long)&stf_barrier_fallback,
  128. BRANCH_SET_LINK);
  129. else
  130. patch_instruction(dest + 1, instrs[1]);
  131. patch_instruction(dest + 2, instrs[2]);
  132. }
  133. printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
  134. (types == STF_BARRIER_NONE) ? "no" :
  135. (types == STF_BARRIER_FALLBACK) ? "fallback" :
  136. (types == STF_BARRIER_EIEIO) ? "eieio" :
  137. (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
  138. : "unknown");
  139. }
  140. void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
  141. {
  142. unsigned int instrs[6], *dest;
  143. long *start, *end;
  144. int i;
  145. start = PTRRELOC(&__start___stf_exit_barrier_fixup),
  146. end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
  147. instrs[0] = 0x60000000; /* nop */
  148. instrs[1] = 0x60000000; /* nop */
  149. instrs[2] = 0x60000000; /* nop */
  150. instrs[3] = 0x60000000; /* nop */
  151. instrs[4] = 0x60000000; /* nop */
  152. instrs[5] = 0x60000000; /* nop */
  153. i = 0;
  154. if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
  155. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  156. instrs[i++] = 0x7db14ba6; /* mtspr 0x131, r13 (HSPRG1) */
  157. instrs[i++] = 0x7db04aa6; /* mfspr r13, 0x130 (HSPRG0) */
  158. } else {
  159. instrs[i++] = 0x7db243a6; /* mtsprg 2,r13 */
  160. instrs[i++] = 0x7db142a6; /* mfsprg r13,1 */
  161. }
  162. instrs[i++] = 0x7c0004ac; /* hwsync */
  163. instrs[i++] = 0xe9ad0000; /* ld r13,0(r13) */
  164. instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
  165. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  166. instrs[i++] = 0x7db14aa6; /* mfspr r13, 0x131 (HSPRG1) */
  167. } else {
  168. instrs[i++] = 0x7db242a6; /* mfsprg r13,2 */
  169. }
  170. } else if (types & STF_BARRIER_EIEIO) {
  171. instrs[i++] = 0x7e0006ac; /* eieio + bit 6 hint */
  172. }
  173. for (i = 0; start < end; start++, i++) {
  174. dest = (void *)start + *start;
  175. pr_devel("patching dest %lx\n", (unsigned long)dest);
  176. patch_instruction(dest, instrs[0]);
  177. patch_instruction(dest + 1, instrs[1]);
  178. patch_instruction(dest + 2, instrs[2]);
  179. patch_instruction(dest + 3, instrs[3]);
  180. patch_instruction(dest + 4, instrs[4]);
  181. patch_instruction(dest + 5, instrs[5]);
  182. }
  183. printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
  184. (types == STF_BARRIER_NONE) ? "no" :
  185. (types == STF_BARRIER_FALLBACK) ? "fallback" :
  186. (types == STF_BARRIER_EIEIO) ? "eieio" :
  187. (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
  188. : "unknown");
  189. }
  190. void do_stf_barrier_fixups(enum stf_barrier_type types)
  191. {
  192. do_stf_entry_barrier_fixups(types);
  193. do_stf_exit_barrier_fixups(types);
  194. }
  195. void do_rfi_flush_fixups(enum l1d_flush_type types)
  196. {
  197. unsigned int instrs[3], *dest;
  198. long *start, *end;
  199. int i;
  200. start = PTRRELOC(&__start___rfi_flush_fixup),
  201. end = PTRRELOC(&__stop___rfi_flush_fixup);
  202. instrs[0] = 0x60000000; /* nop */
  203. instrs[1] = 0x60000000; /* nop */
  204. instrs[2] = 0x60000000; /* nop */
  205. if (types & L1D_FLUSH_FALLBACK)
  206. /* b .+16 to fallback flush */
  207. instrs[0] = 0x48000010;
  208. i = 0;
  209. if (types & L1D_FLUSH_ORI) {
  210. instrs[i++] = 0x63ff0000; /* ori 31,31,0 speculation barrier */
  211. instrs[i++] = 0x63de0000; /* ori 30,30,0 L1d flush*/
  212. }
  213. if (types & L1D_FLUSH_MTTRIG)
  214. instrs[i++] = 0x7c12dba6; /* mtspr TRIG2,r0 (SPR #882) */
  215. for (i = 0; start < end; start++, i++) {
  216. dest = (void *)start + *start;
  217. pr_devel("patching dest %lx\n", (unsigned long)dest);
  218. patch_instruction(dest, instrs[0]);
  219. patch_instruction(dest + 1, instrs[1]);
  220. patch_instruction(dest + 2, instrs[2]);
  221. }
  222. printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
  223. (types == L1D_FLUSH_NONE) ? "no" :
  224. (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
  225. (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
  226. ? "ori+mttrig type"
  227. : "ori type" :
  228. (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
  229. : "unknown");
  230. }
  231. void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
  232. {
  233. unsigned int instr, *dest;
  234. long *start, *end;
  235. int i;
  236. start = fixup_start;
  237. end = fixup_end;
  238. instr = 0x60000000; /* nop */
  239. if (enable) {
  240. pr_info("barrier-nospec: using ORI speculation barrier\n");
  241. instr = 0x63ff0000; /* ori 31,31,0 speculation barrier */
  242. }
  243. for (i = 0; start < end; start++, i++) {
  244. dest = (void *)start + *start;
  245. pr_devel("patching dest %lx\n", (unsigned long)dest);
  246. patch_instruction(dest, instr);
  247. }
  248. printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
  249. }
  250. #endif /* CONFIG_PPC_BOOK3S_64 */
  251. #ifdef CONFIG_PPC_BARRIER_NOSPEC
  252. void do_barrier_nospec_fixups(bool enable)
  253. {
  254. void *start, *end;
  255. start = PTRRELOC(&__start___barrier_nospec_fixup),
  256. end = PTRRELOC(&__stop___barrier_nospec_fixup);
  257. do_barrier_nospec_fixups_range(enable, start, end);
  258. }
  259. #endif /* CONFIG_PPC_BARRIER_NOSPEC */
  260. #ifdef CONFIG_PPC_FSL_BOOK3E
  261. void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
  262. {
  263. unsigned int instr[2], *dest;
  264. long *start, *end;
  265. int i;
  266. start = fixup_start;
  267. end = fixup_end;
  268. instr[0] = PPC_INST_NOP;
  269. instr[1] = PPC_INST_NOP;
  270. if (enable) {
  271. pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
  272. instr[0] = PPC_INST_ISYNC;
  273. instr[1] = PPC_INST_SYNC;
  274. }
  275. for (i = 0; start < end; start++, i++) {
  276. dest = (void *)start + *start;
  277. pr_devel("patching dest %lx\n", (unsigned long)dest);
  278. patch_instruction(dest, instr[0]);
  279. patch_instruction(dest + 1, instr[1]);
  280. }
  281. printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
  282. }
  283. #endif /* CONFIG_PPC_FSL_BOOK3E */
  284. void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
  285. {
  286. long *start, *end;
  287. unsigned int *dest;
  288. if (!(value & CPU_FTR_LWSYNC))
  289. return ;
  290. start = fixup_start;
  291. end = fixup_end;
  292. for (; start < end; start++) {
  293. dest = (void *)start + *start;
  294. raw_patch_instruction(dest, PPC_INST_LWSYNC);
  295. }
  296. }
  297. static void do_final_fixups(void)
  298. {
  299. #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
  300. int *src, *dest;
  301. unsigned long length;
  302. if (PHYSICAL_START == 0)
  303. return;
  304. src = (int *)(KERNELBASE + PHYSICAL_START);
  305. dest = (int *)KERNELBASE;
  306. length = (__end_interrupts - _stext) / sizeof(int);
  307. while (length--) {
  308. raw_patch_instruction(dest, *src);
  309. src++;
  310. dest++;
  311. }
  312. #endif
  313. }
  314. static unsigned long __initdata saved_cpu_features;
  315. static unsigned int __initdata saved_mmu_features;
  316. #ifdef CONFIG_PPC64
  317. static unsigned long __initdata saved_firmware_features;
  318. #endif
  319. void __init apply_feature_fixups(void)
  320. {
  321. struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
  322. *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
  323. *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
  324. /*
  325. * Apply the CPU-specific and firmware specific fixups to kernel text
  326. * (nop out sections not relevant to this CPU or this firmware).
  327. */
  328. do_feature_fixups(spec->cpu_features,
  329. PTRRELOC(&__start___ftr_fixup),
  330. PTRRELOC(&__stop___ftr_fixup));
  331. do_feature_fixups(spec->mmu_features,
  332. PTRRELOC(&__start___mmu_ftr_fixup),
  333. PTRRELOC(&__stop___mmu_ftr_fixup));
  334. do_lwsync_fixups(spec->cpu_features,
  335. PTRRELOC(&__start___lwsync_fixup),
  336. PTRRELOC(&__stop___lwsync_fixup));
  337. #ifdef CONFIG_PPC64
  338. saved_firmware_features = powerpc_firmware_features;
  339. do_feature_fixups(powerpc_firmware_features,
  340. &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
  341. #endif
  342. do_final_fixups();
  343. }
  344. void __init setup_feature_keys(void)
  345. {
  346. /*
  347. * Initialise jump label. This causes all the cpu/mmu_has_feature()
  348. * checks to take on their correct polarity based on the current set of
  349. * CPU/MMU features.
  350. */
  351. jump_label_init();
  352. cpu_feature_keys_init();
  353. mmu_feature_keys_init();
  354. }
  355. static int __init check_features(void)
  356. {
  357. WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
  358. "CPU features changed after feature patching!\n");
  359. WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
  360. "MMU features changed after feature patching!\n");
  361. #ifdef CONFIG_PPC64
  362. WARN(saved_firmware_features != powerpc_firmware_features,
  363. "Firmware features changed after feature patching!\n");
  364. #endif
  365. return 0;
  366. }
  367. late_initcall(check_features);
  368. #ifdef CONFIG_FTR_FIXUP_SELFTEST
  369. #define check(x) \
  370. if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
  371. /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
  372. static struct fixup_entry fixup;
  373. static long calc_offset(struct fixup_entry *entry, unsigned int *p)
  374. {
  375. return (unsigned long)p - (unsigned long)entry;
  376. }
  377. static void test_basic_patching(void)
  378. {
  379. extern unsigned int ftr_fixup_test1[];
  380. extern unsigned int end_ftr_fixup_test1[];
  381. extern unsigned int ftr_fixup_test1_orig[];
  382. extern unsigned int ftr_fixup_test1_expected[];
  383. int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
  384. fixup.value = fixup.mask = 8;
  385. fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
  386. fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
  387. fixup.alt_start_off = fixup.alt_end_off = 0;
  388. /* Sanity check */
  389. check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
  390. /* Check we don't patch if the value matches */
  391. patch_feature_section(8, &fixup);
  392. check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
  393. /* Check we do patch if the value doesn't match */
  394. patch_feature_section(0, &fixup);
  395. check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
  396. /* Check we do patch if the mask doesn't match */
  397. memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
  398. check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
  399. patch_feature_section(~8, &fixup);
  400. check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
  401. }
  402. static void test_alternative_patching(void)
  403. {
  404. extern unsigned int ftr_fixup_test2[];
  405. extern unsigned int end_ftr_fixup_test2[];
  406. extern unsigned int ftr_fixup_test2_orig[];
  407. extern unsigned int ftr_fixup_test2_alt[];
  408. extern unsigned int ftr_fixup_test2_expected[];
  409. int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
  410. fixup.value = fixup.mask = 0xF;
  411. fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
  412. fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
  413. fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
  414. fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
  415. /* Sanity check */
  416. check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
  417. /* Check we don't patch if the value matches */
  418. patch_feature_section(0xF, &fixup);
  419. check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
  420. /* Check we do patch if the value doesn't match */
  421. patch_feature_section(0, &fixup);
  422. check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
  423. /* Check we do patch if the mask doesn't match */
  424. memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
  425. check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
  426. patch_feature_section(~0xF, &fixup);
  427. check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
  428. }
  429. static void test_alternative_case_too_big(void)
  430. {
  431. extern unsigned int ftr_fixup_test3[];
  432. extern unsigned int end_ftr_fixup_test3[];
  433. extern unsigned int ftr_fixup_test3_orig[];
  434. extern unsigned int ftr_fixup_test3_alt[];
  435. int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
  436. fixup.value = fixup.mask = 0xC;
  437. fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
  438. fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
  439. fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
  440. fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
  441. /* Sanity check */
  442. check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
  443. /* Expect nothing to be patched, and the error returned to us */
  444. check(patch_feature_section(0xF, &fixup) == 1);
  445. check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
  446. check(patch_feature_section(0, &fixup) == 1);
  447. check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
  448. check(patch_feature_section(~0xF, &fixup) == 1);
  449. check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
  450. }
  451. static void test_alternative_case_too_small(void)
  452. {
  453. extern unsigned int ftr_fixup_test4[];
  454. extern unsigned int end_ftr_fixup_test4[];
  455. extern unsigned int ftr_fixup_test4_orig[];
  456. extern unsigned int ftr_fixup_test4_alt[];
  457. extern unsigned int ftr_fixup_test4_expected[];
  458. int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
  459. unsigned long flag;
  460. /* Check a high-bit flag */
  461. flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
  462. fixup.value = fixup.mask = flag;
  463. fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
  464. fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
  465. fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
  466. fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
  467. /* Sanity check */
  468. check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
  469. /* Check we don't patch if the value matches */
  470. patch_feature_section(flag, &fixup);
  471. check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
  472. /* Check we do patch if the value doesn't match */
  473. patch_feature_section(0, &fixup);
  474. check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
  475. /* Check we do patch if the mask doesn't match */
  476. memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
  477. check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
  478. patch_feature_section(~flag, &fixup);
  479. check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
  480. }
  481. static void test_alternative_case_with_branch(void)
  482. {
  483. extern unsigned int ftr_fixup_test5[];
  484. extern unsigned int end_ftr_fixup_test5[];
  485. extern unsigned int ftr_fixup_test5_expected[];
  486. int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
  487. check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
  488. }
  489. static void test_alternative_case_with_external_branch(void)
  490. {
  491. extern unsigned int ftr_fixup_test6[];
  492. extern unsigned int end_ftr_fixup_test6[];
  493. extern unsigned int ftr_fixup_test6_expected[];
  494. int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
  495. check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
  496. }
  497. static void test_alternative_case_with_branch_to_end(void)
  498. {
  499. extern unsigned int ftr_fixup_test7[];
  500. extern unsigned int end_ftr_fixup_test7[];
  501. extern unsigned int ftr_fixup_test7_expected[];
  502. int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
  503. check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
  504. }
  505. static void test_cpu_macros(void)
  506. {
  507. extern u8 ftr_fixup_test_FTR_macros[];
  508. extern u8 ftr_fixup_test_FTR_macros_expected[];
  509. unsigned long size = ftr_fixup_test_FTR_macros_expected -
  510. ftr_fixup_test_FTR_macros;
  511. /* The fixups have already been done for us during boot */
  512. check(memcmp(ftr_fixup_test_FTR_macros,
  513. ftr_fixup_test_FTR_macros_expected, size) == 0);
  514. }
  515. static void test_fw_macros(void)
  516. {
  517. #ifdef CONFIG_PPC64
  518. extern u8 ftr_fixup_test_FW_FTR_macros[];
  519. extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
  520. unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
  521. ftr_fixup_test_FW_FTR_macros;
  522. /* The fixups have already been done for us during boot */
  523. check(memcmp(ftr_fixup_test_FW_FTR_macros,
  524. ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
  525. #endif
  526. }
  527. static void test_lwsync_macros(void)
  528. {
  529. extern u8 lwsync_fixup_test[];
  530. extern u8 end_lwsync_fixup_test[];
  531. extern u8 lwsync_fixup_test_expected_LWSYNC[];
  532. extern u8 lwsync_fixup_test_expected_SYNC[];
  533. unsigned long size = end_lwsync_fixup_test -
  534. lwsync_fixup_test;
  535. /* The fixups have already been done for us during boot */
  536. if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
  537. check(memcmp(lwsync_fixup_test,
  538. lwsync_fixup_test_expected_LWSYNC, size) == 0);
  539. } else {
  540. check(memcmp(lwsync_fixup_test,
  541. lwsync_fixup_test_expected_SYNC, size) == 0);
  542. }
  543. }
  544. static int __init test_feature_fixups(void)
  545. {
  546. printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
  547. test_basic_patching();
  548. test_alternative_patching();
  549. test_alternative_case_too_big();
  550. test_alternative_case_too_small();
  551. test_alternative_case_with_branch();
  552. test_alternative_case_with_external_branch();
  553. test_alternative_case_with_branch_to_end();
  554. test_cpu_macros();
  555. test_fw_macros();
  556. test_lwsync_macros();
  557. return 0;
  558. }
  559. late_initcall(test_feature_fixups);
  560. #endif /* CONFIG_FTR_FIXUP_SELFTEST */