extern struct desc_ptr idt_descr;
 extern gate_desc idt_table[];
-extern struct desc_ptr nmi_idt_descr;
-extern gate_desc nmi_idt_table[];
+extern struct desc_ptr debug_idt_descr;
+extern gate_desc debug_idt_table[];
 
 struct gdt_page {
        struct desc_struct gdt[GDT_ENTRIES];
        gate_desc s;
 
        pack_gate(&s, GATE_INTERRUPT, (unsigned long)addr, 0, 0, __KERNEL_CS);
-       write_idt_entry(nmi_idt_table, gate, &s);
+       write_idt_entry(debug_idt_table, gate, &s);
 }
 #endif
 
        _set_gate(n, GATE_INTERRUPT, addr, 0x3, ist, __KERNEL_CS);
 }
 
+#ifdef CONFIG_X86_64
+DECLARE_PER_CPU(u32, debug_idt_ctr);
+static inline bool is_debug_idt_enabled(void)
+{
+       if (this_cpu_read(debug_idt_ctr))
+               return true;
+
+       return false;
+}
+
+static inline void load_debug_idt(void)
+{
+       load_idt((const struct desc_ptr *)&debug_idt_descr);
+}
+#else
+static inline bool is_debug_idt_enabled(void)
+{
+       return false;
+}
+
+static inline void load_debug_idt(void)
+{
+}
+#endif
+
+/*
+ * the load_current_idt() is called with interrupt disabled by local_irq_save()
+ * to avoid races. That way the IDT will always be set back to the expected
+ * descriptor.
+ */
+static inline void load_current_idt(void)
+{
+       unsigned long flags;
+
+       local_irq_save(flags);
+       if (is_debug_idt_enabled())
+               load_debug_idt();
+       else
+               load_idt((const struct desc_ptr *)&idt_descr);
+       local_irq_restore(flags);
+}
 #endif /* _ASM_X86_DESC_H */
 
 
 #ifdef CONFIG_X86_64
 struct desc_ptr idt_descr = { NR_VECTORS * 16 - 1, (unsigned long) idt_table };
-struct desc_ptr nmi_idt_descr = { NR_VECTORS * 16 - 1,
-                                   (unsigned long) nmi_idt_table };
+struct desc_ptr debug_idt_descr = { NR_VECTORS * 16 - 1,
+                                   (unsigned long) debug_idt_table };
 
 DEFINE_PER_CPU_FIRST(union irq_stack_union,
                     irq_stack_union) __aligned(PAGE_SIZE);
                 addr > (__get_cpu_var(debug_stack_addr) - DEBUG_STKSZ));
 }
 
-static DEFINE_PER_CPU(u32, debug_stack_use_ctr);
+DEFINE_PER_CPU(u32, debug_idt_ctr);
 
 void debug_stack_set_zero(void)
 {
-       this_cpu_inc(debug_stack_use_ctr);
-       load_idt((const struct desc_ptr *)&nmi_idt_descr);
+       this_cpu_inc(debug_idt_ctr);
+       load_current_idt();
 }
 
 void debug_stack_reset(void)
 {
-       if (WARN_ON(!this_cpu_read(debug_stack_use_ctr)))
+       if (WARN_ON(!this_cpu_read(debug_idt_ctr)))
                return;
-       if (this_cpu_dec_return(debug_stack_use_ctr) == 0)
-               load_idt((const struct desc_ptr *)&idt_descr);
+       if (this_cpu_dec_return(debug_idt_ctr) == 0)
+               load_current_idt();
 }
 
 #else  /* CONFIG_X86_64 */
 
        x86_init.irqs.trap_init();
 
 #ifdef CONFIG_X86_64
-       memcpy(&nmi_idt_table, &idt_table, IDT_ENTRIES * 16);
+       memcpy(&debug_idt_table, &idt_table, IDT_ENTRIES * 16);
        set_nmi_gate(X86_TRAP_DB, &debug);
        set_nmi_gate(X86_TRAP_BP, &int3);
 #endif