Merge remote-tracking branch 'linus/master' into x86/urgent
[linux-2.6-block.git] / arch / tile / kernel / traps.c
CommitLineData
867e359b
CM
1/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/kprobes.h>
18#include <linux/module.h>
19#include <linux/reboot.h>
20#include <linux/uaccess.h>
21#include <linux/ptrace.h>
0707ad30
CM
22#include <asm/stack.h>
23#include <asm/traps.h>
867e359b
CM
24
25#include <arch/interrupts.h>
26#include <arch/spr_def.h>
eb7c792d 27#include <arch/opcode.h>
867e359b
CM
28
29void __init trap_init(void)
30{
31 /* Nothing needed here since we link code at .intrpt1 */
32}
33
34int unaligned_fixup = 1;
35
36static int __init setup_unaligned_fixup(char *str)
37{
38 /*
39 * Say "=-1" to completely disable it. If you just do "=0", we
40 * will still parse the instruction, then fire a SIGBUS with
41 * the correct address from inside the single_step code.
42 */
43 long val;
44 if (strict_strtol(str, 0, &val) != 0)
45 return 0;
46 unaligned_fixup = val;
0707ad30 47 pr_info("Fixups for unaligned data accesses are %s\n",
867e359b
CM
48 unaligned_fixup >= 0 ?
49 (unaligned_fixup ? "enabled" : "disabled") :
50 "completely disabled");
51 return 1;
52}
53__setup("unaligned_fixup=", setup_unaligned_fixup);
54
55#if CHIP_HAS_TILE_DMA()
56
57static int dma_disabled;
58
59static int __init nodma(char *str)
60{
0707ad30 61 pr_info("User-space DMA is disabled\n");
867e359b
CM
62 dma_disabled = 1;
63 return 1;
64}
65__setup("nodma", nodma);
66
67/* How to decode SPR_GPV_REASON */
68#define IRET_ERROR (1U << 31)
69#define MT_ERROR (1U << 30)
70#define MF_ERROR (1U << 29)
71#define SPR_INDEX ((1U << 15) - 1)
72#define SPR_MPL_SHIFT 9 /* starting bit position for MPL encoded in SPR */
73
74/*
75 * See if this GPV is just to notify the kernel of SPR use and we can
76 * retry the user instruction after adjusting some MPLs suitably.
77 */
78static int retry_gpv(unsigned int gpv_reason)
79{
80 int mpl;
81
82 if (gpv_reason & IRET_ERROR)
83 return 0;
84
85 BUG_ON((gpv_reason & (MT_ERROR|MF_ERROR)) == 0);
86 mpl = (gpv_reason & SPR_INDEX) >> SPR_MPL_SHIFT;
87 if (mpl == INT_DMA_NOTIFY && !dma_disabled) {
88 /* User is turning on DMA. Allow it and retry. */
89 printk(KERN_DEBUG "Process %d/%s is now enabled for DMA\n",
90 current->pid, current->comm);
91 BUG_ON(current->thread.tile_dma_state.enabled);
92 current->thread.tile_dma_state.enabled = 1;
93 grant_dma_mpls();
94 return 1;
95 }
96
97 return 0;
98}
99
100#endif /* CHIP_HAS_TILE_DMA() */
101
867e359b 102#ifdef __tilegx__
0707ad30 103#define bundle_bits tilegx_bundle_bits
867e359b 104#else
0707ad30 105#define bundle_bits tile_bundle_bits
867e359b
CM
106#endif
107
0707ad30
CM
108extern bundle_bits bpt_code;
109
110asm(".pushsection .rodata.bpt_code,\"a\";"
111 ".align 8;"
112 "bpt_code: bpt;"
113 ".size bpt_code,.-bpt_code;"
114 ".popsection");
115
116static int special_ill(bundle_bits bundle, int *sigp, int *codep)
117{
118 int sig, code, maxcode;
119
120 if (bundle == bpt_code) {
121 *sigp = SIGTRAP;
122 *codep = TRAP_BRKPT;
123 return 1;
124 }
125
126 /* If it's a "raise" bundle, then "ill" must be in pipe X1. */
127#ifdef __tilegx__
128 if ((bundle & TILEGX_BUNDLE_MODE_MASK) != 0)
129 return 0;
1fcbe027
CM
130 if (get_Opcode_X1(bundle) != RRR_0_OPCODE_X1)
131 return 0;
132 if (get_RRROpcodeExtension_X1(bundle) != UNARY_RRR_0_OPCODE_X1)
0707ad30
CM
133 return 0;
134 if (get_UnaryOpcodeExtension_X1(bundle) != ILL_UNARY_OPCODE_X1)
135 return 0;
136#else
eb7c792d 137 if (bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK)
0707ad30
CM
138 return 0;
139 if (get_Opcode_X1(bundle) != SHUN_0_OPCODE_X1)
140 return 0;
141 if (get_UnShOpcodeExtension_X1(bundle) != UN_0_SHUN_0_OPCODE_X1)
142 return 0;
143 if (get_UnOpcodeExtension_X1(bundle) != ILL_UN_0_SHUN_0_OPCODE_X1)
144 return 0;
145#endif
146
147 /* Check that the magic distinguishers are set to mean "raise". */
148 if (get_Dest_X1(bundle) != 29 || get_SrcA_X1(bundle) != 37)
149 return 0;
150
151 /* There must be an "addli zero, zero, VAL" in X0. */
152 if (get_Opcode_X0(bundle) != ADDLI_OPCODE_X0)
153 return 0;
154 if (get_Dest_X0(bundle) != TREG_ZERO)
155 return 0;
156 if (get_SrcA_X0(bundle) != TREG_ZERO)
157 return 0;
158
159 /*
160 * Validate the proposed signal number and si_code value.
161 * Note that we embed these in the static instruction itself
162 * so that we perturb the register state as little as possible
163 * at the time of the actual fault; it's unlikely you'd ever
164 * need to dynamically choose which kind of fault to raise
165 * from user space.
166 */
167 sig = get_Imm16_X0(bundle) & 0x3f;
168 switch (sig) {
169 case SIGILL:
170 maxcode = NSIGILL;
171 break;
172 case SIGFPE:
173 maxcode = NSIGFPE;
174 break;
175 case SIGSEGV:
176 maxcode = NSIGSEGV;
177 break;
178 case SIGBUS:
179 maxcode = NSIGBUS;
180 break;
181 case SIGTRAP:
182 maxcode = NSIGTRAP;
183 break;
184 default:
185 return 0;
186 }
187 code = (get_Imm16_X0(bundle) >> 6) & 0xf;
188 if (code <= 0 || code > maxcode)
189 return 0;
190
191 /* Make it the requested signal. */
192 *sigp = sig;
193 *codep = code | __SI_FAULT;
194 return 1;
195}
196
867e359b
CM
197void __kprobes do_trap(struct pt_regs *regs, int fault_num,
198 unsigned long reason)
199{
200 siginfo_t info = { 0 };
201 int signo, code;
202 unsigned long address;
0707ad30 203 bundle_bits instr;
867e359b
CM
204
205 /* Re-enable interrupts. */
206 local_irq_enable();
207
208 /*
209 * If it hits in kernel mode and we can't fix it up, just exit the
210 * current process and hope for the best.
211 */
212 if (!user_mode(regs)) {
213 if (fixup_exception(regs)) /* only UNALIGN_DATA in practice */
214 return;
0707ad30 215 pr_alert("Kernel took bad trap %d at PC %#lx\n",
867e359b
CM
216 fault_num, regs->pc);
217 if (fault_num == INT_GPV)
0707ad30 218 pr_alert("GPV_REASON is %#lx\n", reason);
867e359b
CM
219 show_regs(regs);
220 do_exit(SIGKILL); /* FIXME: implement i386 die() */
221 return;
222 }
223
224 switch (fault_num) {
225 case INT_ILL:
0707ad30
CM
226 if (copy_from_user(&instr, (void __user *)regs->pc,
227 sizeof(instr))) {
228 pr_err("Unreadable instruction for INT_ILL:"
867e359b
CM
229 " %#lx\n", regs->pc);
230 do_exit(SIGKILL);
231 return;
232 }
0707ad30 233 if (!special_ill(instr, &signo, &code)) {
867e359b
CM
234 signo = SIGILL;
235 code = ILL_ILLOPC;
236 }
237 address = regs->pc;
238 break;
239 case INT_GPV:
240#if CHIP_HAS_TILE_DMA()
241 if (retry_gpv(reason))
242 return;
243#endif
244 /*FALLTHROUGH*/
245 case INT_UDN_ACCESS:
246 case INT_IDN_ACCESS:
247#if CHIP_HAS_SN()
248 case INT_SN_ACCESS:
249#endif
250 signo = SIGILL;
251 code = ILL_PRVREG;
252 address = regs->pc;
253 break;
254 case INT_SWINT_3:
255 case INT_SWINT_2:
256 case INT_SWINT_0:
257 signo = SIGILL;
258 code = ILL_ILLTRP;
259 address = regs->pc;
260 break;
261 case INT_UNALIGN_DATA:
233325b9 262#ifndef __tilegx__ /* Emulated support for single step debugging */
867e359b
CM
263 if (unaligned_fixup >= 0) {
264 struct single_step_state *state =
265 current_thread_info()->step_state;
0707ad30
CM
266 if (!state ||
267 (void __user *)(regs->pc) != state->buffer) {
867e359b
CM
268 single_step_once(regs);
269 return;
270 }
271 }
272#endif
273 signo = SIGBUS;
274 code = BUS_ADRALN;
275 address = 0;
276 break;
277 case INT_DOUBLE_FAULT:
278 /*
279 * For double fault, "reason" is actually passed as
a78c942d 280 * SYSTEM_SAVE_K_2, the hypervisor's double-fault info, so
867e359b
CM
281 * we can provide the original fault number rather than
282 * the uninteresting "INT_DOUBLE_FAULT" so the user can
283 * learn what actually struck while PL0 ICS was set.
284 */
285 fault_num = reason;
286 signo = SIGILL;
287 code = ILL_DBLFLT;
288 address = regs->pc;
289 break;
290#ifdef __tilegx__
291 case INT_ILL_TRANS:
292 signo = SIGSEGV;
293 code = SEGV_MAPERR;
294 if (reason & SPR_ILL_TRANS_REASON__I_STREAM_VA_RMASK)
295 address = regs->pc;
296 else
297 address = 0; /* FIXME: GX: single-step for address */
298 break;
299#endif
300 default:
301 panic("Unexpected do_trap interrupt number %d", fault_num);
302 return;
303 }
304
305 info.si_signo = signo;
306 info.si_code = code;
0707ad30 307 info.si_addr = (void __user *)address;
867e359b
CM
308 if (signo == SIGILL)
309 info.si_trapno = fault_num;
571d76ac 310 trace_unhandled_signal("trap", regs, address, signo);
867e359b
CM
311 force_sig_info(signo, &info, current);
312}
313
867e359b
CM
314void kernel_double_fault(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
315{
316 _dump_stack(dummy, pc, lr, sp, r52);
0707ad30 317 pr_emerg("Double fault: exiting\n");
867e359b
CM
318 machine_halt();
319}