[PATCH v2 6/8] x86/debug: Move historical SYSENTER junk into exc_debug_kernel()

From: Peter Zijlstra
Date: Fri Aug 21 2020 - 06:22:00 EST


The historical SYSENTER junk is explicitly for from-kernel, so move it
to the #DB-from-kernel handler.

It is ordered after the notifier, this is important for KGDB which
uses TF single-step and needs to consume the event before we get here.

Signed-off-by: Peter Zijlstra (Intel) <peterz@xxxxxxxxxxxxx>
---
arch/x86/kernel/traps.c | 47 ++++++++++++++++++++++++-----------------------
1 file changed, 24 insertions(+), 23 deletions(-)

--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -783,17 +783,15 @@ static __always_inline unsigned long deb
*
* May run on IST stack.
*/
-static void handle_debug(struct pt_regs *regs, unsigned long dr6)
+static bool handle_debug(struct pt_regs *regs, unsigned long *dr6)
{
struct task_struct *tsk = current;

/* Store the virtualized DR6 value */
- tsk->thread.debugreg6 = dr6;
+ tsk->thread.debugreg6 = *dr6;

- if (notify_die(DIE_DEBUG, "debug", regs, (long)&dr6, 0,
- SIGTRAP) == NOTIFY_STOP) {
- return;
- }
+ if (notify_die(DIE_DEBUG, "debug", regs, (long)dr6, 0, SIGTRAP) == NOTIFY_STOP)
+ return true;

/* It's safe to allow irq's after DR6 has been saved */
cond_local_irq_enable(regs);
@@ -807,30 +805,20 @@ static void handle_debug(struct pt_regs
/*
* Reload dr6, the notifier might have changed it.
*/
- dr6 = tsk->thread.debugreg6;
-
- if (WARN_ON_ONCE((dr6 & DR_STEP) && !user_mode(regs))) {
- /*
- * Historical junk that used to handle SYSENTER single-stepping.
- * This should be unreachable now. If we survive for a while
- * without anyone hitting this warning, we'll turn this into
- * an oops.
- */
- tsk->thread.debugreg6 &= ~DR_STEP;
- set_tsk_thread_flag(tsk, TIF_SINGLESTEP);
- regs->flags &= ~X86_EFLAGS_TF;
- }
+ *dr6 = tsk->thread.debugreg6;

/*
* If dr6 has no reason to give us about the origin of this trap,
* then it's very likely the result of an icebp/int01 trap.
* User wants a sigtrap for that.
*/
- if (dr6 & (DR_STEP | DR_TRAP_BITS) || !dr6)
- send_sigtrap(regs, 0, get_si_code(dr6));
+ if (*dr6 & (DR_STEP | DR_TRAP_BITS) || !*dr6)
+ send_sigtrap(regs, 0, get_si_code(*dr6));

out:
cond_local_irq_disable(regs);
+
+ return false;
}

static __always_inline void exc_debug_kernel(struct pt_regs *regs,
@@ -873,7 +861,20 @@ static __always_inline void exc_debug_ke
if (!dr6)
goto out;

- handle_debug(regs, dr6);
+ if (handle_debug(regs, &dr6))
+ goto out;
+
+ if (WARN_ON_ONCE(dr6 & DR_STEP)) {
+ /*
+ * Historical junk that used to handle SYSENTER single-stepping.
+ * This should be unreachable now. If we survive for a while
+ * without anyone hitting this warning, we'll turn this into
+ * an oops.
+ */
+ dr6 &= ~DR_STEP;
+ set_thread_flag(TIF_SINGLESTEP);
+ regs->flags &= ~X86_EFLAGS_TF;
+ }

out:
instrumentation_end();
@@ -903,7 +904,7 @@ static __always_inline void exc_debug_us
irqentry_enter_from_user_mode(regs);
instrumentation_begin();

- handle_debug(regs, dr6);
+ handle_debug(regs, &dr6);

instrumentation_end();
irqentry_exit_to_user_mode(regs);