proc-v7-bugs.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/arm-smccc.h>
  3. #include <linux/kernel.h>
  4. #include <linux/psci.h>
  5. #include <linux/smp.h>
  6. #include <asm/cp15.h>
  7. #include <asm/cputype.h>
  8. #include <asm/proc-fns.h>
  9. #include <asm/system_misc.h>
  10. #ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
  11. DEFINE_PER_CPU(harden_branch_predictor_fn_t, harden_branch_predictor_fn);
  12. extern void cpu_v7_iciallu_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
  13. extern void cpu_v7_bpiall_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
  14. extern void cpu_v7_smc_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
  15. extern void cpu_v7_hvc_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
  16. static void harden_branch_predictor_bpiall(void)
  17. {
  18. write_sysreg(0, BPIALL);
  19. }
  20. static void harden_branch_predictor_iciallu(void)
  21. {
  22. write_sysreg(0, ICIALLU);
  23. }
  24. static void __maybe_unused call_smc_arch_workaround_1(void)
  25. {
  26. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
  27. }
  28. static void __maybe_unused call_hvc_arch_workaround_1(void)
  29. {
  30. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
  31. }
  32. static void cpu_v7_spectre_init(void)
  33. {
  34. const char *spectre_v2_method = NULL;
  35. int cpu = smp_processor_id();
  36. if (per_cpu(harden_branch_predictor_fn, cpu))
  37. return;
  38. switch (read_cpuid_part()) {
  39. case ARM_CPU_PART_CORTEX_A8:
  40. case ARM_CPU_PART_CORTEX_A9:
  41. case ARM_CPU_PART_CORTEX_A12:
  42. case ARM_CPU_PART_CORTEX_A17:
  43. case ARM_CPU_PART_CORTEX_A73:
  44. case ARM_CPU_PART_CORTEX_A75:
  45. per_cpu(harden_branch_predictor_fn, cpu) =
  46. harden_branch_predictor_bpiall;
  47. spectre_v2_method = "BPIALL";
  48. break;
  49. case ARM_CPU_PART_CORTEX_A15:
  50. case ARM_CPU_PART_BRAHMA_B15:
  51. per_cpu(harden_branch_predictor_fn, cpu) =
  52. harden_branch_predictor_iciallu;
  53. spectre_v2_method = "ICIALLU";
  54. break;
  55. #ifdef CONFIG_ARM_PSCI
  56. case ARM_CPU_PART_BRAHMA_B53:
  57. /* Requires no workaround */
  58. break;
  59. default:
  60. /* Other ARM CPUs require no workaround */
  61. if (read_cpuid_implementor() == ARM_CPU_IMP_ARM)
  62. break;
  63. /* fallthrough */
  64. /* Cortex A57/A72 require firmware workaround */
  65. case ARM_CPU_PART_CORTEX_A57:
  66. case ARM_CPU_PART_CORTEX_A72: {
  67. struct arm_smccc_res res;
  68. if (psci_ops.smccc_version == SMCCC_VERSION_1_0)
  69. break;
  70. switch (psci_ops.conduit) {
  71. case PSCI_CONDUIT_HVC:
  72. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  73. ARM_SMCCC_ARCH_WORKAROUND_1, &res);
  74. if ((int)res.a0 != 0)
  75. break;
  76. per_cpu(harden_branch_predictor_fn, cpu) =
  77. call_hvc_arch_workaround_1;
  78. cpu_do_switch_mm = cpu_v7_hvc_switch_mm;
  79. spectre_v2_method = "hypervisor";
  80. break;
  81. case PSCI_CONDUIT_SMC:
  82. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  83. ARM_SMCCC_ARCH_WORKAROUND_1, &res);
  84. if ((int)res.a0 != 0)
  85. break;
  86. per_cpu(harden_branch_predictor_fn, cpu) =
  87. call_smc_arch_workaround_1;
  88. cpu_do_switch_mm = cpu_v7_smc_switch_mm;
  89. spectre_v2_method = "firmware";
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. #endif
  96. }
  97. if (spectre_v2_method)
  98. pr_info("CPU%u: Spectre v2: using %s workaround\n",
  99. smp_processor_id(), spectre_v2_method);
  100. }
  101. #else
  102. static void cpu_v7_spectre_init(void)
  103. {
  104. }
  105. #endif
  106. static __maybe_unused bool cpu_v7_check_auxcr_set(bool *warned,
  107. u32 mask, const char *msg)
  108. {
  109. u32 aux_cr;
  110. asm("mrc p15, 0, %0, c1, c0, 1" : "=r" (aux_cr));
  111. if ((aux_cr & mask) != mask) {
  112. if (!*warned)
  113. pr_err("CPU%u: %s", smp_processor_id(), msg);
  114. *warned = true;
  115. return false;
  116. }
  117. return true;
  118. }
  119. static DEFINE_PER_CPU(bool, spectre_warned);
  120. static bool check_spectre_auxcr(bool *warned, u32 bit)
  121. {
  122. return IS_ENABLED(CONFIG_HARDEN_BRANCH_PREDICTOR) &&
  123. cpu_v7_check_auxcr_set(warned, bit,
  124. "Spectre v2: firmware did not set auxiliary control register IBE bit, system vulnerable\n");
  125. }
  126. void cpu_v7_ca8_ibe(void)
  127. {
  128. if (check_spectre_auxcr(this_cpu_ptr(&spectre_warned), BIT(6)))
  129. cpu_v7_spectre_init();
  130. }
  131. void cpu_v7_ca15_ibe(void)
  132. {
  133. if (check_spectre_auxcr(this_cpu_ptr(&spectre_warned), BIT(0)))
  134. cpu_v7_spectre_init();
  135. }
  136. void cpu_v7_bugs_init(void)
  137. {
  138. cpu_v7_spectre_init();
  139. }