feature-fixups.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2001 Ben. Herrenschmidt (benh@kernel.crashing.org)
  4. *
  5. * Modifications for ppc64:
  6. * Copyright (C) 2003 Dave Engebretsen <engebret@us.ibm.com>
  7. *
  8. * Copyright 2008 Michael Ellerman, IBM Corporation.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/jump_label.h>
  12. #include <linux/kernel.h>
  13. #include <linux/string.h>
  14. #include <linux/init.h>
  15. #include <linux/sched/mm.h>
  16. #include <linux/stop_machine.h>
  17. #include <asm/cputable.h>
  18. #include <asm/code-patching.h>
  19. #include <asm/interrupt.h>
  20. #include <asm/page.h>
  21. #include <asm/sections.h>
  22. #include <asm/setup.h>
  23. #include <asm/security_features.h>
  24. #include <asm/firmware.h>
  25. #include <asm/inst.h>
  26. /*
  27. * Used to generate warnings if mmu or cpu feature check functions that
  28. * use static keys before they are initialized.
  29. */
  30. bool static_key_feature_checks_initialized __read_mostly;
  31. EXPORT_SYMBOL_GPL(static_key_feature_checks_initialized);
  32. struct fixup_entry {
  33. unsigned long mask;
  34. unsigned long value;
  35. long start_off;
  36. long end_off;
  37. long alt_start_off;
  38. long alt_end_off;
  39. };
  40. static u32 *calc_addr(struct fixup_entry *fcur, long offset)
  41. {
  42. /*
  43. * We store the offset to the code as a negative offset from
  44. * the start of the alt_entry, to support the VDSO. This
  45. * routine converts that back into an actual address.
  46. */
  47. return (u32 *)((unsigned long)fcur + offset);
  48. }
  49. static int patch_alt_instruction(u32 *src, u32 *dest, u32 *alt_start, u32 *alt_end)
  50. {
  51. int err;
  52. ppc_inst_t instr;
  53. instr = ppc_inst_read(src);
  54. if (instr_is_relative_branch(ppc_inst_read(src))) {
  55. u32 *target = (u32 *)branch_target(src);
  56. /* Branch within the section doesn't need translating */
  57. if (target < alt_start || target > alt_end) {
  58. err = translate_branch(&instr, dest, src);
  59. if (err)
  60. return 1;
  61. }
  62. }
  63. raw_patch_instruction(dest, instr);
  64. return 0;
  65. }
  66. static int patch_feature_section_mask(unsigned long value, unsigned long mask,
  67. struct fixup_entry *fcur)
  68. {
  69. u32 *start, *end, *alt_start, *alt_end, *src, *dest;
  70. start = calc_addr(fcur, fcur->start_off);
  71. end = calc_addr(fcur, fcur->end_off);
  72. alt_start = calc_addr(fcur, fcur->alt_start_off);
  73. alt_end = calc_addr(fcur, fcur->alt_end_off);
  74. if ((alt_end - alt_start) > (end - start))
  75. return 1;
  76. if ((value & fcur->mask & mask) == (fcur->value & mask))
  77. return 0;
  78. src = alt_start;
  79. dest = start;
  80. for (; src < alt_end; src = ppc_inst_next(src, src),
  81. dest = ppc_inst_next(dest, dest)) {
  82. if (patch_alt_instruction(src, dest, alt_start, alt_end))
  83. return 1;
  84. }
  85. for (; dest < end; dest++)
  86. raw_patch_instruction(dest, ppc_inst(PPC_RAW_NOP()));
  87. return 0;
  88. }
  89. static void do_feature_fixups_mask(unsigned long value, unsigned long mask,
  90. void *fixup_start, void *fixup_end)
  91. {
  92. struct fixup_entry *fcur, *fend;
  93. fcur = fixup_start;
  94. fend = fixup_end;
  95. for (; fcur < fend; fcur++) {
  96. if (patch_feature_section_mask(value, mask, fcur)) {
  97. WARN_ON(1);
  98. printk("Unable to patch feature section at %p - %p" \
  99. " with %p - %p\n",
  100. calc_addr(fcur, fcur->start_off),
  101. calc_addr(fcur, fcur->end_off),
  102. calc_addr(fcur, fcur->alt_start_off),
  103. calc_addr(fcur, fcur->alt_end_off));
  104. }
  105. }
  106. }
  107. void do_feature_fixups(unsigned long value, void *fixup_start, void *fixup_end)
  108. {
  109. do_feature_fixups_mask(value, ~0, fixup_start, fixup_end);
  110. }
  111. #ifdef CONFIG_PPC_BARRIER_NOSPEC
  112. static bool is_fixup_addr_valid(void *dest, size_t size)
  113. {
  114. return system_state < SYSTEM_FREEING_INITMEM ||
  115. !init_section_contains(dest, size);
  116. }
  117. static int do_patch_fixups(long *start, long *end, unsigned int *instrs, int num)
  118. {
  119. int i;
  120. for (i = 0; start < end; start++, i++) {
  121. int j;
  122. unsigned int *dest = (void *)start + *start;
  123. if (!is_fixup_addr_valid(dest, sizeof(*instrs) * num))
  124. continue;
  125. pr_devel("patching dest %lx\n", (unsigned long)dest);
  126. for (j = 0; j < num; j++)
  127. patch_instruction(dest + j, ppc_inst(instrs[j]));
  128. }
  129. return i;
  130. }
  131. #endif
  132. #ifdef CONFIG_PPC_BOOK3S_64
  133. static int do_patch_entry_fixups(long *start, long *end, unsigned int *instrs,
  134. bool do_fallback, void *fallback)
  135. {
  136. int i;
  137. for (i = 0; start < end; start++, i++) {
  138. unsigned int *dest = (void *)start + *start;
  139. if (!is_fixup_addr_valid(dest, sizeof(*instrs) * 3))
  140. continue;
  141. pr_devel("patching dest %lx\n", (unsigned long)dest);
  142. // See comment in do_entry_flush_fixups() RE order of patching
  143. if (do_fallback) {
  144. patch_instruction(dest, ppc_inst(instrs[0]));
  145. patch_instruction(dest + 2, ppc_inst(instrs[2]));
  146. patch_branch(dest + 1, (unsigned long)fallback, BRANCH_SET_LINK);
  147. } else {
  148. patch_instruction(dest + 1, ppc_inst(instrs[1]));
  149. patch_instruction(dest + 2, ppc_inst(instrs[2]));
  150. patch_instruction(dest, ppc_inst(instrs[0]));
  151. }
  152. }
  153. return i;
  154. }
  155. static void do_stf_entry_barrier_fixups(enum stf_barrier_type types)
  156. {
  157. unsigned int instrs[3];
  158. long *start, *end;
  159. int i;
  160. start = PTRRELOC(&__start___stf_entry_barrier_fixup);
  161. end = PTRRELOC(&__stop___stf_entry_barrier_fixup);
  162. instrs[0] = PPC_RAW_NOP();
  163. instrs[1] = PPC_RAW_NOP();
  164. instrs[2] = PPC_RAW_NOP();
  165. i = 0;
  166. if (types & STF_BARRIER_FALLBACK) {
  167. instrs[i++] = PPC_RAW_MFLR(_R10);
  168. instrs[i++] = PPC_RAW_NOP(); /* branch patched below */
  169. instrs[i++] = PPC_RAW_MTLR(_R10);
  170. } else if (types & STF_BARRIER_EIEIO) {
  171. instrs[i++] = PPC_RAW_EIEIO() | 0x02000000; /* eieio + bit 6 hint */
  172. } else if (types & STF_BARRIER_SYNC_ORI) {
  173. instrs[i++] = PPC_RAW_SYNC();
  174. instrs[i++] = PPC_RAW_LD(_R10, _R13, 0);
  175. instrs[i++] = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
  176. }
  177. i = do_patch_entry_fixups(start, end, instrs, types & STF_BARRIER_FALLBACK,
  178. &stf_barrier_fallback);
  179. printk(KERN_DEBUG "stf-barrier: patched %d entry locations (%s barrier)\n", i,
  180. (types == STF_BARRIER_NONE) ? "no" :
  181. (types == STF_BARRIER_FALLBACK) ? "fallback" :
  182. (types == STF_BARRIER_EIEIO) ? "eieio" :
  183. (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
  184. : "unknown");
  185. }
  186. static void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
  187. {
  188. unsigned int instrs[6];
  189. long *start, *end;
  190. int i;
  191. start = PTRRELOC(&__start___stf_exit_barrier_fixup);
  192. end = PTRRELOC(&__stop___stf_exit_barrier_fixup);
  193. instrs[0] = PPC_RAW_NOP();
  194. instrs[1] = PPC_RAW_NOP();
  195. instrs[2] = PPC_RAW_NOP();
  196. instrs[3] = PPC_RAW_NOP();
  197. instrs[4] = PPC_RAW_NOP();
  198. instrs[5] = PPC_RAW_NOP();
  199. i = 0;
  200. if (types & STF_BARRIER_FALLBACK || types & STF_BARRIER_SYNC_ORI) {
  201. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  202. instrs[i++] = PPC_RAW_MTSPR(SPRN_HSPRG1, _R13);
  203. instrs[i++] = PPC_RAW_MFSPR(_R13, SPRN_HSPRG0);
  204. } else {
  205. instrs[i++] = PPC_RAW_MTSPR(SPRN_SPRG2, _R13);
  206. instrs[i++] = PPC_RAW_MFSPR(_R13, SPRN_SPRG1);
  207. }
  208. instrs[i++] = PPC_RAW_SYNC();
  209. instrs[i++] = PPC_RAW_LD(_R13, _R13, 0);
  210. instrs[i++] = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
  211. if (cpu_has_feature(CPU_FTR_HVMODE))
  212. instrs[i++] = PPC_RAW_MFSPR(_R13, SPRN_HSPRG1);
  213. else
  214. instrs[i++] = PPC_RAW_MFSPR(_R13, SPRN_SPRG2);
  215. } else if (types & STF_BARRIER_EIEIO) {
  216. instrs[i++] = PPC_RAW_EIEIO() | 0x02000000; /* eieio + bit 6 hint */
  217. }
  218. i = do_patch_fixups(start, end, instrs, ARRAY_SIZE(instrs));
  219. printk(KERN_DEBUG "stf-barrier: patched %d exit locations (%s barrier)\n", i,
  220. (types == STF_BARRIER_NONE) ? "no" :
  221. (types == STF_BARRIER_FALLBACK) ? "fallback" :
  222. (types == STF_BARRIER_EIEIO) ? "eieio" :
  223. (types == (STF_BARRIER_SYNC_ORI)) ? "hwsync"
  224. : "unknown");
  225. }
  226. static bool stf_exit_reentrant = false;
  227. static bool rfi_exit_reentrant = false;
  228. static DEFINE_MUTEX(exit_flush_lock);
  229. static int __do_stf_barrier_fixups(void *data)
  230. {
  231. enum stf_barrier_type *types = data;
  232. do_stf_entry_barrier_fixups(*types);
  233. do_stf_exit_barrier_fixups(*types);
  234. return 0;
  235. }
  236. void do_stf_barrier_fixups(enum stf_barrier_type types)
  237. {
  238. /*
  239. * The call to the fallback entry flush, and the fallback/sync-ori exit
  240. * flush can not be safely patched in/out while other CPUs are
  241. * executing them. So call __do_stf_barrier_fixups() on one CPU while
  242. * all other CPUs spin in the stop machine core with interrupts hard
  243. * disabled.
  244. *
  245. * The branch to mark interrupt exits non-reentrant is enabled first,
  246. * then stop_machine runs which will ensure all CPUs are out of the
  247. * low level interrupt exit code before patching. After the patching,
  248. * if allowed, then flip the branch to allow fast exits.
  249. */
  250. // Prevent static key update races with do_rfi_flush_fixups()
  251. mutex_lock(&exit_flush_lock);
  252. static_branch_enable(&interrupt_exit_not_reentrant);
  253. stop_machine(__do_stf_barrier_fixups, &types, NULL);
  254. if ((types & STF_BARRIER_FALLBACK) || (types & STF_BARRIER_SYNC_ORI))
  255. stf_exit_reentrant = false;
  256. else
  257. stf_exit_reentrant = true;
  258. if (stf_exit_reentrant && rfi_exit_reentrant)
  259. static_branch_disable(&interrupt_exit_not_reentrant);
  260. mutex_unlock(&exit_flush_lock);
  261. }
  262. void do_uaccess_flush_fixups(enum l1d_flush_type types)
  263. {
  264. unsigned int instrs[4];
  265. long *start, *end;
  266. int i;
  267. start = PTRRELOC(&__start___uaccess_flush_fixup);
  268. end = PTRRELOC(&__stop___uaccess_flush_fixup);
  269. instrs[0] = PPC_RAW_NOP();
  270. instrs[1] = PPC_RAW_NOP();
  271. instrs[2] = PPC_RAW_NOP();
  272. instrs[3] = PPC_RAW_BLR();
  273. i = 0;
  274. if (types == L1D_FLUSH_FALLBACK) {
  275. instrs[3] = PPC_RAW_NOP();
  276. /* fallthrough to fallback flush */
  277. }
  278. if (types & L1D_FLUSH_ORI) {
  279. instrs[i++] = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
  280. instrs[i++] = PPC_RAW_ORI(_R30, _R30, 0); /* L1d flush */
  281. }
  282. if (types & L1D_FLUSH_MTTRIG)
  283. instrs[i++] = PPC_RAW_MTSPR(SPRN_TRIG2, _R0);
  284. i = do_patch_fixups(start, end, instrs, ARRAY_SIZE(instrs));
  285. printk(KERN_DEBUG "uaccess-flush: patched %d locations (%s flush)\n", i,
  286. (types == L1D_FLUSH_NONE) ? "no" :
  287. (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
  288. (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
  289. ? "ori+mttrig type"
  290. : "ori type" :
  291. (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
  292. : "unknown");
  293. }
  294. static int __do_entry_flush_fixups(void *data)
  295. {
  296. enum l1d_flush_type types = *(enum l1d_flush_type *)data;
  297. unsigned int instrs[3];
  298. long *start, *end;
  299. int i;
  300. instrs[0] = PPC_RAW_NOP();
  301. instrs[1] = PPC_RAW_NOP();
  302. instrs[2] = PPC_RAW_NOP();
  303. i = 0;
  304. if (types == L1D_FLUSH_FALLBACK) {
  305. instrs[i++] = PPC_RAW_MFLR(_R10);
  306. instrs[i++] = PPC_RAW_NOP(); /* branch patched below */
  307. instrs[i++] = PPC_RAW_MTLR(_R10);
  308. }
  309. if (types & L1D_FLUSH_ORI) {
  310. instrs[i++] = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
  311. instrs[i++] = PPC_RAW_ORI(_R30, _R30, 0); /* L1d flush */
  312. }
  313. if (types & L1D_FLUSH_MTTRIG)
  314. instrs[i++] = PPC_RAW_MTSPR(SPRN_TRIG2, _R0);
  315. /*
  316. * If we're patching in or out the fallback flush we need to be careful about the
  317. * order in which we patch instructions. That's because it's possible we could
  318. * take a page fault after patching one instruction, so the sequence of
  319. * instructions must be safe even in a half patched state.
  320. *
  321. * To make that work, when patching in the fallback flush we patch in this order:
  322. * - the mflr (dest)
  323. * - the mtlr (dest + 2)
  324. * - the branch (dest + 1)
  325. *
  326. * That ensures the sequence is safe to execute at any point. In contrast if we
  327. * patch the mtlr last, it's possible we could return from the branch and not
  328. * restore LR, leading to a crash later.
  329. *
  330. * When patching out the fallback flush (either with nops or another flush type),
  331. * we patch in this order:
  332. * - the branch (dest + 1)
  333. * - the mtlr (dest + 2)
  334. * - the mflr (dest)
  335. *
  336. * Note we are protected by stop_machine() from other CPUs executing the code in a
  337. * semi-patched state.
  338. */
  339. start = PTRRELOC(&__start___entry_flush_fixup);
  340. end = PTRRELOC(&__stop___entry_flush_fixup);
  341. i = do_patch_entry_fixups(start, end, instrs, types == L1D_FLUSH_FALLBACK,
  342. &entry_flush_fallback);
  343. start = PTRRELOC(&__start___scv_entry_flush_fixup);
  344. end = PTRRELOC(&__stop___scv_entry_flush_fixup);
  345. i += do_patch_entry_fixups(start, end, instrs, types == L1D_FLUSH_FALLBACK,
  346. &scv_entry_flush_fallback);
  347. printk(KERN_DEBUG "entry-flush: patched %d locations (%s flush)\n", i,
  348. (types == L1D_FLUSH_NONE) ? "no" :
  349. (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
  350. (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
  351. ? "ori+mttrig type"
  352. : "ori type" :
  353. (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
  354. : "unknown");
  355. return 0;
  356. }
  357. void do_entry_flush_fixups(enum l1d_flush_type types)
  358. {
  359. /*
  360. * The call to the fallback flush can not be safely patched in/out while
  361. * other CPUs are executing it. So call __do_entry_flush_fixups() on one
  362. * CPU while all other CPUs spin in the stop machine core with interrupts
  363. * hard disabled.
  364. */
  365. stop_machine(__do_entry_flush_fixups, &types, NULL);
  366. }
  367. static int __do_rfi_flush_fixups(void *data)
  368. {
  369. enum l1d_flush_type types = *(enum l1d_flush_type *)data;
  370. unsigned int instrs[3];
  371. long *start, *end;
  372. int i;
  373. start = PTRRELOC(&__start___rfi_flush_fixup);
  374. end = PTRRELOC(&__stop___rfi_flush_fixup);
  375. instrs[0] = PPC_RAW_NOP();
  376. instrs[1] = PPC_RAW_NOP();
  377. instrs[2] = PPC_RAW_NOP();
  378. if (types & L1D_FLUSH_FALLBACK)
  379. /* b .+16 to fallback flush */
  380. instrs[0] = PPC_RAW_BRANCH(16);
  381. i = 0;
  382. if (types & L1D_FLUSH_ORI) {
  383. instrs[i++] = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
  384. instrs[i++] = PPC_RAW_ORI(_R30, _R30, 0); /* L1d flush */
  385. }
  386. if (types & L1D_FLUSH_MTTRIG)
  387. instrs[i++] = PPC_RAW_MTSPR(SPRN_TRIG2, _R0);
  388. i = do_patch_fixups(start, end, instrs, ARRAY_SIZE(instrs));
  389. printk(KERN_DEBUG "rfi-flush: patched %d locations (%s flush)\n", i,
  390. (types == L1D_FLUSH_NONE) ? "no" :
  391. (types == L1D_FLUSH_FALLBACK) ? "fallback displacement" :
  392. (types & L1D_FLUSH_ORI) ? (types & L1D_FLUSH_MTTRIG)
  393. ? "ori+mttrig type"
  394. : "ori type" :
  395. (types & L1D_FLUSH_MTTRIG) ? "mttrig type"
  396. : "unknown");
  397. return 0;
  398. }
  399. void do_rfi_flush_fixups(enum l1d_flush_type types)
  400. {
  401. /*
  402. * stop_machine gets all CPUs out of the interrupt exit handler same
  403. * as do_stf_barrier_fixups. do_rfi_flush_fixups patching can run
  404. * without stop_machine, so this could be achieved with a broadcast
  405. * IPI instead, but this matches the stf sequence.
  406. */
  407. // Prevent static key update races with do_stf_barrier_fixups()
  408. mutex_lock(&exit_flush_lock);
  409. static_branch_enable(&interrupt_exit_not_reentrant);
  410. stop_machine(__do_rfi_flush_fixups, &types, NULL);
  411. if (types & L1D_FLUSH_FALLBACK)
  412. rfi_exit_reentrant = false;
  413. else
  414. rfi_exit_reentrant = true;
  415. if (stf_exit_reentrant && rfi_exit_reentrant)
  416. static_branch_disable(&interrupt_exit_not_reentrant);
  417. mutex_unlock(&exit_flush_lock);
  418. }
  419. void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
  420. {
  421. unsigned int instr;
  422. long *start, *end;
  423. int i;
  424. start = fixup_start;
  425. end = fixup_end;
  426. instr = PPC_RAW_NOP();
  427. if (enable) {
  428. pr_info("barrier-nospec: using ORI speculation barrier\n");
  429. instr = PPC_RAW_ORI(_R31, _R31, 0); /* speculation barrier */
  430. }
  431. i = do_patch_fixups(start, end, &instr, 1);
  432. printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
  433. }
  434. #endif /* CONFIG_PPC_BOOK3S_64 */
  435. #ifdef CONFIG_PPC_BARRIER_NOSPEC
  436. void do_barrier_nospec_fixups(bool enable)
  437. {
  438. void *start, *end;
  439. start = PTRRELOC(&__start___barrier_nospec_fixup);
  440. end = PTRRELOC(&__stop___barrier_nospec_fixup);
  441. do_barrier_nospec_fixups_range(enable, start, end);
  442. }
  443. #endif /* CONFIG_PPC_BARRIER_NOSPEC */
  444. #ifdef CONFIG_PPC_E500
  445. void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
  446. {
  447. unsigned int instr[2];
  448. long *start, *end;
  449. int i;
  450. start = fixup_start;
  451. end = fixup_end;
  452. instr[0] = PPC_RAW_NOP();
  453. instr[1] = PPC_RAW_NOP();
  454. if (enable) {
  455. pr_info("barrier-nospec: using isync; sync as speculation barrier\n");
  456. instr[0] = PPC_RAW_ISYNC();
  457. instr[1] = PPC_RAW_SYNC();
  458. }
  459. i = do_patch_fixups(start, end, instr, ARRAY_SIZE(instr));
  460. printk(KERN_DEBUG "barrier-nospec: patched %d locations\n", i);
  461. }
  462. static void __init patch_btb_flush_section(long *curr)
  463. {
  464. unsigned int *start, *end;
  465. start = (void *)curr + *curr;
  466. end = (void *)curr + *(curr + 1);
  467. for (; start < end; start++) {
  468. pr_devel("patching dest %lx\n", (unsigned long)start);
  469. patch_instruction(start, ppc_inst(PPC_RAW_NOP()));
  470. }
  471. }
  472. void __init do_btb_flush_fixups(void)
  473. {
  474. long *start, *end;
  475. start = PTRRELOC(&__start__btb_flush_fixup);
  476. end = PTRRELOC(&__stop__btb_flush_fixup);
  477. for (; start < end; start += 2)
  478. patch_btb_flush_section(start);
  479. }
  480. #endif /* CONFIG_PPC_E500 */
  481. void do_lwsync_fixups(unsigned long value, void *fixup_start, void *fixup_end)
  482. {
  483. long *start, *end;
  484. u32 *dest;
  485. if (!(value & CPU_FTR_LWSYNC))
  486. return ;
  487. start = fixup_start;
  488. end = fixup_end;
  489. for (; start < end; start++) {
  490. dest = (void *)start + *start;
  491. raw_patch_instruction(dest, ppc_inst(PPC_INST_LWSYNC));
  492. }
  493. }
  494. static void __init do_final_fixups(void)
  495. {
  496. #if defined(CONFIG_PPC64) && defined(CONFIG_RELOCATABLE)
  497. ppc_inst_t inst;
  498. u32 *src, *dest, *end;
  499. if (PHYSICAL_START == 0)
  500. return;
  501. src = (u32 *)(KERNELBASE + PHYSICAL_START);
  502. dest = (u32 *)KERNELBASE;
  503. end = (void *)src + (__end_interrupts - _stext);
  504. while (src < end) {
  505. inst = ppc_inst_read(src);
  506. raw_patch_instruction(dest, inst);
  507. src = ppc_inst_next(src, src);
  508. dest = ppc_inst_next(dest, dest);
  509. }
  510. #endif
  511. }
  512. static unsigned long __initdata saved_cpu_features;
  513. static unsigned int __initdata saved_mmu_features;
  514. #ifdef CONFIG_PPC64
  515. static unsigned long __initdata saved_firmware_features;
  516. #endif
  517. void __init apply_feature_fixups(void)
  518. {
  519. struct cpu_spec *spec = PTRRELOC(*PTRRELOC(&cur_cpu_spec));
  520. *PTRRELOC(&saved_cpu_features) = spec->cpu_features;
  521. *PTRRELOC(&saved_mmu_features) = spec->mmu_features;
  522. /*
  523. * Apply the CPU-specific and firmware specific fixups to kernel text
  524. * (nop out sections not relevant to this CPU or this firmware).
  525. */
  526. do_feature_fixups(spec->cpu_features,
  527. PTRRELOC(&__start___ftr_fixup),
  528. PTRRELOC(&__stop___ftr_fixup));
  529. do_feature_fixups(spec->mmu_features,
  530. PTRRELOC(&__start___mmu_ftr_fixup),
  531. PTRRELOC(&__stop___mmu_ftr_fixup));
  532. do_lwsync_fixups(spec->cpu_features,
  533. PTRRELOC(&__start___lwsync_fixup),
  534. PTRRELOC(&__stop___lwsync_fixup));
  535. #ifdef CONFIG_PPC64
  536. saved_firmware_features = powerpc_firmware_features;
  537. do_feature_fixups(powerpc_firmware_features,
  538. &__start___fw_ftr_fixup, &__stop___fw_ftr_fixup);
  539. #endif
  540. do_final_fixups();
  541. }
  542. void __init update_mmu_feature_fixups(unsigned long mask)
  543. {
  544. saved_mmu_features &= ~mask;
  545. saved_mmu_features |= cur_cpu_spec->mmu_features & mask;
  546. do_feature_fixups_mask(cur_cpu_spec->mmu_features, mask,
  547. PTRRELOC(&__start___mmu_ftr_fixup),
  548. PTRRELOC(&__stop___mmu_ftr_fixup));
  549. mmu_feature_keys_init();
  550. }
  551. void __init setup_feature_keys(void)
  552. {
  553. /*
  554. * Initialise jump label. This causes all the cpu/mmu_has_feature()
  555. * checks to take on their correct polarity based on the current set of
  556. * CPU/MMU features.
  557. */
  558. jump_label_init();
  559. cpu_feature_keys_init();
  560. mmu_feature_keys_init();
  561. static_key_feature_checks_initialized = true;
  562. }
  563. static int __init check_features(void)
  564. {
  565. WARN(saved_cpu_features != cur_cpu_spec->cpu_features,
  566. "CPU features changed after feature patching!\n");
  567. WARN(saved_mmu_features != cur_cpu_spec->mmu_features,
  568. "MMU features changed after feature patching!\n");
  569. #ifdef CONFIG_PPC64
  570. WARN(saved_firmware_features != powerpc_firmware_features,
  571. "Firmware features changed after feature patching!\n");
  572. #endif
  573. return 0;
  574. }
  575. late_initcall(check_features);
  576. #ifdef CONFIG_FTR_FIXUP_SELFTEST
  577. #define check(x) \
  578. if (!(x)) printk("feature-fixups: test failed at line %d\n", __LINE__);
  579. static int patch_feature_section(unsigned long value, struct fixup_entry *fcur)
  580. {
  581. return patch_feature_section_mask(value, ~0, fcur);
  582. }
  583. /* This must be after the text it fixes up, vmlinux.lds.S enforces that atm */
  584. static struct fixup_entry fixup;
  585. static long __init calc_offset(struct fixup_entry *entry, unsigned int *p)
  586. {
  587. return (unsigned long)p - (unsigned long)entry;
  588. }
  589. static void __init test_basic_patching(void)
  590. {
  591. extern unsigned int ftr_fixup_test1[];
  592. extern unsigned int end_ftr_fixup_test1[];
  593. extern unsigned int ftr_fixup_test1_orig[];
  594. extern unsigned int ftr_fixup_test1_expected[];
  595. int size = 4 * (end_ftr_fixup_test1 - ftr_fixup_test1);
  596. fixup.value = fixup.mask = 8;
  597. fixup.start_off = calc_offset(&fixup, ftr_fixup_test1 + 1);
  598. fixup.end_off = calc_offset(&fixup, ftr_fixup_test1 + 2);
  599. fixup.alt_start_off = fixup.alt_end_off = 0;
  600. /* Sanity check */
  601. check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
  602. /* Check we don't patch if the value matches */
  603. patch_feature_section(8, &fixup);
  604. check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
  605. /* Check we do patch if the value doesn't match */
  606. patch_feature_section(0, &fixup);
  607. check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
  608. /* Check we do patch if the mask doesn't match */
  609. memcpy(ftr_fixup_test1, ftr_fixup_test1_orig, size);
  610. check(memcmp(ftr_fixup_test1, ftr_fixup_test1_orig, size) == 0);
  611. patch_feature_section(~8, &fixup);
  612. check(memcmp(ftr_fixup_test1, ftr_fixup_test1_expected, size) == 0);
  613. }
  614. static void __init test_alternative_patching(void)
  615. {
  616. extern unsigned int ftr_fixup_test2[];
  617. extern unsigned int end_ftr_fixup_test2[];
  618. extern unsigned int ftr_fixup_test2_orig[];
  619. extern unsigned int ftr_fixup_test2_alt[];
  620. extern unsigned int ftr_fixup_test2_expected[];
  621. int size = 4 * (end_ftr_fixup_test2 - ftr_fixup_test2);
  622. fixup.value = fixup.mask = 0xF;
  623. fixup.start_off = calc_offset(&fixup, ftr_fixup_test2 + 1);
  624. fixup.end_off = calc_offset(&fixup, ftr_fixup_test2 + 2);
  625. fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test2_alt);
  626. fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test2_alt + 1);
  627. /* Sanity check */
  628. check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
  629. /* Check we don't patch if the value matches */
  630. patch_feature_section(0xF, &fixup);
  631. check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
  632. /* Check we do patch if the value doesn't match */
  633. patch_feature_section(0, &fixup);
  634. check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
  635. /* Check we do patch if the mask doesn't match */
  636. memcpy(ftr_fixup_test2, ftr_fixup_test2_orig, size);
  637. check(memcmp(ftr_fixup_test2, ftr_fixup_test2_orig, size) == 0);
  638. patch_feature_section(~0xF, &fixup);
  639. check(memcmp(ftr_fixup_test2, ftr_fixup_test2_expected, size) == 0);
  640. }
  641. static void __init test_alternative_case_too_big(void)
  642. {
  643. extern unsigned int ftr_fixup_test3[];
  644. extern unsigned int end_ftr_fixup_test3[];
  645. extern unsigned int ftr_fixup_test3_orig[];
  646. extern unsigned int ftr_fixup_test3_alt[];
  647. int size = 4 * (end_ftr_fixup_test3 - ftr_fixup_test3);
  648. fixup.value = fixup.mask = 0xC;
  649. fixup.start_off = calc_offset(&fixup, ftr_fixup_test3 + 1);
  650. fixup.end_off = calc_offset(&fixup, ftr_fixup_test3 + 2);
  651. fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test3_alt);
  652. fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test3_alt + 2);
  653. /* Sanity check */
  654. check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
  655. /* Expect nothing to be patched, and the error returned to us */
  656. check(patch_feature_section(0xF, &fixup) == 1);
  657. check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
  658. check(patch_feature_section(0, &fixup) == 1);
  659. check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
  660. check(patch_feature_section(~0xF, &fixup) == 1);
  661. check(memcmp(ftr_fixup_test3, ftr_fixup_test3_orig, size) == 0);
  662. }
  663. static void __init test_alternative_case_too_small(void)
  664. {
  665. extern unsigned int ftr_fixup_test4[];
  666. extern unsigned int end_ftr_fixup_test4[];
  667. extern unsigned int ftr_fixup_test4_orig[];
  668. extern unsigned int ftr_fixup_test4_alt[];
  669. extern unsigned int ftr_fixup_test4_expected[];
  670. int size = 4 * (end_ftr_fixup_test4 - ftr_fixup_test4);
  671. unsigned long flag;
  672. /* Check a high-bit flag */
  673. flag = 1UL << ((sizeof(unsigned long) - 1) * 8);
  674. fixup.value = fixup.mask = flag;
  675. fixup.start_off = calc_offset(&fixup, ftr_fixup_test4 + 1);
  676. fixup.end_off = calc_offset(&fixup, ftr_fixup_test4 + 5);
  677. fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_test4_alt);
  678. fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_test4_alt + 2);
  679. /* Sanity check */
  680. check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
  681. /* Check we don't patch if the value matches */
  682. patch_feature_section(flag, &fixup);
  683. check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
  684. /* Check we do patch if the value doesn't match */
  685. patch_feature_section(0, &fixup);
  686. check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
  687. /* Check we do patch if the mask doesn't match */
  688. memcpy(ftr_fixup_test4, ftr_fixup_test4_orig, size);
  689. check(memcmp(ftr_fixup_test4, ftr_fixup_test4_orig, size) == 0);
  690. patch_feature_section(~flag, &fixup);
  691. check(memcmp(ftr_fixup_test4, ftr_fixup_test4_expected, size) == 0);
  692. }
  693. static void test_alternative_case_with_branch(void)
  694. {
  695. extern unsigned int ftr_fixup_test5[];
  696. extern unsigned int end_ftr_fixup_test5[];
  697. extern unsigned int ftr_fixup_test5_expected[];
  698. int size = 4 * (end_ftr_fixup_test5 - ftr_fixup_test5);
  699. check(memcmp(ftr_fixup_test5, ftr_fixup_test5_expected, size) == 0);
  700. }
  701. static void __init test_alternative_case_with_external_branch(void)
  702. {
  703. extern unsigned int ftr_fixup_test6[];
  704. extern unsigned int end_ftr_fixup_test6[];
  705. extern unsigned int ftr_fixup_test6_expected[];
  706. int size = 4 * (end_ftr_fixup_test6 - ftr_fixup_test6);
  707. check(memcmp(ftr_fixup_test6, ftr_fixup_test6_expected, size) == 0);
  708. }
  709. static void __init test_alternative_case_with_branch_to_end(void)
  710. {
  711. extern unsigned int ftr_fixup_test7[];
  712. extern unsigned int end_ftr_fixup_test7[];
  713. extern unsigned int ftr_fixup_test7_expected[];
  714. int size = 4 * (end_ftr_fixup_test7 - ftr_fixup_test7);
  715. check(memcmp(ftr_fixup_test7, ftr_fixup_test7_expected, size) == 0);
  716. }
  717. static void __init test_cpu_macros(void)
  718. {
  719. extern u8 ftr_fixup_test_FTR_macros[];
  720. extern u8 ftr_fixup_test_FTR_macros_expected[];
  721. unsigned long size = ftr_fixup_test_FTR_macros_expected -
  722. ftr_fixup_test_FTR_macros;
  723. /* The fixups have already been done for us during boot */
  724. check(memcmp(ftr_fixup_test_FTR_macros,
  725. ftr_fixup_test_FTR_macros_expected, size) == 0);
  726. }
  727. static void __init test_fw_macros(void)
  728. {
  729. #ifdef CONFIG_PPC64
  730. extern u8 ftr_fixup_test_FW_FTR_macros[];
  731. extern u8 ftr_fixup_test_FW_FTR_macros_expected[];
  732. unsigned long size = ftr_fixup_test_FW_FTR_macros_expected -
  733. ftr_fixup_test_FW_FTR_macros;
  734. /* The fixups have already been done for us during boot */
  735. check(memcmp(ftr_fixup_test_FW_FTR_macros,
  736. ftr_fixup_test_FW_FTR_macros_expected, size) == 0);
  737. #endif
  738. }
  739. static void __init test_lwsync_macros(void)
  740. {
  741. extern u8 lwsync_fixup_test[];
  742. extern u8 end_lwsync_fixup_test[];
  743. extern u8 lwsync_fixup_test_expected_LWSYNC[];
  744. extern u8 lwsync_fixup_test_expected_SYNC[];
  745. unsigned long size = end_lwsync_fixup_test -
  746. lwsync_fixup_test;
  747. /* The fixups have already been done for us during boot */
  748. if (cur_cpu_spec->cpu_features & CPU_FTR_LWSYNC) {
  749. check(memcmp(lwsync_fixup_test,
  750. lwsync_fixup_test_expected_LWSYNC, size) == 0);
  751. } else {
  752. check(memcmp(lwsync_fixup_test,
  753. lwsync_fixup_test_expected_SYNC, size) == 0);
  754. }
  755. }
  756. #ifdef CONFIG_PPC64
  757. static void __init test_prefix_patching(void)
  758. {
  759. extern unsigned int ftr_fixup_prefix1[];
  760. extern unsigned int end_ftr_fixup_prefix1[];
  761. extern unsigned int ftr_fixup_prefix1_orig[];
  762. extern unsigned int ftr_fixup_prefix1_expected[];
  763. int size = sizeof(unsigned int) * (end_ftr_fixup_prefix1 - ftr_fixup_prefix1);
  764. fixup.value = fixup.mask = 8;
  765. fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix1 + 1);
  766. fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix1 + 3);
  767. fixup.alt_start_off = fixup.alt_end_off = 0;
  768. /* Sanity check */
  769. check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) == 0);
  770. patch_feature_section(0, &fixup);
  771. check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_expected, size) == 0);
  772. check(memcmp(ftr_fixup_prefix1, ftr_fixup_prefix1_orig, size) != 0);
  773. }
  774. static void __init test_prefix_alt_patching(void)
  775. {
  776. extern unsigned int ftr_fixup_prefix2[];
  777. extern unsigned int end_ftr_fixup_prefix2[];
  778. extern unsigned int ftr_fixup_prefix2_orig[];
  779. extern unsigned int ftr_fixup_prefix2_expected[];
  780. extern unsigned int ftr_fixup_prefix2_alt[];
  781. int size = sizeof(unsigned int) * (end_ftr_fixup_prefix2 - ftr_fixup_prefix2);
  782. fixup.value = fixup.mask = 8;
  783. fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix2 + 1);
  784. fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix2 + 3);
  785. fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix2_alt);
  786. fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix2_alt + 2);
  787. /* Sanity check */
  788. check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) == 0);
  789. patch_feature_section(0, &fixup);
  790. check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_expected, size) == 0);
  791. check(memcmp(ftr_fixup_prefix2, ftr_fixup_prefix2_orig, size) != 0);
  792. }
  793. static void __init test_prefix_word_alt_patching(void)
  794. {
  795. extern unsigned int ftr_fixup_prefix3[];
  796. extern unsigned int end_ftr_fixup_prefix3[];
  797. extern unsigned int ftr_fixup_prefix3_orig[];
  798. extern unsigned int ftr_fixup_prefix3_expected[];
  799. extern unsigned int ftr_fixup_prefix3_alt[];
  800. int size = sizeof(unsigned int) * (end_ftr_fixup_prefix3 - ftr_fixup_prefix3);
  801. fixup.value = fixup.mask = 8;
  802. fixup.start_off = calc_offset(&fixup, ftr_fixup_prefix3 + 1);
  803. fixup.end_off = calc_offset(&fixup, ftr_fixup_prefix3 + 4);
  804. fixup.alt_start_off = calc_offset(&fixup, ftr_fixup_prefix3_alt);
  805. fixup.alt_end_off = calc_offset(&fixup, ftr_fixup_prefix3_alt + 3);
  806. /* Sanity check */
  807. check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) == 0);
  808. patch_feature_section(0, &fixup);
  809. check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_expected, size) == 0);
  810. patch_feature_section(0, &fixup);
  811. check(memcmp(ftr_fixup_prefix3, ftr_fixup_prefix3_orig, size) != 0);
  812. }
  813. #else
  814. static inline void test_prefix_patching(void) {}
  815. static inline void test_prefix_alt_patching(void) {}
  816. static inline void test_prefix_word_alt_patching(void) {}
  817. #endif /* CONFIG_PPC64 */
  818. static int __init test_feature_fixups(void)
  819. {
  820. printk(KERN_DEBUG "Running feature fixup self-tests ...\n");
  821. test_basic_patching();
  822. test_alternative_patching();
  823. test_alternative_case_too_big();
  824. test_alternative_case_too_small();
  825. test_alternative_case_with_branch();
  826. test_alternative_case_with_external_branch();
  827. test_alternative_case_with_branch_to_end();
  828. test_cpu_macros();
  829. test_fw_macros();
  830. test_lwsync_macros();
  831. test_prefix_patching();
  832. test_prefix_alt_patching();
  833. test_prefix_word_alt_patching();
  834. return 0;
  835. }
  836. late_initcall(test_feature_fixups);
  837. #endif /* CONFIG_FTR_FIXUP_SELFTEST */