提交 709fd884 编写于 作者: S Sean Christopherson 提交者: Paolo Bonzini

KVM: selftests: Convert vmx_exception_with_invalid_guest_state away from VCPU_ID

Convert vmx_exception_with_invalid_guest_state to use
vm_create_with_one_vcpu() and pass around a 'struct kvm_vcpu' object
instead of using a global VCPU_ID.
Signed-off-by: NSean Christopherson <seanjc@google.com>
Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
上级 b4694260
...@@ -10,10 +10,6 @@ ...@@ -10,10 +10,6 @@
#include "kselftest.h" #include "kselftest.h"
#define VCPU_ID 0
static struct kvm_vm *vm;
static void guest_ud_handler(struct ex_regs *regs) static void guest_ud_handler(struct ex_regs *regs)
{ {
/* Loop on the ud2 until guest state is made invalid. */ /* Loop on the ud2 until guest state is made invalid. */
...@@ -24,11 +20,11 @@ static void guest_code(void) ...@@ -24,11 +20,11 @@ static void guest_code(void)
asm volatile("ud2"); asm volatile("ud2");
} }
static void __run_vcpu_with_invalid_state(void) static void __run_vcpu_with_invalid_state(struct kvm_vcpu *vcpu)
{ {
struct kvm_run *run = vcpu_state(vm, VCPU_ID); struct kvm_run *run = vcpu->run;
vcpu_run(vm, VCPU_ID); vcpu_run(vcpu->vm, vcpu->id);
TEST_ASSERT(run->exit_reason == KVM_EXIT_INTERNAL_ERROR, TEST_ASSERT(run->exit_reason == KVM_EXIT_INTERNAL_ERROR,
"Expected KVM_EXIT_INTERNAL_ERROR, got %d (%s)\n", "Expected KVM_EXIT_INTERNAL_ERROR, got %d (%s)\n",
...@@ -38,15 +34,15 @@ static void __run_vcpu_with_invalid_state(void) ...@@ -38,15 +34,15 @@ static void __run_vcpu_with_invalid_state(void)
run->emulation_failure.suberror); run->emulation_failure.suberror);
} }
static void run_vcpu_with_invalid_state(void) static void run_vcpu_with_invalid_state(struct kvm_vcpu *vcpu)
{ {
/* /*
* Always run twice to verify KVM handles the case where _KVM_ queues * Always run twice to verify KVM handles the case where _KVM_ queues
* an exception with invalid state and then exits to userspace, i.e. * an exception with invalid state and then exits to userspace, i.e.
* that KVM doesn't explode if userspace ignores the initial error. * that KVM doesn't explode if userspace ignores the initial error.
*/ */
__run_vcpu_with_invalid_state(); __run_vcpu_with_invalid_state(vcpu);
__run_vcpu_with_invalid_state(); __run_vcpu_with_invalid_state(vcpu);
} }
static void set_timer(void) static void set_timer(void)
...@@ -59,33 +55,43 @@ static void set_timer(void) ...@@ -59,33 +55,43 @@ static void set_timer(void)
ASSERT_EQ(setitimer(ITIMER_REAL, &timer, NULL), 0); ASSERT_EQ(setitimer(ITIMER_REAL, &timer, NULL), 0);
} }
static void set_or_clear_invalid_guest_state(bool set) static void set_or_clear_invalid_guest_state(struct kvm_vcpu *vcpu, bool set)
{ {
static struct kvm_sregs sregs; static struct kvm_sregs sregs;
if (!sregs.cr0) if (!sregs.cr0)
vcpu_sregs_get(vm, VCPU_ID, &sregs); vcpu_sregs_get(vcpu->vm, vcpu->id, &sregs);
sregs.tr.unusable = !!set; sregs.tr.unusable = !!set;
vcpu_sregs_set(vm, VCPU_ID, &sregs); vcpu_sregs_set(vcpu->vm, vcpu->id, &sregs);
} }
static void set_invalid_guest_state(void) static void set_invalid_guest_state(struct kvm_vcpu *vcpu)
{ {
set_or_clear_invalid_guest_state(true); set_or_clear_invalid_guest_state(vcpu, true);
} }
static void clear_invalid_guest_state(void) static void clear_invalid_guest_state(struct kvm_vcpu *vcpu)
{ {
set_or_clear_invalid_guest_state(false); set_or_clear_invalid_guest_state(vcpu, false);
}
static struct kvm_vcpu *get_set_sigalrm_vcpu(struct kvm_vcpu *__vcpu)
{
static struct kvm_vcpu *vcpu = NULL;
if (__vcpu)
vcpu = __vcpu;
return vcpu;
} }
static void sigalrm_handler(int sig) static void sigalrm_handler(int sig)
{ {
struct kvm_vcpu *vcpu = get_set_sigalrm_vcpu(NULL);
struct kvm_vcpu_events events; struct kvm_vcpu_events events;
TEST_ASSERT(sig == SIGALRM, "Unexpected signal = %d", sig); TEST_ASSERT(sig == SIGALRM, "Unexpected signal = %d", sig);
vcpu_events_get(vm, VCPU_ID, &events); vcpu_events_get(vcpu->vm, vcpu->id, &events);
/* /*
* If an exception is pending, attempt KVM_RUN with invalid guest, * If an exception is pending, attempt KVM_RUN with invalid guest,
...@@ -93,8 +99,8 @@ static void sigalrm_handler(int sig) ...@@ -93,8 +99,8 @@ static void sigalrm_handler(int sig)
* between KVM queueing an exception and re-entering the guest. * between KVM queueing an exception and re-entering the guest.
*/ */
if (events.exception.pending) { if (events.exception.pending) {
set_invalid_guest_state(); set_invalid_guest_state(vcpu);
run_vcpu_with_invalid_state(); run_vcpu_with_invalid_state(vcpu);
} else { } else {
set_timer(); set_timer();
} }
...@@ -102,15 +108,19 @@ static void sigalrm_handler(int sig) ...@@ -102,15 +108,19 @@ static void sigalrm_handler(int sig)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct kvm_vcpu *vcpu;
struct kvm_vm *vm;
if (!is_intel_cpu() || vm_is_unrestricted_guest(NULL)) { if (!is_intel_cpu() || vm_is_unrestricted_guest(NULL)) {
print_skip("Must be run with kvm_intel.unrestricted_guest=0"); print_skip("Must be run with kvm_intel.unrestricted_guest=0");
exit(KSFT_SKIP); exit(KSFT_SKIP);
} }
vm = vm_create_default(VCPU_ID, 0, (void *)guest_code); vm = vm_create_with_one_vcpu(&vcpu, guest_code);
get_set_sigalrm_vcpu(vcpu);
vm_init_descriptor_tables(vm); vm_init_descriptor_tables(vm);
vcpu_init_descriptor_tables(vm, VCPU_ID); vcpu_init_descriptor_tables(vm, vcpu->id);
vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler); vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler);
...@@ -119,8 +129,8 @@ int main(int argc, char *argv[]) ...@@ -119,8 +129,8 @@ int main(int argc, char *argv[])
* KVM_RUN should induce a TRIPLE_FAULT in L2 as KVM doesn't support * KVM_RUN should induce a TRIPLE_FAULT in L2 as KVM doesn't support
* emulating invalid guest state for L2. * emulating invalid guest state for L2.
*/ */
set_invalid_guest_state(); set_invalid_guest_state(vcpu);
run_vcpu_with_invalid_state(); run_vcpu_with_invalid_state(vcpu);
/* /*
* Verify KVM also handles the case where userspace gains control while * Verify KVM also handles the case where userspace gains control while
...@@ -129,11 +139,11 @@ int main(int argc, char *argv[]) ...@@ -129,11 +139,11 @@ int main(int argc, char *argv[])
* guest with invalid state when the handler interrupts KVM with an * guest with invalid state when the handler interrupts KVM with an
* exception pending. * exception pending.
*/ */
clear_invalid_guest_state(); clear_invalid_guest_state(vcpu);
TEST_ASSERT(signal(SIGALRM, sigalrm_handler) != SIG_ERR, TEST_ASSERT(signal(SIGALRM, sigalrm_handler) != SIG_ERR,
"Failed to register SIGALRM handler, errno = %d (%s)", "Failed to register SIGALRM handler, errno = %d (%s)",
errno, strerror(errno)); errno, strerror(errno));
set_timer(); set_timer();
run_vcpu_with_invalid_state(); run_vcpu_with_invalid_state(vcpu);
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册