xref: /openbmc/linux/arch/hexagon/kernel/traps.c (revision 12eb4683)
1 /*
2  * Kernel traps/events for Hexagon processor
3  *
4  * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 and
8  * only version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.
19  */
20 
21 #include <linux/init.h>
22 #include <linux/sched.h>
23 #include <linux/module.h>
24 #include <linux/kallsyms.h>
25 #include <linux/kdebug.h>
26 #include <linux/syscalls.h>
27 #include <linux/signal.h>
28 #include <linux/tracehook.h>
29 #include <asm/traps.h>
30 #include <asm/vm_fault.h>
31 #include <asm/syscall.h>
32 #include <asm/registers.h>
33 #include <asm/unistd.h>
34 #include <asm/sections.h>
35 #ifdef CONFIG_KGDB
36 # include <linux/kgdb.h>
37 #endif
38 
39 #define TRAP_SYSCALL	1
40 #define TRAP_DEBUG	0xdb
41 
42 void __init trap_init(void)
43 {
44 }
45 
46 #ifdef CONFIG_GENERIC_BUG
47 /* Maybe should resemble arch/sh/kernel/traps.c ?? */
48 int is_valid_bugaddr(unsigned long addr)
49 {
50 	return 1;
51 }
52 #endif /* CONFIG_GENERIC_BUG */
53 
54 static const char *ex_name(int ex)
55 {
56 	switch (ex) {
57 	case HVM_GE_C_XPROT:
58 	case HVM_GE_C_XUSER:
59 		return "Execute protection fault";
60 	case HVM_GE_C_RPROT:
61 	case HVM_GE_C_RUSER:
62 		return "Read protection fault";
63 	case HVM_GE_C_WPROT:
64 	case HVM_GE_C_WUSER:
65 		return "Write protection fault";
66 	case HVM_GE_C_XMAL:
67 		return "Misaligned instruction";
68 	case HVM_GE_C_WREG:
69 		return "Multiple writes to same register in packet";
70 	case HVM_GE_C_PCAL:
71 		return "Program counter values that are not properly aligned";
72 	case HVM_GE_C_RMAL:
73 		return "Misaligned data load";
74 	case HVM_GE_C_WMAL:
75 		return "Misaligned data store";
76 	case HVM_GE_C_INVI:
77 	case HVM_GE_C_PRIVI:
78 		return "Illegal instruction";
79 	case HVM_GE_C_BUS:
80 		return "Precise bus error";
81 	case HVM_GE_C_CACHE:
82 		return "Cache error";
83 
84 	case 0xdb:
85 		return "Debugger trap";
86 
87 	default:
88 		return "Unrecognized exception";
89 	}
90 }
91 
92 static void do_show_stack(struct task_struct *task, unsigned long *fp,
93 			  unsigned long ip)
94 {
95 	int kstack_depth_to_print = 24;
96 	unsigned long offset, size;
97 	const char *name = NULL;
98 	unsigned long *newfp;
99 	unsigned long low, high;
100 	char tmpstr[128];
101 	char *modname;
102 	int i;
103 
104 	if (task == NULL)
105 		task = current;
106 
107 	printk(KERN_INFO "CPU#%d, %s/%d, Call Trace:\n",
108 	       raw_smp_processor_id(), task->comm,
109 	       task_pid_nr(task));
110 
111 	if (fp == NULL) {
112 		if (task == current) {
113 			asm("%0 = r30" : "=r" (fp));
114 		} else {
115 			fp = (unsigned long *)
116 			     ((struct hexagon_switch_stack *)
117 			     task->thread.switch_sp)->fp;
118 		}
119 	}
120 
121 	if ((((unsigned long) fp) & 0x3) || ((unsigned long) fp < 0x1000)) {
122 		printk(KERN_INFO "-- Corrupt frame pointer %p\n", fp);
123 		return;
124 	}
125 
126 	/* Saved link reg is one word above FP */
127 	if (!ip)
128 		ip = *(fp+1);
129 
130 	/* Expect kernel stack to be in-bounds */
131 	low = (unsigned long)task_stack_page(task);
132 	high = low + THREAD_SIZE - 8;
133 	low += sizeof(struct thread_info);
134 
135 	for (i = 0; i < kstack_depth_to_print; i++) {
136 
137 		name = kallsyms_lookup(ip, &size, &offset, &modname, tmpstr);
138 
139 		printk(KERN_INFO "[%p] 0x%lx: %s + 0x%lx", fp, ip, name,
140 			offset);
141 		if (((unsigned long) fp < low) || (high < (unsigned long) fp))
142 			printk(KERN_CONT " (FP out of bounds!)");
143 		if (modname)
144 			printk(KERN_CONT " [%s] ", modname);
145 		printk(KERN_CONT "\n");
146 
147 		newfp = (unsigned long *) *fp;
148 
149 		if (((unsigned long) newfp) & 0x3) {
150 			printk(KERN_INFO "-- Corrupt frame pointer %p\n",
151 				newfp);
152 			break;
153 		}
154 
155 		/* Attempt to continue past exception. */
156 		if (0 == newfp) {
157 			struct pt_regs *regs = (struct pt_regs *) (((void *)fp)
158 						+ 8);
159 
160 			if (regs->syscall_nr != -1) {
161 				printk(KERN_INFO "-- trap0 -- syscall_nr: %ld",
162 					regs->syscall_nr);
163 				printk(KERN_CONT "  psp: %lx  elr: %lx\n",
164 					 pt_psp(regs), pt_elr(regs));
165 				break;
166 			} else {
167 				/* really want to see more ... */
168 				kstack_depth_to_print += 6;
169 				printk(KERN_INFO "-- %s (0x%lx)  badva: %lx\n",
170 					ex_name(pt_cause(regs)), pt_cause(regs),
171 					pt_badva(regs));
172 			}
173 
174 			newfp = (unsigned long *) regs->r30;
175 			ip = pt_elr(regs);
176 		} else {
177 			ip = *(newfp + 1);
178 		}
179 
180 		/* If link reg is null, we are done. */
181 		if (ip == 0x0)
182 			break;
183 
184 		/* If newfp isn't larger, we're tracing garbage. */
185 		if (newfp > fp)
186 			fp = newfp;
187 		else
188 			break;
189 	}
190 }
191 
192 void show_stack(struct task_struct *task, unsigned long *fp)
193 {
194 	/* Saved link reg is one word above FP */
195 	do_show_stack(task, fp, 0);
196 }
197 
198 int die(const char *str, struct pt_regs *regs, long err)
199 {
200 	static struct {
201 		spinlock_t lock;
202 		int counter;
203 	} die = {
204 		.lock = __SPIN_LOCK_UNLOCKED(die.lock),
205 		.counter = 0
206 	};
207 
208 	console_verbose();
209 	oops_enter();
210 
211 	spin_lock_irq(&die.lock);
212 	bust_spinlocks(1);
213 	printk(KERN_EMERG "Oops: %s[#%d]:\n", str, ++die.counter);
214 
215 	if (notify_die(DIE_OOPS, str, regs, err, pt_cause(regs), SIGSEGV) ==
216 	    NOTIFY_STOP)
217 		return 1;
218 
219 	print_modules();
220 	show_regs(regs);
221 	do_show_stack(current, &regs->r30, pt_elr(regs));
222 
223 	bust_spinlocks(0);
224 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
225 
226 	spin_unlock_irq(&die.lock);
227 
228 	if (in_interrupt())
229 		panic("Fatal exception in interrupt");
230 
231 	if (panic_on_oops)
232 		panic("Fatal exception");
233 
234 	oops_exit();
235 	do_exit(err);
236 	return 0;
237 }
238 
239 int die_if_kernel(char *str, struct pt_regs *regs, long err)
240 {
241 	if (!user_mode(regs))
242 		return die(str, regs, err);
243 	else
244 		return 0;
245 }
246 
247 /*
248  * It's not clear that misaligned fetches are ever recoverable.
249  */
250 static void misaligned_instruction(struct pt_regs *regs)
251 {
252 	die_if_kernel("Misaligned Instruction", regs, 0);
253 	force_sig(SIGBUS, current);
254 }
255 
256 /*
257  * Misaligned loads and stores, on the other hand, can be
258  * emulated, and probably should be, some day.  But for now
259  * they will be considered fatal.
260  */
261 static void misaligned_data_load(struct pt_regs *regs)
262 {
263 	die_if_kernel("Misaligned Data Load", regs, 0);
264 	force_sig(SIGBUS, current);
265 }
266 
267 static void misaligned_data_store(struct pt_regs *regs)
268 {
269 	die_if_kernel("Misaligned Data Store", regs, 0);
270 	force_sig(SIGBUS, current);
271 }
272 
273 static void illegal_instruction(struct pt_regs *regs)
274 {
275 	die_if_kernel("Illegal Instruction", regs, 0);
276 	force_sig(SIGILL, current);
277 }
278 
279 /*
280  * Precise bus errors may be recoverable with a a retry,
281  * but for now, treat them as irrecoverable.
282  */
283 static void precise_bus_error(struct pt_regs *regs)
284 {
285 	die_if_kernel("Precise Bus Error", regs, 0);
286 	force_sig(SIGBUS, current);
287 }
288 
289 /*
290  * If anything is to be done here other than panic,
291  * it will probably be complex and migrate to another
292  * source module.  For now, just die.
293  */
294 static void cache_error(struct pt_regs *regs)
295 {
296 	die("Cache Error", regs, 0);
297 }
298 
299 /*
300  * General exception handler
301  */
302 void do_genex(struct pt_regs *regs)
303 {
304 	/*
305 	 * Decode Cause and Dispatch
306 	 */
307 	switch (pt_cause(regs)) {
308 	case HVM_GE_C_XPROT:
309 	case HVM_GE_C_XUSER:
310 		execute_protection_fault(regs);
311 		break;
312 	case HVM_GE_C_RPROT:
313 	case HVM_GE_C_RUSER:
314 		read_protection_fault(regs);
315 		break;
316 	case HVM_GE_C_WPROT:
317 	case HVM_GE_C_WUSER:
318 		write_protection_fault(regs);
319 		break;
320 	case HVM_GE_C_XMAL:
321 		misaligned_instruction(regs);
322 		break;
323 	case HVM_GE_C_WREG:
324 		illegal_instruction(regs);
325 		break;
326 	case HVM_GE_C_PCAL:
327 		misaligned_instruction(regs);
328 		break;
329 	case HVM_GE_C_RMAL:
330 		misaligned_data_load(regs);
331 		break;
332 	case HVM_GE_C_WMAL:
333 		misaligned_data_store(regs);
334 		break;
335 	case HVM_GE_C_INVI:
336 	case HVM_GE_C_PRIVI:
337 		illegal_instruction(regs);
338 		break;
339 	case HVM_GE_C_BUS:
340 		precise_bus_error(regs);
341 		break;
342 	case HVM_GE_C_CACHE:
343 		cache_error(regs);
344 		break;
345 	default:
346 		/* Halt and catch fire */
347 		panic("Unrecognized exception 0x%lx\n", pt_cause(regs));
348 		break;
349 	}
350 }
351 
352 /* Indirect system call dispatch */
353 long sys_syscall(void)
354 {
355 	printk(KERN_ERR "sys_syscall invoked!\n");
356 	return -ENOSYS;
357 }
358 
359 void do_trap0(struct pt_regs *regs)
360 {
361 	syscall_fn syscall;
362 
363 	switch (pt_cause(regs)) {
364 	case TRAP_SYSCALL:
365 		/* System call is trap0 #1 */
366 
367 		/* allow strace to catch syscall args  */
368 		if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE) &&
369 			tracehook_report_syscall_entry(regs)))
370 			return;  /*  return -ENOSYS somewhere?  */
371 
372 		/* Interrupts should be re-enabled for syscall processing */
373 		__vmsetie(VM_INT_ENABLE);
374 
375 		/*
376 		 * System call number is in r6, arguments in r0..r5.
377 		 * Fortunately, no Linux syscall has more than 6 arguments,
378 		 * and Hexagon ABI passes first 6 arguments in registers.
379 		 * 64-bit arguments are passed in odd/even register pairs.
380 		 * Fortunately, we have no system calls that take more
381 		 * than three arguments with more than one 64-bit value.
382 		 * Should that change, we'd need to redesign to copy
383 		 * between user and kernel stacks.
384 		 */
385 		regs->syscall_nr = regs->r06;
386 
387 		/*
388 		 * GPR R0 carries the first parameter, and is also used
389 		 * to report the return value.  We need a backup of
390 		 * the user's value in case we need to do a late restart
391 		 * of the system call.
392 		 */
393 		regs->restart_r0 = regs->r00;
394 
395 		if ((unsigned long) regs->syscall_nr >= __NR_syscalls) {
396 			regs->r00 = -1;
397 		} else {
398 			syscall = (syscall_fn)
399 				  (sys_call_table[regs->syscall_nr]);
400 			regs->r00 = syscall(regs->r00, regs->r01,
401 				   regs->r02, regs->r03,
402 				   regs->r04, regs->r05);
403 		}
404 
405 		/* allow strace to get the syscall return state  */
406 		if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE)))
407 			tracehook_report_syscall_exit(regs, 0);
408 
409 		break;
410 	case TRAP_DEBUG:
411 		/* Trap0 0xdb is debug breakpoint */
412 		if (user_mode(regs)) {
413 			struct siginfo info;
414 
415 			info.si_signo = SIGTRAP;
416 			info.si_errno = 0;
417 			/*
418 			 * Some architecures add some per-thread state
419 			 * to distinguish between breakpoint traps and
420 			 * trace traps.  We may want to do that, and
421 			 * set the si_code value appropriately, or we
422 			 * may want to use a different trap0 flavor.
423 			 */
424 			info.si_code = TRAP_BRKPT;
425 			info.si_addr = (void __user *) pt_elr(regs);
426 			send_sig_info(SIGTRAP, &info, current);
427 		} else {
428 #ifdef CONFIG_KGDB
429 			kgdb_handle_exception(pt_cause(regs), SIGTRAP,
430 					      TRAP_BRKPT, regs);
431 #endif
432 		}
433 		break;
434 	}
435 	/* Ignore other trap0 codes for now, especially 0 (Angel calls) */
436 }
437 
438 /*
439  * Machine check exception handler
440  */
441 void do_machcheck(struct pt_regs *regs)
442 {
443 	/* Halt and catch fire */
444 	__vmstop();
445 }
446 
447 /*
448  * Treat this like the old 0xdb trap.
449  */
450 
451 void do_debug_exception(struct pt_regs *regs)
452 {
453 	regs->hvmer.vmest &= ~HVM_VMEST_CAUSE_MSK;
454 	regs->hvmer.vmest |= (TRAP_DEBUG << HVM_VMEST_CAUSE_SFT);
455 	do_trap0(regs);
456 }
457