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