2018-05-10 20:09:54 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2018-05-14 22:38:55 +08:00
|
|
|
#include <linux/arm-smccc.h>
|
2018-05-10 20:09:54 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/smp.h>
|
|
|
|
|
2018-05-14 21:20:21 +08:00
|
|
|
#include <asm/cp15.h>
|
|
|
|
#include <asm/cputype.h>
|
2018-05-14 22:38:55 +08:00
|
|
|
#include <asm/proc-fns.h>
|
2018-05-14 21:20:21 +08:00
|
|
|
#include <asm/system_misc.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_HARDEN_BRANCH_PREDICTOR
|
|
|
|
DEFINE_PER_CPU(harden_branch_predictor_fn_t, harden_branch_predictor_fn);
|
|
|
|
|
2018-05-25 21:40:14 +08:00
|
|
|
extern void cpu_v7_iciallu_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
|
|
|
|
extern void cpu_v7_bpiall_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
|
2018-05-14 22:38:55 +08:00
|
|
|
extern void cpu_v7_smc_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
|
|
|
|
extern void cpu_v7_hvc_switch_mm(phys_addr_t pgd_phys, struct mm_struct *mm);
|
|
|
|
|
2018-05-14 21:20:21 +08:00
|
|
|
static void harden_branch_predictor_bpiall(void)
|
|
|
|
{
|
|
|
|
write_sysreg(0, BPIALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void harden_branch_predictor_iciallu(void)
|
|
|
|
{
|
|
|
|
write_sysreg(0, ICIALLU);
|
|
|
|
}
|
|
|
|
|
2018-05-14 22:38:55 +08:00
|
|
|
static void __maybe_unused call_smc_arch_workaround_1(void)
|
|
|
|
{
|
|
|
|
arm_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __maybe_unused call_hvc_arch_workaround_1(void)
|
|
|
|
{
|
|
|
|
arm_smccc_1_1_hvc(ARM_SMCCC_ARCH_WORKAROUND_1, NULL);
|
|
|
|
}
|
|
|
|
|
2018-05-14 21:20:21 +08:00
|
|
|
static void cpu_v7_spectre_init(void)
|
|
|
|
{
|
|
|
|
const char *spectre_v2_method = NULL;
|
|
|
|
int cpu = smp_processor_id();
|
|
|
|
|
|
|
|
if (per_cpu(harden_branch_predictor_fn, cpu))
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (read_cpuid_part()) {
|
|
|
|
case ARM_CPU_PART_CORTEX_A8:
|
|
|
|
case ARM_CPU_PART_CORTEX_A9:
|
|
|
|
case ARM_CPU_PART_CORTEX_A12:
|
|
|
|
case ARM_CPU_PART_CORTEX_A17:
|
|
|
|
case ARM_CPU_PART_CORTEX_A73:
|
|
|
|
case ARM_CPU_PART_CORTEX_A75:
|
|
|
|
per_cpu(harden_branch_predictor_fn, cpu) =
|
|
|
|
harden_branch_predictor_bpiall;
|
|
|
|
spectre_v2_method = "BPIALL";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARM_CPU_PART_CORTEX_A15:
|
|
|
|
case ARM_CPU_PART_BRAHMA_B15:
|
|
|
|
per_cpu(harden_branch_predictor_fn, cpu) =
|
|
|
|
harden_branch_predictor_iciallu;
|
|
|
|
spectre_v2_method = "ICIALLU";
|
|
|
|
break;
|
2018-05-14 22:38:55 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_ARM_PSCI
|
2019-11-11 11:32:04 +08:00
|
|
|
case ARM_CPU_PART_BRAHMA_B53:
|
|
|
|
/* Requires no workaround */
|
|
|
|
break;
|
2018-05-14 22:38:55 +08:00
|
|
|
default:
|
|
|
|
/* Other ARM CPUs require no workaround */
|
|
|
|
if (read_cpuid_implementor() == ARM_CPU_IMP_ARM)
|
|
|
|
break;
|
2020-08-24 06:36:59 +08:00
|
|
|
fallthrough;
|
2018-05-14 22:38:55 +08:00
|
|
|
/* Cortex A57/A72 require firmware workaround */
|
|
|
|
case ARM_CPU_PART_CORTEX_A57:
|
|
|
|
case ARM_CPU_PART_CORTEX_A72: {
|
|
|
|
struct arm_smccc_res res;
|
|
|
|
|
2019-10-21 23:28:22 +08:00
|
|
|
arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
|
|
|
|
ARM_SMCCC_ARCH_WORKAROUND_1, &res);
|
|
|
|
if ((int)res.a0 != 0)
|
|
|
|
return;
|
|
|
|
|
2019-08-09 21:22:42 +08:00
|
|
|
switch (arm_smccc_1_1_get_conduit()) {
|
|
|
|
case SMCCC_CONDUIT_HVC:
|
2018-05-14 22:38:55 +08:00
|
|
|
per_cpu(harden_branch_predictor_fn, cpu) =
|
|
|
|
call_hvc_arch_workaround_1;
|
2018-07-19 19:21:31 +08:00
|
|
|
cpu_do_switch_mm = cpu_v7_hvc_switch_mm;
|
2018-05-14 22:38:55 +08:00
|
|
|
spectre_v2_method = "hypervisor";
|
|
|
|
break;
|
|
|
|
|
2019-08-09 21:22:42 +08:00
|
|
|
case SMCCC_CONDUIT_SMC:
|
2018-05-14 22:38:55 +08:00
|
|
|
per_cpu(harden_branch_predictor_fn, cpu) =
|
|
|
|
call_smc_arch_workaround_1;
|
2018-07-19 19:21:31 +08:00
|
|
|
cpu_do_switch_mm = cpu_v7_smc_switch_mm;
|
2018-05-14 22:38:55 +08:00
|
|
|
spectre_v2_method = "firmware";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2018-05-14 21:20:21 +08:00
|
|
|
}
|
2018-05-14 22:38:55 +08:00
|
|
|
|
2018-05-14 21:20:21 +08:00
|
|
|
if (spectre_v2_method)
|
|
|
|
pr_info("CPU%u: Spectre v2: using %s workaround\n",
|
|
|
|
smp_processor_id(), spectre_v2_method);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void cpu_v7_spectre_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static __maybe_unused bool cpu_v7_check_auxcr_set(bool *warned,
|
2018-05-10 20:09:54 +08:00
|
|
|
u32 mask, const char *msg)
|
|
|
|
{
|
|
|
|
u32 aux_cr;
|
|
|
|
|
|
|
|
asm("mrc p15, 0, %0, c1, c0, 1" : "=r" (aux_cr));
|
|
|
|
|
|
|
|
if ((aux_cr & mask) != mask) {
|
|
|
|
if (!*warned)
|
|
|
|
pr_err("CPU%u: %s", smp_processor_id(), msg);
|
|
|
|
*warned = true;
|
2018-05-14 21:20:21 +08:00
|
|
|
return false;
|
2018-05-10 20:09:54 +08:00
|
|
|
}
|
2018-05-14 21:20:21 +08:00
|
|
|
return true;
|
2018-05-10 20:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static DEFINE_PER_CPU(bool, spectre_warned);
|
|
|
|
|
2018-05-14 21:20:21 +08:00
|
|
|
static bool check_spectre_auxcr(bool *warned, u32 bit)
|
2018-05-10 20:09:54 +08:00
|
|
|
{
|
2018-05-14 21:20:21 +08:00
|
|
|
return IS_ENABLED(CONFIG_HARDEN_BRANCH_PREDICTOR) &&
|
2018-05-10 20:09:54 +08:00
|
|
|
cpu_v7_check_auxcr_set(warned, bit,
|
|
|
|
"Spectre v2: firmware did not set auxiliary control register IBE bit, system vulnerable\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void cpu_v7_ca8_ibe(void)
|
|
|
|
{
|
2018-05-14 21:20:21 +08:00
|
|
|
if (check_spectre_auxcr(this_cpu_ptr(&spectre_warned), BIT(6)))
|
|
|
|
cpu_v7_spectre_init();
|
2018-05-10 20:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cpu_v7_ca15_ibe(void)
|
|
|
|
{
|
2018-05-14 21:20:21 +08:00
|
|
|
if (check_spectre_auxcr(this_cpu_ptr(&spectre_warned), BIT(0)))
|
|
|
|
cpu_v7_spectre_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cpu_v7_bugs_init(void)
|
|
|
|
{
|
|
|
|
cpu_v7_spectre_init();
|
2018-05-10 20:09:54 +08:00
|
|
|
}
|