Re: [PATCH v2 6/6] selftests: KVM: Add test case for MMIO during vectoring

Sean Christopherson
2024-12-11T18:19:42Z

On Mon, Nov 11, 2024, Ivan Orlov wrote:
> Extend the 'set_memory_region_test' with a test case which covers the
> MMIO during vectoring error handling. The test case
>
> 1) Sets an IDT descriptor base to point to an MMIO address
> 2) Generates a #GP in the guest
> 3) Verifies that we got a correct exit reason and suberror code
> 4) Verifies that we got a corrent reported GPA in internal.data[3]
>
> Also, add a definition of non-canonical address to processor.h
>
> Signed-off-by: Ivan Orlov <iorlov@amazon.com>
> ---
> V1 -> V2:
> - Get rid of pronouns, redundant comments and incorrect wording
> - Define noncanonical address in processor.h
> - Fix indentation and wrap lines at 80 columns
>
> .../selftests/kvm/include/x86_64/processor.h | 2 +
> .../selftests/kvm/set_memory_region_test.c | 51 +++++++++++++++++++
> 2 files changed, 53 insertions(+)
>
> diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h
> index 1a60c99b5833..997df5003edb 100644
> --- a/tools/testing/selftests/kvm/include/x86_64/processor.h
> +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h
> @@ -1165,6 +1165,8 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector,
> /* If a toddler were to say "abracadabra". */
> #define KVM_EXCEPTION_MAGIC 0xabacadabaULL
>
> +#define NONCANONICAL 0xaaaaaaaaaaaaaaaaull

Uber nit, I much prefer to place this definition at the top of the header. More
specifically, it needs to not be in the middle of the selftest's exception fixup
code.

> +
> /*
> * KVM selftest exception fixup uses registers to coordinate with the exception
> * handler, versus the kernel's in-memory tables and KVM-Unit-Tests's in-memory
> diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c
> index a1c53cc854a5..d65a9f20aa1a 100644
> --- a/tools/testing/selftests/kvm/set_memory_region_test.c
> +++ b/tools/testing/selftests/kvm/set_memory_region_test.c
> @@ -553,6 +553,56 @@ static void test_add_overlapping_private_memory_regions(void)
> close(memfd);
> kvm_vm_free(vm);
> }
> +
> +static void guest_code_mmio_during_vectoring(void)
> +{
> + const struct desc_ptr idt_desc = {
> + .address = MEM_REGION_GPA,
> + .size = 0xFFF,
> + };
> +
> + set_idt(&idt_desc);
> +
> + /* Generate a #GP by dereferencing a non-canonical address */
> + *((uint8_t *)NONCANONICAL) = 0x1;

Now I'm curious what happens if this uses vcpu_arch_put_guest(), i.e. if the
test forces KVM to emulate the write.

No action needed, the test is a-ok as-is. I'm really just curious :-)