[PATCH] nestedsvm: Don't set CR2 while injecting VMEXIT_EXCEPTION_PF

Ross Lagerwall posted 1 patch 1 week, 3 days ago
xen/arch/x86/hvm/svm/nestedsvm.c | 2 --
1 file changed, 2 deletions(-)
[PATCH] nestedsvm: Don't set CR2 while injecting VMEXIT_EXCEPTION_PF
Posted by Ross Lagerwall 1 week, 3 days ago
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")
Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
---
 xen/arch/x86/hvm/svm/nestedsvm.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/xen/arch/x86/hvm/svm/nestedsvm.c b/xen/arch/x86/hvm/svm/nestedsvm.c
index 81cae929dd05..366178ff54a7 100644
--- a/xen/arch/x86/hvm/svm/nestedsvm.c
+++ b/xen/arch/x86/hvm/svm/nestedsvm.c
@@ -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;
-- 
2.56.0
Re: [PATCH] nestedsvm: Don't set CR2 while injecting VMEXIT_EXCEPTION_PF
Posted by Andrew Cooper 5 days, 4 hours ago
On 13/11/2026 5:24 pm, Ross Lagerwall wrote:
> 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")
> Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
> ---
>  xen/arch/x86/hvm/svm/nestedsvm.c | 2 --
>  1 file changed, 2 deletions(-)
>
> diff --git a/xen/arch/x86/hvm/svm/nestedsvm.c b/xen/arch/x86/hvm/svm/nestedsvm.c
> index 81cae929dd05..366178ff54a7 100644
> --- a/xen/arch/x86/hvm/svm/nestedsvm.c
> +++ b/xen/arch/x86/hvm/svm/nestedsvm.c
> @@ -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;

Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
Re: [PATCH] nestedsvm: Don't set CR2 while injecting VMEXIT_EXCEPTION_PF
Posted by Jan Beulich 1 week ago
On 13.11.2026 18:24, Ross Lagerwall wrote:
> 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")
> Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>

Acked-by: Jan Beulich <jbeulich@suse.com>

> --- a/xen/arch/x86/hvm/svm/nestedsvm.c
> +++ b/xen/arch/x86/hvm/svm/nestedsvm.c
> @@ -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 ...

> -            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
Re: [PATCH] nestedsvm: Don't set CR2 while injecting VMEXIT_EXCEPTION_PF
Posted by Ross Lagerwall 1 week ago
On 11/16/26 11:42 AM, Jan Beulich wrote:
> On 13.11.2026 18:24, Ross Lagerwall wrote:
>> 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")
>> Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
> 
> Acked-by: Jan Beulich <jbeulich@suse.com>

Thanks

> 
>> --- a/xen/arch/x86/hvm/svm/nestedsvm.c
>> +++ b/xen/arch/x86/hvm/svm/nestedsvm.c
>> @@ -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 ...
> 
>> -            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
Re: [PATCH] nestedsvm: Don't set CR2 while injecting VMEXIT_EXCEPTION_PF
Posted by Jan Beulich 1 week ago
On 16.11.2026 13:05, Ross Lagerwall wrote:
> On 11/16/26 11:42 AM, Jan Beulich wrote:
>> On 13.11.2026 18:24, Ross Lagerwall wrote:
>>> 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")
>>> Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
>>
>> Acked-by: Jan Beulich <jbeulich@suse.com>
> 
> Thanks
> 
>>
>>> --- a/xen/arch/x86/hvm/svm/nestedsvm.c
>>> +++ b/xen/arch/x86/hvm/svm/nestedsvm.c
>>> @@ -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 ...
>>
>>> -            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.

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

Jan
Re: [PATCH] nestedsvm: Don't set CR2 while injecting VMEXIT_EXCEPTION_PF
Posted by Ross Lagerwall 1 week ago
On 11/16/26 12:23 PM, Jan Beulich wrote:
> On 16.11.2026 13:05, Ross Lagerwall wrote:
>> On 11/16/26 11:42 AM, Jan Beulich wrote:
>>> On 13.11.2026 18:24, Ross Lagerwall wrote:
>>>> 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")
>>>> Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
>>>
>>> Acked-by: Jan Beulich <jbeulich@suse.com>
>>
>> Thanks
>>
>>>
>>>> --- a/xen/arch/x86/hvm/svm/nestedsvm.c
>>>> +++ b/xen/arch/x86/hvm/svm/nestedsvm.c
>>>> @@ -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 ...
>>>
>>>> -            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.
> 
> 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