qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] Fix GDB support for macOS hvf
@ 2025-04-02 13:52 Mads Ynddal
  2025-04-02 13:52 ` [PATCH 1/2] hvf: avoid repeatedly setting trap debug for each cpu Mads Ynddal
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Mads Ynddal @ 2025-04-02 13:52 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-arm, Phil Dennis-Jordan, Cameron Esfahani, Roman Bolshakov,
	Peter Maydell, Alexander Graf, Mads Ynddal

From: Mads Ynddal <m.ynddal@samsung.com>

In (recent versions of?) macOS, calls to hv_vcpu_set_sys_reg were failing if
they were issued outside of the specific thread that owns the vCPU.

This caused a crash when attaching a debugger through the GDB stub.

This GDB stub has worked before, so it is unclear if Apple changed the
behavior of the function in a release of macOS.

Mads Ynddal (2):
  hvf: avoid repeatedly setting trap debug for each cpu
  hvf: only update sysreg from owning thread

 accel/hvf/hvf-all.c  |  7 ++++++-
 target/arm/hvf/hvf.c | 27 +++++++++++----------------
 2 files changed, 17 insertions(+), 17 deletions(-)

-- 
2.48.1



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH 1/2] hvf: avoid repeatedly setting trap debug for each cpu
  2025-04-02 13:52 [PATCH 0/2] Fix GDB support for macOS hvf Mads Ynddal
@ 2025-04-02 13:52 ` Mads Ynddal
  2025-04-02 18:15   ` Daniel Gomez
  2025-04-14  9:20   ` Alex Bennée
  2025-04-02 13:52 ` [PATCH 2/2] hvf: only update sysreg from owning thread Mads Ynddal
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 9+ messages in thread
From: Mads Ynddal @ 2025-04-02 13:52 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-arm, Phil Dennis-Jordan, Cameron Esfahani, Roman Bolshakov,
	Peter Maydell, Alexander Graf, Mads Ynddal, Daniel Gomez

From: Mads Ynddal <m.ynddal@samsung.com>

hvf_arch_set_traps is already called from a context of a specific
CPUState, so we don't need to do a nested CPU_FOREACH.

It also results in an error from hv_vcpu_set_sys_reg, as it may only be
called from the thread owning the vCPU.

Tested-by: Daniel Gomez <da.gomez@samsung.com>
Signed-off-by: Mads Ynddal <m.ynddal@samsung.com>
---
 target/arm/hvf/hvf.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)

diff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c
index 2439af63a0..48e4b12725 100644
--- a/target/arm/hvf/hvf.c
+++ b/target/arm/hvf/hvf.c
@@ -2277,28 +2277,23 @@ static inline bool hvf_arm_hw_debug_active(CPUState *cpu)
     return ((cur_hw_wps > 0) || (cur_hw_bps > 0));
 }
 
-static void hvf_arch_set_traps(void)
+static void hvf_arch_set_traps(CPUState *cpu)
 {
-    CPUState *cpu;
     bool should_enable_traps = false;
     hv_return_t r = HV_SUCCESS;
 
     /* Check whether guest debugging is enabled for at least one vCPU; if it
      * is, enable exiting the guest on all vCPUs */
-    CPU_FOREACH(cpu) {
-        should_enable_traps |= cpu->accel->guest_debug_enabled;
-    }
-    CPU_FOREACH(cpu) {
-        /* Set whether debug exceptions exit the guest */
-        r = hv_vcpu_set_trap_debug_exceptions(cpu->accel->fd,
-                                              should_enable_traps);
-        assert_hvf_ok(r);
+    should_enable_traps |= cpu->accel->guest_debug_enabled;
+    /* Set whether debug exceptions exit the guest */
+    r = hv_vcpu_set_trap_debug_exceptions(cpu->accel->fd,
+                                            should_enable_traps);
+    assert_hvf_ok(r);
 
-        /* Set whether accesses to debug registers exit the guest */
-        r = hv_vcpu_set_trap_debug_reg_accesses(cpu->accel->fd,
-                                                should_enable_traps);
-        assert_hvf_ok(r);
-    }
+    /* Set whether accesses to debug registers exit the guest */
+    r = hv_vcpu_set_trap_debug_reg_accesses(cpu->accel->fd,
+                                            should_enable_traps);
+    assert_hvf_ok(r);
 }
 
 void hvf_arch_update_guest_debug(CPUState *cpu)
@@ -2339,7 +2334,7 @@ void hvf_arch_update_guest_debug(CPUState *cpu)
             deposit64(env->cp15.mdscr_el1, MDSCR_EL1_MDE_SHIFT, 1, 0);
     }
 
-    hvf_arch_set_traps();
+    hvf_arch_set_traps(cpu);
 }
 
 bool hvf_arch_supports_guest_debug(void)
-- 
2.48.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/2] hvf: only update sysreg from owning thread
  2025-04-02 13:52 [PATCH 0/2] Fix GDB support for macOS hvf Mads Ynddal
  2025-04-02 13:52 ` [PATCH 1/2] hvf: avoid repeatedly setting trap debug for each cpu Mads Ynddal
@ 2025-04-02 13:52 ` Mads Ynddal
  2025-04-14  9:11   ` Alex Bennée
  2025-05-02 12:55   ` Peter Maydell
  2025-04-14  8:07 ` [PATCH 0/2] Fix GDB support for macOS hvf Mads Ynddal
  2025-05-02 12:57 ` Peter Maydell
  3 siblings, 2 replies; 9+ messages in thread
From: Mads Ynddal @ 2025-04-02 13:52 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-arm, Phil Dennis-Jordan, Cameron Esfahani, Roman Bolshakov,
	Peter Maydell, Alexander Graf, Mads Ynddal, Daniel Gomez

From: Mads Ynddal <m.ynddal@samsung.com>

hv_vcpu_set_sys_reg should only be called from the owning thread of the
vCPU, so to avoid crashes, the call to hvf_update_guest_debug is
dispatched to the individual threads.

Tested-by: Daniel Gomez <da.gomez@samsung.com>
Signed-off-by: Mads Ynddal <m.ynddal@samsung.com>
---
 accel/hvf/hvf-all.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/accel/hvf/hvf-all.c b/accel/hvf/hvf-all.c
index d404e01ade..3fc65d6b23 100644
--- a/accel/hvf/hvf-all.c
+++ b/accel/hvf/hvf-all.c
@@ -58,8 +58,13 @@ int hvf_sw_breakpoints_active(CPUState *cpu)
     return !QTAILQ_EMPTY(&hvf_state->hvf_sw_breakpoints);
 }
 
-int hvf_update_guest_debug(CPUState *cpu)
+static void do_hvf_update_guest_debug(CPUState *cpu, run_on_cpu_data arg)
 {
     hvf_arch_update_guest_debug(cpu);
+}
+
+int hvf_update_guest_debug(CPUState *cpu)
+{
+    run_on_cpu(cpu, do_hvf_update_guest_debug, RUN_ON_CPU_NULL);
     return 0;
 }
-- 
2.48.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] hvf: avoid repeatedly setting trap debug for each cpu
  2025-04-02 13:52 ` [PATCH 1/2] hvf: avoid repeatedly setting trap debug for each cpu Mads Ynddal
@ 2025-04-02 18:15   ` Daniel Gomez
  2025-04-14  9:20   ` Alex Bennée
  1 sibling, 0 replies; 9+ messages in thread
From: Daniel Gomez @ 2025-04-02 18:15 UTC (permalink / raw)
  To: Mads Ynddal
  Cc: qemu-devel, qemu-arm, Phil Dennis-Jordan, Cameron Esfahani,
	Roman Bolshakov, Peter Maydell, Alexander Graf, Mads Ynddal,
	Daniel Gomez

On Wed, Apr 02, 2025 at 03:52:28PM +0100, Mads Ynddal wrote:
> From: Mads Ynddal <m.ynddal@samsung.com>
> 
> hvf_arch_set_traps is already called from a context of a specific
> CPUState, so we don't need to do a nested CPU_FOREACH.
> 
> It also results in an error from hv_vcpu_set_sys_reg, as it may only be
> called from the thread owning the vCPU.
> 

Reported-by: Daniel Gomez <da.gomez@samsung.com>
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2895

> Tested-by: Daniel Gomez <da.gomez@samsung.com>
> Signed-off-by: Mads Ynddal <m.ynddal@samsung.com>
> ---
>  target/arm/hvf/hvf.c | 27 +++++++++++----------------
>  1 file changed, 11 insertions(+), 16 deletions(-)
> 
> diff --git a/target/arm/hvf/hvf.c b/target/arm/hvf/hvf.c
> index 2439af63a0..48e4b12725 100644
> --- a/target/arm/hvf/hvf.c
> +++ b/target/arm/hvf/hvf.c
> @@ -2277,28 +2277,23 @@ static inline bool hvf_arm_hw_debug_active(CPUState *cpu)
>      return ((cur_hw_wps > 0) || (cur_hw_bps > 0));
>  }
>  
> -static void hvf_arch_set_traps(void)
> +static void hvf_arch_set_traps(CPUState *cpu)
>  {
> -    CPUState *cpu;
>      bool should_enable_traps = false;
>      hv_return_t r = HV_SUCCESS;
>  
>      /* Check whether guest debugging is enabled for at least one vCPU; if it
>       * is, enable exiting the guest on all vCPUs */
> -    CPU_FOREACH(cpu) {
> -        should_enable_traps |= cpu->accel->guest_debug_enabled;
> -    }
> -    CPU_FOREACH(cpu) {
> -        /* Set whether debug exceptions exit the guest */
> -        r = hv_vcpu_set_trap_debug_exceptions(cpu->accel->fd,
> -                                              should_enable_traps);
> -        assert_hvf_ok(r);
> +    should_enable_traps |= cpu->accel->guest_debug_enabled;
> +    /* Set whether debug exceptions exit the guest */
> +    r = hv_vcpu_set_trap_debug_exceptions(cpu->accel->fd,
> +                                            should_enable_traps);
> +    assert_hvf_ok(r);
>  
> -        /* Set whether accesses to debug registers exit the guest */
> -        r = hv_vcpu_set_trap_debug_reg_accesses(cpu->accel->fd,
> -                                                should_enable_traps);
> -        assert_hvf_ok(r);
> -    }
> +    /* Set whether accesses to debug registers exit the guest */
> +    r = hv_vcpu_set_trap_debug_reg_accesses(cpu->accel->fd,
> +                                            should_enable_traps);
> +    assert_hvf_ok(r);
>  }
>  
>  void hvf_arch_update_guest_debug(CPUState *cpu)
> @@ -2339,7 +2334,7 @@ void hvf_arch_update_guest_debug(CPUState *cpu)
>              deposit64(env->cp15.mdscr_el1, MDSCR_EL1_MDE_SHIFT, 1, 0);
>      }
>  
> -    hvf_arch_set_traps();
> +    hvf_arch_set_traps(cpu);
>  }
>  
>  bool hvf_arch_supports_guest_debug(void)
> -- 
> 2.48.1
> 
> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/2] Fix GDB support for macOS hvf
  2025-04-02 13:52 [PATCH 0/2] Fix GDB support for macOS hvf Mads Ynddal
  2025-04-02 13:52 ` [PATCH 1/2] hvf: avoid repeatedly setting trap debug for each cpu Mads Ynddal
  2025-04-02 13:52 ` [PATCH 2/2] hvf: only update sysreg from owning thread Mads Ynddal
@ 2025-04-14  8:07 ` Mads Ynddal
  2025-05-02 12:57 ` Peter Maydell
  3 siblings, 0 replies; 9+ messages in thread
From: Mads Ynddal @ 2025-04-14  8:07 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-arm, Phil Dennis-Jordan, Cameron Esfahani, Roman Bolshakov,
	Peter Maydell, Alexander Graf, Mads Ynddal, Daniel Gomez


> In (recent versions of?) macOS, calls to hv_vcpu_set_sys_reg were failing if
> they were issued outside of the specific thread that owns the vCPU.
> 
> This caused a crash when attaching a debugger through the GDB stub.
> 
> This GDB stub has worked before, so it is unclear if Apple changed the
> behavior of the function in a release of macOS.
> 
> Mads Ynddal (2):
>  hvf: avoid repeatedly setting trap debug for each cpu
>  hvf: only update sysreg from owning thread
> 
> accel/hvf/hvf-all.c  |  7 ++++++-
> target/arm/hvf/hvf.c | 27 +++++++++++----------------
> 2 files changed, 17 insertions(+), 17 deletions(-)
> 

Daniel Gomez and I have been using this patch for the past two weeks. It seems to be the right fix for the issue.

—
Mads Ynddal

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] hvf: only update sysreg from owning thread
  2025-04-02 13:52 ` [PATCH 2/2] hvf: only update sysreg from owning thread Mads Ynddal
@ 2025-04-14  9:11   ` Alex Bennée
  2025-05-02 12:55   ` Peter Maydell
  1 sibling, 0 replies; 9+ messages in thread
From: Alex Bennée @ 2025-04-14  9:11 UTC (permalink / raw)
  To: Mads Ynddal
  Cc: qemu-devel, qemu-arm, Phil Dennis-Jordan, Cameron Esfahani,
	Roman Bolshakov, Peter Maydell, Alexander Graf, Mads Ynddal,
	Daniel Gomez

Mads Ynddal <mads@ynddal.dk> writes:

> From: Mads Ynddal <m.ynddal@samsung.com>
>
> hv_vcpu_set_sys_reg should only be called from the owning thread of the
> vCPU, so to avoid crashes, the call to hvf_update_guest_debug is
> dispatched to the individual threads.
>
> Tested-by: Daniel Gomez <da.gomez@samsung.com>
> Signed-off-by: Mads Ynddal <m.ynddal@samsung.com>

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] hvf: avoid repeatedly setting trap debug for each cpu
  2025-04-02 13:52 ` [PATCH 1/2] hvf: avoid repeatedly setting trap debug for each cpu Mads Ynddal
  2025-04-02 18:15   ` Daniel Gomez
@ 2025-04-14  9:20   ` Alex Bennée
  1 sibling, 0 replies; 9+ messages in thread
From: Alex Bennée @ 2025-04-14  9:20 UTC (permalink / raw)
  To: Mads Ynddal
  Cc: qemu-devel, qemu-arm, Phil Dennis-Jordan, Cameron Esfahani,
	Roman Bolshakov, Peter Maydell, Alexander Graf, Mads Ynddal,
	Daniel Gomez

Mads Ynddal <mads@ynddal.dk> writes:

> From: Mads Ynddal <m.ynddal@samsung.com>
>
> hvf_arch_set_traps is already called from a context of a specific
> CPUState, so we don't need to do a nested CPU_FOREACH.
>
> It also results in an error from hv_vcpu_set_sys_reg, as it may only be
> called from the thread owning the vCPU.
>
> Tested-by: Daniel Gomez <da.gomez@samsung.com>
> Signed-off-by: Mads Ynddal <m.ynddal@samsung.com>

Looks reasonable to me:

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] hvf: only update sysreg from owning thread
  2025-04-02 13:52 ` [PATCH 2/2] hvf: only update sysreg from owning thread Mads Ynddal
  2025-04-14  9:11   ` Alex Bennée
@ 2025-05-02 12:55   ` Peter Maydell
  1 sibling, 0 replies; 9+ messages in thread
From: Peter Maydell @ 2025-05-02 12:55 UTC (permalink / raw)
  To: Mads Ynddal
  Cc: qemu-devel, qemu-arm, Phil Dennis-Jordan, Cameron Esfahani,
	Roman Bolshakov, Alexander Graf, Mads Ynddal, Daniel Gomez

On Wed, 2 Apr 2025 at 14:52, Mads Ynddal <mads@ynddal.dk> wrote:
>
> From: Mads Ynddal <m.ynddal@samsung.com>
>
> hv_vcpu_set_sys_reg should only be called from the owning thread of the
> vCPU, so to avoid crashes, the call to hvf_update_guest_debug is
> dispatched to the individual threads.
>
> Tested-by: Daniel Gomez <da.gomez@samsung.com>
> Signed-off-by: Mads Ynddal <m.ynddal@samsung.com>
> ---
>  accel/hvf/hvf-all.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/accel/hvf/hvf-all.c b/accel/hvf/hvf-all.c
> index d404e01ade..3fc65d6b23 100644
> --- a/accel/hvf/hvf-all.c
> +++ b/accel/hvf/hvf-all.c
> @@ -58,8 +58,13 @@ int hvf_sw_breakpoints_active(CPUState *cpu)
>      return !QTAILQ_EMPTY(&hvf_state->hvf_sw_breakpoints);
>  }
>
> -int hvf_update_guest_debug(CPUState *cpu)
> +static void do_hvf_update_guest_debug(CPUState *cpu, run_on_cpu_data arg)
>  {
>      hvf_arch_update_guest_debug(cpu);
> +}
> +
> +int hvf_update_guest_debug(CPUState *cpu)
> +{
> +    run_on_cpu(cpu, do_hvf_update_guest_debug, RUN_ON_CPU_NULL);
>      return 0;
>  }

run_on_cpu() implicitly drops the BQL, so it is a potential
really nasty footgun in this kind of situation where callers
to update_guest_debug aren't expecting the BQL to be dropped
on them. But we already use run_on_cpu() in the KVM equivalent
kvm_update_guest_debug(), so presumably it's also OK here...

-- PMM


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 0/2] Fix GDB support for macOS hvf
  2025-04-02 13:52 [PATCH 0/2] Fix GDB support for macOS hvf Mads Ynddal
                   ` (2 preceding siblings ...)
  2025-04-14  8:07 ` [PATCH 0/2] Fix GDB support for macOS hvf Mads Ynddal
@ 2025-05-02 12:57 ` Peter Maydell
  3 siblings, 0 replies; 9+ messages in thread
From: Peter Maydell @ 2025-05-02 12:57 UTC (permalink / raw)
  To: Mads Ynddal
  Cc: qemu-devel, qemu-arm, Phil Dennis-Jordan, Cameron Esfahani,
	Roman Bolshakov, Alexander Graf, Mads Ynddal

On Wed, 2 Apr 2025 at 14:52, Mads Ynddal <mads@ynddal.dk> wrote:
>
> From: Mads Ynddal <m.ynddal@samsung.com>
>
> In (recent versions of?) macOS, calls to hv_vcpu_set_sys_reg were failing if
> they were issued outside of the specific thread that owns the vCPU.
>
> This caused a crash when attaching a debugger through the GDB stub.
>
> This GDB stub has worked before, so it is unclear if Apple changed the
> behavior of the function in a release of macOS.



Applied to target-arm.next, thanks.

-- PMM


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-05-02 12:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-02 13:52 [PATCH 0/2] Fix GDB support for macOS hvf Mads Ynddal
2025-04-02 13:52 ` [PATCH 1/2] hvf: avoid repeatedly setting trap debug for each cpu Mads Ynddal
2025-04-02 18:15   ` Daniel Gomez
2025-04-14  9:20   ` Alex Bennée
2025-04-02 13:52 ` [PATCH 2/2] hvf: only update sysreg from owning thread Mads Ynddal
2025-04-14  9:11   ` Alex Bennée
2025-05-02 12:55   ` Peter Maydell
2025-04-14  8:07 ` [PATCH 0/2] Fix GDB support for macOS hvf Mads Ynddal
2025-05-02 12:57 ` Peter Maydell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).