Skip to contentSkip to conversation

nestedsvm: Don't set CR2 while injecting VMEXIT_EXCEPTION_PF

PATCH In review Ross Lagerwall 3 participants 6 messages Nov 13 – Nov 18

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nestedsvm: Don't set CR2 while injecting VMEXIT_EXCEPTION_PF

According to the APM, the #PF intercept is tested before CR2 is written so don't write CR2 while injecting VMEXIT_EXCEPTION_PF into L1. The faulting address is available to L1 in EXITINFO2.

Fixes 9a779e4fc161 ("Implement SVM specific part for Nested Virtualization")
---
 xen/arch/x86/hvm/svm/nestedsvm.c | 2 --
 1 file changed, 2 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +0 -2
index 81cae929dd05..366178ff54a7 100644
@@ -741,8 +741,6 @@ nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs,
                 ns_vmcb->exit_int_info = ns_vmcb->event_inj;
             break;
         case VMEXIT_EXCEPTION_PF:
-            ns_vmcb->_cr2 = ns_vmcb->ei.exc.cr2;
-            fallthrough;
         case VMEXIT_NPF:
             ns_vmcb->exitinfo2 = svm->ns_vmexit.exitinfo2;
             fallthrough;
Signature · 1 line
2.56.0
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Ross Lagerwall

According to the APM, the #PF intercept is tested before CR2 is written so don't write CR2 while injecting VMEXIT_EXCEPTION_PF into L1. The faulting address is available to L1 in EXITINFO2.

Fixes 9a779e4fc161 ("Implement SVM specific part for Nested Virtualization")

xen/arch/x86/hvm/svm/nestedsvm.c nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs, · line 744

Quoted 7 lines
xen/arch/x86/hvm/svm/nestedsvm.c +0 -1
@@ -741,8 +741,6 @@ nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs,
                 ns_vmcb->exit_int_info = ns_vmcb->event_inj;
             break;
         case VMEXIT_EXCEPTION_PF:
-            ns_vmcb->_cr2 = ns_vmcb->ei.exc.cr2;

Further to what you say in the description, isn't this also a stale value, as ...

xen/arch/x86/hvm/svm/nestedsvm.c nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs, · line 745

Quoted 3 lines
xen/arch/x86/hvm/svm/nestedsvm.c +0 -1
-            fallthrough;
         case VMEXIT_NPF:
             ns_vmcb->exitinfo2 = svm->ns_vmexit.exitinfo2;

... the correct one is written only here? If you agree, I'd be inclined to have the description say "... so don't write CR2 (with a stale value) while ..." (happy to adjust while committing).

Jan

#2 lore raw
Quoted 9 lines · Jan Beulich
Quoted 6 lines · Ross Lagerwall

According to the APM, the #PF intercept is tested before CR2 is written so don't write CR2 while injecting VMEXIT_EXCEPTION_PF into L1. The faulting address is available to L1 in EXITINFO2.

Fixes 9a779e4fc161 ("Implement SVM specific part for Nested Virtualization")

Thanks

Quoted 12 lines

xen/arch/x86/hvm/svm/nestedsvm.c nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs, · line 744

Quoted 8 lines
xen/arch/x86/hvm/svm/nestedsvm.c +0 -1
@@ -741,8 +741,6 @@ nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs,
                  ns_vmcb->exit_int_info = ns_vmcb->event_inj;
              break;
          case VMEXIT_EXCEPTION_PF:
-            ns_vmcb->_cr2 = ns_vmcb->ei.exc.cr2;

Further to what you say in the description, isn't this also a stale value, as ...

Quoted 7 lines

xen/arch/x86/hvm/svm/nestedsvm.c nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs, · line 745

Quoted 3 lines
xen/arch/x86/hvm/svm/nestedsvm.c +0 -1
-            fallthrough;
          case VMEXIT_NPF:
              ns_vmcb->exitinfo2 = svm->ns_vmexit.exitinfo2;

... the correct one is written only here? If you agree, I'd be inclined to have the description say "... so don't write CR2 (with a stale value) while ..." (happy to adjust while committing).

In most cases it wouldn't be stale because ei.exc.cr2 is the same as exitinfo2 and ns_vmcb->exitinfo2 is updated before this in prepare4vmexit(). However, I think it may be stale in some cases, e.g. if the #PF was raised by the L0 emulator.

Ross

#3 lore raw
Quoted 29 lines · Ross Lagerwall
Quoted 9 lines · Jan Beulich
Quoted 6 lines · Ross Lagerwall

According to the APM, the #PF intercept is tested before CR2 is written so don't write CR2 while injecting VMEXIT_EXCEPTION_PF into L1. The faulting address is available to L1 in EXITINFO2.

Fixes 9a779e4fc161 ("Implement SVM specific part for Nested Virtualization")

Thanks

Quoted 16 lines

xen/arch/x86/hvm/svm/nestedsvm.c nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs, · line 744

Quoted 8 lines
xen/arch/x86/hvm/svm/nestedsvm.c +0 -1
@@ -741,8 +741,6 @@ nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs,
                  ns_vmcb->exit_int_info = ns_vmcb->event_inj;
              break;
          case VMEXIT_EXCEPTION_PF:
-            ns_vmcb->_cr2 = ns_vmcb->ei.exc.cr2;

Further to what you say in the description, isn't this also a stale value, as ...

Quoted 3 lines
xen/arch/x86/hvm/svm/nestedsvm.c +0 -1
-            fallthrough;
          case VMEXIT_NPF:
              ns_vmcb->exitinfo2 = svm->ns_vmexit.exitinfo2;
Quoted 8 lines
Quoted 3 lines

... the correct one is written only here? If you agree, I'd be inclined to have the description say "... so don't write CR2 (with a stale value) while ..." (happy to adjust while committing).

In most cases it wouldn't be stale because ei.exc.cr2 is the same as exitinfo2 and ns_vmcb->exitinfo2 is updated before this in prepare4vmexit(). However, I think it may be stale in some cases, e.g. if the #PF was raised by the L0 emulator.

So further add "possibly" to what I suggested? (If ns_vmcb->exitinfo2 was always updated earlier on, the updating here could be avoided.)

Jan

#4 lore raw
Quoted 34 lines · Jan Beulich
Quoted 33 lines · Ross Lagerwall
Quoted 9 lines · Jan Beulich
Quoted 6 lines · Ross Lagerwall

According to the APM, the #PF intercept is tested before CR2 is written so don't write CR2 while injecting VMEXIT_EXCEPTION_PF into L1. The faulting address is available to L1 in EXITINFO2.

Fixes 9a779e4fc161 ("Implement SVM specific part for Nested Virtualization")

Thanks

Quoted 19 lines

xen/arch/x86/hvm/svm/nestedsvm.c nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs, · line 744

Quoted 8 lines
xen/arch/x86/hvm/svm/nestedsvm.c +0 -1
@@ -741,8 +741,6 @@ nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs,
                   ns_vmcb->exit_int_info = ns_vmcb->event_inj;
               break;
           case VMEXIT_EXCEPTION_PF:
-            ns_vmcb->_cr2 = ns_vmcb->ei.exc.cr2;

Further to what you say in the description, isn't this also a stale value, as ...

xen/arch/x86/hvm/svm/nestedsvm.c nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs, · line 745

Quoted 3 lines
xen/arch/x86/hvm/svm/nestedsvm.c +0 -1
-            fallthrough;
           case VMEXIT_NPF:
               ns_vmcb->exitinfo2 = svm->ns_vmexit.exitinfo2;

... the correct one is written only here? If you agree, I'd be inclined to have the description say "... so don't write CR2 (with a stale value) while ..." (happy to adjust while committing).

Quoted 7 lines
Quoted 4 lines

In most cases it wouldn't be stale because ei.exc.cr2 is the same as exitinfo2 and ns_vmcb->exitinfo2 is updated before this in prepare4vmexit(). However, I think it may be stale in some cases, e.g. if the #PF was raised by the L0 emulator.

So further add "possibly" to what I suggested? (If ns_vmcb->exitinfo2 was always updated earlier on, the updating here could be avoided.)

Yes, I agree with that adjustment.

Ross

Reviewed-by on v1 1/1
#5 lore raw
In reply to Ross Lagerwall
Quoted 15 lines · Ross Lagerwall

According to the APM, the #PF intercept is tested before CR2 is written so don't write CR2 while injecting VMEXIT_EXCEPTION_PF into L1. The faulting address is available to L1 in EXITINFO2.

Fixes 9a779e4fc161 ("Implement SVM specific part for Nested Virtualization")
---
 xen/arch/x86/hvm/svm/nestedsvm.c | 2 --
 1 file changed, 2 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +0 -0
index 81cae929dd05..366178ff54a7 100644

xen/arch/x86/hvm/svm/nestedsvm.c nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs, · line 746

Quoted 8 lines
xen/arch/x86/hvm/svm/nestedsvm.c +0 -2
@@ -741,8 +741,6 @@ nsvm_vcpu_vmexit_inject(struct vcpu *v, struct cpu_user_regs *regs,
                 ns_vmcb->exit_int_info = ns_vmcb->event_inj;
             break;
         case VMEXIT_EXCEPTION_PF:
-            ns_vmcb->_cr2 = ns_vmcb->ei.exc.cr2;
-            fallthrough;
         case VMEXIT_NPF:
             ns_vmcb->exitinfo2 = svm->ns_vmexit.exitinfo2;
             fallthrough;