selftests: kvm: Mmap the entire vcpu mmap area
PATCH v2 1/1 selftests: kvm: Mmap the entire vcpu mmap area
The vcpu mmap area may consist of more than just the kvm_run struct. Allocate enough space for the entire vcpu mmap area. Without this, on x86, the PIO page, for example, will be missing. This is problematic when dealing with an unhandled exception from the guest as the exception vector will be incorrectly reported as 0x0.
Co-developed-by Steve Rutherford <[email protected]>
Signed-off-by Steve Rutherford <[email protected]>
Signed-off-by Aaron Lewis <[email protected]>
Reviewed-by Andrew Jones <[email protected]>
--- tools/testing/selftests/kvm/lib/kvm_util.c | 66 +++++++++++----------- 1 file changed, 33 insertions(+), 33 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +33 -33
@@ -486,6 +486,37 @@ struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)return NULL ; } +/* +* VCPU mmap Size +* +* Input Args: None +* +* Output Args: None +* +* Return: +* Size of VCPU state +* +* Returns the size of the structure pointed to by the return value +* of vcpu_state(). +*/ +static int vcpu_mmap_sz (void ) +{ +int dev_fd, ret; + + dev_fd =open (KVM_DEV_PATH ,O_RDONLY ); +if (dev_fd <0 ) +exit (KSFT_SKIP ); + + ret =ioctl (dev_fd,KVM_GET_VCPU_MMAP_SIZE ,NULL ); +TEST_ASSERT (ret >=sizeof (struct kvm_run ), +"%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i" , + __func__, ret, errno); + +close (dev_fd); + +return ret; +} +/* * VM VCPU Remove *
@@ -509,7 +540,7 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct vcpu *vcpu) vcpu->dirty_gfns =NULL ; } - ret =munmap (vcpu->state,sizeof (*vcpu->state)); + ret =munmap (vcpu->state,vcpu_mmap_sz ());TEST_ASSERT (ret ==0 ,"munmap of VCPU fd failed, rc: %i " "errno: %i" , ret, errno);close (vcpu->fd);
@@ -909,37 +940,6 @@ void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot)__vm_mem_region_delete (vm,memslot2region (vm, slot)); } -/* -* VCPU mmap Size -* -* Input Args: None -* -* Output Args: None -* -* Return: -* Size of VCPU state -* -* Returns the size of the structure pointed to by the return value -* of vcpu_state(). -*/ -static int vcpu_mmap_sz (void ) -{ -int dev_fd, ret; - - dev_fd =open (KVM_DEV_PATH ,O_RDONLY ); -if (dev_fd <0 ) -exit (KSFT_SKIP ); - - ret =ioctl (dev_fd,KVM_GET_VCPU_MMAP_SIZE ,NULL ); -TEST_ASSERT (ret >=sizeof (struct kvm_run ), -"%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i" , - __func__, ret, errno); - -close (dev_fd); - -return ret; -} -/* * VM VCPU Add *
@@ -978,7 +978,7 @@ void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid)TEST_ASSERT (vcpu_mmap_sz () >=sizeof (*vcpu->state),"vcpu mmap size " "smaller than expected, vcpu_mmap_sz: %i expected_min: %zi" ,vcpu_mmap_sz (),sizeof (*vcpu->state)); - vcpu->state = (struct kvm_run *)mmap (NULL ,sizeof (*vcpu->state), + vcpu->state = (struct kvm_run *)mmap (NULL ,vcpu_mmap_sz (),PROT_READ |PROT_WRITE ,MAP_SHARED , vcpu->fd,0 );TEST_ASSERT (vcpu->state !=MAP_FAILED ,"mmap vcpu_state failed, " "vcpu id: %u errno: %i" , vcpuid, errno);
Signature · 1 line
2.30.0.478.g8a0d178c01-goog
Quoted 11 lines · Aaron Lewis
The vcpu mmap area may consist of more than just the
kvm_runstruct. Allocate enough space for the entire vcpu mmap area. Without this, on x86, the PIO page, for example, will be missing. This is problematic when dealing with an unhandled exception from the guest as the exception vector will be incorrectly reported as0x0.Co-developed-by Steve Rutherford <[email protected]>Signed-off-by Steve Rutherford <[email protected]>Signed-off-by Aaron Lewis <[email protected]>Reviewed-by Andrew Jones <[email protected]>---
Reviewed-by Sean Christopherson <[email protected]>