proc-v7-bugs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. if (processor.switch_mm != cpu_v7_bpiall_switch_mm)
  46. goto bl_error;
  47. per_cpu(harden_branch_predictor_fn, cpu) =
  48. harden_branch_predictor_bpiall;
  49. spectre_v2_method = "BPIALL";
  50. break;
  51. case ARM_CPU_PART_CORTEX_A15:
  52. case ARM_CPU_PART_BRAHMA_B15:
  53. if (processor.switch_mm != cpu_v7_iciallu_switch_mm)
  54. goto bl_error;
  55. per_cpu(harden_branch_predictor_fn, cpu) =
  56. harden_branch_predictor_iciallu;
  57. spectre_v2_method = "ICIALLU";
  58. break;
  59. #ifdef CONFIG_ARM_PSCI
  60. default:
  61. /* Other ARM CPUs require no workaround */
  62. if (read_cpuid_implementor() == ARM_CPU_IMP_ARM)
  63. break;
  64. /* fallthrough */
  65. /* Cortex A57/A72 require firmware workaround */
  66. case ARM_CPU_PART_CORTEX_A57:
  67. case ARM_CPU_PART_CORTEX_A72: {
  68. struct arm_smccc_res res;
  69. if (psci_ops.smccc_version == SMCCC_VERSION_1_0)
  70. break;
  71. switch (psci_ops.conduit) {
  72. case PSCI_CONDUIT_HVC:
  73. arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  74. ARM_SMCCC_ARCH_WORKAROUND_1, &res);
  75. if ((int)res.a0 != 0)
  76. break;
  77. if (processor.switch_mm != cpu_v7_hvc_switch_mm && cpu)
  78. goto bl_error;
  79. per_cpu(harden_branch_predictor_fn, cpu) =
  80. call_hvc_arch_workaround_1;
  81. processor.switch_mm = cpu_v7_hvc_switch_mm;
  82. spectre_v2_method = "hypervisor";
  83. break;
  84. case PSCI_CONDUIT_SMC:
  85. arm_smccc_1_1_smc(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  86. ARM_SMCCC_ARCH_WORKAROUND_1, &res);
  87. if ((int)res.a0 != 0)
  88. break;
  89. if (processor.switch_mm != cpu_v7_smc_switch_mm && cpu)
  90. goto bl_error;
  91. per_cpu(harden_branch_predictor_fn, cpu) =
  92. call_smc_arch_workaround_1;
  93. processor.switch_mm = cpu_v7_smc_switch_mm;
  94. spectre_v2_method = "firmware";
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. #endif
  101. }
  102. if (spectre_v2_method)
  103. pr_info("CPU%u: Spectre v2: using %s workaround\n",
  104. smp_processor_id(), spectre_v2_method);
  105. return;
  106. bl_error:
  107. pr_err("CPU%u: Spectre v2: incorrect context switching function, system vulnerable\n",
  108. cpu);
  109. }
  110. #else
  111. static void cpu_v7_spectre_init(void)
  112. {
  113. }
  114. #endif
  115. static __maybe_unused bool cpu_v7_check_auxcr_set(bool *warned,
  116. u32 mask, const char *msg)
  117. {
  118. u32 aux_cr;
  119. asm("mrc p15, 0, %0, c1, c0, 1" : "=r" (aux_cr));
  120. if ((aux_cr & mask) != mask) {
  121. if (!*warned)
  122. pr_err("CPU%u: %s", smp_processor_id(), msg);
  123. *warned = true;
  124. return false;
  125. }
  126. return true;
  127. }
  128. static DEFINE_PER_CPU(bool, spectre_warned);
  129. static bool check_spectre_auxcr(bool *warned, u32 bit)
  130. {
  131. return IS_ENABLED(CONFIG_HARDEN_BRANCH_PREDICTOR) &&
  132. cpu_v7_check_auxcr_set(warned, bit,
  133. "Spectre v2: firmware did not set auxiliary control register IBE bit, system vulnerable\n");
  134. }
  135. void cpu_v7_ca8_ibe(void)
  136. {
  137. if (check_spectre_auxcr(this_cpu_ptr(&spectre_warned), BIT(6)))
  138. cpu_v7_spectre_init();
  139. }
  140. void cpu_v7_ca15_ibe(void)
  141. {
  142. if (check_spectre_auxcr(this_cpu_ptr(&spectre_warned), BIT(0)))
  143. cpu_v7_spectre_init();
  144. }
  145. void cpu_v7_bugs_init(void)
  146. {
  147. cpu_v7_spectre_init();
  148. }