proton-pack.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Handle detection, reporting and mitigation of Spectre v1, v2, v3a and v4, as
  4. * detailed at:
  5. *
  6. * https://developer.arm.com/support/arm-security-updates/speculative-processor-vulnerability
  7. *
  8. * This code was originally written hastily under an awful lot of stress and so
  9. * aspects of it are somewhat hacky. Unfortunately, changing anything in here
  10. * instantly makes me feel ill. Thanks, Jann. Thann.
  11. *
  12. * Copyright (C) 2018 ARM Ltd, All Rights Reserved.
  13. * Copyright (C) 2020 Google LLC
  14. *
  15. * "If there's something strange in your neighbourhood, who you gonna call?"
  16. *
  17. * Authors: Will Deacon <will@kernel.org> and Marc Zyngier <maz@kernel.org>
  18. */
  19. #include <linux/arm-smccc.h>
  20. #include <linux/bpf.h>
  21. #include <linux/cpu.h>
  22. #include <linux/device.h>
  23. #include <linux/nospec.h>
  24. #include <linux/prctl.h>
  25. #include <linux/sched/task_stack.h>
  26. #include <asm/debug-monitors.h>
  27. #include <asm/insn.h>
  28. #include <asm/spectre.h>
  29. #include <asm/traps.h>
  30. #include <asm/vectors.h>
  31. #include <asm/virt.h>
  32. /*
  33. * We try to ensure that the mitigation state can never change as the result of
  34. * onlining a late CPU.
  35. */
  36. static void update_mitigation_state(enum mitigation_state *oldp,
  37. enum mitigation_state new)
  38. {
  39. enum mitigation_state state;
  40. do {
  41. state = READ_ONCE(*oldp);
  42. if (new <= state)
  43. break;
  44. /* Userspace almost certainly can't deal with this. */
  45. if (WARN_ON(system_capabilities_finalized()))
  46. break;
  47. } while (cmpxchg_relaxed(oldp, state, new) != state);
  48. }
  49. /*
  50. * Spectre v1.
  51. *
  52. * The kernel can't protect userspace for this one: it's each person for
  53. * themselves. Advertise what we're doing and be done with it.
  54. */
  55. ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr,
  56. char *buf)
  57. {
  58. return sprintf(buf, "Mitigation: __user pointer sanitization\n");
  59. }
  60. /*
  61. * Spectre v2.
  62. *
  63. * This one sucks. A CPU is either:
  64. *
  65. * - Mitigated in hardware and advertised by ID_AA64PFR0_EL1.CSV2.
  66. * - Mitigated in hardware and listed in our "safe list".
  67. * - Mitigated in software by firmware.
  68. * - Mitigated in software by a CPU-specific dance in the kernel and a
  69. * firmware call at EL2.
  70. * - Vulnerable.
  71. *
  72. * It's not unlikely for different CPUs in a big.LITTLE system to fall into
  73. * different camps.
  74. */
  75. static enum mitigation_state spectre_v2_state;
  76. static bool __read_mostly __nospectre_v2;
  77. static int __init parse_spectre_v2_param(char *str)
  78. {
  79. __nospectre_v2 = true;
  80. return 0;
  81. }
  82. early_param("nospectre_v2", parse_spectre_v2_param);
  83. static bool spectre_v2_mitigations_off(void)
  84. {
  85. bool ret = __nospectre_v2 || cpu_mitigations_off();
  86. if (ret)
  87. pr_info_once("spectre-v2 mitigation disabled by command line option\n");
  88. return ret;
  89. }
  90. static const char *get_bhb_affected_string(enum mitigation_state bhb_state)
  91. {
  92. switch (bhb_state) {
  93. case SPECTRE_UNAFFECTED:
  94. return "";
  95. default:
  96. case SPECTRE_VULNERABLE:
  97. return ", but not BHB";
  98. case SPECTRE_MITIGATED:
  99. return ", BHB";
  100. }
  101. }
  102. static bool _unprivileged_ebpf_enabled(void)
  103. {
  104. #ifdef CONFIG_BPF_SYSCALL
  105. return !sysctl_unprivileged_bpf_disabled;
  106. #else
  107. return false;
  108. #endif
  109. }
  110. ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr,
  111. char *buf)
  112. {
  113. enum mitigation_state bhb_state = arm64_get_spectre_bhb_state();
  114. const char *bhb_str = get_bhb_affected_string(bhb_state);
  115. const char *v2_str = "Branch predictor hardening";
  116. switch (spectre_v2_state) {
  117. case SPECTRE_UNAFFECTED:
  118. if (bhb_state == SPECTRE_UNAFFECTED)
  119. return sprintf(buf, "Not affected\n");
  120. /*
  121. * Platforms affected by Spectre-BHB can't report
  122. * "Not affected" for Spectre-v2.
  123. */
  124. v2_str = "CSV2";
  125. fallthrough;
  126. case SPECTRE_MITIGATED:
  127. if (bhb_state == SPECTRE_MITIGATED && _unprivileged_ebpf_enabled())
  128. return sprintf(buf, "Vulnerable: Unprivileged eBPF enabled\n");
  129. return sprintf(buf, "Mitigation: %s%s\n", v2_str, bhb_str);
  130. case SPECTRE_VULNERABLE:
  131. fallthrough;
  132. default:
  133. return sprintf(buf, "Vulnerable\n");
  134. }
  135. }
  136. static enum mitigation_state spectre_v2_get_cpu_hw_mitigation_state(void)
  137. {
  138. u64 pfr0;
  139. static const struct midr_range spectre_v2_safe_list[] = {
  140. MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
  141. MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
  142. MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
  143. MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
  144. MIDR_ALL_VERSIONS(MIDR_HISI_TSV110),
  145. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_2XX_SILVER),
  146. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_3XX_SILVER),
  147. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_4XX_SILVER),
  148. { /* sentinel */ }
  149. };
  150. /* If the CPU has CSV2 set, we're safe */
  151. pfr0 = read_cpuid(ID_AA64PFR0_EL1);
  152. if (cpuid_feature_extract_unsigned_field(pfr0, ID_AA64PFR0_EL1_CSV2_SHIFT))
  153. return SPECTRE_UNAFFECTED;
  154. /* Alternatively, we have a list of unaffected CPUs */
  155. if (is_midr_in_range_list(read_cpuid_id(), spectre_v2_safe_list))
  156. return SPECTRE_UNAFFECTED;
  157. return SPECTRE_VULNERABLE;
  158. }
  159. static enum mitigation_state spectre_v2_get_cpu_fw_mitigation_state(void)
  160. {
  161. int ret;
  162. struct arm_smccc_res res;
  163. arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  164. ARM_SMCCC_ARCH_WORKAROUND_1, &res);
  165. ret = res.a0;
  166. switch (ret) {
  167. case SMCCC_RET_SUCCESS:
  168. return SPECTRE_MITIGATED;
  169. case SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED:
  170. return SPECTRE_UNAFFECTED;
  171. default:
  172. fallthrough;
  173. case SMCCC_RET_NOT_SUPPORTED:
  174. return SPECTRE_VULNERABLE;
  175. }
  176. }
  177. bool has_spectre_v2(const struct arm64_cpu_capabilities *entry, int scope)
  178. {
  179. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  180. if (spectre_v2_get_cpu_hw_mitigation_state() == SPECTRE_UNAFFECTED)
  181. return false;
  182. if (spectre_v2_get_cpu_fw_mitigation_state() == SPECTRE_UNAFFECTED)
  183. return false;
  184. return true;
  185. }
  186. enum mitigation_state arm64_get_spectre_v2_state(void)
  187. {
  188. return spectre_v2_state;
  189. }
  190. DEFINE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
  191. static void install_bp_hardening_cb(bp_hardening_cb_t fn)
  192. {
  193. __this_cpu_write(bp_hardening_data.fn, fn);
  194. /*
  195. * Vinz Clortho takes the hyp_vecs start/end "keys" at
  196. * the door when we're a guest. Skip the hyp-vectors work.
  197. */
  198. if (!is_hyp_mode_available())
  199. return;
  200. __this_cpu_write(bp_hardening_data.slot, HYP_VECTOR_SPECTRE_DIRECT);
  201. }
  202. /* Called during entry so must be noinstr */
  203. static noinstr void call_smc_arch_workaround_1(void)
  204. {
  205. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
  206. }
  207. /* Called during entry so must be noinstr */
  208. static noinstr void call_hvc_arch_workaround_1(void)
  209. {
  210. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
  211. }
  212. /* Called during entry so must be noinstr */
  213. static noinstr void qcom_link_stack_sanitisation(void)
  214. {
  215. u64 tmp;
  216. asm volatile("mov %0, x30 \n"
  217. ".rept 16 \n"
  218. "bl . + 4 \n"
  219. ".endr \n"
  220. "mov x30, %0 \n"
  221. : "=&r" (tmp));
  222. }
  223. static bp_hardening_cb_t spectre_v2_get_sw_mitigation_cb(void)
  224. {
  225. u32 midr = read_cpuid_id();
  226. if (((midr & MIDR_CPU_MODEL_MASK) != MIDR_QCOM_FALKOR) &&
  227. ((midr & MIDR_CPU_MODEL_MASK) != MIDR_QCOM_FALKOR_V1))
  228. return NULL;
  229. return qcom_link_stack_sanitisation;
  230. }
  231. static enum mitigation_state spectre_v2_enable_fw_mitigation(void)
  232. {
  233. bp_hardening_cb_t cb;
  234. enum mitigation_state state;
  235. state = spectre_v2_get_cpu_fw_mitigation_state();
  236. if (state != SPECTRE_MITIGATED)
  237. return state;
  238. if (spectre_v2_mitigations_off())
  239. return SPECTRE_VULNERABLE;
  240. switch (arm_smccc_1_1_get_conduit()) {
  241. case SMCCC_CONDUIT_HVC:
  242. cb = call_hvc_arch_workaround_1;
  243. break;
  244. case SMCCC_CONDUIT_SMC:
  245. cb = call_smc_arch_workaround_1;
  246. break;
  247. default:
  248. return SPECTRE_VULNERABLE;
  249. }
  250. /*
  251. * Prefer a CPU-specific workaround if it exists. Note that we
  252. * still rely on firmware for the mitigation at EL2.
  253. */
  254. cb = spectre_v2_get_sw_mitigation_cb() ?: cb;
  255. install_bp_hardening_cb(cb);
  256. return SPECTRE_MITIGATED;
  257. }
  258. void spectre_v2_enable_mitigation(const struct arm64_cpu_capabilities *__unused)
  259. {
  260. enum mitigation_state state;
  261. WARN_ON(preemptible());
  262. state = spectre_v2_get_cpu_hw_mitigation_state();
  263. if (state == SPECTRE_VULNERABLE)
  264. state = spectre_v2_enable_fw_mitigation();
  265. update_mitigation_state(&spectre_v2_state, state);
  266. }
  267. /*
  268. * Spectre-v3a.
  269. *
  270. * Phew, there's not an awful lot to do here! We just instruct EL2 to use
  271. * an indirect trampoline for the hyp vectors so that guests can't read
  272. * VBAR_EL2 to defeat randomisation of the hypervisor VA layout.
  273. */
  274. bool has_spectre_v3a(const struct arm64_cpu_capabilities *entry, int scope)
  275. {
  276. static const struct midr_range spectre_v3a_unsafe_list[] = {
  277. MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
  278. MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
  279. {},
  280. };
  281. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  282. return is_midr_in_range_list(read_cpuid_id(), spectre_v3a_unsafe_list);
  283. }
  284. void spectre_v3a_enable_mitigation(const struct arm64_cpu_capabilities *__unused)
  285. {
  286. struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
  287. if (this_cpu_has_cap(ARM64_SPECTRE_V3A))
  288. data->slot += HYP_VECTOR_INDIRECT;
  289. }
  290. /*
  291. * Spectre v4.
  292. *
  293. * If you thought Spectre v2 was nasty, wait until you see this mess. A CPU is
  294. * either:
  295. *
  296. * - Mitigated in hardware and listed in our "safe list".
  297. * - Mitigated in hardware via PSTATE.SSBS.
  298. * - Mitigated in software by firmware (sometimes referred to as SSBD).
  299. *
  300. * Wait, that doesn't sound so bad, does it? Keep reading...
  301. *
  302. * A major source of headaches is that the software mitigation is enabled both
  303. * on a per-task basis, but can also be forced on for the kernel, necessitating
  304. * both context-switch *and* entry/exit hooks. To make it even worse, some CPUs
  305. * allow EL0 to toggle SSBS directly, which can end up with the prctl() state
  306. * being stale when re-entering the kernel. The usual big.LITTLE caveats apply,
  307. * so you can have systems that have both firmware and SSBS mitigations. This
  308. * means we actually have to reject late onlining of CPUs with mitigations if
  309. * all of the currently onlined CPUs are safelisted, as the mitigation tends to
  310. * be opt-in for userspace. Yes, really, the cure is worse than the disease.
  311. *
  312. * The only good part is that if the firmware mitigation is present, then it is
  313. * present for all CPUs, meaning we don't have to worry about late onlining of a
  314. * vulnerable CPU if one of the boot CPUs is using the firmware mitigation.
  315. *
  316. * Give me a VAX-11/780 any day of the week...
  317. */
  318. static enum mitigation_state spectre_v4_state;
  319. /* This is the per-cpu state tracking whether we need to talk to firmware */
  320. DEFINE_PER_CPU_READ_MOSTLY(u64, arm64_ssbd_callback_required);
  321. enum spectre_v4_policy {
  322. SPECTRE_V4_POLICY_MITIGATION_DYNAMIC,
  323. SPECTRE_V4_POLICY_MITIGATION_ENABLED,
  324. SPECTRE_V4_POLICY_MITIGATION_DISABLED,
  325. };
  326. static enum spectre_v4_policy __read_mostly __spectre_v4_policy;
  327. static const struct spectre_v4_param {
  328. const char *str;
  329. enum spectre_v4_policy policy;
  330. } spectre_v4_params[] = {
  331. { "force-on", SPECTRE_V4_POLICY_MITIGATION_ENABLED, },
  332. { "force-off", SPECTRE_V4_POLICY_MITIGATION_DISABLED, },
  333. { "kernel", SPECTRE_V4_POLICY_MITIGATION_DYNAMIC, },
  334. };
  335. static int __init parse_spectre_v4_param(char *str)
  336. {
  337. int i;
  338. if (!str || !str[0])
  339. return -EINVAL;
  340. for (i = 0; i < ARRAY_SIZE(spectre_v4_params); i++) {
  341. const struct spectre_v4_param *param = &spectre_v4_params[i];
  342. if (strncmp(str, param->str, strlen(param->str)))
  343. continue;
  344. __spectre_v4_policy = param->policy;
  345. return 0;
  346. }
  347. return -EINVAL;
  348. }
  349. early_param("ssbd", parse_spectre_v4_param);
  350. /*
  351. * Because this was all written in a rush by people working in different silos,
  352. * we've ended up with multiple command line options to control the same thing.
  353. * Wrap these up in some helpers, which prefer disabling the mitigation if faced
  354. * with contradictory parameters. The mitigation is always either "off",
  355. * "dynamic" or "on".
  356. */
  357. static bool spectre_v4_mitigations_off(void)
  358. {
  359. bool ret = cpu_mitigations_off() ||
  360. __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DISABLED;
  361. if (ret)
  362. pr_info_once("spectre-v4 mitigation disabled by command-line option\n");
  363. return ret;
  364. }
  365. /* Do we need to toggle the mitigation state on entry to/exit from the kernel? */
  366. static bool spectre_v4_mitigations_dynamic(void)
  367. {
  368. return !spectre_v4_mitigations_off() &&
  369. __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_DYNAMIC;
  370. }
  371. static bool spectre_v4_mitigations_on(void)
  372. {
  373. return !spectre_v4_mitigations_off() &&
  374. __spectre_v4_policy == SPECTRE_V4_POLICY_MITIGATION_ENABLED;
  375. }
  376. ssize_t cpu_show_spec_store_bypass(struct device *dev,
  377. struct device_attribute *attr, char *buf)
  378. {
  379. switch (spectre_v4_state) {
  380. case SPECTRE_UNAFFECTED:
  381. return sprintf(buf, "Not affected\n");
  382. case SPECTRE_MITIGATED:
  383. return sprintf(buf, "Mitigation: Speculative Store Bypass disabled via prctl\n");
  384. case SPECTRE_VULNERABLE:
  385. fallthrough;
  386. default:
  387. return sprintf(buf, "Vulnerable\n");
  388. }
  389. }
  390. enum mitigation_state arm64_get_spectre_v4_state(void)
  391. {
  392. return spectre_v4_state;
  393. }
  394. static enum mitigation_state spectre_v4_get_cpu_hw_mitigation_state(void)
  395. {
  396. static const struct midr_range spectre_v4_safe_list[] = {
  397. MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
  398. MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
  399. MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
  400. MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
  401. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_3XX_SILVER),
  402. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_4XX_SILVER),
  403. { /* sentinel */ },
  404. };
  405. if (is_midr_in_range_list(read_cpuid_id(), spectre_v4_safe_list))
  406. return SPECTRE_UNAFFECTED;
  407. /* CPU features are detected first */
  408. if (this_cpu_has_cap(ARM64_SSBS))
  409. return SPECTRE_MITIGATED;
  410. return SPECTRE_VULNERABLE;
  411. }
  412. static enum mitigation_state spectre_v4_get_cpu_fw_mitigation_state(void)
  413. {
  414. int ret;
  415. struct arm_smccc_res res;
  416. arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  417. ARM_SMCCC_ARCH_WORKAROUND_2, &res);
  418. ret = res.a0;
  419. switch (ret) {
  420. case SMCCC_RET_SUCCESS:
  421. return SPECTRE_MITIGATED;
  422. case SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED:
  423. fallthrough;
  424. case SMCCC_RET_NOT_REQUIRED:
  425. return SPECTRE_UNAFFECTED;
  426. default:
  427. fallthrough;
  428. case SMCCC_RET_NOT_SUPPORTED:
  429. return SPECTRE_VULNERABLE;
  430. }
  431. }
  432. bool has_spectre_v4(const struct arm64_cpu_capabilities *cap, int scope)
  433. {
  434. enum mitigation_state state;
  435. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  436. state = spectre_v4_get_cpu_hw_mitigation_state();
  437. if (state == SPECTRE_VULNERABLE)
  438. state = spectre_v4_get_cpu_fw_mitigation_state();
  439. return state != SPECTRE_UNAFFECTED;
  440. }
  441. bool try_emulate_el1_ssbs(struct pt_regs *regs, u32 instr)
  442. {
  443. const u32 instr_mask = ~(1U << PSTATE_Imm_shift);
  444. const u32 instr_val = 0xd500401f | PSTATE_SSBS;
  445. if ((instr & instr_mask) != instr_val)
  446. return false;
  447. if (instr & BIT(PSTATE_Imm_shift))
  448. regs->pstate |= PSR_SSBS_BIT;
  449. else
  450. regs->pstate &= ~PSR_SSBS_BIT;
  451. arm64_skip_faulting_instruction(regs, 4);
  452. return true;
  453. }
  454. static enum mitigation_state spectre_v4_enable_hw_mitigation(void)
  455. {
  456. enum mitigation_state state;
  457. /*
  458. * If the system is mitigated but this CPU doesn't have SSBS, then
  459. * we must be on the safelist and there's nothing more to do.
  460. */
  461. state = spectre_v4_get_cpu_hw_mitigation_state();
  462. if (state != SPECTRE_MITIGATED || !this_cpu_has_cap(ARM64_SSBS))
  463. return state;
  464. if (spectre_v4_mitigations_off()) {
  465. sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_DSSBS);
  466. set_pstate_ssbs(1);
  467. return SPECTRE_VULNERABLE;
  468. }
  469. /* SCTLR_EL1.DSSBS was initialised to 0 during boot */
  470. set_pstate_ssbs(0);
  471. /*
  472. * SSBS is self-synchronizing and is intended to affect subsequent
  473. * speculative instructions, but some CPUs can speculate with a stale
  474. * value of SSBS.
  475. *
  476. * Mitigate this with an unconditional speculation barrier, as CPUs
  477. * could mis-speculate branches and bypass a conditional barrier.
  478. */
  479. if (IS_ENABLED(CONFIG_ARM64_ERRATUM_3194386))
  480. spec_bar();
  481. return SPECTRE_MITIGATED;
  482. }
  483. /*
  484. * Patch a branch over the Spectre-v4 mitigation code with a NOP so that
  485. * we fallthrough and check whether firmware needs to be called on this CPU.
  486. */
  487. void __init spectre_v4_patch_fw_mitigation_enable(struct alt_instr *alt,
  488. __le32 *origptr,
  489. __le32 *updptr, int nr_inst)
  490. {
  491. BUG_ON(nr_inst != 1); /* Branch -> NOP */
  492. if (spectre_v4_mitigations_off())
  493. return;
  494. if (cpus_have_cap(ARM64_SSBS))
  495. return;
  496. if (spectre_v4_mitigations_dynamic())
  497. *updptr = cpu_to_le32(aarch64_insn_gen_nop());
  498. }
  499. /*
  500. * Patch a NOP in the Spectre-v4 mitigation code with an SMC/HVC instruction
  501. * to call into firmware to adjust the mitigation state.
  502. */
  503. void __init smccc_patch_fw_mitigation_conduit(struct alt_instr *alt,
  504. __le32 *origptr,
  505. __le32 *updptr, int nr_inst)
  506. {
  507. u32 insn;
  508. BUG_ON(nr_inst != 1); /* NOP -> HVC/SMC */
  509. switch (arm_smccc_1_1_get_conduit()) {
  510. case SMCCC_CONDUIT_HVC:
  511. insn = aarch64_insn_get_hvc_value();
  512. break;
  513. case SMCCC_CONDUIT_SMC:
  514. insn = aarch64_insn_get_smc_value();
  515. break;
  516. default:
  517. return;
  518. }
  519. *updptr = cpu_to_le32(insn);
  520. }
  521. static enum mitigation_state spectre_v4_enable_fw_mitigation(void)
  522. {
  523. enum mitigation_state state;
  524. state = spectre_v4_get_cpu_fw_mitigation_state();
  525. if (state != SPECTRE_MITIGATED)
  526. return state;
  527. if (spectre_v4_mitigations_off()) {
  528. arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_WORKAROUND_2, false, NULL);
  529. return SPECTRE_VULNERABLE;
  530. }
  531. arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_WORKAROUND_2, true, NULL);
  532. if (spectre_v4_mitigations_dynamic())
  533. __this_cpu_write(arm64_ssbd_callback_required, 1);
  534. return SPECTRE_MITIGATED;
  535. }
  536. void spectre_v4_enable_mitigation(const struct arm64_cpu_capabilities *__unused)
  537. {
  538. enum mitigation_state state;
  539. WARN_ON(preemptible());
  540. state = spectre_v4_enable_hw_mitigation();
  541. if (state == SPECTRE_VULNERABLE)
  542. state = spectre_v4_enable_fw_mitigation();
  543. update_mitigation_state(&spectre_v4_state, state);
  544. }
  545. static void __update_pstate_ssbs(struct pt_regs *regs, bool state)
  546. {
  547. u64 bit = compat_user_mode(regs) ? PSR_AA32_SSBS_BIT : PSR_SSBS_BIT;
  548. if (state)
  549. regs->pstate |= bit;
  550. else
  551. regs->pstate &= ~bit;
  552. }
  553. void spectre_v4_enable_task_mitigation(struct task_struct *tsk)
  554. {
  555. struct pt_regs *regs = task_pt_regs(tsk);
  556. bool ssbs = false, kthread = tsk->flags & PF_KTHREAD;
  557. if (spectre_v4_mitigations_off())
  558. ssbs = true;
  559. else if (spectre_v4_mitigations_dynamic() && !kthread)
  560. ssbs = !test_tsk_thread_flag(tsk, TIF_SSBD);
  561. __update_pstate_ssbs(regs, ssbs);
  562. }
  563. /*
  564. * The Spectre-v4 mitigation can be controlled via a prctl() from userspace.
  565. * This is interesting because the "speculation disabled" behaviour can be
  566. * configured so that it is preserved across exec(), which means that the
  567. * prctl() may be necessary even when PSTATE.SSBS can be toggled directly
  568. * from userspace.
  569. */
  570. static void ssbd_prctl_enable_mitigation(struct task_struct *task)
  571. {
  572. task_clear_spec_ssb_noexec(task);
  573. task_set_spec_ssb_disable(task);
  574. set_tsk_thread_flag(task, TIF_SSBD);
  575. }
  576. static void ssbd_prctl_disable_mitigation(struct task_struct *task)
  577. {
  578. task_clear_spec_ssb_noexec(task);
  579. task_clear_spec_ssb_disable(task);
  580. clear_tsk_thread_flag(task, TIF_SSBD);
  581. }
  582. static int ssbd_prctl_set(struct task_struct *task, unsigned long ctrl)
  583. {
  584. switch (ctrl) {
  585. case PR_SPEC_ENABLE:
  586. /* Enable speculation: disable mitigation */
  587. /*
  588. * Force disabled speculation prevents it from being
  589. * re-enabled.
  590. */
  591. if (task_spec_ssb_force_disable(task))
  592. return -EPERM;
  593. /*
  594. * If the mitigation is forced on, then speculation is forced
  595. * off and we again prevent it from being re-enabled.
  596. */
  597. if (spectre_v4_mitigations_on())
  598. return -EPERM;
  599. ssbd_prctl_disable_mitigation(task);
  600. break;
  601. case PR_SPEC_FORCE_DISABLE:
  602. /* Force disable speculation: force enable mitigation */
  603. /*
  604. * If the mitigation is forced off, then speculation is forced
  605. * on and we prevent it from being disabled.
  606. */
  607. if (spectre_v4_mitigations_off())
  608. return -EPERM;
  609. task_set_spec_ssb_force_disable(task);
  610. fallthrough;
  611. case PR_SPEC_DISABLE:
  612. /* Disable speculation: enable mitigation */
  613. /* Same as PR_SPEC_FORCE_DISABLE */
  614. if (spectre_v4_mitigations_off())
  615. return -EPERM;
  616. ssbd_prctl_enable_mitigation(task);
  617. break;
  618. case PR_SPEC_DISABLE_NOEXEC:
  619. /* Disable speculation until execve(): enable mitigation */
  620. /*
  621. * If the mitigation state is forced one way or the other, then
  622. * we must fail now before we try to toggle it on execve().
  623. */
  624. if (task_spec_ssb_force_disable(task) ||
  625. spectre_v4_mitigations_off() ||
  626. spectre_v4_mitigations_on()) {
  627. return -EPERM;
  628. }
  629. ssbd_prctl_enable_mitigation(task);
  630. task_set_spec_ssb_noexec(task);
  631. break;
  632. default:
  633. return -ERANGE;
  634. }
  635. spectre_v4_enable_task_mitigation(task);
  636. return 0;
  637. }
  638. int arch_prctl_spec_ctrl_set(struct task_struct *task, unsigned long which,
  639. unsigned long ctrl)
  640. {
  641. switch (which) {
  642. case PR_SPEC_STORE_BYPASS:
  643. return ssbd_prctl_set(task, ctrl);
  644. default:
  645. return -ENODEV;
  646. }
  647. }
  648. static int ssbd_prctl_get(struct task_struct *task)
  649. {
  650. switch (spectre_v4_state) {
  651. case SPECTRE_UNAFFECTED:
  652. return PR_SPEC_NOT_AFFECTED;
  653. case SPECTRE_MITIGATED:
  654. if (spectre_v4_mitigations_on())
  655. return PR_SPEC_NOT_AFFECTED;
  656. if (spectre_v4_mitigations_dynamic())
  657. break;
  658. /* Mitigations are disabled, so we're vulnerable. */
  659. fallthrough;
  660. case SPECTRE_VULNERABLE:
  661. fallthrough;
  662. default:
  663. return PR_SPEC_ENABLE;
  664. }
  665. /* Check the mitigation state for this task */
  666. if (task_spec_ssb_force_disable(task))
  667. return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
  668. if (task_spec_ssb_noexec(task))
  669. return PR_SPEC_PRCTL | PR_SPEC_DISABLE_NOEXEC;
  670. if (task_spec_ssb_disable(task))
  671. return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
  672. return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
  673. }
  674. int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
  675. {
  676. switch (which) {
  677. case PR_SPEC_STORE_BYPASS:
  678. return ssbd_prctl_get(task);
  679. default:
  680. return -ENODEV;
  681. }
  682. }
  683. /*
  684. * Spectre BHB.
  685. *
  686. * A CPU is either:
  687. * - Mitigated by a branchy loop a CPU specific number of times, and listed
  688. * in our "loop mitigated list".
  689. * - Mitigated in software by the firmware Spectre v2 call.
  690. * - Has the ClearBHB instruction to perform the mitigation.
  691. * - Has the 'Exception Clears Branch History Buffer' (ECBHB) feature, so no
  692. * software mitigation in the vectors is needed.
  693. * - Has CSV2.3, so is unaffected.
  694. */
  695. static enum mitigation_state spectre_bhb_state;
  696. enum mitigation_state arm64_get_spectre_bhb_state(void)
  697. {
  698. return spectre_bhb_state;
  699. }
  700. enum bhb_mitigation_bits {
  701. BHB_LOOP,
  702. BHB_FW,
  703. BHB_HW,
  704. BHB_INSN,
  705. };
  706. static unsigned long system_bhb_mitigations;
  707. /*
  708. * This must be called with SCOPE_LOCAL_CPU for each type of CPU, before any
  709. * SCOPE_SYSTEM call will give the right answer.
  710. */
  711. static bool is_spectre_bhb_safe(int scope)
  712. {
  713. static const struct midr_range spectre_bhb_safe_list[] = {
  714. MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
  715. MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
  716. MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
  717. MIDR_ALL_VERSIONS(MIDR_CORTEX_A510),
  718. MIDR_ALL_VERSIONS(MIDR_CORTEX_A520),
  719. MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
  720. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_2XX_SILVER),
  721. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_3XX_SILVER),
  722. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_4XX_SILVER),
  723. {},
  724. };
  725. static bool all_safe = true;
  726. if (scope != SCOPE_LOCAL_CPU)
  727. return all_safe;
  728. if (is_midr_in_range_list(read_cpuid_id(), spectre_bhb_safe_list))
  729. return true;
  730. all_safe = false;
  731. return false;
  732. }
  733. static u8 spectre_bhb_loop_affected(void)
  734. {
  735. u8 k = 0;
  736. static const struct midr_range spectre_bhb_k132_list[] = {
  737. MIDR_ALL_VERSIONS(MIDR_CORTEX_X3),
  738. MIDR_ALL_VERSIONS(MIDR_NEOVERSE_V2),
  739. {},
  740. };
  741. static const struct midr_range spectre_bhb_k38_list[] = {
  742. MIDR_ALL_VERSIONS(MIDR_CORTEX_A715),
  743. MIDR_ALL_VERSIONS(MIDR_CORTEX_A720),
  744. {},
  745. };
  746. static const struct midr_range spectre_bhb_k32_list[] = {
  747. MIDR_ALL_VERSIONS(MIDR_CORTEX_A78),
  748. MIDR_ALL_VERSIONS(MIDR_CORTEX_A78AE),
  749. MIDR_ALL_VERSIONS(MIDR_CORTEX_A78C),
  750. MIDR_ALL_VERSIONS(MIDR_CORTEX_X1),
  751. MIDR_ALL_VERSIONS(MIDR_CORTEX_X1C),
  752. MIDR_ALL_VERSIONS(MIDR_CORTEX_A710),
  753. MIDR_ALL_VERSIONS(MIDR_CORTEX_X2),
  754. MIDR_ALL_VERSIONS(MIDR_NEOVERSE_N2),
  755. MIDR_ALL_VERSIONS(MIDR_NEOVERSE_V1),
  756. {},
  757. };
  758. static const struct midr_range spectre_bhb_k24_list[] = {
  759. MIDR_ALL_VERSIONS(MIDR_CORTEX_A76),
  760. MIDR_ALL_VERSIONS(MIDR_CORTEX_A76AE),
  761. MIDR_ALL_VERSIONS(MIDR_CORTEX_A77),
  762. MIDR_ALL_VERSIONS(MIDR_NEOVERSE_N1),
  763. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_4XX_GOLD),
  764. MIDR_ALL_VERSIONS(MIDR_HISI_HIP09),
  765. {},
  766. };
  767. static const struct midr_range spectre_bhb_k11_list[] = {
  768. MIDR_ALL_VERSIONS(MIDR_AMPERE1),
  769. {},
  770. };
  771. static const struct midr_range spectre_bhb_k8_list[] = {
  772. MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
  773. MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
  774. {},
  775. };
  776. if (is_midr_in_range_list(read_cpuid_id(), spectre_bhb_k132_list))
  777. k = 132;
  778. else if (is_midr_in_range_list(read_cpuid_id(), spectre_bhb_k38_list))
  779. k = 38;
  780. else if (is_midr_in_range_list(read_cpuid_id(), spectre_bhb_k32_list))
  781. k = 32;
  782. else if (is_midr_in_range_list(read_cpuid_id(), spectre_bhb_k24_list))
  783. k = 24;
  784. else if (is_midr_in_range_list(read_cpuid_id(), spectre_bhb_k11_list))
  785. k = 11;
  786. else if (is_midr_in_range_list(read_cpuid_id(), spectre_bhb_k8_list))
  787. k = 8;
  788. return k;
  789. }
  790. static enum mitigation_state spectre_bhb_get_cpu_fw_mitigation_state(void)
  791. {
  792. int ret;
  793. struct arm_smccc_res res;
  794. arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  795. ARM_SMCCC_ARCH_WORKAROUND_3, &res);
  796. ret = res.a0;
  797. switch (ret) {
  798. case SMCCC_RET_SUCCESS:
  799. return SPECTRE_MITIGATED;
  800. case SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED:
  801. return SPECTRE_UNAFFECTED;
  802. default:
  803. fallthrough;
  804. case SMCCC_RET_NOT_SUPPORTED:
  805. return SPECTRE_VULNERABLE;
  806. }
  807. }
  808. static bool has_spectre_bhb_fw_mitigation(void)
  809. {
  810. enum mitigation_state fw_state;
  811. bool has_smccc = arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_NONE;
  812. fw_state = spectre_bhb_get_cpu_fw_mitigation_state();
  813. return has_smccc && fw_state == SPECTRE_MITIGATED;
  814. }
  815. static bool supports_ecbhb(int scope)
  816. {
  817. u64 mmfr1;
  818. if (scope == SCOPE_LOCAL_CPU)
  819. mmfr1 = read_sysreg_s(SYS_ID_AA64MMFR1_EL1);
  820. else
  821. mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
  822. return cpuid_feature_extract_unsigned_field(mmfr1,
  823. ID_AA64MMFR1_EL1_ECBHB_SHIFT);
  824. }
  825. static u8 max_bhb_k;
  826. bool is_spectre_bhb_affected(const struct arm64_cpu_capabilities *entry,
  827. int scope)
  828. {
  829. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  830. if (supports_csv2p3(scope))
  831. return false;
  832. if (is_spectre_bhb_safe(scope))
  833. return false;
  834. /*
  835. * At this point the core isn't known to be "safe" so we're going to
  836. * assume it's vulnerable. We still need to update `max_bhb_k` though,
  837. * but only if we aren't mitigating with clearbhb though.
  838. */
  839. if (scope == SCOPE_LOCAL_CPU && !supports_clearbhb(SCOPE_LOCAL_CPU))
  840. max_bhb_k = max(max_bhb_k, spectre_bhb_loop_affected());
  841. return true;
  842. }
  843. u8 get_spectre_bhb_loop_value(void)
  844. {
  845. return max_bhb_k;
  846. }
  847. static void this_cpu_set_vectors(enum arm64_bp_harden_el1_vectors slot)
  848. {
  849. const char *v = arm64_get_bp_hardening_vector(slot);
  850. __this_cpu_write(this_cpu_vector, v);
  851. /*
  852. * When KPTI is in use, the vectors are switched when exiting to
  853. * user-space.
  854. */
  855. if (cpus_have_cap(ARM64_UNMAP_KERNEL_AT_EL0))
  856. return;
  857. write_sysreg(v, vbar_el1);
  858. isb();
  859. }
  860. bool __read_mostly __nospectre_bhb;
  861. static int __init parse_spectre_bhb_param(char *str)
  862. {
  863. __nospectre_bhb = true;
  864. return 0;
  865. }
  866. early_param("nospectre_bhb", parse_spectre_bhb_param);
  867. void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *entry)
  868. {
  869. bp_hardening_cb_t cpu_cb;
  870. enum mitigation_state state = SPECTRE_VULNERABLE;
  871. struct bp_hardening_data *data = this_cpu_ptr(&bp_hardening_data);
  872. if (!is_spectre_bhb_affected(entry, SCOPE_LOCAL_CPU))
  873. return;
  874. if (arm64_get_spectre_v2_state() == SPECTRE_VULNERABLE) {
  875. /* No point mitigating Spectre-BHB alone. */
  876. } else if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY)) {
  877. pr_info_once("spectre-bhb mitigation disabled by compile time option\n");
  878. } else if (cpu_mitigations_off() || __nospectre_bhb) {
  879. pr_info_once("spectre-bhb mitigation disabled by command line option\n");
  880. } else if (supports_ecbhb(SCOPE_LOCAL_CPU)) {
  881. state = SPECTRE_MITIGATED;
  882. set_bit(BHB_HW, &system_bhb_mitigations);
  883. } else if (supports_clearbhb(SCOPE_LOCAL_CPU)) {
  884. /*
  885. * Ensure KVM uses the indirect vector which will have ClearBHB
  886. * added.
  887. */
  888. if (!data->slot)
  889. data->slot = HYP_VECTOR_INDIRECT;
  890. this_cpu_set_vectors(EL1_VECTOR_BHB_CLEAR_INSN);
  891. state = SPECTRE_MITIGATED;
  892. set_bit(BHB_INSN, &system_bhb_mitigations);
  893. } else if (spectre_bhb_loop_affected()) {
  894. /*
  895. * Ensure KVM uses the indirect vector which will have the
  896. * branchy-loop added. A57/A72-r0 will already have selected
  897. * the spectre-indirect vector, which is sufficient for BHB
  898. * too.
  899. */
  900. if (!data->slot)
  901. data->slot = HYP_VECTOR_INDIRECT;
  902. this_cpu_set_vectors(EL1_VECTOR_BHB_LOOP);
  903. state = SPECTRE_MITIGATED;
  904. set_bit(BHB_LOOP, &system_bhb_mitigations);
  905. } else if (has_spectre_bhb_fw_mitigation()) {
  906. /*
  907. * Ensure KVM uses one of the spectre bp_hardening
  908. * vectors. The indirect vector doesn't include the EL3
  909. * call, so needs upgrading to
  910. * HYP_VECTOR_SPECTRE_INDIRECT.
  911. */
  912. if (!data->slot || data->slot == HYP_VECTOR_INDIRECT)
  913. data->slot += 1;
  914. this_cpu_set_vectors(EL1_VECTOR_BHB_FW);
  915. /*
  916. * The WA3 call in the vectors supersedes the WA1 call
  917. * made during context-switch. Uninstall any firmware
  918. * bp_hardening callback.
  919. */
  920. cpu_cb = spectre_v2_get_sw_mitigation_cb();
  921. if (__this_cpu_read(bp_hardening_data.fn) != cpu_cb)
  922. __this_cpu_write(bp_hardening_data.fn, NULL);
  923. state = SPECTRE_MITIGATED;
  924. set_bit(BHB_FW, &system_bhb_mitigations);
  925. }
  926. update_mitigation_state(&spectre_bhb_state, state);
  927. }
  928. bool is_spectre_bhb_fw_mitigated(void)
  929. {
  930. return test_bit(BHB_FW, &system_bhb_mitigations);
  931. }
  932. /* Patched to NOP when enabled */
  933. void noinstr spectre_bhb_patch_loop_mitigation_enable(struct alt_instr *alt,
  934. __le32 *origptr,
  935. __le32 *updptr, int nr_inst)
  936. {
  937. BUG_ON(nr_inst != 1);
  938. if (test_bit(BHB_LOOP, &system_bhb_mitigations))
  939. *updptr++ = cpu_to_le32(aarch64_insn_gen_nop());
  940. }
  941. /* Patched to NOP when enabled */
  942. void noinstr spectre_bhb_patch_fw_mitigation_enabled(struct alt_instr *alt,
  943. __le32 *origptr,
  944. __le32 *updptr, int nr_inst)
  945. {
  946. BUG_ON(nr_inst != 1);
  947. if (test_bit(BHB_FW, &system_bhb_mitigations))
  948. *updptr++ = cpu_to_le32(aarch64_insn_gen_nop());
  949. }
  950. /* Patched to correct the immediate */
  951. void noinstr spectre_bhb_patch_loop_iter(struct alt_instr *alt,
  952. __le32 *origptr, __le32 *updptr, int nr_inst)
  953. {
  954. u8 rd;
  955. u32 insn;
  956. BUG_ON(nr_inst != 1); /* MOV -> MOV */
  957. if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY))
  958. return;
  959. insn = le32_to_cpu(*origptr);
  960. rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD, insn);
  961. insn = aarch64_insn_gen_movewide(rd, max_bhb_k, 0,
  962. AARCH64_INSN_VARIANT_64BIT,
  963. AARCH64_INSN_MOVEWIDE_ZERO);
  964. *updptr++ = cpu_to_le32(insn);
  965. }
  966. /* Patched to mov WA3 when supported */
  967. void noinstr spectre_bhb_patch_wa3(struct alt_instr *alt,
  968. __le32 *origptr, __le32 *updptr, int nr_inst)
  969. {
  970. u8 rd;
  971. u32 insn;
  972. BUG_ON(nr_inst != 1); /* MOV -> MOV */
  973. if (!IS_ENABLED(CONFIG_MITIGATE_SPECTRE_BRANCH_HISTORY) ||
  974. !test_bit(BHB_FW, &system_bhb_mitigations))
  975. return;
  976. insn = le32_to_cpu(*origptr);
  977. rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD, insn);
  978. insn = aarch64_insn_gen_logical_immediate(AARCH64_INSN_LOGIC_ORR,
  979. AARCH64_INSN_VARIANT_32BIT,
  980. AARCH64_INSN_REG_ZR, rd,
  981. ARM_SMCCC_ARCH_WORKAROUND_3);
  982. if (WARN_ON_ONCE(insn == AARCH64_BREAK_FAULT))
  983. return;
  984. *updptr++ = cpu_to_le32(insn);
  985. }
  986. /* Patched to NOP when not supported */
  987. void __init spectre_bhb_patch_clearbhb(struct alt_instr *alt,
  988. __le32 *origptr, __le32 *updptr, int nr_inst)
  989. {
  990. BUG_ON(nr_inst != 2);
  991. if (test_bit(BHB_INSN, &system_bhb_mitigations))
  992. return;
  993. *updptr++ = cpu_to_le32(aarch64_insn_gen_nop());
  994. *updptr++ = cpu_to_le32(aarch64_insn_gen_nop());
  995. }
  996. #ifdef CONFIG_BPF_SYSCALL
  997. #define EBPF_WARN "Unprivileged eBPF is enabled, data leaks possible via Spectre v2 BHB attacks!\n"
  998. void unpriv_ebpf_notify(int new_state)
  999. {
  1000. if (spectre_v2_state == SPECTRE_VULNERABLE ||
  1001. spectre_bhb_state != SPECTRE_MITIGATED)
  1002. return;
  1003. if (!new_state)
  1004. pr_err("WARNING: %s", EBPF_WARN);
  1005. }
  1006. #endif