1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel traps/events for Hexagon processor
5 * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
8 #include <linux/init.h>
9 #include <linux/sched/signal.h>
10 #include <linux/sched/debug.h>
11 #include <linux/sched/task_stack.h>
12 #include <linux/module.h>
13 #include <linux/kallsyms.h>
14 #include <linux/kdebug.h>
15 #include <linux/syscalls.h>
16 #include <linux/signal.h>
17 #include <linux/tracehook.h>
18 #include <asm/traps.h>
19 #include <asm/vm_fault.h>
20 #include <asm/syscall.h>
21 #include <asm/registers.h>
22 #include <asm/unistd.h>
23 #include <asm/sections.h>
25 # include <linux/kgdb.h>
28 #define TRAP_SYSCALL 1
29 #define TRAP_DEBUG 0xdb
31 void __init trap_init(void)
35 #ifdef CONFIG_GENERIC_BUG
36 /* Maybe should resemble arch/sh/kernel/traps.c ?? */
37 int is_valid_bugaddr(unsigned long addr)
41 #endif /* CONFIG_GENERIC_BUG */
43 static const char *ex_name(int ex)
48 return "Execute protection fault";
51 return "Read protection fault";
54 return "Write protection fault";
56 return "Misaligned instruction";
58 return "Multiple writes to same register in packet";
60 return "Program counter values that are not properly aligned";
62 return "Misaligned data load";
64 return "Misaligned data store";
67 return "Illegal instruction";
69 return "Precise bus error";
74 return "Debugger trap";
77 return "Unrecognized exception";
81 static void do_show_stack(struct task_struct *task, unsigned long *fp,
84 int kstack_depth_to_print = 24;
85 unsigned long offset, size;
86 const char *name = NULL;
88 unsigned long low, high;
96 printk(KERN_INFO "CPU#%d, %s/%d, Call Trace:\n",
97 raw_smp_processor_id(), task->comm,
101 if (task == current) {
102 asm("%0 = r30" : "=r" (fp));
104 fp = (unsigned long *)
105 ((struct hexagon_switch_stack *)
106 task->thread.switch_sp)->fp;
110 if ((((unsigned long) fp) & 0x3) || ((unsigned long) fp < 0x1000)) {
111 printk(KERN_INFO "-- Corrupt frame pointer %p\n", fp);
115 /* Saved link reg is one word above FP */
119 /* Expect kernel stack to be in-bounds */
120 low = (unsigned long)task_stack_page(task);
121 high = low + THREAD_SIZE - 8;
122 low += sizeof(struct thread_info);
124 for (i = 0; i < kstack_depth_to_print; i++) {
126 name = kallsyms_lookup(ip, &size, &offset, &modname, tmpstr);
128 printk(KERN_INFO "[%p] 0x%lx: %s + 0x%lx", fp, ip, name,
130 if (((unsigned long) fp < low) || (high < (unsigned long) fp))
131 printk(KERN_CONT " (FP out of bounds!)");
133 printk(KERN_CONT " [%s] ", modname);
134 printk(KERN_CONT "\n");
136 newfp = (unsigned long *) *fp;
138 if (((unsigned long) newfp) & 0x3) {
139 printk(KERN_INFO "-- Corrupt frame pointer %p\n",
144 /* Attempt to continue past exception. */
146 struct pt_regs *regs = (struct pt_regs *) (((void *)fp)
149 if (regs->syscall_nr != -1) {
150 printk(KERN_INFO "-- trap0 -- syscall_nr: %ld",
152 printk(KERN_CONT " psp: %lx elr: %lx\n",
153 pt_psp(regs), pt_elr(regs));
156 /* really want to see more ... */
157 kstack_depth_to_print += 6;
158 printk(KERN_INFO "-- %s (0x%lx) badva: %lx\n",
159 ex_name(pt_cause(regs)), pt_cause(regs),
163 newfp = (unsigned long *) regs->r30;
169 /* If link reg is null, we are done. */
173 /* If newfp isn't larger, we're tracing garbage. */
181 void show_stack(struct task_struct *task, unsigned long *fp)
183 /* Saved link reg is one word above FP */
184 do_show_stack(task, fp, 0);
187 int die(const char *str, struct pt_regs *regs, long err)
193 .lock = __SPIN_LOCK_UNLOCKED(die.lock),
200 spin_lock_irq(&die.lock);
202 printk(KERN_EMERG "Oops: %s[#%d]:\n", str, ++die.counter);
204 if (notify_die(DIE_OOPS, str, regs, err, pt_cause(regs), SIGSEGV) ==
210 do_show_stack(current, ®s->r30, pt_elr(regs));
213 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
215 spin_unlock_irq(&die.lock);
218 panic("Fatal exception in interrupt");
221 panic("Fatal exception");
228 int die_if_kernel(char *str, struct pt_regs *regs, long err)
230 if (!user_mode(regs))
231 return die(str, regs, err);
237 * It's not clear that misaligned fetches are ever recoverable.
239 static void misaligned_instruction(struct pt_regs *regs)
241 die_if_kernel("Misaligned Instruction", regs, 0);
246 * Misaligned loads and stores, on the other hand, can be
247 * emulated, and probably should be, some day. But for now
248 * they will be considered fatal.
250 static void misaligned_data_load(struct pt_regs *regs)
252 die_if_kernel("Misaligned Data Load", regs, 0);
256 static void misaligned_data_store(struct pt_regs *regs)
258 die_if_kernel("Misaligned Data Store", regs, 0);
262 static void illegal_instruction(struct pt_regs *regs)
264 die_if_kernel("Illegal Instruction", regs, 0);
269 * Precise bus errors may be recoverable with a a retry,
270 * but for now, treat them as irrecoverable.
272 static void precise_bus_error(struct pt_regs *regs)
274 die_if_kernel("Precise Bus Error", regs, 0);
279 * If anything is to be done here other than panic,
280 * it will probably be complex and migrate to another
281 * source module. For now, just die.
283 static void cache_error(struct pt_regs *regs)
285 die("Cache Error", regs, 0);
289 * General exception handler
291 void do_genex(struct pt_regs *regs)
294 * Decode Cause and Dispatch
296 switch (pt_cause(regs)) {
299 execute_protection_fault(regs);
303 read_protection_fault(regs);
307 write_protection_fault(regs);
310 misaligned_instruction(regs);
313 illegal_instruction(regs);
316 misaligned_instruction(regs);
319 misaligned_data_load(regs);
322 misaligned_data_store(regs);
326 illegal_instruction(regs);
329 precise_bus_error(regs);
335 /* Halt and catch fire */
336 panic("Unrecognized exception 0x%lx\n", pt_cause(regs));
341 /* Indirect system call dispatch */
342 long sys_syscall(void)
344 printk(KERN_ERR "sys_syscall invoked!\n");
348 void do_trap0(struct pt_regs *regs)
352 switch (pt_cause(regs)) {
354 /* System call is trap0 #1 */
356 /* allow strace to catch syscall args */
357 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE) &&
358 tracehook_report_syscall_entry(regs)))
359 return; /* return -ENOSYS somewhere? */
361 /* Interrupts should be re-enabled for syscall processing */
362 __vmsetie(VM_INT_ENABLE);
365 * System call number is in r6, arguments in r0..r5.
366 * Fortunately, no Linux syscall has more than 6 arguments,
367 * and Hexagon ABI passes first 6 arguments in registers.
368 * 64-bit arguments are passed in odd/even register pairs.
369 * Fortunately, we have no system calls that take more
370 * than three arguments with more than one 64-bit value.
371 * Should that change, we'd need to redesign to copy
372 * between user and kernel stacks.
374 regs->syscall_nr = regs->r06;
377 * GPR R0 carries the first parameter, and is also used
378 * to report the return value. We need a backup of
379 * the user's value in case we need to do a late restart
380 * of the system call.
382 regs->restart_r0 = regs->r00;
384 if ((unsigned long) regs->syscall_nr >= __NR_syscalls) {
387 syscall = (syscall_fn)
388 (sys_call_table[regs->syscall_nr]);
389 regs->r00 = syscall(regs->r00, regs->r01,
390 regs->r02, regs->r03,
391 regs->r04, regs->r05);
394 /* allow strace to get the syscall return state */
395 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE)))
396 tracehook_report_syscall_exit(regs, 0);
400 /* Trap0 0xdb is debug breakpoint */
401 if (user_mode(regs)) {
403 * Some architecures add some per-thread state
404 * to distinguish between breakpoint traps and
405 * trace traps. We may want to do that, and
406 * set the si_code value appropriately, or we
407 * may want to use a different trap0 flavor.
409 force_sig_fault(SIGTRAP, TRAP_BRKPT,
410 (void __user *) pt_elr(regs));
413 kgdb_handle_exception(pt_cause(regs), SIGTRAP,
419 /* Ignore other trap0 codes for now, especially 0 (Angel calls) */
423 * Machine check exception handler
425 void do_machcheck(struct pt_regs *regs)
427 /* Halt and catch fire */
432 * Treat this like the old 0xdb trap.
435 void do_debug_exception(struct pt_regs *regs)
437 regs->hvmer.vmest &= ~HVM_VMEST_CAUSE_MSK;
438 regs->hvmer.vmest |= (TRAP_DEBUG << HVM_VMEST_CAUSE_SFT);