Add gdbstub support to HVF
cover Add gdbstub support to HVF
From: Francesco Cagnin <[email protected]>
Hello,
This patch series aims to add gdbstub support to HVF (the 'QEMU accelerator on macOS that employs Hypervisor.framework') on Apple Silicon hosts.
The proposed implementation, structured like the KVM counterpart, handles single-stepping, software breakpoints, hardware breakpoints and hardware watchpoints on single-core guests (i.e. '-smp 1'). (If possible, I'd like to receive guidance on how to add proper support for multi-core guests.)
The patch has been most recently tested working on macOS Ventura 13.0 hosts and Linux kernel 5.19 guests with the test script 'tests/guest-debug/test-gdbstub.py' (slightly updated to make it work with Linux kernels compiled on macOS).
If deemed useful, I can also submit an analogous patch targeting Intel hosts.
Francesco Cagnin (3):
arm: move KVM breakpoints helpers
hvf: implement guest debugging on Apple Silicon hosts
hvf: handle writes of MDSCR_EL1 and DBG*_EL1
accel/hvf/hvf-accel-ops.c | 124 ++++++++++++++ accel/hvf/hvf-all.c | 24 +++ cpu.c | 3 + include/sysemu/hvf.h | 29 ++++ include/sysemu/hvf_int.h | 1 + target/arm/debug_helper.c | 241 +++++++++++++++++++++++++++ target/arm/hvf/hvf.c | 334 +++++++++++++++++++++++++++++++++++++- target/arm/internals.h | 50 ++++++ target/arm/kvm64.c | 276 ------------------------------- 9 files changed, 805 insertions(+), 277 deletions(-)
Signature · 1 line
2.38.1
PATCH 1/3 arm: move KVM breakpoints helpers
From: Francesco Cagnin <[email protected]>
These helpers will be also used for HVF. Aside from reformatting a couple of comments for 'checkpatch.pl', this is just code motion.
--- target/arm/debug_helper.c | 241 +++++++++++++++++++++++++++++++++ target/arm/internals.h | 50 +++++++ target/arm/kvm64.c | 276 -------------------------------------- 3 files changed, 291 insertions(+), 276 deletions(-)
target/arm/debug_helper.c +241 -0
@@ -12,6 +12,7 @@#include "cpregs.h" #include "exec/exec-all.h" #include "exec/helper-proto.h" +#include "exec/gdbstub.h" /* Return the Exception Level targeted by debug exceptions. */
@@ -1134,3 +1135,243 @@ vaddr arm_adjust_watchpoint_address(CPUState *cs, vaddr addr, int len) }#endif + +/* Maximum and current break/watch point counts */ +int max_hw_bps, max_hw_wps; +GArray *hw_breakpoints, *hw_watchpoints; + +/** +* insert_hw_breakpoint() +* @addr: address of breakpoint +* +* See ARM ARM D2.9.1 for details but here we are only going to create +* simple un-linked breakpoints (i.e. we don't chain breakpoints +* together to match address and context or vmid). The hardware is +* capable of fancier matching but that will require exposing that +* fanciness to GDB's interface +* +* DBGBCR<n>_EL1, Debug Breakpoint Control Registers +* +* 31 24 23 20 19 16 15 14 13 12 9 8 5 4 3 2 1 0 +* +------+------+-------+-----+----+------+-----+------+-----+---+ +* | RES0 | BT | LBN | SSC | HMC| RES0 | BAS | RES0 | PMC | E | +* +------+------+-------+-----+----+------+-----+------+-----+---+ +* +* BT: Breakpoint type (0 = unlinked address match) +* LBN: Linked BP number (0 = unused) +* SSC/HMC/PMC: Security, Higher and Priv access control (Table D-12) +* BAS: Byte Address Select (RES1 for AArch64) +* E: Enable bit +* +* DBGBVR<n>_EL1, Debug Breakpoint Value Registers +* +* 63 53 52 49 48 2 1 0 +* +------+-----------+----------+-----+ +* | RESS | VA[52:49] | VA[48:2] | 0 0 | +* +------+-----------+----------+-----+ +* +* Depending on the addressing mode bits the top bits of the register +* are a sign extension of the highest applicable VA bit. Some +* versions of GDB don't do it correctly so we ensure they are correct +* here so future PC comparisons will work properly. +*/ + +int insert_hw_breakpoint (target_ulong addr) +{ +HWBreakpoint brk = { + .bcr =0x1 ,/* BCR E=1, enable */ + .bvr =sextract64 (addr,0 ,53 ) + }; + +if (cur_hw_bps >= max_hw_bps) { +return -ENOBUFS ; + } + + brk.bcr =deposit32 (brk.bcr,1 ,2 ,0x3 );/* PMC = 11 */ + brk.bcr =deposit32 (brk.bcr,5 ,4 ,0xf );/* BAS = RES1 */ + +g_array_append_val (hw_breakpoints, brk); + +return 0 ; +} + +/** +* delete_hw_breakpoint() +* @pc: address of breakpoint +* +* Delete a breakpoint and shuffle any above down +*/ + +int delete_hw_breakpoint (target_ulong pc) +{ +int i; +for (i =0 ; i < hw_breakpoints->len; i++) { +HWBreakpoint *brk =get_hw_bp (i); +if (brk->bvr == pc) { +g_array_remove_index (hw_breakpoints, i); +return 0 ; + } + } +return -ENOENT ; +} + +/** +* insert_hw_watchpoint() +* @addr: address of watch point +* @len: size of area +* @type: type of watch point +* +* See ARM ARM D2.10. As with the breakpoints we can do some advanced +* stuff if we want to. The watch points can be linked with the break +* points above to make them context aware. However for simplicity +* currently we only deal with simple read/write watch points. +* +* D7.3.11 DBGWCR<n>_EL1, Debug Watchpoint Control Registers +* +* 31 29 28 24 23 21 20 19 16 15 14 13 12 5 4 3 2 1 0 +* +------+-------+------+----+-----+-----+-----+-----+-----+-----+---+ +* | RES0 | MASK | RES0 | WT | LBN | SSC | HMC | BAS | LSC | PAC | E | +* +------+-------+------+----+-----+-----+-----+-----+-----+-----+---+ +* +* MASK: num bits addr mask (0=none,01/10=res,11=3 bits (8 bytes)) +* WT: 0 - unlinked, 1 - linked (not currently used) +* LBN: Linked BP number (not currently used) +* SSC/HMC/PAC: Security, Higher and Priv access control (Table D2-11) +* BAS: Byte Address Select +* LSC: Load/Store control (01: load, 10: store, 11: both) +* E: Enable +* +* The bottom 2 bits of the value register are masked. Therefore to +* break on any sizes smaller than an unaligned word you need to set +* MASK=0, BAS=bit per byte in question. For larger regions (^2) you +* need to ensure you mask the address as required and set BAS=0xff +*/ + +int insert_hw_watchpoint (target_ulong addr, +target_ulong len,int type) +{ +HWWatchpoint wp = { + .wcr =R_DBGWCR_E_MASK ,/* E=1, enable */ + .wvr = addr & (~0x7ULL ), + .details = { .vaddr = addr, .len = len } + }; + +if (cur_hw_wps >= max_hw_wps) { +return -ENOBUFS ; + } + +/* +* HMC=0 SSC=0 PAC=3 will hit EL0 or EL1, any security state, +* valid whether EL3 is implemented or not +*/ + wp.wcr =FIELD_DP64 (wp.wcr,DBGWCR ,PAC ,3 ); + +switch (type) { +case GDB_WATCHPOINT_READ : + wp.wcr =FIELD_DP64 (wp.wcr,DBGWCR ,LSC ,1 ); + wp.details.flags =BP_MEM_READ ; +break ; +case GDB_WATCHPOINT_WRITE : + wp.wcr =FIELD_DP64 (wp.wcr,DBGWCR ,LSC ,2 ); + wp.details.flags =BP_MEM_WRITE ; +break ; +case GDB_WATCHPOINT_ACCESS : + wp.wcr =FIELD_DP64 (wp.wcr,DBGWCR ,LSC ,3 ); + wp.details.flags =BP_MEM_ACCESS ; +break ; +default : +g_assert_not_reached (); +break ; + } +if (len <=8 ) { +/* we align the address and set the bits in BAS */ +int off = addr &0x7 ; +int bas = (1 << len) -1 ; + + wp.wcr =deposit32 (wp.wcr,5 + off,8 - off, bas); + }else { +/* For ranges above 8 bytes we need to be a power of 2 */ +if (is_power_of_2 (len)) { +int bits =ctz64 (len); + + wp.wvr &= ~((1 << bits) -1 ); + wp.wcr =FIELD_DP64 (wp.wcr,DBGWCR ,MASK , bits); + wp.wcr =FIELD_DP64 (wp.wcr,DBGWCR ,BAS ,0xff ); + }else { +return -ENOBUFS ; + } + } + +g_array_append_val (hw_watchpoints, wp); +return 0 ; +} + +bool check_watchpoint_in_range (int i,target_ulong addr) +{ +HWWatchpoint *wp =get_hw_wp (i); +uint64_t addr_top, addr_bottom = wp->wvr; +int bas =extract32 (wp->wcr,5 ,8 ); +int mask =extract32 (wp->wcr,24 ,4 ); + +if (mask) { + addr_top = addr_bottom + (1 << mask); + }else { +/* +* BAS must be contiguous but can offset against the base +* address in DBGWVR +*/ + addr_bottom = addr_bottom +ctz32 (bas); + addr_top = addr_bottom +clo32 (bas); + } + +if (addr >= addr_bottom && addr <= addr_top) { +return true; + } + +return false; +} + +/** +* delete_hw_watchpoint() +* @addr: address of breakpoint +* +* Delete a breakpoint and shuffle any above down +*/ + +int delete_hw_watchpoint (target_ulong addr, +target_ulong len,int type) +{ +int i; +for (i =0 ; i < cur_hw_wps; i++) { +if (check_watchpoint_in_range (i, addr)) { +g_array_remove_index (hw_watchpoints, i); +return 0 ; + } + } +return -ENOENT ; +} + +bool find_hw_breakpoint (CPUState *cpu,target_ulong pc) +{ +int i; + +for (i =0 ; i < cur_hw_bps; i++) { +HWBreakpoint *bp =get_hw_bp (i); +if (bp->bvr == pc) { +return true; + } + } +return false; +} + +CPUWatchpoint *find_hw_watchpoint (CPUState *cpu,target_ulong addr) +{ +int i; + +for (i =0 ; i < cur_hw_wps; i++) { +if (check_watchpoint_in_range (i, addr)) { +return &get_hw_wp (i)->details; + } + } +return NULL ; +}
target/arm/internals.h +50 -0
54 lines not on this page · raw message
target/arm/kvm64.c +0 -276
294 lines not on this page · raw message
Signature · 1 line
2.38.1
Re: [PATCH 1/3] arm: move KVM breakpoints helpers
Quoted 8 lines · On 4 Nov 2022, at 19.40, [email protected] wrote:
From: Francesco Cagnin <[email protected]>
These helpers will be also used for HVF. Aside from reformatting a couple of comments for 'checkpatch.pl', this is just code motion.
Quoted 6 lines
Signed-off-by Francesco Cagnin <[email protected]>---
target/arm/debug_helper.c | 241 +++++++++++++++++++++++++++++++++ target/arm/internals.h | 50 +++++++ target/arm/kvm64.c | 276 -------------------------------------- 3 files changed, 291 insertions(+), 276 deletions(-)
Looks good. I was going to do the exact same in my patchset.
Re: [PATCH 1/3] arm: move KVM breakpoints helpers
In reply to [email protected]Quoted 9 lines · [email protected] writes:
From: Francesco Cagnin <[email protected]>
These helpers will be also used for HVF. Aside from reformatting a couple of comments for 'checkpatch.pl', this is just code motion.
Signed-off-by Francesco Cagnin <[email protected]>--- target/arm/debug_helper.c | 241 +++++++++++++++++++++++++++++++++ target/arm/internals.h | 50 +++++++
Moving out of kvm64.c seems fine to me but I wonder if debug_helper.c is the best location. debug_helpers is currently very focused on just handling the TCG emulation case where as we are doing this tracking just for the VMM cases or KVM and now HVF.
We are (slowly) trying to clean up the code in target/arm so we can support builds like --disable-tcg and to do that we want to avoid too much ifdef hackery in the individual compilation units.
Peter, what do you think?
Quoted 2 lines
target/arm/kvm64.c | 276 -------------------------------------- 3 files changed, 291 insertions(+), 276 deletions(-)
<snip>
Signature · 1 line
Alex Bennée
Re: [PATCH 1/3] arm: move KVM breakpoints helpers
Quoted 13 lines · Alex Bennée
Quoted 9 lines · [email protected] writes:
From: Francesco Cagnin <[email protected]>
These helpers will be also used for HVF. Aside from reformatting a couple of comments for 'checkpatch.pl', this is just code motion.
Signed-off-by Francesco Cagnin <[email protected]>--- target/arm/debug_helper.c | 241 +++++++++++++++++++++++++++++++++ target/arm/internals.h | 50 +++++++
Quoted 8 lines
Moving out of
kvm64.cseems fine to me but I wonder ifdebug_helper.cis the best location.debug_helpersis currently very focused on just handling the TCG emulation case where as we are doing this tracking just for the VMM cases or KVM and now HVF.We are (slowly) trying to clean up the code in target/arm so we can support builds like --disable-tcg and to do that we want to avoid too much ifdef hackery in the individual compilation units.
I was planning to move hypervisor-specific code to target/arm/$hypervisor/, but here Francesco wants to re-use the same code between 2 hypervisors... Maybe move it to target/arm/hyp_gdbstub.c
and let meson add it conditionally?
Quoted 4 lines
Quoted 2 lines
target/arm/kvm64.c | 276 -------------------------------------- 3 files changed, 291 insertions(+), 276 deletions(-)<snip>
Re: [PATCH 1/3] arm: move KVM breakpoints helpers
Quoted 3 lines
I was planning to move hypervisor-specific code to target/arm/$hypervisor/, but here Francesco wants to re-use the same code between 2 hypervisors... Maybe move it to
target/arm/hyp_gdbstub.c
and let meson add it conditionally?
Something like this?
-arm_ss.add(when: 'CONFIG_KVM', if_true: files('kvm.c', 'kvm64.c'),if_false: files('kvm-stub.c'))
+arm_ss.add(when: 'CONFIG_KVM', if_true: files('hyp_gdbstub.c', 'kvm.c', 'kvm64.c'), if_false: files('kvm-stub.c')) +arm_ss.add(when: 'CONFIG_HVF', if_true: files('hyp_gdbstub.c'))
PATCH 2/3 hvf: implement guest debugging on Apple Silicon hosts
In reply to [email protected]From: Francesco Cagnin <[email protected]>
Support is added for single-stepping, software breakpoints, hardware breakpoints and watchpoints. The code has been structured like the KVM counterpart (and many parts are basically identical).
Guests can be debugged through the gdbstub.
--- accel/hvf/hvf-accel-ops.c | 124 ++++++++++++++++++++++++ accel/hvf/hvf-all.c | 24 +++++ cpu.c | 3 + include/sysemu/hvf.h | 29 ++++++ include/sysemu/hvf_int.h | 1 + target/arm/hvf/hvf.c | 194 +++++++++++++++++++++++++++++++++++++- 6 files changed, 374 insertions(+), 1 deletion(-)
accel/hvf/hvf-accel-ops.c +124 -0
@@ -52,6 +52,7 @@#include "qemu/main-loop.h" #include "exec/address-spaces.h" #include "exec/exec-all.h" +#include "exec/gdbstub.h" #include "sysemu/cpus.h" #include "sysemu/hvf.h" #include "sysemu/hvf_int.h"
@@ -340,12 +341,18 @@ static int hvf_accel_init(MachineState *ms)return hvf_arch_init (); } +static int hvf_gdbstub_sstep_flags (void ) +{ +return SSTEP_ENABLE ; +} +static void hvf_accel_class_init (ObjectClass *oc,void *data) {AccelClass *ac =ACCEL_CLASS (oc); ac->name ="HVF" ; ac->init_machine = hvf_accel_init; ac->allowed = &hvf_allowed; + ac->gdbstub_supported_sstep_flags = hvf_gdbstub_sstep_flags; }static const TypeInfo hvf_accel_type = {
@@ -462,6 +469,118 @@ static void hvf_start_vcpu_thread(CPUState *cpu) cpu, QEMU_THREAD_JOINABLE); } +static bool hvf_supports_guest_debug (void ) +{ +#ifdef TARGET_AARCH64 +return true; +#else +return false; +#endif +} + +static int hvf_insert_breakpoint (CPUState *cpu,int type,hwaddr addr,hwaddr len) +{ +struct hvf_sw_breakpoint *bp; +int err; + +if (type ==GDB_BREAKPOINT_SW ) { + bp =hvf_find_sw_breakpoint (cpu, addr); +if (bp) { + bp->use_count++; +return 0 ; + } + + bp = g_new(struct hvf_sw_breakpoint, 1); + bp->pc = addr; + bp->use_count =1 ; + err =hvf_arch_insert_sw_breakpoint (cpu, bp); +if (err) { +g_free (bp); +return err; + } + +QTAILQ_INSERT_HEAD (&hvf_state->hvf_sw_breakpoints, bp, entry); + }else { + err =hvf_arch_insert_hw_breakpoint (addr, len, type); +if (err) { +return err; + } + } + +CPU_FOREACH (cpu) { + err =hvf_update_guest_debug (cpu); +if (err) { +return err; + } + } +return 0 ; +} + +static int hvf_remove_breakpoint (CPUState *cpu,int type,hwaddr addr,hwaddr len) +{ +struct hvf_sw_breakpoint *bp; +int err; + +if (type ==GDB_BREAKPOINT_SW ) { + bp =hvf_find_sw_breakpoint (cpu, addr); +if (!bp) { +return -ENOENT ; + } + +if (bp->use_count >1 ) { + bp->use_count--; +return 0 ; + } + + err =hvf_arch_remove_sw_breakpoint (cpu, bp); +if (err) { +return err; + } + +QTAILQ_REMOVE (&hvf_state->hvf_sw_breakpoints, bp, entry); +g_free (bp); + }else { + err =hvf_arch_remove_hw_breakpoint (addr, len, type); +if (err) { +return err; + } + } + +CPU_FOREACH (cpu) { + err =hvf_update_guest_debug (cpu); +if (err) { +return err; + } + } +return 0 ; +} + +static void hvf_remove_all_breakpoints (CPUState *cpu) +{ +struct hvf_sw_breakpoint *bp, *next; +HVFState *s = hvf_state; +CPUState *tmpcpu; + + QTAILQ_FOREACH_SAFE(bp, &s->hvf_sw_breakpoints, entry, next) { +if (hvf_arch_remove_sw_breakpoint (cpu, bp) !=0 ) { +/* Try harder to find a CPU that currently sees the breakpoint. */ +CPU_FOREACH (tmpcpu) + { +if (hvf_arch_remove_sw_breakpoint (tmpcpu, bp) ==0 ) { +break ; + } + } + } +QTAILQ_REMOVE (&s->hvf_sw_breakpoints, bp, entry); +g_free (bp); + } +hvf_arch_remove_all_hw_breakpoints (); + +CPU_FOREACH (cpu) { +hvf_update_guest_debug (cpu); + } +} +static void hvf_accel_ops_class_init (ObjectClass *oc,void *data) {AccelOpsClass *ops =ACCEL_OPS_CLASS (oc);
@@ -473,6 +592,11 @@ static void hvf_accel_ops_class_init(ObjectClass *oc, void *data) ops->synchronize_post_init = hvf_cpu_synchronize_post_init; ops->synchronize_state = hvf_cpu_synchronize_state; ops->synchronize_pre_loadvm = hvf_cpu_synchronize_pre_loadvm; + + ops->supports_guest_debug = hvf_supports_guest_debug; + ops->insert_breakpoint = hvf_insert_breakpoint; + ops->remove_breakpoint = hvf_remove_breakpoint; + ops->remove_all_breakpoints = hvf_remove_all_breakpoints; };static const TypeInfo hvf_accel_ops_type = { .name =ACCEL_OPS_NAME ("hvf" ),
accel/hvf/hvf-all.c +24 -0
@@ -10,6 +10,7 @@#include "qemu/osdep.h" #include "qemu/error-report.h" +#include "exec/gdbstub.h" #include "sysemu/hvf.h" #include "sysemu/hvf_int.h"
@@ -44,3 +45,26 @@ void assert_hvf_ok(hv_return_t ret)abort (); } + +struct hvf_sw_breakpoint *hvf_find_sw_breakpoint (CPUState *cpu,target_ulong pc) +{ +struct hvf_sw_breakpoint *bp; + + QTAILQ_FOREACH(bp, &hvf_state->hvf_sw_breakpoints, entry) { +if (bp->pc == pc) { +return bp; + } + } +return NULL ; +} + +int hvf_sw_breakpoints_active (CPUState *cpu) +{ +return !QTAILQ_EMPTY (&hvf_state->hvf_sw_breakpoints); +} + +int hvf_update_guest_debug (CPUState *cpu) +{ +hvf_arch_update_guest_debug (cpu); +return 0 ; +}
cpu.c +3 -0
@@ -33,6 +33,7 @@#endif #include "sysemu/tcg.h" #include "sysemu/kvm.h" +#include "sysemu/hvf.h" #include "sysemu/replay.h" #include "exec/cpu-common.h" #include "exec/exec-all.h"
@@ -389,6 +390,8 @@ void cpu_single_step(CPUState *cpu, int enabled) cpu->singlestep_enabled = enabled;if (kvm_enabled ()) {kvm_update_guest_debug (cpu,0 ); + }else if (hvf_enabled ()) { +hvf_update_guest_debug (cpu); }trace_breakpoint_singlestep (cpu->cpu_index, enabled); }
include/sysemu/hvf.h +29 -0
@@ -36,4 +36,33 @@ typedef struct HVFState HVFState;DECLARE_INSTANCE_CHECKER (HVFState,HVF_STATE , TYPE_HVF_ACCEL) +#ifdef NEED_CPU_H +#include "cpu.h" + +int hvf_update_guest_debug (CPUState *cpu); + +struct hvf_sw_breakpoint { +target_ulong pc; +target_ulong saved_insn; +int use_count; +QTAILQ_ENTRY (hvf_sw_breakpoint) entry; +}; + +struct hvf_sw_breakpoint *hvf_find_sw_breakpoint (CPUState *cpu, +target_ulong pc); + +int hvf_sw_breakpoints_active (CPUState *cpu); + +int hvf_arch_insert_sw_breakpoint (CPUState *cpu,struct hvf_sw_breakpoint *bp); +int hvf_arch_remove_sw_breakpoint (CPUState *cpu,struct hvf_sw_breakpoint *bp); +int hvf_arch_insert_hw_breakpoint (target_ulong addr,target_ulong len, +int type); +int hvf_arch_remove_hw_breakpoint (target_ulong addr,target_ulong len, +int type); +void hvf_arch_remove_all_hw_breakpoints (void ); + +void hvf_arch_update_guest_debug (CPUState *cpu); + +#endif /* NEED_CPU_H */ +#endif
include/sysemu/hvf_int.h +1 -0
@@ -45,6 +45,7 @@ struct HVFState {hvf_vcpu_caps *hvf_caps;uint64_t vtimer_offset; + QTAILQ_HEAD(, hvf_sw_breakpoint) hvf_sw_breakpoints; };extern HVFState *hvf_state;
target/arm/hvf/hvf.c +193 -1
239 lines not on this page · raw message
Signature · 1 line
2.38.1
Re: [PATCH 2/3] hvf: implement guest debugging on Apple Silicon hosts
Quoted 12 lines · On 4 Nov 2022, at 19.41, [email protected] wrote:
From: Francesco Cagnin <[email protected]>
Support is added for single-stepping, software breakpoints, hardware breakpoints and watchpoints. The code has been structured like the KVM counterpart (and many parts are basically identical).
Guests can be debugged through the gdbstub.
Signed-off-by Francesco Cagnin <[email protected]>
Quoted 8 lines
---
accel/hvf/hvf-accel-ops.c | 124 ++++++++++++++++++++++++ accel/hvf/hvf-all.c | 24 +++++ cpu.c | 3 + include/sysemu/hvf.h | 29 ++++++ include/sysemu/hvf_int.h | 1 + target/arm/hvf/hvf.c | 194 +++++++++++++++++++++++++++++++++++++- 6 files changed, 374 insertions(+), 1 deletion(-)
I've been working on the exact same features just last week, and had it working just hours before you posted, but you beat me to it. I can see we have solved it almost exactly the same way, so I won't post my patchset.
I can see you are missing support for SSTEP_NOIRQ. I've handled it like this:
accel/hvf/hvf-accel-ops.c +1 -1
@@ -343,7 +343,7 @@ static int hvf_accel_init(MachineState *ms)static int hvf_gdbstub_sstep_flags (void ) { -return SSTEP_ENABLE ; +return SSTEP_ENABLE |SSTEP_NOIRQ ; }static void hvf_accel_class_init (ObjectClass *oc,void *data)
target/arm/hvf/hvf.c +1 -1
@@ -1331,7 +1331,7 @@ int hvf_vcpu_exec(CPUState *cpu)hv_return_t r;bool advance_pc = false; -if (hvf_inject_interrupts (cpu)) { +if (!(cpu->singlestep_enabled &SSTEP_NOIRQ ) &&hvf_inject_interrupts (cpu)) {return EXCP_INTERRUPT ; }
You'll have to suppress the interrupts while you're single-stepping the code. Otherwise, you'll only be stepping a few times, and suddenly get taken to the interrupt-handler.
What issues do you have with multi-core systems?
Re: [PATCH 2/3] hvf: implement guest debugging on Apple Silicon hosts
In reply to [email protected]Quoted 27 lines · On 4 Nov 2022, at 19.41, [email protected] wrote:
From: Francesco Cagnin <[email protected]>
Support is added for single-stepping, software breakpoints, hardware breakpoints and watchpoints. The code has been structured like the KVM counterpart (and many parts are basically identical).
Guests can be debugged through the gdbstub.
Signed-off-by Francesco Cagnin <[email protected]>---
accel/hvf/hvf-accel-ops.c | 124 ++++++++++++++++++++++++ accel/hvf/hvf-all.c | 24 +++++ cpu.c | 3 + include/sysemu/hvf.h | 29 ++++++ include/sysemu/hvf_int.h | 1 + target/arm/hvf/hvf.c | 194 +++++++++++++++++++++++++++++++++++++- 6 files changed, 374 insertions(+), 1 deletion(-)include/sysemu/hvf.h +0
-0
include/sysemu/hvf.h int hvf_vcpu_exec(CPUState *cpu) · line 1208
Quoted 8 lines
include/sysemu/hvf.h +3
-0@@ -1180,6 +1201,9 @@ int hvf_vcpu_exec(CPUState *cpu)flush_cpu_state (cpu); + r =hv_vcpu_set_trap_debug_exceptions (cpu->hvf->fd, trap_debug_exceptions); +assert_hvf_ok (r); +qemu_mutex_unlock_iothread ();assert_hvf_ok (hv_vcpu_run (cpu->hvf->fd));
We don't need to set this at every call to hvf_vcpu_exec. Would it make sense to move it to hvf_arch_update_guest_debug or even hvf_arch_init_vcpu?
(CC'ed Alex Bennée as we discussed the GDB stub in HVF at KVM Forum 2022)
Re: [PATCH 2/3] hvf: implement guest debugging on Apple Silicon hosts
Mads, thanks for the review and feedbacks. I worked on this a few months ago but only managed to have it published now, I'm sorry for work being done twice.
I'll add support for SSTEP_NOIRQ as you suggested.
For multi-core systems there's no particular issue, just I'm not familiar with all of these topics and I'm asking for good practices. KVM saves breakpoints information in struct 'KVMState', and following the same approach I've updated struct 'HVFState' to store equivalent data. To keep separate information for each cpu, KVM uses field 'kvm_state' in struct 'CPUState' (see 'include/hw/core/cpu.h'), but at the moment there's no analogous field for the HVF state (which is instead defined as a single global variable in 'accel/hvf/hvf-accel-ops.c'). Should we add a new field to 'CPUState' for the HVF state, or take a different approach?
For your last question regarding hv_vcpu_set_trap_debug_exceptions(), I think it's possible to move it to hvf_arch_update_guest_debug() but I'm not confident about hvf_arch_init_vcpu(), I will experiment again.
Re: [PATCH 2/3] hvf: implement guest debugging on Apple Silicon hosts
Quoted 20 lines · Francesco Cagnin
Mads, thanks for the review and feedbacks. I worked on this a few months ago but only managed to have it published now, I'm sorry for work being done twice.
I'll add support for
SSTEP_NOIRQas you suggested.For multi-core systems there's no particular issue, just I'm not familiar with all of these topics and I'm asking for good practices. KVM saves breakpoints information in struct 'KVMState', and following the same approach I've updated struct 'HVFState' to store equivalent data. To keep separate information for each cpu, KVM uses field '
kvm_state' in struct 'CPUState' (see 'include/hw/core/cpu.h'), but at the moment there's no analogous field for the HVF state (which is instead defined as a single global variable in 'accel/hvf/hvf-accel-ops.c'). Should we add a new field to 'CPUState' for the HVF state, or take a different approach?
Quoted 3 lines
For your last question regarding
hv_vcpu_set_trap_debug_exceptions(), I think it's possible to move it tohvf_arch_update_guest_debug()but I'm not confident abouthvf_arch_init_vcpu(), I will experiment again.
You're welcome. And that's ok. It's just unlucky timing.
In my version, I added the sw breakpoints to hvf_vcpu_state so they would follow each CPU, but it's effectively a copy of the global set of breakpoints. I'm no expert on QEMU, as this would have been my first contribution, but AFAIK, KVM will also add the breakpoints to all CPUs as we can't know which CPU will take a certain process.
Moving it to hvf_arch_update_guest_debug would probably be best. Having it in hvf_arch_init_vcpu would mean that it's always enabled. In some corner-cases, that might have an adverse effect.
I also noticed you are adding 1 to the WRPs and BRPs. As I interpret the documentation, you should subtract 1 instead, given the value 0 is reserved:
target/arm/hvf/hvf.c +2 -2
@@ -39,11 +39,11 @@ static void hvf_arm_init_debug(CPUState *cpu) {ARMCPU *arm_cpu =ARM_CPU (cpu); - max_hw_bps =1 +extract64 (arm_cpu->isar.id_aa64dfr0,12 ,4 ); + max_hw_bps =extract64 (arm_cpu->isar.id_aa64dfr0,12 ,4 ) -1 ; hw_breakpoints =g_array_sized_new (true, true,sizeof (HWBreakpoint), max_hw_bps); - max_hw_wps =1 +extract64 (arm_cpu->isar.id_aa64dfr0,20 ,4 ); + max_hw_wps =extract64 (arm_cpu->isar.id_aa64dfr0,20 ,4 ) -1 ; hw_watchpoints =g_array_sized_new (true, true,sizeof (HWWatchpoint), max_hw_wps);return ;
But the documentation is a bit ambiguous on that. Maybe we can test it?
Re: [PATCH 2/3] hvf: implement guest debugging on Apple Silicon hosts
Quoted 5 lines · Mads Ynddal
I also noticed you are adding 1 to the WRPs and BRPs. As I interpret the documentation, you should subtract 1 instead, given the value 0 is reserved:
I tested it, and you do have to add 1. I guess it's implied that you cannot have less than 2 WRPs/BRPs then.
Re: [PATCH 2/3] hvf: implement guest debugging on Apple Silicon hosts
In reply to Mads YnddalQuoted 22 lines · Mads Ynddal
I also noticed you are adding 1 to the WRPs and BRPs. As I interpret the documentation, you should subtract 1 instead, given the value 0 is reserved:
target/arm/hvf/hvf.c +2
-2@@ -39,11 +39,11 @@ static void hvf_arm_init_debug(CPUState *cpu) {ARMCPU *arm_cpu =ARM_CPU (cpu); - max_hw_bps =1 +extract64 (arm_cpu->isar.id_aa64dfr0,12 ,4 ); + max_hw_bps =extract64 (arm_cpu->isar.id_aa64dfr0,12 ,4 ) -1 ; hw_breakpoints =g_array_sized_new (true, true,sizeof (HWBreakpoint), max_hw_bps); - max_hw_wps =1 +extract64 (arm_cpu->isar.id_aa64dfr0,20 ,4 ); + max_hw_wps =extract64 (arm_cpu->isar.id_aa64dfr0,20 ,4 ) -1 ; hw_watchpoints =g_array_sized_new (true, true,sizeof (HWWatchpoint), max_hw_wps);return ;
Quoted 1 line
But the documentation is a bit ambiguous on that. Maybe we can test it?
Adding 1 is correct -- the field definition is "number of breakpoints - 1", so the number of bps is "field value + 1". You don't need to open-code this, though -- there are functions arm_num_brps() and arm_num_wrps() in target/arm/internals.h that extract the fields from the ID registers and adjust them to give the actual number.
thanks
-- PMM
Re: [PATCH 2/3] hvf: implement guest debugging on Apple Silicon hosts
Quoted 2 lines
In my version, I added the sw breakpoints to
hvf_vcpu_stateso they would follow each CPU, but it's effectively a copy of the global set of breakpoints.
Ah, I missed this field, and it seems the proper one to use. I'll switch to this.
Quoted 3 lines
Moving it to
hvf_arch_update_guest_debugwould probably be best. Having it inhvf_arch_init_vcpuwould mean that it's always enabled. In some corner-cases, that might have an adverse effect.
I agree, I'll move it to 'hvf_arch_update_guest_debug()'.
Quoted 16 lines
I also noticed you are adding 1 to the WRPs and BRPs. As I interpret the documentation, you should subtract 1 instead, given the value 0 is reserved:
target/arm/hvf/hvf.c +1
-1@@ -39,11 +39,11 @@ static void hvf_arm_init_debug(CPUState *cpu) {ARMCPU *arm_cpu =ARM_CPU (cpu); - max_hw_bps =1 +extract64 (arm_cpu->isar.id_aa64dfr0,12 ,4 ); + max_hw_bps =extract64 (arm_cpu->isar.id_aa64dfr0,12 ,4 ) -1 ;
hw_breakpoints=g_array_sized_new(true, true, sizeof(HWBreakpoint), max_hw_bps);
Quoted 7 lines
-
max_hw_wps= 1 +extract64(arm_cpu->isar.id_aa64dfr0, 20, 4);
+max_hw_wps=extract64(arm_cpu->isar.id_aa64dfr0, 20, 4)- 1;hw_watchpoints=g_array_sized_new(true, true, sizeof(HWWatchpoint), max_hw_wps); return;But the documentation is a bit ambiguous on that. Maybe we can test it?
I tested this again and indeed adding 1 is correct. Thanks Peter for also pointing out 'arm_num_brps()' and 'arm_num_wrps()', I'll switch to those.
PATCH 3/3 hvf: handle writes of MDSCR_EL1 and DBG*_EL1
In reply to [email protected]From: Francesco Cagnin <[email protected]>
This proved to be required when debugging the Linux kernel's initial code, as the Hypervisor framework was triggering 'EC_SYSTEMREGISTERTRAP' VM exits after enabling trap exceptions with 'hv_vcpu_set_trap_debug_exceptions()'.
--- target/arm/hvf/hvf.c | 140 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+)
target/arm/hvf/hvf.c +140 -0
@@ -97,6 +97,71 @@ static void hvf_arm_init_debug(CPUState *cpu)#define SYSREG_PMCEID1_EL0 SYSREG(3, 3, 9, 12, 7)#define SYSREG_PMCCNTR_EL0 SYSREG(3, 3, 9, 13, 0)#define SYSREG_PMCCFILTR_EL0 SYSREG(3, 3, 14, 15, 7) +#define SYSREG_MDSCR_EL1 SYSREG(2, 0, 0, 2, 2) +#define SYSREG_DBGBVR0_EL1 SYSREG(2, 0, 0, 0, 4) +#define SYSREG_DBGBCR0_EL1 SYSREG(2, 0, 0, 0, 5) +#define SYSREG_DBGWVR0_EL1 SYSREG(2, 0, 0, 0, 6) +#define SYSREG_DBGWCR0_EL1 SYSREG(2, 0, 0, 0, 7) +#define SYSREG_DBGBVR1_EL1 SYSREG(2, 0, 0, 1, 4) +#define SYSREG_DBGBCR1_EL1 SYSREG(2, 0, 0, 1, 5) +#define SYSREG_DBGWVR1_EL1 SYSREG(2, 0, 0, 1, 6) +#define SYSREG_DBGWCR1_EL1 SYSREG(2, 0, 0, 1, 7) +#define SYSREG_DBGBVR2_EL1 SYSREG(2, 0, 0, 2, 4) +#define SYSREG_DBGBCR2_EL1 SYSREG(2, 0, 0, 2, 5) +#define SYSREG_DBGWVR2_EL1 SYSREG(2, 0, 0, 2, 6) +#define SYSREG_DBGWCR2_EL1 SYSREG(2, 0, 0, 2, 7) +#define SYSREG_DBGBVR3_EL1 SYSREG(2, 0, 0, 3, 4) +#define SYSREG_DBGBCR3_EL1 SYSREG(2, 0, 0, 3, 5) +#define SYSREG_DBGWVR3_EL1 SYSREG(2, 0, 0, 3, 6) +#define SYSREG_DBGWCR3_EL1 SYSREG(2, 0, 0, 3, 7) +#define SYSREG_DBGBVR4_EL1 SYSREG(2, 0, 0, 4, 4) +#define SYSREG_DBGBCR4_EL1 SYSREG(2, 0, 0, 4, 5) +#define SYSREG_DBGWVR4_EL1 SYSREG(2, 0, 0, 4, 6) +#define SYSREG_DBGWCR4_EL1 SYSREG(2, 0, 0, 4, 7) +#define SYSREG_DBGBVR5_EL1 SYSREG(2, 0, 0, 5, 4) +#define SYSREG_DBGBCR5_EL1 SYSREG(2, 0, 0, 5, 5) +#define SYSREG_DBGWVR5_EL1 SYSREG(2, 0, 0, 5, 6) +#define SYSREG_DBGWCR5_EL1 SYSREG(2, 0, 0, 5, 7) +#define SYSREG_DBGBVR6_EL1 SYSREG(2, 0, 0, 6, 4) +#define SYSREG_DBGBCR6_EL1 SYSREG(2, 0, 0, 6, 5) +#define SYSREG_DBGWVR6_EL1 SYSREG(2, 0, 0, 6, 6) +#define SYSREG_DBGWCR6_EL1 SYSREG(2, 0, 0, 6, 7) +#define SYSREG_DBGBVR7_EL1 SYSREG(2, 0, 0, 7, 4) +#define SYSREG_DBGBCR7_EL1 SYSREG(2, 0, 0, 7, 5) +#define SYSREG_DBGWVR7_EL1 SYSREG(2, 0, 0, 7, 6) +#define SYSREG_DBGWCR7_EL1 SYSREG(2, 0, 0, 7, 7) +#define SYSREG_DBGBVR8_EL1 SYSREG(2, 0, 0, 8, 4) +#define SYSREG_DBGBCR8_EL1 SYSREG(2, 0, 0, 8, 5) +#define SYSREG_DBGWVR8_EL1 SYSREG(2, 0, 0, 8, 6) +#define SYSREG_DBGWCR8_EL1 SYSREG(2, 0, 0, 8, 7) +#define SYSREG_DBGBVR9_EL1 SYSREG(2, 0, 0, 9, 4) +#define SYSREG_DBGBCR9_EL1 SYSREG(2, 0, 0, 9, 5) +#define SYSREG_DBGWVR9_EL1 SYSREG(2, 0, 0, 9, 6) +#define SYSREG_DBGWCR9_EL1 SYSREG(2, 0, 0, 9, 7) +#define SYSREG_DBGBVR10_EL1 SYSREG(2, 0, 0, 10, 4) +#define SYSREG_DBGBCR10_EL1 SYSREG(2, 0, 0, 10, 5) +#define SYSREG_DBGWVR10_EL1 SYSREG(2, 0, 0, 10, 6) +#define SYSREG_DBGWCR10_EL1 SYSREG(2, 0, 0, 10, 7) +#define SYSREG_DBGBVR11_EL1 SYSREG(2, 0, 0, 11, 4) +#define SYSREG_DBGBCR11_EL1 SYSREG(2, 0, 0, 11, 5) +#define SYSREG_DBGWVR11_EL1 SYSREG(2, 0, 0, 11, 6) +#define SYSREG_DBGWCR11_EL1 SYSREG(2, 0, 0, 11, 7) +#define SYSREG_DBGBVR12_EL1 SYSREG(2, 0, 0, 12, 4) +#define SYSREG_DBGBCR12_EL1 SYSREG(2, 0, 0, 12, 5) +#define SYSREG_DBGWVR12_EL1 SYSREG(2, 0, 0, 12, 6) +#define SYSREG_DBGWCR12_EL1 SYSREG(2, 0, 0, 12, 7) +#define SYSREG_DBGBVR13_EL1 SYSREG(2, 0, 0, 13, 4) +#define SYSREG_DBGBCR13_EL1 SYSREG(2, 0, 0, 13, 5) +#define SYSREG_DBGWVR13_EL1 SYSREG(2, 0, 0, 13, 6) +#define SYSREG_DBGWCR13_EL1 SYSREG(2, 0, 0, 13, 7) +#define SYSREG_DBGBVR14_EL1 SYSREG(2, 0, 0, 14, 4) +#define SYSREG_DBGBCR14_EL1 SYSREG(2, 0, 0, 14, 5) +#define SYSREG_DBGWVR14_EL1 SYSREG(2, 0, 0, 14, 6) +#define SYSREG_DBGWCR14_EL1 SYSREG(2, 0, 0, 14, 7) +#define SYSREG_DBGBVR15_EL1 SYSREG(2, 0, 0, 15, 4) +#define SYSREG_DBGBCR15_EL1 SYSREG(2, 0, 0, 15, 5) +#define SYSREG_DBGWVR15_EL1 SYSREG(2, 0, 0, 15, 6) +#define SYSREG_DBGWCR15_EL1 SYSREG(2, 0, 0, 15, 7)#define WFX_IS_WFE (1 << 0)
@@ -1041,6 +1106,81 @@ static int hvf_sysreg_write(CPUState *cpu, uint32_t reg, uint64_t val)case SYSREG_OSDLR_EL1 :/* Dummy register */ break ; +case SYSREG_MDSCR_EL1 : + env->cp15.mdscr_el1 = val; +break ; +case SYSREG_DBGBVR0_EL1 : +case SYSREG_DBGBVR1_EL1 : +case SYSREG_DBGBVR2_EL1 : +case SYSREG_DBGBVR3_EL1 : +case SYSREG_DBGBVR4_EL1 : +case SYSREG_DBGBVR5_EL1 : +case SYSREG_DBGBVR6_EL1 : +case SYSREG_DBGBVR7_EL1 : +case SYSREG_DBGBVR8_EL1 : +case SYSREG_DBGBVR9_EL1 : +case SYSREG_DBGBVR10_EL1 : +case SYSREG_DBGBVR11_EL1 : +case SYSREG_DBGBVR12_EL1 : +case SYSREG_DBGBVR13_EL1 : +case SYSREG_DBGBVR14_EL1 : +case SYSREG_DBGBVR15_EL1 : + env->cp15.dbgbvr[SYSREG_CRM (reg)] = val; +break ; +case SYSREG_DBGBCR0_EL1 : +case SYSREG_DBGBCR1_EL1 : +case SYSREG_DBGBCR2_EL1 : +case SYSREG_DBGBCR3_EL1 : +case SYSREG_DBGBCR4_EL1 : +case SYSREG_DBGBCR5_EL1 : +case SYSREG_DBGBCR6_EL1 : +case SYSREG_DBGBCR7_EL1 : +case SYSREG_DBGBCR8_EL1 : +case SYSREG_DBGBCR9_EL1 : +case SYSREG_DBGBCR10_EL1 : +case SYSREG_DBGBCR11_EL1 : +case SYSREG_DBGBCR12_EL1 : +case SYSREG_DBGBCR13_EL1 : +case SYSREG_DBGBCR14_EL1 : +case SYSREG_DBGBCR15_EL1 : + env->cp15.dbgbcr[SYSREG_CRM (reg)] = val; +break ; +case SYSREG_DBGWVR0_EL1 : +case SYSREG_DBGWVR1_EL1 : +case SYSREG_DBGWVR2_EL1 : +case SYSREG_DBGWVR3_EL1 : +case SYSREG_DBGWVR4_EL1 : +case SYSREG_DBGWVR5_EL1 : +case SYSREG_DBGWVR6_EL1 : +case SYSREG_DBGWVR7_EL1 : +case SYSREG_DBGWVR8_EL1 : +case SYSREG_DBGWVR9_EL1 : +case SYSREG_DBGWVR10_EL1 : +case SYSREG_DBGWVR11_EL1 : +case SYSREG_DBGWVR12_EL1 : +case SYSREG_DBGWVR13_EL1 : +case SYSREG_DBGWVR14_EL1 : +case SYSREG_DBGWVR15_EL1 : + env->cp15.dbgwvr[SYSREG_CRM (reg)] = val; +break ; +case SYSREG_DBGWCR0_EL1 : +case SYSREG_DBGWCR1_EL1 : +case SYSREG_DBGWCR2_EL1 : +case SYSREG_DBGWCR3_EL1 : +case SYSREG_DBGWCR4_EL1 : +case SYSREG_DBGWCR5_EL1 : +case SYSREG_DBGWCR6_EL1 : +case SYSREG_DBGWCR7_EL1 : +case SYSREG_DBGWCR8_EL1 : +case SYSREG_DBGWCR9_EL1 : +case SYSREG_DBGWCR10_EL1 : +case SYSREG_DBGWCR11_EL1 : +case SYSREG_DBGWCR12_EL1 : +case SYSREG_DBGWCR13_EL1 : +case SYSREG_DBGWCR14_EL1 : +case SYSREG_DBGWCR15_EL1 : + env->cp15.dbgwcr[SYSREG_CRM (reg)] = val; +break ;default :cpu_synchronize_state (cpu); trace_hvf_unhandled_sysreg_write(env->pc, reg,
Signature · 1 line
2.38.1
Re: [PATCH 3/3] hvf: handle writes of MDSCR_EL1 and DBG*_EL1
Quoted 10 lines · On 4 Nov 2022, at 19.41, [email protected] wrote:
From: Francesco Cagnin <[email protected]>
This proved to be required when debugging the Linux kernel's initial code, as the Hypervisor framework was triggering '
EC_SYSTEMREGISTERTRAP' VM exits after enabling trap exceptions with 'hv_vcpu_set_trap_debug_exceptions()'.
Quoted 4 lines
Signed-off-by Francesco Cagnin <[email protected]>---
target/arm/hvf/hvf.c| 140 +++++++++++++++++++++++++++++++++++++++++++1 file changed, 140 insertions(+)