* [PATCH v2 1/3] target/riscv/kvm: add software breakpoints support
2024-05-28 7:20 [PATCH v2 0/3] target/riscv/kvm: QEMU support for KVM Guest Debug on RISC-V Chao Du
@ 2024-05-28 7:20 ` Chao Du
2024-05-28 7:20 ` [PATCH v2 2/3] target/riscv/kvm: handle the exit with debug reason Chao Du
2024-05-28 7:20 ` [PATCH v2 3/3] target/riscv/kvm: define TARGET_KVM_HAVE_GUEST_DEBUG Chao Du
2 siblings, 0 replies; 4+ messages in thread
From: Chao Du @ 2024-05-28 7:20 UTC (permalink / raw)
To: qemu-devel, qemu-riscv, pbonzini, ajones, alistair23, bin.meng,
liweiwei, dbarboza, zhiwei_liu, palmer, anup, duchao713
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 <duchao@eswincomputing.com>
---
target/riscv/kvm/kvm-cpu.c | 69 ++++++++++++++++++++++++++++++++++++++
1 file changed, 69 insertions(+)
diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
index 235e2cdaca..c50f058aff 100644
--- a/target/riscv/kvm/kvm-cpu.c
+++ b/target/riscv/kvm/kvm-cpu.c
@@ -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, &ebreak_insn, 4, 1)) {
+ return -EINVAL;
+ }
+ } else {
+ if (cpu_memory_rw_debug(cs, bp->pc, &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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 2/3] target/riscv/kvm: handle the exit with debug reason
2024-05-28 7:20 [PATCH v2 0/3] target/riscv/kvm: QEMU support for KVM Guest Debug on RISC-V Chao Du
2024-05-28 7:20 ` [PATCH v2 1/3] target/riscv/kvm: add software breakpoints support Chao Du
@ 2024-05-28 7:20 ` Chao Du
2024-05-28 7:20 ` [PATCH v2 3/3] target/riscv/kvm: define TARGET_KVM_HAVE_GUEST_DEBUG Chao Du
2 siblings, 0 replies; 4+ messages in thread
From: Chao Du @ 2024-05-28 7:20 UTC (permalink / raw)
To: qemu-devel, qemu-riscv, pbonzini, ajones, alistair23, bin.meng,
liweiwei, dbarboza, zhiwei_liu, palmer, anup, duchao713
If the breakpoint belongs to the userspace then set the ret value.
Signed-off-by: Chao Du <duchao@eswincomputing.com>
Reviewed-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
---
target/riscv/kvm/kvm-cpu.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
index c50f058aff..bbde86c3a4 100644
--- a/target/riscv/kvm/kvm-cpu.c
+++ b/target/riscv/kvm/kvm-cpu.c
@@ -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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 3/3] target/riscv/kvm: define TARGET_KVM_HAVE_GUEST_DEBUG
2024-05-28 7:20 [PATCH v2 0/3] target/riscv/kvm: QEMU support for KVM Guest Debug on RISC-V Chao Du
2024-05-28 7:20 ` [PATCH v2 1/3] target/riscv/kvm: add software breakpoints support Chao Du
2024-05-28 7:20 ` [PATCH v2 2/3] target/riscv/kvm: handle the exit with debug reason Chao Du
@ 2024-05-28 7:20 ` Chao Du
2 siblings, 0 replies; 4+ messages in thread
From: Chao Du @ 2024-05-28 7:20 UTC (permalink / raw)
To: qemu-devel, qemu-riscv, pbonzini, ajones, alistair23, bin.meng,
liweiwei, dbarboza, zhiwei_liu, palmer, anup, duchao713
To enable the KVM GUEST DEBUG for RISC-V at QEMU side.
Signed-off-by: Chao Du <duchao@eswincomputing.com>
Reviewed-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
---
configs/targets/riscv64-softmmu.mak | 1 +
1 file changed, 1 insertion(+)
diff --git a/configs/targets/riscv64-softmmu.mak b/configs/targets/riscv64-softmmu.mak
index f688ffa7bc..917980e63e 100644
--- a/configs/targets/riscv64-softmmu.mak
+++ b/configs/targets/riscv64-softmmu.mak
@@ -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
^ permalink raw reply related [flat|nested] 4+ messages in thread