@@ -745,9 +745,21 @@ static __always_inline unsigned long debug_read_clear_dr6(void)
745745 * Keep it simple: clear DR6 immediately.
746746 */
747747 get_debugreg (dr6 , 6 );
748- set_debugreg (0 , 6 );
749- /* Filter out all the reserved bits which are preset to 1 */
750- dr6 &= ~DR6_RESERVED ;
748+ set_debugreg (DR6_RESERVED , 6 );
749+ dr6 ^= DR6_RESERVED ; /* Flip to positive polarity */
750+
751+ /*
752+ * Clear the virtual DR6 value, ptrace routines will set bits here for
753+ * things we want signals for.
754+ */
755+ current -> thread .virtual_dr6 = 0 ;
756+
757+ /*
758+ * The SDM says "The processor clears the BTF flag when it
759+ * generates a debug exception." Clear TIF_BLOCKSTEP to keep
760+ * TIF_BLOCKSTEP in sync with the hardware BTF flag.
761+ */
762+ clear_thread_flag (TIF_BLOCKSTEP );
751763
752764 return dr6 ;
753765}
@@ -776,74 +788,20 @@ static __always_inline unsigned long debug_read_clear_dr6(void)
776788 *
777789 * May run on IST stack.
778790 */
779- static void handle_debug (struct pt_regs * regs , unsigned long dr6 , bool user )
780- {
781- struct task_struct * tsk = current ;
782- bool user_icebp ;
783- int si_code ;
784-
785- /*
786- * The SDM says "The processor clears the BTF flag when it
787- * generates a debug exception." Clear TIF_BLOCKSTEP to keep
788- * TIF_BLOCKSTEP in sync with the hardware BTF flag.
789- */
790- clear_thread_flag (TIF_BLOCKSTEP );
791-
792- /*
793- * If DR6 is zero, no point in trying to handle it. The kernel is
794- * not using INT1.
795- */
796- if (!user && !dr6 )
797- return ;
798791
792+ static bool notify_debug (struct pt_regs * regs , unsigned long * dr6 )
793+ {
799794 /*
800- * If dr6 has no reason to give us about the origin of this trap,
801- * then it's very likely the result of an icebp/int01 trap.
802- * User wants a sigtrap for that.
795+ * Notifiers will clear bits in @dr6 to indicate the event has been
796+ * consumed - hw_breakpoint_handler(), single_stop_cont().
797+ *
798+ * Notifiers will set bits in @virtual_dr6 to indicate the desire
799+ * for signals - ptrace_triggered(), kgdb_hw_overflow_handler().
803800 */
804- user_icebp = user && !dr6 ;
805-
806- /* Store the virtualized DR6 value */
807- tsk -> thread .debugreg6 = dr6 ;
808-
809- #ifdef CONFIG_KPROBES
810- if (kprobe_debug_handler (regs )) {
811- return ;
812- }
813- #endif
814-
815- if (notify_die (DIE_DEBUG , "debug" , regs , (long )& dr6 , 0 ,
816- SIGTRAP ) == NOTIFY_STOP ) {
817- return ;
818- }
819-
820- /* It's safe to allow irq's after DR6 has been saved */
821- cond_local_irq_enable (regs );
822-
823- if (v8086_mode (regs )) {
824- handle_vm86_trap ((struct kernel_vm86_regs * ) regs , 0 ,
825- X86_TRAP_DB );
826- goto out ;
827- }
828-
829- if (WARN_ON_ONCE ((dr6 & DR_STEP ) && !user_mode (regs ))) {
830- /*
831- * Historical junk that used to handle SYSENTER single-stepping.
832- * This should be unreachable now. If we survive for a while
833- * without anyone hitting this warning, we'll turn this into
834- * an oops.
835- */
836- tsk -> thread .debugreg6 &= ~DR_STEP ;
837- set_tsk_thread_flag (tsk , TIF_SINGLESTEP );
838- regs -> flags &= ~X86_EFLAGS_TF ;
839- }
840-
841- si_code = get_si_code (tsk -> thread .debugreg6 );
842- if (tsk -> thread .debugreg6 & (DR_STEP | DR_TRAP_BITS ) || user_icebp )
843- send_sigtrap (regs , 0 , si_code );
801+ if (notify_die (DIE_DEBUG , "debug" , regs , (long )dr6 , 0 , SIGTRAP ) == NOTIFY_STOP )
802+ return true;
844803
845- out :
846- cond_local_irq_disable (regs );
804+ return false;
847805}
848806
849807static __always_inline void exc_debug_kernel (struct pt_regs * regs ,
@@ -877,8 +835,32 @@ static __always_inline void exc_debug_kernel(struct pt_regs *regs,
877835 if ((dr6 & DR_STEP ) && is_sysenter_singlestep (regs ))
878836 dr6 &= ~DR_STEP ;
879837
880- handle_debug (regs , dr6 , false);
838+ if (kprobe_debug_handler (regs ))
839+ goto out ;
840+
841+ /*
842+ * The kernel doesn't use INT1
843+ */
844+ if (!dr6 )
845+ goto out ;
881846
847+ if (notify_debug (regs , & dr6 ))
848+ goto out ;
849+
850+ /*
851+ * The kernel doesn't use TF single-step outside of:
852+ *
853+ * - Kprobes, consumed through kprobe_debug_handler()
854+ * - KGDB, consumed through notify_debug()
855+ *
856+ * So if we get here with DR_STEP set, something is wonky.
857+ *
858+ * A known way to trigger this is through QEMU's GDB stub,
859+ * which leaks #DB into the guest and causes IST recursion.
860+ */
861+ if (WARN_ON_ONCE (dr6 & DR_STEP ))
862+ regs -> flags &= ~X86_EFLAGS_TF ;
863+ out :
882864 instrumentation_end ();
883865 idtentry_exit_nmi (regs , irq_state );
884866
@@ -888,6 +870,8 @@ static __always_inline void exc_debug_kernel(struct pt_regs *regs,
888870static __always_inline void exc_debug_user (struct pt_regs * regs ,
889871 unsigned long dr6 )
890872{
873+ bool icebp ;
874+
891875 /*
892876 * If something gets miswired and we end up here for a kernel mode
893877 * #DB, we will malfunction.
@@ -906,8 +890,32 @@ static __always_inline void exc_debug_user(struct pt_regs *regs,
906890 irqentry_enter_from_user_mode (regs );
907891 instrumentation_begin ();
908892
909- handle_debug (regs , dr6 , true);
893+ /*
894+ * If dr6 has no reason to give us about the origin of this trap,
895+ * then it's very likely the result of an icebp/int01 trap.
896+ * User wants a sigtrap for that.
897+ */
898+ icebp = !dr6 ;
910899
900+ if (notify_debug (regs , & dr6 ))
901+ goto out ;
902+
903+ /* It's safe to allow irq's after DR6 has been saved */
904+ local_irq_enable ();
905+
906+ if (v8086_mode (regs )) {
907+ handle_vm86_trap ((struct kernel_vm86_regs * )regs , 0 , X86_TRAP_DB );
908+ goto out_irq ;
909+ }
910+
911+ /* Add the virtual_dr6 bits for signals. */
912+ dr6 |= current -> thread .virtual_dr6 ;
913+ if (dr6 & (DR_STEP | DR_TRAP_BITS ) || icebp )
914+ send_sigtrap (regs , 0 , get_si_code (dr6 ));
915+
916+ out_irq :
917+ local_irq_disable ();
918+ out :
911919 instrumentation_end ();
912920 irqentry_exit_to_user_mode (regs );
913921}
0 commit comments