Add Automatic IBRS support
cover Add Automatic IBRS support
Adds support for AMD's Automatic IBRS. It's a set-and-forget feature that prevents lower privileged executions from affecting speculations of higher privileged executions, so retpolines are not required. Furthermore, it clears the RSB upon VMEXIT, so we can avoid doing it if the feature is present.
Patch 1 adds the relevant bit definitions for CPUID and EFER.
Patch 2 Hooks up AutoIBRS to spec_ctrl. so it's used when IBRS is picked.
It also tweaks the heuristics so AutoIBRS is preferred over retpolines as BTI mitigation. This is enough to protect Xen.
Patch 3 exposes the feature to HVM guests.
Alejandro Vallejo (3):
x86: Add bit definitions for Automatic IBRS
x86: Add support for AMD's Automatic IBRS
x86: Expose Automatic IBRS to guests
tools/libs/light/libxl_cpuid.c | 1 + tools/misc/xen-cpuid.c | 2 + xen/arch/x86/hvm/hvm.c | 3 ++ xen/arch/x86/include/asm/cpufeature.h | 1 + xen/arch/x86/include/asm/msr-index.h | 4 +- xen/arch/x86/pv/emul-priv-op.c | 4 +- xen/arch/x86/setup.c | 3 ++ xen/arch/x86/smpboot.c | 3 ++ xen/arch/x86/spec_ctrl.c | 52 +++++++++++++++------ xen/include/public/arch-x86/cpufeatureset.h | 1 + 10 files changed, 56 insertions(+), 18 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 x86: Add bit definitions for Automatic IBRS
This is AMD's version of Intel's Enhanced IBRS. Exposed in CPUID and toggled in EFER.
--- tools/libs/light/libxl_cpuid.c | 1 + tools/misc/xen-cpuid.c | 2 ++ xen/arch/x86/include/asm/cpufeature.h | 1 + xen/arch/x86/include/asm/msr-index.h | 1 + xen/include/public/arch-x86/cpufeatureset.h | 1 + 5 files changed, 6 insertions(+)
tools/libs/light/libxl_cpuid.c +1 -0
@@ -317,6 +317,7 @@ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* str) {"lfence+", 0x80000021, NA, CPUID_REG_EAX, 2, 1}, {"nscb", 0x80000021, NA, CPUID_REG_EAX, 6, 1}, + {"auto-ibrs", 0x80000021, NA, CPUID_REG_EAX, 8, 1}, {"cpuid-user-dis", 0x80000021, NA, CPUID_REG_EAX, 17, 1}, {"maxhvleaf", 0x40000000, NA, CPUID_REG_EAX, 0, 8},
tools/misc/xen-cpuid.c +2 -0
@@ -200,6 +200,8 @@ static const char *const str_e21a[32] = [ 2] = "lfence+", [ 6] = "nscb", + [8 ] ="auto-ibrs" , + /* 16 */ [17] = "cpuid-user-dis", };
xen/arch/x86/include/asm/cpufeature.h +1 -0
@@ -161,6 +161,7 @@ static inline bool boot_cpu_has(unsigned int feat)#define cpu_has_amd_ssbd boot_cpu_has(X86_FEATURE_AMD_SSBD)#define cpu_has_virt_ssbd boot_cpu_has(X86_FEATURE_VIRT_SSBD)#define cpu_has_ssb_no boot_cpu_has(X86_FEATURE_SSB_NO) +#define cpu_has_auto_ibrs boot_cpu_has(X86_FEATURE_AUTOMATIC_IBRS)/* CPUID level 0x00000007:0.edx */ #define cpu_has_avx512_4vnniw boot_cpu_has(X86_FEATURE_AVX512_4VNNIW)
xen/arch/x86/include/asm/msr-index.h +1 -0
@@ -175,6 +175,7 @@#define EFER_NXE (_AC(1, ULL) << 11)/* No Execute Enable */ #define EFER_SVME (_AC(1, ULL) << 12)/* Secure Virtual Machine Enable */ #define EFER_FFXSE (_AC(1, ULL) << 14)/* Fast FXSAVE/FXRSTOR */ +#define EFER_AIBRSE (_AC(1, ULL) << 21)/* Automatic IBRS Enable */ #define EFER_KNOWN_MASK \ (EFER_SCE | EFER_LME | EFER_LMA | EFER_NXE | EFER_SVME | EFER_FFXSE)
xen/include/public/arch-x86/cpufeatureset.h +1 -0
@@ -287,6 +287,7 @@ XEN_CPUFEATURE(AVX_IFMA, 10*32+23) /*A AVX-IFMA Instructions *//* AMD-defined CPU features, CPUID level 0x80000021.eax, word 11 */ XEN_CPUFEATURE(LFENCE_DISPATCH, 11*32+ 2) /*A LFENCE always serializing */ XEN_CPUFEATURE(NSCB, 11*32+ 6) /*A Null Selector Clears Base (and limit too) */ +XEN_CPUFEATURE(AUTOMATIC_IBRS, 11*32+ 8) /* HW can handle IBRS on its own */XEN_CPUFEATURE (CPUID_USER_DIS ,11 *32 +17 )/* CPUID disable for CPL > 0 software */ /* Intel-defined CPU features, CPUID level 0x00000007:1.ebx, word 12 */
Signature · 1 line
2.34.1
Re: [PATCH 1/3] x86: Add bit definitions for Automatic IBRS
Quoted 2 lines · Alejandro Vallejo
This is AMD's version of Intel's Enhanced IBRS. Exposed in CPUID and toggled in EFER.
AIBRS and EIBRS are very much not the same, and I argued hard to not have Linux confuse the too, but alas.
Don't mention EIBRS at all.
Simply "Auto IBRS is a new feature in AMD Zen4 CPUs and late, intended to reduce the overhead involved with operating IBRS", or something along these lines.
tools/misc/xen-cpuid.c static const char *const str_e21a[32] = · line 204
Quoted 10 lines
tools/misc/xen-cpuid.c +2
-0@@ -200,6 +200,8 @@ static const char *const str_e21a[32] = [ 2] = "lfence+", [ 6] = "nscb", + [ 8] = "auto-ibrs", +
This wants to be:
[ 6] = "nscb",
+ [ 8] = "auto-ibrs",
as they are adjacent with names in two columns. Gaps are only for discontinuities in numbering.
xen/include/public/arch-x86/cpufeatureset.h XEN_CPUFEATURE(AVX_IFMA, 10*32+23) /*A AVX-IFMA Instructions */ · line 290
Quoted 9 lines
xen/include/public/arch-x86/cpufeatureset.h +1
-0@@ -287,6 +287,7 @@ XEN_CPUFEATURE(AVX_IFMA, 10*32+23) /*A AVX-IFMA Instructions *//* AMD-defined CPU features, CPUID level 0x80000021.eax, word 11 */ XEN_CPUFEATURE(LFENCE_DISPATCH, 11*32+ 2) /*A LFENCE always serializing */ XEN_CPUFEATURE(NSCB, 11*32+ 6) /*A Null Selector Clears Base (and limit too) */ +XEN_CPUFEATURE (AUTOMATIC_IBRS ,11 *32 +8 )/* HW can handle IBRS on its own */
Were possible, we want to use the same names. AUTO_IBRS is fine here, and shorter to use throughout Xen.
Furthermore, it must match the cpu_has_* name, and that's already in the better form.
~Andrew
Re: [PATCH 1/3] x86: Add bit definitions for Automatic IBRS
Quoted 8 lines · Andrew Cooper
AIBRS and EIBRS are very much not the same, and I argued hard to not have Linux confuse the too, but alas.
Don't mention EIBRS at all.
Simply "Auto IBRS is a new feature in AMD Zen4 CPUs and late, intended to reduce the overhead involved with operating IBRS", or something along these lines.
Sure. I did go out of my way to avoid ambiguityin the code, but it's true the commit message is unfortunate.
Quoted 17 lines
tools/misc/xen-cpuid.c static const char *const str_e21a[32] = · line 204
Quoted 11 lines
tools/misc/xen-cpuid.c +2
-0@@ -200,6 +200,8 @@ static const char *const str_e21a[32] = [ 2] = "lfence+", [ 6] = "nscb", + [ 8] = "auto-ibrs", +This wants to be:
[ 6] = "nscb",
+ [ 8] = "auto-ibrs",
Quoted 7 lines
as they are adjacent with names in two columns. Gaps are only for discontinuities in numbering.
[...]
Were possible, we want to use the same names.
AUTO_IBRSis fine here, and shorter to use throughout Xen.
Sure to both.
Alejandro
PATCH 2/3 x86: Add support for AMD's Automatic IBRS
In reply to Alejandro VallejoIn cases where AutoIBRS is supported by the host:
* Prefer AutoIBRS to retpolines as BTI mitigation in heuristics calculations.
* Always enable AutoIBRS if IBRS is chosen as a BTI mitigation.
* Avoid stuffing the RAS/RSB on VMEXIT if AutoIBRS is enabled.
* Delay setting AutoIBRS until after dom0 is set up, just like setting SPEC_CTRL.
--- xen/arch/x86/setup.c | 3 +++ xen/arch/x86/smpboot.c | 3 +++ xen/arch/x86/spec_ctrl.c | 52 ++++++++++++++++++++++++++++------------ 3 files changed, 43 insertions(+), 15 deletions(-)
xen/arch/x86/setup.c +3 -0
@@ -2036,6 +2036,9 @@ void __init noreturn __start_xen(unsigned long mbi_p)barrier ();wrmsrl (MSR_SPEC_CTRL , default_xen_spec_ctrl); info->last_spec_ctrl = default_xen_spec_ctrl; + +if ( cpu_has_auto_ibrs && (default_xen_spec_ctrl &SPEC_CTRL_IBRS ) ) +write_efer (read_efer () |EFER_AIBRSE ); }/* Copy the cpu info block, and move onto the BSP stack. */
xen/arch/x86/smpboot.c +3 -0
@@ -376,6 +376,9 @@ void start_secondary(void *unused) {wrmsrl (MSR_SPEC_CTRL , default_xen_spec_ctrl); info->last_spec_ctrl = default_xen_spec_ctrl; + +if ( cpu_has_auto_ibrs && (default_xen_spec_ctrl &SPEC_CTRL_IBRS ) ) +write_efer (read_efer () |EFER_AIBRSE ); }update_mcu_opt_ctrl ();
xen/arch/x86/spec_ctrl.c +37 -15
108 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH 2/3] x86: Add support for AMD's Automatic IBRS
Quoted 18 lines · Alejandro Vallejo
In cases where AutoIBRS is supported by the host:
* Prefer AutoIBRS to retpolines as BTI mitigation in heuristics calculations.
* Always enable AutoIBRS if IBRS is chosen as a BTI mitigation.
* Avoid stuffing the RAS/RSB on VMEXIT if AutoIBRS is enabled.
* Delay setting AutoIBRS until after dom0 is set up, just like settingSPEC_CTRL.Signed-off-by Alejandro Vallejo <[email protected]>---
xen/arch/x86/spec_ctrl.c +0
-0@@ -390,7 +390,7 @@ custom_param("pv-l1tf", parse_pv_l1tf);
xen/arch/x86/spec_ctrl.c static void __init print_details(enum ind_thunk thunk) · line 405
Quoted 7 lines
xen/arch/x86/spec_ctrl.c +3
-0@@ -399,7 +399,10 @@ static void __init print_details(enum ind_thunk thunk)if ( max >=2 )cpuid_count (7 ,2 , &tmp, &tmp, &tmp, &_7d2);if ( boot_cpu_data.extended_cpuid_level >=0x80000008 ) + {cpuid (0x80000008 , &tmp, &e8b, &tmp, &tmp); +cpuid (0x80000021 , &e21a, &tmp, &tmp, &tmp); + }
Do you need to check boot_cpu_data.extended_cpuid_level >= 0x80000021?
Regards,
Jason
Re: [PATCH 2/3] x86: Add support for AMD's Automatic IBRS
Quoted 10 lines · Jason Andryuk
xen/arch/x86/spec_ctrl.c static void __init print_details(enum ind_thunk thunk) · line 405
Quoted 5 lines
if ( boot_cpu_data.extended_cpuid_level >= 0x80000008 )xen/arch/x86/spec_ctrl.c +3
-0+ {cpuid (0x80000008 , &tmp, &e8b, &tmp, &tmp); +cpuid (0x80000021 , &e21a, &tmp, &tmp, &tmp); + }Do you need to check boot_cpu_data.extended_cpuid_level >=
0x80000021?Regards,
Jason
True that, yes. Will do on v2.
Alejandro
Re: [PATCH 2/3] x86: Add support for AMD's Automatic IBRS
In reply to Alejandro Vallejoxen/arch/x86/smpboot.c void start_secondary(void *unused) · line 382
Quoted 10 lines · Alejandro Vallejo
xen/arch/x86/smpboot.c +3
-0@@ -376,6 +376,9 @@ void start_secondary(void *unused) {wrmsrl (MSR_SPEC_CTRL , default_xen_spec_ctrl); info->last_spec_ctrl = default_xen_spec_ctrl; + +if ( cpu_has_auto_ibrs && (default_xen_spec_ctrl &SPEC_CTRL_IBRS ) ) +write_efer (read_efer () |EFER_AIBRSE ); }
Did you consider using trampoline_efer instead, which would then also take care of the S3 resume path (which otherwise I think you'd also need to fiddle with)?
Jan
Re: [PATCH 2/3] x86: Add support for AMD's Automatic IBRS
Quoted 16 lines · Jan Beulich
xen/arch/x86/smpboot.c void start_secondary(void *unused) · line 382
Quoted 10 lines · Alejandro Vallejo
xen/arch/x86/smpboot.c +3
-0@@ -376,6 +376,9 @@ void start_secondary(void *unused) {wrmsrl (MSR_SPEC_CTRL , default_xen_spec_ctrl); info->last_spec_ctrl = default_xen_spec_ctrl; + +if ( cpu_has_auto_ibrs && (default_xen_spec_ctrl &SPEC_CTRL_IBRS ) ) +write_efer (read_efer () |EFER_AIBRSE ); }Did you consider using
trampoline_eferinstead, which would then also take care of the S3 resume path (which otherwise I think you'd also need to fiddle with)?
Quoted 1 line
Jan
I didn't because I didn't know about it. Good call though, it's indeed a better place for it. Will do on v2.
Alejandro
Re: [PATCH 2/3] x86: Add support for AMD's Automatic IBRS
In reply to Alejandro Vallejoxen/arch/x86/setup.c void __init noreturn __start_xen(unsigned long mbi_p) · line 2042
Quoted 12 lines · Alejandro Vallejo
xen/arch/x86/setup.c +3
-0@@ -2036,6 +2036,9 @@ void __init noreturn __start_xen(unsigned long mbi_p)barrier ();wrmsrl (MSR_SPEC_CTRL , default_xen_spec_ctrl); info->last_spec_ctrl = default_xen_spec_ctrl; + +if ( cpu_has_auto_ibrs && (default_xen_spec_ctrl &SPEC_CTRL_IBRS ) ) +write_efer (read_efer () |EFER_AIBRSE ); }
After thinking things through I think I'll get rid of this "delay AutoIBRS" setting. I initially thought there might have been some handshake between the newly created dom0 and Xen on this path, but that doesn't seem to be the case. If so, I can remove some of this disjoint logic by setting AIBRSE on the local EFER and trampoline_efer during init_speculation_mitigation. Then the BSP will have the correct setting, the APs will pick it up on boot and S3 wakeups will do the right thing too.
I'm assuming then bsp_delay_spec_ctrl is there mostly to delay STIBP for as long as possible?
Alejandro
PATCH 3/3 x86: Expose Automatic IBRS to guests
In reply to Alejandro VallejoExpose AutoIBRS to HVM guests, because they can just use it. Make sure writes to EFER:AIBRSE are gated on the feature being exposed. Also hide EFER:AIBRSE from PV guests as they have no say in the matter.
--- xen/arch/x86/hvm/hvm.c | 3 +++ xen/arch/x86/include/asm/msr-index.h | 3 ++- xen/arch/x86/pv/emul-priv-op.c | 4 ++-- xen/include/public/arch-x86/cpufeatureset.h | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/hvm.c +3 -0
@@ -936,6 +936,9 @@ const char *hvm_efer_valid(const struct vcpu *v, uint64_t value,if ( (value &EFER_FFXSE ) && !p->extd.ffxsr )return "FFXSE without feature" ; +if ( (value &EFER_AIBRSE ) && !p->extd.automatic_ibrs ) +return "AutoIBRS without feature" ; +return NULL ; }
xen/arch/x86/include/asm/msr-index.h +2 -1
@@ -178,7 +178,8 @@#define EFER_AIBRSE (_AC(1, ULL) << 21)/* Automatic IBRS Enable */ #define EFER_KNOWN_MASK \ - (EFER_SCE | EFER_LME | EFER_LMA | EFER_NXE | EFER_SVME | EFER_FFXSE) + (EFER_SCE | EFER_LME | EFER_LMA | EFER_NXE | EFER_SVME | EFER_FFXSE | \ + EFER_AIBRSE)#define MSR_STAR 0xc0000081/* legacy mode SYSCALL target */ #define MSR_LSTAR 0xc0000082/* long mode SYSCALL target */
xen/arch/x86/pv/emul-priv-op.c +2 -2
@@ -853,8 +853,8 @@ static uint64_t guest_efer(const struct domain *d) {uint64_t val; -/* Hide unknown bits, and unconditionally hide SVME from guests. */ - val =read_efer () &EFER_KNOWN_MASK & ~EFER_SVME ; +/* Hide unknown bits, and unconditionally hide SVME and AIBRSE from guests. */ + val =read_efer () &EFER_KNOWN_MASK & ~(EFER_SVME |EFER_AIBRSE );/* * Hide the 64-bit features from 32-bit guests. SCE has * vendor-dependent behaviour.
xen/include/public/arch-x86/cpufeatureset.h +1 -1
@@ -287,7 +287,7 @@ XEN_CPUFEATURE(AVX_IFMA, 10*32+23) /*A AVX-IFMA Instructions *//* AMD-defined CPU features, CPUID level 0x80000021.eax, word 11 */ XEN_CPUFEATURE(LFENCE_DISPATCH, 11*32+ 2) /*A LFENCE always serializing */ XEN_CPUFEATURE(NSCB, 11*32+ 6) /*A Null Selector Clears Base (and limit too) */ -XEN_CPUFEATURE(AUTOMATIC_IBRS, 11*32+ 8) /* HW can handle IBRS on its own */ +XEN_CPUFEATURE(AUTOMATIC_IBRS, 11*32+ 8) /*S HW can handle IBRS on its own */XEN_CPUFEATURE (CPUID_USER_DIS ,11 *32 +17 )/* CPUID disable for CPL > 0 software */ /* Intel-defined CPU features, CPUID level 0x00000007:1.ebx, word 12 */
Signature · 1 line
2.34.1
Re: [PATCH 3/3] x86: Expose Automatic IBRS to guests
Quoted 5 lines · Alejandro Vallejo
Expose AutoIBRS to HVM guests, because they can just use it. Make sure writes to EFER:AIBRSE are gated on the feature being exposed. Also hide EFER:AIBRSE from PV guests as they have no say in the matter.
Signed-off-by Alejandro Vallejo <[email protected]>
It's worth saying "EFER is fully switched by VMRUN, so there's nothing further for Xen to do in order for HVM guests to use AutoIBRS".
We can in principle support AutoIBRS on PV guests, but it's fine not to for now.
This patch probably wants reordering to #2, because it is entirely independent of what Xen is doing with AutoIBRS for spec safety.
It will need a minor rebase over the bit name shortening, but otherwise