x86/oprofile: Fix bogus GCC-8 warning in nmi_setup()
[cris-mirror.git] / arch / tile / kernel / traps.c
blob83a7186198d7838ca4ce9b0012cd201b597b43bb
1 /*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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.
15 #include <linux/sched.h>
16 #include <linux/sched/debug.h>
17 #include <linux/kernel.h>
18 #include <linux/kprobes.h>
19 #include <linux/kdebug.h>
20 #include <linux/module.h>
21 #include <linux/reboot.h>
22 #include <linux/uaccess.h>
23 #include <linux/ptrace.h>
24 #include <linux/hardirq.h>
25 #include <linux/nmi.h>
26 #include <asm/stack.h>
27 #include <asm/traps.h>
28 #include <asm/setup.h>
30 #include <arch/interrupts.h>
31 #include <arch/spr_def.h>
32 #include <arch/opcode.h>
34 void __init trap_init(void)
36 /* Nothing needed here since we link code at .intrpt */
39 int unaligned_fixup = 1;
41 static int __init setup_unaligned_fixup(char *str)
44 * Say "=-1" to completely disable it. If you just do "=0", we
45 * will still parse the instruction, then fire a SIGBUS with
46 * the correct address from inside the single_step code.
48 if (kstrtoint(str, 0, &unaligned_fixup) != 0)
49 return 0;
51 pr_info("Fixups for unaligned data accesses are %s\n",
52 unaligned_fixup >= 0 ?
53 (unaligned_fixup ? "enabled" : "disabled") :
54 "completely disabled");
55 return 1;
57 __setup("unaligned_fixup=", setup_unaligned_fixup);
59 #if CHIP_HAS_TILE_DMA()
61 static int dma_disabled;
63 static int __init nodma(char *str)
65 pr_info("User-space DMA is disabled\n");
66 dma_disabled = 1;
67 return 1;
69 __setup("nodma", nodma);
71 /* How to decode SPR_GPV_REASON */
72 #define IRET_ERROR (1U << 31)
73 #define MT_ERROR (1U << 30)
74 #define MF_ERROR (1U << 29)
75 #define SPR_INDEX ((1U << 15) - 1)
76 #define SPR_MPL_SHIFT 9 /* starting bit position for MPL encoded in SPR */
79 * See if this GPV is just to notify the kernel of SPR use and we can
80 * retry the user instruction after adjusting some MPLs suitably.
82 static int retry_gpv(unsigned int gpv_reason)
84 int mpl;
86 if (gpv_reason & IRET_ERROR)
87 return 0;
89 BUG_ON((gpv_reason & (MT_ERROR|MF_ERROR)) == 0);
90 mpl = (gpv_reason & SPR_INDEX) >> SPR_MPL_SHIFT;
91 if (mpl == INT_DMA_NOTIFY && !dma_disabled) {
92 /* User is turning on DMA. Allow it and retry. */
93 printk(KERN_DEBUG "Process %d/%s is now enabled for DMA\n",
94 current->pid, current->comm);
95 BUG_ON(current->thread.tile_dma_state.enabled);
96 current->thread.tile_dma_state.enabled = 1;
97 grant_dma_mpls();
98 return 1;
101 return 0;
104 #endif /* CHIP_HAS_TILE_DMA() */
106 extern tile_bundle_bits bpt_code;
108 asm(".pushsection .rodata.bpt_code,\"a\";"
109 ".align 8;"
110 "bpt_code: bpt;"
111 ".size bpt_code,.-bpt_code;"
112 ".popsection");
114 static int special_ill(tile_bundle_bits bundle, int *sigp, int *codep)
116 int sig, code, maxcode;
118 if (bundle == bpt_code) {
119 *sigp = SIGTRAP;
120 *codep = TRAP_BRKPT;
121 return 1;
124 /* If it's a "raise" bundle, then "ill" must be in pipe X1. */
125 #ifdef __tilegx__
126 if ((bundle & TILEGX_BUNDLE_MODE_MASK) != 0)
127 return 0;
128 if (get_Opcode_X1(bundle) != RRR_0_OPCODE_X1)
129 return 0;
130 if (get_RRROpcodeExtension_X1(bundle) != UNARY_RRR_0_OPCODE_X1)
131 return 0;
132 if (get_UnaryOpcodeExtension_X1(bundle) != ILL_UNARY_OPCODE_X1)
133 return 0;
134 #else
135 if (bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK)
136 return 0;
137 if (get_Opcode_X1(bundle) != SHUN_0_OPCODE_X1)
138 return 0;
139 if (get_UnShOpcodeExtension_X1(bundle) != UN_0_SHUN_0_OPCODE_X1)
140 return 0;
141 if (get_UnOpcodeExtension_X1(bundle) != ILL_UN_0_SHUN_0_OPCODE_X1)
142 return 0;
143 #endif
145 /* Check that the magic distinguishers are set to mean "raise". */
146 if (get_Dest_X1(bundle) != 29 || get_SrcA_X1(bundle) != 37)
147 return 0;
149 /* There must be an "addli zero, zero, VAL" in X0. */
150 if (get_Opcode_X0(bundle) != ADDLI_OPCODE_X0)
151 return 0;
152 if (get_Dest_X0(bundle) != TREG_ZERO)
153 return 0;
154 if (get_SrcA_X0(bundle) != TREG_ZERO)
155 return 0;
158 * Validate the proposed signal number and si_code value.
159 * Note that we embed these in the static instruction itself
160 * so that we perturb the register state as little as possible
161 * at the time of the actual fault; it's unlikely you'd ever
162 * need to dynamically choose which kind of fault to raise
163 * from user space.
165 sig = get_Imm16_X0(bundle) & 0x3f;
166 switch (sig) {
167 case SIGILL:
168 maxcode = NSIGILL;
169 break;
170 case SIGFPE:
171 maxcode = NSIGFPE;
172 break;
173 case SIGSEGV:
174 maxcode = NSIGSEGV;
175 break;
176 case SIGBUS:
177 maxcode = NSIGBUS;
178 break;
179 case SIGTRAP:
180 maxcode = NSIGTRAP;
181 break;
182 default:
183 return 0;
185 code = (get_Imm16_X0(bundle) >> 6) & 0xf;
186 if (code <= 0 || code > maxcode)
187 return 0;
189 /* Make it the requested signal. */
190 *sigp = sig;
191 *codep = code;
192 return 1;
195 static const char *const int_name[] = {
196 [INT_MEM_ERROR] = "Memory error",
197 [INT_ILL] = "Illegal instruction",
198 [INT_GPV] = "General protection violation",
199 [INT_UDN_ACCESS] = "UDN access",
200 [INT_IDN_ACCESS] = "IDN access",
201 #if CHIP_HAS_SN()
202 [INT_SN_ACCESS] = "SN access",
203 #endif
204 [INT_SWINT_3] = "Software interrupt 3",
205 [INT_SWINT_2] = "Software interrupt 2",
206 [INT_SWINT_0] = "Software interrupt 0",
207 [INT_UNALIGN_DATA] = "Unaligned data",
208 [INT_DOUBLE_FAULT] = "Double fault",
209 #ifdef __tilegx__
210 [INT_ILL_TRANS] = "Illegal virtual address",
211 #endif
214 static int do_bpt(struct pt_regs *regs)
216 unsigned long bundle, bcode, bpt;
218 bundle = *(unsigned long *)instruction_pointer(regs);
221 * bpt shoule be { bpt; nop }, which is 0x286a44ae51485000ULL.
222 * we encode the unused least significant bits for other purpose.
224 bpt = bundle & ~((1ULL << 12) - 1);
225 if (bpt != TILE_BPT_BUNDLE)
226 return 0;
228 bcode = bundle & ((1ULL << 12) - 1);
230 * notify the kprobe handlers, if instruction is likely to
231 * pertain to them.
233 switch (bcode) {
234 /* breakpoint_insn */
235 case 0:
236 notify_die(DIE_BREAK, "debug", regs, bundle,
237 INT_ILL, SIGTRAP);
238 break;
239 /* compiled_bpt */
240 case DIE_COMPILED_BPT:
241 notify_die(DIE_COMPILED_BPT, "debug", regs, bundle,
242 INT_ILL, SIGTRAP);
243 break;
244 /* breakpoint2_insn */
245 case DIE_SSTEPBP:
246 notify_die(DIE_SSTEPBP, "single_step", regs, bundle,
247 INT_ILL, SIGTRAP);
248 break;
249 default:
250 return 0;
253 return 1;
256 void __kprobes do_trap(struct pt_regs *regs, int fault_num,
257 unsigned long reason)
259 siginfo_t info;
260 int signo, code;
261 unsigned long address = 0;
262 tile_bundle_bits instr;
263 int is_kernel = !user_mode(regs);
265 clear_siginfo(&info);
267 /* Handle breakpoints, etc. */
268 if (is_kernel && fault_num == INT_ILL && do_bpt(regs))
269 return;
271 /* Re-enable interrupts, if they were previously enabled. */
272 if (!(regs->flags & PT_FLAGS_DISABLE_IRQ))
273 local_irq_enable();
276 * If it hits in kernel mode and we can't fix it up, just exit the
277 * current process and hope for the best.
279 if (is_kernel) {
280 const char *name;
281 char buf[100];
282 if (fixup_exception(regs)) /* ILL_TRANS or UNALIGN_DATA */
283 return;
284 if (fault_num >= 0 &&
285 fault_num < ARRAY_SIZE(int_name) &&
286 int_name[fault_num] != NULL)
287 name = int_name[fault_num];
288 else
289 name = "Unknown interrupt";
290 if (fault_num == INT_GPV)
291 snprintf(buf, sizeof(buf), "; GPV_REASON %#lx", reason);
292 #ifdef __tilegx__
293 else if (fault_num == INT_ILL_TRANS)
294 snprintf(buf, sizeof(buf), "; address %#lx", reason);
295 #endif
296 else
297 buf[0] = '\0';
298 pr_alert("Kernel took bad trap %d (%s) at PC %#lx%s\n",
299 fault_num, name, regs->pc, buf);
300 show_regs(regs);
301 do_exit(SIGKILL); /* FIXME: implement i386 die() */
304 switch (fault_num) {
305 case INT_MEM_ERROR:
306 signo = SIGBUS;
307 code = BUS_OBJERR;
308 break;
309 case INT_ILL:
310 if (copy_from_user(&instr, (void __user *)regs->pc,
311 sizeof(instr))) {
312 pr_err("Unreadable instruction for INT_ILL: %#lx\n",
313 regs->pc);
314 do_exit(SIGKILL);
316 if (!special_ill(instr, &signo, &code)) {
317 signo = SIGILL;
318 code = ILL_ILLOPC;
320 address = regs->pc;
321 break;
322 case INT_GPV:
323 #if CHIP_HAS_TILE_DMA()
324 if (retry_gpv(reason))
325 return;
326 #endif
327 /*FALLTHROUGH*/
328 case INT_UDN_ACCESS:
329 case INT_IDN_ACCESS:
330 #if CHIP_HAS_SN()
331 case INT_SN_ACCESS:
332 #endif
333 signo = SIGILL;
334 code = ILL_PRVREG;
335 address = regs->pc;
336 break;
337 case INT_SWINT_3:
338 case INT_SWINT_2:
339 case INT_SWINT_0:
340 signo = SIGILL;
341 code = ILL_ILLTRP;
342 address = regs->pc;
343 break;
344 case INT_UNALIGN_DATA:
345 #ifndef __tilegx__ /* Emulated support for single step debugging */
346 if (unaligned_fixup >= 0) {
347 struct single_step_state *state =
348 current_thread_info()->step_state;
349 if (!state ||
350 (void __user *)(regs->pc) != state->buffer) {
351 single_step_once(regs);
352 return;
355 #endif
356 signo = SIGBUS;
357 code = BUS_ADRALN;
358 address = 0;
359 break;
360 case INT_DOUBLE_FAULT:
362 * For double fault, "reason" is actually passed as
363 * SYSTEM_SAVE_K_2, the hypervisor's double-fault info, so
364 * we can provide the original fault number rather than
365 * the uninteresting "INT_DOUBLE_FAULT" so the user can
366 * learn what actually struck while PL0 ICS was set.
368 fault_num = reason;
369 signo = SIGILL;
370 code = ILL_DBLFLT;
371 address = regs->pc;
372 break;
373 #ifdef __tilegx__
374 case INT_ILL_TRANS: {
375 /* Avoid a hardware erratum with the return address stack. */
376 fill_ra_stack();
378 signo = SIGSEGV;
379 address = reason;
380 code = SEGV_MAPERR;
381 break;
383 #endif
384 default:
385 panic("Unexpected do_trap interrupt number %d", fault_num);
388 info.si_signo = signo;
389 info.si_code = code;
390 info.si_addr = (void __user *)address;
391 if (signo == SIGILL)
392 info.si_trapno = fault_num;
393 if (signo != SIGTRAP)
394 trace_unhandled_signal("trap", regs, address, signo);
395 force_sig_info(signo, &info, current);
398 void do_nmi(struct pt_regs *regs, int fault_num, unsigned long reason)
400 nmi_enter();
401 switch (reason) {
402 #ifdef arch_trigger_cpumask_backtrace
403 case TILE_NMI_DUMP_STACK:
404 nmi_cpu_backtrace(regs);
405 break;
406 #endif
407 default:
408 panic("Unexpected do_nmi type %ld", reason);
410 nmi_exit();
413 /* Deprecated function currently only used here. */
414 extern void _dump_stack(int dummy, ulong pc, ulong lr, ulong sp, ulong r52);
416 void kernel_double_fault(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
418 _dump_stack(dummy, pc, lr, sp, r52);
419 pr_emerg("Double fault: exiting\n");
420 machine_halt();