MIPS: Emulate the new MIPS R6 BEQZC and JIC instructions
[deliverable/linux.git] / arch / mips / math-emu / cp1emu.c
CommitLineData
1da177e4 1/*
3f7cac41 2 * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
1da177e4
LT
3 *
4 * MIPS floating point support
5 * Copyright (C) 1994-2000 Algorithmics Ltd.
1da177e4
LT
6 *
7 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
8 * Copyright (C) 2000 MIPS Technologies, Inc.
9 *
10 * This program is free software; you can distribute it and/or modify it
11 * under the terms of the GNU General Public License (Version 2) as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
3f7cac41 21 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1da177e4
LT
22 *
23 * A complete emulator for MIPS coprocessor 1 instructions. This is
24 * required for #float(switch) or #float(trap), where it catches all
25 * COP1 instructions via the "CoProcessor Unusable" exception.
26 *
27 * More surprisingly it is also required for #float(ieee), to help out
3f7cac41 28 * the hardware FPU at the boundaries of the IEEE-754 representation
1da177e4
LT
29 * (denormalised values, infinities, underflow, etc). It is made
30 * quite nasty because emulation of some non-COP1 instructions is
31 * required, e.g. in branch delay slots.
32 *
3f7cac41 33 * Note if you know that you won't have an FPU, then you'll get much
1da177e4
LT
34 * better performance by compiling with -msoft-float!
35 */
36#include <linux/sched.h>
83fd38ca 37#include <linux/debugfs.h>
08a07904 38#include <linux/kconfig.h>
85c51c51 39#include <linux/percpu-defs.h>
7f788d2d 40#include <linux/perf_event.h>
1da177e4 41
cd8ee345 42#include <asm/branch.h>
1da177e4 43#include <asm/inst.h>
1da177e4
LT
44#include <asm/ptrace.h>
45#include <asm/signal.h>
cd8ee345
RB
46#include <asm/uaccess.h>
47
48#include <asm/processor.h>
1da177e4 49#include <asm/fpu_emulator.h>
102cedc3 50#include <asm/fpu.h>
1da177e4
LT
51
52#include "ieee754.h"
1da177e4 53
1da177e4
LT
54/* Function which emulates a floating point instruction. */
55
eae89076 56static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
1da177e4
LT
57 mips_instruction);
58
1da177e4 59static int fpux_emu(struct pt_regs *,
515b029d 60 struct mips_fpu_struct *, mips_instruction, void *__user *);
1da177e4 61
1da177e4
LT
62/* Control registers */
63
64#define FPCREG_RID 0 /* $0 = revision id */
65#define FPCREG_CSR 31 /* $31 = csr */
66
95e8f634
SM
67/* Determine rounding mode from the RM bits of the FCSR */
68#define modeindex(v) ((v) & FPU_CSR_RM)
69
1da177e4
LT
70/* convert condition code register number to csr bit */
71static const unsigned int fpucondbit[8] = {
72 FPU_CSR_COND0,
73 FPU_CSR_COND1,
74 FPU_CSR_COND2,
75 FPU_CSR_COND3,
76 FPU_CSR_COND4,
77 FPU_CSR_COND5,
78 FPU_CSR_COND6,
79 FPU_CSR_COND7
80};
1da177e4 81
102cedc3
LY
82/* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
83static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
84static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
85static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
86static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
87
88/*
89 * This functions translates a 32-bit microMIPS instruction
90 * into a 32-bit MIPS32 instruction. Returns 0 on success
91 * and SIGILL otherwise.
92 */
93static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
94{
95 union mips_instruction insn = *insn_ptr;
96 union mips_instruction mips32_insn = insn;
97 int func, fmt, op;
98
99 switch (insn.mm_i_format.opcode) {
100 case mm_ldc132_op:
101 mips32_insn.mm_i_format.opcode = ldc1_op;
102 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
103 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
104 break;
105 case mm_lwc132_op:
106 mips32_insn.mm_i_format.opcode = lwc1_op;
107 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
108 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
109 break;
110 case mm_sdc132_op:
111 mips32_insn.mm_i_format.opcode = sdc1_op;
112 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
113 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
114 break;
115 case mm_swc132_op:
116 mips32_insn.mm_i_format.opcode = swc1_op;
117 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
118 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
119 break;
120 case mm_pool32i_op:
121 /* NOTE: offset is << by 1 if in microMIPS mode. */
122 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
123 (insn.mm_i_format.rt == mm_bc1t_op)) {
124 mips32_insn.fb_format.opcode = cop1_op;
125 mips32_insn.fb_format.bc = bc_op;
126 mips32_insn.fb_format.flag =
127 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
128 } else
129 return SIGILL;
130 break;
131 case mm_pool32f_op:
132 switch (insn.mm_fp0_format.func) {
133 case mm_32f_01_op:
134 case mm_32f_11_op:
135 case mm_32f_02_op:
136 case mm_32f_12_op:
137 case mm_32f_41_op:
138 case mm_32f_51_op:
139 case mm_32f_42_op:
140 case mm_32f_52_op:
141 op = insn.mm_fp0_format.func;
142 if (op == mm_32f_01_op)
143 func = madd_s_op;
144 else if (op == mm_32f_11_op)
145 func = madd_d_op;
146 else if (op == mm_32f_02_op)
147 func = nmadd_s_op;
148 else if (op == mm_32f_12_op)
149 func = nmadd_d_op;
150 else if (op == mm_32f_41_op)
151 func = msub_s_op;
152 else if (op == mm_32f_51_op)
153 func = msub_d_op;
154 else if (op == mm_32f_42_op)
155 func = nmsub_s_op;
156 else
157 func = nmsub_d_op;
158 mips32_insn.fp6_format.opcode = cop1x_op;
159 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
160 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
161 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
162 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
163 mips32_insn.fp6_format.func = func;
164 break;
165 case mm_32f_10_op:
166 func = -1; /* Invalid */
167 op = insn.mm_fp5_format.op & 0x7;
168 if (op == mm_ldxc1_op)
169 func = ldxc1_op;
170 else if (op == mm_sdxc1_op)
171 func = sdxc1_op;
172 else if (op == mm_lwxc1_op)
173 func = lwxc1_op;
174 else if (op == mm_swxc1_op)
175 func = swxc1_op;
176
177 if (func != -1) {
178 mips32_insn.r_format.opcode = cop1x_op;
179 mips32_insn.r_format.rs =
180 insn.mm_fp5_format.base;
181 mips32_insn.r_format.rt =
182 insn.mm_fp5_format.index;
183 mips32_insn.r_format.rd = 0;
184 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
185 mips32_insn.r_format.func = func;
186 } else
187 return SIGILL;
188 break;
189 case mm_32f_40_op:
190 op = -1; /* Invalid */
191 if (insn.mm_fp2_format.op == mm_fmovt_op)
192 op = 1;
193 else if (insn.mm_fp2_format.op == mm_fmovf_op)
194 op = 0;
195 if (op != -1) {
196 mips32_insn.fp0_format.opcode = cop1_op;
197 mips32_insn.fp0_format.fmt =
198 sdps_format[insn.mm_fp2_format.fmt];
199 mips32_insn.fp0_format.ft =
200 (insn.mm_fp2_format.cc<<2) + op;
201 mips32_insn.fp0_format.fs =
202 insn.mm_fp2_format.fs;
203 mips32_insn.fp0_format.fd =
204 insn.mm_fp2_format.fd;
205 mips32_insn.fp0_format.func = fmovc_op;
206 } else
207 return SIGILL;
208 break;
209 case mm_32f_60_op:
210 func = -1; /* Invalid */
211 if (insn.mm_fp0_format.op == mm_fadd_op)
212 func = fadd_op;
213 else if (insn.mm_fp0_format.op == mm_fsub_op)
214 func = fsub_op;
215 else if (insn.mm_fp0_format.op == mm_fmul_op)
216 func = fmul_op;
217 else if (insn.mm_fp0_format.op == mm_fdiv_op)
218 func = fdiv_op;
219 if (func != -1) {
220 mips32_insn.fp0_format.opcode = cop1_op;
221 mips32_insn.fp0_format.fmt =
222 sdps_format[insn.mm_fp0_format.fmt];
223 mips32_insn.fp0_format.ft =
224 insn.mm_fp0_format.ft;
225 mips32_insn.fp0_format.fs =
226 insn.mm_fp0_format.fs;
227 mips32_insn.fp0_format.fd =
228 insn.mm_fp0_format.fd;
229 mips32_insn.fp0_format.func = func;
230 } else
231 return SIGILL;
232 break;
233 case mm_32f_70_op:
234 func = -1; /* Invalid */
235 if (insn.mm_fp0_format.op == mm_fmovn_op)
236 func = fmovn_op;
237 else if (insn.mm_fp0_format.op == mm_fmovz_op)
238 func = fmovz_op;
239 if (func != -1) {
240 mips32_insn.fp0_format.opcode = cop1_op;
241 mips32_insn.fp0_format.fmt =
242 sdps_format[insn.mm_fp0_format.fmt];
243 mips32_insn.fp0_format.ft =
244 insn.mm_fp0_format.ft;
245 mips32_insn.fp0_format.fs =
246 insn.mm_fp0_format.fs;
247 mips32_insn.fp0_format.fd =
248 insn.mm_fp0_format.fd;
249 mips32_insn.fp0_format.func = func;
250 } else
251 return SIGILL;
252 break;
253 case mm_32f_73_op: /* POOL32FXF */
254 switch (insn.mm_fp1_format.op) {
255 case mm_movf0_op:
256 case mm_movf1_op:
257 case mm_movt0_op:
258 case mm_movt1_op:
259 if ((insn.mm_fp1_format.op & 0x7f) ==
260 mm_movf0_op)
261 op = 0;
262 else
263 op = 1;
264 mips32_insn.r_format.opcode = spec_op;
265 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
266 mips32_insn.r_format.rt =
267 (insn.mm_fp4_format.cc << 2) + op;
268 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
269 mips32_insn.r_format.re = 0;
270 mips32_insn.r_format.func = movc_op;
271 break;
272 case mm_fcvtd0_op:
273 case mm_fcvtd1_op:
274 case mm_fcvts0_op:
275 case mm_fcvts1_op:
276 if ((insn.mm_fp1_format.op & 0x7f) ==
277 mm_fcvtd0_op) {
278 func = fcvtd_op;
279 fmt = swl_format[insn.mm_fp3_format.fmt];
280 } else {
281 func = fcvts_op;
282 fmt = dwl_format[insn.mm_fp3_format.fmt];
283 }
284 mips32_insn.fp0_format.opcode = cop1_op;
285 mips32_insn.fp0_format.fmt = fmt;
286 mips32_insn.fp0_format.ft = 0;
287 mips32_insn.fp0_format.fs =
288 insn.mm_fp3_format.fs;
289 mips32_insn.fp0_format.fd =
290 insn.mm_fp3_format.rt;
291 mips32_insn.fp0_format.func = func;
292 break;
293 case mm_fmov0_op:
294 case mm_fmov1_op:
295 case mm_fabs0_op:
296 case mm_fabs1_op:
297 case mm_fneg0_op:
298 case mm_fneg1_op:
299 if ((insn.mm_fp1_format.op & 0x7f) ==
300 mm_fmov0_op)
301 func = fmov_op;
302 else if ((insn.mm_fp1_format.op & 0x7f) ==
303 mm_fabs0_op)
304 func = fabs_op;
305 else
306 func = fneg_op;
307 mips32_insn.fp0_format.opcode = cop1_op;
308 mips32_insn.fp0_format.fmt =
309 sdps_format[insn.mm_fp3_format.fmt];
310 mips32_insn.fp0_format.ft = 0;
311 mips32_insn.fp0_format.fs =
312 insn.mm_fp3_format.fs;
313 mips32_insn.fp0_format.fd =
314 insn.mm_fp3_format.rt;
315 mips32_insn.fp0_format.func = func;
316 break;
317 case mm_ffloorl_op:
318 case mm_ffloorw_op:
319 case mm_fceill_op:
320 case mm_fceilw_op:
321 case mm_ftruncl_op:
322 case mm_ftruncw_op:
323 case mm_froundl_op:
324 case mm_froundw_op:
325 case mm_fcvtl_op:
326 case mm_fcvtw_op:
327 if (insn.mm_fp1_format.op == mm_ffloorl_op)
328 func = ffloorl_op;
329 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
330 func = ffloor_op;
331 else if (insn.mm_fp1_format.op == mm_fceill_op)
332 func = fceill_op;
333 else if (insn.mm_fp1_format.op == mm_fceilw_op)
334 func = fceil_op;
335 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
336 func = ftruncl_op;
337 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
338 func = ftrunc_op;
339 else if (insn.mm_fp1_format.op == mm_froundl_op)
340 func = froundl_op;
341 else if (insn.mm_fp1_format.op == mm_froundw_op)
342 func = fround_op;
343 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
344 func = fcvtl_op;
345 else
346 func = fcvtw_op;
347 mips32_insn.fp0_format.opcode = cop1_op;
348 mips32_insn.fp0_format.fmt =
349 sd_format[insn.mm_fp1_format.fmt];
350 mips32_insn.fp0_format.ft = 0;
351 mips32_insn.fp0_format.fs =
352 insn.mm_fp1_format.fs;
353 mips32_insn.fp0_format.fd =
354 insn.mm_fp1_format.rt;
355 mips32_insn.fp0_format.func = func;
356 break;
357 case mm_frsqrt_op:
358 case mm_fsqrt_op:
359 case mm_frecip_op:
360 if (insn.mm_fp1_format.op == mm_frsqrt_op)
361 func = frsqrt_op;
362 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
363 func = fsqrt_op;
364 else
365 func = frecip_op;
366 mips32_insn.fp0_format.opcode = cop1_op;
367 mips32_insn.fp0_format.fmt =
368 sdps_format[insn.mm_fp1_format.fmt];
369 mips32_insn.fp0_format.ft = 0;
370 mips32_insn.fp0_format.fs =
371 insn.mm_fp1_format.fs;
372 mips32_insn.fp0_format.fd =
373 insn.mm_fp1_format.rt;
374 mips32_insn.fp0_format.func = func;
375 break;
376 case mm_mfc1_op:
377 case mm_mtc1_op:
378 case mm_cfc1_op:
379 case mm_ctc1_op:
9355e59c
SH
380 case mm_mfhc1_op:
381 case mm_mthc1_op:
102cedc3
LY
382 if (insn.mm_fp1_format.op == mm_mfc1_op)
383 op = mfc_op;
384 else if (insn.mm_fp1_format.op == mm_mtc1_op)
385 op = mtc_op;
386 else if (insn.mm_fp1_format.op == mm_cfc1_op)
387 op = cfc_op;
9355e59c 388 else if (insn.mm_fp1_format.op == mm_ctc1_op)
102cedc3 389 op = ctc_op;
9355e59c
SH
390 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
391 op = mfhc_op;
392 else
393 op = mthc_op;
102cedc3
LY
394 mips32_insn.fp1_format.opcode = cop1_op;
395 mips32_insn.fp1_format.op = op;
396 mips32_insn.fp1_format.rt =
397 insn.mm_fp1_format.rt;
398 mips32_insn.fp1_format.fs =
399 insn.mm_fp1_format.fs;
400 mips32_insn.fp1_format.fd = 0;
401 mips32_insn.fp1_format.func = 0;
402 break;
403 default:
404 return SIGILL;
102cedc3
LY
405 }
406 break;
407 case mm_32f_74_op: /* c.cond.fmt */
408 mips32_insn.fp0_format.opcode = cop1_op;
409 mips32_insn.fp0_format.fmt =
410 sdps_format[insn.mm_fp4_format.fmt];
411 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
412 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
413 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
414 mips32_insn.fp0_format.func =
415 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
416 break;
417 default:
418 return SIGILL;
102cedc3
LY
419 }
420 break;
421 default:
422 return SIGILL;
102cedc3
LY
423 }
424
425 *insn_ptr = mips32_insn;
426 return 0;
427}
428
1da177e4
LT
429/*
430 * Redundant with logic already in kernel/branch.c,
431 * embedded in compute_return_epc. At some point,
432 * a single subroutine should be used across both
433 * modules.
434 */
102cedc3
LY
435static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
436 unsigned long *contpc)
1da177e4 437{
102cedc3
LY
438 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
439 unsigned int fcr31;
440 unsigned int bit = 0;
441
442 switch (insn.i_format.opcode) {
1da177e4 443 case spec_op:
102cedc3 444 switch (insn.r_format.func) {
1da177e4 445 case jalr_op:
102cedc3
LY
446 regs->regs[insn.r_format.rd] =
447 regs->cp0_epc + dec_insn.pc_inc +
448 dec_insn.next_pc_inc;
449 /* Fall through */
1da177e4 450 case jr_op:
5f9f41c4
MC
451 /* For R6, JR already emulated in jalr_op */
452 if (NO_R6EMU && insn.r_format.opcode == jr_op)
453 break;
102cedc3 454 *contpc = regs->regs[insn.r_format.rs];
1da177e4
LT
455 return 1;
456 }
457 break;
1da177e4 458 case bcond_op:
102cedc3
LY
459 switch (insn.i_format.rt) {
460 case bltzal_op:
461 case bltzall_op:
319824ea
MC
462 if (NO_R6EMU && (insn.i_format.rs ||
463 insn.i_format.rt == bltzall_op))
464 break;
465
102cedc3
LY
466 regs->regs[31] = regs->cp0_epc +
467 dec_insn.pc_inc +
468 dec_insn.next_pc_inc;
469 /* Fall through */
1da177e4 470 case bltzl_op:
319824ea
MC
471 if (NO_R6EMU)
472 break;
473 case bltz_op:
102cedc3
LY
474 if ((long)regs->regs[insn.i_format.rs] < 0)
475 *contpc = regs->cp0_epc +
476 dec_insn.pc_inc +
477 (insn.i_format.simmediate << 2);
478 else
479 *contpc = regs->cp0_epc +
480 dec_insn.pc_inc +
481 dec_insn.next_pc_inc;
482 return 1;
1da177e4 483 case bgezal_op:
1da177e4 484 case bgezall_op:
319824ea
MC
485 if (NO_R6EMU && (insn.i_format.rs ||
486 insn.i_format.rt == bgezall_op))
487 break;
488
102cedc3
LY
489 regs->regs[31] = regs->cp0_epc +
490 dec_insn.pc_inc +
491 dec_insn.next_pc_inc;
492 /* Fall through */
102cedc3 493 case bgezl_op:
319824ea
MC
494 if (NO_R6EMU)
495 break;
496 case bgez_op:
102cedc3
LY
497 if ((long)regs->regs[insn.i_format.rs] >= 0)
498 *contpc = regs->cp0_epc +
499 dec_insn.pc_inc +
500 (insn.i_format.simmediate << 2);
501 else
502 *contpc = regs->cp0_epc +
503 dec_insn.pc_inc +
504 dec_insn.next_pc_inc;
1da177e4
LT
505 return 1;
506 }
507 break;
1da177e4 508 case jalx_op:
102cedc3
LY
509 set_isa16_mode(bit);
510 case jal_op:
511 regs->regs[31] = regs->cp0_epc +
512 dec_insn.pc_inc +
513 dec_insn.next_pc_inc;
514 /* Fall through */
515 case j_op:
516 *contpc = regs->cp0_epc + dec_insn.pc_inc;
517 *contpc >>= 28;
518 *contpc <<= 28;
519 *contpc |= (insn.j_format.target << 2);
520 /* Set microMIPS mode bit: XOR for jalx. */
521 *contpc ^= bit;
522 return 1;
1da177e4 523 case beql_op:
319824ea
MC
524 if (NO_R6EMU)
525 break;
526 case beq_op:
102cedc3
LY
527 if (regs->regs[insn.i_format.rs] ==
528 regs->regs[insn.i_format.rt])
529 *contpc = regs->cp0_epc +
530 dec_insn.pc_inc +
531 (insn.i_format.simmediate << 2);
532 else
533 *contpc = regs->cp0_epc +
534 dec_insn.pc_inc +
535 dec_insn.next_pc_inc;
536 return 1;
1da177e4 537 case bnel_op:
319824ea
MC
538 if (NO_R6EMU)
539 break;
540 case bne_op:
102cedc3
LY
541 if (regs->regs[insn.i_format.rs] !=
542 regs->regs[insn.i_format.rt])
543 *contpc = regs->cp0_epc +
544 dec_insn.pc_inc +
545 (insn.i_format.simmediate << 2);
546 else
547 *contpc = regs->cp0_epc +
548 dec_insn.pc_inc +
549 dec_insn.next_pc_inc;
550 return 1;
1da177e4 551 case blezl_op:
319824ea
MC
552 if (NO_R6EMU)
553 break;
554 case blez_op:
a8ff66f5
MC
555
556 /*
557 * Compact branches for R6 for the
558 * blez and blezl opcodes.
559 * BLEZ | rs = 0 | rt != 0 == BLEZALC
560 * BLEZ | rs = rt != 0 == BGEZALC
561 * BLEZ | rs != 0 | rt != 0 == BGEUC
562 * BLEZL | rs = 0 | rt != 0 == BLEZC
563 * BLEZL | rs = rt != 0 == BGEZC
564 * BLEZL | rs != 0 | rt != 0 == BGEC
565 *
566 * For real BLEZ{,L}, rt is always 0.
567 */
568 if (cpu_has_mips_r6 && insn.i_format.rt) {
569 if ((insn.i_format.opcode == blez_op) &&
570 ((!insn.i_format.rs && insn.i_format.rt) ||
571 (insn.i_format.rs == insn.i_format.rt)))
572 regs->regs[31] = regs->cp0_epc +
573 dec_insn.pc_inc;
574 *contpc = regs->cp0_epc + dec_insn.pc_inc +
575 dec_insn.next_pc_inc;
576
577 return 1;
578 }
102cedc3
LY
579 if ((long)regs->regs[insn.i_format.rs] <= 0)
580 *contpc = regs->cp0_epc +
581 dec_insn.pc_inc +
582 (insn.i_format.simmediate << 2);
583 else
584 *contpc = regs->cp0_epc +
585 dec_insn.pc_inc +
586 dec_insn.next_pc_inc;
587 return 1;
1da177e4 588 case bgtzl_op:
319824ea
MC
589 if (NO_R6EMU)
590 break;
591 case bgtz_op:
f1b44067
MC
592 /*
593 * Compact branches for R6 for the
594 * bgtz and bgtzl opcodes.
595 * BGTZ | rs = 0 | rt != 0 == BGTZALC
596 * BGTZ | rs = rt != 0 == BLTZALC
597 * BGTZ | rs != 0 | rt != 0 == BLTUC
598 * BGTZL | rs = 0 | rt != 0 == BGTZC
599 * BGTZL | rs = rt != 0 == BLTZC
600 * BGTZL | rs != 0 | rt != 0 == BLTC
601 *
602 * *ZALC varint for BGTZ &&& rt != 0
603 * For real GTZ{,L}, rt is always 0.
604 */
605 if (cpu_has_mips_r6 && insn.i_format.rt) {
606 if ((insn.i_format.opcode == blez_op) &&
607 ((!insn.i_format.rs && insn.i_format.rt) ||
608 (insn.i_format.rs == insn.i_format.rt)))
609 regs->regs[31] = regs->cp0_epc +
610 dec_insn.pc_inc;
611 *contpc = regs->cp0_epc + dec_insn.pc_inc +
612 dec_insn.next_pc_inc;
613
614 return 1;
615 }
616
102cedc3
LY
617 if ((long)regs->regs[insn.i_format.rs] > 0)
618 *contpc = regs->cp0_epc +
619 dec_insn.pc_inc +
620 (insn.i_format.simmediate << 2);
621 else
622 *contpc = regs->cp0_epc +
623 dec_insn.pc_inc +
624 dec_insn.next_pc_inc;
1da177e4 625 return 1;
c893ce38 626 case cbcond0_op:
10d962d5 627 case cbcond1_op:
c893ce38
MC
628 if (!cpu_has_mips_r6)
629 break;
630 if (insn.i_format.rt && !insn.i_format.rs)
631 regs->regs[31] = regs->cp0_epc + 4;
632 *contpc = regs->cp0_epc + dec_insn.pc_inc +
633 dec_insn.next_pc_inc;
634
635 return 1;
c26d4219
DD
636#ifdef CONFIG_CPU_CAVIUM_OCTEON
637 case lwc2_op: /* This is bbit0 on Octeon */
638 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
639 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
640 else
641 *contpc = regs->cp0_epc + 8;
642 return 1;
643 case ldc2_op: /* This is bbit032 on Octeon */
644 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
645 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
646 else
647 *contpc = regs->cp0_epc + 8;
648 return 1;
649 case swc2_op: /* This is bbit1 on Octeon */
650 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
651 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
652 else
653 *contpc = regs->cp0_epc + 8;
654 return 1;
655 case sdc2_op: /* This is bbit132 on Octeon */
656 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
657 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
658 else
659 *contpc = regs->cp0_epc + 8;
660 return 1;
8467ca01
MC
661#else
662 case bc6_op:
663 /*
664 * Only valid for MIPS R6 but we can still end up
665 * here from a broken userland so just tell emulator
666 * this is not a branch and let it break later on.
667 */
668 if (!cpu_has_mips_r6)
669 break;
670 *contpc = regs->cp0_epc + dec_insn.pc_inc +
671 dec_insn.next_pc_inc;
672
84fef630
MC
673 return 1;
674 case balc6_op:
675 if (!cpu_has_mips_r6)
676 break;
677 regs->regs[31] = regs->cp0_epc + 4;
678 *contpc = regs->cp0_epc + dec_insn.pc_inc +
679 dec_insn.next_pc_inc;
680
69b9a2fd
MC
681 return 1;
682 case beqzcjic_op:
683 if (!cpu_has_mips_r6)
684 break;
685 *contpc = regs->cp0_epc + dec_insn.pc_inc +
686 dec_insn.next_pc_inc;
687
8467ca01 688 return 1;
c26d4219 689#endif
1da177e4
LT
690 case cop0_op:
691 case cop1_op:
c8a34581
MC
692 /* Need to check for R6 bc1nez and bc1eqz branches */
693 if (cpu_has_mips_r6 &&
694 ((insn.i_format.rs == bc1eqz_op) ||
695 (insn.i_format.rs == bc1nez_op))) {
696 bit = 0;
697 switch (insn.i_format.rs) {
698 case bc1eqz_op:
699 if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
700 bit = 1;
701 break;
702 case bc1nez_op:
703 if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
704 bit = 1;
705 break;
706 }
707 if (bit)
708 *contpc = regs->cp0_epc +
709 dec_insn.pc_inc +
710 (insn.i_format.simmediate << 2);
711 else
712 *contpc = regs->cp0_epc +
713 dec_insn.pc_inc +
714 dec_insn.next_pc_inc;
715
716 return 1;
717 }
718 /* R2/R6 compatible cop1 instruction. Fall through */
1da177e4
LT
719 case cop2_op:
720 case cop1x_op:
102cedc3
LY
721 if (insn.i_format.rs == bc_op) {
722 preempt_disable();
723 if (is_fpu_owner())
842dfc11 724 fcr31 = read_32bit_cp1_register(CP1_STATUS);
102cedc3
LY
725 else
726 fcr31 = current->thread.fpu.fcr31;
727 preempt_enable();
728
729 bit = (insn.i_format.rt >> 2);
730 bit += (bit != 0);
731 bit += 23;
732 switch (insn.i_format.rt & 3) {
733 case 0: /* bc1f */
734 case 2: /* bc1fl */
735 if (~fcr31 & (1 << bit))
736 *contpc = regs->cp0_epc +
737 dec_insn.pc_inc +
738 (insn.i_format.simmediate << 2);
739 else
740 *contpc = regs->cp0_epc +
741 dec_insn.pc_inc +
742 dec_insn.next_pc_inc;
743 return 1;
102cedc3
LY
744 case 1: /* bc1t */
745 case 3: /* bc1tl */
746 if (fcr31 & (1 << bit))
747 *contpc = regs->cp0_epc +
748 dec_insn.pc_inc +
749 (insn.i_format.simmediate << 2);
750 else
751 *contpc = regs->cp0_epc +
752 dec_insn.pc_inc +
753 dec_insn.next_pc_inc;
754 return 1;
102cedc3
LY
755 }
756 }
1da177e4
LT
757 break;
758 }
1da177e4
LT
759 return 0;
760}
761
762/*
763 * In the Linux kernel, we support selection of FPR format on the
70342287 764 * basis of the Status.FR bit. If an FPU is not present, the FR bit
da0bac33 765 * is hardwired to zero, which would imply a 32-bit FPU even for
597ce172 766 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
51d943f0
RB
767 * FPU emu is slow and bulky and optimizing this function offers fairly
768 * sizeable benefits so we try to be clever and make this function return
769 * a constant whenever possible, that is on 64-bit kernels without O32
597ce172 770 * compatibility enabled and on 32-bit without 64-bit FPU support.
1da177e4 771 */
da0bac33
DD
772static inline int cop1_64bit(struct pt_regs *xcp)
773{
08a07904
RB
774 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
775 return 1;
776 else if (config_enabled(CONFIG_32BIT) &&
777 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
778 return 0;
779
597ce172 780 return !test_thread_flag(TIF_32BIT_FPREGS);
da0bac33
DD
781}
782
4227a2d4
PB
783static inline bool hybrid_fprs(void)
784{
785 return test_thread_flag(TIF_HYBRID_FPREGS);
786}
787
47fa0c02
RB
788#define SIFROMREG(si, x) \
789do { \
4227a2d4 790 if (cop1_64bit(xcp) && !hybrid_fprs()) \
c8c0da6b 791 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
bbd426f5 792 else \
c8c0da6b 793 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
bbd426f5 794} while (0)
1da177e4 795
47fa0c02
RB
796#define SITOREG(si, x) \
797do { \
4227a2d4 798 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
ef1c47af 799 unsigned i; \
bbd426f5 800 set_fpr32(&ctx->fpr[x], 0, si); \
ef1c47af
PB
801 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
802 set_fpr32(&ctx->fpr[x], i, 0); \
803 } else { \
bbd426f5 804 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
ef1c47af 805 } \
bbd426f5 806} while (0)
1da177e4 807
c8c0da6b 808#define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
ef1c47af 809
47fa0c02
RB
810#define SITOHREG(si, x) \
811do { \
ef1c47af
PB
812 unsigned i; \
813 set_fpr32(&ctx->fpr[x], 1, si); \
814 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
815 set_fpr32(&ctx->fpr[x], i, 0); \
816} while (0)
1ac94400 817
47fa0c02 818#define DIFROMREG(di, x) \
bbd426f5
PB
819 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
820
47fa0c02
RB
821#define DITOREG(di, x) \
822do { \
ef1c47af
PB
823 unsigned fpr, i; \
824 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
825 set_fpr64(&ctx->fpr[fpr], 0, di); \
826 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
827 set_fpr64(&ctx->fpr[fpr], i, 0); \
828} while (0)
1da177e4 829
21a151d8
RB
830#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
831#define SPTOREG(sp, x) SITOREG((sp).bits, x)
832#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
833#define DPTOREG(dp, x) DITOREG((dp).bits, x)
1da177e4
LT
834
835/*
836 * Emulate the single floating point instruction pointed at by EPC.
837 * Two instructions if the instruction is in a branch delay slot.
838 */
839
515b029d 840static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
102cedc3 841 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
1da177e4 842{
102cedc3 843 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
3f7cac41
RB
844 unsigned int cond, cbit;
845 mips_instruction ir;
846 int likely, pc_inc;
847 u32 __user *wva;
848 u64 __user *dva;
849 u32 value;
850 u32 wval;
851 u64 dval;
852 int sig;
1da177e4 853
70e4c234
RB
854 /*
855 * These are giving gcc a gentle hint about what to expect in
856 * dec_inst in order to do better optimization.
857 */
858 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
859 unreachable();
860
1da177e4 861 /* XXX NEC Vr54xx bug workaround */
e7e9cae5 862 if (delay_slot(xcp)) {
102cedc3
LY
863 if (dec_insn.micro_mips_mode) {
864 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
e7e9cae5 865 clear_delay_slot(xcp);
102cedc3
LY
866 } else {
867 if (!isBranchInstr(xcp, dec_insn, &contpc))
e7e9cae5 868 clear_delay_slot(xcp);
102cedc3
LY
869 }
870 }
1da177e4 871
e7e9cae5 872 if (delay_slot(xcp)) {
1da177e4
LT
873 /*
874 * The instruction to be emulated is in a branch delay slot
70342287 875 * which means that we have to emulate the branch instruction
1da177e4
LT
876 * BEFORE we do the cop1 instruction.
877 *
878 * This branch could be a COP1 branch, but in that case we
879 * would have had a trap for that instruction, and would not
880 * come through this route.
881 *
882 * Linux MIPS branch emulator operates on context, updating the
883 * cp0_epc.
884 */
102cedc3
LY
885 ir = dec_insn.next_insn; /* process delay slot instr */
886 pc_inc = dec_insn.next_pc_inc;
887 } else {
888 ir = dec_insn.insn; /* process current instr */
889 pc_inc = dec_insn.pc_inc;
890 }
1da177e4 891
102cedc3
LY
892 /*
893 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
894 * instructions, we want to convert microMIPS FPU instructions
895 * into MIPS32 instructions so that we could reuse all of the
896 * FPU emulation code.
897 *
898 * NOTE: We cannot do this for branch instructions since they
899 * are not a subset. Example: Cannot emulate a 16-bit
900 * aligned target address with a MIPS32 instruction.
901 */
902 if (dec_insn.micro_mips_mode) {
903 /*
904 * If next instruction is a 16-bit instruction, then it
905 * it cannot be a FPU instruction. This could happen
906 * since we can be called for non-FPU instructions.
907 */
908 if ((pc_inc == 2) ||
909 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
910 == SIGILL))
1da177e4 911 return SIGILL;
1da177e4
LT
912 }
913
3f7cac41 914emul:
a8b0ca17 915 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
b6ee75ed 916 MIPS_FPU_EMU_INC_STATS(emulated);
1da177e4 917 switch (MIPSInst_OPCODE(ir)) {
3f7cac41
RB
918 case ldc1_op:
919 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
920 MIPSInst_SIMM(ir));
b6ee75ed 921 MIPS_FPU_EMU_INC_STATS(loads);
515b029d 922
3f7cac41 923 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
b6ee75ed 924 MIPS_FPU_EMU_INC_STATS(errors);
3f7cac41 925 *fault_addr = dva;
1da177e4
LT
926 return SIGBUS;
927 }
3f7cac41 928 if (__get_user(dval, dva)) {
515b029d 929 MIPS_FPU_EMU_INC_STATS(errors);
3f7cac41 930 *fault_addr = dva;
515b029d
DD
931 return SIGSEGV;
932 }
3f7cac41 933 DITOREG(dval, MIPSInst_RT(ir));
1da177e4 934 break;
1da177e4 935
3f7cac41
RB
936 case sdc1_op:
937 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
938 MIPSInst_SIMM(ir));
b6ee75ed 939 MIPS_FPU_EMU_INC_STATS(stores);
3f7cac41
RB
940 DIFROMREG(dval, MIPSInst_RT(ir));
941 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
b6ee75ed 942 MIPS_FPU_EMU_INC_STATS(errors);
3f7cac41 943 *fault_addr = dva;
1da177e4
LT
944 return SIGBUS;
945 }
3f7cac41 946 if (__put_user(dval, dva)) {
515b029d 947 MIPS_FPU_EMU_INC_STATS(errors);
3f7cac41 948 *fault_addr = dva;
515b029d
DD
949 return SIGSEGV;
950 }
1da177e4 951 break;
1da177e4 952
3f7cac41
RB
953 case lwc1_op:
954 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
955 MIPSInst_SIMM(ir));
b6ee75ed 956 MIPS_FPU_EMU_INC_STATS(loads);
3f7cac41 957 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
b6ee75ed 958 MIPS_FPU_EMU_INC_STATS(errors);
3f7cac41 959 *fault_addr = wva;
1da177e4
LT
960 return SIGBUS;
961 }
3f7cac41 962 if (__get_user(wval, wva)) {
515b029d 963 MIPS_FPU_EMU_INC_STATS(errors);
3f7cac41 964 *fault_addr = wva;
515b029d
DD
965 return SIGSEGV;
966 }
3f7cac41 967 SITOREG(wval, MIPSInst_RT(ir));
1da177e4 968 break;
1da177e4 969
3f7cac41
RB
970 case swc1_op:
971 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
972 MIPSInst_SIMM(ir));
b6ee75ed 973 MIPS_FPU_EMU_INC_STATS(stores);
3f7cac41
RB
974 SIFROMREG(wval, MIPSInst_RT(ir));
975 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
b6ee75ed 976 MIPS_FPU_EMU_INC_STATS(errors);
3f7cac41 977 *fault_addr = wva;
1da177e4
LT
978 return SIGBUS;
979 }
3f7cac41 980 if (__put_user(wval, wva)) {
515b029d 981 MIPS_FPU_EMU_INC_STATS(errors);
3f7cac41 982 *fault_addr = wva;
515b029d
DD
983 return SIGSEGV;
984 }
1da177e4 985 break;
1da177e4
LT
986
987 case cop1_op:
988 switch (MIPSInst_RS(ir)) {
1da177e4 989 case dmfc_op:
08a07904
RB
990 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
991 return SIGILL;
992
1da177e4
LT
993 /* copregister fs -> gpr[rt] */
994 if (MIPSInst_RT(ir) != 0) {
995 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
996 MIPSInst_RD(ir));
997 }
998 break;
999
1000 case dmtc_op:
08a07904
RB
1001 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1002 return SIGILL;
1003
1da177e4
LT
1004 /* copregister fs <- rt */
1005 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1006 break;
1da177e4 1007
1ac94400
LY
1008 case mfhc_op:
1009 if (!cpu_has_mips_r2)
1010 goto sigill;
1011
1012 /* copregister rd -> gpr[rt] */
1013 if (MIPSInst_RT(ir) != 0) {
1014 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1015 MIPSInst_RD(ir));
1016 }
1017 break;
1018
1019 case mthc_op:
1020 if (!cpu_has_mips_r2)
1021 goto sigill;
1022
1023 /* copregister rd <- gpr[rt] */
1024 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1025 break;
1026
1da177e4
LT
1027 case mfc_op:
1028 /* copregister rd -> gpr[rt] */
1da177e4
LT
1029 if (MIPSInst_RT(ir) != 0) {
1030 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1031 MIPSInst_RD(ir));
1032 }
1033 break;
1034
1035 case mtc_op:
1036 /* copregister rd <- rt */
1da177e4
LT
1037 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1038 break;
1039
3f7cac41 1040 case cfc_op:
1da177e4 1041 /* cop control register rd -> gpr[rt] */
1da177e4
LT
1042 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1043 value = ctx->fcr31;
56a64733 1044 value = (value & ~FPU_CSR_RM) | modeindex(value);
92df0f8b
RB
1045 pr_debug("%p gpr[%d]<-csr=%08x\n",
1046 (void *) (xcp->cp0_epc),
1047 MIPSInst_RT(ir), value);
1da177e4
LT
1048 }
1049 else if (MIPSInst_RD(ir) == FPCREG_RID)
1050 value = 0;
1051 else
1052 value = 0;
1053 if (MIPSInst_RT(ir))
1054 xcp->regs[MIPSInst_RT(ir)] = value;
1055 break;
1da177e4 1056
3f7cac41 1057 case ctc_op:
1da177e4 1058 /* copregister rd <- rt */
1da177e4
LT
1059 if (MIPSInst_RT(ir) == 0)
1060 value = 0;
1061 else
1062 value = xcp->regs[MIPSInst_RT(ir)];
1063
1064 /* we only have one writable control reg
1065 */
1066 if (MIPSInst_RD(ir) == FPCREG_CSR) {
92df0f8b
RB
1067 pr_debug("%p gpr[%d]->csr=%08x\n",
1068 (void *) (xcp->cp0_epc),
1069 MIPSInst_RT(ir), value);
95e8f634
SM
1070
1071 /*
1072 * Don't write reserved bits,
1073 * and convert to ieee library modes
1074 */
56a64733
RB
1075 ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
1076 modeindex(value);
1da177e4
LT
1077 }
1078 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1079 return SIGFPE;
1080 }
1081 break;
1da177e4 1082
3f7cac41 1083 case bc_op:
e7e9cae5 1084 if (delay_slot(xcp))
1da177e4
LT
1085 return SIGILL;
1086
08a07904
RB
1087 if (cpu_has_mips_4_5_r)
1088 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1089 else
1090 cbit = FPU_CSR_COND;
1091 cond = ctx->fcr31 & cbit;
1092
3f7cac41 1093 likely = 0;
1da177e4
LT
1094 switch (MIPSInst_RT(ir) & 3) {
1095 case bcfl_op:
1096 likely = 1;
1097 case bcf_op:
1098 cond = !cond;
1099 break;
1100 case bctl_op:
1101 likely = 1;
1102 case bct_op:
1103 break;
1104 default:
1105 /* thats an illegal instruction */
1106 return SIGILL;
1107 }
1108
e7e9cae5 1109 set_delay_slot(xcp);
1da177e4 1110 if (cond) {
3f7cac41
RB
1111 /*
1112 * Branch taken: emulate dslot instruction
1da177e4 1113 */
102cedc3
LY
1114 xcp->cp0_epc += dec_insn.pc_inc;
1115
1116 contpc = MIPSInst_SIMM(ir);
1117 ir = dec_insn.next_insn;
1118 if (dec_insn.micro_mips_mode) {
1119 contpc = (xcp->cp0_epc + (contpc << 1));
1120
1121 /* If 16-bit instruction, not FPU. */
1122 if ((dec_insn.next_pc_inc == 2) ||
1123 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1124
1125 /*
1126 * Since this instruction will
1127 * be put on the stack with
1128 * 32-bit words, get around
1129 * this problem by putting a
1130 * NOP16 as the second one.
1131 */
1132 if (dec_insn.next_pc_inc == 2)
1133 ir = (ir & (~0xffff)) | MM_NOP16;
1134
1135 /*
1136 * Single step the non-CP1
1137 * instruction in the dslot.
1138 */
1139 return mips_dsemul(xcp, ir, contpc);
1140 }
1141 } else
1142 contpc = (xcp->cp0_epc + (contpc << 2));
1da177e4
LT
1143
1144 switch (MIPSInst_OPCODE(ir)) {
1145 case lwc1_op:
08a07904 1146 goto emul;
3f7cac41 1147
1da177e4 1148 case swc1_op:
08a07904 1149 goto emul;
3f7cac41 1150
1da177e4
LT
1151 case ldc1_op:
1152 case sdc1_op:
08a07904
RB
1153 if (cpu_has_mips_2_3_4_5 ||
1154 cpu_has_mips64)
1155 goto emul;
1156
1157 return SIGILL;
1158 goto emul;
3f7cac41 1159
1da177e4 1160 case cop1_op:
1da177e4 1161 goto emul;
3f7cac41 1162
08a07904 1163 case cop1x_op:
a5466d7b 1164 if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
08a07904
RB
1165 /* its one of ours */
1166 goto emul;
1167
1168 return SIGILL;
3f7cac41 1169
1da177e4 1170 case spec_op:
08a07904
RB
1171 if (!cpu_has_mips_4_5_r)
1172 return SIGILL;
1173
1da177e4
LT
1174 if (MIPSInst_FUNC(ir) == movc_op)
1175 goto emul;
1176 break;
1da177e4
LT
1177 }
1178
1179 /*
1180 * Single step the non-cp1
1181 * instruction in the dslot
1182 */
e70dfc10 1183 return mips_dsemul(xcp, ir, contpc);
3f7cac41 1184 } else if (likely) { /* branch not taken */
1da177e4
LT
1185 /*
1186 * branch likely nullifies
1187 * dslot if not taken
1188 */
102cedc3
LY
1189 xcp->cp0_epc += dec_insn.pc_inc;
1190 contpc += dec_insn.pc_inc;
1da177e4
LT
1191 /*
1192 * else continue & execute
1193 * dslot as normal insn
1194 */
1195 }
1da177e4 1196 break;
1da177e4
LT
1197
1198 default:
1199 if (!(MIPSInst_RS(ir) & 0x10))
1200 return SIGILL;
1da177e4 1201
3f7cac41
RB
1202 /* a real fpu computation instruction */
1203 if ((sig = fpu_emu(xcp, ctx, ir)))
1204 return sig;
1da177e4
LT
1205 }
1206 break;
1207
3f7cac41 1208 case cop1x_op:
a5466d7b 1209 if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
08a07904
RB
1210 return SIGILL;
1211
1212 sig = fpux_emu(xcp, ctx, ir, fault_addr);
515b029d 1213 if (sig)
1da177e4
LT
1214 return sig;
1215 break;
1da177e4 1216
1da177e4 1217 case spec_op:
08a07904
RB
1218 if (!cpu_has_mips_4_5_r)
1219 return SIGILL;
1220
1da177e4
LT
1221 if (MIPSInst_FUNC(ir) != movc_op)
1222 return SIGILL;
1223 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1224 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1225 xcp->regs[MIPSInst_RD(ir)] =
1226 xcp->regs[MIPSInst_RS(ir)];
1227 break;
1da177e4 1228 default:
1ac94400 1229sigill:
1da177e4
LT
1230 return SIGILL;
1231 }
1232
1233 /* we did it !! */
e70dfc10 1234 xcp->cp0_epc = contpc;
e7e9cae5 1235 clear_delay_slot(xcp);
333d1f67 1236
1da177e4
LT
1237 return 0;
1238}
1239
1240/*
1241 * Conversion table from MIPS compare ops 48-63
1242 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1243 */
1244static const unsigned char cmptab[8] = {
1245 0, /* cmp_0 (sig) cmp_sf */
1246 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1247 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1248 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1249 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1250 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1251 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1252 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1253};
1254
1255
1da177e4
LT
1256/*
1257 * Additional MIPS4 instructions
1258 */
1259
47fa0c02
RB
1260#define DEF3OP(name, p, f1, f2, f3) \
1261static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1262 union ieee754##p s, union ieee754##p t) \
1263{ \
1264 struct _ieee754_csr ieee754_csr_save; \
1265 s = f1(s, t); \
1266 ieee754_csr_save = ieee754_csr; \
1267 s = f2(s, r); \
1268 ieee754_csr_save.cx |= ieee754_csr.cx; \
1269 ieee754_csr_save.sx |= ieee754_csr.sx; \
1270 s = f3(s); \
1271 ieee754_csr.cx |= ieee754_csr_save.cx; \
1272 ieee754_csr.sx |= ieee754_csr_save.sx; \
1273 return s; \
1da177e4
LT
1274}
1275
2209bcb1 1276static union ieee754dp fpemu_dp_recip(union ieee754dp d)
1da177e4
LT
1277{
1278 return ieee754dp_div(ieee754dp_one(0), d);
1279}
1280
2209bcb1 1281static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
1da177e4
LT
1282{
1283 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1284}
1285
2209bcb1 1286static union ieee754sp fpemu_sp_recip(union ieee754sp s)
1da177e4
LT
1287{
1288 return ieee754sp_div(ieee754sp_one(0), s);
1289}
1290
2209bcb1 1291static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
1da177e4
LT
1292{
1293 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1294}
1295
21a151d8
RB
1296DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1297DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
1da177e4
LT
1298DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1299DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
21a151d8
RB
1300DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1301DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
1da177e4
LT
1302DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1303DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1304
eae89076 1305static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
515b029d 1306 mips_instruction ir, void *__user *fault_addr)
1da177e4
LT
1307{
1308 unsigned rcsr = 0; /* resulting csr */
1309
b6ee75ed 1310 MIPS_FPU_EMU_INC_STATS(cp1xops);
1da177e4
LT
1311
1312 switch (MIPSInst_FMA_FFMT(ir)) {
1313 case s_fmt:{ /* 0 */
1314
2209bcb1
RB
1315 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1316 union ieee754sp fd, fr, fs, ft;
3fccc015 1317 u32 __user *va;
1da177e4
LT
1318 u32 val;
1319
1320 switch (MIPSInst_FUNC(ir)) {
1321 case lwxc1_op:
3fccc015 1322 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1da177e4
LT
1323 xcp->regs[MIPSInst_FT(ir)]);
1324
b6ee75ed 1325 MIPS_FPU_EMU_INC_STATS(loads);
515b029d 1326 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
b6ee75ed 1327 MIPS_FPU_EMU_INC_STATS(errors);
515b029d 1328 *fault_addr = va;
1da177e4
LT
1329 return SIGBUS;
1330 }
515b029d
DD
1331 if (__get_user(val, va)) {
1332 MIPS_FPU_EMU_INC_STATS(errors);
1333 *fault_addr = va;
1334 return SIGSEGV;
1335 }
1da177e4
LT
1336 SITOREG(val, MIPSInst_FD(ir));
1337 break;
1338
1339 case swxc1_op:
3fccc015 1340 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1da177e4
LT
1341 xcp->regs[MIPSInst_FT(ir)]);
1342
b6ee75ed 1343 MIPS_FPU_EMU_INC_STATS(stores);
1da177e4
LT
1344
1345 SIFROMREG(val, MIPSInst_FS(ir));
515b029d 1346 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
b6ee75ed 1347 MIPS_FPU_EMU_INC_STATS(errors);
515b029d 1348 *fault_addr = va;
1da177e4
LT
1349 return SIGBUS;
1350 }
515b029d
DD
1351 if (put_user(val, va)) {
1352 MIPS_FPU_EMU_INC_STATS(errors);
1353 *fault_addr = va;
1354 return SIGSEGV;
1355 }
1da177e4
LT
1356 break;
1357
1358 case madd_s_op:
1359 handler = fpemu_sp_madd;
1360 goto scoptop;
1361 case msub_s_op:
1362 handler = fpemu_sp_msub;
1363 goto scoptop;
1364 case nmadd_s_op:
1365 handler = fpemu_sp_nmadd;
1366 goto scoptop;
1367 case nmsub_s_op:
1368 handler = fpemu_sp_nmsub;
1369 goto scoptop;
1370
1371 scoptop:
1372 SPFROMREG(fr, MIPSInst_FR(ir));
1373 SPFROMREG(fs, MIPSInst_FS(ir));
1374 SPFROMREG(ft, MIPSInst_FT(ir));
1375 fd = (*handler) (fr, fs, ft);
1376 SPTOREG(fd, MIPSInst_FD(ir));
1377
1378 copcsr:
c4103526
DCZ
1379 if (ieee754_cxtest(IEEE754_INEXACT)) {
1380 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
1da177e4 1381 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
c4103526
DCZ
1382 }
1383 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1384 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
1da177e4 1385 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
c4103526
DCZ
1386 }
1387 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1388 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
1da177e4 1389 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
c4103526
DCZ
1390 }
1391 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1392 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
1da177e4 1393 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
c4103526 1394 }
1da177e4
LT
1395
1396 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1da177e4 1397 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
3f7cac41 1398 /*printk ("SIGFPE: FPU csr = %08x\n",
1da177e4
LT
1399 ctx->fcr31); */
1400 return SIGFPE;
1401 }
1402
1403 break;
1404
1405 default:
1406 return SIGILL;
1407 }
1408 break;
1409 }
1410
1da177e4 1411 case d_fmt:{ /* 1 */
2209bcb1
RB
1412 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1413 union ieee754dp fd, fr, fs, ft;
3fccc015 1414 u64 __user *va;
1da177e4
LT
1415 u64 val;
1416
1417 switch (MIPSInst_FUNC(ir)) {
1418 case ldxc1_op:
3fccc015 1419 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1da177e4
LT
1420 xcp->regs[MIPSInst_FT(ir)]);
1421
b6ee75ed 1422 MIPS_FPU_EMU_INC_STATS(loads);
515b029d 1423 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
b6ee75ed 1424 MIPS_FPU_EMU_INC_STATS(errors);
515b029d 1425 *fault_addr = va;
1da177e4
LT
1426 return SIGBUS;
1427 }
515b029d
DD
1428 if (__get_user(val, va)) {
1429 MIPS_FPU_EMU_INC_STATS(errors);
1430 *fault_addr = va;
1431 return SIGSEGV;
1432 }
1da177e4
LT
1433 DITOREG(val, MIPSInst_FD(ir));
1434 break;
1435
1436 case sdxc1_op:
3fccc015 1437 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1da177e4
LT
1438 xcp->regs[MIPSInst_FT(ir)]);
1439
b6ee75ed 1440 MIPS_FPU_EMU_INC_STATS(stores);
1da177e4 1441 DIFROMREG(val, MIPSInst_FS(ir));
515b029d 1442 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
b6ee75ed 1443 MIPS_FPU_EMU_INC_STATS(errors);
515b029d 1444 *fault_addr = va;
1da177e4
LT
1445 return SIGBUS;
1446 }
515b029d
DD
1447 if (__put_user(val, va)) {
1448 MIPS_FPU_EMU_INC_STATS(errors);
1449 *fault_addr = va;
1450 return SIGSEGV;
1451 }
1da177e4
LT
1452 break;
1453
1454 case madd_d_op:
1455 handler = fpemu_dp_madd;
1456 goto dcoptop;
1457 case msub_d_op:
1458 handler = fpemu_dp_msub;
1459 goto dcoptop;
1460 case nmadd_d_op:
1461 handler = fpemu_dp_nmadd;
1462 goto dcoptop;
1463 case nmsub_d_op:
1464 handler = fpemu_dp_nmsub;
1465 goto dcoptop;
1466
1467 dcoptop:
1468 DPFROMREG(fr, MIPSInst_FR(ir));
1469 DPFROMREG(fs, MIPSInst_FS(ir));
1470 DPFROMREG(ft, MIPSInst_FT(ir));
1471 fd = (*handler) (fr, fs, ft);
1472 DPTOREG(fd, MIPSInst_FD(ir));
1473 goto copcsr;
1474
1475 default:
1476 return SIGILL;
1477 }
1478 break;
1479 }
1da177e4 1480
51061b88
DCZ
1481 case 0x3:
1482 if (MIPSInst_FUNC(ir) != pfetch_op)
1da177e4 1483 return SIGILL;
51061b88 1484
1da177e4
LT
1485 /* ignore prefx operation */
1486 break;
1487
1488 default:
1489 return SIGILL;
1490 }
1491
1492 return 0;
1493}
1da177e4
LT
1494
1495
1496
1497/*
1498 * Emulate a single COP1 arithmetic instruction.
1499 */
eae89076 1500static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1da177e4
LT
1501 mips_instruction ir)
1502{
1503 int rfmt; /* resulting format */
1504 unsigned rcsr = 0; /* resulting csr */
3f7cac41
RB
1505 unsigned int oldrm;
1506 unsigned int cbit;
1da177e4
LT
1507 unsigned cond;
1508 union {
2209bcb1
RB
1509 union ieee754dp d;
1510 union ieee754sp s;
1da177e4 1511 int w;
1da177e4 1512 s64 l;
1da177e4 1513 } rv; /* resulting value */
3f7cac41 1514 u64 bits;
1da177e4 1515
b6ee75ed 1516 MIPS_FPU_EMU_INC_STATS(cp1ops);
1da177e4 1517 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
3f7cac41 1518 case s_fmt: { /* 0 */
1da177e4 1519 union {
2209bcb1
RB
1520 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1521 union ieee754sp(*u) (union ieee754sp);
1da177e4 1522 } handler;
3f7cac41 1523 union ieee754sp fs, ft;
1da177e4
LT
1524
1525 switch (MIPSInst_FUNC(ir)) {
1526 /* binary ops */
1527 case fadd_op:
1528 handler.b = ieee754sp_add;
1529 goto scopbop;
1530 case fsub_op:
1531 handler.b = ieee754sp_sub;
1532 goto scopbop;
1533 case fmul_op:
1534 handler.b = ieee754sp_mul;
1535 goto scopbop;
1536 case fdiv_op:
1537 handler.b = ieee754sp_div;
1538 goto scopbop;
1539
1540 /* unary ops */
1da177e4 1541 case fsqrt_op:
08a07904
RB
1542 if (!cpu_has_mips_4_5_r)
1543 return SIGILL;
1544
1da177e4
LT
1545 handler.u = ieee754sp_sqrt;
1546 goto scopuop;
3f7cac41 1547
08a07904
RB
1548 /*
1549 * Note that on some MIPS IV implementations such as the
1550 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1551 * achieve full IEEE-754 accuracy - however this emulator does.
1552 */
1da177e4 1553 case frsqrt_op:
08a07904
RB
1554 if (!cpu_has_mips_4_5_r2)
1555 return SIGILL;
1556
1da177e4
LT
1557 handler.u = fpemu_sp_rsqrt;
1558 goto scopuop;
3f7cac41 1559
1da177e4 1560 case frecip_op:
08a07904
RB
1561 if (!cpu_has_mips_4_5_r2)
1562 return SIGILL;
1563
1da177e4
LT
1564 handler.u = fpemu_sp_recip;
1565 goto scopuop;
08a07904 1566
1da177e4 1567 case fmovc_op:
08a07904
RB
1568 if (!cpu_has_mips_4_5_r)
1569 return SIGILL;
1570
1da177e4
LT
1571 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1572 if (((ctx->fcr31 & cond) != 0) !=
1573 ((MIPSInst_FT(ir) & 1) != 0))
1574 return 0;
1575 SPFROMREG(rv.s, MIPSInst_FS(ir));
1576 break;
3f7cac41 1577
1da177e4 1578 case fmovz_op:
08a07904
RB
1579 if (!cpu_has_mips_4_5_r)
1580 return SIGILL;
1581
1da177e4
LT
1582 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1583 return 0;
1584 SPFROMREG(rv.s, MIPSInst_FS(ir));
1585 break;
3f7cac41 1586
1da177e4 1587 case fmovn_op:
08a07904
RB
1588 if (!cpu_has_mips_4_5_r)
1589 return SIGILL;
1590
1da177e4
LT
1591 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1592 return 0;
1593 SPFROMREG(rv.s, MIPSInst_FS(ir));
1594 break;
3f7cac41 1595
1da177e4
LT
1596 case fabs_op:
1597 handler.u = ieee754sp_abs;
1598 goto scopuop;
3f7cac41 1599
1da177e4
LT
1600 case fneg_op:
1601 handler.u = ieee754sp_neg;
1602 goto scopuop;
3f7cac41 1603
1da177e4
LT
1604 case fmov_op:
1605 /* an easy one */
1606 SPFROMREG(rv.s, MIPSInst_FS(ir));
1607 goto copcsr;
1608
1609 /* binary op on handler */
3f7cac41
RB
1610scopbop:
1611 SPFROMREG(fs, MIPSInst_FS(ir));
1612 SPFROMREG(ft, MIPSInst_FT(ir));
1da177e4 1613
3f7cac41
RB
1614 rv.s = (*handler.b) (fs, ft);
1615 goto copcsr;
1616scopuop:
1617 SPFROMREG(fs, MIPSInst_FS(ir));
1618 rv.s = (*handler.u) (fs);
1619 goto copcsr;
1620copcsr:
c4103526
DCZ
1621 if (ieee754_cxtest(IEEE754_INEXACT)) {
1622 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
1da177e4 1623 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
c4103526
DCZ
1624 }
1625 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1626 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
1da177e4 1627 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
c4103526
DCZ
1628 }
1629 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1630 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
1da177e4 1631 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
c4103526
DCZ
1632 }
1633 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1634 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
1da177e4 1635 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
c4103526
DCZ
1636 }
1637 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1638 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
1da177e4 1639 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
c4103526 1640 }
1da177e4
LT
1641 break;
1642
1643 /* unary conv ops */
1644 case fcvts_op:
1645 return SIGILL; /* not defined */
1da177e4 1646
3f7cac41 1647 case fcvtd_op:
1da177e4
LT
1648 SPFROMREG(fs, MIPSInst_FS(ir));
1649 rv.d = ieee754dp_fsp(fs);
1650 rfmt = d_fmt;
1651 goto copcsr;
1da177e4 1652
3f7cac41 1653 case fcvtw_op:
1da177e4
LT
1654 SPFROMREG(fs, MIPSInst_FS(ir));
1655 rv.w = ieee754sp_tint(fs);
1656 rfmt = w_fmt;
1657 goto copcsr;
1da177e4 1658
1da177e4
LT
1659 case fround_op:
1660 case ftrunc_op:
1661 case fceil_op:
3f7cac41 1662 case ffloor_op:
08a07904
RB
1663 if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
1664 return SIGILL;
1665
3f7cac41 1666 oldrm = ieee754_csr.rm;
1da177e4 1667 SPFROMREG(fs, MIPSInst_FS(ir));
56a64733 1668 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1da177e4
LT
1669 rv.w = ieee754sp_tint(fs);
1670 ieee754_csr.rm = oldrm;
1671 rfmt = w_fmt;
1672 goto copcsr;
1da177e4 1673
3f7cac41 1674 case fcvtl_op:
08a07904
RB
1675 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1676 return SIGILL;
1677
1da177e4
LT
1678 SPFROMREG(fs, MIPSInst_FS(ir));
1679 rv.l = ieee754sp_tlong(fs);
1680 rfmt = l_fmt;
1681 goto copcsr;
1da177e4
LT
1682
1683 case froundl_op:
1684 case ftruncl_op:
1685 case fceill_op:
3f7cac41 1686 case ffloorl_op:
08a07904
RB
1687 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1688 return SIGILL;
1689
3f7cac41 1690 oldrm = ieee754_csr.rm;
1da177e4 1691 SPFROMREG(fs, MIPSInst_FS(ir));
56a64733 1692 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1da177e4
LT
1693 rv.l = ieee754sp_tlong(fs);
1694 ieee754_csr.rm = oldrm;
1695 rfmt = l_fmt;
1696 goto copcsr;
1da177e4
LT
1697
1698 default:
1699 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1700 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
2209bcb1 1701 union ieee754sp fs, ft;
1da177e4
LT
1702
1703 SPFROMREG(fs, MIPSInst_FS(ir));
1704 SPFROMREG(ft, MIPSInst_FT(ir));
1705 rv.w = ieee754sp_cmp(fs, ft,
1706 cmptab[cmpop & 0x7], cmpop & 0x8);
1707 rfmt = -1;
1708 if ((cmpop & 0x8) && ieee754_cxtest
1709 (IEEE754_INVALID_OPERATION))
1710 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1711 else
1712 goto copcsr;
1713
3f7cac41 1714 } else
1da177e4 1715 return SIGILL;
1da177e4
LT
1716 break;
1717 }
1718 break;
1719 }
1720
3f7cac41
RB
1721 case d_fmt: {
1722 union ieee754dp fs, ft;
1da177e4 1723 union {
2209bcb1
RB
1724 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1725 union ieee754dp(*u) (union ieee754dp);
1da177e4
LT
1726 } handler;
1727
1728 switch (MIPSInst_FUNC(ir)) {
1729 /* binary ops */
1730 case fadd_op:
1731 handler.b = ieee754dp_add;
1732 goto dcopbop;
1733 case fsub_op:
1734 handler.b = ieee754dp_sub;
1735 goto dcopbop;
1736 case fmul_op:
1737 handler.b = ieee754dp_mul;
1738 goto dcopbop;
1739 case fdiv_op:
1740 handler.b = ieee754dp_div;
1741 goto dcopbop;
1742
1743 /* unary ops */
1da177e4 1744 case fsqrt_op:
08a07904
RB
1745 if (!cpu_has_mips_2_3_4_5_r)
1746 return SIGILL;
1747
1da177e4
LT
1748 handler.u = ieee754dp_sqrt;
1749 goto dcopuop;
08a07904
RB
1750 /*
1751 * Note that on some MIPS IV implementations such as the
1752 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1753 * achieve full IEEE-754 accuracy - however this emulator does.
1754 */
1da177e4 1755 case frsqrt_op:
08a07904
RB
1756 if (!cpu_has_mips_4_5_r2)
1757 return SIGILL;
1758
1da177e4
LT
1759 handler.u = fpemu_dp_rsqrt;
1760 goto dcopuop;
1761 case frecip_op:
08a07904
RB
1762 if (!cpu_has_mips_4_5_r2)
1763 return SIGILL;
1764
1da177e4
LT
1765 handler.u = fpemu_dp_recip;
1766 goto dcopuop;
1da177e4 1767 case fmovc_op:
08a07904
RB
1768 if (!cpu_has_mips_4_5_r)
1769 return SIGILL;
1770
1da177e4
LT
1771 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1772 if (((ctx->fcr31 & cond) != 0) !=
1773 ((MIPSInst_FT(ir) & 1) != 0))
1774 return 0;
1775 DPFROMREG(rv.d, MIPSInst_FS(ir));
1776 break;
1777 case fmovz_op:
08a07904
RB
1778 if (!cpu_has_mips_4_5_r)
1779 return SIGILL;
1780
1da177e4
LT
1781 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1782 return 0;
1783 DPFROMREG(rv.d, MIPSInst_FS(ir));
1784 break;
1785 case fmovn_op:
08a07904
RB
1786 if (!cpu_has_mips_4_5_r)
1787 return SIGILL;
1788
1da177e4
LT
1789 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1790 return 0;
1791 DPFROMREG(rv.d, MIPSInst_FS(ir));
1792 break;
1da177e4
LT
1793 case fabs_op:
1794 handler.u = ieee754dp_abs;
1795 goto dcopuop;
1796
1797 case fneg_op:
1798 handler.u = ieee754dp_neg;
1799 goto dcopuop;
1800
1801 case fmov_op:
1802 /* an easy one */
1803 DPFROMREG(rv.d, MIPSInst_FS(ir));
1804 goto copcsr;
1805
1806 /* binary op on handler */
3f7cac41
RB
1807dcopbop:
1808 DPFROMREG(fs, MIPSInst_FS(ir));
1809 DPFROMREG(ft, MIPSInst_FT(ir));
1da177e4 1810
3f7cac41
RB
1811 rv.d = (*handler.b) (fs, ft);
1812 goto copcsr;
1813dcopuop:
1814 DPFROMREG(fs, MIPSInst_FS(ir));
1815 rv.d = (*handler.u) (fs);
1816 goto copcsr;
1da177e4 1817
3f7cac41
RB
1818 /*
1819 * unary conv ops
1820 */
1821 case fcvts_op:
1da177e4
LT
1822 DPFROMREG(fs, MIPSInst_FS(ir));
1823 rv.s = ieee754sp_fdp(fs);
1824 rfmt = s_fmt;
1825 goto copcsr;
3f7cac41 1826
1da177e4
LT
1827 case fcvtd_op:
1828 return SIGILL; /* not defined */
1829
3f7cac41 1830 case fcvtw_op:
1da177e4
LT
1831 DPFROMREG(fs, MIPSInst_FS(ir));
1832 rv.w = ieee754dp_tint(fs); /* wrong */
1833 rfmt = w_fmt;
1834 goto copcsr;
1da177e4 1835
1da177e4
LT
1836 case fround_op:
1837 case ftrunc_op:
1838 case fceil_op:
3f7cac41 1839 case ffloor_op:
08a07904
RB
1840 if (!cpu_has_mips_2_3_4_5_r)
1841 return SIGILL;
1842
3f7cac41 1843 oldrm = ieee754_csr.rm;
1da177e4 1844 DPFROMREG(fs, MIPSInst_FS(ir));
56a64733 1845 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1da177e4
LT
1846 rv.w = ieee754dp_tint(fs);
1847 ieee754_csr.rm = oldrm;
1848 rfmt = w_fmt;
1849 goto copcsr;
1da177e4 1850
3f7cac41 1851 case fcvtl_op:
08a07904
RB
1852 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1853 return SIGILL;
1854
1da177e4
LT
1855 DPFROMREG(fs, MIPSInst_FS(ir));
1856 rv.l = ieee754dp_tlong(fs);
1857 rfmt = l_fmt;
1858 goto copcsr;
1da177e4
LT
1859
1860 case froundl_op:
1861 case ftruncl_op:
1862 case fceill_op:
3f7cac41 1863 case ffloorl_op:
08a07904
RB
1864 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1865 return SIGILL;
1866
3f7cac41 1867 oldrm = ieee754_csr.rm;
1da177e4 1868 DPFROMREG(fs, MIPSInst_FS(ir));
56a64733 1869 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
1da177e4
LT
1870 rv.l = ieee754dp_tlong(fs);
1871 ieee754_csr.rm = oldrm;
1872 rfmt = l_fmt;
1873 goto copcsr;
1da177e4
LT
1874
1875 default:
1876 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1877 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
2209bcb1 1878 union ieee754dp fs, ft;
1da177e4
LT
1879
1880 DPFROMREG(fs, MIPSInst_FS(ir));
1881 DPFROMREG(ft, MIPSInst_FT(ir));
1882 rv.w = ieee754dp_cmp(fs, ft,
1883 cmptab[cmpop & 0x7], cmpop & 0x8);
1884 rfmt = -1;
1885 if ((cmpop & 0x8)
1886 &&
1887 ieee754_cxtest
1888 (IEEE754_INVALID_OPERATION))
1889 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1890 else
1891 goto copcsr;
1892
1893 }
1894 else {
1895 return SIGILL;
1896 }
1897 break;
1898 }
1899 break;
1da177e4 1900
3f7cac41 1901 case w_fmt:
1da177e4
LT
1902 switch (MIPSInst_FUNC(ir)) {
1903 case fcvts_op:
1904 /* convert word to single precision real */
1905 SPFROMREG(fs, MIPSInst_FS(ir));
1906 rv.s = ieee754sp_fint(fs.bits);
1907 rfmt = s_fmt;
1908 goto copcsr;
1da177e4
LT
1909 case fcvtd_op:
1910 /* convert word to double precision real */
1911 SPFROMREG(fs, MIPSInst_FS(ir));
1912 rv.d = ieee754dp_fint(fs.bits);
1913 rfmt = d_fmt;
1914 goto copcsr;
1da177e4
LT
1915 default:
1916 return SIGILL;
1917 }
1918 break;
1919 }
1920
3f7cac41 1921 case l_fmt:
08a07904
RB
1922
1923 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1924 return SIGILL;
1925
bbd426f5
PB
1926 DIFROMREG(bits, MIPSInst_FS(ir));
1927
1da177e4
LT
1928 switch (MIPSInst_FUNC(ir)) {
1929 case fcvts_op:
1930 /* convert long to single precision real */
bbd426f5 1931 rv.s = ieee754sp_flong(bits);
1da177e4
LT
1932 rfmt = s_fmt;
1933 goto copcsr;
1934 case fcvtd_op:
1935 /* convert long to double precision real */
bbd426f5 1936 rv.d = ieee754dp_flong(bits);
1da177e4
LT
1937 rfmt = d_fmt;
1938 goto copcsr;
1939 default:
1940 return SIGILL;
1941 }
1942 break;
1da177e4
LT
1943
1944 default:
1945 return SIGILL;
1946 }
1947
1948 /*
1949 * Update the fpu CSR register for this operation.
1950 * If an exception is required, generate a tidy SIGFPE exception,
1951 * without updating the result register.
1952 * Note: cause exception bits do not accumulate, they are rewritten
1953 * for each op; only the flag/sticky bits accumulate.
1954 */
1955 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1956 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
3f7cac41 1957 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
1da177e4
LT
1958 return SIGFPE;
1959 }
1960
1961 /*
1962 * Now we can safely write the result back to the register file.
1963 */
1964 switch (rfmt) {
08a07904
RB
1965 case -1:
1966
1967 if (cpu_has_mips_4_5_r)
c3b9b945 1968 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
08a07904
RB
1969 else
1970 cbit = FPU_CSR_COND;
1da177e4 1971 if (rv.w)
08a07904 1972 ctx->fcr31 |= cbit;
1da177e4 1973 else
08a07904 1974 ctx->fcr31 &= ~cbit;
1da177e4 1975 break;
08a07904 1976
1da177e4
LT
1977 case d_fmt:
1978 DPTOREG(rv.d, MIPSInst_FD(ir));
1979 break;
1da177e4
LT
1980 case s_fmt:
1981 SPTOREG(rv.s, MIPSInst_FD(ir));
1982 break;
1983 case w_fmt:
1984 SITOREG(rv.w, MIPSInst_FD(ir));
1985 break;
1da177e4 1986 case l_fmt:
08a07904
RB
1987 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1988 return SIGILL;
1989
1da177e4
LT
1990 DITOREG(rv.l, MIPSInst_FD(ir));
1991 break;
1da177e4
LT
1992 default:
1993 return SIGILL;
1994 }
1995
1996 return 0;
1997}
1998
e04582b7 1999int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
515b029d 2000 int has_fpu, void *__user *fault_addr)
1da177e4 2001{
333d1f67 2002 unsigned long oldepc, prevepc;
102cedc3
LY
2003 struct mm_decoded_insn dec_insn;
2004 u16 instr[4];
2005 u16 *instr_ptr;
1da177e4
LT
2006 int sig = 0;
2007
2008 oldepc = xcp->cp0_epc;
2009 do {
2010 prevepc = xcp->cp0_epc;
2011
102cedc3
LY
2012 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2013 /*
2014 * Get next 2 microMIPS instructions and convert them
2015 * into 32-bit instructions.
2016 */
2017 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2018 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2019 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2020 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2021 MIPS_FPU_EMU_INC_STATS(errors);
2022 return SIGBUS;
2023 }
2024 instr_ptr = instr;
2025
2026 /* Get first instruction. */
2027 if (mm_insn_16bit(*instr_ptr)) {
2028 /* Duplicate the half-word. */
2029 dec_insn.insn = (*instr_ptr << 16) |
2030 (*instr_ptr);
2031 /* 16-bit instruction. */
2032 dec_insn.pc_inc = 2;
2033 instr_ptr += 1;
2034 } else {
2035 dec_insn.insn = (*instr_ptr << 16) |
2036 *(instr_ptr+1);
2037 /* 32-bit instruction. */
2038 dec_insn.pc_inc = 4;
2039 instr_ptr += 2;
2040 }
2041 /* Get second instruction. */
2042 if (mm_insn_16bit(*instr_ptr)) {
2043 /* Duplicate the half-word. */
2044 dec_insn.next_insn = (*instr_ptr << 16) |
2045 (*instr_ptr);
2046 /* 16-bit instruction. */
2047 dec_insn.next_pc_inc = 2;
2048 } else {
2049 dec_insn.next_insn = (*instr_ptr << 16) |
2050 *(instr_ptr+1);
2051 /* 32-bit instruction. */
2052 dec_insn.next_pc_inc = 4;
2053 }
2054 dec_insn.micro_mips_mode = 1;
2055 } else {
2056 if ((get_user(dec_insn.insn,
2057 (mips_instruction __user *) xcp->cp0_epc)) ||
2058 (get_user(dec_insn.next_insn,
2059 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2060 MIPS_FPU_EMU_INC_STATS(errors);
2061 return SIGBUS;
2062 }
2063 dec_insn.pc_inc = 4;
2064 dec_insn.next_pc_inc = 4;
2065 dec_insn.micro_mips_mode = 0;
515b029d 2066 }
102cedc3
LY
2067
2068 if ((dec_insn.insn == 0) ||
2069 ((dec_insn.pc_inc == 2) &&
2070 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2071 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
1da177e4 2072 else {
cd21dfcf
RB
2073 /*
2074 * The 'ieee754_csr' is an alias of
70342287
RB
2075 * ctx->fcr31. No need to copy ctx->fcr31 to
2076 * ieee754_csr. But ieee754_csr.rm is ieee
cd21dfcf
RB
2077 * library modes. (not mips rounding mode)
2078 */
102cedc3 2079 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
1da177e4
LT
2080 }
2081
e04582b7 2082 if (has_fpu)
1da177e4
LT
2083 break;
2084 if (sig)
2085 break;
2086
2087 cond_resched();
2088 } while (xcp->cp0_epc > prevepc);
2089
2090 /* SIGILL indicates a non-fpu instruction */
2091 if (sig == SIGILL && xcp->cp0_epc != oldepc)
3f7cac41 2092 /* but if EPC has advanced, then ignore it */
1da177e4
LT
2093 sig = 0;
2094
2095 return sig;
2096}
This page took 0.814834 seconds and 5 git commands to generate.