Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 1996, 97, 2000, 2001 by Ralf Baechle |
| 7 | * Copyright (C) 2001 MIPS Technologies, Inc. |
| 8 | */ |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/signal.h> |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 12 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <asm/branch.h> |
| 14 | #include <asm/cpu.h> |
| 15 | #include <asm/cpu-features.h> |
Ralf Baechle | 1d74f6b | 2005-05-09 13:16:07 +0000 | [diff] [blame] | 16 | #include <asm/fpu.h> |
Leonid Yegoshin | fb6883e | 2013-03-25 13:08:40 -0500 | [diff] [blame^] | 17 | #include <asm/fpu_emulator.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/inst.h> |
| 19 | #include <asm/ptrace.h> |
| 20 | #include <asm/uaccess.h> |
| 21 | |
Leonid Yegoshin | fb6883e | 2013-03-25 13:08:40 -0500 | [diff] [blame^] | 22 | /* |
| 23 | * Calculate and return exception PC in case of branch delay |
| 24 | * slot for microMIPS. It does not clear the ISA mode bit. |
| 25 | */ |
| 26 | int __isa_exception_epc(struct pt_regs *regs) |
| 27 | { |
| 28 | long epc = regs->cp0_epc; |
| 29 | unsigned short inst; |
| 30 | |
| 31 | /* Calculate exception PC in branch delay slot. */ |
| 32 | if (__get_user(inst, (u16 __user *) msk_isa16_mode(epc))) { |
| 33 | /* This should never happen because delay slot was checked. */ |
| 34 | force_sig(SIGSEGV, current); |
| 35 | return epc; |
| 36 | } |
| 37 | |
| 38 | if (mm_insn_16bit(inst)) |
| 39 | epc += 2; |
| 40 | else |
| 41 | epc += 4; |
| 42 | |
| 43 | return epc; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Compute return address and emulate branch in microMIPS mode after an |
| 48 | * exception only. It does not handle compact branches/jumps and cannot |
| 49 | * be used in interrupt context. (Compact branches/jumps do not cause |
| 50 | * exceptions.) |
| 51 | */ |
| 52 | int __microMIPS_compute_return_epc(struct pt_regs *regs) |
| 53 | { |
| 54 | u16 __user *pc16; |
| 55 | u16 halfword; |
| 56 | unsigned int word; |
| 57 | unsigned long contpc; |
| 58 | struct mm_decoded_insn mminsn = { 0 }; |
| 59 | |
| 60 | mminsn.micro_mips_mode = 1; |
| 61 | |
| 62 | /* This load never faults. */ |
| 63 | pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc); |
| 64 | __get_user(halfword, pc16); |
| 65 | pc16++; |
| 66 | contpc = regs->cp0_epc + 2; |
| 67 | word = ((unsigned int)halfword << 16); |
| 68 | mminsn.pc_inc = 2; |
| 69 | |
| 70 | if (!mm_insn_16bit(halfword)) { |
| 71 | __get_user(halfword, pc16); |
| 72 | pc16++; |
| 73 | contpc = regs->cp0_epc + 4; |
| 74 | mminsn.pc_inc = 4; |
| 75 | word |= halfword; |
| 76 | } |
| 77 | mminsn.insn = word; |
| 78 | |
| 79 | if (get_user(halfword, pc16)) |
| 80 | goto sigsegv; |
| 81 | mminsn.next_pc_inc = 2; |
| 82 | word = ((unsigned int)halfword << 16); |
| 83 | |
| 84 | if (!mm_insn_16bit(halfword)) { |
| 85 | pc16++; |
| 86 | if (get_user(halfword, pc16)) |
| 87 | goto sigsegv; |
| 88 | mminsn.next_pc_inc = 4; |
| 89 | word |= halfword; |
| 90 | } |
| 91 | mminsn.next_insn = word; |
| 92 | |
| 93 | mm_isBranchInstr(regs, mminsn, &contpc); |
| 94 | |
| 95 | regs->cp0_epc = contpc; |
| 96 | |
| 97 | return 0; |
| 98 | |
| 99 | sigsegv: |
| 100 | force_sig(SIGSEGV, current); |
| 101 | return -EFAULT; |
| 102 | } |
| 103 | |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 104 | /** |
| 105 | * __compute_return_epc_for_insn - Computes the return address and do emulate |
| 106 | * branch simulation, if required. |
| 107 | * |
| 108 | * @regs: Pointer to pt_regs |
| 109 | * @insn: branch instruction to decode |
| 110 | * @returns: -EFAULT on error and forces SIGBUS, and on success |
| 111 | * returns 0 or BRANCH_LIKELY_TAKEN as appropriate after |
| 112 | * evaluating the branch. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | */ |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 114 | int __compute_return_epc_for_insn(struct pt_regs *regs, |
| 115 | union mips_instruction insn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Atsushi Nemoto | 5e0373b | 2007-07-13 23:02:42 +0900 | [diff] [blame] | 117 | unsigned int bit, fcr31, dspcontrol; |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 118 | long epc = regs->cp0_epc; |
| 119 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | switch (insn.i_format.opcode) { |
| 122 | /* |
| 123 | * jr and jalr are in r_format format. |
| 124 | */ |
| 125 | case spec_op: |
| 126 | switch (insn.r_format.func) { |
| 127 | case jalr_op: |
| 128 | regs->regs[insn.r_format.rd] = epc + 8; |
| 129 | /* Fall through */ |
| 130 | case jr_op: |
| 131 | regs->cp0_epc = regs->regs[insn.r_format.rs]; |
| 132 | break; |
| 133 | } |
| 134 | break; |
| 135 | |
| 136 | /* |
| 137 | * This group contains: |
| 138 | * bltz_op, bgez_op, bltzl_op, bgezl_op, |
| 139 | * bltzal_op, bgezal_op, bltzall_op, bgezall_op. |
| 140 | */ |
| 141 | case bcond_op: |
| 142 | switch (insn.i_format.rt) { |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 143 | case bltz_op: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | case bltzl_op: |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 145 | if ((long)regs->regs[insn.i_format.rs] < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 147 | if (insn.i_format.rt == bltzl_op) |
| 148 | ret = BRANCH_LIKELY_TAKEN; |
| 149 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | epc += 8; |
| 151 | regs->cp0_epc = epc; |
| 152 | break; |
| 153 | |
| 154 | case bgez_op: |
| 155 | case bgezl_op: |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 156 | if ((long)regs->regs[insn.i_format.rs] >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 158 | if (insn.i_format.rt == bgezl_op) |
| 159 | ret = BRANCH_LIKELY_TAKEN; |
| 160 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | epc += 8; |
| 162 | regs->cp0_epc = epc; |
| 163 | break; |
| 164 | |
| 165 | case bltzal_op: |
| 166 | case bltzall_op: |
| 167 | regs->regs[31] = epc + 8; |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 168 | if ((long)regs->regs[insn.i_format.rs] < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 170 | if (insn.i_format.rt == bltzall_op) |
| 171 | ret = BRANCH_LIKELY_TAKEN; |
| 172 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | epc += 8; |
| 174 | regs->cp0_epc = epc; |
| 175 | break; |
| 176 | |
| 177 | case bgezal_op: |
| 178 | case bgezall_op: |
| 179 | regs->regs[31] = epc + 8; |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 180 | if ((long)regs->regs[insn.i_format.rs] >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 182 | if (insn.i_format.rt == bgezall_op) |
| 183 | ret = BRANCH_LIKELY_TAKEN; |
| 184 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | epc += 8; |
| 186 | regs->cp0_epc = epc; |
| 187 | break; |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 188 | |
Ralf Baechle | e50c0a8 | 2005-05-31 11:49:19 +0000 | [diff] [blame] | 189 | case bposge32_op: |
| 190 | if (!cpu_has_dsp) |
| 191 | goto sigill; |
| 192 | |
| 193 | dspcontrol = rddsp(0x01); |
| 194 | |
| 195 | if (dspcontrol >= 32) { |
| 196 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 197 | } else |
| 198 | epc += 8; |
| 199 | regs->cp0_epc = epc; |
| 200 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | break; |
| 203 | |
| 204 | /* |
| 205 | * These are unconditional and in j_format. |
| 206 | */ |
| 207 | case jal_op: |
| 208 | regs->regs[31] = regs->cp0_epc + 8; |
| 209 | case j_op: |
| 210 | epc += 4; |
| 211 | epc >>= 28; |
| 212 | epc <<= 28; |
| 213 | epc |= (insn.j_format.target << 2); |
| 214 | regs->cp0_epc = epc; |
Leonid Yegoshin | fb6883e | 2013-03-25 13:08:40 -0500 | [diff] [blame^] | 215 | if (insn.i_format.opcode == jalx_op) |
| 216 | set_isa16_mode(regs->cp0_epc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | break; |
| 218 | |
| 219 | /* |
| 220 | * These are conditional and in i_format. |
| 221 | */ |
| 222 | case beq_op: |
| 223 | case beql_op: |
| 224 | if (regs->regs[insn.i_format.rs] == |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 225 | regs->regs[insn.i_format.rt]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 227 | if (insn.i_format.rt == beql_op) |
| 228 | ret = BRANCH_LIKELY_TAKEN; |
| 229 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | epc += 8; |
| 231 | regs->cp0_epc = epc; |
| 232 | break; |
| 233 | |
| 234 | case bne_op: |
| 235 | case bnel_op: |
| 236 | if (regs->regs[insn.i_format.rs] != |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 237 | regs->regs[insn.i_format.rt]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 239 | if (insn.i_format.rt == bnel_op) |
| 240 | ret = BRANCH_LIKELY_TAKEN; |
| 241 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | epc += 8; |
| 243 | regs->cp0_epc = epc; |
| 244 | break; |
| 245 | |
| 246 | case blez_op: /* not really i_format */ |
| 247 | case blezl_op: |
| 248 | /* rt field assumed to be zero */ |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 249 | if ((long)regs->regs[insn.i_format.rs] <= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 251 | if (insn.i_format.rt == bnel_op) |
| 252 | ret = BRANCH_LIKELY_TAKEN; |
| 253 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | epc += 8; |
| 255 | regs->cp0_epc = epc; |
| 256 | break; |
| 257 | |
| 258 | case bgtz_op: |
| 259 | case bgtzl_op: |
| 260 | /* rt field assumed to be zero */ |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 261 | if ((long)regs->regs[insn.i_format.rs] > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 263 | if (insn.i_format.rt == bnel_op) |
| 264 | ret = BRANCH_LIKELY_TAKEN; |
| 265 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | epc += 8; |
| 267 | regs->cp0_epc = epc; |
| 268 | break; |
| 269 | |
| 270 | /* |
| 271 | * And now the FPA/cp1 branch instructions. |
| 272 | */ |
| 273 | case cop1_op: |
Ralf Baechle | 1d74f6b | 2005-05-09 13:16:07 +0000 | [diff] [blame] | 274 | preempt_disable(); |
| 275 | if (is_fpu_owner()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | asm volatile("cfc1\t%0,$31" : "=r" (fcr31)); |
Ralf Baechle | 1d74f6b | 2005-05-09 13:16:07 +0000 | [diff] [blame] | 277 | else |
Atsushi Nemoto | eae8907 | 2006-05-16 01:26:03 +0900 | [diff] [blame] | 278 | fcr31 = current->thread.fpu.fcr31; |
Ralf Baechle | 1d74f6b | 2005-05-09 13:16:07 +0000 | [diff] [blame] | 279 | preempt_enable(); |
| 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | bit = (insn.i_format.rt >> 2); |
| 282 | bit += (bit != 0); |
| 283 | bit += 23; |
Ralf Baechle | ee1cca1 | 2006-04-26 21:33:03 +0100 | [diff] [blame] | 284 | switch (insn.i_format.rt & 3) { |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 285 | case 0: /* bc1f */ |
| 286 | case 2: /* bc1fl */ |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 287 | if (~fcr31 & (1 << bit)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 289 | if (insn.i_format.rt == 2) |
| 290 | ret = BRANCH_LIKELY_TAKEN; |
| 291 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | epc += 8; |
| 293 | regs->cp0_epc = epc; |
| 294 | break; |
| 295 | |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 296 | case 1: /* bc1t */ |
| 297 | case 3: /* bc1tl */ |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 298 | if (fcr31 & (1 << bit)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 300 | if (insn.i_format.rt == 3) |
| 301 | ret = BRANCH_LIKELY_TAKEN; |
| 302 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | epc += 8; |
| 304 | regs->cp0_epc = epc; |
| 305 | break; |
| 306 | } |
| 307 | break; |
David Daney | 126336f | 2008-12-11 15:33:34 -0800 | [diff] [blame] | 308 | #ifdef CONFIG_CPU_CAVIUM_OCTEON |
| 309 | case lwc2_op: /* This is bbit0 on Octeon */ |
| 310 | if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) |
| 311 | == 0) |
| 312 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 313 | else |
| 314 | epc += 8; |
| 315 | regs->cp0_epc = epc; |
| 316 | break; |
| 317 | case ldc2_op: /* This is bbit032 on Octeon */ |
| 318 | if ((regs->regs[insn.i_format.rs] & |
| 319 | (1ull<<(insn.i_format.rt+32))) == 0) |
| 320 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 321 | else |
| 322 | epc += 8; |
| 323 | regs->cp0_epc = epc; |
| 324 | break; |
| 325 | case swc2_op: /* This is bbit1 on Octeon */ |
| 326 | if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) |
| 327 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 328 | else |
| 329 | epc += 8; |
| 330 | regs->cp0_epc = epc; |
| 331 | break; |
| 332 | case sdc2_op: /* This is bbit132 on Octeon */ |
| 333 | if (regs->regs[insn.i_format.rs] & |
| 334 | (1ull<<(insn.i_format.rt+32))) |
| 335 | epc = epc + 4 + (insn.i_format.simmediate << 2); |
| 336 | else |
| 337 | epc += 8; |
| 338 | regs->cp0_epc = epc; |
| 339 | break; |
| 340 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Maneesh Soni | d8d4e3a | 2011-11-08 17:07:11 +0530 | [diff] [blame] | 343 | return ret; |
| 344 | |
| 345 | sigill: |
| 346 | printk("%s: DSP branch but not DSP ASE - sending SIGBUS.\n", current->comm); |
| 347 | force_sig(SIGBUS, current); |
| 348 | return -EFAULT; |
| 349 | } |
| 350 | EXPORT_SYMBOL_GPL(__compute_return_epc_for_insn); |
| 351 | |
| 352 | int __compute_return_epc(struct pt_regs *regs) |
| 353 | { |
| 354 | unsigned int __user *addr; |
| 355 | long epc; |
| 356 | union mips_instruction insn; |
| 357 | |
| 358 | epc = regs->cp0_epc; |
| 359 | if (epc & 3) |
| 360 | goto unaligned; |
| 361 | |
| 362 | /* |
| 363 | * Read the instruction |
| 364 | */ |
| 365 | addr = (unsigned int __user *) epc; |
| 366 | if (__get_user(insn.word, addr)) { |
| 367 | force_sig(SIGSEGV, current); |
| 368 | return -EFAULT; |
| 369 | } |
| 370 | |
| 371 | return __compute_return_epc_for_insn(regs, insn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | unaligned: |
| 374 | printk("%s: unaligned epc - sending SIGBUS.\n", current->comm); |
| 375 | force_sig(SIGBUS, current); |
| 376 | return -EFAULT; |
Ralf Baechle | e50c0a8 | 2005-05-31 11:49:19 +0000 | [diff] [blame] | 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |