x86/svm: Work around VMRUN's corruption of VMCB.INTR_SHADOW
PATCH 1/1 x86/svm: Work around VMRUN's corruption of VMCB.INTR_SHADOW
I was asked me to explain the comment beside STI in the svm_asm_do_resume(), and, in trying to do so, things unravelled.
The original fix inherited from KVM prevented the guests INTR_SHADOW being changed from 0 to 1. This prevents undue delay to interrupts, but it caused the value to get changed from 1 to 0 if the guest was in an INTR_SHADOW.
Whether this matters for IDT guests is unknown; it depends on what else VMRUN can do beside event injection and still write back the wrong value. This does matter for FRED guests, as INTR_SHADOW is exposed architecturally in the FRED frame via the STB field.
---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Jason Andryuk <[email protected]>
CC: Ross Lagerwall <[email protected]>
CC: Doug Covelli <[email protected]>
CC: Sean Christopherson <[email protected]>
CC: Paolo Bonzini <[email protected]>
CC: Borislav Petkov <[email protected]>
CC: David Kaplan <[email protected]>
CC: David Woodhouse <[email protected]>
CC: Shivansh Dhiman <[email protected]>
CC: Nikunj Dadhania <[email protected]>
CC: Santosh Shukla <[email protected]>
AMD have confirmed this is broken back to at least Zen1. Hopefully an erratum will be issued.
Slightly RFC. I don't currently have a Venice system to demonstrate the FRED part of this. CC-ing the KVM folk and AMD FRED folk too, as someone will presumably want to adapt this fix.
Concerning encrypted VMs, there is no fix; INTR_SHADOW is encrypted state which the VMM can't access.
--- xen/arch/x86/hvm/svm/entry.S | 40 ++++++++++++++++++++++++------- xen/arch/x86/x86_64/asm-offsets.c | 9 +++++++ 2 files changed, 41 insertions(+), 8 deletions(-)
xen/arch/x86/hvm/svm/entry.S +32 -8
@@ -57,6 +57,37 @@ __UNLIKELY_END(nsvm_hap) clgi + /* + * Interrupts need enabling prior to VMRUN. Anywhere after CLGI is + * safe. + * + * On all Zen CPUs to date, VMRUN has a bug and mixes up the host and + * guest INTR_SHADOW state. If there is a VMExit during VMRUN + * (e.g. taking NPT_FAULT during event injection), then the host's + * INTR_SHADOW gets written back into the VMCB, corrupting guest + * state. + * + * This is believed to go unnoticed for pre-FRED guests (injecting + * events cancels INTR_SHADOW), but FRED changes this behaviour to be + * able to observe and restore the shadow via the STB field in a FRED + * frame. + * + * In order to fix, arrange for Xen's INTR_SHADOW at the point of + * VMRUN to match the guest's INTR_SHADOW. This way, if a VMExit + * occurs, the VMCB is corrupted with the "correct" value. + * + * Commonly the guest will not be in an INTR_SHADOW. In this case, + * execute STI early to get Xen's INTR_SHADOW over and done with; the + * second STI does nothing as INTR_SHADOW only occurs when IF changes + * from 0 to 1. If the guest is in an INTR_SHADOW, skip the early STI + * so the late STI does trigger INTR_SHADOW and cover VMRUN. + */ + mov VCPU_svm_vmcb(%rbx), %rax + testb $1, VMCB_int_stat(%rax) + jnz 1f + sti +1: + /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */ /* SPEC_CTRL_EXIT_TO_SVM Req: b=curr %rsp=regs/cpuinfo, Clob: acd */ .macro svm_vmentry_spec_ctrl
@@ -74,19 +105,12 @@ __UNLIKELY_END(nsvm_hap) ALTERNATIVE "", svm_vmentry_spec_ctrl, X86_FEATURE_SC_MSR_HVM ALTERNATIVE "", DO_SPEC_CTRL_DIV, X86_FEATURE_SC_DIV - /* - * Set EFLAGS.IF after CLGI covers us from real interrupts, but not - * immediately prior to VMRUN. The VMRUN instruction leaks it's - * INTR_SHADOW into guest state if a VMExit occurs before VMRUN - * completes (e.g. taking #NPF during event injecting.) - */ - sti - mov VCPU_svm_vmcb_pa(%rbx),%rax POP_GPRS skip_rax=1 /* %rax restored by VMRUN. */ SPEC_CTRL_COND_VERW /* Req: %rsp=eframe Clob: efl */ + sti vmrun PUSH_AND_CLEAR_GPRS
xen/arch/x86/x86_64/asm-offsets.c +9 -0
@@ -21,6 +21,10 @@# include "../boot/video.h" #endif +#ifdef CONFIG_AMD_SVM +# include "../hvm/svm/vmcb.h" +#endif + #define DEFINE(_sym, _val) \ asm volatile ( "\n.ascii\"==>#define " #_sym " %0 /* " #_val " */<==\""\ :: "i" (_val) )
@@ -133,6 +137,11 @@ void __dummy__(void)BLANK ();#endif +#ifdef CONFIG_AMD_SVM + OFFSET(VMCB_int_stat, struct vmcb_struct, int_stat); +BLANK (); +#endif +#ifdef CONFIG_PV32 OFFSET(DOMAIN_is_32bit_pv, struct domain, arch.pv.is_32bit);BLANK ();
base-commit: f9b72572c8d5b6d312fd0b5014f65bed20efac40
Signature · 1 line
2.39.5
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 34 lines · Sean Christopherson
Quoted 33 lines · Andrew Cooper
I was asked me to explain the comment beside STI in the
svm_asm_do_resume(), and, in trying to do so, things unravelled.The original fix inherited from KVM prevented the guests
INTR_SHADOWbeing changed from 0 to 1. This prevents undue delay to interrupts, but it caused the value to get changed from 1 to 0 if the guest was in anINTR_SHADOW.Whether this matters for IDT guests is unknown; it depends on what else VMRUN can do beside event injection and still write back the wrong value. This does matter for FRED guests, as
INTR_SHADOWis exposed architecturally in the FRED frame via the STB field.Fixes c989ff614f6b ("x86/svm: Separate STI and VMRUN instructions in svm_asm_do_resume()")Signed-off-by Andrew Cooper <[email protected]>---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Jason Andryuk <[email protected]>
CC: Ross Lagerwall <[email protected]>
CC: Doug Covelli <[email protected]>
CC: Sean Christopherson <[email protected]>
CC: Paolo Bonzini <[email protected]>
CC: Borislav Petkov <[email protected]>
CC: David Kaplan <[email protected]>
CC: David Woodhouse <[email protected]>
CC: Shivansh Dhiman <[email protected]>
CC: Nikunj Dadhania <[email protected]>
CC: Santosh Shukla <[email protected]>AMD have confirmed this is broken back to at least Zen1. Hopefully an erratum will be issued.
Quoted 6 lines
Quoted 3 lines
Slightly RFC. I don't currently have a Venice system to demonstrate the FRED part of this. CC-ing the KVM folk and AMD FRED folk too, as someone will presumably want to adapt this fix.
I wouldn't say I "want" to adapt this. :-)
Thanks for the heads up, and for the suggested fix!
You're welcome. After all, I did stea^W borrow the fix from you last time around.
Quoted 10 lines
xen/arch/x86/hvm/svm/entry.S __UNLIKELY_END(nsvm_hap) · line 73
Quoted 3 lines
xen/arch/x86/hvm/svm/entry.S +3
-0+ * events cancels INTR_SHADOW), but FRED changes this behaviour to be + * able to observe and restore the shadow via the STB field in a FRED + * frame.Hmm, does that even matter in practice? IIUC, hitting this would require taking an exception in an STI shadow, which would break pretty much any software flow I can think of that relies on the STI shadow.
E.g. IIUC,
STI->HLTwith a #PF on the HLT code fetch would break pre-FRED because HLT would execute without the STI shadow. Unless software quite deliberately does things that only work for FRED, I assume the entire thing is benign?
STI; HLT vs #PF was fixed very intentionally in FRED. In other news, Windows pages out kernel code, and uses the STI; HLT pattern extensively.
It's not just the #PF case being fixed by this. e.g. Instruction breakpoints now don't need special casing in FRED either. Even if we don't see a need today, it won't be long until OSes will be expecting to use the niceties that FRED offers.
The unknown here is what besides event injection also triggers this bug in the VMRUN microflow. I've not had an answer to this question.
~Andrew
Quoted 7 lines · Andrew Cooper
I was asked me to explain the comment beside STI in the
svm_asm_do_resume(), and, in trying to do so, things unravelled.The original fix inherited from KVM prevented the guests
INTR_SHADOWbeing changed from 0 to 1. This prevents undue delay to interrupts, but it caused the value to get changed from 1 to 0 if the guest was in anINTR_SHADOW.
Quoted 7 lines
Whether this matters for IDT guests is unknown; it depends on what else VMRUN can do beside event injection and still write back the wrong value. This does matter for FRED guests, as
INTR_SHADOWis exposed architecturally in the FRED frame via the STB field.Fixes c989ff614f6b ("x86/svm: Separate STI and VMRUN instructions in svm_asm_do_resume()")Signed-off-by Andrew Cooper <[email protected]>