Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / arch / hexagon / kernel / traps.c
blob7858663352b9ec1662959ad759a62ca07e3b639c
1 /*
2 * Kernel traps/events for Hexagon processor
4 * Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
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.
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.
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.
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
39 #define TRAP_SYSCALL 1
40 #define TRAP_DEBUG 0xdb
42 void __init trap_init(void)
46 #ifdef CONFIG_GENERIC_BUG
47 /* Maybe should resemble arch/sh/kernel/traps.c ?? */
48 int is_valid_bugaddr(unsigned long addr)
50 return 1;
52 #endif /* CONFIG_GENERIC_BUG */
54 static const char *ex_name(int ex)
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";
84 case 0xdb:
85 return "Debugger trap";
87 default:
88 return "Unrecognized exception";
92 static void do_show_stack(struct task_struct *task, unsigned long *fp,
93 unsigned long ip)
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;
104 if (task == NULL)
105 task = current;
107 printk(KERN_INFO "CPU#%d, %s/%d, Call Trace:\n",
108 raw_smp_processor_id(), task->comm,
109 task_pid_nr(task));
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;
121 if ((((unsigned long) fp) & 0x3) || ((unsigned long) fp < 0x1000)) {
122 printk(KERN_INFO "-- Corrupt frame pointer %p\n", fp);
123 return;
126 /* Saved link reg is one word above FP */
127 if (!ip)
128 ip = *(fp+1);
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);
135 for (i = 0; i < kstack_depth_to_print; i++) {
137 name = kallsyms_lookup(ip, &size, &offset, &modname, tmpstr);
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");
147 newfp = (unsigned long *) *fp;
149 if (((unsigned long) newfp) & 0x3) {
150 printk(KERN_INFO "-- Corrupt frame pointer %p\n",
151 newfp);
152 break;
155 /* Attempt to continue past exception. */
156 if (0 == newfp) {
157 struct pt_regs *regs = (struct pt_regs *) (((void *)fp)
158 + 8);
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));
174 newfp = (unsigned long *) regs->r30;
175 ip = pt_elr(regs);
176 } else {
177 ip = *(newfp + 1);
180 /* If link reg is null, we are done. */
181 if (ip == 0x0)
182 break;
184 /* If newfp isn't larger, we're tracing garbage. */
185 if (newfp > fp)
186 fp = newfp;
187 else
188 break;
192 void show_stack(struct task_struct *task, unsigned long *fp)
194 /* Saved link reg is one word above FP */
195 do_show_stack(task, fp, 0);
198 int die(const char *str, struct pt_regs *regs, long err)
200 static struct {
201 spinlock_t lock;
202 int counter;
203 } die = {
204 .lock = __SPIN_LOCK_UNLOCKED(die.lock),
205 .counter = 0
208 console_verbose();
209 oops_enter();
211 spin_lock_irq(&die.lock);
212 bust_spinlocks(1);
213 printk(KERN_EMERG "Oops: %s[#%d]:\n", str, ++die.counter);
215 if (notify_die(DIE_OOPS, str, regs, err, pt_cause(regs), SIGSEGV) ==
216 NOTIFY_STOP)
217 return 1;
219 print_modules();
220 show_regs(regs);
221 do_show_stack(current, &regs->r30, pt_elr(regs));
223 bust_spinlocks(0);
224 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
226 spin_unlock_irq(&die.lock);
228 if (in_interrupt())
229 panic("Fatal exception in interrupt");
231 if (panic_on_oops)
232 panic("Fatal exception");
234 oops_exit();
235 do_exit(err);
236 return 0;
239 int die_if_kernel(char *str, struct pt_regs *regs, long err)
241 if (!user_mode(regs))
242 return die(str, regs, err);
243 else
244 return 0;
248 * It's not clear that misaligned fetches are ever recoverable.
250 static void misaligned_instruction(struct pt_regs *regs)
252 die_if_kernel("Misaligned Instruction", regs, 0);
253 force_sig(SIGBUS, current);
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.
261 static void misaligned_data_load(struct pt_regs *regs)
263 die_if_kernel("Misaligned Data Load", regs, 0);
264 force_sig(SIGBUS, current);
267 static void misaligned_data_store(struct pt_regs *regs)
269 die_if_kernel("Misaligned Data Store", regs, 0);
270 force_sig(SIGBUS, current);
273 static void illegal_instruction(struct pt_regs *regs)
275 die_if_kernel("Illegal Instruction", regs, 0);
276 force_sig(SIGILL, current);
280 * Precise bus errors may be recoverable with a a retry,
281 * but for now, treat them as irrecoverable.
283 static void precise_bus_error(struct pt_regs *regs)
285 die_if_kernel("Precise Bus Error", regs, 0);
286 force_sig(SIGBUS, current);
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.
294 static void cache_error(struct pt_regs *regs)
296 die("Cache Error", regs, 0);
300 * General exception handler
302 void do_genex(struct pt_regs *regs)
305 * Decode Cause and Dispatch
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;
352 /* Indirect system call dispatch */
353 long sys_syscall(void)
355 printk(KERN_ERR "sys_syscall invoked!\n");
356 return -ENOSYS;
359 void do_trap0(struct pt_regs *regs)
361 syscall_fn syscall;
363 switch (pt_cause(regs)) {
364 case TRAP_SYSCALL:
365 /* System call is trap0 #1 */
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? */
372 /* Interrupts should be re-enabled for syscall processing */
373 __vmsetie(VM_INT_ENABLE);
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.
385 regs->syscall_nr = regs->r06;
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.
393 regs->restart_r0 = regs->r00;
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);
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);
409 break;
410 case TRAP_DEBUG:
411 /* Trap0 0xdb is debug breakpoint */
412 if (user_mode(regs)) {
413 struct siginfo info;
415 info.si_signo = SIGTRAP;
416 info.si_errno = 0;
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.
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
433 break;
435 /* Ignore other trap0 codes for now, especially 0 (Angel calls) */
439 * Machine check exception handler
441 void do_machcheck(struct pt_regs *regs)
443 /* Halt and catch fire */
444 __vmstop();
448 * Treat this like the old 0xdb trap.
451 void do_debug_exception(struct pt_regs *regs)
453 regs->hvmer.vmest &= ~HVM_VMEST_CAUSE_MSK;
454 regs->hvmer.vmest |= (TRAP_DEBUG << HVM_VMEST_CAUSE_SFT);
455 do_trap0(regs);