xref: /openbmc/linux/arch/arc/kernel/traps.c (revision f35e839a)
1 /*
2  * Traps/Non-MMU Exception handling for ARC
3  *
4  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
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 as
8  * published by the Free Software Foundation.
9  *
10  * vineetg: May 2011
11  *  -user-space unaligned access emulation
12  *
13  * Rahul Trivedi: Codito Technologies 2004
14  */
15 
16 #include <linux/sched.h>
17 #include <linux/kdebug.h>
18 #include <linux/uaccess.h>
19 #include <linux/ptrace.h>
20 #include <linux/kprobes.h>
21 #include <linux/kgdb.h>
22 #include <asm/setup.h>
23 #include <asm/unaligned.h>
24 #include <asm/kprobes.h>
25 
26 void __init trap_init(void)
27 {
28 	return;
29 }
30 
31 void die(const char *str, struct pt_regs *regs, unsigned long address,
32 	 unsigned long cause_reg)
33 {
34 	show_kernel_fault_diag(str, regs, address, cause_reg);
35 
36 	/* DEAD END */
37 	__asm__("flag 1");
38 }
39 
40 /*
41  * Helper called for bulk of exceptions NOT needing specific handling
42  *  -for user faults enqueues requested signal
43  *  -for kernel, chk if due to copy_(to|from)_user, otherwise die()
44  */
45 static noinline int handle_exception(unsigned long cause, char *str,
46 				     struct pt_regs *regs, siginfo_t *info)
47 {
48 	if (user_mode(regs)) {
49 		struct task_struct *tsk = current;
50 
51 		tsk->thread.fault_address = (__force unsigned int)info->si_addr;
52 		tsk->thread.cause_code = cause;
53 
54 		force_sig_info(info->si_signo, info, tsk);
55 
56 	} else {
57 		/* If not due to copy_(to|from)_user, we are doomed */
58 		if (fixup_exception(regs))
59 			return 0;
60 
61 		die(str, regs, (unsigned long)info->si_addr, cause);
62 	}
63 
64 	return 1;
65 }
66 
67 #define DO_ERROR_INFO(signr, str, name, sicode) \
68 int name(unsigned long cause, unsigned long address, struct pt_regs *regs) \
69 {						\
70 	siginfo_t info = {			\
71 		.si_signo = signr,		\
72 		.si_errno = 0,			\
73 		.si_code  = sicode,		\
74 		.si_addr = (void __user *)address,	\
75 	};					\
76 	return handle_exception(cause, str, regs, &info);\
77 }
78 
79 /*
80  * Entry points for exceptions NOT needing specific handling
81  */
82 DO_ERROR_INFO(SIGILL, "Priv Op/Disabled Extn", do_privilege_fault, ILL_PRVOPC)
83 DO_ERROR_INFO(SIGILL, "Invalid Extn Insn", do_extension_fault, ILL_ILLOPC)
84 DO_ERROR_INFO(SIGILL, "Illegal Insn (or Seq)", insterror_is_error, ILL_ILLOPC)
85 DO_ERROR_INFO(SIGBUS, "Invalid Mem Access", do_memory_error, BUS_ADRERR)
86 DO_ERROR_INFO(SIGTRAP, "Breakpoint Set", trap_is_brkpt, TRAP_BRKPT)
87 DO_ERROR_INFO(SIGBUS, "Misaligned Access", do_misaligned_error, BUS_ADRALN)
88 
89 #ifdef CONFIG_ARC_MISALIGN_ACCESS
90 /*
91  * Entry Point for Misaligned Data access Exception, for emulating in software
92  */
93 int do_misaligned_access(unsigned long cause, unsigned long address,
94 			 struct pt_regs *regs, struct callee_regs *cregs)
95 {
96 	if (misaligned_fixup(address, regs, cause, cregs) != 0)
97 		return do_misaligned_error(cause, address, regs);
98 
99 	return 0;
100 }
101 #endif
102 
103 /*
104  * Entry point for miscll errors such as Nested Exceptions
105  *  -Duplicate TLB entry is handled seperately though
106  */
107 void do_machine_check_fault(unsigned long cause, unsigned long address,
108 			    struct pt_regs *regs)
109 {
110 	die("Machine Check Exception", regs, address, cause);
111 }
112 
113 
114 /*
115  * Entry point for traps induced by ARCompact TRAP_S <n> insn
116  * This is same family as TRAP0/SWI insn (use the same vector).
117  * The only difference being SWI insn take no operand, while TRAP_S does
118  * which reflects in ECR Reg as 8 bit param.
119  * Thus TRAP_S <n> can be used for specific purpose
120  *  -1 used for software breakpointing (gdb)
121  *  -2 used by kprobes
122  */
123 void do_non_swi_trap(unsigned long cause, unsigned long address,
124 			struct pt_regs *regs)
125 {
126 	unsigned int param = cause & 0xff;
127 
128 	switch (param) {
129 	case 1:
130 		trap_is_brkpt(cause, address, regs);
131 		break;
132 
133 	case 2:
134 		trap_is_kprobe(param, address, regs);
135 		break;
136 
137 	case 3:
138 	case 4:
139 		kgdb_trap(regs, param);
140 		break;
141 
142 	default:
143 		break;
144 	}
145 }
146 
147 /*
148  * Entry point for Instruction Error Exception
149  *  -For a corner case, ARC kprobes implementation resorts to using
150  *   this exception, hence the check
151  */
152 void do_insterror_or_kprobe(unsigned long cause,
153 				       unsigned long address,
154 				       struct pt_regs *regs)
155 {
156 	/* Check if this exception is caused by kprobes */
157 	if (notify_die(DIE_IERR, "kprobe_ierr", regs, address,
158 		       cause, SIGILL) == NOTIFY_STOP)
159 		return;
160 
161 	insterror_is_error(cause, address, regs);
162 }
163