blob: a03836b5b683d68b2363526cc3ff06a4ee56a7fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Sonid8d4e3a2011-11-08 17:07:11 +053012#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/branch.h>
14#include <asm/cpu.h>
15#include <asm/cpu-features.h>
Ralf Baechle1d74f6b2005-05-09 13:16:07 +000016#include <asm/fpu.h>
Leonid Yegoshinfb6883e2013-03-25 13:08:40 -050017#include <asm/fpu_emulator.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/inst.h>
19#include <asm/ptrace.h>
20#include <asm/uaccess.h>
21
Leonid Yegoshinfb6883e2013-03-25 13:08:40 -050022/*
23 * Calculate and return exception PC in case of branch delay
24 * slot for microMIPS. It does not clear the ISA mode bit.
25 */
26int __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 */
52int __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
99sigsegv:
100 force_sig(SIGSEGV, current);
101 return -EFAULT;
102}
103
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530104/**
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 Torvalds1da177e2005-04-16 15:20:36 -0700113 */
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530114int __compute_return_epc_for_insn(struct pt_regs *regs,
115 union mips_instruction insn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Atsushi Nemoto5e0373b2007-07-13 23:02:42 +0900117 unsigned int bit, fcr31, dspcontrol;
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530118 long epc = regs->cp0_epc;
119 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 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 Baechle70342282013-01-22 12:59:30 +0100143 case bltz_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 case bltzl_op:
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530145 if ((long)regs->regs[insn.i_format.rs] < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 epc = epc + 4 + (insn.i_format.simmediate << 2);
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530147 if (insn.i_format.rt == bltzl_op)
148 ret = BRANCH_LIKELY_TAKEN;
149 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 epc += 8;
151 regs->cp0_epc = epc;
152 break;
153
154 case bgez_op:
155 case bgezl_op:
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530156 if ((long)regs->regs[insn.i_format.rs] >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 epc = epc + 4 + (insn.i_format.simmediate << 2);
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530158 if (insn.i_format.rt == bgezl_op)
159 ret = BRANCH_LIKELY_TAKEN;
160 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 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 Sonid8d4e3a2011-11-08 17:07:11 +0530168 if ((long)regs->regs[insn.i_format.rs] < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 epc = epc + 4 + (insn.i_format.simmediate << 2);
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530170 if (insn.i_format.rt == bltzall_op)
171 ret = BRANCH_LIKELY_TAKEN;
172 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 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 Sonid8d4e3a2011-11-08 17:07:11 +0530180 if ((long)regs->regs[insn.i_format.rs] >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 epc = epc + 4 + (insn.i_format.simmediate << 2);
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530182 if (insn.i_format.rt == bgezall_op)
183 ret = BRANCH_LIKELY_TAKEN;
184 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 epc += 8;
186 regs->cp0_epc = epc;
187 break;
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530188
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000189 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 Torvalds1da177e2005-04-16 15:20:36 -0700201 }
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 Yegoshinfb6883e2013-03-25 13:08:40 -0500215 if (insn.i_format.opcode == jalx_op)
216 set_isa16_mode(regs->cp0_epc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 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 Sonid8d4e3a2011-11-08 17:07:11 +0530225 regs->regs[insn.i_format.rt]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 epc = epc + 4 + (insn.i_format.simmediate << 2);
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530227 if (insn.i_format.rt == beql_op)
228 ret = BRANCH_LIKELY_TAKEN;
229 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 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 Sonid8d4e3a2011-11-08 17:07:11 +0530237 regs->regs[insn.i_format.rt]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 epc = epc + 4 + (insn.i_format.simmediate << 2);
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530239 if (insn.i_format.rt == bnel_op)
240 ret = BRANCH_LIKELY_TAKEN;
241 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 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 Sonid8d4e3a2011-11-08 17:07:11 +0530249 if ((long)regs->regs[insn.i_format.rs] <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 epc = epc + 4 + (insn.i_format.simmediate << 2);
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530251 if (insn.i_format.rt == bnel_op)
252 ret = BRANCH_LIKELY_TAKEN;
253 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 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 Sonid8d4e3a2011-11-08 17:07:11 +0530261 if ((long)regs->regs[insn.i_format.rs] > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 epc = epc + 4 + (insn.i_format.simmediate << 2);
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530263 if (insn.i_format.rt == bnel_op)
264 ret = BRANCH_LIKELY_TAKEN;
265 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 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 Baechle1d74f6b2005-05-09 13:16:07 +0000274 preempt_disable();
275 if (is_fpu_owner())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
Ralf Baechle1d74f6b2005-05-09 13:16:07 +0000277 else
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900278 fcr31 = current->thread.fpu.fcr31;
Ralf Baechle1d74f6b2005-05-09 13:16:07 +0000279 preempt_enable();
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 bit = (insn.i_format.rt >> 2);
282 bit += (bit != 0);
283 bit += 23;
Ralf Baechleee1cca12006-04-26 21:33:03 +0100284 switch (insn.i_format.rt & 3) {
Ralf Baechle70342282013-01-22 12:59:30 +0100285 case 0: /* bc1f */
286 case 2: /* bc1fl */
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530287 if (~fcr31 & (1 << bit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 epc = epc + 4 + (insn.i_format.simmediate << 2);
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530289 if (insn.i_format.rt == 2)
290 ret = BRANCH_LIKELY_TAKEN;
291 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 epc += 8;
293 regs->cp0_epc = epc;
294 break;
295
Ralf Baechle70342282013-01-22 12:59:30 +0100296 case 1: /* bc1t */
297 case 3: /* bc1tl */
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530298 if (fcr31 & (1 << bit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 epc = epc + 4 + (insn.i_format.simmediate << 2);
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530300 if (insn.i_format.rt == 3)
301 ret = BRANCH_LIKELY_TAKEN;
302 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 epc += 8;
304 regs->cp0_epc = epc;
305 break;
306 }
307 break;
David Daney126336f2008-12-11 15:33:34 -0800308#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 Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
Maneesh Sonid8d4e3a2011-11-08 17:07:11 +0530343 return ret;
344
345sigill:
346 printk("%s: DSP branch but not DSP ASE - sending SIGBUS.\n", current->comm);
347 force_sig(SIGBUS, current);
348 return -EFAULT;
349}
350EXPORT_SYMBOL_GPL(__compute_return_epc_for_insn);
351
352int __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 Torvalds1da177e2005-04-16 15:20:36 -0700372
373unaligned:
374 printk("%s: unaligned epc - sending SIGBUS.\n", current->comm);
375 force_sig(SIGBUS, current);
376 return -EFAULT;
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}