[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20201027093607.956147736@infradead.org>
Date: Tue, 27 Oct 2020 10:15:05 +0100
From: Peter Zijlstra <peterz@...radead.org>
To: tglx@...utronix.de, luto@...nel.org, me@...ehuey.com
Cc: x86@...nel.org, linux-kernel@...r.kernel.org,
torvalds@...ux-foundation.org, rocallahan@...il.com,
alexandre.chartre@...cle.com, paulmck@...nel.org,
frederic@...nel.org, pbonzini@...hat.com,
sean.j.christopherson@...el.com, mhiramat@...nel.org,
pmladek@...e.com, joel@...lfernandes.org, rostedt@...dmis.org,
boris.ostrovsky@...cle.com, jgross@...e.com, brgerst@...il.com,
jpoimboe@...hat.com, daniel.thompson@...aro.org,
julliard@...ehq.org, pgofman@...eweavers.com, peterz@...radead.org
Subject: [PATCH 1/3] x86/debug: Fix BTF handling
The SDM states that #DB clears DEBUGCTLMSR_BTF, this means that when
we set that bit for userspace (TIF_BLOCKSTEP) and get a kernel #DB
first, we'll loose the BTF bit meant for userspace execution.
Have the kernel #DB handler restore the BTF bit when it was requested
for userspace.
Signed-off-by: Peter Zijlstra (Intel) <peterz@...radead.org>
---
arch/x86/kernel/traps.c | 28 +++++++++++++++++++++-------
1 file changed, 21 insertions(+), 7 deletions(-)
--- a/arch/x86/kernel/traps.c
+++ b/arch/x86/kernel/traps.c
@@ -799,13 +799,6 @@ static __always_inline unsigned long deb
*/
current->thread.virtual_dr6 = 0;
- /*
- * The SDM says "The processor clears the BTF flag when it
- * generates a debug exception." Clear TIF_BLOCKSTEP to keep
- * TIF_BLOCKSTEP in sync with the hardware BTF flag.
- */
- clear_thread_flag(TIF_BLOCKSTEP);
-
return dr6;
}
@@ -873,6 +866,20 @@ static __always_inline void exc_debug_ke
*/
WARN_ON_ONCE(user_mode(regs));
+ if (test_thread_flag(TIF_BLOCKSTEP)) {
+ /*
+ * The SDM says "The processor clears the BTF flag when it
+ * generates a debug exception." but PTRACE_BLOCKSTEP requested
+ * it for userspace, but we just took a kernel #DB, so re-set
+ * BTF.
+ */
+ unsigned long debugctl;
+
+ rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
+ debugctl |= DEBUGCTLMSR_BTF;
+ wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl);
+ }
+
/*
* Catch SYSENTER with TF set and clear DR_STEP. If this hit a
* watchpoint at the same time then that will still be handled.
@@ -936,6 +943,13 @@ static __always_inline void exc_debug_us
instrumentation_begin();
/*
+ * The SDM says "The processor clears the BTF flag when it
+ * generates a debug exception." Clear TIF_BLOCKSTEP to keep
+ * TIF_BLOCKSTEP in sync with the hardware BTF flag.
+ */
+ clear_thread_flag(TIF_BLOCKSTEP);
+
+ /*
* 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.
Powered by blists - more mailing lists