The XSAVES malfunction described in AMD Erratum 1386 also affects certain Hygon Family 18h processors. Under specific conditions, XSAVES may fail to save XMM register state, allowing stale register values to reappear after the state is restored. The failure mode is documented as Erratum 1386 in "Revision Guide for AMD Family 19h Models 00h-0Fh Processors", Publication #56683 [1]. None of the affected processors enumerate supervisor XSAVE state components. XSAVEC is therefore equivalent for their enabled state components and is not affected by the erratum. Family 18h models 00h through 06h and model 10h are affected. Other Family 18h models contain the fix in hardware. For the following model and stepping combinations, the minimum microcode revisions containing the fix are: Model Stepping Microcode 04h 0 0x9040000f 04h 1 0x9041000f 06h 0 0x9060000f 06h 1 0x9061000f 10h 0 0x91000004 These are the only known steppings for those models. Disable XSAVES on affected processors unless a matching model and stepping entry identifies a sufficiently recent microcode revision containing the fix. This allows the x86 FPU code to use XSAVEC instead. [1] https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/revision-guides/56683.pdf Signed-off-by: Tina Zhang Tested-by: Qiming Deng Reviewed-by: Zhen Yang Cc: stable@vger.kernel.org --- Changes in v3: - Match Family 18h model 00h explicitly because a model field of zero in an x86_cpu_id entry is treated as X86_MODEL_ANY. v2: https://lore.kernel.org/r/20261121105320.478279-1-zhang_wei@open-hieco.net v1: https://lore.kernel.org/r/20261028235712.383977-1-zhang_wei@open-hieco.net arch/x86/kernel/cpu/hygon.c | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/arch/x86/kernel/cpu/hygon.c b/arch/x86/kernel/cpu/hygon.c index ec51c2b9a257..6670dc8bb22f 100644 --- a/arch/x86/kernel/cpu/hygon.c +++ b/arch/x86/kernel/cpu/hygon.c @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -170,11 +171,56 @@ static void early_init_hygon(struct cpuinfo_x86 *c) set_cpu_cap(c, X86_FEATURE_VMMCALL); } +#define X86_MATCH_HYGON_MODEL_STEP_UCODE(model, step, ucode) \ + X86_MATCH_VFM_STEPS(VFM_MAKE(X86_VENDOR_HYGON, 0x18, model), \ + step, step, ucode) + +static const struct x86_cpu_id erratum_1386_microcode[] = { + X86_MATCH_HYGON_MODEL_STEP_UCODE(0x04, 0x00, 0x9040000f), + X86_MATCH_HYGON_MODEL_STEP_UCODE(0x04, 0x01, 0x9041000f), + X86_MATCH_HYGON_MODEL_STEP_UCODE(0x06, 0x00, 0x9060000f), + X86_MATCH_HYGON_MODEL_STEP_UCODE(0x06, 0x01, 0x9061000f), + X86_MATCH_HYGON_MODEL_STEP_UCODE(0x10, 0x00, 0x91000004), + {} +}; + +static const struct x86_cpu_id erratum_1386_cpus[] = { + X86_MATCH_VENDOR_FAM_MODEL(HYGON, 0x18, 0x01, NULL), + X86_MATCH_VENDOR_FAM_MODEL(HYGON, 0x18, 0x02, NULL), + X86_MATCH_VENDOR_FAM_MODEL(HYGON, 0x18, 0x03, NULL), + X86_MATCH_VENDOR_FAM_MODEL(HYGON, 0x18, 0x04, NULL), + X86_MATCH_VENDOR_FAM_MODEL(HYGON, 0x18, 0x05, NULL), + X86_MATCH_VENDOR_FAM_MODEL(HYGON, 0x18, 0x06, NULL), + X86_MATCH_VENDOR_FAM_MODEL(HYGON, 0x18, 0x10, NULL), + {} +}; + +static void fix_erratum_1386(struct cpuinfo_x86 *c) +{ + /* + * Work around Erratum 1386. The XSAVES instruction malfunctions in + * certain circumstances on affected Hygon processors. Clear the feature + * flag only on microcode revisions without the fix. + */ + if (x86_match_min_microcode_rev(erratum_1386_microcode)) + return; + + clear_cpu_cap(c, X86_FEATURE_XSAVES); +} + static void init_hygon(struct cpuinfo_x86 *c) { u64 vm_cr; early_init_hygon(c); + /* + * Model 00h is a valid Hygon processor model, but x86_match_cpu() + * treats a model value of 0 as X86_MODEL_ANY. Match Model 00h + * explicitly. + */ + if ((c->x86 == 0x18 && c->x86_model == 0x00) || + x86_match_cpu(erratum_1386_cpus)) + fix_erratum_1386(c); set_cpu_cap(c, X86_FEATURE_REP_GOOD); -- 2.43.7