target/riscv/kvm: QEMU support for KVM Guest Debug on RISC-V
v2 cover target/riscv/kvm: QEMU support for KVM Guest Debug on RISC-V
This series implements QEMU KVM Guest Debug on RISC-V, with which we could debug RISC-V KVM guest from the host side, using software breakpoints.
This series is based on riscv-to-apply.next branch (v9.0.0) and is also available at:
https://github.com/Du-Chao/alistair23-qemu/tree/riscv-to-apply.next.0528
The corresponding KVM side patches have been merged already:
https://lore.kernel.org/kvm/[email protected]/
A TODO list which will be added later:
1. HW breakpoints support
2. A 'corner case' in which the debug exception is not inserted by the debugger, need to be re-injected to the guest.
v2->v2 resend:
- add the type conversion in patch #1 to avoid warnings
v1->v2:
- squash patch #2 into #1
- check the instruction length from the tail two bits, instead of passing the length information by parameters.
RFC->v1:
- Rebased on riscv-to-apply.next
- use configs/ definition to conditionalize debug support
v1 link:
https://lore.kernel.org/qemu-riscv/[email protected]/
RFC link:
https://lore.kernel.org/qemu-riscv/[email protected]/
Chao Du (3):
target/riscv/kvm: add software breakpoints support target/riscv/kvm: handle the exit with debug reason target/riscv/kvm: define TARGET_KVM_HAVE_GUEST_DEBUG
configs/targets/riscv64-softmmu.mak | 1 + target/riscv/kvm/kvm-cpu.c | 89 +++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+)
--
2.17.1
PATCH RESEND v2 1/3 target/riscv/kvm: add software breakpoints support
This patch implements insert/remove software breakpoint process.
For RISC-V, GDB treats single-step similarly to breakpoint: add a breakpoint at the next step address, then continue. So this also works for single-step debugging.
Implement kvm_arch_update_guest_debug(): Set the control flag when there are active breakpoints. This will help KVM to know the status in the userspace.
Add some stubs which are necessary for building, and will be implemented later.
--- target/riscv/kvm/kvm-cpu.c | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)
target/riscv/kvm/kvm-cpu.c +69 -0
@@ -1969,3 +1969,72 @@ static const TypeInfo riscv_kvm_cpu_type_infos[] = { };DEFINE_TYPES (riscv_kvm_cpu_type_infos) + +static const uint32_t ebreak_insn = 0x00100073; +static const uint16_t c_ebreak_insn =0x9002 ; + +int kvm_arch_insert_sw_breakpoint (CPUState *cs,struct kvm_sw_breakpoint *bp) +{ +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,2 ,0 )) { +return -EINVAL ; + } + +if ((bp->saved_insn &0x3 ) ==0x3 ) { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,4 ,0 ) + ||cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&ebreak_insn,4 ,1 )) { +return -EINVAL ; + } + }else { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&c_ebreak_insn,2 ,1 )) { +return -EINVAL ; + } + } + +return 0 ; +} + +int kvm_arch_remove_sw_breakpoint (CPUState *cs,struct kvm_sw_breakpoint *bp) +{ +uint32_t ebreak; +uint16_t c_ebreak; + +if ((bp->saved_insn &0x3 ) ==0x3 ) { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&ebreak,4 ,0 ) || + ebreak != ebreak_insn || +cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,4 ,1 )) { +return -EINVAL ; + } + }else { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&c_ebreak,2 ,0 ) || + c_ebreak != c_ebreak_insn || +cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,2 ,1 )) { +return -EINVAL ; + } + } + +return 0 ; +} + +int kvm_arch_insert_hw_breakpoint (vaddr addr,vaddr len,int type) +{ +/* TODO; To be implemented later. */ +return -EINVAL ; +} + +int kvm_arch_remove_hw_breakpoint (vaddr addr,vaddr len,int type) +{ +/* TODO; To be implemented later. */ +return -EINVAL ; +} + +void kvm_arch_remove_all_hw_breakpoints (void ) +{ +/* TODO; To be implemented later. */ +} + +void kvm_arch_update_guest_debug (CPUState *cs,struct kvm_guest_debug *dbg) +{ +if (kvm_sw_breakpoints_active (cs)) { + dbg->control |=KVM_GUESTDBG_ENABLE ; + } +}
--
2.17.1
Re: [PATCH RESEND v2 1/3] target/riscv/kvm: add software breakpoints support
Quoted 96 lines · Chao Du
This patch implements insert/remove software breakpoint process.
For RISC-V, GDB treats single-step similarly to breakpoint: add a breakpoint at the next step address, then continue. So this also works for single-step debugging.
Implement
kvm_arch_update_guest_debug(): Set the control flag when there are active breakpoints. This will help KVM to know the status in the userspace.Add some stubs which are necessary for building, and will be implemented later.
Signed-off-by Chao Du <[email protected]>--- target/riscv/kvm/kvm-cpu.c | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)target/riscv/kvm/kvm-cpu.c +69
-1@@ -1969,3 +1969,72 @@ static const TypeInfo riscv_kvm_cpu_type_infos[] = { };DEFINE_TYPES (riscv_kvm_cpu_type_infos) + +static const uint32_t ebreak_insn = 0x00100073; +static const uint16_t c_ebreak_insn =0x9002 ; + +int kvm_arch_insert_sw_breakpoint (CPUState *cs,struct kvm_sw_breakpoint *bp) +{ +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,2 ,0 )) { +return -EINVAL ; + } + +if ((bp->saved_insn &0x3 ) ==0x3 ) { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,4 ,0 ) + ||cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&ebreak_insn,4 ,1 )) { +return -EINVAL ; + } + }else { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&c_ebreak_insn,2 ,1 )) { +return -EINVAL ; + } + } + +return 0 ; +} + +int kvm_arch_remove_sw_breakpoint (CPUState *cs,struct kvm_sw_breakpoint *bp) +{ +uint32_t ebreak; +uint16_t c_ebreak; + +if ((bp->saved_insn &0x3 ) ==0x3 ) { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&ebreak,4 ,0 ) || + ebreak != ebreak_insn || +cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,4 ,1 )) { +return -EINVAL ; + } + }else { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&c_ebreak,2 ,0 ) || + c_ebreak != c_ebreak_insn || +cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,2 ,1 )) { +return -EINVAL ; + } + } + +return 0 ; +} + +int kvm_arch_insert_hw_breakpoint (vaddr addr,vaddr len,int type) +{ +/* TODO; To be implemented later. */ +return -EINVAL ; +} + +int kvm_arch_remove_hw_breakpoint (vaddr addr,vaddr len,int type) +{ +/* TODO; To be implemented later. */ +return -EINVAL ; +} + +void kvm_arch_remove_all_hw_breakpoints (void ) +{ +/* TODO; To be implemented later. */ +} + +void kvm_arch_update_guest_debug (CPUState *cs,struct kvm_guest_debug *dbg) +{ +if (kvm_sw_breakpoints_active (cs)) { + dbg->control |=KVM_GUESTDBG_ENABLE ; + } +} --
Quoted 2 lines
2.17.1
Re: [PATCH RESEND v2 1/3] target/riscv/kvm: add software breakpoints support
In reply to Chao DuQuoted 7 lines · Chao Du
This patch implements insert/remove software breakpoint process.
For RISC-V, GDB treats single-step similarly to breakpoint: add a breakpoint at the next step address, then continue. So this also works for single-step debugging.
Quoted 8 lines
Implement
kvm_arch_update_guest_debug(): Set the control flag when there are active breakpoints. This will help KVM to know the status in the userspace.Add some stubs which are necessary for building, and will be implemented later.
Signed-off-by Chao Du <[email protected]>
Alistair
Quoted 84 lines
--- target/riscv/kvm/kvm-cpu.c | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)target/riscv/kvm/kvm-cpu.c +69
-1@@ -1969,3 +1969,72 @@ static const TypeInfo riscv_kvm_cpu_type_infos[] = { };DEFINE_TYPES (riscv_kvm_cpu_type_infos) + +static const uint32_t ebreak_insn = 0x00100073; +static const uint16_t c_ebreak_insn =0x9002 ; + +int kvm_arch_insert_sw_breakpoint (CPUState *cs,struct kvm_sw_breakpoint *bp) +{ +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,2 ,0 )) { +return -EINVAL ; + } + +if ((bp->saved_insn &0x3 ) ==0x3 ) { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,4 ,0 ) + ||cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&ebreak_insn,4 ,1 )) { +return -EINVAL ; + } + }else { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&c_ebreak_insn,2 ,1 )) { +return -EINVAL ; + } + } + +return 0 ; +} + +int kvm_arch_remove_sw_breakpoint (CPUState *cs,struct kvm_sw_breakpoint *bp) +{ +uint32_t ebreak; +uint16_t c_ebreak; + +if ((bp->saved_insn &0x3 ) ==0x3 ) { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&ebreak,4 ,0 ) || + ebreak != ebreak_insn || +cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,4 ,1 )) { +return -EINVAL ; + } + }else { +if (cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&c_ebreak,2 ,0 ) || + c_ebreak != c_ebreak_insn || +cpu_memory_rw_debug (cs, bp->pc, (uint8_t *)&bp->saved_insn,2 ,1 )) { +return -EINVAL ; + } + } + +return 0 ; +} + +int kvm_arch_insert_hw_breakpoint (vaddr addr,vaddr len,int type) +{ +/* TODO; To be implemented later. */ +return -EINVAL ; +} + +int kvm_arch_remove_hw_breakpoint (vaddr addr,vaddr len,int type) +{ +/* TODO; To be implemented later. */ +return -EINVAL ; +} + +void kvm_arch_remove_all_hw_breakpoints (void ) +{ +/* TODO; To be implemented later. */ +} + +void kvm_arch_update_guest_debug (CPUState *cs,struct kvm_guest_debug *dbg) +{ +if (kvm_sw_breakpoints_active (cs)) { + dbg->control |=KVM_GUESTDBG_ENABLE ; + } +} --2.17.1
PATCH RESEND v2 2/3 target/riscv/kvm: handle the exit with debug reason
In reply to Chao DuIf the breakpoint belongs to the userspace then set the ret value.
--- target/riscv/kvm/kvm-cpu.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
target/riscv/kvm/kvm-cpu.c +20 -0
@@ -1555,6 +1555,21 @@ static int kvm_riscv_handle_csr(CPUState *cs, struct kvm_run *run)return ret; } +static bool kvm_riscv_handle_debug (CPUState *cs) +{ +RISCVCPU *cpu =RISCV_CPU (cs); +CPURISCVState *env = &cpu->env; + +/* Ensure PC is synchronised */ +kvm_cpu_synchronize_state (cs); + +if (kvm_find_sw_breakpoint (cs, env->pc)) { +return true; + } + +return false; +} +int kvm_arch_handle_exit (CPUState *cs,struct kvm_run *run) {int ret =0 ;
@@ -1565,6 +1580,11 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)case KVM_EXIT_RISCV_CSR : ret =kvm_riscv_handle_csr (cs, run);break ; +case KVM_EXIT_DEBUG : +if (kvm_riscv_handle_debug (cs)) { + ret =EXCP_DEBUG ; + } +break ;default :qemu_log_mask (LOG_UNIMP ,"%s: un-handled exit reason %d\n" , __func__, run->exit_reason);
--
2.17.1
Re: [PATCH RESEND v2 2/3] target/riscv/kvm: handle the exit with debug reason
Quoted 6 lines · Chao Du
If the breakpoint belongs to the userspace then set the ret value.
Signed-off-by Chao Du <[email protected]>Reviewed-by Daniel Henrique Barboza <[email protected]>Reviewed-by Andrew Jones <[email protected]>
Alistair
Quoted 45 lines
--- target/riscv/kvm/kvm-cpu.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)target/riscv/kvm/kvm-cpu.c +20
-1@@ -1555,6 +1555,21 @@ static int kvm_riscv_handle_csr(CPUState *cs, struct kvm_run *run)return ret; } +static bool kvm_riscv_handle_debug (CPUState *cs) +{ +RISCVCPU *cpu =RISCV_CPU (cs); +CPURISCVState *env = &cpu->env; + +/* Ensure PC is synchronised */ +kvm_cpu_synchronize_state (cs); + +if (kvm_find_sw_breakpoint (cs, env->pc)) { +return true; + } + +return false; +} +int kvm_arch_handle_exit (CPUState *cs,struct kvm_run *run) {int ret =0 ;@@ -1565,6 +1580,11 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)case KVM_EXIT_RISCV_CSR : ret =kvm_riscv_handle_csr (cs, run);break ; +case KVM_EXIT_DEBUG : +if (kvm_riscv_handle_debug (cs)) { + ret =EXCP_DEBUG ; + } +break ;default :qemu_log_mask (LOG_UNIMP ,"%s: un-handled exit reason %d\n" , __func__, run->exit_reason); --2.17.1
PATCH RESEND v2 3/3 target/riscv/kvm: define TARGET_KVM_HAVE_GUEST_DEBUG
In reply to Chao DuTo enable the KVM GUEST DEBUG for RISC-V at QEMU side.
--- configs/targets/riscv64-softmmu.mak | 1 + 1 file changed, 1 insertion(+)
configs/targets/riscv64-softmmu.mak +1 -0
@@ -1,6 +1,7 @@ TARGET_ARCH=riscv64 TARGET_BASE_ARCH=riscv TARGET_SUPPORTS_MTTCG=y +TARGET_KVM_HAVE_GUEST_DEBUG=y TARGET_XML_FILES= gdb-xml/riscv-64bit-cpu.xml gdb-xml/riscv-32bit-fpu.xml gdb-xml/riscv-64bit-fpu.xml gdb-xml/riscv-64bit-virtual.xml # needed by boot.c TARGET_NEED_FDT=y
--
2.17.1
Re: [PATCH RESEND v2 3/3] target/riscv/kvm: define TARGET_KVM_HAVE_GUEST_DEBUG
Quoted 6 lines · Chao Du
To enable the KVM GUEST DEBUG for RISC-V at QEMU side.
Signed-off-by Chao Du <[email protected]>Reviewed-by Daniel Henrique Barboza <[email protected]>Reviewed-by Andrew Jones <[email protected]>
Alistair
Quoted 19 lines
--- configs/targets/riscv64-softmmu.mak | 1 + 1 file changed, 1 insertion(+)configs/targets/riscv64-softmmu.mak +1
-1@@ -1,6 +1,7 @@ TARGET_ARCH=riscv64 TARGET_BASE_ARCH=riscv TARGET_SUPPORTS_MTTCG=y +TARGET_KVM_HAVE_GUEST_DEBUG=y TARGET_XML_FILES= gdb-xml/riscv-64bit-cpu.xml gdb-xml/riscv-32bit-fpu.xml gdb-xml/riscv-64bit-fpu.xml gdb-xml/riscv-64bit-virtual.xml # needed by boot.c TARGET_NEED_FDT=y --2.17.1