2022-11-05 05:33:48

by Vipin Sharma

[permalink] [raw]
Subject: [PATCH 0/6] Add Hyper-v extended hypercall support in KVM

This patch series adds Hyper-V extended hypercall support. All
hypercalls will exit to userspace if CPUID.0x40000003.EBX BIT(20) is
set.

Patch 4 and 5 are prep patches, they move some code to hyperv.h later
used by newly introduced test hyperv_extended_hcalls in Patch 6.

RFC: https://lore.kernel.org/lkml/[email protected]/

Vipin Sharma (6):
KVM: x86: hyper-v: Use common code for hypercall userspace exit
KVM: x86: hyper-v: Add extended hypercall support in Hyper-v
KVM: selftests: Test Hyper-V extended hypercall enablement
KVM: selftests: Make Hyper-V guest OS ID common
KVM: selftests: Move hypercall() to hyper.h
KVM: selftests: Test Hyper-V extended hypercall exit to userspace

arch/x86/kvm/hyperv.c | 43 +++++----
tools/testing/selftests/kvm/.gitignore | 1 +
tools/testing/selftests/kvm/Makefile | 1 +
.../selftests/kvm/include/x86_64/hyperv.h | 31 +++++++
.../selftests/kvm/x86_64/hyperv_clock.c | 2 +-
.../kvm/x86_64/hyperv_extended_hcalls.c | 90 +++++++++++++++++++
.../selftests/kvm/x86_64/hyperv_features.c | 32 +++----
.../selftests/kvm/x86_64/hyperv_svm_test.c | 2 +-
8 files changed, 163 insertions(+), 39 deletions(-)
create mode 100644 tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c

--
2.38.1.273.g43a17bfeac-goog



2022-11-05 05:34:12

by Vipin Sharma

[permalink] [raw]
Subject: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h

hypercall() can be used by other hyperv tests, move it to hyperv.h.

Signed-off-by: Vipin Sharma <[email protected]>
---
.../selftests/kvm/include/x86_64/hyperv.h | 17 +++++++++++++++++
.../selftests/kvm/x86_64/hyperv_features.c | 17 -----------------
2 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
index 9d8c325af1d9..87d8d9e444f7 100644
--- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
+++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
@@ -199,4 +199,21 @@ static inline uint64_t hv_linux_guest_id(void)
((uint64_t)LINUX_VERSION_CODE << 16);
}

+static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
+ vm_vaddr_t output_address, uint64_t *hv_status)
+{
+ uint8_t vector;
+
+ /* Note both the hypercall and the "asm safe" clobber r9-r11. */
+ asm volatile("mov %[output_address], %%r8\n\t"
+ KVM_ASM_SAFE("vmcall")
+ : "=a" (*hv_status),
+ "+c" (control), "+d" (input_address),
+ KVM_ASM_SAFE_OUTPUTS(vector)
+ : [output_address] "r"(output_address),
+ "a" (-EFAULT)
+ : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
+ return vector;
+}
+
#endif /* !SELFTEST_KVM_HYPERV_H */
diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
index b5a42cf1ad9d..31b22ee07dfb 100644
--- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
+++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
@@ -13,23 +13,6 @@
#include "processor.h"
#include "hyperv.h"

-static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
- vm_vaddr_t output_address, uint64_t *hv_status)
-{
- uint8_t vector;
-
- /* Note both the hypercall and the "asm safe" clobber r9-r11. */
- asm volatile("mov %[output_address], %%r8\n\t"
- KVM_ASM_SAFE("vmcall")
- : "=a" (*hv_status),
- "+c" (control), "+d" (input_address),
- KVM_ASM_SAFE_OUTPUTS(vector)
- : [output_address] "r"(output_address),
- "a" (-EFAULT)
- : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
- return vector;
-}
-
struct msr_data {
uint32_t idx;
bool available;
--
2.38.1.273.g43a17bfeac-goog


2022-11-07 18:43:29

by David Matlack

[permalink] [raw]
Subject: Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h

On Fri, Nov 04, 2022 at 09:57:03PM -0700, Vipin Sharma wrote:
> hypercall() can be used by other hyperv tests, move it to hyperv.h.
>
> Signed-off-by: Vipin Sharma <[email protected]>
> ---
> .../selftests/kvm/include/x86_64/hyperv.h | 17 +++++++++++++++++
> .../selftests/kvm/x86_64/hyperv_features.c | 17 -----------------
> 2 files changed, 17 insertions(+), 17 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> index 9d8c325af1d9..87d8d9e444f7 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> @@ -199,4 +199,21 @@ static inline uint64_t hv_linux_guest_id(void)
> ((uint64_t)LINUX_VERSION_CODE << 16);
> }
>
> +static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> + vm_vaddr_t output_address, uint64_t *hv_status)
> +{
> + uint8_t vector;
> +
> + /* Note both the hypercall and the "asm safe" clobber r9-r11. */
> + asm volatile("mov %[output_address], %%r8\n\t"
> + KVM_ASM_SAFE("vmcall")
> + : "=a" (*hv_status),
> + "+c" (control), "+d" (input_address),
> + KVM_ASM_SAFE_OUTPUTS(vector)
> + : [output_address] "r"(output_address),
> + "a" (-EFAULT)
> + : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> + return vector;
> +}

Since this function is Hyper-V specific it probably makes sense to
rename it to hyperv_hypercall() as part of moving it to library, e.g. to
differentiate it from kvm_hypercall().

> +
> #endif /* !SELFTEST_KVM_HYPERV_H */
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> index b5a42cf1ad9d..31b22ee07dfb 100644
> --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> @@ -13,23 +13,6 @@
> #include "processor.h"
> #include "hyperv.h"
>
> -static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> - vm_vaddr_t output_address, uint64_t *hv_status)
> -{
> - uint8_t vector;
> -
> - /* Note both the hypercall and the "asm safe" clobber r9-r11. */
> - asm volatile("mov %[output_address], %%r8\n\t"
> - KVM_ASM_SAFE("vmcall")
> - : "=a" (*hv_status),
> - "+c" (control), "+d" (input_address),
> - KVM_ASM_SAFE_OUTPUTS(vector)
> - : [output_address] "r"(output_address),
> - "a" (-EFAULT)
> - : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> - return vector;
> -}
> -
> struct msr_data {
> uint32_t idx;
> bool available;
> --
> 2.38.1.273.g43a17bfeac-goog
>

2022-11-08 02:07:39

by Vipin Sharma

[permalink] [raw]
Subject: Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h

On Mon, Nov 7, 2022 at 10:30 AM David Matlack <[email protected]> wrote:
>
> On Fri, Nov 04, 2022 at 09:57:03PM -0700, Vipin Sharma wrote:
> > hypercall() can be used by other hyperv tests, move it to hyperv.h.
> >
> > Signed-off-by: Vipin Sharma <[email protected]>
> > ---
> > .../selftests/kvm/include/x86_64/hyperv.h | 17 +++++++++++++++++
> > .../selftests/kvm/x86_64/hyperv_features.c | 17 -----------------
> > 2 files changed, 17 insertions(+), 17 deletions(-)
> >
> > diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > index 9d8c325af1d9..87d8d9e444f7 100644
> > --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > @@ -199,4 +199,21 @@ static inline uint64_t hv_linux_guest_id(void)
> > ((uint64_t)LINUX_VERSION_CODE << 16);
> > }
> >
> > +static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> > + vm_vaddr_t output_address, uint64_t *hv_status)
> > +{
> > + uint8_t vector;
> > +
> > + /* Note both the hypercall and the "asm safe" clobber r9-r11. */
> > + asm volatile("mov %[output_address], %%r8\n\t"
> > + KVM_ASM_SAFE("vmcall")
> > + : "=a" (*hv_status),
> > + "+c" (control), "+d" (input_address),
> > + KVM_ASM_SAFE_OUTPUTS(vector)
> > + : [output_address] "r"(output_address),
> > + "a" (-EFAULT)
> > + : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> > + return vector;
> > +}
>
> Since this function is Hyper-V specific it probably makes sense to
> rename it to hyperv_hypercall() as part of moving it to library, e.g. to
> differentiate it from kvm_hypercall().
>

Sounds good. Does it keeping it in header file "hyperv.h" seems fine
or should I create a new "hyperv.c" lib file and move function
definition there?

> > +
> > #endif /* !SELFTEST_KVM_HYPERV_H */
> > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > index b5a42cf1ad9d..31b22ee07dfb 100644
> > --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> > @@ -13,23 +13,6 @@
> > #include "processor.h"
> > #include "hyperv.h"
> >
> > -static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> > - vm_vaddr_t output_address, uint64_t *hv_status)
> > -{
> > - uint8_t vector;
> > -
> > - /* Note both the hypercall and the "asm safe" clobber r9-r11. */
> > - asm volatile("mov %[output_address], %%r8\n\t"
> > - KVM_ASM_SAFE("vmcall")
> > - : "=a" (*hv_status),
> > - "+c" (control), "+d" (input_address),
> > - KVM_ASM_SAFE_OUTPUTS(vector)
> > - : [output_address] "r"(output_address),
> > - "a" (-EFAULT)
> > - : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> > - return vector;
> > -}
> > -
> > struct msr_data {
> > uint32_t idx;
> > bool available;
> > --
> > 2.38.1.273.g43a17bfeac-goog
> >

2022-11-08 18:13:06

by David Matlack

[permalink] [raw]
Subject: Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h

On Mon, Nov 7, 2022 at 5:49 PM Vipin Sharma <[email protected]> wrote:
>
> On Mon, Nov 7, 2022 at 10:30 AM David Matlack <[email protected]> wrote:
> >
> > On Fri, Nov 04, 2022 at 09:57:03PM -0700, Vipin Sharma wrote:
> > > hypercall() can be used by other hyperv tests, move it to hyperv.h.
> > >
> > > Signed-off-by: Vipin Sharma <[email protected]>
> > > ---
> > > .../selftests/kvm/include/x86_64/hyperv.h | 17 +++++++++++++++++
> > > .../selftests/kvm/x86_64/hyperv_features.c | 17 -----------------
> > > 2 files changed, 17 insertions(+), 17 deletions(-)
> > >
> > > diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > > index 9d8c325af1d9..87d8d9e444f7 100644
> > > --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > > +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> > > @@ -199,4 +199,21 @@ static inline uint64_t hv_linux_guest_id(void)
> > > ((uint64_t)LINUX_VERSION_CODE << 16);
> > > }
> > >
> > > +static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> > > + vm_vaddr_t output_address, uint64_t *hv_status)
> > > +{
> > > + uint8_t vector;
> > > +
> > > + /* Note both the hypercall and the "asm safe" clobber r9-r11. */
> > > + asm volatile("mov %[output_address], %%r8\n\t"
> > > + KVM_ASM_SAFE("vmcall")
> > > + : "=a" (*hv_status),
> > > + "+c" (control), "+d" (input_address),
> > > + KVM_ASM_SAFE_OUTPUTS(vector)
> > > + : [output_address] "r"(output_address),
> > > + "a" (-EFAULT)
> > > + : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> > > + return vector;
> > > +}
> >
> > Since this function is Hyper-V specific it probably makes sense to
> > rename it to hyperv_hypercall() as part of moving it to library, e.g. to
> > differentiate it from kvm_hypercall().
> >
>
> Sounds good. Does it keeping it in header file "hyperv.h" seems fine
> or should I create a new "hyperv.c" lib file and move function
> definition there?

I think it's fine to keep in hyperv.h. It seems like the type of
function we'd want to be inlined anyway, and the implementation is
short.

2022-11-09 14:38:13

by Vitaly Kuznetsov

[permalink] [raw]
Subject: Re: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h

Vipin Sharma <[email protected]> writes:

> hypercall() can be used by other hyperv tests, move it to hyperv.h.
>

A similar patch is pending in my TLB flush series:

https://lore.kernel.org/kvm/[email protected]/

> Signed-off-by: Vipin Sharma <[email protected]>
> ---
> .../selftests/kvm/include/x86_64/hyperv.h | 17 +++++++++++++++++
> .../selftests/kvm/x86_64/hyperv_features.c | 17 -----------------
> 2 files changed, 17 insertions(+), 17 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> index 9d8c325af1d9..87d8d9e444f7 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h
> @@ -199,4 +199,21 @@ static inline uint64_t hv_linux_guest_id(void)
> ((uint64_t)LINUX_VERSION_CODE << 16);
> }
>
> +static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> + vm_vaddr_t output_address, uint64_t *hv_status)
> +{
> + uint8_t vector;
> +
> + /* Note both the hypercall and the "asm safe" clobber r9-r11. */
> + asm volatile("mov %[output_address], %%r8\n\t"
> + KVM_ASM_SAFE("vmcall")
> + : "=a" (*hv_status),
> + "+c" (control), "+d" (input_address),
> + KVM_ASM_SAFE_OUTPUTS(vector)
> + : [output_address] "r"(output_address),
> + "a" (-EFAULT)
> + : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> + return vector;
> +}
> +
> #endif /* !SELFTEST_KVM_HYPERV_H */
> diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> index b5a42cf1ad9d..31b22ee07dfb 100644
> --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c
> @@ -13,23 +13,6 @@
> #include "processor.h"
> #include "hyperv.h"
>
> -static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address,
> - vm_vaddr_t output_address, uint64_t *hv_status)
> -{
> - uint8_t vector;
> -
> - /* Note both the hypercall and the "asm safe" clobber r9-r11. */
> - asm volatile("mov %[output_address], %%r8\n\t"
> - KVM_ASM_SAFE("vmcall")
> - : "=a" (*hv_status),
> - "+c" (control), "+d" (input_address),
> - KVM_ASM_SAFE_OUTPUTS(vector)
> - : [output_address] "r"(output_address),
> - "a" (-EFAULT)
> - : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS);
> - return vector;
> -}
> -
> struct msr_data {
> uint32_t idx;
> bool available;

--
Vitaly