1 /*
2  *  linux/arch/cris/traps.c
3  *
4  *  Here we handle the break vectors not used by the system call
5  *  mechanism, as well as some general stack/register dumping
6  *  things.
7  *
8  *  Copyright (C) 2000, 2001, 2002, 2003 Axis Communications AB
9  *
10  *  Authors:   Bjorn Wesen
11  *  	       Hans-Peter Nilsson
12  *
13  */
14 
15 #include <linux/init.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/ptrace.h>
21 #include <linux/timer.h>
22 #include <linux/mm.h>
23 #include <asm/uaccess.h>
24 
25 #include <asm/system.h>
26 #include <asm/segment.h>
27 #include <asm/io.h>
28 #include <asm/pgtable.h>
29 
30 int kstack_depth_to_print = 24;
31 
show_trace(unsigned long * stack)32 void show_trace(unsigned long * stack)
33 {
34 	unsigned long addr, module_start, module_end;
35 	extern char _stext, _etext;
36 	int i;
37 
38         printk("\nCall Trace: ");
39 
40         i = 1;
41         module_start = VMALLOC_START;
42         module_end = VMALLOC_END;
43 
44         while (((long) stack & (THREAD_SIZE-1)) != 0) {
45 		if (__get_user (addr, stack)) {
46 			/* This message matches "failing address" marked
47 			   s390 in ksymoops, so lines containing it will
48 			   not be filtered out by ksymoops.  */
49 			printk ("Failing address 0x%lx\n", (unsigned long)stack);
50 			break;
51 		}
52 		stack++;
53 
54                 /*
55                  * If the address is either in the text segment of the
56                  * kernel, or in the region which contains vmalloc'ed
57                  * memory, it *may* be the address of a calling
58                  * routine; if so, print it so that someone tracing
59                  * down the cause of the crash will be able to figure
60                  * out the call path that was taken.
61                  */
62                 if (((addr >= (unsigned long) &_stext) &&
63                      (addr <= (unsigned long) &_etext)) ||
64                     ((addr >= module_start) && (addr <= module_end))) {
65                         if (i && ((i % 8) == 0))
66                                 printk("\n       ");
67                         printk("[<%08lx>] ", addr);
68                         i++;
69                 }
70         }
71 }
72 
show_trace_task(struct task_struct * tsk)73 void show_trace_task(struct task_struct *tsk)
74 {
75 	/* TODO, this is not really useful since its called from
76 	 * SysRq-T and we don't have a keyboard.. :)
77 	 */
78 }
79 
80 
81 /*
82  * These constants are for searching for possible module text
83  * segments. MODULE_RANGE is a guess of how much space is likely
84  * to be vmalloced.
85  */
86 
87 #define MODULE_RANGE (8*1024*1024)
88 
89 /*
90  * The output (format, strings and order) is adjusted to be usable with
91  * ksymoops-2.4.1 with some necessary CRIS-specific patches.  Please don't
92  * change it unless you're serious about adjusting ksymoops and syncing
93  * with the ksymoops maintainer.
94  */
95 
96 void
show_stack(unsigned long * sp)97 show_stack(unsigned long *sp)
98 {
99         unsigned long *stack, addr;
100         int i;
101 
102 	/*
103 	 * debugging aid: "show_stack(NULL);" prints a
104 	 * back trace.
105 	 */
106 
107         if(sp == NULL)
108                 sp = (unsigned long*)rdsp();
109 
110         stack = sp;
111 
112 	printk("\nStack from %08lx:\n       ", (unsigned long)stack);
113         for(i = 0; i < kstack_depth_to_print; i++) {
114                 if (((long) stack & (THREAD_SIZE-1)) == 0)
115                         break;
116                 if (i && ((i % 8) == 0))
117                         printk("\n       ");
118 		if (__get_user (addr, stack)) {
119 			/* This message matches "failing address" marked
120 			   s390 in ksymoops, so lines containing it will
121 			   not be filtered out by ksymoops.  */
122 			printk ("Failing address 0x%lx\n", (unsigned long)stack);
123 			break;
124 		}
125 		stack++;
126 		printk("%08lx ", addr);
127         }
128 	show_trace(sp);
129 }
130 
131 #if 0
132 /* displays a short stack trace */
133 
134 int
135 show_stack()
136 {
137 	unsigned long *sp = (unsigned long *)rdusp();
138 	int i;
139 	printk("Stack dump [0x%08lx]:\n", (unsigned long)sp);
140 	for(i = 0; i < 16; i++)
141 		printk("sp + %d: 0x%08lx\n", i*4, sp[i]);
142 	return 0;
143 }
144 #endif
145 
146 void
show_registers(struct pt_regs * regs)147 show_registers(struct pt_regs * regs)
148 {
149 	/* We either use rdusp() - the USP register, which might not
150 	   correspond to the current process for all cases we're called,
151 	   or we use the current->thread.usp, which is not up to date for
152 	   the current process.  Experience shows we want the USP
153 	   register.  */
154 	unsigned long usp = rdusp();
155 
156 	printk("IRP: %08lx SRP: %08lx DCCR: %08lx USP: %08lx MOF: %08lx\n",
157 	       regs->irp, regs->srp, regs->dccr, usp, regs->mof );
158 	printk(" r0: %08lx  r1: %08lx   r2: %08lx  r3: %08lx\n",
159 	       regs->r0, regs->r1, regs->r2, regs->r3);
160 	printk(" r4: %08lx  r5: %08lx   r6: %08lx  r7: %08lx\n",
161 	       regs->r4, regs->r5, regs->r6, regs->r7);
162 	printk(" r8: %08lx  r9: %08lx  r10: %08lx r11: %08lx\n",
163 	       regs->r8, regs->r9, regs->r10, regs->r11);
164 	printk("r12: %08lx r13: %08lx oR10: %08lx\n",
165 	       regs->r12, regs->r13, regs->orig_r10);
166 	printk("R_MMU_CAUSE: %08lx\n", (unsigned long)*R_MMU_CAUSE);
167 	printk("Process %s (pid: %d, stackpage=%08lx)\n",
168 	       current->comm, current->pid, (unsigned long)current);
169 
170 	/*
171          * When in-kernel, we also print out the stack and code at the
172          * time of the fault..
173          */
174         if (! user_mode(regs)) {
175 	  	int i;
176 
177                 show_stack((unsigned long*)usp);
178 
179 		/* Dump kernel stack if the previous dump wasn't one.  */
180 		if (usp != 0)
181 			show_stack (NULL);
182 
183                 printk("\nCode: ");
184                 if(regs->irp < PAGE_OFFSET)
185                         goto bad;
186 
187 		/* Often enough the value at regs->irp does not point to
188 		   the interesting instruction, which is most often the
189 		   _previous_ instruction.  So we dump at an offset large
190 		   enough that instruction decoding should be in sync at
191 		   the interesting point, but small enough to fit on a row
192 		   (sort of).  We point out the regs->irp location in a
193 		   ksymoops-friendly way by wrapping the byte for that
194 		   address in parentheses.  */
195                 for(i = -12; i < 12; i++)
196                 {
197                         unsigned char c;
198                         if(__get_user(c, &((unsigned char*)regs->irp)[i])) {
199 bad:
200                                 printk(" Bad IP value.");
201                                 break;
202                         }
203 
204 			if (i == 0)
205 			  printk("(%02x) ", c);
206 			else
207 			  printk("%02x ", c);
208                 }
209 		printk("\n");
210         }
211 }
212 
213 /* Called from entry.S when the watchdog has bitten
214  * We print out something resembling an oops dump, and if
215  * we have the nice doggy development flag set, we halt here
216  * instead of rebooting.
217  */
218 extern void reset_watchdog(void);
219 extern void stop_watchdog(void);
220 
221 void
watchdog_bite_hook(struct pt_regs * regs)222 watchdog_bite_hook(struct pt_regs *regs)
223 {
224 #ifdef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
225 	cli();
226 	stop_watchdog();
227 	show_registers(regs);
228 	while(1) /* nothing */;
229 #else
230 	show_registers(regs);
231 #endif
232 }
233 
dump_stack(void)234 void dump_stack(void)
235 {
236 	show_stack(NULL);
237 }
238 
239 /* This is normally the 'Oops' routine */
240 void
die_if_kernel(const char * str,struct pt_regs * regs,long err)241 die_if_kernel(const char * str, struct pt_regs * regs, long err)
242 {
243 	if(user_mode(regs))
244 		return;
245 
246 #ifdef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
247 	/* This printout might take too long and trigger the
248 	 * watchdog normally. If we're in the nice doggy
249 	 * development mode, stop the watchdog during printout.
250 	 */
251 	stop_watchdog();
252 #endif
253 
254 	printk("%s: %04lx\n", str, err & 0xffff);
255 
256 	show_registers(regs);
257 
258 #ifdef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
259 	reset_watchdog();
260 #endif
261 	do_exit(SIGSEGV);
262 }
263 
264 void __init
trap_init(void)265 trap_init(void)
266 {
267 	/* Nothing needs to be done */
268 }
269