mirror of
https://github.com/torvalds/linux.git
synced 2024-11-23 12:42:02 +00:00
LoongArch: KVM: Add software breakpoint support
When VM runs in kvm mode, system will not exit to host mode when executing a general software breakpoint instruction such as INSN_BREAK, trap exception happens in guest mode rather than host mode. In order to debug guest kernel on host side, one mechanism should be used to let VM exit to host mode. Here a hypercall instruction with a special code is used for software breakpoint usage. VM exits to host mode and kvm hypervisor identifies the special hypercall code and sets exit_reason with KVM_EXIT_DEBUG. And then let qemu handle it. Idea comes from ppc kvm, one api KVM_REG_LOONGARCH_DEBUG_INST is added to get the hypercall code. VMM needs get sw breakpoint instruction with this api and set the corresponding sw break point for guest kernel. Signed-off-by: Bibo Mao <maobibo@loongson.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
This commit is contained in:
parent
74c16b2e2b
commit
163e9fc695
@ -12,6 +12,7 @@
|
||||
|
||||
#define INSN_NOP 0x03400000
|
||||
#define INSN_BREAK 0x002a0000
|
||||
#define INSN_HVCL 0x002b8000
|
||||
|
||||
#define ADDR_IMMMASK_LU52ID 0xFFF0000000000000
|
||||
#define ADDR_IMMMASK_LU32ID 0x000FFFFF00000000
|
||||
|
@ -31,6 +31,11 @@
|
||||
|
||||
#define KVM_HALT_POLL_NS_DEFAULT 500000
|
||||
|
||||
#define KVM_GUESTDBG_SW_BP_MASK \
|
||||
(KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)
|
||||
#define KVM_GUESTDBG_VALID_MASK \
|
||||
(KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP | KVM_GUESTDBG_SINGLESTEP)
|
||||
|
||||
struct kvm_vm_stat {
|
||||
struct kvm_vm_stat_generic generic;
|
||||
u64 pages;
|
||||
|
@ -10,10 +10,13 @@
|
||||
#define HYPERCALL_ENCODE(vendor, code) ((vendor << HYPERVISOR_VENDOR_SHIFT) + code)
|
||||
|
||||
#define KVM_HCALL_CODE_SERVICE 0
|
||||
#define KVM_HCALL_CODE_SWDBG 1
|
||||
|
||||
#define KVM_HCALL_SERVICE HYPERCALL_ENCODE(HYPERVISOR_KVM, KVM_HCALL_CODE_SERVICE)
|
||||
#define KVM_HCALL_FUNC_IPI 1
|
||||
|
||||
#define KVM_HCALL_SWDBG HYPERCALL_ENCODE(HYPERVISOR_KVM, KVM_HCALL_CODE_SWDBG)
|
||||
|
||||
/*
|
||||
* LoongArch hypercall return code
|
||||
*/
|
||||
|
@ -17,6 +17,8 @@
|
||||
#define KVM_COALESCED_MMIO_PAGE_OFFSET 1
|
||||
#define KVM_DIRTY_LOG_PAGE_OFFSET 64
|
||||
|
||||
#define KVM_GUESTDBG_USE_SW_BP 0x00010000
|
||||
|
||||
/*
|
||||
* for KVM_GET_REGS and KVM_SET_REGS
|
||||
*/
|
||||
@ -72,6 +74,8 @@ struct kvm_fpu {
|
||||
|
||||
#define KVM_REG_LOONGARCH_COUNTER (KVM_REG_LOONGARCH_KVM | KVM_REG_SIZE_U64 | 1)
|
||||
#define KVM_REG_LOONGARCH_VCPU_RESET (KVM_REG_LOONGARCH_KVM | KVM_REG_SIZE_U64 | 2)
|
||||
/* Debugging: Special instruction for software breakpoint */
|
||||
#define KVM_REG_LOONGARCH_DEBUG_INST (KVM_REG_LOONGARCH_KVM | KVM_REG_SIZE_U64 | 3)
|
||||
|
||||
#define LOONGARCH_REG_SHIFT 3
|
||||
#define LOONGARCH_REG_64(TYPE, REG) (TYPE | KVM_REG_SIZE_U64 | (REG << LOONGARCH_REG_SHIFT))
|
||||
|
@ -760,25 +760,37 @@ static void kvm_handle_service(struct kvm_vcpu *vcpu)
|
||||
|
||||
static int kvm_handle_hypercall(struct kvm_vcpu *vcpu)
|
||||
{
|
||||
int ret;
|
||||
larch_inst inst;
|
||||
unsigned int code;
|
||||
|
||||
inst.word = vcpu->arch.badi;
|
||||
code = inst.reg0i15_format.immediate;
|
||||
update_pc(&vcpu->arch);
|
||||
ret = RESUME_GUEST;
|
||||
|
||||
switch (code) {
|
||||
case KVM_HCALL_SERVICE:
|
||||
vcpu->stat.hypercall_exits++;
|
||||
kvm_handle_service(vcpu);
|
||||
break;
|
||||
case KVM_HCALL_SWDBG:
|
||||
/* KVM_HCALL_SWDBG only in effective when SW_BP is enabled */
|
||||
if (vcpu->guest_debug & KVM_GUESTDBG_SW_BP_MASK) {
|
||||
vcpu->run->exit_reason = KVM_EXIT_DEBUG;
|
||||
ret = RESUME_HOST;
|
||||
break;
|
||||
}
|
||||
fallthrough;
|
||||
default:
|
||||
/* Treat it as noop intruction, only set return value */
|
||||
kvm_write_reg(vcpu, LOONGARCH_GPR_A0, KVM_HCALL_INVALID_CODE);
|
||||
break;
|
||||
}
|
||||
|
||||
return RESUME_GUEST;
|
||||
if (ret == RESUME_GUEST)
|
||||
update_pc(&vcpu->arch);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -248,7 +248,15 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
|
||||
int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
|
||||
struct kvm_guest_debug *dbg)
|
||||
{
|
||||
return -EINVAL;
|
||||
if (dbg->control & ~KVM_GUESTDBG_VALID_MASK)
|
||||
return -EINVAL;
|
||||
|
||||
if (dbg->control & KVM_GUESTDBG_ENABLE)
|
||||
vcpu->guest_debug = dbg->control;
|
||||
else
|
||||
vcpu->guest_debug = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int kvm_set_cpuid(struct kvm_vcpu *vcpu, u64 val)
|
||||
@ -499,6 +507,9 @@ static int kvm_get_one_reg(struct kvm_vcpu *vcpu,
|
||||
case KVM_REG_LOONGARCH_COUNTER:
|
||||
*v = drdtime() + vcpu->kvm->arch.time_offset;
|
||||
break;
|
||||
case KVM_REG_LOONGARCH_DEBUG_INST:
|
||||
*v = INSN_HVCL | KVM_HCALL_SWDBG;
|
||||
break;
|
||||
default:
|
||||
ret = -EINVAL;
|
||||
break;
|
||||
|
@ -76,6 +76,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
|
||||
case KVM_CAP_IMMEDIATE_EXIT:
|
||||
case KVM_CAP_IOEVENTFD:
|
||||
case KVM_CAP_MP_STATE:
|
||||
case KVM_CAP_SET_GUEST_DEBUG:
|
||||
r = 1;
|
||||
break;
|
||||
case KVM_CAP_NR_VCPUS:
|
||||
|
Loading…
Reference in New Issue
Block a user