Commit 28039449 authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: selftests: Convert debug_regs away from VCPU_ID

Convert debug_regs to use vm_create_with_one_vcpu() and pass around a
'struct kvm_vcpu' object instead of using a global VCPU_ID.

Opportunstically drop the CLEAR_DEBUG/APPLY_DEBUG macros as they only
obfuscate the code, e.g. operating on local variables not "passed" to the
macro is all kinds of confusing.
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 42975c21
...@@ -10,8 +10,6 @@ ...@@ -10,8 +10,6 @@
#include "processor.h" #include "processor.h"
#include "apic.h" #include "apic.h"
#define VCPU_ID 0
#define DR6_BD (1 << 13) #define DR6_BD (1 << 13)
#define DR7_GD (1 << 13) #define DR7_GD (1 << 13)
...@@ -66,13 +64,11 @@ static void guest_code(void) ...@@ -66,13 +64,11 @@ static void guest_code(void)
GUEST_DONE(); GUEST_DONE();
} }
#define CLEAR_DEBUG() memset(&debug, 0, sizeof(debug))
#define APPLY_DEBUG() vcpu_guest_debug_set(vm, VCPU_ID, &debug)
#define CAST_TO_RIP(v) ((unsigned long long)&(v)) #define CAST_TO_RIP(v) ((unsigned long long)&(v))
#define SET_RIP(v) do { \ #define SET_RIP(v) do { \
vcpu_regs_get(vm, VCPU_ID, &regs); \ vcpu_regs_get(vm, vcpu->id, &regs); \
regs.rip = (v); \ regs.rip = (v); \
vcpu_regs_set(vm, VCPU_ID, &regs); \ vcpu_regs_set(vm, vcpu->id, &regs); \
} while (0) } while (0)
#define MOVE_RIP(v) SET_RIP(regs.rip + (v)); #define MOVE_RIP(v) SET_RIP(regs.rip + (v));
...@@ -80,6 +76,7 @@ int main(void) ...@@ -80,6 +76,7 @@ int main(void)
{ {
struct kvm_guest_debug debug; struct kvm_guest_debug debug;
unsigned long long target_dr6, target_rip; unsigned long long target_dr6, target_rip;
struct kvm_vcpu *vcpu;
struct kvm_regs regs; struct kvm_regs regs;
struct kvm_run *run; struct kvm_run *run;
struct kvm_vm *vm; struct kvm_vm *vm;
...@@ -101,14 +98,14 @@ int main(void) ...@@ -101,14 +98,14 @@ int main(void)
return 0; return 0;
} }
vm = vm_create_default(VCPU_ID, 0, guest_code); vm = vm_create_with_one_vcpu(&vcpu, guest_code);
run = vcpu_state(vm, VCPU_ID); run = vcpu->run;
/* Test software BPs - int3 */ /* Test software BPs - int3 */
CLEAR_DEBUG(); memset(&debug, 0, sizeof(debug));
debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP; debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;
APPLY_DEBUG(); vcpu_guest_debug_set(vm, vcpu->id, &debug);
vcpu_run(vm, VCPU_ID); vcpu_run(vm, vcpu->id);
TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG && TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG &&
run->debug.arch.exception == BP_VECTOR && run->debug.arch.exception == BP_VECTOR &&
run->debug.arch.pc == CAST_TO_RIP(sw_bp), run->debug.arch.pc == CAST_TO_RIP(sw_bp),
...@@ -119,12 +116,12 @@ int main(void) ...@@ -119,12 +116,12 @@ int main(void)
/* Test instruction HW BP over DR[0-3] */ /* Test instruction HW BP over DR[0-3] */
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
CLEAR_DEBUG(); memset(&debug, 0, sizeof(debug));
debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP; debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
debug.arch.debugreg[i] = CAST_TO_RIP(hw_bp); debug.arch.debugreg[i] = CAST_TO_RIP(hw_bp);
debug.arch.debugreg[7] = 0x400 | (1UL << (2*i+1)); debug.arch.debugreg[7] = 0x400 | (1UL << (2*i+1));
APPLY_DEBUG(); vcpu_guest_debug_set(vm, vcpu->id, &debug);
vcpu_run(vm, VCPU_ID); vcpu_run(vm, vcpu->id);
target_dr6 = 0xffff0ff0 | (1UL << i); target_dr6 = 0xffff0ff0 | (1UL << i);
TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG && TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG &&
run->debug.arch.exception == DB_VECTOR && run->debug.arch.exception == DB_VECTOR &&
...@@ -141,13 +138,13 @@ int main(void) ...@@ -141,13 +138,13 @@ int main(void)
/* Test data access HW BP over DR[0-3] */ /* Test data access HW BP over DR[0-3] */
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
CLEAR_DEBUG(); memset(&debug, 0, sizeof(debug));
debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP; debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
debug.arch.debugreg[i] = CAST_TO_RIP(guest_value); debug.arch.debugreg[i] = CAST_TO_RIP(guest_value);
debug.arch.debugreg[7] = 0x00000400 | (1UL << (2*i+1)) | debug.arch.debugreg[7] = 0x00000400 | (1UL << (2*i+1)) |
(0x000d0000UL << (4*i)); (0x000d0000UL << (4*i));
APPLY_DEBUG(); vcpu_guest_debug_set(vm, vcpu->id, &debug);
vcpu_run(vm, VCPU_ID); vcpu_run(vm, vcpu->id);
target_dr6 = 0xffff0ff0 | (1UL << i); target_dr6 = 0xffff0ff0 | (1UL << i);
TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG && TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG &&
run->debug.arch.exception == DB_VECTOR && run->debug.arch.exception == DB_VECTOR &&
...@@ -167,15 +164,15 @@ int main(void) ...@@ -167,15 +164,15 @@ int main(void)
/* Test single step */ /* Test single step */
target_rip = CAST_TO_RIP(ss_start); target_rip = CAST_TO_RIP(ss_start);
target_dr6 = 0xffff4ff0ULL; target_dr6 = 0xffff4ff0ULL;
vcpu_regs_get(vm, VCPU_ID, &regs); vcpu_regs_get(vm, vcpu->id, &regs);
for (i = 0; i < (sizeof(ss_size) / sizeof(ss_size[0])); i++) { for (i = 0; i < (sizeof(ss_size) / sizeof(ss_size[0])); i++) {
target_rip += ss_size[i]; target_rip += ss_size[i];
CLEAR_DEBUG(); memset(&debug, 0, sizeof(debug));
debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP | debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP |
KVM_GUESTDBG_BLOCKIRQ; KVM_GUESTDBG_BLOCKIRQ;
debug.arch.debugreg[7] = 0x00000400; debug.arch.debugreg[7] = 0x00000400;
APPLY_DEBUG(); vcpu_guest_debug_set(vm, vcpu->id, &debug);
vcpu_run(vm, VCPU_ID); vcpu_run(vm, vcpu->id);
TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG && TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG &&
run->debug.arch.exception == DB_VECTOR && run->debug.arch.exception == DB_VECTOR &&
run->debug.arch.pc == target_rip && run->debug.arch.pc == target_rip &&
...@@ -188,11 +185,11 @@ int main(void) ...@@ -188,11 +185,11 @@ int main(void)
} }
/* Finally test global disable */ /* Finally test global disable */
CLEAR_DEBUG(); memset(&debug, 0, sizeof(debug));
debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP; debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;
debug.arch.debugreg[7] = 0x400 | DR7_GD; debug.arch.debugreg[7] = 0x400 | DR7_GD;
APPLY_DEBUG(); vcpu_guest_debug_set(vm, vcpu->id, &debug);
vcpu_run(vm, VCPU_ID); vcpu_run(vm, vcpu->id);
target_dr6 = 0xffff0ff0 | DR6_BD; target_dr6 = 0xffff0ff0 | DR6_BD;
TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG && TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG &&
run->debug.arch.exception == DB_VECTOR && run->debug.arch.exception == DB_VECTOR &&
...@@ -205,12 +202,12 @@ int main(void) ...@@ -205,12 +202,12 @@ int main(void)
target_dr6); target_dr6);
/* Disable all debug controls, run to the end */ /* Disable all debug controls, run to the end */
CLEAR_DEBUG(); memset(&debug, 0, sizeof(debug));
APPLY_DEBUG(); vcpu_guest_debug_set(vm, vcpu->id, &debug);
vcpu_run(vm, VCPU_ID); vcpu_run(vm, vcpu->id);
TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, "KVM_EXIT_IO"); TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, "KVM_EXIT_IO");
cmd = get_ucall(vm, VCPU_ID, &uc); cmd = get_ucall(vm, vcpu->id, &uc);
TEST_ASSERT(cmd == UCALL_DONE, "UCALL_DONE"); TEST_ASSERT(cmd == UCALL_DONE, "UCALL_DONE");
kvm_vm_free(vm); kvm_vm_free(vm);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment