Commit ab331ac5 authored by Zeng Heng's avatar Zeng Heng
Browse files

arm64/mpam: Check mpam_detect_is_enabled() before accessing MPAM registers

hulk inclusion
category: bugfix
bugzilla: https://gitee.com/src-openeuler/kernel/issues/I9OXPO



--------------------------------

If BIOS firmware doesn't enable MPAM function under EL3 environment, while
the hardware has the MPAM ability, that would cause illegal instruction
fault when access MPAM registers.

In order to be compatible with this scenario and avoid crashes during
startup, only by adding the 'arm64.mpam' boot parameter in kernel command
line make the MPAM feature be enabled.

Fixes: 21771eaa ("arm64: cpufeature: discover CPU support for MPAM")
Signed-off-by: default avatarZeng Heng <zengheng4@huawei.com>
parent 8888a342
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -858,6 +858,15 @@ static inline bool cpus_support_mpam(void)
		cpus_have_final_cap(ARM64_MPAM);
}

#ifdef CONFIG_ARM64_MPAM
bool mpam_detect_is_enabled(void);
#else
static inline bool mpam_detect_is_enabled(void)
{
	return false;
}
#endif

int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt);
bool try_emulate_mrs(struct pt_regs *regs, u32 isn);

+35 −4
Original line number Diff line number Diff line
@@ -1078,8 +1078,9 @@ void __init init_cpu_features(struct cpuinfo_arm64 *info)
		vec_init_vq_map(ARM64_VEC_SME);
	}

	if (id_aa64pfr0_mpam(info->reg_id_aa64pfr0) ||
	    id_aa64pfr1_mpamfrac(info->reg_id_aa64pfr1))
	if (mpam_detect_is_enabled() &&
	   (id_aa64pfr0_mpam(info->reg_id_aa64pfr0) ||
	    id_aa64pfr1_mpamfrac(info->reg_id_aa64pfr1)))
		init_cpu_ftr_reg(SYS_MPAMIDR_EL1, info->reg_mpamidr);

	if (id_aa64pfr1_mte(info->reg_id_aa64pfr1))
@@ -1341,8 +1342,9 @@ void update_cpu_features(int cpu,
			vec_update_vq_map(ARM64_VEC_SME);
	}

	if (id_aa64pfr0_mpam(info->reg_id_aa64pfr0) ||
	    id_aa64pfr1_mpamfrac(info->reg_id_aa64pfr1)) {
	if (mpam_detect_is_enabled() &&
	   (id_aa64pfr0_mpam(info->reg_id_aa64pfr0) ||
	    id_aa64pfr1_mpamfrac(info->reg_id_aa64pfr1))) {
		taint |= check_update_ftr_reg(SYS_MPAMIDR_EL1, cpu,
					info->reg_mpamidr, boot->reg_mpamidr);
	}
@@ -2307,6 +2309,19 @@ cpucap_panic_on_conflict(const struct arm64_cpu_capabilities *cap)
	return !!(cap->type & ARM64_CPUCAP_PANIC_ON_CONFLICT);
}

static bool __read_mostly mpam_force_enabled;
bool mpam_detect_is_enabled(void)
{
	return mpam_force_enabled;
}

static int __init mpam_setup(char *str)
{
	mpam_force_enabled = true;
	return 0;
}
early_param("arm64.mpam", mpam_setup);

static bool __maybe_unused
test_has_mpam(const struct arm64_cpu_capabilities *entry, int scope)
{
@@ -2317,6 +2332,12 @@ test_has_mpam(const struct arm64_cpu_capabilities *entry, int scope)
	    !id_aa64pfr1_mpamfrac(pfr1))
		return false;

	if (is_kdump_kernel())
		return false;

	if (!mpam_detect_is_enabled())
		return false;

	/* Check firmware actually enabled MPAM on this cpu. */
	return (read_sysreg_s(SYS_MPAM1_EL1) & MPAM_SYSREG_EN);
}
@@ -2324,6 +2345,16 @@ test_has_mpam(const struct arm64_cpu_capabilities *entry, int scope)
static void __maybe_unused
cpu_enable_mpam(const struct arm64_cpu_capabilities *entry)
{
	u64 idr = read_sanitised_ftr_reg(SYS_MPAMIDR_EL1);

	/*
	 * Initialise MPAM EL2 registers and disable EL2 traps.
	 */
	write_sysreg_s(0, SYS_MPAM2_EL2);

	if (idr & MPAMIDR_HAS_HCR)
		write_sysreg_s(0, SYS_MPAMHCR_EL2);

	/*
	 * Access by the kernel (at EL1) should use the reserved PARTID
	 * which is configured unrestricted. This avoids priority-inversion
+1 −0
Original line number Diff line number Diff line
@@ -461,6 +461,7 @@ static void __cpuinfo_store_cpu(struct cpuinfo_arm64 *info)
		__cpuinfo_store_cpu_32bit(&info->aarch32);

	if (IS_ENABLED(CONFIG_ARM64_MPAM) &&
	    mpam_detect_is_enabled() &&
	   (id_aa64pfr0_mpam(info->reg_id_aa64pfr0) ||
	    id_aa64pfr1_mpamfrac(info->reg_id_aa64pfr1)))
		info->reg_mpamidr = read_cpuid(MPAMIDR_EL1);
+11 −4
Original line number Diff line number Diff line
@@ -15,13 +15,20 @@ DEFINE_PER_CPU(u64, arm64_mpam_current);

static int __init arm64_mpam_register_cpus(void)
{
	u16 partid_max;
	u64 mpamidr;
	u8 pmg_max;

	if (is_kdump_kernel())
		return 0;

	u64 mpamidr = read_sanitised_ftr_reg(SYS_MPAMIDR_EL1);
	u16 partid_max = FIELD_GET(MPAMIDR_PARTID_MAX, mpamidr);
	u8 pmg_max = FIELD_GET(MPAMIDR_PMG_MAX, mpamidr);
	if (!mpam_cpus_have_feature())
		return 0;

	mpamidr = read_sanitised_ftr_reg(SYS_MPAMIDR_EL1);
	partid_max = FIELD_GET(MPAMIDR_PARTID_MAX, mpamidr);
	pmg_max = FIELD_GET(MPAMIDR_PMG_MAX, mpamidr);

	return mpam_register_requestor(partid_max, pmg_max);
}
arch_initcall(arm64_mpam_register_cpus)
arch_initcall(arm64_mpam_register_cpus);