[PATCH v2 3/4] KVM: selftests: Add support for posted interrupt handling in L2

Mingwei Zhang posted 4 patches 3 years, 7 months ago
[PATCH v2 3/4] KVM: selftests: Add support for posted interrupt handling in L2
Posted by Mingwei Zhang 3 years, 7 months ago
Add support for posted interrupt handling in L2. This is done by adding
needed data structures in vmx_pages and APIs to allow an L2 receive posted
interrupts.

Cc: Jim Mattson <jmattson@google.com>
Signed-off-by: Mingwei Zhang <mizhang@google.com>
---
 tools/testing/selftests/kvm/include/x86_64/vmx.h | 10 ++++++++++
 tools/testing/selftests/kvm/lib/x86_64/vmx.c     | 14 ++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/tools/testing/selftests/kvm/include/x86_64/vmx.h b/tools/testing/selftests/kvm/include/x86_64/vmx.h
index 99fa1410964c..69784fc71bce 100644
--- a/tools/testing/selftests/kvm/include/x86_64/vmx.h
+++ b/tools/testing/selftests/kvm/include/x86_64/vmx.h
@@ -577,6 +577,14 @@ struct vmx_pages {
 	void *apic_access_hva;
 	uint64_t apic_access_gpa;
 	void *apic_access;
+
+	void *virtual_apic_hva;
+	uint64_t virtual_apic_gpa;
+	void *virtual_apic;
+
+	void *posted_intr_desc_hva;
+	uint64_t posted_intr_desc_gpa;
+	void *posted_intr_desc;
 };
 
 union vmx_basic {
@@ -620,5 +628,7 @@ void nested_identity_map_1g(struct vmx_pages *vmx, struct kvm_vm *vm,
 void prepare_eptp(struct vmx_pages *vmx, struct kvm_vm *vm,
 		  uint32_t eptp_memslot);
 void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm);
+void prepare_virtual_apic(struct vmx_pages *vmx, struct kvm_vm *vm);
+void prepare_posted_intr_desc(struct vmx_pages *vmx, struct kvm_vm *vm);
 
 #endif /* SELFTEST_KVM_VMX_H */
diff --git a/tools/testing/selftests/kvm/lib/x86_64/vmx.c b/tools/testing/selftests/kvm/lib/x86_64/vmx.c
index 80a568c439b8..47ae419d7eb1 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/vmx.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/vmx.c
@@ -556,3 +556,17 @@ void prepare_virtualize_apic_accesses(struct vmx_pages *vmx, struct kvm_vm *vm)
 	vmx->apic_access_hva = addr_gva2hva(vm, (uintptr_t)vmx->apic_access);
 	vmx->apic_access_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->apic_access);
 }
+
+void prepare_virtual_apic(struct vmx_pages *vmx, struct kvm_vm *vm)
+{
+	vmx->virtual_apic = (void *)vm_vaddr_alloc_page(vm);
+	vmx->virtual_apic_hva = addr_gva2hva(vm, (uintptr_t)vmx->virtual_apic);
+	vmx->virtual_apic_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->virtual_apic);
+}
+
+void prepare_posted_intr_desc(struct vmx_pages *vmx, struct kvm_vm *vm)
+{
+	vmx->posted_intr_desc = (void *)vm_vaddr_alloc_page(vm);
+	vmx->posted_intr_desc_hva = addr_gva2hva(vm, vmx->posted_intr_desc);
+	vmx->posted_intr_desc_gpa = addr_gva2gpa(vm, vmx->posted_intr_desc);
+}
-- 
2.37.2.672.g94769d06f0-goog
Re: [PATCH v2 3/4] KVM: selftests: Add support for posted interrupt handling in L2
Posted by Sean Christopherson 3 years, 7 months ago
On Sun, Aug 28, 2022, Mingwei Zhang wrote:
> Add support for posted interrupt handling in L2. This is done by adding
> needed data structures in vmx_pages and APIs to allow an L2 receive posted
> interrupts.
> 
> Cc: Jim Mattson <jmattson@google.com>
> Signed-off-by: Mingwei Zhang <mizhang@google.com>
> ---
>  tools/testing/selftests/kvm/include/x86_64/vmx.h | 10 ++++++++++
>  tools/testing/selftests/kvm/lib/x86_64/vmx.c     | 14 ++++++++++++++
>  2 files changed, 24 insertions(+)
> 
> diff --git a/tools/testing/selftests/kvm/include/x86_64/vmx.h b/tools/testing/selftests/kvm/include/x86_64/vmx.h
> index 99fa1410964c..69784fc71bce 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/vmx.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/vmx.h
> @@ -577,6 +577,14 @@ struct vmx_pages {
>  	void *apic_access_hva;
>  	uint64_t apic_access_gpa;
>  	void *apic_access;
> +
> +	void *virtual_apic_hva;
> +	uint64_t virtual_apic_gpa;
> +	void *virtual_apic;
> +
> +	void *posted_intr_desc_hva;
> +	uint64_t posted_intr_desc_gpa;
> +	void *posted_intr_desc;

Can you add a prep patch to dedup the absurd amount of copy-paste code related to
vmx_pages?

I.e. take this and give all the other triplets the same treatment.

---
 tools/testing/selftests/kvm/include/x86_64/vmx.h | 10 +++++++---
 tools/testing/selftests/kvm/lib/x86_64/vmx.c     | 15 ++++++++++-----
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/x86_64/vmx.h b/tools/testing/selftests/kvm/include/x86_64/vmx.h
index 99fa1410964c..ecc66d65acc1 100644
--- a/tools/testing/selftests/kvm/include/x86_64/vmx.h
+++ b/tools/testing/selftests/kvm/include/x86_64/vmx.h
@@ -537,10 +537,14 @@ static inline uint32_t vmcs_revision(void)
 	return rdmsr(MSR_IA32_VMX_BASIC);
 }

+struct vmx_page {
+	void *gva;
+	uint64_t gpa;
+	void *hva;
+};
+
 struct vmx_pages {
-	void *vmxon_hva;
-	uint64_t vmxon_gpa;
-	void *vmxon;
+	struct vmx_page vmxon;

 	void *vmcs_hva;
 	uint64_t vmcs_gpa;
diff --git a/tools/testing/selftests/kvm/lib/x86_64/vmx.c b/tools/testing/selftests/kvm/lib/x86_64/vmx.c
index 80a568c439b8..e4eeab85741a 100644
--- a/tools/testing/selftests/kvm/lib/x86_64/vmx.c
+++ b/tools/testing/selftests/kvm/lib/x86_64/vmx.c
@@ -58,6 +58,13 @@ int vcpu_enable_evmcs(struct kvm_vcpu *vcpu)
 	return evmcs_ver;
 }

+static void vcpu_alloc_vmx_page(struct kvm_vm *vm, struct vmx_page *page)
+{
+	page->gva = (void *)vm_vaddr_alloc_page(vm);
+	page->hva = addr_gva2hva(vm, (uintptr_t)page->gva);
+	page->gpa = addr_gva2gpa(vm, (uintptr_t)page->gva);
+}
+
 /* Allocate memory regions for nested VMX tests.
  *
  * Input Args:
@@ -76,9 +83,7 @@ vcpu_alloc_vmx(struct kvm_vm *vm, vm_vaddr_t *p_vmx_gva)
 	struct vmx_pages *vmx = addr_gva2hva(vm, vmx_gva);

 	/* Setup of a region of guest memory for the vmxon region. */
-	vmx->vmxon = (void *)vm_vaddr_alloc_page(vm);
-	vmx->vmxon_hva = addr_gva2hva(vm, (uintptr_t)vmx->vmxon);
-	vmx->vmxon_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->vmxon);
+	vcpu_alloc_vmx_page(vm, &vmx->vmxon);

 	/* Setup of a region of guest memory for a vmcs. */
 	vmx->vmcs = (void *)vm_vaddr_alloc_page(vm);
@@ -160,8 +165,8 @@ bool prepare_for_vmx_operation(struct vmx_pages *vmx)
 		wrmsr(MSR_IA32_FEAT_CTL, feature_control | required);

 	/* Enter VMX root operation. */
-	*(uint32_t *)(vmx->vmxon) = vmcs_revision();
-	if (vmxon(vmx->vmxon_gpa))
+	*(uint32_t *)(vmx->vmxon.gva) = vmcs_revision();
+	if (vmxon(vmx->vmxon.gpa))
 		return false;

 	return true;

base-commit: 372d07084593dc7a399bf9bee815711b1fb1bcf2
--
Re: [PATCH v2 3/4] KVM: selftests: Add support for posted interrupt handling in L2
Posted by Mingwei Zhang 3 years, 7 months ago
On Mon, Aug 29, 2022, Sean Christopherson wrote:
> On Sun, Aug 28, 2022, Mingwei Zhang wrote:
> > Add support for posted interrupt handling in L2. This is done by adding
> > needed data structures in vmx_pages and APIs to allow an L2 receive posted
> > interrupts.
> > 
> > Cc: Jim Mattson <jmattson@google.com>
> > Signed-off-by: Mingwei Zhang <mizhang@google.com>
> > ---
> >  tools/testing/selftests/kvm/include/x86_64/vmx.h | 10 ++++++++++
> >  tools/testing/selftests/kvm/lib/x86_64/vmx.c     | 14 ++++++++++++++
> >  2 files changed, 24 insertions(+)
> > 
> > diff --git a/tools/testing/selftests/kvm/include/x86_64/vmx.h b/tools/testing/selftests/kvm/include/x86_64/vmx.h
> > index 99fa1410964c..69784fc71bce 100644
> > --- a/tools/testing/selftests/kvm/include/x86_64/vmx.h
> > +++ b/tools/testing/selftests/kvm/include/x86_64/vmx.h
> > @@ -577,6 +577,14 @@ struct vmx_pages {
> >  	void *apic_access_hva;
> >  	uint64_t apic_access_gpa;
> >  	void *apic_access;
> > +
> > +	void *virtual_apic_hva;
> > +	uint64_t virtual_apic_gpa;
> > +	void *virtual_apic;
> > +
> > +	void *posted_intr_desc_hva;
> > +	uint64_t posted_intr_desc_gpa;
> > +	void *posted_intr_desc;
> 
> Can you add a prep patch to dedup the absurd amount of copy-paste code related to
> vmx_pages?
> 
> I.e. take this and give all the other triplets the same treatment.

Will do.

> 
> ---
>  tools/testing/selftests/kvm/include/x86_64/vmx.h | 10 +++++++---
>  tools/testing/selftests/kvm/lib/x86_64/vmx.c     | 15 ++++++++++-----
>  2 files changed, 17 insertions(+), 8 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/include/x86_64/vmx.h b/tools/testing/selftests/kvm/include/x86_64/vmx.h
> index 99fa1410964c..ecc66d65acc1 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/vmx.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/vmx.h
> @@ -537,10 +537,14 @@ static inline uint32_t vmcs_revision(void)
>  	return rdmsr(MSR_IA32_VMX_BASIC);
>  }
> 
> +struct vmx_page {
> +	void *gva;
> +	uint64_t gpa;
> +	void *hva;
> +};
> +
>  struct vmx_pages {
> -	void *vmxon_hva;
> -	uint64_t vmxon_gpa;
> -	void *vmxon;
> +	struct vmx_page vmxon;
> 
>  	void *vmcs_hva;
>  	uint64_t vmcs_gpa;
> diff --git a/tools/testing/selftests/kvm/lib/x86_64/vmx.c b/tools/testing/selftests/kvm/lib/x86_64/vmx.c
> index 80a568c439b8..e4eeab85741a 100644
> --- a/tools/testing/selftests/kvm/lib/x86_64/vmx.c
> +++ b/tools/testing/selftests/kvm/lib/x86_64/vmx.c
> @@ -58,6 +58,13 @@ int vcpu_enable_evmcs(struct kvm_vcpu *vcpu)
>  	return evmcs_ver;
>  }
> 
> +static void vcpu_alloc_vmx_page(struct kvm_vm *vm, struct vmx_page *page)
> +{
> +	page->gva = (void *)vm_vaddr_alloc_page(vm);
> +	page->hva = addr_gva2hva(vm, (uintptr_t)page->gva);
> +	page->gpa = addr_gva2gpa(vm, (uintptr_t)page->gva);
> +}
> +
>  /* Allocate memory regions for nested VMX tests.
>   *
>   * Input Args:
> @@ -76,9 +83,7 @@ vcpu_alloc_vmx(struct kvm_vm *vm, vm_vaddr_t *p_vmx_gva)
>  	struct vmx_pages *vmx = addr_gva2hva(vm, vmx_gva);
> 
>  	/* Setup of a region of guest memory for the vmxon region. */
> -	vmx->vmxon = (void *)vm_vaddr_alloc_page(vm);
> -	vmx->vmxon_hva = addr_gva2hva(vm, (uintptr_t)vmx->vmxon);
> -	vmx->vmxon_gpa = addr_gva2gpa(vm, (uintptr_t)vmx->vmxon);
> +	vcpu_alloc_vmx_page(vm, &vmx->vmxon);
> 
>  	/* Setup of a region of guest memory for a vmcs. */
>  	vmx->vmcs = (void *)vm_vaddr_alloc_page(vm);
> @@ -160,8 +165,8 @@ bool prepare_for_vmx_operation(struct vmx_pages *vmx)
>  		wrmsr(MSR_IA32_FEAT_CTL, feature_control | required);
> 
>  	/* Enter VMX root operation. */
> -	*(uint32_t *)(vmx->vmxon) = vmcs_revision();
> -	if (vmxon(vmx->vmxon_gpa))
> +	*(uint32_t *)(vmx->vmxon.gva) = vmcs_revision();
> +	if (vmxon(vmx->vmxon.gpa))
>  		return false;
> 
>  	return true;
> 
> base-commit: 372d07084593dc7a399bf9bee815711b1fb1bcf2
> --
>