feature-fixups.c 31 KB

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