cpu_errata.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. * Contains CPU specific errata definitions
  3. *
  4. * Copyright (C) 2014 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/arm-smccc.h>
  19. #include <linux/psci.h>
  20. #include <linux/types.h>
  21. #include <linux/cpu.h>
  22. #include <asm/cpu.h>
  23. #include <asm/cputype.h>
  24. #include <asm/cpufeature.h>
  25. #include <asm/smp_plat.h>
  26. static bool __maybe_unused
  27. is_affected_midr_range(const struct arm64_cpu_capabilities *entry, int scope)
  28. {
  29. const struct arm64_midr_revidr *fix;
  30. u32 midr = read_cpuid_id(), revidr;
  31. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  32. if (!is_midr_in_range(midr, &entry->midr_range))
  33. return false;
  34. midr &= MIDR_REVISION_MASK | MIDR_VARIANT_MASK;
  35. revidr = read_cpuid(REVIDR_EL1);
  36. for (fix = entry->fixed_revs; fix && fix->revidr_mask; fix++)
  37. if (midr == fix->midr_rv && (revidr & fix->revidr_mask))
  38. return false;
  39. return true;
  40. }
  41. static bool __maybe_unused
  42. is_affected_midr_range_list(const struct arm64_cpu_capabilities *entry,
  43. int scope)
  44. {
  45. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  46. return is_midr_in_range_list(read_cpuid_id(), entry->midr_range_list);
  47. }
  48. static bool __maybe_unused
  49. is_kryo_midr(const struct arm64_cpu_capabilities *entry, int scope)
  50. {
  51. u32 model;
  52. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  53. model = read_cpuid_id();
  54. model &= MIDR_IMPLEMENTOR_MASK | (0xf00 << MIDR_PARTNUM_SHIFT) |
  55. MIDR_ARCHITECTURE_MASK;
  56. return model == entry->midr_range.model;
  57. }
  58. static bool
  59. has_mismatched_cache_type(const struct arm64_cpu_capabilities *entry,
  60. int scope)
  61. {
  62. u64 mask = CTR_CACHE_MINLINE_MASK;
  63. /* Skip matching the min line sizes for cache type check */
  64. if (entry->capability == ARM64_MISMATCHED_CACHE_TYPE)
  65. mask ^= arm64_ftr_reg_ctrel0.strict_mask;
  66. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  67. return (read_cpuid_cachetype() & mask) !=
  68. (arm64_ftr_reg_ctrel0.sys_val & mask);
  69. }
  70. static void
  71. cpu_enable_trap_ctr_access(const struct arm64_cpu_capabilities *__unused)
  72. {
  73. sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCT, 0);
  74. }
  75. atomic_t arm64_el2_vector_last_slot = ATOMIC_INIT(-1);
  76. #include <asm/mmu_context.h>
  77. #include <asm/cacheflush.h>
  78. DEFINE_PER_CPU_READ_MOSTLY(struct bp_hardening_data, bp_hardening_data);
  79. #ifdef CONFIG_KVM_INDIRECT_VECTORS
  80. extern char __smccc_workaround_1_smc_start[];
  81. extern char __smccc_workaround_1_smc_end[];
  82. static void __copy_hyp_vect_bpi(int slot, const char *hyp_vecs_start,
  83. const char *hyp_vecs_end)
  84. {
  85. void *dst = lm_alias(__bp_harden_hyp_vecs_start + slot * SZ_2K);
  86. int i;
  87. for (i = 0; i < SZ_2K; i += 0x80)
  88. memcpy(dst + i, hyp_vecs_start, hyp_vecs_end - hyp_vecs_start);
  89. __flush_icache_range((uintptr_t)dst, (uintptr_t)dst + SZ_2K);
  90. }
  91. static void install_bp_hardening_cb(bp_hardening_cb_t fn,
  92. const char *hyp_vecs_start,
  93. const char *hyp_vecs_end)
  94. {
  95. static DEFINE_SPINLOCK(bp_lock);
  96. int cpu, slot = -1;
  97. spin_lock(&bp_lock);
  98. for_each_possible_cpu(cpu) {
  99. if (per_cpu(bp_hardening_data.fn, cpu) == fn) {
  100. slot = per_cpu(bp_hardening_data.hyp_vectors_slot, cpu);
  101. break;
  102. }
  103. }
  104. if (slot == -1) {
  105. slot = atomic_inc_return(&arm64_el2_vector_last_slot);
  106. BUG_ON(slot >= BP_HARDEN_EL2_SLOTS);
  107. __copy_hyp_vect_bpi(slot, hyp_vecs_start, hyp_vecs_end);
  108. }
  109. __this_cpu_write(bp_hardening_data.hyp_vectors_slot, slot);
  110. __this_cpu_write(bp_hardening_data.fn, fn);
  111. spin_unlock(&bp_lock);
  112. }
  113. #else
  114. #define __smccc_workaround_1_smc_start NULL
  115. #define __smccc_workaround_1_smc_end NULL
  116. static void install_bp_hardening_cb(bp_hardening_cb_t fn,
  117. const char *hyp_vecs_start,
  118. const char *hyp_vecs_end)
  119. {
  120. __this_cpu_write(bp_hardening_data.fn, fn);
  121. }
  122. #endif /* CONFIG_KVM_INDIRECT_VECTORS */
  123. #include <uapi/linux/psci.h>
  124. #include <linux/arm-smccc.h>
  125. #include <linux/psci.h>
  126. static void call_smc_arch_workaround_1(void)
  127. {
  128. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
  129. }
  130. static void call_hvc_arch_workaround_1(void)
  131. {
  132. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
  133. }
  134. static void qcom_link_stack_sanitization(void)
  135. {
  136. u64 tmp;
  137. asm volatile("mov %0, x30 \n"
  138. ".rept 16 \n"
  139. "bl . + 4 \n"
  140. ".endr \n"
  141. "mov x30, %0 \n"
  142. : "=&r" (tmp));
  143. }
  144. static bool __nospectre_v2;
  145. static int __init parse_nospectre_v2(char *str)
  146. {
  147. __nospectre_v2 = true;
  148. return 0;
  149. }
  150. early_param("nospectre_v2", parse_nospectre_v2);
  151. /*
  152. * -1: No workaround
  153. * 0: No workaround required
  154. * 1: Workaround installed
  155. */
  156. static int detect_harden_bp_fw(void)
  157. {
  158. bp_hardening_cb_t cb;
  159. void *smccc_start, *smccc_end;
  160. struct arm_smccc_res res;
  161. u32 midr = read_cpuid_id();
  162. if (psci_ops.smccc_version == SMCCC_VERSION_1_0)
  163. return -1;
  164. switch (psci_ops.conduit) {
  165. case PSCI_CONDUIT_HVC:
  166. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  167. ARM_SMCCC_ARCH_WORKAROUND_1, &res);
  168. switch ((int)res.a0) {
  169. case 1:
  170. /* Firmware says we're just fine */
  171. return 0;
  172. case 0:
  173. cb = call_hvc_arch_workaround_1;
  174. /* This is a guest, no need to patch KVM vectors */
  175. smccc_start = NULL;
  176. smccc_end = NULL;
  177. break;
  178. default:
  179. return -1;
  180. }
  181. break;
  182. case PSCI_CONDUIT_SMC:
  183. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  184. ARM_SMCCC_ARCH_WORKAROUND_1, &res);
  185. switch ((int)res.a0) {
  186. case 1:
  187. /* Firmware says we're just fine */
  188. return 0;
  189. case 0:
  190. cb = call_smc_arch_workaround_1;
  191. smccc_start = __smccc_workaround_1_smc_start;
  192. smccc_end = __smccc_workaround_1_smc_end;
  193. break;
  194. default:
  195. return -1;
  196. }
  197. break;
  198. default:
  199. return -1;
  200. }
  201. if (((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR) ||
  202. ((midr & MIDR_CPU_MODEL_MASK) == MIDR_QCOM_FALKOR_V1))
  203. cb = qcom_link_stack_sanitization;
  204. if (IS_ENABLED(CONFIG_HARDEN_BRANCH_PREDICTOR))
  205. install_bp_hardening_cb(cb, smccc_start, smccc_end);
  206. return 1;
  207. }
  208. DEFINE_PER_CPU_READ_MOSTLY(u64, arm64_ssbd_callback_required);
  209. int ssbd_state __read_mostly = ARM64_SSBD_KERNEL;
  210. static bool __ssb_safe = true;
  211. static const struct ssbd_options {
  212. const char *str;
  213. int state;
  214. } ssbd_options[] = {
  215. { "force-on", ARM64_SSBD_FORCE_ENABLE, },
  216. { "force-off", ARM64_SSBD_FORCE_DISABLE, },
  217. { "kernel", ARM64_SSBD_KERNEL, },
  218. };
  219. static int __init ssbd_cfg(char *buf)
  220. {
  221. int i;
  222. if (!buf || !buf[0])
  223. return -EINVAL;
  224. for (i = 0; i < ARRAY_SIZE(ssbd_options); i++) {
  225. int len = strlen(ssbd_options[i].str);
  226. if (strncmp(buf, ssbd_options[i].str, len))
  227. continue;
  228. ssbd_state = ssbd_options[i].state;
  229. return 0;
  230. }
  231. return -EINVAL;
  232. }
  233. early_param("ssbd", ssbd_cfg);
  234. void __init arm64_update_smccc_conduit(struct alt_instr *alt,
  235. __le32 *origptr, __le32 *updptr,
  236. int nr_inst)
  237. {
  238. u32 insn;
  239. BUG_ON(nr_inst != 1);
  240. switch (psci_ops.conduit) {
  241. case PSCI_CONDUIT_HVC:
  242. insn = aarch64_insn_get_hvc_value();
  243. break;
  244. case PSCI_CONDUIT_SMC:
  245. insn = aarch64_insn_get_smc_value();
  246. break;
  247. default:
  248. return;
  249. }
  250. *updptr = cpu_to_le32(insn);
  251. }
  252. void __init arm64_enable_wa2_handling(struct alt_instr *alt,
  253. __le32 *origptr, __le32 *updptr,
  254. int nr_inst)
  255. {
  256. BUG_ON(nr_inst != 1);
  257. /*
  258. * Only allow mitigation on EL1 entry/exit and guest
  259. * ARCH_WORKAROUND_2 handling if the SSBD state allows it to
  260. * be flipped.
  261. */
  262. if (arm64_get_ssbd_state() == ARM64_SSBD_KERNEL)
  263. *updptr = cpu_to_le32(aarch64_insn_gen_nop());
  264. }
  265. void arm64_set_ssbd_mitigation(bool state)
  266. {
  267. if (!IS_ENABLED(CONFIG_ARM64_SSBD)) {
  268. pr_info_once("SSBD disabled by kernel configuration\n");
  269. return;
  270. }
  271. if (this_cpu_has_cap(ARM64_SSBS)) {
  272. if (state)
  273. asm volatile(SET_PSTATE_SSBS(0));
  274. else
  275. asm volatile(SET_PSTATE_SSBS(1));
  276. return;
  277. }
  278. switch (psci_ops.conduit) {
  279. case PSCI_CONDUIT_HVC:
  280. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_2, state, NULL);
  281. break;
  282. case PSCI_CONDUIT_SMC:
  283. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, state, NULL);
  284. break;
  285. default:
  286. WARN_ON_ONCE(1);
  287. break;
  288. }
  289. }
  290. static bool has_ssbd_mitigation(const struct arm64_cpu_capabilities *entry,
  291. int scope)
  292. {
  293. struct arm_smccc_res res;
  294. bool required = true;
  295. s32 val;
  296. bool this_cpu_safe = false;
  297. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  298. if (cpu_mitigations_off())
  299. ssbd_state = ARM64_SSBD_FORCE_DISABLE;
  300. /* delay setting __ssb_safe until we get a firmware response */
  301. if (is_midr_in_range_list(read_cpuid_id(), entry->midr_range_list))
  302. this_cpu_safe = true;
  303. if (this_cpu_has_cap(ARM64_SSBS)) {
  304. if (!this_cpu_safe)
  305. __ssb_safe = false;
  306. required = false;
  307. goto out_printmsg;
  308. }
  309. if (psci_ops.smccc_version == SMCCC_VERSION_1_0) {
  310. ssbd_state = ARM64_SSBD_UNKNOWN;
  311. if (!this_cpu_safe)
  312. __ssb_safe = false;
  313. return false;
  314. }
  315. switch (psci_ops.conduit) {
  316. case PSCI_CONDUIT_HVC:
  317. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  318. ARM_SMCCC_ARCH_WORKAROUND_2, &res);
  319. break;
  320. case PSCI_CONDUIT_SMC:
  321. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  322. ARM_SMCCC_ARCH_WORKAROUND_2, &res);
  323. break;
  324. default:
  325. ssbd_state = ARM64_SSBD_UNKNOWN;
  326. if (!this_cpu_safe)
  327. __ssb_safe = false;
  328. return false;
  329. }
  330. val = (s32)res.a0;
  331. switch (val) {
  332. case SMCCC_RET_NOT_SUPPORTED:
  333. ssbd_state = ARM64_SSBD_UNKNOWN;
  334. if (!this_cpu_safe)
  335. __ssb_safe = false;
  336. return false;
  337. /* machines with mixed mitigation requirements must not return this */
  338. case SMCCC_RET_NOT_REQUIRED:
  339. pr_info_once("%s mitigation not required\n", entry->desc);
  340. ssbd_state = ARM64_SSBD_MITIGATED;
  341. return false;
  342. case SMCCC_RET_SUCCESS:
  343. __ssb_safe = false;
  344. required = true;
  345. break;
  346. case 1: /* Mitigation not required on this CPU */
  347. required = false;
  348. break;
  349. default:
  350. WARN_ON(1);
  351. if (!this_cpu_safe)
  352. __ssb_safe = false;
  353. return false;
  354. }
  355. switch (ssbd_state) {
  356. case ARM64_SSBD_FORCE_DISABLE:
  357. arm64_set_ssbd_mitigation(false);
  358. required = false;
  359. break;
  360. case ARM64_SSBD_KERNEL:
  361. if (required) {
  362. __this_cpu_write(arm64_ssbd_callback_required, 1);
  363. arm64_set_ssbd_mitigation(true);
  364. }
  365. break;
  366. case ARM64_SSBD_FORCE_ENABLE:
  367. arm64_set_ssbd_mitigation(true);
  368. required = true;
  369. break;
  370. default:
  371. WARN_ON(1);
  372. break;
  373. }
  374. out_printmsg:
  375. switch (ssbd_state) {
  376. case ARM64_SSBD_FORCE_DISABLE:
  377. pr_info_once("%s disabled from command-line\n", entry->desc);
  378. break;
  379. case ARM64_SSBD_FORCE_ENABLE:
  380. pr_info_once("%s forced from command-line\n", entry->desc);
  381. break;
  382. }
  383. return required;
  384. }
  385. /* known invulnerable cores */
  386. static const struct midr_range arm64_ssb_cpus[] = {
  387. MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
  388. MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
  389. MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
  390. {},
  391. };
  392. #ifdef CONFIG_ARM64_ERRATUM_1463225
  393. DEFINE_PER_CPU(int, __in_cortex_a76_erratum_1463225_wa);
  394. static bool
  395. has_cortex_a76_erratum_1463225(const struct arm64_cpu_capabilities *entry,
  396. int scope)
  397. {
  398. u32 midr = read_cpuid_id();
  399. /* Cortex-A76 r0p0 - r3p1 */
  400. struct midr_range range = MIDR_RANGE(MIDR_CORTEX_A76, 0, 0, 3, 1);
  401. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  402. return is_midr_in_range(midr, &range) && is_kernel_in_hyp_mode();
  403. }
  404. #endif
  405. #define CAP_MIDR_RANGE(model, v_min, r_min, v_max, r_max) \
  406. .matches = is_affected_midr_range, \
  407. .midr_range = MIDR_RANGE(model, v_min, r_min, v_max, r_max)
  408. #define CAP_MIDR_ALL_VERSIONS(model) \
  409. .matches = is_affected_midr_range, \
  410. .midr_range = MIDR_ALL_VERSIONS(model)
  411. #define MIDR_FIXED(rev, revidr_mask) \
  412. .fixed_revs = (struct arm64_midr_revidr[]){{ (rev), (revidr_mask) }, {}}
  413. #define ERRATA_MIDR_RANGE(model, v_min, r_min, v_max, r_max) \
  414. .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
  415. CAP_MIDR_RANGE(model, v_min, r_min, v_max, r_max)
  416. #define CAP_MIDR_RANGE_LIST(list) \
  417. .matches = is_affected_midr_range_list, \
  418. .midr_range_list = list
  419. /* Errata affecting a range of revisions of given model variant */
  420. #define ERRATA_MIDR_REV_RANGE(m, var, r_min, r_max) \
  421. ERRATA_MIDR_RANGE(m, var, r_min, var, r_max)
  422. /* Errata affecting a single variant/revision of a model */
  423. #define ERRATA_MIDR_REV(model, var, rev) \
  424. ERRATA_MIDR_RANGE(model, var, rev, var, rev)
  425. /* Errata affecting all variants/revisions of a given a model */
  426. #define ERRATA_MIDR_ALL_VERSIONS(model) \
  427. .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
  428. CAP_MIDR_ALL_VERSIONS(model)
  429. /* Errata affecting a list of midr ranges, with same work around */
  430. #define ERRATA_MIDR_RANGE_LIST(midr_list) \
  431. .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, \
  432. CAP_MIDR_RANGE_LIST(midr_list)
  433. /* Track overall mitigation state. We are only mitigated if all cores are ok */
  434. static bool __hardenbp_enab = true;
  435. static bool __spectrev2_safe = true;
  436. /*
  437. * Generic helper for handling capabilties with multiple (match,enable) pairs
  438. * of call backs, sharing the same capability bit.
  439. * Iterate over each entry to see if at least one matches.
  440. */
  441. static bool __maybe_unused
  442. multi_entry_cap_matches(const struct arm64_cpu_capabilities *entry, int scope)
  443. {
  444. const struct arm64_cpu_capabilities *caps;
  445. for (caps = entry->match_list; caps->matches; caps++)
  446. if (caps->matches(caps, scope))
  447. return true;
  448. return false;
  449. }
  450. /*
  451. * Take appropriate action for all matching entries in the shared capability
  452. * entry.
  453. */
  454. static void __maybe_unused
  455. multi_entry_cap_cpu_enable(const struct arm64_cpu_capabilities *entry)
  456. {
  457. const struct arm64_cpu_capabilities *caps;
  458. for (caps = entry->match_list; caps->matches; caps++)
  459. if (caps->matches(caps, SCOPE_LOCAL_CPU) &&
  460. caps->cpu_enable)
  461. caps->cpu_enable(caps);
  462. }
  463. /*
  464. * List of CPUs that do not need any Spectre-v2 mitigation at all.
  465. */
  466. static const struct midr_range spectre_v2_safe_list[] = {
  467. MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
  468. MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
  469. MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
  470. { /* sentinel */ }
  471. };
  472. /*
  473. * Track overall bp hardening for all heterogeneous cores in the machine.
  474. * We are only considered "safe" if all booted cores are known safe.
  475. */
  476. static bool __maybe_unused
  477. check_branch_predictor(const struct arm64_cpu_capabilities *entry, int scope)
  478. {
  479. int need_wa;
  480. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  481. /* If the CPU has CSV2 set, we're safe */
  482. if (cpuid_feature_extract_unsigned_field(read_cpuid(ID_AA64PFR0_EL1),
  483. ID_AA64PFR0_CSV2_SHIFT))
  484. return false;
  485. /* Alternatively, we have a list of unaffected CPUs */
  486. if (is_midr_in_range_list(read_cpuid_id(), spectre_v2_safe_list))
  487. return false;
  488. /* Fallback to firmware detection */
  489. need_wa = detect_harden_bp_fw();
  490. if (!need_wa)
  491. return false;
  492. __spectrev2_safe = false;
  493. if (!IS_ENABLED(CONFIG_HARDEN_BRANCH_PREDICTOR)) {
  494. pr_warn_once("spectrev2 mitigation disabled by kernel configuration\n");
  495. __hardenbp_enab = false;
  496. return false;
  497. }
  498. /* forced off */
  499. if (__nospectre_v2 || cpu_mitigations_off()) {
  500. pr_info_once("spectrev2 mitigation disabled by command line option\n");
  501. __hardenbp_enab = false;
  502. return false;
  503. }
  504. if (need_wa < 0) {
  505. pr_warn_once("ARM_SMCCC_ARCH_WORKAROUND_1 missing from firmware\n");
  506. __hardenbp_enab = false;
  507. }
  508. return (need_wa > 0);
  509. }
  510. static void
  511. cpu_enable_branch_predictor_hardening(const struct arm64_cpu_capabilities *cap)
  512. {
  513. cap->matches(cap, SCOPE_LOCAL_CPU);
  514. }
  515. static const __maybe_unused struct midr_range tx2_family_cpus[] = {
  516. MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
  517. MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
  518. {},
  519. };
  520. static bool __maybe_unused
  521. needs_tx2_tvm_workaround(const struct arm64_cpu_capabilities *entry,
  522. int scope)
  523. {
  524. int i;
  525. if (!is_affected_midr_range_list(entry, scope) ||
  526. !is_hyp_mode_available())
  527. return false;
  528. for_each_possible_cpu(i) {
  529. if (MPIDR_AFFINITY_LEVEL(cpu_logical_map(i), 0) != 0)
  530. return true;
  531. }
  532. return false;
  533. }
  534. static bool __maybe_unused
  535. has_neoverse_n1_erratum_1542419(const struct arm64_cpu_capabilities *entry,
  536. int scope)
  537. {
  538. u32 midr = read_cpuid_id();
  539. bool has_dic = read_cpuid_cachetype() & BIT(CTR_DIC_SHIFT);
  540. const struct midr_range range = MIDR_ALL_VERSIONS(MIDR_NEOVERSE_N1);
  541. WARN_ON(scope != SCOPE_LOCAL_CPU || preemptible());
  542. return is_midr_in_range(midr, &range) && has_dic;
  543. }
  544. #ifdef CONFIG_HARDEN_EL2_VECTORS
  545. static const struct midr_range arm64_harden_el2_vectors[] = {
  546. MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
  547. MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
  548. {},
  549. };
  550. #endif
  551. const struct arm64_cpu_capabilities arm64_errata[] = {
  552. #if defined(CONFIG_ARM64_ERRATUM_826319) || \
  553. defined(CONFIG_ARM64_ERRATUM_827319) || \
  554. defined(CONFIG_ARM64_ERRATUM_824069)
  555. {
  556. /* Cortex-A53 r0p[012] */
  557. .desc = "ARM errata 826319, 827319, 824069",
  558. .capability = ARM64_WORKAROUND_CLEAN_CACHE,
  559. ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 2),
  560. .cpu_enable = cpu_enable_cache_maint_trap,
  561. },
  562. #endif
  563. #ifdef CONFIG_ARM64_ERRATUM_819472
  564. {
  565. /* Cortex-A53 r0p[01] */
  566. .desc = "ARM errata 819472",
  567. .capability = ARM64_WORKAROUND_CLEAN_CACHE,
  568. ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 1),
  569. .cpu_enable = cpu_enable_cache_maint_trap,
  570. },
  571. #endif
  572. #ifdef CONFIG_ARM64_ERRATUM_832075
  573. {
  574. /* Cortex-A57 r0p0 - r1p2 */
  575. .desc = "ARM erratum 832075",
  576. .capability = ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE,
  577. ERRATA_MIDR_RANGE(MIDR_CORTEX_A57,
  578. 0, 0,
  579. 1, 2),
  580. },
  581. #endif
  582. #ifdef CONFIG_ARM64_ERRATUM_834220
  583. {
  584. /* Cortex-A57 r0p0 - r1p2 */
  585. .desc = "ARM erratum 834220",
  586. .capability = ARM64_WORKAROUND_834220,
  587. ERRATA_MIDR_RANGE(MIDR_CORTEX_A57,
  588. 0, 0,
  589. 1, 2),
  590. },
  591. #endif
  592. #ifdef CONFIG_ARM64_ERRATUM_843419
  593. {
  594. /* Cortex-A53 r0p[01234] */
  595. .desc = "ARM erratum 843419",
  596. .capability = ARM64_WORKAROUND_843419,
  597. ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 4),
  598. MIDR_FIXED(0x4, BIT(8)),
  599. },
  600. #endif
  601. #ifdef CONFIG_ARM64_ERRATUM_845719
  602. {
  603. /* Cortex-A53 r0p[01234] */
  604. .desc = "ARM erratum 845719",
  605. .capability = ARM64_WORKAROUND_845719,
  606. ERRATA_MIDR_REV_RANGE(MIDR_CORTEX_A53, 0, 0, 4),
  607. },
  608. #endif
  609. #ifdef CONFIG_CAVIUM_ERRATUM_23154
  610. {
  611. /* Cavium ThunderX, pass 1.x */
  612. .desc = "Cavium erratum 23154",
  613. .capability = ARM64_WORKAROUND_CAVIUM_23154,
  614. ERRATA_MIDR_REV_RANGE(MIDR_THUNDERX, 0, 0, 1),
  615. },
  616. #endif
  617. #ifdef CONFIG_CAVIUM_ERRATUM_27456
  618. {
  619. /* Cavium ThunderX, T88 pass 1.x - 2.1 */
  620. .desc = "Cavium erratum 27456",
  621. .capability = ARM64_WORKAROUND_CAVIUM_27456,
  622. ERRATA_MIDR_RANGE(MIDR_THUNDERX,
  623. 0, 0,
  624. 1, 1),
  625. },
  626. {
  627. /* Cavium ThunderX, T81 pass 1.0 */
  628. .desc = "Cavium erratum 27456",
  629. .capability = ARM64_WORKAROUND_CAVIUM_27456,
  630. ERRATA_MIDR_REV(MIDR_THUNDERX_81XX, 0, 0),
  631. },
  632. #endif
  633. #ifdef CONFIG_CAVIUM_ERRATUM_30115
  634. {
  635. /* Cavium ThunderX, T88 pass 1.x - 2.2 */
  636. .desc = "Cavium erratum 30115",
  637. .capability = ARM64_WORKAROUND_CAVIUM_30115,
  638. ERRATA_MIDR_RANGE(MIDR_THUNDERX,
  639. 0, 0,
  640. 1, 2),
  641. },
  642. {
  643. /* Cavium ThunderX, T81 pass 1.0 - 1.2 */
  644. .desc = "Cavium erratum 30115",
  645. .capability = ARM64_WORKAROUND_CAVIUM_30115,
  646. ERRATA_MIDR_REV_RANGE(MIDR_THUNDERX_81XX, 0, 0, 2),
  647. },
  648. {
  649. /* Cavium ThunderX, T83 pass 1.0 */
  650. .desc = "Cavium erratum 30115",
  651. .capability = ARM64_WORKAROUND_CAVIUM_30115,
  652. ERRATA_MIDR_REV(MIDR_THUNDERX_83XX, 0, 0),
  653. },
  654. #endif
  655. {
  656. .desc = "Mismatched cache line size",
  657. .capability = ARM64_MISMATCHED_CACHE_LINE_SIZE,
  658. .matches = has_mismatched_cache_type,
  659. .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
  660. .cpu_enable = cpu_enable_trap_ctr_access,
  661. },
  662. {
  663. .desc = "Mismatched cache type",
  664. .capability = ARM64_MISMATCHED_CACHE_TYPE,
  665. .matches = has_mismatched_cache_type,
  666. .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
  667. .cpu_enable = cpu_enable_trap_ctr_access,
  668. },
  669. #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1003
  670. {
  671. .desc = "Qualcomm Technologies Falkor erratum 1003",
  672. .capability = ARM64_WORKAROUND_QCOM_FALKOR_E1003,
  673. ERRATA_MIDR_REV(MIDR_QCOM_FALKOR_V1, 0, 0),
  674. },
  675. {
  676. .desc = "Qualcomm Technologies Kryo erratum 1003",
  677. .capability = ARM64_WORKAROUND_QCOM_FALKOR_E1003,
  678. .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
  679. .midr_range.model = MIDR_QCOM_KRYO,
  680. .matches = is_kryo_midr,
  681. },
  682. #endif
  683. #ifdef CONFIG_QCOM_FALKOR_ERRATUM_1009
  684. {
  685. .desc = "Qualcomm Technologies Falkor erratum 1009",
  686. .capability = ARM64_WORKAROUND_REPEAT_TLBI,
  687. ERRATA_MIDR_REV(MIDR_QCOM_FALKOR_V1, 0, 0),
  688. },
  689. #endif
  690. #ifdef CONFIG_ARM64_ERRATUM_858921
  691. {
  692. /* Cortex-A73 all versions */
  693. .desc = "ARM erratum 858921",
  694. .capability = ARM64_WORKAROUND_858921,
  695. ERRATA_MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
  696. },
  697. #endif
  698. {
  699. .desc = "Branch predictor hardening",
  700. .capability = ARM64_HARDEN_BRANCH_PREDICTOR,
  701. .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
  702. .matches = check_branch_predictor,
  703. .cpu_enable = cpu_enable_branch_predictor_hardening,
  704. },
  705. #ifdef CONFIG_HARDEN_EL2_VECTORS
  706. {
  707. .desc = "EL2 vector hardening",
  708. .capability = ARM64_HARDEN_EL2_VECTORS,
  709. ERRATA_MIDR_RANGE_LIST(arm64_harden_el2_vectors),
  710. },
  711. #endif
  712. {
  713. .desc = "Speculative Store Bypass Disable",
  714. .capability = ARM64_SSBD,
  715. .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
  716. .matches = has_ssbd_mitigation,
  717. .midr_range_list = arm64_ssb_cpus,
  718. },
  719. #ifdef CONFIG_ARM64_ERRATUM_1463225
  720. {
  721. .desc = "ARM erratum 1463225",
  722. .capability = ARM64_WORKAROUND_1463225,
  723. .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
  724. .matches = has_cortex_a76_erratum_1463225,
  725. },
  726. #endif
  727. #ifdef CONFIG_CAVIUM_TX2_ERRATUM_219
  728. {
  729. .desc = "Cavium ThunderX2 erratum 219 (KVM guest sysreg trapping)",
  730. .capability = ARM64_WORKAROUND_CAVIUM_TX2_219_TVM,
  731. ERRATA_MIDR_RANGE_LIST(tx2_family_cpus),
  732. .matches = needs_tx2_tvm_workaround,
  733. },
  734. #endif
  735. #ifdef CONFIG_ARM64_ERRATUM_1542419
  736. {
  737. /* we depend on the firmware portion for correctness */
  738. .desc = "ARM erratum 1542419 (kernel portion)",
  739. .capability = ARM64_WORKAROUND_1542419,
  740. .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM,
  741. .matches = has_neoverse_n1_erratum_1542419,
  742. .cpu_enable = cpu_enable_trap_ctr_access,
  743. },
  744. #endif
  745. {
  746. }
  747. };
  748. ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr,
  749. char *buf)
  750. {
  751. return sprintf(buf, "Mitigation: __user pointer sanitization\n");
  752. }
  753. ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr,
  754. char *buf)
  755. {
  756. if (__spectrev2_safe)
  757. return sprintf(buf, "Not affected\n");
  758. if (__hardenbp_enab)
  759. return sprintf(buf, "Mitigation: Branch predictor hardening\n");
  760. return sprintf(buf, "Vulnerable\n");
  761. }
  762. ssize_t cpu_show_spec_store_bypass(struct device *dev,
  763. struct device_attribute *attr, char *buf)
  764. {
  765. if (__ssb_safe)
  766. return sprintf(buf, "Not affected\n");
  767. switch (ssbd_state) {
  768. case ARM64_SSBD_KERNEL:
  769. case ARM64_SSBD_FORCE_ENABLE:
  770. if (IS_ENABLED(CONFIG_ARM64_SSBD))
  771. return sprintf(buf,
  772. "Mitigation: Speculative Store Bypass disabled via prctl\n");
  773. }
  774. return sprintf(buf, "Vulnerable\n");
  775. }