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
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/kprobes.h>
18 #include <linux/kdebug.h>
19 #include <linux/module.h>
20 #include <linux/reboot.h>
21 #include <linux/uaccess.h>
22 #include <linux/ptrace.h>
23 #include <linux/context_tracking.h>
24 #include <asm/stack.h>
25 #include <asm/traps.h>
26 #include <asm/setup.h>
28 #include <arch/interrupts.h>
29 #include <arch/spr_def.h>
30 #include <arch/opcode.h>
32 void __init trap_init(void)
34 /* Nothing needed here since we link code at .intrpt */
37 int unaligned_fixup = 1;
39 static int __init setup_unaligned_fixup(char *str)
42 * Say "=-1" to completely disable it. If you just do "=0", we
43 * will still parse the instruction, then fire a SIGBUS with
44 * the correct address from inside the single_step code.
46 if (kstrtoint(str, 0, &unaligned_fixup) != 0)
49 pr_info("Fixups for unaligned data accesses are %s\n",
50 unaligned_fixup >= 0 ?
51 (unaligned_fixup ? "enabled" : "disabled") :
52 "completely disabled");
55 __setup("unaligned_fixup=", setup_unaligned_fixup);
57 #if CHIP_HAS_TILE_DMA()
59 static int dma_disabled;
61 static int __init nodma(char *str)
63 pr_info("User-space DMA is disabled\n");
67 __setup("nodma", nodma);
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 */
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.
80 static int retry_gpv(unsigned int gpv_reason)
84 if (gpv_reason & IRET_ERROR)
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;
102 #endif /* CHIP_HAS_TILE_DMA() */
104 extern tile_bundle_bits bpt_code;
106 asm(".pushsection .rodata.bpt_code,\"a\";"
109 ".size bpt_code,.-bpt_code;"
112 static int special_ill(tile_bundle_bits bundle, int *sigp, int *codep)
114 int sig, code, maxcode;
116 if (bundle == bpt_code) {
122 /* If it's a "raise" bundle, then "ill" must be in pipe X1. */
124 if ((bundle & TILEGX_BUNDLE_MODE_MASK) != 0)
126 if (get_Opcode_X1(bundle) != RRR_0_OPCODE_X1)
128 if (get_RRROpcodeExtension_X1(bundle) != UNARY_RRR_0_OPCODE_X1)
130 if (get_UnaryOpcodeExtension_X1(bundle) != ILL_UNARY_OPCODE_X1)
133 if (bundle & TILEPRO_BUNDLE_Y_ENCODING_MASK)
135 if (get_Opcode_X1(bundle) != SHUN_0_OPCODE_X1)
137 if (get_UnShOpcodeExtension_X1(bundle) != UN_0_SHUN_0_OPCODE_X1)
139 if (get_UnOpcodeExtension_X1(bundle) != ILL_UN_0_SHUN_0_OPCODE_X1)
143 /* Check that the magic distinguishers are set to mean "raise". */
144 if (get_Dest_X1(bundle) != 29 || get_SrcA_X1(bundle) != 37)
147 /* There must be an "addli zero, zero, VAL" in X0. */
148 if (get_Opcode_X0(bundle) != ADDLI_OPCODE_X0)
150 if (get_Dest_X0(bundle) != TREG_ZERO)
152 if (get_SrcA_X0(bundle) != TREG_ZERO)
156 * Validate the proposed signal number and si_code value.
157 * Note that we embed these in the static instruction itself
158 * so that we perturb the register state as little as possible
159 * at the time of the actual fault; it's unlikely you'd ever
160 * need to dynamically choose which kind of fault to raise
163 sig = get_Imm16_X0(bundle) & 0x3f;
183 code = (get_Imm16_X0(bundle) >> 6) & 0xf;
184 if (code <= 0 || code > maxcode)
187 /* Make it the requested signal. */
189 *codep = code | __SI_FAULT;
193 static const char *const int_name[] = {
194 [INT_MEM_ERROR] = "Memory error",
195 [INT_ILL] = "Illegal instruction",
196 [INT_GPV] = "General protection violation",
197 [INT_UDN_ACCESS] = "UDN access",
198 [INT_IDN_ACCESS] = "IDN access",
200 [INT_SN_ACCESS] = "SN access",
202 [INT_SWINT_3] = "Software interrupt 3",
203 [INT_SWINT_2] = "Software interrupt 2",
204 [INT_SWINT_0] = "Software interrupt 0",
205 [INT_UNALIGN_DATA] = "Unaligned data",
206 [INT_DOUBLE_FAULT] = "Double fault",
208 [INT_ILL_TRANS] = "Illegal virtual address",
212 static int do_bpt(struct pt_regs *regs)
214 unsigned long bundle, bcode, bpt;
216 bundle = *(unsigned long *)instruction_pointer(regs);
219 * bpt shoule be { bpt; nop }, which is 0x286a44ae51485000ULL.
220 * we encode the unused least significant bits for other purpose.
222 bpt = bundle & ~((1ULL << 12) - 1);
223 if (bpt != TILE_BPT_BUNDLE)
226 bcode = bundle & ((1ULL << 12) - 1);
228 * notify the kprobe handlers, if instruction is likely to
232 /* breakpoint_insn */
234 notify_die(DIE_BREAK, "debug", regs, bundle,
238 case DIE_COMPILED_BPT:
239 notify_die(DIE_COMPILED_BPT, "debug", regs, bundle,
242 /* breakpoint2_insn */
244 notify_die(DIE_SSTEPBP, "single_step", regs, bundle,
254 void __kprobes do_trap(struct pt_regs *regs, int fault_num,
255 unsigned long reason)
257 enum ctx_state prev_state = exception_enter();
258 siginfo_t info = { 0 };
260 unsigned long address = 0;
261 tile_bundle_bits instr;
262 int is_kernel = !user_mode(regs);
264 /* Handle breakpoints, etc. */
265 if (is_kernel && fault_num == INT_ILL && do_bpt(regs))
268 /* Re-enable interrupts, if they were previously enabled. */
269 if (!(regs->flags & PT_FLAGS_DISABLE_IRQ))
273 * If it hits in kernel mode and we can't fix it up, just exit the
274 * current process and hope for the best.
279 if (fixup_exception(regs)) /* ILL_TRANS or UNALIGN_DATA */
281 if (fault_num >= 0 &&
282 fault_num < ARRAY_SIZE(int_name) &&
283 int_name[fault_num] != NULL)
284 name = int_name[fault_num];
286 name = "Unknown interrupt";
287 if (fault_num == INT_GPV)
288 snprintf(buf, sizeof(buf), "; GPV_REASON %#lx", reason);
290 else if (fault_num == INT_ILL_TRANS)
291 snprintf(buf, sizeof(buf), "; address %#lx", reason);
295 pr_alert("Kernel took bad trap %d (%s) at PC %#lx%s\n",
296 fault_num, name, regs->pc, buf);
298 do_exit(SIGKILL); /* FIXME: implement i386 die() */
307 if (copy_from_user(&instr, (void __user *)regs->pc,
309 pr_err("Unreadable instruction for INT_ILL: %#lx\n",
313 if (!special_ill(instr, &signo, &code)) {
320 #if CHIP_HAS_TILE_DMA()
321 if (retry_gpv(reason))
341 case INT_UNALIGN_DATA:
342 #ifndef __tilegx__ /* Emulated support for single step debugging */
343 if (unaligned_fixup >= 0) {
344 struct single_step_state *state =
345 current_thread_info()->step_state;
347 (void __user *)(regs->pc) != state->buffer) {
348 single_step_once(regs);
357 case INT_DOUBLE_FAULT:
359 * For double fault, "reason" is actually passed as
360 * SYSTEM_SAVE_K_2, the hypervisor's double-fault info, so
361 * we can provide the original fault number rather than
362 * the uninteresting "INT_DOUBLE_FAULT" so the user can
363 * learn what actually struck while PL0 ICS was set.
371 case INT_ILL_TRANS: {
372 /* Avoid a hardware erratum with the return address stack. */
382 panic("Unexpected do_trap interrupt number %d", fault_num);
385 info.si_signo = signo;
387 info.si_addr = (void __user *)address;
389 info.si_trapno = fault_num;
390 if (signo != SIGTRAP)
391 trace_unhandled_signal("trap", regs, address, signo);
392 force_sig_info(signo, &info, current);
395 exception_exit(prev_state);
398 void do_nmi(struct pt_regs *regs, int fault_num, unsigned long reason)
401 case TILE_NMI_DUMP_STACK:
402 do_nmi_dump_stack(regs);
405 panic("Unexpected do_nmi type %ld", reason);
410 /* Deprecated function currently only used here. */
411 extern void _dump_stack(int dummy, ulong pc, ulong lr, ulong sp, ulong r52);
413 void kernel_double_fault(int dummy, ulong pc, ulong lr, ulong sp, ulong r52)
415 _dump_stack(dummy, pc, lr, sp, r52);
416 pr_emerg("Double fault: exiting\n");