kgdb: core changes to support kdb
[safe/jmp/linux-2.6] / arch / x86 / kernel / kgdb.c
index 8c7e555..f95a2c0 100644 (file)
 #include <linux/init.h>
 #include <linux/smp.h>
 #include <linux/nmi.h>
+#include <linux/hw_breakpoint.h>
 
+#include <asm/debugreg.h>
 #include <asm/apicdef.h>
 #include <asm/system.h>
 
-#ifdef CONFIG_X86_32
-# include <mach_ipi.h>
-#else
-# include <asm/mach_apic.h>
-#endif
+#include <asm/apic.h>
 
 /*
  * Put the error code here just in case the user cares:
@@ -73,6 +71,9 @@ static int gdb_x86vector = -1;
  */
 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
 {
+#ifndef CONFIG_X86_32
+       u32 *gdb_regs32 = (u32 *)gdb_regs;
+#endif
        gdb_regs[GDB_AX]        = regs->ax;
        gdb_regs[GDB_BX]        = regs->bx;
        gdb_regs[GDB_CX]        = regs->cx;
@@ -80,15 +81,21 @@ void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
        gdb_regs[GDB_SI]        = regs->si;
        gdb_regs[GDB_DI]        = regs->di;
        gdb_regs[GDB_BP]        = regs->bp;
-       gdb_regs[GDB_PS]        = regs->flags;
        gdb_regs[GDB_PC]        = regs->ip;
 #ifdef CONFIG_X86_32
+       gdb_regs[GDB_PS]        = regs->flags;
        gdb_regs[GDB_DS]        = regs->ds;
        gdb_regs[GDB_ES]        = regs->es;
        gdb_regs[GDB_CS]        = regs->cs;
-       gdb_regs[GDB_SS]        = __KERNEL_DS;
        gdb_regs[GDB_FS]        = 0xFFFF;
        gdb_regs[GDB_GS]        = 0xFFFF;
+       if (user_mode_vm(regs)) {
+               gdb_regs[GDB_SS] = regs->ss;
+               gdb_regs[GDB_SP] = regs->sp;
+       } else {
+               gdb_regs[GDB_SS] = __KERNEL_DS;
+               gdb_regs[GDB_SP] = kernel_stack_pointer(regs);
+       }
 #else
        gdb_regs[GDB_R8]        = regs->r8;
        gdb_regs[GDB_R9]        = regs->r9;
@@ -98,8 +105,11 @@ void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
        gdb_regs[GDB_R13]       = regs->r13;
        gdb_regs[GDB_R14]       = regs->r14;
        gdb_regs[GDB_R15]       = regs->r15;
+       gdb_regs32[GDB_PS]      = regs->flags;
+       gdb_regs32[GDB_CS]      = regs->cs;
+       gdb_regs32[GDB_SS]      = regs->ss;
+       gdb_regs[GDB_SP]        = kernel_stack_pointer(regs);
 #endif
-       gdb_regs[GDB_SP]        = regs->sp;
 }
 
 /**
@@ -116,6 +126,9 @@ void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
  */
 void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
 {
+#ifndef CONFIG_X86_32
+       u32 *gdb_regs32 = (u32 *)gdb_regs;
+#endif
        gdb_regs[GDB_AX]        = 0;
        gdb_regs[GDB_BX]        = 0;
        gdb_regs[GDB_CX]        = 0;
@@ -133,7 +146,9 @@ void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
        gdb_regs[GDB_FS]        = 0xFFFF;
        gdb_regs[GDB_GS]        = 0xFFFF;
 #else
-       gdb_regs[GDB_PS]        = *(unsigned long *)(p->thread.sp + 8);
+       gdb_regs32[GDB_PS]      = *(unsigned long *)(p->thread.sp + 8);
+       gdb_regs32[GDB_CS]      = __KERNEL_CS;
+       gdb_regs32[GDB_SS]      = __KERNEL_DS;
        gdb_regs[GDB_PC]        = 0;
        gdb_regs[GDB_R8]        = 0;
        gdb_regs[GDB_R9]        = 0;
@@ -157,6 +172,9 @@ void sleeping_thread_to_gdb_regs(unsigned long *gdb_regs, struct task_struct *p)
  */
 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
 {
+#ifndef CONFIG_X86_32
+       u32 *gdb_regs32 = (u32 *)gdb_regs;
+#endif
        regs->ax                = gdb_regs[GDB_AX];
        regs->bx                = gdb_regs[GDB_BX];
        regs->cx                = gdb_regs[GDB_CX];
@@ -164,9 +182,9 @@ void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
        regs->si                = gdb_regs[GDB_SI];
        regs->di                = gdb_regs[GDB_DI];
        regs->bp                = gdb_regs[GDB_BP];
-       regs->flags             = gdb_regs[GDB_PS];
        regs->ip                = gdb_regs[GDB_PC];
 #ifdef CONFIG_X86_32
+       regs->flags             = gdb_regs[GDB_PS];
        regs->ds                = gdb_regs[GDB_DS];
        regs->es                = gdb_regs[GDB_ES];
        regs->cs                = gdb_regs[GDB_CS];
@@ -179,46 +197,89 @@ void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
        regs->r13               = gdb_regs[GDB_R13];
        regs->r14               = gdb_regs[GDB_R14];
        regs->r15               = gdb_regs[GDB_R15];
+       regs->flags             = gdb_regs32[GDB_PS];
+       regs->cs                = gdb_regs32[GDB_CS];
+       regs->ss                = gdb_regs32[GDB_SS];
 #endif
 }
 
 static struct hw_breakpoint {
        unsigned                enabled;
-       unsigned                type;
-       unsigned                len;
        unsigned long           addr;
+       int                     len;
+       int                     type;
+       struct perf_event       **pev;
 } breakinfo[4];
 
 static void kgdb_correct_hw_break(void)
 {
-       unsigned long dr7;
-       int correctit = 0;
-       int breakbit;
        int breakno;
 
-       get_debugreg(dr7, 7);
        for (breakno = 0; breakno < 4; breakno++) {
-               breakbit = 2 << (breakno << 1);
-               if (!(dr7 & breakbit) && breakinfo[breakno].enabled) {
-                       correctit = 1;
-                       dr7 |= breakbit;
-                       dr7 &= ~(0xf0000 << (breakno << 2));
-                       dr7 |= ((breakinfo[breakno].len << 2) |
-                                breakinfo[breakno].type) <<
-                              ((breakno << 2) + 16);
-                       if (breakno >= 0 && breakno <= 3)
-                               set_debugreg(breakinfo[breakno].addr, breakno);
-
-               } else {
-                       if ((dr7 & breakbit) && !breakinfo[breakno].enabled) {
-                               correctit = 1;
-                               dr7 &= ~breakbit;
-                               dr7 &= ~(0xf0000 << (breakno << 2));
-                       }
-               }
+               struct perf_event *bp;
+               struct arch_hw_breakpoint *info;
+               int val;
+               int cpu = raw_smp_processor_id();
+               if (!breakinfo[breakno].enabled)
+                       continue;
+               bp = *per_cpu_ptr(breakinfo[breakno].pev, cpu);
+               info = counter_arch_bp(bp);
+               if (bp->attr.disabled != 1)
+                       continue;
+               bp->attr.bp_addr = breakinfo[breakno].addr;
+               bp->attr.bp_len = breakinfo[breakno].len;
+               bp->attr.bp_type = breakinfo[breakno].type;
+               info->address = breakinfo[breakno].addr;
+               info->len = breakinfo[breakno].len;
+               info->type = breakinfo[breakno].type;
+               val = arch_install_hw_breakpoint(bp);
+               if (!val)
+                       bp->attr.disabled = 0;
+       }
+       hw_breakpoint_restore();
+}
+
+static int hw_break_reserve_slot(int breakno)
+{
+       int cpu;
+       int cnt = 0;
+       struct perf_event **pevent;
+
+       for_each_online_cpu(cpu) {
+               cnt++;
+               pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
+               if (dbg_reserve_bp_slot(*pevent))
+                       goto fail;
+       }
+
+       return 0;
+
+fail:
+       for_each_online_cpu(cpu) {
+               cnt--;
+               if (!cnt)
+                       break;
+               pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
+               dbg_release_bp_slot(*pevent);
+       }
+       return -1;
+}
+
+static int hw_break_release_slot(int breakno)
+{
+       struct perf_event **pevent;
+       int cpu;
+
+       for_each_online_cpu(cpu) {
+               pevent = per_cpu_ptr(breakinfo[breakno].pev, cpu);
+               if (dbg_release_bp_slot(*pevent))
+                       /*
+                        * The debugger is responisble for handing the retry on
+                        * remove failure.
+                        */
+                       return -1;
        }
-       if (correctit)
-               set_debugreg(dr7, 7);
+       return 0;
 }
 
 static int
@@ -232,6 +293,10 @@ kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
        if (i == 4)
                return -1;
 
+       if (hw_break_release_slot(i)) {
+               printk(KERN_ERR "Cannot remove hw breakpoint at %lx\n", addr);
+               return -1;
+       }
        breakinfo[i].enabled = 0;
 
        return 0;
@@ -240,15 +305,23 @@ kgdb_remove_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
 static void kgdb_remove_all_hw_break(void)
 {
        int i;
+       int cpu = raw_smp_processor_id();
+       struct perf_event *bp;
 
-       for (i = 0; i < 4; i++)
-               memset(&breakinfo[i], 0, sizeof(struct hw_breakpoint));
+       for (i = 0; i < 4; i++) {
+               if (!breakinfo[i].enabled)
+                       continue;
+               bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
+               if (bp->attr.disabled == 1)
+                       continue;
+               arch_uninstall_hw_breakpoint(bp);
+               bp->attr.disabled = 1;
+       }
 }
 
 static int
 kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
 {
-       unsigned type;
        int i;
 
        for (i = 0; i < 4; i++)
@@ -259,27 +332,42 @@ kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
 
        switch (bptype) {
        case BP_HARDWARE_BREAKPOINT:
-               type = 0;
-               len  = 1;
+               len = 1;
+               breakinfo[i].type = X86_BREAKPOINT_EXECUTE;
                break;
        case BP_WRITE_WATCHPOINT:
-               type = 1;
+               breakinfo[i].type = X86_BREAKPOINT_WRITE;
                break;
        case BP_ACCESS_WATCHPOINT:
-               type = 3;
+               breakinfo[i].type = X86_BREAKPOINT_RW;
                break;
        default:
                return -1;
        }
-
-       if (len == 1 || len == 2 || len == 4)
-               breakinfo[i].len  = len - 1;
-       else
+       switch (len) {
+       case 1:
+               breakinfo[i].len = X86_BREAKPOINT_LEN_1;
+               break;
+       case 2:
+               breakinfo[i].len = X86_BREAKPOINT_LEN_2;
+               break;
+       case 4:
+               breakinfo[i].len = X86_BREAKPOINT_LEN_4;
+               break;
+#ifdef CONFIG_X86_64
+       case 8:
+               breakinfo[i].len = X86_BREAKPOINT_LEN_8;
+               break;
+#endif
+       default:
                return -1;
-
-       breakinfo[i].enabled = 1;
+       }
        breakinfo[i].addr = addr;
-       breakinfo[i].type = type;
+       if (hw_break_reserve_slot(i)) {
+               breakinfo[i].addr = 0;
+               return -1;
+       }
+       breakinfo[i].enabled = 1;
 
        return 0;
 }
@@ -294,8 +382,21 @@ kgdb_set_hw_break(unsigned long addr, int len, enum kgdb_bptype bptype)
  */
 void kgdb_disable_hw_debug(struct pt_regs *regs)
 {
+       int i;
+       int cpu = raw_smp_processor_id();
+       struct perf_event *bp;
+
        /* Disable hardware debugging while we are in kgdb: */
        set_debugreg(0UL, 7);
+       for (i = 0; i < 4; i++) {
+               if (!breakinfo[i].enabled)
+                       continue;
+               bp = *per_cpu_ptr(breakinfo[i].pev, cpu);
+               if (bp->attr.disabled == 1)
+                       continue;
+               arch_uninstall_hw_breakpoint(bp);
+               bp->attr.disabled = 1;
+       }
 }
 
 /**
@@ -334,7 +435,7 @@ void kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
  */
 void kgdb_roundup_cpus(unsigned long flags)
 {
-       send_IPI_allbutself(APIC_DM_NMI);
+       apic->send_IPI_allbutself(APIC_DM_NMI);
 }
 #endif
 
@@ -359,7 +460,6 @@ int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
                               struct pt_regs *linux_regs)
 {
        unsigned long addr;
-       unsigned long dr6;
        char *ptr;
        int newPC;
 
@@ -375,33 +475,16 @@ int kgdb_arch_handle_exception(int e_vector, int signo, int err_code,
                newPC = linux_regs->ip;
 
                /* clear the trace bit */
-               linux_regs->flags &= ~TF_MASK;
+               linux_regs->flags &= ~X86_EFLAGS_TF;
                atomic_set(&kgdb_cpu_doing_single_step, -1);
 
                /* set the trace bit if we're stepping */
                if (remcomInBuffer[0] == 's') {
-                       linux_regs->flags |= TF_MASK;
-                       kgdb_single_step = 1;
-                       if (kgdb_contthread) {
-                               atomic_set(&kgdb_cpu_doing_single_step,
-                                          raw_smp_processor_id());
-                       }
+                       linux_regs->flags |= X86_EFLAGS_TF;
+                       atomic_set(&kgdb_cpu_doing_single_step,
+                                  raw_smp_processor_id());
                }
 
-               get_debugreg(dr6, 6);
-               if (!(dr6 & 0x4000)) {
-                       int breakno;
-
-                       for (breakno = 0; breakno < 4; breakno++) {
-                               if (dr6 & (1 << breakno) &&
-                                   breakinfo[breakno].type == 0) {
-                                       /* Set restore flag: */
-                                       linux_regs->flags |= X86_EFLAGS_RF;
-                                       break;
-                               }
-                       }
-               }
-               set_debugreg(0UL, 6);
                kgdb_correct_hw_break();
 
                return 0;
@@ -422,6 +505,11 @@ single_step_cont(struct pt_regs *regs, struct die_args *args)
                        "resuming...\n");
        kgdb_arch_handle_exception(args->trapnr, args->signr,
                                   args->err, "c", "", regs);
+       /*
+        * Reset the BS bit in dr6 (pointed by args->err) to
+        * denote completion of processing
+        */
+       (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
 
        return NOTIFY_STOP;
 }
@@ -444,12 +532,7 @@ static int __kgdb_notify(struct die_args *args, unsigned long cmd)
                return NOTIFY_DONE;
 
        case DIE_NMI_IPI:
-               if (atomic_read(&kgdb_active) != -1) {
-                       /* KGDB CPU roundup */
-                       kgdb_nmicallback(raw_smp_processor_id(), regs);
-                       was_in_debug_nmi[raw_smp_processor_id()] = 1;
-                       touch_nmi_watchdog();
-               }
+               /* Just ignore, we will handle the roundup on DIE_NMI. */
                return NOTIFY_DONE;
 
        case DIE_NMIUNKNOWN:
@@ -469,10 +552,15 @@ static int __kgdb_notify(struct die_args *args, unsigned long cmd)
                break;
 
        case DIE_DEBUG:
-               if (atomic_read(&kgdb_cpu_doing_single_step) ==
-                       raw_smp_processor_id() &&
-                       user_mode(regs))
-                       return single_step_cont(regs, args);
+               if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
+                       if (user_mode(regs))
+                               return single_step_cont(regs, args);
+                       break;
+               } else if (test_thread_flag(TIF_SINGLESTEP))
+                       /* This means a user thread is single stepping
+                        * a system call which should be ignored
+                        */
+                       return NOTIFY_DONE;
                /* fall through */
        default:
                if (user_mode(regs))
@@ -517,7 +605,42 @@ static struct notifier_block kgdb_notifier = {
  */
 int kgdb_arch_init(void)
 {
-       return register_die_notifier(&kgdb_notifier);
+       int i, cpu;
+       int ret;
+       struct perf_event_attr attr;
+       struct perf_event **pevent;
+
+       ret = register_die_notifier(&kgdb_notifier);
+       if (ret != 0)
+               return ret;
+       /*
+        * Pre-allocate the hw breakpoint structions in the non-atomic
+        * portion of kgdb because this operation requires mutexs to
+        * complete.
+        */
+       hw_breakpoint_init(&attr);
+       attr.bp_addr = (unsigned long)kgdb_arch_init;
+       attr.bp_len = HW_BREAKPOINT_LEN_1;
+       attr.bp_type = HW_BREAKPOINT_W;
+       attr.disabled = 1;
+       for (i = 0; i < 4; i++) {
+               breakinfo[i].pev = register_wide_hw_breakpoint(&attr, NULL);
+               if (IS_ERR(breakinfo[i].pev)) {
+                       printk(KERN_ERR "kgdb: Could not allocate hw breakpoints\n");
+                       breakinfo[i].pev = NULL;
+                       kgdb_arch_exit();
+                       return -1;
+               }
+               for_each_online_cpu(cpu) {
+                       pevent = per_cpu_ptr(breakinfo[i].pev, cpu);
+                       pevent[0]->hw.sample_period = 1;
+                       if (pevent[0]->destroy != NULL) {
+                               pevent[0]->destroy = NULL;
+                               release_bp_slot(*pevent);
+                       }
+               }
+       }
+       return ret;
 }
 
 /**
@@ -528,6 +651,13 @@ int kgdb_arch_init(void)
  */
 void kgdb_arch_exit(void)
 {
+       int i;
+       for (i = 0; i < 4; i++) {
+               if (breakinfo[i].pev) {
+                       unregister_wide_hw_breakpoint(breakinfo[i].pev);
+                       breakinfo[i].pev = NULL;
+               }
+       }
        unregister_die_notifier(&kgdb_notifier);
 }
 
@@ -560,6 +690,11 @@ unsigned long kgdb_arch_pc(int exception, struct pt_regs *regs)
        return instruction_pointer(regs);
 }
 
+void kgdb_arch_set_pc(struct pt_regs *regs, unsigned long ip)
+{
+       regs->ip = ip;
+}
+
 struct kgdb_arch arch_kgdb_ops = {
        /* Breakpoint instruction: */
        .gdb_bpt_instr          = { 0xcc },