tunnels: fix netns vs proto registration ordering
[safe/jmp/linux-2.6] / kernel / kgdb.c
index e3f6037..2eb517e 100644 (file)
 #include <asm/byteorder.h>
 #include <asm/atomic.h>
 #include <asm/system.h>
+#include <asm/unaligned.h>
 
 static int kgdb_break_asap;
 
+#define KGDB_MAX_THREAD_QUERY 17
 struct kgdb_state {
        int                     ex_vector;
        int                     signo;
        int                     err_code;
        int                     cpu;
        int                     pass_exception;
-       long                    threadid;
+       unsigned long           thr_query;
+       unsigned long           threadid;
        long                    kgdb_usethreadid;
        struct pt_regs          *linux_regs;
 };
@@ -126,6 +129,7 @@ struct task_struct          *kgdb_usethread;
 struct task_struct             *kgdb_contthread;
 
 int                            kgdb_single_step;
+pid_t                          kgdb_sstep_pid;
 
 /* Our I/O buffers. */
 static char                    remcom_in_buffer[BUFMAX];
@@ -146,7 +150,7 @@ atomic_t                    kgdb_cpu_doing_single_step = ATOMIC_INIT(-1);
  * the other CPUs might interfere with your debugging context, so
  * use this with care:
  */
-int                            kgdb_do_roundup = 1;
+static int kgdb_do_roundup = 1;
 
 static int __init opt_nokgdbroundup(char *str)
 {
@@ -165,13 +169,6 @@ early_param("nokgdbroundup", opt_nokgdbroundup);
  * Weak aliases for breakpoint management,
  * can be overriden by architectures when needed:
  */
-int __weak kgdb_validate_break_address(unsigned long addr)
-{
-       char tmp_variable[BREAK_INSTR_SIZE];
-
-       return probe_kernel_read(tmp_variable, (char *)addr, BREAK_INSTR_SIZE);
-}
-
 int __weak kgdb_arch_set_breakpoint(unsigned long addr, char *saved_instr)
 {
        int err;
@@ -190,6 +187,25 @@ int __weak kgdb_arch_remove_breakpoint(unsigned long addr, char *bundle)
                                  (char *)bundle, BREAK_INSTR_SIZE);
 }
 
+int __weak kgdb_validate_break_address(unsigned long addr)
+{
+       char tmp_variable[BREAK_INSTR_SIZE];
+       int err;
+       /* Validate setting the breakpoint and then removing it.  In the
+        * remove fails, the kernel needs to emit a bad message because we
+        * are deep trouble not being able to put things back the way we
+        * found them.
+        */
+       err = kgdb_arch_set_breakpoint(addr, tmp_variable);
+       if (err)
+               return err;
+       err = kgdb_arch_remove_breakpoint(addr, tmp_variable);
+       if (err)
+               printk(KERN_ERR "KGDB: Critical breakpoint error, kernel "
+                  "memory destroyed at: %lx", addr);
+       return err;
+}
+
 unsigned long __weak kgdb_arch_pc(int exception, struct pt_regs *regs)
 {
        return instruction_pointer(regs);
@@ -200,6 +216,17 @@ int __weak kgdb_arch_init(void)
        return 0;
 }
 
+int __weak kgdb_skipexception(int exception, struct pt_regs *regs)
+{
+       return 0;
+}
+
+void __weak
+kgdb_post_primary_code(struct pt_regs *regs, int e_vector, int err_code)
+{
+       return;
+}
+
 /**
  *     kgdb_disable_hw_debug - Disable hardware debugging while we in kgdb.
  *     @regs: Current &struct pt_regs.
@@ -216,8 +243,6 @@ void __weak kgdb_disable_hw_debug(struct pt_regs *regs)
  * GDB remote protocol parser:
  */
 
-static const char      hexchars[] = "0123456789abcdef";
-
 static int hex(char ch)
 {
        if ((ch >= 'a') && (ch <= 'f'))
@@ -305,8 +330,8 @@ static void put_packet(char *buffer)
                }
 
                kgdb_io_ops->write_char('#');
-               kgdb_io_ops->write_char(hexchars[checksum >> 4]);
-               kgdb_io_ops->write_char(hexchars[checksum & 0xf]);
+               kgdb_io_ops->write_char(hex_asc_hi(checksum));
+               kgdb_io_ops->write_char(hex_asc_lo(checksum));
                if (kgdb_io_ops->flush)
                        kgdb_io_ops->flush();
 
@@ -335,14 +360,6 @@ static void put_packet(char *buffer)
        }
 }
 
-static char *pack_hex_byte(char *pkt, u8 byte)
-{
-       *pkt++ = hexchars[byte >> 4];
-       *pkt++ = hexchars[byte & 0xf];
-
-       return pkt;
-}
-
 /*
  * Convert the memory pointed to by mem into hex, placing result in buf.
  * Return a pointer to the last char put in buf (null). May return an error.
@@ -427,13 +444,18 @@ int kgdb_hex2mem(char *buf, char *mem, int count)
  * While we find nice hex chars, build a long_val.
  * Return number of chars processed.
  */
-int kgdb_hex2long(char **ptr, long *long_val)
+int kgdb_hex2long(char **ptr, unsigned long *long_val)
 {
        int hex_val;
        int num = 0;
+       int negate = 0;
 
        *long_val = 0;
 
+       if (**ptr == '-') {
+               negate = 1;
+               (*ptr)++;
+       }
        while (**ptr) {
                hex_val = hex(**ptr);
                if (hex_val < 0)
@@ -444,6 +466,9 @@ int kgdb_hex2long(char **ptr, long *long_val)
                (*ptr)++;
        }
 
+       if (negate)
+               *long_val = -*long_val;
+
        return num;
 }
 
@@ -464,7 +489,7 @@ static int write_mem_msg(int binary)
                if (err)
                        return err;
                if (CACHE_FLUSH_IS_SAFE)
-                       flush_icache_range(addr, addr + length + 1);
+                       flush_icache_range(addr, addr + length);
                return 0;
        }
 
@@ -475,8 +500,8 @@ static void error_packet(char *pkt, int error)
 {
        error = -error;
        pkt[0] = 'E';
-       pkt[1] = hexchars[(error / 10)];
-       pkt[2] = hexchars[(error % 10)];
+       pkt[1] = hex_asc[(error / 10)];
+       pkt[2] = hex_asc[(error % 10)];
        pkt[3] = '\0';
 }
 
@@ -507,19 +532,27 @@ static void int_to_threadref(unsigned char *id, int value)
        scan = (unsigned char *)id;
        while (i--)
                *scan++ = 0;
-       *scan++ = (value >> 24) & 0xff;
-       *scan++ = (value >> 16) & 0xff;
-       *scan++ = (value >> 8) & 0xff;
-       *scan++ = (value & 0xff);
+       put_unaligned_be32(value, scan);
 }
 
 static struct task_struct *getthread(struct pt_regs *regs, int tid)
 {
        /*
-        * Non-positive TIDs are remapped idle tasks:
+        * Non-positive TIDs are remapped to the cpu shadow information
         */
-       if (tid <= 0)
-               return idle_task(-tid);
+       if (tid == 0 || tid == -1)
+               tid = -atomic_read(&kgdb_active) - 2;
+       if (tid < -1 && tid > -NR_CPUS - 2) {
+               if (kgdb_info[-tid - 2].task)
+                       return kgdb_info[-tid - 2].task;
+               else
+                       return idle_task(-tid - 2);
+       }
+       if (tid <= 0) {
+               printk(KERN_ERR "KGDB: Internal thread select error\n");
+               dump_stack();
+               return NULL;
+       }
 
        /*
         * find_task_by_pid_ns() does not take the tasklist lock anymore
@@ -550,18 +583,6 @@ static void kgdb_wait(struct pt_regs *regs)
        smp_wmb();
        atomic_set(&cpu_in_kgdb[cpu], 1);
 
-       /*
-        * The primary CPU must be active to enter here, but this is
-        * guard in case the primary CPU had not been selected if
-        * this was an entry via nmi.
-        */
-       while (atomic_read(&kgdb_active) == -1)
-               cpu_relax();
-
-       /* Wait till primary CPU goes completely into the debugger. */
-       while (!atomic_read(&cpu_in_kgdb[atomic_read(&kgdb_active)]))
-               cpu_relax();
-
        /* Wait till primary CPU is done with debugging */
        while (atomic_read(&passive_cpu_wait[cpu]))
                cpu_relax();
@@ -575,6 +596,7 @@ static void kgdb_wait(struct pt_regs *regs)
 
        /* Signal the primary CPU that we are done: */
        atomic_set(&cpu_in_kgdb[cpu], 0);
+       touch_softlockup_watchdog();
        clocksource_touch_watchdog();
        local_irq_restore(flags);
 }
@@ -589,12 +611,12 @@ static void kgdb_flush_swbreak_addr(unsigned long addr)
        if (!CACHE_FLUSH_IS_SAFE)
                return;
 
-       if (current->mm) {
+       if (current->mm && current->mm->mmap_cache) {
                flush_cache_range(current->mm->mmap_cache,
                                  addr, addr + BREAK_INSTR_SIZE);
-       } else {
-               flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
        }
+       /* Force flush instruction cache if it was outside the mm */
+       flush_icache_range(addr, addr + BREAK_INSTR_SIZE);
 }
 
 /*
@@ -603,7 +625,8 @@ static void kgdb_flush_swbreak_addr(unsigned long addr)
 static int kgdb_activate_sw_breakpoints(void)
 {
        unsigned long addr;
-       int error = 0;
+       int error;
+       int ret = 0;
        int i;
 
        for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
@@ -613,13 +636,16 @@ static int kgdb_activate_sw_breakpoints(void)
                addr = kgdb_break[i].bpt_addr;
                error = kgdb_arch_set_breakpoint(addr,
                                kgdb_break[i].saved_instr);
-               if (error)
-                       return error;
+               if (error) {
+                       ret = error;
+                       printk(KERN_INFO "KGDB: BP install failed: %lx", addr);
+                       continue;
+               }
 
                kgdb_flush_swbreak_addr(addr);
                kgdb_break[i].state = BP_ACTIVE;
        }
-       return 0;
+       return ret;
 }
 
 static int kgdb_set_sw_break(unsigned long addr)
@@ -666,7 +692,8 @@ static int kgdb_set_sw_break(unsigned long addr)
 static int kgdb_deactivate_sw_breakpoints(void)
 {
        unsigned long addr;
-       int error = 0;
+       int error;
+       int ret = 0;
        int i;
 
        for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
@@ -675,13 +702,15 @@ static int kgdb_deactivate_sw_breakpoints(void)
                addr = kgdb_break[i].bpt_addr;
                error = kgdb_arch_remove_breakpoint(addr,
                                        kgdb_break[i].saved_instr);
-               if (error)
-                       return error;
+               if (error) {
+                       printk(KERN_INFO "KGDB: BP remove failed: %lx\n", addr);
+                       ret = error;
+               }
 
                kgdb_flush_swbreak_addr(addr);
                kgdb_break[i].state = BP_SET;
        }
-       return 0;
+       return ret;
 }
 
 static int kgdb_remove_sw_break(unsigned long addr)
@@ -710,7 +739,7 @@ int kgdb_isremovedbreak(unsigned long addr)
        return 0;
 }
 
-int remove_all_break(void)
+static int remove_all_break(void)
 {
        unsigned long addr;
        int error;
@@ -718,14 +747,16 @@ int remove_all_break(void)
 
        /* Clear memory breakpoints. */
        for (i = 0; i < KGDB_MAX_BREAKPOINTS; i++) {
-               if (kgdb_break[i].state != BP_SET)
-                       continue;
+               if (kgdb_break[i].state != BP_ACTIVE)
+                       goto setundefined;
                addr = kgdb_break[i].bpt_addr;
                error = kgdb_arch_remove_breakpoint(addr,
                                kgdb_break[i].saved_instr);
                if (error)
-                       return error;
-               kgdb_break[i].state = BP_REMOVED;
+                       printk(KERN_ERR "KGDB: breakpoint remove failed: %lx\n",
+                          addr);
+setundefined:
+               kgdb_break[i].state = BP_UNDEFINED;
        }
 
        /* Clear hardware breakpoints. */
@@ -736,14 +767,15 @@ int remove_all_break(void)
 }
 
 /*
- * Remap normal tasks to their real PID, idle tasks to -1 ... -NR_CPUs:
+ * Remap normal tasks to their real PID,
+ * CPU shadow threads are mapped to -CPU - 2
  */
 static inline int shadow_pid(int realpid)
 {
        if (realpid)
                return realpid;
 
-       return -1-raw_smp_processor_id();
+       return -raw_smp_processor_id() - 2;
 }
 
 static char gdbmsgbuf[BUFMAX + 1];
@@ -837,7 +869,7 @@ static void gdb_cmd_getregs(struct kgdb_state *ks)
                local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
        } else {
                local_debuggerinfo = NULL;
-               for (i = 0; i < NR_CPUS; i++) {
+               for_each_online_cpu(i) {
                        /*
                         * Try to find the task on some other
                         * or possibly this node if we do not
@@ -851,7 +883,7 @@ static void gdb_cmd_getregs(struct kgdb_state *ks)
 
        /*
         * All threads that don't have debuggerinfo should be
-        * in __schedule() sleeping, since all other CPUs
+        * in schedule() sleeping, since all other CPUs
         * are in kgdb_wait, and thus have debuggerinfo.
         */
        if (local_debuggerinfo) {
@@ -971,10 +1003,13 @@ static int gdb_cmd_reboot(struct kgdb_state *ks)
 /* Handle the 'q' query packets */
 static void gdb_cmd_query(struct kgdb_state *ks)
 {
-       struct task_struct *thread;
+       struct task_struct *g;
+       struct task_struct *p;
        unsigned char thref[8];
        char *ptr;
        int i;
+       int cpu;
+       int finished = 0;
 
        switch (remcom_in_buffer[1]) {
        case 's':
@@ -984,22 +1019,34 @@ static void gdb_cmd_query(struct kgdb_state *ks)
                        break;
                }
 
-               if (remcom_in_buffer[1] == 'f')
-                       ks->threadid = 1;
-
+               i = 0;
                remcom_out_buffer[0] = 'm';
                ptr = remcom_out_buffer + 1;
-
-               for (i = 0; i < 17; ks->threadid++) {
-                       thread = getthread(ks->linux_regs, ks->threadid);
-                       if (thread) {
-                               int_to_threadref(thref, ks->threadid);
+               if (remcom_in_buffer[1] == 'f') {
+                       /* Each cpu is a shadow thread */
+                       for_each_online_cpu(cpu) {
+                               ks->thr_query = 0;
+                               int_to_threadref(thref, -cpu - 2);
                                pack_threadid(ptr, thref);
                                ptr += BUF_THREAD_ID_SIZE;
                                *(ptr++) = ',';
                                i++;
                        }
                }
+
+               do_each_thread(g, p) {
+                       if (i >= ks->thr_query && !finished) {
+                               int_to_threadref(thref, p->pid);
+                               pack_threadid(ptr, thref);
+                               ptr += BUF_THREAD_ID_SIZE;
+                               *(ptr++) = ',';
+                               ks->thr_query++;
+                               if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
+                                       finished = 1;
+                       }
+                       i++;
+               } while_each_thread(g, p);
+
                *(--ptr) = '\0';
                break;
 
@@ -1022,15 +1069,15 @@ static void gdb_cmd_query(struct kgdb_state *ks)
                        error_packet(remcom_out_buffer, -EINVAL);
                        break;
                }
-               if (ks->threadid > 0) {
+               if ((int)ks->threadid > 0) {
                        kgdb_mem2hex(getthread(ks->linux_regs,
                                        ks->threadid)->comm,
                                        remcom_out_buffer, 16);
                } else {
                        static char tmpstr[23 + BUF_THREAD_ID_SIZE];
 
-                       sprintf(tmpstr, "Shadow task %d for pid 0",
-                                       (int)(-ks->threadid-1));
+                       sprintf(tmpstr, "shadowCPU%d",
+                                       (int)(-ks->threadid - 2));
                        kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
                }
                break;
@@ -1139,10 +1186,10 @@ static void gdb_cmd_break(struct kgdb_state *ks)
                error = kgdb_remove_sw_break(addr);
        else if (remcom_in_buffer[0] == 'Z')
                error = arch_kgdb_ops.set_hw_breakpoint(addr,
-                       (int)length, *bpt_type);
+                       (int)length, *bpt_type - '0');
        else if (remcom_in_buffer[0] == 'z')
                error = arch_kgdb_ops.remove_hw_breakpoint(addr,
-                       (int) length, *bpt_type);
+                       (int) length, *bpt_type - '0');
 
        if (error == 0)
                strcpy(remcom_out_buffer, "OK");
@@ -1170,8 +1217,10 @@ static int gdb_cmd_exception_pass(struct kgdb_state *ks)
                return 1;
 
        } else {
-               error_packet(remcom_out_buffer, -EINVAL);
-               return 0;
+               kgdb_msg_write("KGDB only knows signal 9 (pass)"
+                       " and 15 (pass and disconnect)\n"
+                       "Executing a continue without signal passing\n", 0);
+               remcom_in_buffer[0] = 'c';
        }
 
        /* Indicate fall through */
@@ -1327,7 +1376,8 @@ static int kgdb_reenter_check(struct kgdb_state *ks)
                exception_level = 0;
                kgdb_skipexception(ks->ex_vector, ks->linux_regs);
                kgdb_activate_sw_breakpoints();
-               printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed\n");
+               printk(KERN_CRIT "KGDB: re-enter error: breakpoint removed %lx\n",
+                       addr);
                WARN_ON_ONCE(1);
 
                return 1;
@@ -1360,6 +1410,7 @@ kgdb_handle_exception(int evector, int signo, int ecode, struct pt_regs *regs)
        struct kgdb_state kgdb_var;
        struct kgdb_state *ks = &kgdb_var;
        unsigned long flags;
+       int sstep_tries = 100;
        int error = 0;
        int i, cpu;
 
@@ -1390,14 +1441,16 @@ acquirelock:
                cpu_relax();
 
        /*
-        * Do not start the debugger connection on this CPU if the last
-        * instance of the exception handler wanted to come into the
-        * debugger on a different CPU via a single step
+        * For single stepping, try to only enter on the processor
+        * that was single stepping.  To gaurd against a deadlock, the
+        * kernel will only try for the value of sstep_tries before
+        * giving up and continuing on.
         */
        if (atomic_read(&kgdb_cpu_doing_single_step) != -1 &&
-           atomic_read(&kgdb_cpu_doing_single_step) != cpu) {
-
+           (kgdb_info[cpu].task &&
+            kgdb_info[cpu].task->pid != kgdb_sstep_pid) && --sstep_tries) {
                atomic_set(&kgdb_active, -1);
+               touch_softlockup_watchdog();
                clocksource_touch_watchdog();
                local_irq_restore(flags);
 
@@ -1428,23 +1481,23 @@ acquirelock:
         * Get the passive CPU lock which will hold all the non-primary
         * CPU in a spin state while the debugger is active
         */
-       if (!kgdb_single_step || !kgdb_contthread) {
+       if (!kgdb_single_step) {
                for (i = 0; i < NR_CPUS; i++)
                        atomic_set(&passive_cpu_wait[i], 1);
        }
 
-#ifdef CONFIG_SMP
-       /* Signal the other CPUs to enter kgdb_wait() */
-       if ((!kgdb_single_step || !kgdb_contthread) && kgdb_do_roundup)
-               kgdb_roundup_cpus(flags);
-#endif
-
        /*
         * spin_lock code is good enough as a barrier so we don't
         * need one here:
         */
        atomic_set(&cpu_in_kgdb[ks->cpu], 1);
 
+#ifdef CONFIG_SMP
+       /* Signal the other CPUs to enter kgdb_wait() */
+       if ((!kgdb_single_step) && kgdb_do_roundup)
+               kgdb_roundup_cpus(flags);
+#endif
+
        /*
         * Wait for the other CPUs to be notified and be waiting for us:
         */
@@ -1460,7 +1513,7 @@ acquirelock:
        kgdb_post_primary_code(ks->linux_regs, ks->ex_vector, ks->err_code);
        kgdb_deactivate_sw_breakpoints();
        kgdb_single_step = 0;
-       kgdb_contthread = NULL;
+       kgdb_contthread = current;
        exception_level = 0;
 
        /* Talk to debugger with gdbserial protocol */
@@ -1474,7 +1527,7 @@ acquirelock:
        kgdb_info[ks->cpu].task = NULL;
        atomic_set(&cpu_in_kgdb[ks->cpu], 0);
 
-       if (!kgdb_single_step || !kgdb_contthread) {
+       if (!kgdb_single_step) {
                for (i = NR_CPUS-1; i >= 0; i--)
                        atomic_set(&passive_cpu_wait[i], 0);
                /*
@@ -1488,8 +1541,16 @@ acquirelock:
        }
 
 kgdb_restore:
+       if (atomic_read(&kgdb_cpu_doing_single_step) != -1) {
+               int sstep_cpu = atomic_read(&kgdb_cpu_doing_single_step);
+               if (kgdb_info[sstep_cpu].task)
+                       kgdb_sstep_pid = kgdb_info[sstep_cpu].task->pid;
+               else
+                       kgdb_sstep_pid = 0;
+       }
        /* Free kgdb_active */
        atomic_set(&kgdb_active, -1);
+       touch_softlockup_watchdog();
        clocksource_touch_watchdog();
        local_irq_restore(flags);
 
@@ -1500,7 +1561,8 @@ int kgdb_nmicallback(int cpu, void *regs)
 {
 #ifdef CONFIG_SMP
        if (!atomic_read(&cpu_in_kgdb[cpu]) &&
-                       atomic_read(&kgdb_active) != cpu) {
+                       atomic_read(&kgdb_active) != cpu &&
+                       atomic_read(&cpu_in_kgdb[atomic_read(&kgdb_active)])) {
                kgdb_wait((struct pt_regs *)regs);
                return 0;
        }
@@ -1508,7 +1570,8 @@ int kgdb_nmicallback(int cpu, void *regs)
        return 1;
 }
 
-void kgdb_console_write(struct console *co, const char *s, unsigned count)
+static void kgdb_console_write(struct console *co, const char *s,
+   unsigned count)
 {
        unsigned long flags;
 
@@ -1544,8 +1607,8 @@ static void sysrq_handle_gdb(int key, struct tty_struct *tty)
 
 static struct sysrq_key_op sysrq_gdb_op = {
        .handler        = sysrq_handle_gdb,
-       .help_msg       = "Gdb",
-       .action_msg     = "GDB",
+       .help_msg       = "debug(G)",
+       .action_msg     = "DEBUG",
 };
 #endif
 
@@ -1593,7 +1656,7 @@ static void kgdb_initial_breakpoint(void)
 }
 
 /**
- *     kkgdb_register_io_module - register KGDB IO module
+ *     kgdb_register_io_module - register KGDB IO module
  *     @new_kgdb_io_ops: the io ops vector
  *
  *     Register it with the KGDB core.