88#define _GNU_SOURCE /* for program_invocation_short_name */
99#include <sys/ioctl.h>
1010
11+ #include "kvm_test_harness.h"
1112#include "test_util.h"
1213#include "kvm_util.h"
1314#include "vmx.h"
@@ -527,14 +528,13 @@ static void run_guest_then_process_ucall_done(struct kvm_vcpu *vcpu)
527528 process_ucall_done (vcpu );
528529}
529530
530- static void test_msr_filter_allow (void )
531+ KVM_ONE_VCPU_TEST_SUITE (user_msr );
532+
533+ KVM_ONE_VCPU_TEST (user_msr , msr_filter_allow , guest_code_filter_allow )
531534{
532- struct kvm_vcpu * vcpu ;
533- struct kvm_vm * vm ;
535+ struct kvm_vm * vm = vcpu -> vm ;
534536 int rc ;
535537
536- vm = vm_create_with_one_vcpu (& vcpu , guest_code_filter_allow );
537-
538538 rc = kvm_check_cap (KVM_CAP_X86_USER_SPACE_MSR );
539539 TEST_ASSERT (rc , "KVM_CAP_X86_USER_SPACE_MSR is available" );
540540 vm_enable_cap (vm , KVM_CAP_X86_USER_SPACE_MSR , KVM_MSR_EXIT_REASON_FILTER );
@@ -585,8 +585,6 @@ static void test_msr_filter_allow(void)
585585 } else {
586586 printf ("To run the instruction emulated tests set the module parameter 'kvm.force_emulation_prefix=1'\n" );
587587 }
588-
589- kvm_vm_free (vm );
590588}
591589
592590static int handle_ucall (struct kvm_vcpu * vcpu )
@@ -646,16 +644,12 @@ static void handle_wrmsr(struct kvm_run *run)
646644 }
647645}
648646
649- static void test_msr_filter_deny ( void )
647+ KVM_ONE_VCPU_TEST ( user_msr , msr_filter_deny , guest_code_filter_deny )
650648{
651- struct kvm_vcpu * vcpu ;
652- struct kvm_vm * vm ;
653- struct kvm_run * run ;
649+ struct kvm_vm * vm = vcpu -> vm ;
650+ struct kvm_run * run = vcpu -> run ;
654651 int rc ;
655652
656- vm = vm_create_with_one_vcpu (& vcpu , guest_code_filter_deny );
657- run = vcpu -> run ;
658-
659653 rc = kvm_check_cap (KVM_CAP_X86_USER_SPACE_MSR );
660654 TEST_ASSERT (rc , "KVM_CAP_X86_USER_SPACE_MSR is available" );
661655 vm_enable_cap (vm , KVM_CAP_X86_USER_SPACE_MSR , KVM_MSR_EXIT_REASON_INVAL |
@@ -689,18 +683,13 @@ static void test_msr_filter_deny(void)
689683done :
690684 TEST_ASSERT (msr_reads == 4 , "Handled 4 rdmsr in user space" );
691685 TEST_ASSERT (msr_writes == 3 , "Handled 3 wrmsr in user space" );
692-
693- kvm_vm_free (vm );
694686}
695687
696- static void test_msr_permission_bitmap ( void )
688+ KVM_ONE_VCPU_TEST ( user_msr , msr_permission_bitmap , guest_code_permission_bitmap )
697689{
698- struct kvm_vcpu * vcpu ;
699- struct kvm_vm * vm ;
690+ struct kvm_vm * vm = vcpu -> vm ;
700691 int rc ;
701692
702- vm = vm_create_with_one_vcpu (& vcpu , guest_code_permission_bitmap );
703-
704693 rc = kvm_check_cap (KVM_CAP_X86_USER_SPACE_MSR );
705694 TEST_ASSERT (rc , "KVM_CAP_X86_USER_SPACE_MSR is available" );
706695 vm_enable_cap (vm , KVM_CAP_X86_USER_SPACE_MSR , KVM_MSR_EXIT_REASON_FILTER );
@@ -715,8 +704,6 @@ static void test_msr_permission_bitmap(void)
715704 vm_ioctl (vm , KVM_X86_SET_MSR_FILTER , & filter_gs );
716705 run_guest_then_process_rdmsr (vcpu , MSR_GS_BASE );
717706 run_guest_then_process_ucall_done (vcpu );
718-
719- kvm_vm_free (vm );
720707}
721708
722709#define test_user_exit_msr_ioctl (vm , cmd , arg , flag , valid_mask ) \
@@ -786,31 +773,18 @@ static void run_msr_filter_flag_test(struct kvm_vm *vm)
786773}
787774
788775/* Test that attempts to write to the unused bits in a flag fails. */
789- static void test_user_exit_msr_flags ( void )
776+ KVM_ONE_VCPU_TEST ( user_msr , user_exit_msr_flags , NULL )
790777{
791- struct kvm_vcpu * vcpu ;
792- struct kvm_vm * vm ;
793-
794- vm = vm_create_with_one_vcpu (& vcpu , NULL );
778+ struct kvm_vm * vm = vcpu -> vm ;
795779
796780 /* Test flags for KVM_CAP_X86_USER_SPACE_MSR. */
797781 run_user_space_msr_flag_test (vm );
798782
799783 /* Test flags and range flags for KVM_X86_SET_MSR_FILTER. */
800784 run_msr_filter_flag_test (vm );
801-
802- kvm_vm_free (vm );
803785}
804786
805787int main (int argc , char * argv [])
806788{
807- test_msr_filter_allow ();
808-
809- test_msr_filter_deny ();
810-
811- test_msr_permission_bitmap ();
812-
813- test_user_exit_msr_flags ();
814-
815- return 0 ;
789+ return test_harness_run (argc , argv );
816790}
0 commit comments