Path: blob/master/tools/testing/selftests/kvm/x86/debug_regs.c
38237 views
// SPDX-License-Identifier: GPL-2.01/*2* KVM guest debug register tests3*4* Copyright (C) 2020, Red Hat, Inc.5*/6#include <stdio.h>7#include <string.h>8#include "kvm_util.h"9#include "processor.h"10#include "apic.h"1112#define DR6_BD (1 << 13)13#define DR7_GD (1 << 13)1415#define IRQ_VECTOR 0xAA1617/* For testing data access debug BP */18uint32_t guest_value;1920extern unsigned char sw_bp, hw_bp, write_data, ss_start, bd_start;2122static void guest_code(void)23{24/* Create a pending interrupt on current vCPU */25x2apic_enable();26x2apic_write_reg(APIC_ICR, APIC_DEST_SELF | APIC_INT_ASSERT |27APIC_DM_FIXED | IRQ_VECTOR);2829/*30* Software BP tests.31*32* NOTE: sw_bp need to be before the cmd here, because int3 is an33* exception rather than a normal trap for KVM_SET_GUEST_DEBUG (we34* capture it using the vcpu exception bitmap).35*/36asm volatile("sw_bp: int3");3738/* Hardware instruction BP test */39asm volatile("hw_bp: nop");4041/* Hardware data BP test */42asm volatile("mov $1234,%%rax;\n\t"43"mov %%rax,%0;\n\t write_data:"44: "=m" (guest_value) : : "rax");4546/*47* Single step test, covers 2 basic instructions and 2 emulated48*49* Enable interrupts during the single stepping to see that pending50* interrupt we raised is not handled due to KVM_GUESTDBG_BLOCKIRQ.51*52* Write MSR_IA32_TSC_DEADLINE to verify that KVM's fastpath handler53* exits to userspace due to single-step being enabled.54*/55asm volatile("ss_start: "56"sti\n\t"57"xor %%eax,%%eax\n\t"58"cpuid\n\t"59"movl $" __stringify(MSR_IA32_TSC_DEADLINE) ", %%ecx\n\t"60"wrmsr\n\t"61"cli\n\t"62: : : "eax", "ebx", "ecx", "edx");6364/* DR6.BD test */65asm volatile("bd_start: mov %%dr0, %%rax" : : : "rax");66GUEST_DONE();67}6869#define CAST_TO_RIP(v) ((unsigned long long)&(v))7071static void vcpu_skip_insn(struct kvm_vcpu *vcpu, int insn_len)72{73struct kvm_regs regs;7475vcpu_regs_get(vcpu, ®s);76regs.rip += insn_len;77vcpu_regs_set(vcpu, ®s);78}7980int main(void)81{82struct kvm_guest_debug debug;83unsigned long long target_dr6, target_rip;84struct kvm_vcpu *vcpu;85struct kvm_run *run;86struct kvm_vm *vm;87struct ucall uc;88uint64_t cmd;89int i;90/* Instruction lengths starting at ss_start */91int ss_size[6] = {921, /* sti*/932, /* xor */942, /* cpuid */955, /* mov */962, /* rdmsr */971, /* cli */98};99100TEST_REQUIRE(kvm_has_cap(KVM_CAP_SET_GUEST_DEBUG));101102vm = vm_create_with_one_vcpu(&vcpu, guest_code);103run = vcpu->run;104105/* Test software BPs - int3 */106memset(&debug, 0, sizeof(debug));107debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP;108vcpu_guest_debug_set(vcpu, &debug);109vcpu_run(vcpu);110TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG &&111run->debug.arch.exception == BP_VECTOR &&112run->debug.arch.pc == CAST_TO_RIP(sw_bp),113"INT3: exit %d exception %d rip 0x%llx (should be 0x%llx)",114run->exit_reason, run->debug.arch.exception,115run->debug.arch.pc, CAST_TO_RIP(sw_bp));116vcpu_skip_insn(vcpu, 1);117118/* Test instruction HW BP over DR[0-3] */119for (i = 0; i < 4; i++) {120memset(&debug, 0, sizeof(debug));121debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;122debug.arch.debugreg[i] = CAST_TO_RIP(hw_bp);123debug.arch.debugreg[7] = 0x400 | (1UL << (2*i+1));124vcpu_guest_debug_set(vcpu, &debug);125vcpu_run(vcpu);126target_dr6 = 0xffff0ff0 | (1UL << i);127TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG &&128run->debug.arch.exception == DB_VECTOR &&129run->debug.arch.pc == CAST_TO_RIP(hw_bp) &&130run->debug.arch.dr6 == target_dr6,131"INS_HW_BP (DR%d): exit %d exception %d rip 0x%llx "132"(should be 0x%llx) dr6 0x%llx (should be 0x%llx)",133i, run->exit_reason, run->debug.arch.exception,134run->debug.arch.pc, CAST_TO_RIP(hw_bp),135run->debug.arch.dr6, target_dr6);136}137/* Skip "nop" */138vcpu_skip_insn(vcpu, 1);139140/* Test data access HW BP over DR[0-3] */141for (i = 0; i < 4; i++) {142memset(&debug, 0, sizeof(debug));143debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;144debug.arch.debugreg[i] = CAST_TO_RIP(guest_value);145debug.arch.debugreg[7] = 0x00000400 | (1UL << (2*i+1)) |146(0x000d0000UL << (4*i));147vcpu_guest_debug_set(vcpu, &debug);148vcpu_run(vcpu);149target_dr6 = 0xffff0ff0 | (1UL << i);150TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG &&151run->debug.arch.exception == DB_VECTOR &&152run->debug.arch.pc == CAST_TO_RIP(write_data) &&153run->debug.arch.dr6 == target_dr6,154"DATA_HW_BP (DR%d): exit %d exception %d rip 0x%llx "155"(should be 0x%llx) dr6 0x%llx (should be 0x%llx)",156i, run->exit_reason, run->debug.arch.exception,157run->debug.arch.pc, CAST_TO_RIP(write_data),158run->debug.arch.dr6, target_dr6);159/* Rollback the 4-bytes "mov" */160vcpu_skip_insn(vcpu, -7);161}162/* Skip the 4-bytes "mov" */163vcpu_skip_insn(vcpu, 7);164165/* Test single step */166target_rip = CAST_TO_RIP(ss_start);167target_dr6 = 0xffff4ff0ULL;168for (i = 0; i < ARRAY_SIZE(ss_size); i++) {169target_rip += ss_size[i];170memset(&debug, 0, sizeof(debug));171debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_SINGLESTEP |172KVM_GUESTDBG_BLOCKIRQ;173debug.arch.debugreg[7] = 0x00000400;174vcpu_guest_debug_set(vcpu, &debug);175vcpu_run(vcpu);176TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG &&177run->debug.arch.exception == DB_VECTOR &&178run->debug.arch.pc == target_rip &&179run->debug.arch.dr6 == target_dr6,180"SINGLE_STEP[%d]: exit %d exception %d rip 0x%llx "181"(should be 0x%llx) dr6 0x%llx (should be 0x%llx)",182i, run->exit_reason, run->debug.arch.exception,183run->debug.arch.pc, target_rip, run->debug.arch.dr6,184target_dr6);185}186187/* Finally test global disable */188memset(&debug, 0, sizeof(debug));189debug.control = KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_HW_BP;190debug.arch.debugreg[7] = 0x400 | DR7_GD;191vcpu_guest_debug_set(vcpu, &debug);192vcpu_run(vcpu);193target_dr6 = 0xffff0ff0 | DR6_BD;194TEST_ASSERT(run->exit_reason == KVM_EXIT_DEBUG &&195run->debug.arch.exception == DB_VECTOR &&196run->debug.arch.pc == CAST_TO_RIP(bd_start) &&197run->debug.arch.dr6 == target_dr6,198"DR7.GD: exit %d exception %d rip 0x%llx "199"(should be 0x%llx) dr6 0x%llx (should be 0x%llx)",200run->exit_reason, run->debug.arch.exception,201run->debug.arch.pc, target_rip, run->debug.arch.dr6,202target_dr6);203204/* Disable all debug controls, run to the end */205memset(&debug, 0, sizeof(debug));206vcpu_guest_debug_set(vcpu, &debug);207208vcpu_run(vcpu);209TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);210cmd = get_ucall(vcpu, &uc);211TEST_ASSERT(cmd == UCALL_DONE, "UCALL_DONE");212213kvm_vm_free(vm);214215return 0;216}217218219