blob: a937365b3429824e6947cc011b0bc383b0b521aa [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
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>
Tony Lu3fa17c32013-08-09 15:08:57 -040018#include <linux/kdebug.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040019#include <linux/module.h>
20#include <linux/reboot.h>
21#include <linux/uaccess.h>
22#include <linux/ptrace.h>
Chris Metcalf0707ad32010-06-25 17:04:17 -040023#include <asm/stack.h>
24#include <asm/traps.h>
David Howellsbd119c692012-03-28 18:30:03 +010025#include <asm/setup.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040026
27#include <arch/interrupts.h>
28#include <arch/spr_def.h>
Chris Metcalfeb7c7922011-11-02 23:02:17 -040029#include <arch/opcode.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040030
31void __init trap_init(void)
32{
Chris Metcalfacbde1d2013-09-03 14:41:36 -040033 /* Nothing needed here since we link code at .intrpt */
Chris Metcalf867e3592010-05-28 23:09:12 -040034}
35
36int unaligned_fixup = 1;
37
38static int __init setup_unaligned_fixup(char *str)
39{
40 /*
41 * Say "=-1" to completely disable it. If you just do "=0", we
42 * will still parse the instruction, then fire a SIGBUS with
43 * the correct address from inside the single_step code.
44 */
45 long val;
46 if (strict_strtol(str, 0, &val) != 0)
47 return 0;
48 unaligned_fixup = val;
Chris Metcalf0707ad32010-06-25 17:04:17 -040049 pr_info("Fixups for unaligned data accesses are %s\n",
Chris Metcalf867e3592010-05-28 23:09:12 -040050 unaligned_fixup >= 0 ?
51 (unaligned_fixup ? "enabled" : "disabled") :
52 "completely disabled");
53 return 1;
54}
55__setup("unaligned_fixup=", setup_unaligned_fixup);
56
57#if CHIP_HAS_TILE_DMA()
58
59static int dma_disabled;
60
61static int __init nodma(char *str)
62{
Chris Metcalf0707ad32010-06-25 17:04:17 -040063 pr_info("User-space DMA is disabled\n");
Chris Metcalf867e3592010-05-28 23:09:12 -040064 dma_disabled = 1;
65 return 1;
66}
67__setup("nodma", nodma);
68
69/* How to decode SPR_GPV_REASON */
70#define IRET_ERROR (1U << 31)
71#define MT_ERROR (1U << 30)
72#define MF_ERROR (1U << 29)
73#define SPR_INDEX ((1U << 15) - 1)
74#define SPR_MPL_SHIFT 9 /* starting bit position for MPL encoded in SPR */
75
76/*
77 * See if this GPV is just to notify the kernel of SPR use and we can
78 * retry the user instruction after adjusting some MPLs suitably.
79 */
80static int retry_gpv(unsigned int gpv_reason)
81{
82 int mpl;
83
84 if (gpv_reason & IRET_ERROR)
85 return 0;
86
87 BUG_ON((gpv_reason & (MT_ERROR|MF_ERROR)) == 0);
88 mpl = (gpv_reason & SPR_INDEX) >> SPR_MPL_SHIFT;
89 if (mpl == INT_DMA_NOTIFY && !dma_disabled) {
90 /* User is turning on DMA. Allow it and retry. */
91 printk(KERN_DEBUG "Process %d/%s is now enabled for DMA\n",
92 current->pid, current->comm);
93 BUG_ON(current->thread.tile_dma_state.enabled);
94 current->thread.tile_dma_state.enabled = 1;
95 grant_dma_mpls();
96 return 1;
97 }
98
99 return 0;
100}
101
102#endif /* CHIP_HAS_TILE_DMA() */
103
Chris Metcalf867e3592010-05-28 23:09:12 -0400104#ifdef __tilegx__
Chris Metcalf0707ad32010-06-25 17:04:17 -0400105#define bundle_bits tilegx_bundle_bits
Chris Metcalf867e3592010-05-28 23:09:12 -0400106#else
Chris Metcalf0707ad32010-06-25 17:04:17 -0400107#define bundle_bits tile_bundle_bits
Chris Metcalf867e3592010-05-28 23:09:12 -0400108#endif
109
Chris Metcalf0707ad32010-06-25 17:04:17 -0400110extern bundle_bits bpt_code;
111
112asm(".pushsection .rodata.bpt_code,\"a\";"
113 ".align 8;"
114 "bpt_code: bpt;"
115 ".size bpt_code,.-bpt_code;"
116 ".popsection");
117
118static int special_ill(bundle_bits bundle, int *sigp, int *codep)
119{
120 int sig, code, maxcode;
121
122 if (bundle == bpt_code) {
123 *sigp = SIGTRAP;
124 *codep = TRAP_BRKPT;
125 return 1;
126 }
127
128 /* If it's a "raise" bundle, then "ill" must be in pipe X1. */
129#ifdef __tilegx__
130 if ((bundle & TILEGX_BUNDLE_MODE_MASK) != 0)
131 return 0;
Chris Metcalf1fcbe022010-08-13 08:40:57 -0400132 if (get_Opcode_X1(bundle) != RRR_0_OPCODE_X1)
133 return 0;
134 if (get_RRROpcodeExtension_X1(bundle) != UNARY_RRR_0_OPCODE_X1)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400135 return 0;
136 if (get_UnaryOpcodeExtension_X1(bundle) != ILL_UNARY_OPCODE_X1)
137 return 0;
138#else
Chris Metcalfeb7c7922011-11-02 23:02:17 -0400139 if (bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400140 return 0;
141 if (get_Opcode_X1(bundle) != SHUN_0_OPCODE_X1)
142 return 0;
143 if (get_UnShOpcodeExtension_X1(bundle) != UN_0_SHUN_0_OPCODE_X1)
144 return 0;
145 if (get_UnOpcodeExtension_X1(bundle) != ILL_UN_0_SHUN_0_OPCODE_X1)
146 return 0;
147#endif
148
149 /* Check that the magic distinguishers are set to mean "raise". */
150 if (get_Dest_X1(bundle) != 29 || get_SrcA_X1(bundle) != 37)
151 return 0;
152
153 /* There must be an "addli zero, zero, VAL" in X0. */
154 if (get_Opcode_X0(bundle) != ADDLI_OPCODE_X0)
155 return 0;
156 if (get_Dest_X0(bundle) != TREG_ZERO)
157 return 0;
158 if (get_SrcA_X0(bundle) != TREG_ZERO)
159 return 0;
160
161 /*
162 * Validate the proposed signal number and si_code value.
163 * Note that we embed these in the static instruction itself
164 * so that we perturb the register state as little as possible
165 * at the time of the actual fault; it's unlikely you'd ever
166 * need to dynamically choose which kind of fault to raise
167 * from user space.
168 */
169 sig = get_Imm16_X0(bundle) & 0x3f;
170 switch (sig) {
171 case SIGILL:
172 maxcode = NSIGILL;
173 break;
174 case SIGFPE:
175 maxcode = NSIGFPE;
176 break;
177 case SIGSEGV:
178 maxcode = NSIGSEGV;
179 break;
180 case SIGBUS:
181 maxcode = NSIGBUS;
182 break;
183 case SIGTRAP:
184 maxcode = NSIGTRAP;
185 break;
186 default:
187 return 0;
188 }
189 code = (get_Imm16_X0(bundle) >> 6) & 0xf;
190 if (code <= 0 || code > maxcode)
191 return 0;
192
193 /* Make it the requested signal. */
194 *sigp = sig;
195 *codep = code | __SI_FAULT;
196 return 1;
197}
198
Chris Metcalfc6f696f2012-03-30 16:31:08 -0400199static const char *const int_name[] = {
200 [INT_MEM_ERROR] = "Memory error",
201 [INT_ILL] = "Illegal instruction",
202 [INT_GPV] = "General protection violation",
203 [INT_UDN_ACCESS] = "UDN access",
204 [INT_IDN_ACCESS] = "IDN access",
205#if CHIP_HAS_SN()
206 [INT_SN_ACCESS] = "SN access",
207#endif
208 [INT_SWINT_3] = "Software interrupt 3",
209 [INT_SWINT_2] = "Software interrupt 2",
210 [INT_SWINT_0] = "Software interrupt 0",
211 [INT_UNALIGN_DATA] = "Unaligned data",
212 [INT_DOUBLE_FAULT] = "Double fault",
213#ifdef __tilegx__
214 [INT_ILL_TRANS] = "Illegal virtual address",
215#endif
216};
217
Tony Lu3fa17c32013-08-09 15:08:57 -0400218static int do_bpt(struct pt_regs *regs)
219{
220 unsigned long bundle, bcode, bpt;
221
222 bundle = *(unsigned long *)instruction_pointer(regs);
223
224 /*
225 * bpt shoule be { bpt; nop }, which is 0x286a44ae51485000ULL.
226 * we encode the unused least significant bits for other purpose.
227 */
228 bpt = bundle & ~((1ULL << 12) - 1);
229 if (bpt != TILE_BPT_BUNDLE)
230 return 0;
231
232 bcode = bundle & ((1ULL << 12) - 1);
233 /*
234 * notify the kprobe handlers, if instruction is likely to
235 * pertain to them.
236 */
237 switch (bcode) {
238 /* breakpoint_insn */
239 case 0:
240 notify_die(DIE_BREAK, "debug", regs, bundle,
241 INT_ILL, SIGTRAP);
242 break;
Chris Metcalf81571072013-08-28 19:53:17 -0400243 /* compiled_bpt */
244 case DIE_COMPILED_BPT:
245 notify_die(DIE_COMPILED_BPT, "debug", regs, bundle,
246 INT_ILL, SIGTRAP);
247 break;
Tony Lu3fa17c32013-08-09 15:08:57 -0400248 /* breakpoint2_insn */
249 case DIE_SSTEPBP:
250 notify_die(DIE_SSTEPBP, "single_step", regs, bundle,
251 INT_ILL, SIGTRAP);
252 break;
253 default:
254 return 0;
255 }
256
257 return 1;
258}
259
Chris Metcalf867e3592010-05-28 23:09:12 -0400260void __kprobes do_trap(struct pt_regs *regs, int fault_num,
261 unsigned long reason)
262{
263 siginfo_t info = { 0 };
264 int signo, code;
Chris Metcalfa714fff2012-03-29 15:23:54 -0400265 unsigned long address = 0;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400266 bundle_bits instr;
Tony Lu3fa17c32013-08-09 15:08:57 -0400267 int is_kernel = !user_mode(regs);
268
269 /* Handle breakpoints, etc. */
270 if (is_kernel && fault_num == INT_ILL && do_bpt(regs))
271 return;
Chris Metcalf867e3592010-05-28 23:09:12 -0400272
Chris Metcalf70d2b592013-08-07 12:11:56 -0400273 /* Re-enable interrupts, if they were previously enabled. */
274 if (!(regs->flags & PT_FLAGS_DISABLE_IRQ))
275 local_irq_enable();
Chris Metcalf867e3592010-05-28 23:09:12 -0400276
277 /*
278 * If it hits in kernel mode and we can't fix it up, just exit the
279 * current process and hope for the best.
280 */
Tony Lu3fa17c32013-08-09 15:08:57 -0400281 if (is_kernel) {
Chris Metcalfc6f696f2012-03-30 16:31:08 -0400282 const char *name;
Chris Metcalf70d2b592013-08-07 12:11:56 -0400283 char buf[100];
284 if (fixup_exception(regs)) /* ILL_TRANS or UNALIGN_DATA */
Chris Metcalf867e3592010-05-28 23:09:12 -0400285 return;
Chris Metcalfc6f696f2012-03-30 16:31:08 -0400286 if (fault_num >= 0 &&
287 fault_num < sizeof(int_name)/sizeof(int_name[0]) &&
288 int_name[fault_num] != NULL)
289 name = int_name[fault_num];
290 else
291 name = "Unknown interrupt";
Chris Metcalf867e3592010-05-28 23:09:12 -0400292 if (fault_num == INT_GPV)
Chris Metcalf70d2b592013-08-07 12:11:56 -0400293 snprintf(buf, sizeof(buf), "; GPV_REASON %#lx", reason);
294#ifdef __tilegx__
295 else if (fault_num == INT_ILL_TRANS)
296 snprintf(buf, sizeof(buf), "; address %#lx", reason);
297#endif
298 else
299 buf[0] = '\0';
300 pr_alert("Kernel took bad trap %d (%s) at PC %#lx%s\n",
301 fault_num, name, regs->pc, buf);
Chris Metcalf867e3592010-05-28 23:09:12 -0400302 show_regs(regs);
303 do_exit(SIGKILL); /* FIXME: implement i386 die() */
304 return;
305 }
306
307 switch (fault_num) {
Chris Metcalfa714fff2012-03-29 15:23:54 -0400308 case INT_MEM_ERROR:
309 signo = SIGBUS;
310 code = BUS_OBJERR;
311 break;
Chris Metcalf867e3592010-05-28 23:09:12 -0400312 case INT_ILL:
Chris Metcalf0707ad32010-06-25 17:04:17 -0400313 if (copy_from_user(&instr, (void __user *)regs->pc,
314 sizeof(instr))) {
315 pr_err("Unreadable instruction for INT_ILL:"
Chris Metcalf867e3592010-05-28 23:09:12 -0400316 " %#lx\n", regs->pc);
317 do_exit(SIGKILL);
318 return;
319 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400320 if (!special_ill(instr, &signo, &code)) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400321 signo = SIGILL;
322 code = ILL_ILLOPC;
323 }
324 address = regs->pc;
325 break;
326 case INT_GPV:
327#if CHIP_HAS_TILE_DMA()
328 if (retry_gpv(reason))
329 return;
330#endif
331 /*FALLTHROUGH*/
332 case INT_UDN_ACCESS:
333 case INT_IDN_ACCESS:
334#if CHIP_HAS_SN()
335 case INT_SN_ACCESS:
336#endif
337 signo = SIGILL;
338 code = ILL_PRVREG;
339 address = regs->pc;
340 break;
341 case INT_SWINT_3:
342 case INT_SWINT_2:
343 case INT_SWINT_0:
344 signo = SIGILL;
345 code = ILL_ILLTRP;
346 address = regs->pc;
347 break;
348 case INT_UNALIGN_DATA:
Chris Metcalf233325b2010-10-14 16:32:41 -0400349#ifndef __tilegx__ /* Emulated support for single step debugging */
Chris Metcalf867e3592010-05-28 23:09:12 -0400350 if (unaligned_fixup >= 0) {
351 struct single_step_state *state =
352 current_thread_info()->step_state;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400353 if (!state ||
354 (void __user *)(regs->pc) != state->buffer) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400355 single_step_once(regs);
356 return;
357 }
358 }
359#endif
360 signo = SIGBUS;
361 code = BUS_ADRALN;
362 address = 0;
363 break;
364 case INT_DOUBLE_FAULT:
365 /*
366 * For double fault, "reason" is actually passed as
Chris Metcalfa78c9422010-10-14 16:23:03 -0400367 * SYSTEM_SAVE_K_2, the hypervisor's double-fault info, so
Chris Metcalf867e3592010-05-28 23:09:12 -0400368 * we can provide the original fault number rather than
369 * the uninteresting "INT_DOUBLE_FAULT" so the user can
370 * learn what actually struck while PL0 ICS was set.
371 */
372 fault_num = reason;
373 signo = SIGILL;
374 code = ILL_DBLFLT;
375 address = regs->pc;
376 break;
377#ifdef __tilegx__
Chris Metcalfe1723532012-03-29 14:52:00 -0400378 case INT_ILL_TRANS: {
379 /* Avoid a hardware erratum with the return address stack. */
380 fill_ra_stack();
381
Chris Metcalf867e3592010-05-28 23:09:12 -0400382 signo = SIGSEGV;
Chris Metcalf70d2b592013-08-07 12:11:56 -0400383 address = reason;
Chris Metcalf867e3592010-05-28 23:09:12 -0400384 code = SEGV_MAPERR;
Chris Metcalf867e3592010-05-28 23:09:12 -0400385 break;
Chris Metcalfe1723532012-03-29 14:52:00 -0400386 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400387#endif
388 default:
389 panic("Unexpected do_trap interrupt number %d", fault_num);
390 return;
391 }
392
393 info.si_signo = signo;
394 info.si_code = code;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400395 info.si_addr = (void __user *)address;
Chris Metcalf867e3592010-05-28 23:09:12 -0400396 if (signo == SIGILL)
397 info.si_trapno = fault_num;
Chris Metcalfa714fff2012-03-29 15:23:54 -0400398 if (signo != SIGTRAP)
399 trace_unhandled_signal("trap", regs, address, signo);
Chris Metcalf867e3592010-05-28 23:09:12 -0400400 force_sig_info(signo, &info, current);
401}
402
Chris Metcalf867e3592010-05-28 23:09:12 -0400403void kernel_double_fault(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
404{
405 _dump_stack(dummy, pc, lr, sp, r52);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400406 pr_emerg("Double fault: exiting\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400407 machine_halt();
408}