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 #ifdef CONFIG_GENERIC_BUG
32 /* Maybe should resemble arch/sh/kernel/traps.c ?? */
33 int is_valid_bugaddr(unsigned long addr)
37 #endif /* CONFIG_GENERIC_BUG */
39 static const char *ex_name(int ex)
44 return "Execute protection fault";
47 return "Read protection fault";
50 return "Write protection fault";
52 return "Misaligned instruction";
54 return "Multiple writes to same register in packet";
56 return "Program counter values that are not properly aligned";
58 return "Misaligned data load";
60 return "Misaligned data store";
63 return "Illegal instruction";
65 return "Precise bus error";
70 return "Debugger trap";
73 return "Unrecognized exception";
77 static void do_show_stack(struct task_struct *task, unsigned long *fp,
78 unsigned long ip, const char *loglvl)
80 int kstack_depth_to_print = 24;
81 unsigned long offset, size;
82 const char *name = NULL;
84 unsigned long low, high;
92 printk("%sCPU#%d, %s/%d, Call Trace:\n", loglvl, raw_smp_processor_id(),
93 task->comm, task_pid_nr(task));
96 if (task == current) {
97 asm("%0 = r30" : "=r" (fp));
99 fp = (unsigned long *)
100 ((struct hexagon_switch_stack *)
101 task->thread.switch_sp)->fp;
105 if ((((unsigned long) fp) & 0x3) || ((unsigned long) fp < 0x1000)) {
106 printk("%s-- Corrupt frame pointer %p\n", loglvl, fp);
110 /* Saved link reg is one word above FP */
114 /* Expect kernel stack to be in-bounds */
115 low = (unsigned long)task_stack_page(task);
116 high = low + THREAD_SIZE - 8;
117 low += sizeof(struct thread_info);
119 for (i = 0; i < kstack_depth_to_print; i++) {
121 name = kallsyms_lookup(ip, &size, &offset, &modname, tmpstr);
123 printk("%s[%p] 0x%lx: %s + 0x%lx", loglvl, fp, ip, name, offset);
124 if (((unsigned long) fp < low) || (high < (unsigned long) fp))
125 printk(KERN_CONT " (FP out of bounds!)");
127 printk(KERN_CONT " [%s] ", modname);
128 printk(KERN_CONT "\n");
130 newfp = (unsigned long *) *fp;
132 if (((unsigned long) newfp) & 0x3) {
133 printk("%s-- Corrupt frame pointer %p\n", loglvl, newfp);
137 /* Attempt to continue past exception. */
139 struct pt_regs *regs = (struct pt_regs *) (((void *)fp)
142 if (regs->syscall_nr != -1) {
143 printk("%s-- trap0 -- syscall_nr: %ld", loglvl,
145 printk(KERN_CONT " psp: %lx elr: %lx\n",
146 pt_psp(regs), pt_elr(regs));
149 /* really want to see more ... */
150 kstack_depth_to_print += 6;
151 printk("%s-- %s (0x%lx) badva: %lx\n", loglvl,
152 ex_name(pt_cause(regs)), pt_cause(regs),
156 newfp = (unsigned long *) regs->r30;
162 /* If link reg is null, we are done. */
166 /* If newfp isn't larger, we're tracing garbage. */
174 void show_stack(struct task_struct *task, unsigned long *fp, const char *loglvl)
176 /* Saved link reg is one word above FP */
177 do_show_stack(task, fp, 0, loglvl);
180 int die(const char *str, struct pt_regs *regs, long err)
186 .lock = __SPIN_LOCK_UNLOCKED(die.lock),
193 spin_lock_irq(&die.lock);
195 printk(KERN_EMERG "Oops: %s[#%d]:\n", str, ++die.counter);
197 if (notify_die(DIE_OOPS, str, regs, err, pt_cause(regs), SIGSEGV) ==
203 do_show_stack(current, ®s->r30, pt_elr(regs), KERN_EMERG);
206 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
208 spin_unlock_irq(&die.lock);
211 panic("Fatal exception in interrupt");
214 panic("Fatal exception");
221 int die_if_kernel(char *str, struct pt_regs *regs, long err)
223 if (!user_mode(regs))
224 return die(str, regs, err);
230 * It's not clear that misaligned fetches are ever recoverable.
232 static void misaligned_instruction(struct pt_regs *regs)
234 die_if_kernel("Misaligned Instruction", regs, 0);
239 * Misaligned loads and stores, on the other hand, can be
240 * emulated, and probably should be, some day. But for now
241 * they will be considered fatal.
243 static void misaligned_data_load(struct pt_regs *regs)
245 die_if_kernel("Misaligned Data Load", regs, 0);
249 static void misaligned_data_store(struct pt_regs *regs)
251 die_if_kernel("Misaligned Data Store", regs, 0);
255 static void illegal_instruction(struct pt_regs *regs)
257 die_if_kernel("Illegal Instruction", regs, 0);
262 * Precise bus errors may be recoverable with a a retry,
263 * but for now, treat them as irrecoverable.
265 static void precise_bus_error(struct pt_regs *regs)
267 die_if_kernel("Precise Bus Error", regs, 0);
272 * If anything is to be done here other than panic,
273 * it will probably be complex and migrate to another
274 * source module. For now, just die.
276 static void cache_error(struct pt_regs *regs)
278 die("Cache Error", regs, 0);
282 * General exception handler
284 void do_genex(struct pt_regs *regs)
287 * Decode Cause and Dispatch
289 switch (pt_cause(regs)) {
292 execute_protection_fault(regs);
296 read_protection_fault(regs);
300 write_protection_fault(regs);
303 misaligned_instruction(regs);
306 illegal_instruction(regs);
309 misaligned_instruction(regs);
312 misaligned_data_load(regs);
315 misaligned_data_store(regs);
319 illegal_instruction(regs);
322 precise_bus_error(regs);
328 /* Halt and catch fire */
329 panic("Unrecognized exception 0x%lx\n", pt_cause(regs));
334 /* Indirect system call dispatch */
335 long sys_syscall(void)
337 printk(KERN_ERR "sys_syscall invoked!\n");
341 void do_trap0(struct pt_regs *regs)
345 switch (pt_cause(regs)) {
347 /* System call is trap0 #1 */
349 /* allow strace to catch syscall args */
350 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE) &&
351 tracehook_report_syscall_entry(regs)))
352 return; /* return -ENOSYS somewhere? */
354 /* Interrupts should be re-enabled for syscall processing */
355 __vmsetie(VM_INT_ENABLE);
358 * System call number is in r6, arguments in r0..r5.
359 * Fortunately, no Linux syscall has more than 6 arguments,
360 * and Hexagon ABI passes first 6 arguments in registers.
361 * 64-bit arguments are passed in odd/even register pairs.
362 * Fortunately, we have no system calls that take more
363 * than three arguments with more than one 64-bit value.
364 * Should that change, we'd need to redesign to copy
365 * between user and kernel stacks.
367 regs->syscall_nr = regs->r06;
370 * GPR R0 carries the first parameter, and is also used
371 * to report the return value. We need a backup of
372 * the user's value in case we need to do a late restart
373 * of the system call.
375 regs->restart_r0 = regs->r00;
377 if ((unsigned long) regs->syscall_nr >= __NR_syscalls) {
380 syscall = (syscall_fn)
381 (sys_call_table[regs->syscall_nr]);
382 regs->r00 = syscall(regs->r00, regs->r01,
383 regs->r02, regs->r03,
384 regs->r04, regs->r05);
387 /* allow strace to get the syscall return state */
388 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACE)))
389 tracehook_report_syscall_exit(regs, 0);
393 /* Trap0 0xdb is debug breakpoint */
394 if (user_mode(regs)) {
396 * Some architecures add some per-thread state
397 * to distinguish between breakpoint traps and
398 * trace traps. We may want to do that, and
399 * set the si_code value appropriately, or we
400 * may want to use a different trap0 flavor.
402 force_sig_fault(SIGTRAP, TRAP_BRKPT,
403 (void __user *) pt_elr(regs));
406 kgdb_handle_exception(pt_cause(regs), SIGTRAP,
412 /* Ignore other trap0 codes for now, especially 0 (Angel calls) */
416 * Machine check exception handler
418 void do_machcheck(struct pt_regs *regs)
420 /* Halt and catch fire */
425 * Treat this like the old 0xdb trap.
428 void do_debug_exception(struct pt_regs *regs)
430 regs->hvmer.vmest &= ~HVM_VMEST_CAUSE_MSK;
431 regs->hvmer.vmest |= (TRAP_DEBUG << HVM_VMEST_CAUSE_SFT);