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