2 * Kernel traps/events for Hexagon processor
4 * Copyright (c) 2010-2014, 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
21 #include <linux/init.h>
22 #include <linux/sched/signal.h>
23 #include <linux/sched/debug.h>
24 #include <linux/sched/task_stack.h>
25 #include <linux/module.h>
26 #include <linux/kallsyms.h>
27 #include <linux/kdebug.h>
28 #include <linux/syscalls.h>
29 #include <linux/signal.h>
30 #include <linux/tracehook.h>
31 #include <asm/traps.h>
32 #include <asm/vm_fault.h>
33 #include <asm/syscall.h>
34 #include <asm/registers.h>
35 #include <asm/unistd.h>
36 #include <asm/sections.h>
38 # include <linux/kgdb.h>
41 #define TRAP_SYSCALL 1
42 #define TRAP_DEBUG 0xdb
44 void __init
trap_init(void)
48 #ifdef CONFIG_GENERIC_BUG
49 /* Maybe should resemble arch/sh/kernel/traps.c ?? */
50 int is_valid_bugaddr(unsigned long addr
)
54 #endif /* CONFIG_GENERIC_BUG */
56 static const char *ex_name(int ex
)
61 return "Execute protection fault";
64 return "Read protection fault";
67 return "Write protection fault";
69 return "Misaligned instruction";
71 return "Multiple writes to same register in packet";
73 return "Program counter values that are not properly aligned";
75 return "Misaligned data load";
77 return "Misaligned data store";
80 return "Illegal instruction";
82 return "Precise bus error";
87 return "Debugger trap";
90 return "Unrecognized exception";
94 static void do_show_stack(struct task_struct
*task
, unsigned long *fp
,
97 int kstack_depth_to_print
= 24;
98 unsigned long offset
, size
;
99 const char *name
= NULL
;
100 unsigned long *newfp
;
101 unsigned long low
, high
;
109 printk(KERN_INFO
"CPU#%d, %s/%d, Call Trace:\n",
110 raw_smp_processor_id(), task
->comm
,
114 if (task
== current
) {
115 asm("%0 = r30" : "=r" (fp
));
117 fp
= (unsigned long *)
118 ((struct hexagon_switch_stack
*)
119 task
->thread
.switch_sp
)->fp
;
123 if ((((unsigned long) fp
) & 0x3) || ((unsigned long) fp
< 0x1000)) {
124 printk(KERN_INFO
"-- Corrupt frame pointer %p\n", fp
);
128 /* Saved link reg is one word above FP */
132 /* Expect kernel stack to be in-bounds */
133 low
= (unsigned long)task_stack_page(task
);
134 high
= low
+ THREAD_SIZE
- 8;
135 low
+= sizeof(struct thread_info
);
137 for (i
= 0; i
< kstack_depth_to_print
; i
++) {
139 name
= kallsyms_lookup(ip
, &size
, &offset
, &modname
, tmpstr
);
141 printk(KERN_INFO
"[%p] 0x%lx: %s + 0x%lx", fp
, ip
, name
,
143 if (((unsigned long) fp
< low
) || (high
< (unsigned long) fp
))
144 printk(KERN_CONT
" (FP out of bounds!)");
146 printk(KERN_CONT
" [%s] ", modname
);
147 printk(KERN_CONT
"\n");
149 newfp
= (unsigned long *) *fp
;
151 if (((unsigned long) newfp
) & 0x3) {
152 printk(KERN_INFO
"-- Corrupt frame pointer %p\n",
157 /* Attempt to continue past exception. */
159 struct pt_regs
*regs
= (struct pt_regs
*) (((void *)fp
)
162 if (regs
->syscall_nr
!= -1) {
163 printk(KERN_INFO
"-- trap0 -- syscall_nr: %ld",
165 printk(KERN_CONT
" psp: %lx elr: %lx\n",
166 pt_psp(regs
), pt_elr(regs
));
169 /* really want to see more ... */
170 kstack_depth_to_print
+= 6;
171 printk(KERN_INFO
"-- %s (0x%lx) badva: %lx\n",
172 ex_name(pt_cause(regs
)), pt_cause(regs
),
176 newfp
= (unsigned long *) regs
->r30
;
182 /* If link reg is null, we are done. */
186 /* If newfp isn't larger, we're tracing garbage. */
194 void show_stack(struct task_struct
*task
, unsigned long *fp
)
196 /* Saved link reg is one word above FP */
197 do_show_stack(task
, fp
, 0);
200 int die(const char *str
, struct pt_regs
*regs
, long err
)
206 .lock
= __SPIN_LOCK_UNLOCKED(die
.lock
),
213 spin_lock_irq(&die
.lock
);
215 printk(KERN_EMERG
"Oops: %s[#%d]:\n", str
, ++die
.counter
);
217 if (notify_die(DIE_OOPS
, str
, regs
, err
, pt_cause(regs
), SIGSEGV
) ==
223 do_show_stack(current
, ®s
->r30
, pt_elr(regs
));
226 add_taint(TAINT_DIE
, LOCKDEP_NOW_UNRELIABLE
);
228 spin_unlock_irq(&die
.lock
);
231 panic("Fatal exception in interrupt");
234 panic("Fatal exception");
241 int die_if_kernel(char *str
, struct pt_regs
*regs
, long err
)
243 if (!user_mode(regs
))
244 return die(str
, regs
, err
);
250 * It's not clear that misaligned fetches are ever recoverable.
252 static void misaligned_instruction(struct pt_regs
*regs
)
254 die_if_kernel("Misaligned Instruction", regs
, 0);
255 force_sig(SIGBUS
, current
);
259 * Misaligned loads and stores, on the other hand, can be
260 * emulated, and probably should be, some day. But for now
261 * they will be considered fatal.
263 static void misaligned_data_load(struct pt_regs
*regs
)
265 die_if_kernel("Misaligned Data Load", regs
, 0);
266 force_sig(SIGBUS
, current
);
269 static void misaligned_data_store(struct pt_regs
*regs
)
271 die_if_kernel("Misaligned Data Store", regs
, 0);
272 force_sig(SIGBUS
, current
);
275 static void illegal_instruction(struct pt_regs
*regs
)
277 die_if_kernel("Illegal Instruction", regs
, 0);
278 force_sig(SIGILL
, current
);
282 * Precise bus errors may be recoverable with a a retry,
283 * but for now, treat them as irrecoverable.
285 static void precise_bus_error(struct pt_regs
*regs
)
287 die_if_kernel("Precise Bus Error", regs
, 0);
288 force_sig(SIGBUS
, current
);
292 * If anything is to be done here other than panic,
293 * it will probably be complex and migrate to another
294 * source module. For now, just die.
296 static void cache_error(struct pt_regs
*regs
)
298 die("Cache Error", regs
, 0);
302 * General exception handler
304 void do_genex(struct pt_regs
*regs
)
307 * Decode Cause and Dispatch
309 switch (pt_cause(regs
)) {
312 execute_protection_fault(regs
);
316 read_protection_fault(regs
);
320 write_protection_fault(regs
);
323 misaligned_instruction(regs
);
326 illegal_instruction(regs
);
329 misaligned_instruction(regs
);
332 misaligned_data_load(regs
);
335 misaligned_data_store(regs
);
339 illegal_instruction(regs
);
342 precise_bus_error(regs
);
348 /* Halt and catch fire */
349 panic("Unrecognized exception 0x%lx\n", pt_cause(regs
));
354 /* Indirect system call dispatch */
355 long sys_syscall(void)
357 printk(KERN_ERR
"sys_syscall invoked!\n");
361 void do_trap0(struct pt_regs
*regs
)
365 switch (pt_cause(regs
)) {
367 /* System call is trap0 #1 */
369 /* allow strace to catch syscall args */
370 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE
) &&
371 tracehook_report_syscall_entry(regs
)))
372 return; /* return -ENOSYS somewhere? */
374 /* Interrupts should be re-enabled for syscall processing */
375 __vmsetie(VM_INT_ENABLE
);
378 * System call number is in r6, arguments in r0..r5.
379 * Fortunately, no Linux syscall has more than 6 arguments,
380 * and Hexagon ABI passes first 6 arguments in registers.
381 * 64-bit arguments are passed in odd/even register pairs.
382 * Fortunately, we have no system calls that take more
383 * than three arguments with more than one 64-bit value.
384 * Should that change, we'd need to redesign to copy
385 * between user and kernel stacks.
387 regs
->syscall_nr
= regs
->r06
;
390 * GPR R0 carries the first parameter, and is also used
391 * to report the return value. We need a backup of
392 * the user's value in case we need to do a late restart
393 * of the system call.
395 regs
->restart_r0
= regs
->r00
;
397 if ((unsigned long) regs
->syscall_nr
>= __NR_syscalls
) {
400 syscall
= (syscall_fn
)
401 (sys_call_table
[regs
->syscall_nr
]);
402 regs
->r00
= syscall(regs
->r00
, regs
->r01
,
403 regs
->r02
, regs
->r03
,
404 regs
->r04
, regs
->r05
);
407 /* allow strace to get the syscall return state */
408 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE
)))
409 tracehook_report_syscall_exit(regs
, 0);
413 /* Trap0 0xdb is debug breakpoint */
414 if (user_mode(regs
)) {
416 * Some architecures add some per-thread state
417 * to distinguish between breakpoint traps and
418 * trace traps. We may want to do that, and
419 * set the si_code value appropriately, or we
420 * may want to use a different trap0 flavor.
422 force_sig_fault(SIGTRAP
, TRAP_BRKPT
,
423 (void __user
*) pt_elr(regs
), current
);
426 kgdb_handle_exception(pt_cause(regs
), SIGTRAP
,
432 /* Ignore other trap0 codes for now, especially 0 (Angel calls) */
436 * Machine check exception handler
438 void do_machcheck(struct pt_regs
*regs
)
440 /* Halt and catch fire */
445 * Treat this like the old 0xdb trap.
448 void do_debug_exception(struct pt_regs
*regs
)
450 regs
->hvmer
.vmest
&= ~HVM_VMEST_CAUSE_MSK
;
451 regs
->hvmer
.vmest
|= (TRAP_DEBUG
<< HVM_VMEST_CAUSE_SFT
);