]> Pileus Git - ~andy/linux/blobdiff - arch/avr32/kernel/process.c
[AVR32] Enable debugging only when needed
[~andy/linux] / arch / avr32 / kernel / process.c
index 0b4325946a41629cc7a4755a7c679311e85e688f..eaaa69bbdc38d5546b395311f29896a6a2c357db 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/fs.h>
 #include <linux/ptrace.h>
 #include <linux/reboot.h>
+#include <linux/uaccess.h>
 #include <linux/unistd.h>
 
 #include <asm/sysreg.h>
@@ -19,6 +20,8 @@
 void (*pm_power_off)(void) = NULL;
 EXPORT_SYMBOL(pm_power_off);
 
+extern void cpu_idle_sleep(void);
+
 /*
  * This file handles the architecture-dependent parts of process handling..
  */
@@ -27,9 +30,8 @@ void cpu_idle(void)
 {
        /* endless idle loop with no priority at all */
        while (1) {
-               /* TODO: Enter sleep mode */
                while (!need_resched())
-                       cpu_relax();
+                       cpu_idle_sleep();
                preempt_enable_no_resched();
                schedule();
                preempt_disable();
@@ -53,8 +55,8 @@ void machine_power_off(void)
 
 void machine_restart(char *cmd)
 {
-       __mtdr(DBGREG_DC, DC_DBE);
-       __mtdr(DBGREG_DC, DC_RES);
+       ocd_write(DC, (1 << OCD_DC_DBE_BIT));
+       ocd_write(DC, (1 << OCD_DC_RES_BIT));
        while (1) ;
 }
 
@@ -101,7 +103,7 @@ EXPORT_SYMBOL(kernel_thread);
  */
 void exit_thread(void)
 {
-       /* nothing to do */
+       ocd_disable(current);
 }
 
 void flush_thread(void)
@@ -114,51 +116,203 @@ void release_thread(struct task_struct *dead_task)
        /* do nothing */
 }
 
+static void dump_mem(const char *str, const char *log_lvl,
+                    unsigned long bottom, unsigned long top)
+{
+       unsigned long p;
+       int i;
+
+       printk("%s%s(0x%08lx to 0x%08lx)\n", log_lvl, str, bottom, top);
+
+       for (p = bottom & ~31; p < top; ) {
+               printk("%s%04lx: ", log_lvl, p & 0xffff);
+
+               for (i = 0; i < 8; i++, p += 4) {
+                       unsigned int val;
+
+                       if (p < bottom || p >= top)
+                               printk("         ");
+                       else {
+                               if (__get_user(val, (unsigned int __user *)p)) {
+                                       printk("\n");
+                                       goto out;
+                               }
+                               printk("%08x ", val);
+                       }
+               }
+               printk("\n");
+       }
+
+out:
+       return;
+}
+
+static inline int valid_stack_ptr(struct thread_info *tinfo, unsigned long p)
+{
+       return (p > (unsigned long)tinfo)
+               && (p < (unsigned long)tinfo + THREAD_SIZE - 3);
+}
+
+#ifdef CONFIG_FRAME_POINTER
+static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
+                              struct pt_regs *regs, const char *log_lvl)
+{
+       unsigned long lr, fp;
+       struct thread_info *tinfo;
+
+       if (regs)
+               fp = regs->r7;
+       else if (tsk == current)
+               asm("mov %0, r7" : "=r"(fp));
+       else
+               fp = tsk->thread.cpu_context.r7;
+
+       /*
+        * Walk the stack as long as the frame pointer (a) is within
+        * the kernel stack of the task, and (b) it doesn't move
+        * downwards.
+        */
+       tinfo = task_thread_info(tsk);
+       printk("%sCall trace:\n", log_lvl);
+       while (valid_stack_ptr(tinfo, fp)) {
+               unsigned long new_fp;
+
+               lr = *(unsigned long *)fp;
+#ifdef CONFIG_KALLSYMS
+               printk("%s [<%08lx>] ", log_lvl, lr);
+#else
+               printk(" [<%08lx>] ", lr);
+#endif
+               print_symbol("%s\n", lr);
+
+               new_fp = *(unsigned long *)(fp + 4);
+               if (new_fp <= fp)
+                       break;
+               fp = new_fp;
+       }
+       printk("\n");
+}
+#else
+static void show_trace_log_lvl(struct task_struct *tsk, unsigned long *sp,
+                              struct pt_regs *regs, const char *log_lvl)
+{
+       unsigned long addr;
+
+       printk("%sCall trace:\n", log_lvl);
+
+       while (!kstack_end(sp)) {
+               addr = *sp++;
+               if (kernel_text_address(addr)) {
+#ifdef CONFIG_KALLSYMS
+                       printk("%s [<%08lx>] ", log_lvl, addr);
+#else
+                       printk(" [<%08lx>] ", addr);
+#endif
+                       print_symbol("%s\n", addr);
+               }
+       }
+       printk("\n");
+}
+#endif
+
+void show_stack_log_lvl(struct task_struct *tsk, unsigned long sp,
+                       struct pt_regs *regs, const char *log_lvl)
+{
+       struct thread_info *tinfo;
+
+       if (sp == 0) {
+               if (tsk)
+                       sp = tsk->thread.cpu_context.ksp;
+               else
+                       sp = (unsigned long)&tinfo;
+       }
+       if (!tsk)
+               tsk = current;
+
+       tinfo = task_thread_info(tsk);
+
+       if (valid_stack_ptr(tinfo, sp)) {
+               dump_mem("Stack: ", log_lvl, sp,
+                        THREAD_SIZE + (unsigned long)tinfo);
+               show_trace_log_lvl(tsk, (unsigned long *)sp, regs, log_lvl);
+       }
+}
+
+void show_stack(struct task_struct *tsk, unsigned long *stack)
+{
+       show_stack_log_lvl(tsk, (unsigned long)stack, NULL, "");
+}
+
+void dump_stack(void)
+{
+       unsigned long stack;
+
+       show_trace_log_lvl(current, &stack, NULL, "");
+}
+EXPORT_SYMBOL(dump_stack);
+
 static const char *cpu_modes[] = {
        "Application", "Supervisor", "Interrupt level 0", "Interrupt level 1",
        "Interrupt level 2", "Interrupt level 3", "Exception", "NMI"
 };
 
-void show_regs(struct pt_regs *regs)
+void show_regs_log_lvl(struct pt_regs *regs, const char *log_lvl)
 {
        unsigned long sp = regs->sp;
        unsigned long lr = regs->lr;
        unsigned long mode = (regs->sr & MODE_MASK) >> MODE_SHIFT;
 
-       if (!user_mode(regs))
+       if (!user_mode(regs)) {
                sp = (unsigned long)regs + FRAME_SIZE_FULL;
 
-       print_symbol("PC is at %s\n", instruction_pointer(regs));
-       print_symbol("LR is at %s\n", lr);
-       printk("pc : [<%08lx>]    lr : [<%08lx>]    %s\n"
-              "sp : %08lx  r12: %08lx  r11: %08lx\n",
-              instruction_pointer(regs),
-              lr, print_tainted(), sp, regs->r12, regs->r11);
-       printk("r10: %08lx  r9 : %08lx  r8 : %08lx\n",
-              regs->r10, regs->r9, regs->r8);
-       printk("r7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
-              regs->r7, regs->r6, regs->r5, regs->r4);
-       printk("r3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
-              regs->r3, regs->r2, regs->r1, regs->r0);
-       printk("Flags: %c%c%c%c%c\n",
+               printk("%s", log_lvl);
+               print_symbol("PC is at %s\n", instruction_pointer(regs));
+               printk("%s", log_lvl);
+               print_symbol("LR is at %s\n", lr);
+       }
+
+       printk("%spc : [<%08lx>]    lr : [<%08lx>]    %s\n"
+              "%ssp : %08lx  r12: %08lx  r11: %08lx\n",
+              log_lvl, instruction_pointer(regs), lr, print_tainted(),
+              log_lvl, sp, regs->r12, regs->r11);
+       printk("%sr10: %08lx  r9 : %08lx  r8 : %08lx\n",
+              log_lvl, regs->r10, regs->r9, regs->r8);
+       printk("%sr7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
+              log_lvl, regs->r7, regs->r6, regs->r5, regs->r4);
+       printk("%sr3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
+              log_lvl, regs->r3, regs->r2, regs->r1, regs->r0);
+       printk("%sFlags: %c%c%c%c%c\n", log_lvl,
               regs->sr & SR_Q ? 'Q' : 'q',
               regs->sr & SR_V ? 'V' : 'v',
               regs->sr & SR_N ? 'N' : 'n',
               regs->sr & SR_Z ? 'Z' : 'z',
               regs->sr & SR_C ? 'C' : 'c');
-       printk("Mode bits: %c%c%c%c%c%c%c%c%c\n",
+       printk("%sMode bits: %c%c%c%c%c%c%c%c%c%c\n", log_lvl,
               regs->sr & SR_H ? 'H' : 'h',
-              regs->sr & SR_R ? 'R' : 'r',
               regs->sr & SR_J ? 'J' : 'j',
+              regs->sr & SR_DM ? 'M' : 'm',
+              regs->sr & SR_D ? 'D' : 'd',
               regs->sr & SR_EM ? 'E' : 'e',
               regs->sr & SR_I3M ? '3' : '.',
               regs->sr & SR_I2M ? '2' : '.',
               regs->sr & SR_I1M ? '1' : '.',
               regs->sr & SR_I0M ? '0' : '.',
               regs->sr & SR_GM ? 'G' : 'g');
-       printk("CPU Mode: %s\n", cpu_modes[mode]);
+       printk("%sCPU Mode: %s\n", log_lvl, cpu_modes[mode]);
+       printk("%sProcess: %s [%d] (task: %p thread: %p)\n",
+              log_lvl, current->comm, current->pid, current,
+              task_thread_info(current));
+}
 
-       show_trace(NULL, (unsigned long *)sp, regs);
+void show_regs(struct pt_regs *regs)
+{
+       unsigned long sp = regs->sp;
+
+       if (!user_mode(regs))
+               sp = (unsigned long)regs + FRAME_SIZE_FULL;
+
+       show_regs_log_lvl(regs, "");
+       show_trace_log_lvl(current, (unsigned long *)sp, regs, "");
 }
 EXPORT_SYMBOL(show_regs);
 
@@ -177,13 +331,13 @@ int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
 {
        struct pt_regs *childregs;
 
-       childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long)p->thread_info)) - 1;
+       childregs = ((struct pt_regs *)(THREAD_SIZE + (unsigned long)task_stack_page(p))) - 1;
        *childregs = *regs;
 
        if (user_mode(regs))
                childregs->sp = usp;
        else
-               childregs->sp = (unsigned long)p->thread_info + THREAD_SIZE;
+               childregs->sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
 
        childregs->r12 = 0; /* Set return value for child */
 
@@ -191,6 +345,9 @@ int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
        p->thread.cpu_context.ksp = (unsigned long)childregs;
        p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
 
+       if ((clone_flags & CLONE_PTRACE) && test_thread_flag(TIF_DEBUG))
+               ocd_enable(p);
+
        return 0;
 }
 
@@ -250,7 +407,7 @@ unsigned long get_wchan(struct task_struct *p)
        if (!p || p == current || p->state == TASK_RUNNING)
                return 0;
 
-       stack_page = (unsigned long)p->thread_info;
+       stack_page = (unsigned long)task_stack_page(p);
        BUG_ON(!stack_page);
 
        /*