2022-03-04 19:59:55

by Isaku Yamahata

[permalink] [raw]
Subject: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function

From: Sean Christopherson <[email protected]>

By necessity, TDX will use a different register ABI for hypercalls.
Break out the core functionality so that it may be reused for TDX.

Signed-off-by: Sean Christopherson <[email protected]>
Signed-off-by: Isaku Yamahata <[email protected]>
---
arch/x86/include/asm/kvm_host.h | 4 +++
arch/x86/kvm/x86.c | 54 ++++++++++++++++++++-------------
2 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 8dab9f16f559..33b75b0e3de1 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1818,6 +1818,10 @@ void kvm_request_apicv_update(struct kvm *kvm, bool activate,
void __kvm_request_apicv_update(struct kvm *kvm, bool activate,
unsigned long bit);

+unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
+ unsigned long a0, unsigned long a1,
+ unsigned long a2, unsigned long a3,
+ int op_64_bit);
int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);

int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 314ae43e07bf..9acb33a17445 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -9090,26 +9090,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)
return kvm_skip_emulated_instruction(vcpu);
}

-int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
+unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
+ unsigned long a0, unsigned long a1,
+ unsigned long a2, unsigned long a3,
+ int op_64_bit)
{
- unsigned long nr, a0, a1, a2, a3, ret;
- int op_64_bit;
-
- if (kvm_xen_hypercall_enabled(vcpu->kvm))
- return kvm_xen_hypercall(vcpu);
-
- if (kvm_hv_hypercall_enabled(vcpu))
- return kvm_hv_hypercall(vcpu);
-
- nr = kvm_rax_read(vcpu);
- a0 = kvm_rbx_read(vcpu);
- a1 = kvm_rcx_read(vcpu);
- a2 = kvm_rdx_read(vcpu);
- a3 = kvm_rsi_read(vcpu);
+ unsigned long ret;

trace_kvm_hypercall(nr, a0, a1, a2, a3);

- op_64_bit = is_64_bit_hypercall(vcpu);
if (!op_64_bit) {
nr &= 0xFFFFFFFF;
a0 &= 0xFFFFFFFF;
@@ -9118,11 +9107,6 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
a3 &= 0xFFFFFFFF;
}

- if (static_call(kvm_x86_get_cpl)(vcpu) != 0) {
- ret = -KVM_EPERM;
- goto out;
- }
-
ret = -KVM_ENOSYS;

switch (nr) {
@@ -9181,6 +9165,34 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
ret = -KVM_ENOSYS;
break;
}
+ return ret;
+}
+EXPORT_SYMBOL_GPL(__kvm_emulate_hypercall);
+
+int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
+{
+ unsigned long nr, a0, a1, a2, a3, ret;
+ int op_64_bit;
+
+ if (kvm_xen_hypercall_enabled(vcpu->kvm))
+ return kvm_xen_hypercall(vcpu);
+
+ if (kvm_hv_hypercall_enabled(vcpu))
+ return kvm_hv_hypercall(vcpu);
+
+ nr = kvm_rax_read(vcpu);
+ a0 = kvm_rbx_read(vcpu);
+ a1 = kvm_rcx_read(vcpu);
+ a2 = kvm_rdx_read(vcpu);
+ a3 = kvm_rsi_read(vcpu);
+ op_64_bit = is_64_bit_mode(vcpu);
+
+ if (static_call(kvm_x86_get_cpl)(vcpu) != 0) {
+ ret = -KVM_EPERM;
+ goto out;
+ }
+
+ ret = __kvm_emulate_hypercall(vcpu, nr, a0, a1, a2, a3, op_64_bit);
out:
if (!op_64_bit)
ret = (u32)ret;
--
2.25.1


2022-03-21 21:06:09

by Sagi Shahar

[permalink] [raw]
Subject: Re: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function

On Fri, Mar 4, 2022 at 12:00 PM <[email protected]> wrote:
>
> From: Sean Christopherson <[email protected]>
>
> By necessity, TDX will use a different register ABI for hypercalls.
> Break out the core functionality so that it may be reused for TDX.
>
> Signed-off-by: Sean Christopherson <[email protected]>
> Signed-off-by: Isaku Yamahata <[email protected]>
> ---
> arch/x86/include/asm/kvm_host.h | 4 +++
> arch/x86/kvm/x86.c | 54 ++++++++++++++++++++-------------
> 2 files changed, 37 insertions(+), 21 deletions(-)
>
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index 8dab9f16f559..33b75b0e3de1 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -1818,6 +1818,10 @@ void kvm_request_apicv_update(struct kvm *kvm, bool activate,
> void __kvm_request_apicv_update(struct kvm *kvm, bool activate,
> unsigned long bit);
>
> +unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
> + unsigned long a0, unsigned long a1,
> + unsigned long a2, unsigned long a3,
> + int op_64_bit);
> int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
>
> int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 314ae43e07bf..9acb33a17445 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -9090,26 +9090,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)
> return kvm_skip_emulated_instruction(vcpu);
> }
>
> -int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
> +unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
> + unsigned long a0, unsigned long a1,
> + unsigned long a2, unsigned long a3,
> + int op_64_bit)
> {
> - unsigned long nr, a0, a1, a2, a3, ret;
> - int op_64_bit;
> -
> - if (kvm_xen_hypercall_enabled(vcpu->kvm))
> - return kvm_xen_hypercall(vcpu);
> -
> - if (kvm_hv_hypercall_enabled(vcpu))
> - return kvm_hv_hypercall(vcpu);
> -
> - nr = kvm_rax_read(vcpu);
> - a0 = kvm_rbx_read(vcpu);
> - a1 = kvm_rcx_read(vcpu);
> - a2 = kvm_rdx_read(vcpu);
> - a3 = kvm_rsi_read(vcpu);
> + unsigned long ret;
>
> trace_kvm_hypercall(nr, a0, a1, a2, a3);
>
> - op_64_bit = is_64_bit_hypercall(vcpu);
> if (!op_64_bit) {
> nr &= 0xFFFFFFFF;
> a0 &= 0xFFFFFFFF;
> @@ -9118,11 +9107,6 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
> a3 &= 0xFFFFFFFF;
> }
>
> - if (static_call(kvm_x86_get_cpl)(vcpu) != 0) {
> - ret = -KVM_EPERM;
> - goto out;
> - }
> -
> ret = -KVM_ENOSYS;
>
> switch (nr) {
> @@ -9181,6 +9165,34 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
> ret = -KVM_ENOSYS;
> break;
> }
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(__kvm_emulate_hypercall);
> +
> +int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
> +{
> + unsigned long nr, a0, a1, a2, a3, ret;
> + int op_64_bit;
> +
> + if (kvm_xen_hypercall_enabled(vcpu->kvm))
> + return kvm_xen_hypercall(vcpu);
> +
> + if (kvm_hv_hypercall_enabled(vcpu))
> + return kvm_hv_hypercall(vcpu);
> +
> + nr = kvm_rax_read(vcpu);
> + a0 = kvm_rbx_read(vcpu);
> + a1 = kvm_rcx_read(vcpu);
> + a2 = kvm_rdx_read(vcpu);
> + a3 = kvm_rsi_read(vcpu);
> + op_64_bit = is_64_bit_mode(vcpu);

I think this should be "op_64_bit = is_64_bit_hypercall(vcpu);"
is_64_bit_mode was replaced with is_64_bit_hypercall to support
protected guests here:
https://lore.kernel.org/all/[email protected]/T/

Without it, op_64_bit will be set to 0 for TD VMs which will cause the
upper 32 bit of the registers to be cleared in __kvm_emulate_hypercall

> +
> + if (static_call(kvm_x86_get_cpl)(vcpu) != 0) {
> + ret = -KVM_EPERM;
> + goto out;
> + }
> +
> + ret = __kvm_emulate_hypercall(vcpu, nr, a0, a1, a2, a3, op_64_bit);
> out:
> if (!op_64_bit)
> ret = (u32)ret;
> --
> 2.25.1
>

Sagi

2022-03-24 23:34:46

by Isaku Yamahata

[permalink] [raw]
Subject: Re: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function

On Mon, Mar 21, 2022 at 11:32:21AM -0700,
Sagi Shahar <[email protected]> wrote:

> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index 314ae43e07bf..9acb33a17445 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -9090,26 +9090,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)
> > return kvm_skip_emulated_instruction(vcpu);
> > }
> >
> > -int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
> > +unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
> > + unsigned long a0, unsigned long a1,
> > + unsigned long a2, unsigned long a3,
> > + int op_64_bit)
> > {
> > - unsigned long nr, a0, a1, a2, a3, ret;
> > - int op_64_bit;
> > -
> > - if (kvm_xen_hypercall_enabled(vcpu->kvm))
> > - return kvm_xen_hypercall(vcpu);
> > -
> > - if (kvm_hv_hypercall_enabled(vcpu))
> > - return kvm_hv_hypercall(vcpu);
> > -
> > - nr = kvm_rax_read(vcpu);
> > - a0 = kvm_rbx_read(vcpu);
> > - a1 = kvm_rcx_read(vcpu);
> > - a2 = kvm_rdx_read(vcpu);
> > - a3 = kvm_rsi_read(vcpu);
> > + unsigned long ret;
> >
> > trace_kvm_hypercall(nr, a0, a1, a2, a3);
> >
> > - op_64_bit = is_64_bit_hypercall(vcpu);
> > if (!op_64_bit) {
> > nr &= 0xFFFFFFFF;
> > a0 &= 0xFFFFFFFF;
> > @@ -9118,11 +9107,6 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
> > a3 &= 0xFFFFFFFF;
> > }
> >
> > - if (static_call(kvm_x86_get_cpl)(vcpu) != 0) {
> > - ret = -KVM_EPERM;
> > - goto out;
> > - }
> > -
> > ret = -KVM_ENOSYS;
> >
> > switch (nr) {
> > @@ -9181,6 +9165,34 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
> > ret = -KVM_ENOSYS;
> > break;
> > }
> > + return ret;
> > +}
> > +EXPORT_SYMBOL_GPL(__kvm_emulate_hypercall);
> > +
> > +int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
> > +{
> > + unsigned long nr, a0, a1, a2, a3, ret;
> > + int op_64_bit;
> > +
> > + if (kvm_xen_hypercall_enabled(vcpu->kvm))
> > + return kvm_xen_hypercall(vcpu);
> > +
> > + if (kvm_hv_hypercall_enabled(vcpu))
> > + return kvm_hv_hypercall(vcpu);
> > +
> > + nr = kvm_rax_read(vcpu);
> > + a0 = kvm_rbx_read(vcpu);
> > + a1 = kvm_rcx_read(vcpu);
> > + a2 = kvm_rdx_read(vcpu);
> > + a3 = kvm_rsi_read(vcpu);
> > + op_64_bit = is_64_bit_mode(vcpu);
>
> I think this should be "op_64_bit = is_64_bit_hypercall(vcpu);"
> is_64_bit_mode was replaced with is_64_bit_hypercall to support
> protected guests here:
> https://lore.kernel.org/all/[email protected]/T/
>
> Without it, op_64_bit will be set to 0 for TD VMs which will cause the
> upper 32 bit of the registers to be cleared in __kvm_emulate_hypercall

Oops, thanks for pointing it out. I'll fix it up with next respin.
--
Isaku Yamahata <[email protected]>

2022-04-07 21:25:09

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function

On 3/21/22 19:32, Sagi Shahar wrote:
> On Fri, Mar 4, 2022 at 12:00 PM <[email protected]> wrote:
>>
>> From: Sean Christopherson <[email protected]>
>>
>> By necessity, TDX will use a different register ABI for hypercalls.
>> Break out the core functionality so that it may be reused for TDX.
>>
>> Signed-off-by: Sean Christopherson <[email protected]>
>> Signed-off-by: Isaku Yamahata <[email protected]>
>> ---
>> arch/x86/include/asm/kvm_host.h | 4 +++
>> arch/x86/kvm/x86.c | 54 ++++++++++++++++++++-------------
>> 2 files changed, 37 insertions(+), 21 deletions(-)
>>
>> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
>> index 8dab9f16f559..33b75b0e3de1 100644
>> --- a/arch/x86/include/asm/kvm_host.h
>> +++ b/arch/x86/include/asm/kvm_host.h
>> @@ -1818,6 +1818,10 @@ void kvm_request_apicv_update(struct kvm *kvm, bool activate,
>> void __kvm_request_apicv_update(struct kvm *kvm, bool activate,
>> unsigned long bit);
>>
>> +unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
>> + unsigned long a0, unsigned long a1,
>> + unsigned long a2, unsigned long a3,
>> + int op_64_bit);
>> int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
>>
>> int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>> index 314ae43e07bf..9acb33a17445 100644
>> --- a/arch/x86/kvm/x86.c
>> +++ b/arch/x86/kvm/x86.c
>> @@ -9090,26 +9090,15 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)
>> return kvm_skip_emulated_instruction(vcpu);
>> }
>>
>> -int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
>> +unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
>> + unsigned long a0, unsigned long a1,
>> + unsigned long a2, unsigned long a3,
>> + int op_64_bit)
>> {
>> - unsigned long nr, a0, a1, a2, a3, ret;
>> - int op_64_bit;
>> -
>> - if (kvm_xen_hypercall_enabled(vcpu->kvm))
>> - return kvm_xen_hypercall(vcpu);
>> -
>> - if (kvm_hv_hypercall_enabled(vcpu))
>> - return kvm_hv_hypercall(vcpu);

Please keep Xen and Hyper-V hypercalls to kvm_emulate_hypercall (more on
this in the reply to patch 89). __kvm_emulate_hypercall should only
handle KVM hypercalls.

>> + if (static_call(kvm_x86_get_cpl)(vcpu) != 0) {
>> + ret = -KVM_EPERM;
>> + goto out;
>> + }

Is this guaranteed by TDG.VP.VMCALL?

Paolo

>> + ret = __kvm_emulate_hypercall(vcpu, nr, a0, a1, a2, a3, op_64_bit);
>> out:
>> if (!op_64_bit)
>> ret = (u32)ret;
>> --
>> 2.25.1
>>
>
> Sagi
>

2022-04-08 06:05:31

by Isaku Yamahata

[permalink] [raw]
Subject: Re: [RFC PATCH v5 083/104] KVM: x86: Split core of hypercall emulation to helper function

On Thu, Apr 07, 2022 at 03:12:57PM +0200,
Paolo Bonzini <[email protected]> wrote:

> > > + if (static_call(kvm_x86_get_cpl)(vcpu) != 0) {
> > > + ret = -KVM_EPERM;
> > > + goto out;
> > > + }
>
> Is this guaranteed by TDG.VP.VMCALL?

Yes. TDCALL instruction in TD results in #GP(0) if CPL > 0.
It's documented in trust domain CPU architectural extensions spec.
https://www.intel.com/content/dam/develop/external/us/en/documents-tps/intel-tdx-cpu-architectural-specification.pdf

Anyway VMM can't know TD guest CPL (or other CPU state).
--
Isaku Yamahata <[email protected]>