nestedsvm: GIF cleanup and fix
v2 cover nestedsvm: GIF cleanup and fix
Hi,
The first patch is code cleanup and shouldn't result in a visible behaviour change. I confirmed this by writing an XTF test that checks interrupt and event delivery behaviour with various combinations of IF and GIF set.
The second patch fixes L1 hitting an assertion error in Xen. I confirmed the fix by writing an XTF test that calls VMRUN on a VMCB without the VMRUN intercept set.
Thanks,
Ross
Ross Lagerwall (2):
x86/svm: Don't toggle local event delivery on stgi/clgi
nestedsvm: Clear GIF when injecting VMEXIT
xen/arch/x86/hvm/svm/nestedsvm.c | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/2 x86/svm: Don't toggle local event delivery on stgi/clgi
There is no need to explicitly enable/disable local event delivery on stgi/clgi since HVM event delivery already takes into account the state of the GIF (and IF). This ensures the behaviour is consistent regardless of whether vGIF is used.
Fold the functions into the callers since they are now only a single assignment.
---
New in v2.
xen/arch/x86/hvm/svm/nestedsvm.c | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +5 -21
@@ -19,22 +19,6 @@#define NSVM_ERROR_VVMCB 1#define NSVM_ERROR_VMENTRY 2 -static void -nestedsvm_vcpu_clgi (struct vcpu *v) -{ -/* clear gif flag */ -vcpu_nestedsvm (v).ns_gif =0 ; -local_event_delivery_disable ();/* mask events for PV drivers */ -} - -static void -nestedsvm_vcpu_stgi (struct vcpu *v) -{ -/* enable gif flag */ -vcpu_nestedsvm (v).ns_gif =1 ; -local_event_delivery_enable ();/* unmask events for PV drivers */ -} -int nestedsvm_vmcb_map (struct vcpu *v,uint64_t vmcbaddr) {struct nestedvcpu *nv = &vcpu_nestedhvm (v);
@@ -161,7 +145,7 @@ int cf_check nsvm_vcpu_reset(struct vcpu *v) svm->ns_iomap =NULL ; -nestedsvm_vcpu_stgi (v); + svm->ns_gif =1 ;return 0 ; }
@@ -667,7 +651,7 @@ nsvm_vcpu_vmentry(struct vcpu *v, struct cpu_user_regs *regs,return ret; } -nestedsvm_vcpu_stgi (v); + svm->ns_gif =1 ;return 0 ; }
@@ -1214,7 +1198,7 @@ nestedsvm_vmexit_defer(struct vcpu *v,if ( vmcb->_vintr.fields.vgif_enable ) vmcb->_vintr.fields.vgif =0 ;else -nestedsvm_vcpu_clgi (v); + svm->ns_gif =0 ; svm->ns_vmexit.exitcode = exitcode; svm->ns_vmexit.exitinfo1 = exitinfo1;
@@ -1502,7 +1486,7 @@ void svm_vmexit_do_stgi(struct cpu_user_regs *regs, struct vcpu *v)if ( (inst_len =svm_get_insn_len (v,INSTR_STGI )) ==0 )return ; -nestedsvm_vcpu_stgi (v); +vcpu_nestedsvm (v).ns_gif =1 ;__update_guest_eip (regs, inst_len); }
@@ -1523,7 +1507,7 @@ void svm_vmexit_do_clgi(struct cpu_user_regs *regs, struct vcpu *v)if ( (inst_len =svm_get_insn_len (v,INSTR_CLGI )) ==0 )return ; -nestedsvm_vcpu_clgi (v); +vcpu_nestedsvm (v).ns_gif =0 ;/* After a CLGI no interrupts should come */ intr =vmcb_get_vintr (vmcb);
Signature · 1 line
2.53.0
Re: [cross-arch mess] [PATCH v2 1/2] x86/svm: Don't toggle local event delivery on stgi/clgi
Quoted 1 line · Ross Lagerwall
There is no need to explicitly enable/disable local event delivery on
While the name of the function is local event, I think this needs to state evtchn because "events" have a separate meaning in this area on x86.
Personally, I'd phrase it more sternly. The masking (or not) of evtchn has nothing to do with GIF/IF.
Quoted 1 line
stgi/clgi since HVM event delivery already takes into account the state
STGI/CLGI.
Quoted 7 lines
of the GIF (and IF). This ensures the behaviour is consistent regardless of whether vGIF is used.
Fold the functions into the callers since they are now only a single assignment.
Signed-off-by Ross Lagerwall <[email protected]>
For the patch itself, Reviewed-by: Andrew Cooper <[email protected]>
However there's another can of worms.
This patch deletes the only user of local_event_delivery_disable().
It does not delete the only user of local_event_delivery_enable() and pulling that thread shows up a mess. The other caller is vcpu_block_enable_events() which comes from SCHEDOP_block (probably legitimate; enabling events is at least documented for this hypercall) and hvm_hlt() which is definitely bogus.
It needs deleting, but we may want to instrument that path to see if we ever clear the evtchn mask in practice, then run as many distros as we can find.
Furthermore, while RISC-V and PPC have BUG() stubs for these functions, ARM implements it as clearing regs->cpsr.irq_mask which is equally bogus mixing virtualised evtchns with architectural interrupt handling.
That, equally, needs deleting but I'll have to defer to the ARM maintainers (CC'd) on how to do it safely.
~Andrew
Re: [PATCH v2 1/2] x86/svm: Don't toggle local event delivery on stgi/clgi
Quoted 6 lines · Andrew Cooper
Quoted 1 line · Ross Lagerwall
There is no need to explicitly enable/disable local event delivery on
While the name of the function is local event, I think this needs to state evtchn because "events" have a separate meaning in this area on x86.
Quoted 7 lines
Personally, I'd phrase it more sternly. The masking (or not) of evtchn has nothing to do with GIF/IF.
Quoted 1 line
stgi/clgi since HVM event delivery already takes into account the state
STGI/CLGI.
OK. How about this instead?
x86/svm: Don't mask/unmask evtchns on CLGI/STGI
Whether or not evtchns are masked or unmasked has nothing to do with the state of the GIF/IF. Therefore, do not toggle the evtchn mask on STGI/CLGI - HVM event delivery already takes into account the state of the GIF/IF. This ensures the behaviour is consistent regardless of whether vGIF is used.
Fold the functions into the callers since they are now only a single assignment.
PATCH v2 2/2 nestedsvm: Clear GIF when injecting VMEXIT
In reply to Ross LagerwallIf L1 executes VMRUN with the GIF set and it fails consistency checks, Xen will inject a VMEXIT and fail the assert checking the GIF is cleared.
Instead, clear the GIF when injecting a VMEXIT to match what hardware does.
---
In v2: Adjusted for the preceding cleanup patch.
xen/arch/x86/hvm/svm/nestedsvm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +2 -2
@@ -717,9 +717,9 @@ nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs,struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;if ( vmcb->_vintr.fields.vgif_enable ) -ASSERT (vmcb->_vintr.fields.vgif ==0 ); + vmcb->_vintr.fields.vgif =0 ;else -ASSERT (svm->ns_gif ==0 ); + svm->ns_gif =0 ; ns_vmcb = nv->nv_vvmcx;
Signature · 1 line
2.53.0
Re: [PATCH v2 2/2] nestedsvm: Clear GIF when injecting VMEXIT
Quoted 8 lines · Ross Lagerwall
If L1 executes VMRUN with the GIF set and it fails consistency checks, Xen will inject a VMEXIT and fail the assert checking the GIF is cleared.
Instead, clear the GIF when injecting a VMEXIT to match what hardware does.
Fixes 9a779e4fc161 ("Implement SVM specific part for Nested Virtualization")Signed-off-by Ross Lagerwall <[email protected]>