git: d8db6b4f079e - stable/13 - arm64: Mask non-debug exceptions when single stepping

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Mon, 15 Jul 2024 12:38:08 UTC
The branch stable/13 has been updated by andrew:

URL: https://cgit.FreeBSD.org/src/commit/?id=d8db6b4f079efc2de3195392a624ac2d47c3997f

commit d8db6b4f079efc2de3195392a624ac2d47c3997f
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2024-03-14 14:02:56 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2024-07-15 12:34:28 +0000

    arm64: Mask non-debug exceptions when single stepping
    
    When an exception is pending when single stepping we may execute the
    handler for that exception rather than the single step handler. This
    could cause the scheduler to fire to run a new thread. This will mean
    we single step to a new thread causing unexpected results.
    
    Handle this by masking non-debug exceptions. This will cause issues
    when stepping over instructions that access the DAIF values so future
    work is needed to handle these cases, but for most code this now works
    as expected.
    
    Reviewed by:    jhb
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D44350
    
    (cherry picked from commit ed3c6cd76de8560c46607abe506a03568e9acab2)
---
 sys/arm64/arm64/debug_monitor.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/sys/arm64/arm64/debug_monitor.c b/sys/arm64/arm64/debug_monitor.c
index 812f44398a67..6de148161a7f 100644
--- a/sys/arm64/arm64/debug_monitor.c
+++ b/sys/arm64/arm64/debug_monitor.c
@@ -188,6 +188,15 @@ kdb_cpu_set_singlestep(void)
 	    ("%s: debug exceptions are not masked", __func__));
 
 	kdb_frame->tf_spsr |= PSR_SS;
+
+	/*
+	 * TODO: Handle single stepping over instructions that access
+	 * the DAIF values. On a read the value will be incorrect.
+	 */
+	kernel_monitor.dbg_flags &= ~PSR_DAIF;
+	kernel_monitor.dbg_flags |= kdb_frame->tf_spsr & PSR_DAIF;
+	kdb_frame->tf_spsr |= (PSR_A | PSR_I | PSR_F);
+
 	WRITE_SPECIALREG(mdscr_el1, READ_SPECIALREG(mdscr_el1) |
 	    MDSCR_SS | MDSCR_KDE);
 
@@ -209,6 +218,9 @@ kdb_cpu_clear_singlestep(void)
 	KASSERT((READ_SPECIALREG(daif) & PSR_D) == PSR_D,
 	    ("%s: debug exceptions are not masked", __func__));
 
+	kdb_frame->tf_spsr &= ~PSR_DAIF;
+	kdb_frame->tf_spsr |= kernel_monitor.dbg_flags & PSR_DAIF;
+
 	WRITE_SPECIALREG(mdscr_el1, READ_SPECIALREG(mdscr_el1) &
 	    ~(MDSCR_SS | MDSCR_KDE));