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