GNU Linux-libre 4.4.295-gnu1
[releases.git] / arch / mips / math-emu / cp1emu.c
1 /*
2  * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
3  *
4  * MIPS floating point support
5  * Copyright (C) 1994-2000 Algorithmics Ltd.
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.,
21  *  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
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
28  * the hardware FPU at the boundaries of the IEEE-754 representation
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  *
33  * Note if you know that you won't have an FPU, then you'll get much
34  * better performance by compiling with -msoft-float!
35  */
36 #include <linux/sched.h>
37 #include <linux/debugfs.h>
38 #include <linux/kconfig.h>
39 #include <linux/percpu-defs.h>
40 #include <linux/perf_event.h>
41
42 #include <asm/branch.h>
43 #include <asm/inst.h>
44 #include <asm/ptrace.h>
45 #include <asm/signal.h>
46 #include <asm/uaccess.h>
47
48 #include <asm/cpu-info.h>
49 #include <asm/processor.h>
50 #include <asm/fpu_emulator.h>
51 #include <asm/fpu.h>
52 #include <asm/mips-r2-to-r6-emul.h>
53
54 #include "ieee754.h"
55
56 /* Function which emulates a floating point instruction. */
57
58 static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
59         mips_instruction);
60
61 static int fpux_emu(struct pt_regs *,
62         struct mips_fpu_struct *, mips_instruction, void *__user *);
63
64 /* Control registers */
65
66 #define FPCREG_RID      0       /* $0  = revision id */
67 #define FPCREG_FCCR     25      /* $25 = fccr */
68 #define FPCREG_FEXR     26      /* $26 = fexr */
69 #define FPCREG_FENR     28      /* $28 = fenr */
70 #define FPCREG_CSR      31      /* $31 = csr */
71
72 /* convert condition code register number to csr bit */
73 const unsigned int fpucondbit[8] = {
74         FPU_CSR_COND,
75         FPU_CSR_COND1,
76         FPU_CSR_COND2,
77         FPU_CSR_COND3,
78         FPU_CSR_COND4,
79         FPU_CSR_COND5,
80         FPU_CSR_COND6,
81         FPU_CSR_COND7
82 };
83
84 /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
85 static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
86 static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
87 static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
88 static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
89
90 /*
91  * This functions translates a 32-bit microMIPS instruction
92  * into a 32-bit MIPS32 instruction. Returns 0 on success
93  * and SIGILL otherwise.
94  */
95 static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
96 {
97         union mips_instruction insn = *insn_ptr;
98         union mips_instruction mips32_insn = insn;
99         int func, fmt, op;
100
101         switch (insn.mm_i_format.opcode) {
102         case mm_ldc132_op:
103                 mips32_insn.mm_i_format.opcode = ldc1_op;
104                 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
105                 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
106                 break;
107         case mm_lwc132_op:
108                 mips32_insn.mm_i_format.opcode = lwc1_op;
109                 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
110                 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
111                 break;
112         case mm_sdc132_op:
113                 mips32_insn.mm_i_format.opcode = sdc1_op;
114                 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
115                 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
116                 break;
117         case mm_swc132_op:
118                 mips32_insn.mm_i_format.opcode = swc1_op;
119                 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
120                 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
121                 break;
122         case mm_pool32i_op:
123                 /* NOTE: offset is << by 1 if in microMIPS mode. */
124                 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
125                     (insn.mm_i_format.rt == mm_bc1t_op)) {
126                         mips32_insn.fb_format.opcode = cop1_op;
127                         mips32_insn.fb_format.bc = bc_op;
128                         mips32_insn.fb_format.flag =
129                                 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
130                 } else
131                         return SIGILL;
132                 break;
133         case mm_pool32f_op:
134                 switch (insn.mm_fp0_format.func) {
135                 case mm_32f_01_op:
136                 case mm_32f_11_op:
137                 case mm_32f_02_op:
138                 case mm_32f_12_op:
139                 case mm_32f_41_op:
140                 case mm_32f_51_op:
141                 case mm_32f_42_op:
142                 case mm_32f_52_op:
143                         op = insn.mm_fp0_format.func;
144                         if (op == mm_32f_01_op)
145                                 func = madd_s_op;
146                         else if (op == mm_32f_11_op)
147                                 func = madd_d_op;
148                         else if (op == mm_32f_02_op)
149                                 func = nmadd_s_op;
150                         else if (op == mm_32f_12_op)
151                                 func = nmadd_d_op;
152                         else if (op == mm_32f_41_op)
153                                 func = msub_s_op;
154                         else if (op == mm_32f_51_op)
155                                 func = msub_d_op;
156                         else if (op == mm_32f_42_op)
157                                 func = nmsub_s_op;
158                         else
159                                 func = nmsub_d_op;
160                         mips32_insn.fp6_format.opcode = cop1x_op;
161                         mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
162                         mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
163                         mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
164                         mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
165                         mips32_insn.fp6_format.func = func;
166                         break;
167                 case mm_32f_10_op:
168                         func = -1;      /* Invalid */
169                         op = insn.mm_fp5_format.op & 0x7;
170                         if (op == mm_ldxc1_op)
171                                 func = ldxc1_op;
172                         else if (op == mm_sdxc1_op)
173                                 func = sdxc1_op;
174                         else if (op == mm_lwxc1_op)
175                                 func = lwxc1_op;
176                         else if (op == mm_swxc1_op)
177                                 func = swxc1_op;
178
179                         if (func != -1) {
180                                 mips32_insn.r_format.opcode = cop1x_op;
181                                 mips32_insn.r_format.rs =
182                                         insn.mm_fp5_format.base;
183                                 mips32_insn.r_format.rt =
184                                         insn.mm_fp5_format.index;
185                                 mips32_insn.r_format.rd = 0;
186                                 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
187                                 mips32_insn.r_format.func = func;
188                         } else
189                                 return SIGILL;
190                         break;
191                 case mm_32f_40_op:
192                         op = -1;        /* Invalid */
193                         if (insn.mm_fp2_format.op == mm_fmovt_op)
194                                 op = 1;
195                         else if (insn.mm_fp2_format.op == mm_fmovf_op)
196                                 op = 0;
197                         if (op != -1) {
198                                 mips32_insn.fp0_format.opcode = cop1_op;
199                                 mips32_insn.fp0_format.fmt =
200                                         sdps_format[insn.mm_fp2_format.fmt];
201                                 mips32_insn.fp0_format.ft =
202                                         (insn.mm_fp2_format.cc<<2) + op;
203                                 mips32_insn.fp0_format.fs =
204                                         insn.mm_fp2_format.fs;
205                                 mips32_insn.fp0_format.fd =
206                                         insn.mm_fp2_format.fd;
207                                 mips32_insn.fp0_format.func = fmovc_op;
208                         } else
209                                 return SIGILL;
210                         break;
211                 case mm_32f_60_op:
212                         func = -1;      /* Invalid */
213                         if (insn.mm_fp0_format.op == mm_fadd_op)
214                                 func = fadd_op;
215                         else if (insn.mm_fp0_format.op == mm_fsub_op)
216                                 func = fsub_op;
217                         else if (insn.mm_fp0_format.op == mm_fmul_op)
218                                 func = fmul_op;
219                         else if (insn.mm_fp0_format.op == mm_fdiv_op)
220                                 func = fdiv_op;
221                         if (func != -1) {
222                                 mips32_insn.fp0_format.opcode = cop1_op;
223                                 mips32_insn.fp0_format.fmt =
224                                         sdps_format[insn.mm_fp0_format.fmt];
225                                 mips32_insn.fp0_format.ft =
226                                         insn.mm_fp0_format.ft;
227                                 mips32_insn.fp0_format.fs =
228                                         insn.mm_fp0_format.fs;
229                                 mips32_insn.fp0_format.fd =
230                                         insn.mm_fp0_format.fd;
231                                 mips32_insn.fp0_format.func = func;
232                         } else
233                                 return SIGILL;
234                         break;
235                 case mm_32f_70_op:
236                         func = -1;      /* Invalid */
237                         if (insn.mm_fp0_format.op == mm_fmovn_op)
238                                 func = fmovn_op;
239                         else if (insn.mm_fp0_format.op == mm_fmovz_op)
240                                 func = fmovz_op;
241                         if (func != -1) {
242                                 mips32_insn.fp0_format.opcode = cop1_op;
243                                 mips32_insn.fp0_format.fmt =
244                                         sdps_format[insn.mm_fp0_format.fmt];
245                                 mips32_insn.fp0_format.ft =
246                                         insn.mm_fp0_format.ft;
247                                 mips32_insn.fp0_format.fs =
248                                         insn.mm_fp0_format.fs;
249                                 mips32_insn.fp0_format.fd =
250                                         insn.mm_fp0_format.fd;
251                                 mips32_insn.fp0_format.func = func;
252                         } else
253                                 return SIGILL;
254                         break;
255                 case mm_32f_73_op:    /* POOL32FXF */
256                         switch (insn.mm_fp1_format.op) {
257                         case mm_movf0_op:
258                         case mm_movf1_op:
259                         case mm_movt0_op:
260                         case mm_movt1_op:
261                                 if ((insn.mm_fp1_format.op & 0x7f) ==
262                                     mm_movf0_op)
263                                         op = 0;
264                                 else
265                                         op = 1;
266                                 mips32_insn.r_format.opcode = spec_op;
267                                 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
268                                 mips32_insn.r_format.rt =
269                                         (insn.mm_fp4_format.cc << 2) + op;
270                                 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
271                                 mips32_insn.r_format.re = 0;
272                                 mips32_insn.r_format.func = movc_op;
273                                 break;
274                         case mm_fcvtd0_op:
275                         case mm_fcvtd1_op:
276                         case mm_fcvts0_op:
277                         case mm_fcvts1_op:
278                                 if ((insn.mm_fp1_format.op & 0x7f) ==
279                                     mm_fcvtd0_op) {
280                                         func = fcvtd_op;
281                                         fmt = swl_format[insn.mm_fp3_format.fmt];
282                                 } else {
283                                         func = fcvts_op;
284                                         fmt = dwl_format[insn.mm_fp3_format.fmt];
285                                 }
286                                 mips32_insn.fp0_format.opcode = cop1_op;
287                                 mips32_insn.fp0_format.fmt = fmt;
288                                 mips32_insn.fp0_format.ft = 0;
289                                 mips32_insn.fp0_format.fs =
290                                         insn.mm_fp3_format.fs;
291                                 mips32_insn.fp0_format.fd =
292                                         insn.mm_fp3_format.rt;
293                                 mips32_insn.fp0_format.func = func;
294                                 break;
295                         case mm_fmov0_op:
296                         case mm_fmov1_op:
297                         case mm_fabs0_op:
298                         case mm_fabs1_op:
299                         case mm_fneg0_op:
300                         case mm_fneg1_op:
301                                 if ((insn.mm_fp1_format.op & 0x7f) ==
302                                     mm_fmov0_op)
303                                         func = fmov_op;
304                                 else if ((insn.mm_fp1_format.op & 0x7f) ==
305                                          mm_fabs0_op)
306                                         func = fabs_op;
307                                 else
308                                         func = fneg_op;
309                                 mips32_insn.fp0_format.opcode = cop1_op;
310                                 mips32_insn.fp0_format.fmt =
311                                         sdps_format[insn.mm_fp3_format.fmt];
312                                 mips32_insn.fp0_format.ft = 0;
313                                 mips32_insn.fp0_format.fs =
314                                         insn.mm_fp3_format.fs;
315                                 mips32_insn.fp0_format.fd =
316                                         insn.mm_fp3_format.rt;
317                                 mips32_insn.fp0_format.func = func;
318                                 break;
319                         case mm_ffloorl_op:
320                         case mm_ffloorw_op:
321                         case mm_fceill_op:
322                         case mm_fceilw_op:
323                         case mm_ftruncl_op:
324                         case mm_ftruncw_op:
325                         case mm_froundl_op:
326                         case mm_froundw_op:
327                         case mm_fcvtl_op:
328                         case mm_fcvtw_op:
329                                 if (insn.mm_fp1_format.op == mm_ffloorl_op)
330                                         func = ffloorl_op;
331                                 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
332                                         func = ffloor_op;
333                                 else if (insn.mm_fp1_format.op == mm_fceill_op)
334                                         func = fceill_op;
335                                 else if (insn.mm_fp1_format.op == mm_fceilw_op)
336                                         func = fceil_op;
337                                 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
338                                         func = ftruncl_op;
339                                 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
340                                         func = ftrunc_op;
341                                 else if (insn.mm_fp1_format.op == mm_froundl_op)
342                                         func = froundl_op;
343                                 else if (insn.mm_fp1_format.op == mm_froundw_op)
344                                         func = fround_op;
345                                 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
346                                         func = fcvtl_op;
347                                 else
348                                         func = fcvtw_op;
349                                 mips32_insn.fp0_format.opcode = cop1_op;
350                                 mips32_insn.fp0_format.fmt =
351                                         sd_format[insn.mm_fp1_format.fmt];
352                                 mips32_insn.fp0_format.ft = 0;
353                                 mips32_insn.fp0_format.fs =
354                                         insn.mm_fp1_format.fs;
355                                 mips32_insn.fp0_format.fd =
356                                         insn.mm_fp1_format.rt;
357                                 mips32_insn.fp0_format.func = func;
358                                 break;
359                         case mm_frsqrt_op:
360                         case mm_fsqrt_op:
361                         case mm_frecip_op:
362                                 if (insn.mm_fp1_format.op == mm_frsqrt_op)
363                                         func = frsqrt_op;
364                                 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
365                                         func = fsqrt_op;
366                                 else
367                                         func = frecip_op;
368                                 mips32_insn.fp0_format.opcode = cop1_op;
369                                 mips32_insn.fp0_format.fmt =
370                                         sdps_format[insn.mm_fp1_format.fmt];
371                                 mips32_insn.fp0_format.ft = 0;
372                                 mips32_insn.fp0_format.fs =
373                                         insn.mm_fp1_format.fs;
374                                 mips32_insn.fp0_format.fd =
375                                         insn.mm_fp1_format.rt;
376                                 mips32_insn.fp0_format.func = func;
377                                 break;
378                         case mm_mfc1_op:
379                         case mm_mtc1_op:
380                         case mm_cfc1_op:
381                         case mm_ctc1_op:
382                         case mm_mfhc1_op:
383                         case mm_mthc1_op:
384                                 if (insn.mm_fp1_format.op == mm_mfc1_op)
385                                         op = mfc_op;
386                                 else if (insn.mm_fp1_format.op == mm_mtc1_op)
387                                         op = mtc_op;
388                                 else if (insn.mm_fp1_format.op == mm_cfc1_op)
389                                         op = cfc_op;
390                                 else if (insn.mm_fp1_format.op == mm_ctc1_op)
391                                         op = ctc_op;
392                                 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
393                                         op = mfhc_op;
394                                 else
395                                         op = mthc_op;
396                                 mips32_insn.fp1_format.opcode = cop1_op;
397                                 mips32_insn.fp1_format.op = op;
398                                 mips32_insn.fp1_format.rt =
399                                         insn.mm_fp1_format.rt;
400                                 mips32_insn.fp1_format.fs =
401                                         insn.mm_fp1_format.fs;
402                                 mips32_insn.fp1_format.fd = 0;
403                                 mips32_insn.fp1_format.func = 0;
404                                 break;
405                         default:
406                                 return SIGILL;
407                         }
408                         break;
409                 case mm_32f_74_op:      /* c.cond.fmt */
410                         mips32_insn.fp0_format.opcode = cop1_op;
411                         mips32_insn.fp0_format.fmt =
412                                 sdps_format[insn.mm_fp4_format.fmt];
413                         mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
414                         mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
415                         mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
416                         mips32_insn.fp0_format.func =
417                                 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
418                         break;
419                 default:
420                         return SIGILL;
421                 }
422                 break;
423         default:
424                 return SIGILL;
425         }
426
427         *insn_ptr = mips32_insn;
428         return 0;
429 }
430
431 /*
432  * Redundant with logic already in kernel/branch.c,
433  * embedded in compute_return_epc.  At some point,
434  * a single subroutine should be used across both
435  * modules.
436  */
437 static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
438                          unsigned long *contpc)
439 {
440         union mips_instruction insn = (union mips_instruction)dec_insn.insn;
441         unsigned int fcr31;
442         unsigned int bit = 0;
443
444         switch (insn.i_format.opcode) {
445         case spec_op:
446                 switch (insn.r_format.func) {
447                 case jalr_op:
448                         if (insn.r_format.rd != 0) {
449                                 regs->regs[insn.r_format.rd] =
450                                         regs->cp0_epc + dec_insn.pc_inc +
451                                         dec_insn.next_pc_inc;
452                         }
453                         /* Fall through */
454                 case jr_op:
455                         /* For R6, JR already emulated in jalr_op */
456                         if (NO_R6EMU && insn.r_format.func == jr_op)
457                                 break;
458                         *contpc = regs->regs[insn.r_format.rs];
459                         return 1;
460                 }
461                 break;
462         case bcond_op:
463                 switch (insn.i_format.rt) {
464                 case bltzal_op:
465                 case bltzall_op:
466                         if (NO_R6EMU && (insn.i_format.rs ||
467                             insn.i_format.rt == bltzall_op))
468                                 break;
469
470                         regs->regs[31] = regs->cp0_epc +
471                                 dec_insn.pc_inc +
472                                 dec_insn.next_pc_inc;
473                         /* Fall through */
474                 case bltzl_op:
475                         if (NO_R6EMU)
476                                 break;
477                 case bltz_op:
478                         if ((long)regs->regs[insn.i_format.rs] < 0)
479                                 *contpc = regs->cp0_epc +
480                                         dec_insn.pc_inc +
481                                         (insn.i_format.simmediate << 2);
482                         else
483                                 *contpc = regs->cp0_epc +
484                                         dec_insn.pc_inc +
485                                         dec_insn.next_pc_inc;
486                         return 1;
487                 case bgezal_op:
488                 case bgezall_op:
489                         if (NO_R6EMU && (insn.i_format.rs ||
490                             insn.i_format.rt == bgezall_op))
491                                 break;
492
493                         regs->regs[31] = regs->cp0_epc +
494                                 dec_insn.pc_inc +
495                                 dec_insn.next_pc_inc;
496                         /* Fall through */
497                 case bgezl_op:
498                         if (NO_R6EMU)
499                                 break;
500                 case bgez_op:
501                         if ((long)regs->regs[insn.i_format.rs] >= 0)
502                                 *contpc = regs->cp0_epc +
503                                         dec_insn.pc_inc +
504                                         (insn.i_format.simmediate << 2);
505                         else
506                                 *contpc = regs->cp0_epc +
507                                         dec_insn.pc_inc +
508                                         dec_insn.next_pc_inc;
509                         return 1;
510                 }
511                 break;
512         case jalx_op:
513                 set_isa16_mode(bit);
514         case jal_op:
515                 regs->regs[31] = regs->cp0_epc +
516                         dec_insn.pc_inc +
517                         dec_insn.next_pc_inc;
518                 /* Fall through */
519         case j_op:
520                 *contpc = regs->cp0_epc + dec_insn.pc_inc;
521                 *contpc >>= 28;
522                 *contpc <<= 28;
523                 *contpc |= (insn.j_format.target << 2);
524                 /* Set microMIPS mode bit: XOR for jalx. */
525                 *contpc ^= bit;
526                 return 1;
527         case beql_op:
528                 if (NO_R6EMU)
529                         break;
530         case beq_op:
531                 if (regs->regs[insn.i_format.rs] ==
532                     regs->regs[insn.i_format.rt])
533                         *contpc = regs->cp0_epc +
534                                 dec_insn.pc_inc +
535                                 (insn.i_format.simmediate << 2);
536                 else
537                         *contpc = regs->cp0_epc +
538                                 dec_insn.pc_inc +
539                                 dec_insn.next_pc_inc;
540                 return 1;
541         case bnel_op:
542                 if (NO_R6EMU)
543                         break;
544         case bne_op:
545                 if (regs->regs[insn.i_format.rs] !=
546                     regs->regs[insn.i_format.rt])
547                         *contpc = regs->cp0_epc +
548                                 dec_insn.pc_inc +
549                                 (insn.i_format.simmediate << 2);
550                 else
551                         *contpc = regs->cp0_epc +
552                                 dec_insn.pc_inc +
553                                 dec_insn.next_pc_inc;
554                 return 1;
555         case blezl_op:
556                 if (!insn.i_format.rt && NO_R6EMU)
557                         break;
558         case blez_op:
559
560                 /*
561                  * Compact branches for R6 for the
562                  * blez and blezl opcodes.
563                  * BLEZ  | rs = 0 | rt != 0  == BLEZALC
564                  * BLEZ  | rs = rt != 0      == BGEZALC
565                  * BLEZ  | rs != 0 | rt != 0 == BGEUC
566                  * BLEZL | rs = 0 | rt != 0  == BLEZC
567                  * BLEZL | rs = rt != 0      == BGEZC
568                  * BLEZL | rs != 0 | rt != 0 == BGEC
569                  *
570                  * For real BLEZ{,L}, rt is always 0.
571                  */
572                 if (cpu_has_mips_r6 && insn.i_format.rt) {
573                         if ((insn.i_format.opcode == blez_op) &&
574                             ((!insn.i_format.rs && insn.i_format.rt) ||
575                              (insn.i_format.rs == insn.i_format.rt)))
576                                 regs->regs[31] = regs->cp0_epc +
577                                         dec_insn.pc_inc;
578                         *contpc = regs->cp0_epc + dec_insn.pc_inc +
579                                 dec_insn.next_pc_inc;
580
581                         return 1;
582                 }
583                 if ((long)regs->regs[insn.i_format.rs] <= 0)
584                         *contpc = regs->cp0_epc +
585                                 dec_insn.pc_inc +
586                                 (insn.i_format.simmediate << 2);
587                 else
588                         *contpc = regs->cp0_epc +
589                                 dec_insn.pc_inc +
590                                 dec_insn.next_pc_inc;
591                 return 1;
592         case bgtzl_op:
593                 if (!insn.i_format.rt && NO_R6EMU)
594                         break;
595         case bgtz_op:
596                 /*
597                  * Compact branches for R6 for the
598                  * bgtz and bgtzl opcodes.
599                  * BGTZ  | rs = 0 | rt != 0  == BGTZALC
600                  * BGTZ  | rs = rt != 0      == BLTZALC
601                  * BGTZ  | rs != 0 | rt != 0 == BLTUC
602                  * BGTZL | rs = 0 | rt != 0  == BGTZC
603                  * BGTZL | rs = rt != 0      == BLTZC
604                  * BGTZL | rs != 0 | rt != 0 == BLTC
605                  *
606                  * *ZALC varint for BGTZ &&& rt != 0
607                  * For real GTZ{,L}, rt is always 0.
608                  */
609                 if (cpu_has_mips_r6 && insn.i_format.rt) {
610                         if ((insn.i_format.opcode == blez_op) &&
611                             ((!insn.i_format.rs && insn.i_format.rt) ||
612                              (insn.i_format.rs == insn.i_format.rt)))
613                                 regs->regs[31] = regs->cp0_epc +
614                                         dec_insn.pc_inc;
615                         *contpc = regs->cp0_epc + dec_insn.pc_inc +
616                                 dec_insn.next_pc_inc;
617
618                         return 1;
619                 }
620
621                 if ((long)regs->regs[insn.i_format.rs] > 0)
622                         *contpc = regs->cp0_epc +
623                                 dec_insn.pc_inc +
624                                 (insn.i_format.simmediate << 2);
625                 else
626                         *contpc = regs->cp0_epc +
627                                 dec_insn.pc_inc +
628                                 dec_insn.next_pc_inc;
629                 return 1;
630         case cbcond0_op:
631         case cbcond1_op:
632                 if (!cpu_has_mips_r6)
633                         break;
634                 if (insn.i_format.rt && !insn.i_format.rs)
635                         regs->regs[31] = regs->cp0_epc + 4;
636                 *contpc = regs->cp0_epc + dec_insn.pc_inc +
637                         dec_insn.next_pc_inc;
638
639                 return 1;
640 #ifdef CONFIG_CPU_CAVIUM_OCTEON
641         case lwc2_op: /* This is bbit0 on Octeon */
642                 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
643                         *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
644                 else
645                         *contpc = regs->cp0_epc + 8;
646                 return 1;
647         case ldc2_op: /* This is bbit032 on Octeon */
648                 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
649                         *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
650                 else
651                         *contpc = regs->cp0_epc + 8;
652                 return 1;
653         case swc2_op: /* This is bbit1 on Octeon */
654                 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
655                         *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
656                 else
657                         *contpc = regs->cp0_epc + 8;
658                 return 1;
659         case sdc2_op: /* This is bbit132 on Octeon */
660                 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
661                         *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
662                 else
663                         *contpc = regs->cp0_epc + 8;
664                 return 1;
665 #else
666         case bc6_op:
667                 /*
668                  * Only valid for MIPS R6 but we can still end up
669                  * here from a broken userland so just tell emulator
670                  * this is not a branch and let it break later on.
671                  */
672                 if  (!cpu_has_mips_r6)
673                         break;
674                 *contpc = regs->cp0_epc + dec_insn.pc_inc +
675                         dec_insn.next_pc_inc;
676
677                 return 1;
678         case balc6_op:
679                 if (!cpu_has_mips_r6)
680                         break;
681                 regs->regs[31] = regs->cp0_epc + 4;
682                 *contpc = regs->cp0_epc + dec_insn.pc_inc +
683                         dec_insn.next_pc_inc;
684
685                 return 1;
686         case beqzcjic_op:
687                 if (!cpu_has_mips_r6)
688                         break;
689                 *contpc = regs->cp0_epc + dec_insn.pc_inc +
690                         dec_insn.next_pc_inc;
691
692                 return 1;
693         case bnezcjialc_op:
694                 if (!cpu_has_mips_r6)
695                         break;
696                 if (!insn.i_format.rs)
697                         regs->regs[31] = regs->cp0_epc + 4;
698                 *contpc = regs->cp0_epc + dec_insn.pc_inc +
699                         dec_insn.next_pc_inc;
700
701                 return 1;
702 #endif
703         case cop0_op:
704         case cop1_op:
705                 /* Need to check for R6 bc1nez and bc1eqz branches */
706                 if (cpu_has_mips_r6 &&
707                     ((insn.i_format.rs == bc1eqz_op) ||
708                      (insn.i_format.rs == bc1nez_op))) {
709                         bit = 0;
710                         switch (insn.i_format.rs) {
711                         case bc1eqz_op:
712                                 if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
713                                     bit = 1;
714                                 break;
715                         case bc1nez_op:
716                                 if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
717                                     bit = 1;
718                                 break;
719                         }
720                         if (bit)
721                                 *contpc = regs->cp0_epc +
722                                         dec_insn.pc_inc +
723                                         (insn.i_format.simmediate << 2);
724                         else
725                                 *contpc = regs->cp0_epc +
726                                         dec_insn.pc_inc +
727                                         dec_insn.next_pc_inc;
728
729                         return 1;
730                 }
731                 /* R2/R6 compatible cop1 instruction. Fall through */
732         case cop2_op:
733         case cop1x_op:
734                 if (insn.i_format.rs == bc_op) {
735                         preempt_disable();
736                         if (is_fpu_owner())
737                                 fcr31 = read_32bit_cp1_register(CP1_STATUS);
738                         else
739                                 fcr31 = current->thread.fpu.fcr31;
740                         preempt_enable();
741
742                         bit = (insn.i_format.rt >> 2);
743                         bit += (bit != 0);
744                         bit += 23;
745                         switch (insn.i_format.rt & 3) {
746                         case 0: /* bc1f */
747                         case 2: /* bc1fl */
748                                 if (~fcr31 & (1 << bit))
749                                         *contpc = regs->cp0_epc +
750                                                 dec_insn.pc_inc +
751                                                 (insn.i_format.simmediate << 2);
752                                 else
753                                         *contpc = regs->cp0_epc +
754                                                 dec_insn.pc_inc +
755                                                 dec_insn.next_pc_inc;
756                                 return 1;
757                         case 1: /* bc1t */
758                         case 3: /* bc1tl */
759                                 if (fcr31 & (1 << bit))
760                                         *contpc = regs->cp0_epc +
761                                                 dec_insn.pc_inc +
762                                                 (insn.i_format.simmediate << 2);
763                                 else
764                                         *contpc = regs->cp0_epc +
765                                                 dec_insn.pc_inc +
766                                                 dec_insn.next_pc_inc;
767                                 return 1;
768                         }
769                 }
770                 break;
771         }
772         return 0;
773 }
774
775 /*
776  * In the Linux kernel, we support selection of FPR format on the
777  * basis of the Status.FR bit.  If an FPU is not present, the FR bit
778  * is hardwired to zero, which would imply a 32-bit FPU even for
779  * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
780  * FPU emu is slow and bulky and optimizing this function offers fairly
781  * sizeable benefits so we try to be clever and make this function return
782  * a constant whenever possible, that is on 64-bit kernels without O32
783  * compatibility enabled and on 32-bit without 64-bit FPU support.
784  */
785 static inline int cop1_64bit(struct pt_regs *xcp)
786 {
787         if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
788                 return 1;
789         else if (config_enabled(CONFIG_32BIT) &&
790                  !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
791                 return 0;
792
793         return !test_thread_flag(TIF_32BIT_FPREGS);
794 }
795
796 static inline bool hybrid_fprs(void)
797 {
798         return test_thread_flag(TIF_HYBRID_FPREGS);
799 }
800
801 #define SIFROMREG(si, x)                                                \
802 do {                                                                    \
803         if (cop1_64bit(xcp) && !hybrid_fprs())                          \
804                 (si) = (int)get_fpr32(&ctx->fpr[x], 0);                 \
805         else                                                            \
806                 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);    \
807 } while (0)
808
809 #define SITOREG(si, x)                                                  \
810 do {                                                                    \
811         if (cop1_64bit(xcp) && !hybrid_fprs()) {                        \
812                 unsigned i;                                             \
813                 set_fpr32(&ctx->fpr[x], 0, si);                         \
814                 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)     \
815                         set_fpr32(&ctx->fpr[x], i, 0);                  \
816         } else {                                                        \
817                 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);            \
818         }                                                               \
819 } while (0)
820
821 #define SIFROMHREG(si, x)       ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
822
823 #define SITOHREG(si, x)                                                 \
824 do {                                                                    \
825         unsigned i;                                                     \
826         set_fpr32(&ctx->fpr[x], 1, si);                                 \
827         for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)             \
828                 set_fpr32(&ctx->fpr[x], i, 0);                          \
829 } while (0)
830
831 #define DIFROMREG(di, x)                                                \
832         ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) ^ 1)], 0))
833
834 #define DITOREG(di, x)                                                  \
835 do {                                                                    \
836         unsigned fpr, i;                                                \
837         fpr = (x) & ~(cop1_64bit(xcp) ^ 1);                             \
838         set_fpr64(&ctx->fpr[fpr], 0, di);                               \
839         for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++)             \
840                 set_fpr64(&ctx->fpr[fpr], i, 0);                        \
841 } while (0)
842
843 #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
844 #define SPTOREG(sp, x)  SITOREG((sp).bits, x)
845 #define DPFROMREG(dp, x)        DIFROMREG((dp).bits, x)
846 #define DPTOREG(dp, x)  DITOREG((dp).bits, x)
847
848 /*
849  * Emulate a CFC1 instruction.
850  */
851 static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
852                             mips_instruction ir)
853 {
854         u32 fcr31 = ctx->fcr31;
855         u32 value = 0;
856
857         switch (MIPSInst_RD(ir)) {
858         case FPCREG_CSR:
859                 value = fcr31;
860                 pr_debug("%p gpr[%d]<-csr=%08x\n",
861                          (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
862                 break;
863
864         case FPCREG_FENR:
865                 if (!cpu_has_mips_r)
866                         break;
867                 value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
868                         MIPS_FENR_FS;
869                 value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
870                 pr_debug("%p gpr[%d]<-enr=%08x\n",
871                          (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
872                 break;
873
874         case FPCREG_FEXR:
875                 if (!cpu_has_mips_r)
876                         break;
877                 value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
878                 pr_debug("%p gpr[%d]<-exr=%08x\n",
879                          (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
880                 break;
881
882         case FPCREG_FCCR:
883                 if (!cpu_has_mips_r)
884                         break;
885                 value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
886                         MIPS_FCCR_COND0;
887                 value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
888                          (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
889                 pr_debug("%p gpr[%d]<-ccr=%08x\n",
890                          (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
891                 break;
892
893         case FPCREG_RID:
894                 value = boot_cpu_data.fpu_id;
895                 break;
896
897         default:
898                 break;
899         }
900
901         if (MIPSInst_RT(ir))
902                 xcp->regs[MIPSInst_RT(ir)] = value;
903 }
904
905 /*
906  * Emulate a CTC1 instruction.
907  */
908 static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
909                             mips_instruction ir)
910 {
911         u32 fcr31 = ctx->fcr31;
912         u32 value;
913         u32 mask;
914
915         if (MIPSInst_RT(ir) == 0)
916                 value = 0;
917         else
918                 value = xcp->regs[MIPSInst_RT(ir)];
919
920         switch (MIPSInst_RD(ir)) {
921         case FPCREG_CSR:
922                 pr_debug("%p gpr[%d]->csr=%08x\n",
923                          (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
924
925                 /* Preserve read-only bits.  */
926                 mask = boot_cpu_data.fpu_msk31;
927                 fcr31 = (value & ~mask) | (fcr31 & mask);
928                 break;
929
930         case FPCREG_FENR:
931                 if (!cpu_has_mips_r)
932                         break;
933                 pr_debug("%p gpr[%d]->enr=%08x\n",
934                          (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
935                 fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
936                 fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
937                          FPU_CSR_FS;
938                 fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
939                 break;
940
941         case FPCREG_FEXR:
942                 if (!cpu_has_mips_r)
943                         break;
944                 pr_debug("%p gpr[%d]->exr=%08x\n",
945                          (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
946                 fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
947                 fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
948                 break;
949
950         case FPCREG_FCCR:
951                 if (!cpu_has_mips_r)
952                         break;
953                 pr_debug("%p gpr[%d]->ccr=%08x\n",
954                          (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
955                 fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
956                 fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
957                          FPU_CSR_COND;
958                 fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
959                          FPU_CSR_CONDX;
960                 break;
961
962         default:
963                 break;
964         }
965
966         ctx->fcr31 = fcr31;
967 }
968
969 /*
970  * Emulate the single floating point instruction pointed at by EPC.
971  * Two instructions if the instruction is in a branch delay slot.
972  */
973
974 static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
975                 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
976 {
977         unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
978         unsigned int cond, cbit, bit0;
979         mips_instruction ir;
980         int likely, pc_inc;
981         union fpureg *fpr;
982         u32 __user *wva;
983         u64 __user *dva;
984         u32 wval;
985         u64 dval;
986         int sig;
987
988         /*
989          * These are giving gcc a gentle hint about what to expect in
990          * dec_inst in order to do better optimization.
991          */
992         if (!cpu_has_mmips && dec_insn.micro_mips_mode)
993                 unreachable();
994
995         /* XXX NEC Vr54xx bug workaround */
996         if (delay_slot(xcp)) {
997                 if (dec_insn.micro_mips_mode) {
998                         if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
999                                 clear_delay_slot(xcp);
1000                 } else {
1001                         if (!isBranchInstr(xcp, dec_insn, &contpc))
1002                                 clear_delay_slot(xcp);
1003                 }
1004         }
1005
1006         if (delay_slot(xcp)) {
1007                 /*
1008                  * The instruction to be emulated is in a branch delay slot
1009                  * which means that we have to  emulate the branch instruction
1010                  * BEFORE we do the cop1 instruction.
1011                  *
1012                  * This branch could be a COP1 branch, but in that case we
1013                  * would have had a trap for that instruction, and would not
1014                  * come through this route.
1015                  *
1016                  * Linux MIPS branch emulator operates on context, updating the
1017                  * cp0_epc.
1018                  */
1019                 ir = dec_insn.next_insn;  /* process delay slot instr */
1020                 pc_inc = dec_insn.next_pc_inc;
1021         } else {
1022                 ir = dec_insn.insn;       /* process current instr */
1023                 pc_inc = dec_insn.pc_inc;
1024         }
1025
1026         /*
1027          * Since microMIPS FPU instructios are a subset of MIPS32 FPU
1028          * instructions, we want to convert microMIPS FPU instructions
1029          * into MIPS32 instructions so that we could reuse all of the
1030          * FPU emulation code.
1031          *
1032          * NOTE: We cannot do this for branch instructions since they
1033          *       are not a subset. Example: Cannot emulate a 16-bit
1034          *       aligned target address with a MIPS32 instruction.
1035          */
1036         if (dec_insn.micro_mips_mode) {
1037                 /*
1038                  * If next instruction is a 16-bit instruction, then it
1039                  * it cannot be a FPU instruction. This could happen
1040                  * since we can be called for non-FPU instructions.
1041                  */
1042                 if ((pc_inc == 2) ||
1043                         (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
1044                          == SIGILL))
1045                         return SIGILL;
1046         }
1047
1048 emul:
1049         perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
1050         MIPS_FPU_EMU_INC_STATS(emulated);
1051         switch (MIPSInst_OPCODE(ir)) {
1052         case ldc1_op:
1053                 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1054                                      MIPSInst_SIMM(ir));
1055                 MIPS_FPU_EMU_INC_STATS(loads);
1056
1057                 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
1058                         MIPS_FPU_EMU_INC_STATS(errors);
1059                         *fault_addr = dva;
1060                         return SIGBUS;
1061                 }
1062                 if (__get_user(dval, dva)) {
1063                         MIPS_FPU_EMU_INC_STATS(errors);
1064                         *fault_addr = dva;
1065                         return SIGSEGV;
1066                 }
1067                 DITOREG(dval, MIPSInst_RT(ir));
1068                 break;
1069
1070         case sdc1_op:
1071                 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1072                                       MIPSInst_SIMM(ir));
1073                 MIPS_FPU_EMU_INC_STATS(stores);
1074                 DIFROMREG(dval, MIPSInst_RT(ir));
1075                 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
1076                         MIPS_FPU_EMU_INC_STATS(errors);
1077                         *fault_addr = dva;
1078                         return SIGBUS;
1079                 }
1080                 if (__put_user(dval, dva)) {
1081                         MIPS_FPU_EMU_INC_STATS(errors);
1082                         *fault_addr = dva;
1083                         return SIGSEGV;
1084                 }
1085                 break;
1086
1087         case lwc1_op:
1088                 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1089                                       MIPSInst_SIMM(ir));
1090                 MIPS_FPU_EMU_INC_STATS(loads);
1091                 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
1092                         MIPS_FPU_EMU_INC_STATS(errors);
1093                         *fault_addr = wva;
1094                         return SIGBUS;
1095                 }
1096                 if (__get_user(wval, wva)) {
1097                         MIPS_FPU_EMU_INC_STATS(errors);
1098                         *fault_addr = wva;
1099                         return SIGSEGV;
1100                 }
1101                 SITOREG(wval, MIPSInst_RT(ir));
1102                 break;
1103
1104         case swc1_op:
1105                 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1106                                       MIPSInst_SIMM(ir));
1107                 MIPS_FPU_EMU_INC_STATS(stores);
1108                 SIFROMREG(wval, MIPSInst_RT(ir));
1109                 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
1110                         MIPS_FPU_EMU_INC_STATS(errors);
1111                         *fault_addr = wva;
1112                         return SIGBUS;
1113                 }
1114                 if (__put_user(wval, wva)) {
1115                         MIPS_FPU_EMU_INC_STATS(errors);
1116                         *fault_addr = wva;
1117                         return SIGSEGV;
1118                 }
1119                 break;
1120
1121         case cop1_op:
1122                 switch (MIPSInst_RS(ir)) {
1123                 case dmfc_op:
1124                         if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1125                                 return SIGILL;
1126
1127                         /* copregister fs -> gpr[rt] */
1128                         if (MIPSInst_RT(ir) != 0) {
1129                                 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1130                                         MIPSInst_RD(ir));
1131                         }
1132                         break;
1133
1134                 case dmtc_op:
1135                         if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1136                                 return SIGILL;
1137
1138                         /* copregister fs <- rt */
1139                         DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1140                         break;
1141
1142                 case mfhc_op:
1143                         if (!cpu_has_mips_r2_r6)
1144                                 goto sigill;
1145
1146                         /* copregister rd -> gpr[rt] */
1147                         if (MIPSInst_RT(ir) != 0) {
1148                                 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1149                                         MIPSInst_RD(ir));
1150                         }
1151                         break;
1152
1153                 case mthc_op:
1154                         if (!cpu_has_mips_r2_r6)
1155                                 goto sigill;
1156
1157                         /* copregister rd <- gpr[rt] */
1158                         SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1159                         break;
1160
1161                 case mfc_op:
1162                         /* copregister rd -> gpr[rt] */
1163                         if (MIPSInst_RT(ir) != 0) {
1164                                 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1165                                         MIPSInst_RD(ir));
1166                         }
1167                         break;
1168
1169                 case mtc_op:
1170                         /* copregister rd <- rt */
1171                         SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1172                         break;
1173
1174                 case cfc_op:
1175                         /* cop control register rd -> gpr[rt] */
1176                         cop1_cfc(xcp, ctx, ir);
1177                         break;
1178
1179                 case ctc_op:
1180                         /* copregister rd <- rt */
1181                         cop1_ctc(xcp, ctx, ir);
1182                         if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1183                                 return SIGFPE;
1184                         }
1185                         break;
1186
1187                 case bc1eqz_op:
1188                 case bc1nez_op:
1189                         if (!cpu_has_mips_r6 || delay_slot(xcp))
1190                                 return SIGILL;
1191
1192                         cond = likely = 0;
1193                         fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
1194                         bit0 = get_fpr32(fpr, 0) & 0x1;
1195                         switch (MIPSInst_RS(ir)) {
1196                         case bc1eqz_op:
1197                                 cond = bit0 == 0;
1198                                 break;
1199                         case bc1nez_op:
1200                                 cond = bit0 != 0;
1201                                 break;
1202                         }
1203                         goto branch_common;
1204
1205                 case bc_op:
1206                         if (delay_slot(xcp))
1207                                 return SIGILL;
1208
1209                         if (cpu_has_mips_4_5_r)
1210                                 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1211                         else
1212                                 cbit = FPU_CSR_COND;
1213                         cond = ctx->fcr31 & cbit;
1214
1215                         likely = 0;
1216                         switch (MIPSInst_RT(ir) & 3) {
1217                         case bcfl_op:
1218                                 if (cpu_has_mips_2_3_4_5_r)
1219                                         likely = 1;
1220                                 /* Fall through */
1221                         case bcf_op:
1222                                 cond = !cond;
1223                                 break;
1224                         case bctl_op:
1225                                 if (cpu_has_mips_2_3_4_5_r)
1226                                         likely = 1;
1227                                 /* Fall through */
1228                         case bct_op:
1229                                 break;
1230                         }
1231 branch_common:
1232                         set_delay_slot(xcp);
1233                         if (cond) {
1234                                 /*
1235                                  * Branch taken: emulate dslot instruction
1236                                  */
1237                                 unsigned long bcpc;
1238
1239                                 /*
1240                                  * Remember EPC at the branch to point back
1241                                  * at so that any delay-slot instruction
1242                                  * signal is not silently ignored.
1243                                  */
1244                                 bcpc = xcp->cp0_epc;
1245                                 xcp->cp0_epc += dec_insn.pc_inc;
1246
1247                                 contpc = MIPSInst_SIMM(ir);
1248                                 ir = dec_insn.next_insn;
1249                                 if (dec_insn.micro_mips_mode) {
1250                                         contpc = (xcp->cp0_epc + (contpc << 1));
1251
1252                                         /* If 16-bit instruction, not FPU. */
1253                                         if ((dec_insn.next_pc_inc == 2) ||
1254                                                 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1255
1256                                                 /*
1257                                                  * Since this instruction will
1258                                                  * be put on the stack with
1259                                                  * 32-bit words, get around
1260                                                  * this problem by putting a
1261                                                  * NOP16 as the second one.
1262                                                  */
1263                                                 if (dec_insn.next_pc_inc == 2)
1264                                                         ir = (ir & (~0xffff)) | MM_NOP16;
1265
1266                                                 /*
1267                                                  * Single step the non-CP1
1268                                                  * instruction in the dslot.
1269                                                  */
1270                                                 sig = mips_dsemul(xcp, ir,
1271                                                                   contpc);
1272                                                 if (sig)
1273                                                         xcp->cp0_epc = bcpc;
1274                                                 /*
1275                                                  * SIGILL forces out of
1276                                                  * the emulation loop.
1277                                                  */
1278                                                 return sig ? sig : SIGILL;
1279                                         }
1280                                 } else
1281                                         contpc = (xcp->cp0_epc + (contpc << 2));
1282
1283                                 switch (MIPSInst_OPCODE(ir)) {
1284                                 case lwc1_op:
1285                                 case swc1_op:
1286                                         goto emul;
1287
1288                                 case ldc1_op:
1289                                 case sdc1_op:
1290                                         if (cpu_has_mips_2_3_4_5_r)
1291                                                 goto emul;
1292
1293                                         goto bc_sigill;
1294
1295                                 case cop1_op:
1296                                         goto emul;
1297
1298                                 case cop1x_op:
1299                                         if (cpu_has_mips_4_5_64_r2_r6)
1300                                                 /* its one of ours */
1301                                                 goto emul;
1302
1303                                         goto bc_sigill;
1304
1305                                 case spec_op:
1306                                         switch (MIPSInst_FUNC(ir)) {
1307                                         case movc_op:
1308                                                 if (cpu_has_mips_4_5_r)
1309                                                         goto emul;
1310
1311                                                 goto bc_sigill;
1312                                         }
1313                                         break;
1314
1315                                 bc_sigill:
1316                                         xcp->cp0_epc = bcpc;
1317                                         return SIGILL;
1318                                 }
1319
1320                                 /*
1321                                  * Single step the non-cp1
1322                                  * instruction in the dslot
1323                                  */
1324                                 sig = mips_dsemul(xcp, ir, contpc);
1325                                 if (sig)
1326                                         xcp->cp0_epc = bcpc;
1327                                 /* SIGILL forces out of the emulation loop.  */
1328                                 return sig ? sig : SIGILL;
1329                         } else if (likely) {    /* branch not taken */
1330                                 /*
1331                                  * branch likely nullifies
1332                                  * dslot if not taken
1333                                  */
1334                                 xcp->cp0_epc += dec_insn.pc_inc;
1335                                 contpc += dec_insn.pc_inc;
1336                                 /*
1337                                  * else continue & execute
1338                                  * dslot as normal insn
1339                                  */
1340                         }
1341                         break;
1342
1343                 default:
1344                         if (!(MIPSInst_RS(ir) & 0x10))
1345                                 return SIGILL;
1346
1347                         /* a real fpu computation instruction */
1348                         if ((sig = fpu_emu(xcp, ctx, ir)))
1349                                 return sig;
1350                 }
1351                 break;
1352
1353         case cop1x_op:
1354                 if (!cpu_has_mips_4_5_64_r2_r6)
1355                         return SIGILL;
1356
1357                 sig = fpux_emu(xcp, ctx, ir, fault_addr);
1358                 if (sig)
1359                         return sig;
1360                 break;
1361
1362         case spec_op:
1363                 if (!cpu_has_mips_4_5_r)
1364                         return SIGILL;
1365
1366                 if (MIPSInst_FUNC(ir) != movc_op)
1367                         return SIGILL;
1368                 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1369                 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1370                         xcp->regs[MIPSInst_RD(ir)] =
1371                                 xcp->regs[MIPSInst_RS(ir)];
1372                 break;
1373         default:
1374 sigill:
1375                 return SIGILL;
1376         }
1377
1378         /* we did it !! */
1379         xcp->cp0_epc = contpc;
1380         clear_delay_slot(xcp);
1381
1382         return 0;
1383 }
1384
1385 /*
1386  * Conversion table from MIPS compare ops 48-63
1387  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1388  */
1389 static const unsigned char cmptab[8] = {
1390         0,                      /* cmp_0 (sig) cmp_sf */
1391         IEEE754_CUN,            /* cmp_un (sig) cmp_ngle */
1392         IEEE754_CEQ,            /* cmp_eq (sig) cmp_seq */
1393         IEEE754_CEQ | IEEE754_CUN,      /* cmp_ueq (sig) cmp_ngl  */
1394         IEEE754_CLT,            /* cmp_olt (sig) cmp_lt */
1395         IEEE754_CLT | IEEE754_CUN,      /* cmp_ult (sig) cmp_nge */
1396         IEEE754_CLT | IEEE754_CEQ,      /* cmp_ole (sig) cmp_le */
1397         IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,        /* cmp_ule (sig) cmp_ngt */
1398 };
1399
1400 static const unsigned char negative_cmptab[8] = {
1401         0, /* Reserved */
1402         IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
1403         IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
1404         IEEE754_CLT | IEEE754_CGT,
1405         /* Reserved */
1406 };
1407
1408
1409 /*
1410  * Additional MIPS4 instructions
1411  */
1412
1413 #define DEF3OP(name, p, f1, f2, f3)                                     \
1414 static union ieee754##p fpemu_##p##_##name(union ieee754##p r,          \
1415         union ieee754##p s, union ieee754##p t)                         \
1416 {                                                                       \
1417         struct _ieee754_csr ieee754_csr_save;                           \
1418         s = f1(s, t);                                                   \
1419         ieee754_csr_save = ieee754_csr;                                 \
1420         s = f2(s, r);                                                   \
1421         ieee754_csr_save.cx |= ieee754_csr.cx;                          \
1422         ieee754_csr_save.sx |= ieee754_csr.sx;                          \
1423         s = f3(s);                                                      \
1424         ieee754_csr.cx |= ieee754_csr_save.cx;                          \
1425         ieee754_csr.sx |= ieee754_csr_save.sx;                          \
1426         return s;                                                       \
1427 }
1428
1429 static union ieee754dp fpemu_dp_recip(union ieee754dp d)
1430 {
1431         return ieee754dp_div(ieee754dp_one(0), d);
1432 }
1433
1434 static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
1435 {
1436         return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1437 }
1438
1439 static union ieee754sp fpemu_sp_recip(union ieee754sp s)
1440 {
1441         return ieee754sp_div(ieee754sp_one(0), s);
1442 }
1443
1444 static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
1445 {
1446         return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1447 }
1448
1449 DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1450 DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
1451 DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1452 DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
1453 DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1454 DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
1455 DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1456 DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1457
1458 static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1459         mips_instruction ir, void *__user *fault_addr)
1460 {
1461         unsigned rcsr = 0;      /* resulting csr */
1462
1463         MIPS_FPU_EMU_INC_STATS(cp1xops);
1464
1465         switch (MIPSInst_FMA_FFMT(ir)) {
1466         case s_fmt:{            /* 0 */
1467
1468                 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1469                 union ieee754sp fd, fr, fs, ft;
1470                 u32 __user *va;
1471                 u32 val;
1472
1473                 switch (MIPSInst_FUNC(ir)) {
1474                 case lwxc1_op:
1475                         va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1476                                 xcp->regs[MIPSInst_FT(ir)]);
1477
1478                         MIPS_FPU_EMU_INC_STATS(loads);
1479                         if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1480                                 MIPS_FPU_EMU_INC_STATS(errors);
1481                                 *fault_addr = va;
1482                                 return SIGBUS;
1483                         }
1484                         if (__get_user(val, va)) {
1485                                 MIPS_FPU_EMU_INC_STATS(errors);
1486                                 *fault_addr = va;
1487                                 return SIGSEGV;
1488                         }
1489                         SITOREG(val, MIPSInst_FD(ir));
1490                         break;
1491
1492                 case swxc1_op:
1493                         va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1494                                 xcp->regs[MIPSInst_FT(ir)]);
1495
1496                         MIPS_FPU_EMU_INC_STATS(stores);
1497
1498                         SIFROMREG(val, MIPSInst_FS(ir));
1499                         if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1500                                 MIPS_FPU_EMU_INC_STATS(errors);
1501                                 *fault_addr = va;
1502                                 return SIGBUS;
1503                         }
1504                         if (put_user(val, va)) {
1505                                 MIPS_FPU_EMU_INC_STATS(errors);
1506                                 *fault_addr = va;
1507                                 return SIGSEGV;
1508                         }
1509                         break;
1510
1511                 case madd_s_op:
1512                         handler = fpemu_sp_madd;
1513                         goto scoptop;
1514                 case msub_s_op:
1515                         handler = fpemu_sp_msub;
1516                         goto scoptop;
1517                 case nmadd_s_op:
1518                         handler = fpemu_sp_nmadd;
1519                         goto scoptop;
1520                 case nmsub_s_op:
1521                         handler = fpemu_sp_nmsub;
1522                         goto scoptop;
1523
1524                       scoptop:
1525                         SPFROMREG(fr, MIPSInst_FR(ir));
1526                         SPFROMREG(fs, MIPSInst_FS(ir));
1527                         SPFROMREG(ft, MIPSInst_FT(ir));
1528                         fd = (*handler) (fr, fs, ft);
1529                         SPTOREG(fd, MIPSInst_FD(ir));
1530
1531                       copcsr:
1532                         if (ieee754_cxtest(IEEE754_INEXACT)) {
1533                                 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
1534                                 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1535                         }
1536                         if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1537                                 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
1538                                 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1539                         }
1540                         if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1541                                 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
1542                                 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1543                         }
1544                         if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1545                                 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
1546                                 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1547                         }
1548
1549                         ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1550                         if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1551                                 /*printk ("SIGFPE: FPU csr = %08x\n",
1552                                    ctx->fcr31); */
1553                                 return SIGFPE;
1554                         }
1555
1556                         break;
1557
1558                 default:
1559                         return SIGILL;
1560                 }
1561                 break;
1562         }
1563
1564         case d_fmt:{            /* 1 */
1565                 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1566                 union ieee754dp fd, fr, fs, ft;
1567                 u64 __user *va;
1568                 u64 val;
1569
1570                 switch (MIPSInst_FUNC(ir)) {
1571                 case ldxc1_op:
1572                         va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1573                                 xcp->regs[MIPSInst_FT(ir)]);
1574
1575                         MIPS_FPU_EMU_INC_STATS(loads);
1576                         if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1577                                 MIPS_FPU_EMU_INC_STATS(errors);
1578                                 *fault_addr = va;
1579                                 return SIGBUS;
1580                         }
1581                         if (__get_user(val, va)) {
1582                                 MIPS_FPU_EMU_INC_STATS(errors);
1583                                 *fault_addr = va;
1584                                 return SIGSEGV;
1585                         }
1586                         DITOREG(val, MIPSInst_FD(ir));
1587                         break;
1588
1589                 case sdxc1_op:
1590                         va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
1591                                 xcp->regs[MIPSInst_FT(ir)]);
1592
1593                         MIPS_FPU_EMU_INC_STATS(stores);
1594                         DIFROMREG(val, MIPSInst_FS(ir));
1595                         if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1596                                 MIPS_FPU_EMU_INC_STATS(errors);
1597                                 *fault_addr = va;
1598                                 return SIGBUS;
1599                         }
1600                         if (__put_user(val, va)) {
1601                                 MIPS_FPU_EMU_INC_STATS(errors);
1602                                 *fault_addr = va;
1603                                 return SIGSEGV;
1604                         }
1605                         break;
1606
1607                 case madd_d_op:
1608                         handler = fpemu_dp_madd;
1609                         goto dcoptop;
1610                 case msub_d_op:
1611                         handler = fpemu_dp_msub;
1612                         goto dcoptop;
1613                 case nmadd_d_op:
1614                         handler = fpemu_dp_nmadd;
1615                         goto dcoptop;
1616                 case nmsub_d_op:
1617                         handler = fpemu_dp_nmsub;
1618                         goto dcoptop;
1619
1620                       dcoptop:
1621                         DPFROMREG(fr, MIPSInst_FR(ir));
1622                         DPFROMREG(fs, MIPSInst_FS(ir));
1623                         DPFROMREG(ft, MIPSInst_FT(ir));
1624                         fd = (*handler) (fr, fs, ft);
1625                         DPTOREG(fd, MIPSInst_FD(ir));
1626                         goto copcsr;
1627
1628                 default:
1629                         return SIGILL;
1630                 }
1631                 break;
1632         }
1633
1634         case 0x3:
1635                 if (MIPSInst_FUNC(ir) != pfetch_op)
1636                         return SIGILL;
1637
1638                 /* ignore prefx operation */
1639                 break;
1640
1641         default:
1642                 return SIGILL;
1643         }
1644
1645         return 0;
1646 }
1647
1648
1649
1650 /*
1651  * Emulate a single COP1 arithmetic instruction.
1652  */
1653 static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1654         mips_instruction ir)
1655 {
1656         int rfmt;               /* resulting format */
1657         unsigned rcsr = 0;      /* resulting csr */
1658         unsigned int oldrm;
1659         unsigned int cbit;
1660         unsigned cond;
1661         union {
1662                 union ieee754dp d;
1663                 union ieee754sp s;
1664                 int w;
1665                 s64 l;
1666         } rv;                   /* resulting value */
1667         u64 bits;
1668
1669         MIPS_FPU_EMU_INC_STATS(cp1ops);
1670         switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
1671         case s_fmt: {           /* 0 */
1672                 union {
1673                         union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1674                         union ieee754sp(*u) (union ieee754sp);
1675                 } handler;
1676                 union ieee754sp fs, ft;
1677
1678                 switch (MIPSInst_FUNC(ir)) {
1679                         /* binary ops */
1680                 case fadd_op:
1681                         handler.b = ieee754sp_add;
1682                         goto scopbop;
1683                 case fsub_op:
1684                         handler.b = ieee754sp_sub;
1685                         goto scopbop;
1686                 case fmul_op:
1687                         handler.b = ieee754sp_mul;
1688                         goto scopbop;
1689                 case fdiv_op:
1690                         handler.b = ieee754sp_div;
1691                         goto scopbop;
1692
1693                         /* unary  ops */
1694                 case fsqrt_op:
1695                         if (!cpu_has_mips_2_3_4_5_r)
1696                                 return SIGILL;
1697
1698                         handler.u = ieee754sp_sqrt;
1699                         goto scopuop;
1700
1701                 /*
1702                  * Note that on some MIPS IV implementations such as the
1703                  * R5000 and R8000 the FSQRT and FRECIP instructions do not
1704                  * achieve full IEEE-754 accuracy - however this emulator does.
1705                  */
1706                 case frsqrt_op:
1707                         if (!cpu_has_mips_4_5_64_r2_r6)
1708                                 return SIGILL;
1709
1710                         handler.u = fpemu_sp_rsqrt;
1711                         goto scopuop;
1712
1713                 case frecip_op:
1714                         if (!cpu_has_mips_4_5_64_r2_r6)
1715                                 return SIGILL;
1716
1717                         handler.u = fpemu_sp_recip;
1718                         goto scopuop;
1719
1720                 case fmovc_op:
1721                         if (!cpu_has_mips_4_5_r)
1722                                 return SIGILL;
1723
1724                         cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1725                         if (((ctx->fcr31 & cond) != 0) !=
1726                                 ((MIPSInst_FT(ir) & 1) != 0))
1727                                 return 0;
1728                         SPFROMREG(rv.s, MIPSInst_FS(ir));
1729                         break;
1730
1731                 case fmovz_op:
1732                         if (!cpu_has_mips_4_5_r)
1733                                 return SIGILL;
1734
1735                         if (xcp->regs[MIPSInst_FT(ir)] != 0)
1736                                 return 0;
1737                         SPFROMREG(rv.s, MIPSInst_FS(ir));
1738                         break;
1739
1740                 case fmovn_op:
1741                         if (!cpu_has_mips_4_5_r)
1742                                 return SIGILL;
1743
1744                         if (xcp->regs[MIPSInst_FT(ir)] == 0)
1745                                 return 0;
1746                         SPFROMREG(rv.s, MIPSInst_FS(ir));
1747                         break;
1748
1749                 case fseleqz_op:
1750                         if (!cpu_has_mips_r6)
1751                                 return SIGILL;
1752
1753                         SPFROMREG(rv.s, MIPSInst_FT(ir));
1754                         if (rv.w & 0x1)
1755                                 rv.w = 0;
1756                         else
1757                                 SPFROMREG(rv.s, MIPSInst_FS(ir));
1758                         break;
1759
1760                 case fselnez_op:
1761                         if (!cpu_has_mips_r6)
1762                                 return SIGILL;
1763
1764                         SPFROMREG(rv.s, MIPSInst_FT(ir));
1765                         if (rv.w & 0x1)
1766                                 SPFROMREG(rv.s, MIPSInst_FS(ir));
1767                         else
1768                                 rv.w = 0;
1769                         break;
1770
1771                 case fmaddf_op: {
1772                         union ieee754sp ft, fs, fd;
1773
1774                         if (!cpu_has_mips_r6)
1775                                 return SIGILL;
1776
1777                         SPFROMREG(ft, MIPSInst_FT(ir));
1778                         SPFROMREG(fs, MIPSInst_FS(ir));
1779                         SPFROMREG(fd, MIPSInst_FD(ir));
1780                         rv.s = ieee754sp_maddf(fd, fs, ft);
1781                         goto copcsr;
1782                 }
1783
1784                 case fmsubf_op: {
1785                         union ieee754sp ft, fs, fd;
1786
1787                         if (!cpu_has_mips_r6)
1788                                 return SIGILL;
1789
1790                         SPFROMREG(ft, MIPSInst_FT(ir));
1791                         SPFROMREG(fs, MIPSInst_FS(ir));
1792                         SPFROMREG(fd, MIPSInst_FD(ir));
1793                         rv.s = ieee754sp_msubf(fd, fs, ft);
1794                         goto copcsr;
1795                 }
1796
1797                 case frint_op: {
1798                         union ieee754sp fs;
1799
1800                         if (!cpu_has_mips_r6)
1801                                 return SIGILL;
1802
1803                         SPFROMREG(fs, MIPSInst_FS(ir));
1804                         rv.l = ieee754sp_tlong(fs);
1805                         rv.s = ieee754sp_flong(rv.l);
1806                         goto copcsr;
1807                 }
1808
1809                 case fclass_op: {
1810                         union ieee754sp fs;
1811
1812                         if (!cpu_has_mips_r6)
1813                                 return SIGILL;
1814
1815                         SPFROMREG(fs, MIPSInst_FS(ir));
1816                         rv.w = ieee754sp_2008class(fs);
1817                         rfmt = w_fmt;
1818                         goto copcsr;
1819                 }
1820
1821                 case fmin_op: {
1822                         union ieee754sp fs, ft;
1823
1824                         if (!cpu_has_mips_r6)
1825                                 return SIGILL;
1826
1827                         SPFROMREG(ft, MIPSInst_FT(ir));
1828                         SPFROMREG(fs, MIPSInst_FS(ir));
1829                         rv.s = ieee754sp_fmin(fs, ft);
1830                         goto copcsr;
1831                 }
1832
1833                 case fmina_op: {
1834                         union ieee754sp fs, ft;
1835
1836                         if (!cpu_has_mips_r6)
1837                                 return SIGILL;
1838
1839                         SPFROMREG(ft, MIPSInst_FT(ir));
1840                         SPFROMREG(fs, MIPSInst_FS(ir));
1841                         rv.s = ieee754sp_fmina(fs, ft);
1842                         goto copcsr;
1843                 }
1844
1845                 case fmax_op: {
1846                         union ieee754sp fs, ft;
1847
1848                         if (!cpu_has_mips_r6)
1849                                 return SIGILL;
1850
1851                         SPFROMREG(ft, MIPSInst_FT(ir));
1852                         SPFROMREG(fs, MIPSInst_FS(ir));
1853                         rv.s = ieee754sp_fmax(fs, ft);
1854                         goto copcsr;
1855                 }
1856
1857                 case fmaxa_op: {
1858                         union ieee754sp fs, ft;
1859
1860                         if (!cpu_has_mips_r6)
1861                                 return SIGILL;
1862
1863                         SPFROMREG(ft, MIPSInst_FT(ir));
1864                         SPFROMREG(fs, MIPSInst_FS(ir));
1865                         rv.s = ieee754sp_fmaxa(fs, ft);
1866                         goto copcsr;
1867                 }
1868
1869                 case fabs_op:
1870                         handler.u = ieee754sp_abs;
1871                         goto scopuop;
1872
1873                 case fneg_op:
1874                         handler.u = ieee754sp_neg;
1875                         goto scopuop;
1876
1877                 case fmov_op:
1878                         /* an easy one */
1879                         SPFROMREG(rv.s, MIPSInst_FS(ir));
1880                         goto copcsr;
1881
1882                         /* binary op on handler */
1883 scopbop:
1884                         SPFROMREG(fs, MIPSInst_FS(ir));
1885                         SPFROMREG(ft, MIPSInst_FT(ir));
1886
1887                         rv.s = (*handler.b) (fs, ft);
1888                         goto copcsr;
1889 scopuop:
1890                         SPFROMREG(fs, MIPSInst_FS(ir));
1891                         rv.s = (*handler.u) (fs);
1892                         goto copcsr;
1893 copcsr:
1894                         if (ieee754_cxtest(IEEE754_INEXACT)) {
1895                                 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
1896                                 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1897                         }
1898                         if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1899                                 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
1900                                 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1901                         }
1902                         if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1903                                 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
1904                                 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1905                         }
1906                         if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1907                                 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
1908                                 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1909                         }
1910                         if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1911                                 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
1912                                 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1913                         }
1914                         break;
1915
1916                         /* unary conv ops */
1917                 case fcvts_op:
1918                         return SIGILL;  /* not defined */
1919
1920                 case fcvtd_op:
1921                         SPFROMREG(fs, MIPSInst_FS(ir));
1922                         rv.d = ieee754dp_fsp(fs);
1923                         rfmt = d_fmt;
1924                         goto copcsr;
1925
1926                 case fcvtw_op:
1927                         SPFROMREG(fs, MIPSInst_FS(ir));
1928                         rv.w = ieee754sp_tint(fs);
1929                         rfmt = w_fmt;
1930                         goto copcsr;
1931
1932                 case fround_op:
1933                 case ftrunc_op:
1934                 case fceil_op:
1935                 case ffloor_op:
1936                         if (!cpu_has_mips_2_3_4_5_r)
1937                                 return SIGILL;
1938
1939                         oldrm = ieee754_csr.rm;
1940                         SPFROMREG(fs, MIPSInst_FS(ir));
1941                         ieee754_csr.rm = MIPSInst_FUNC(ir);
1942                         rv.w = ieee754sp_tint(fs);
1943                         ieee754_csr.rm = oldrm;
1944                         rfmt = w_fmt;
1945                         goto copcsr;
1946
1947                 case fcvtl_op:
1948                         if (!cpu_has_mips_3_4_5_64_r2_r6)
1949                                 return SIGILL;
1950
1951                         SPFROMREG(fs, MIPSInst_FS(ir));
1952                         rv.l = ieee754sp_tlong(fs);
1953                         rfmt = l_fmt;
1954                         goto copcsr;
1955
1956                 case froundl_op:
1957                 case ftruncl_op:
1958                 case fceill_op:
1959                 case ffloorl_op:
1960                         if (!cpu_has_mips_3_4_5_64_r2_r6)
1961                                 return SIGILL;
1962
1963                         oldrm = ieee754_csr.rm;
1964                         SPFROMREG(fs, MIPSInst_FS(ir));
1965                         ieee754_csr.rm = MIPSInst_FUNC(ir);
1966                         rv.l = ieee754sp_tlong(fs);
1967                         ieee754_csr.rm = oldrm;
1968                         rfmt = l_fmt;
1969                         goto copcsr;
1970
1971                 default:
1972                         if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
1973                                 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1974                                 union ieee754sp fs, ft;
1975
1976                                 SPFROMREG(fs, MIPSInst_FS(ir));
1977                                 SPFROMREG(ft, MIPSInst_FT(ir));
1978                                 rv.w = ieee754sp_cmp(fs, ft,
1979                                         cmptab[cmpop & 0x7], cmpop & 0x8);
1980                                 rfmt = -1;
1981                                 if ((cmpop & 0x8) && ieee754_cxtest
1982                                         (IEEE754_INVALID_OPERATION))
1983                                         rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1984                                 else
1985                                         goto copcsr;
1986
1987                         } else
1988                                 return SIGILL;
1989                         break;
1990                 }
1991                 break;
1992         }
1993
1994         case d_fmt: {
1995                 union ieee754dp fs, ft;
1996                 union {
1997                         union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1998                         union ieee754dp(*u) (union ieee754dp);
1999                 } handler;
2000
2001                 switch (MIPSInst_FUNC(ir)) {
2002                         /* binary ops */
2003                 case fadd_op:
2004                         handler.b = ieee754dp_add;
2005                         goto dcopbop;
2006                 case fsub_op:
2007                         handler.b = ieee754dp_sub;
2008                         goto dcopbop;
2009                 case fmul_op:
2010                         handler.b = ieee754dp_mul;
2011                         goto dcopbop;
2012                 case fdiv_op:
2013                         handler.b = ieee754dp_div;
2014                         goto dcopbop;
2015
2016                         /* unary  ops */
2017                 case fsqrt_op:
2018                         if (!cpu_has_mips_2_3_4_5_r)
2019                                 return SIGILL;
2020
2021                         handler.u = ieee754dp_sqrt;
2022                         goto dcopuop;
2023                 /*
2024                  * Note that on some MIPS IV implementations such as the
2025                  * R5000 and R8000 the FSQRT and FRECIP instructions do not
2026                  * achieve full IEEE-754 accuracy - however this emulator does.
2027                  */
2028                 case frsqrt_op:
2029                         if (!cpu_has_mips_4_5_64_r2_r6)
2030                                 return SIGILL;
2031
2032                         handler.u = fpemu_dp_rsqrt;
2033                         goto dcopuop;
2034                 case frecip_op:
2035                         if (!cpu_has_mips_4_5_64_r2_r6)
2036                                 return SIGILL;
2037
2038                         handler.u = fpemu_dp_recip;
2039                         goto dcopuop;
2040                 case fmovc_op:
2041                         if (!cpu_has_mips_4_5_r)
2042                                 return SIGILL;
2043
2044                         cond = fpucondbit[MIPSInst_FT(ir) >> 2];
2045                         if (((ctx->fcr31 & cond) != 0) !=
2046                                 ((MIPSInst_FT(ir) & 1) != 0))
2047                                 return 0;
2048                         DPFROMREG(rv.d, MIPSInst_FS(ir));
2049                         break;
2050                 case fmovz_op:
2051                         if (!cpu_has_mips_4_5_r)
2052                                 return SIGILL;
2053
2054                         if (xcp->regs[MIPSInst_FT(ir)] != 0)
2055                                 return 0;
2056                         DPFROMREG(rv.d, MIPSInst_FS(ir));
2057                         break;
2058                 case fmovn_op:
2059                         if (!cpu_has_mips_4_5_r)
2060                                 return SIGILL;
2061
2062                         if (xcp->regs[MIPSInst_FT(ir)] == 0)
2063                                 return 0;
2064                         DPFROMREG(rv.d, MIPSInst_FS(ir));
2065                         break;
2066
2067                 case fseleqz_op:
2068                         if (!cpu_has_mips_r6)
2069                                 return SIGILL;
2070
2071                         DPFROMREG(rv.d, MIPSInst_FT(ir));
2072                         if (rv.l & 0x1)
2073                                 rv.l = 0;
2074                         else
2075                                 DPFROMREG(rv.d, MIPSInst_FS(ir));
2076                         break;
2077
2078                 case fselnez_op:
2079                         if (!cpu_has_mips_r6)
2080                                 return SIGILL;
2081
2082                         DPFROMREG(rv.d, MIPSInst_FT(ir));
2083                         if (rv.l & 0x1)
2084                                 DPFROMREG(rv.d, MIPSInst_FS(ir));
2085                         else
2086                                 rv.l = 0;
2087                         break;
2088
2089                 case fmaddf_op: {
2090                         union ieee754dp ft, fs, fd;
2091
2092                         if (!cpu_has_mips_r6)
2093                                 return SIGILL;
2094
2095                         DPFROMREG(ft, MIPSInst_FT(ir));
2096                         DPFROMREG(fs, MIPSInst_FS(ir));
2097                         DPFROMREG(fd, MIPSInst_FD(ir));
2098                         rv.d = ieee754dp_maddf(fd, fs, ft);
2099                         goto copcsr;
2100                 }
2101
2102                 case fmsubf_op: {
2103                         union ieee754dp ft, fs, fd;
2104
2105                         if (!cpu_has_mips_r6)
2106                                 return SIGILL;
2107
2108                         DPFROMREG(ft, MIPSInst_FT(ir));
2109                         DPFROMREG(fs, MIPSInst_FS(ir));
2110                         DPFROMREG(fd, MIPSInst_FD(ir));
2111                         rv.d = ieee754dp_msubf(fd, fs, ft);
2112                         goto copcsr;
2113                 }
2114
2115                 case frint_op: {
2116                         union ieee754dp fs;
2117
2118                         if (!cpu_has_mips_r6)
2119                                 return SIGILL;
2120
2121                         DPFROMREG(fs, MIPSInst_FS(ir));
2122                         rv.l = ieee754dp_tlong(fs);
2123                         rv.d = ieee754dp_flong(rv.l);
2124                         goto copcsr;
2125                 }
2126
2127                 case fclass_op: {
2128                         union ieee754dp fs;
2129
2130                         if (!cpu_has_mips_r6)
2131                                 return SIGILL;
2132
2133                         DPFROMREG(fs, MIPSInst_FS(ir));
2134                         rv.w = ieee754dp_2008class(fs);
2135                         rfmt = w_fmt;
2136                         goto copcsr;
2137                 }
2138
2139                 case fmin_op: {
2140                         union ieee754dp fs, ft;
2141
2142                         if (!cpu_has_mips_r6)
2143                                 return SIGILL;
2144
2145                         DPFROMREG(ft, MIPSInst_FT(ir));
2146                         DPFROMREG(fs, MIPSInst_FS(ir));
2147                         rv.d = ieee754dp_fmin(fs, ft);
2148                         goto copcsr;
2149                 }
2150
2151                 case fmina_op: {
2152                         union ieee754dp fs, ft;
2153
2154                         if (!cpu_has_mips_r6)
2155                                 return SIGILL;
2156
2157                         DPFROMREG(ft, MIPSInst_FT(ir));
2158                         DPFROMREG(fs, MIPSInst_FS(ir));
2159                         rv.d = ieee754dp_fmina(fs, ft);
2160                         goto copcsr;
2161                 }
2162
2163                 case fmax_op: {
2164                         union ieee754dp fs, ft;
2165
2166                         if (!cpu_has_mips_r6)
2167                                 return SIGILL;
2168
2169                         DPFROMREG(ft, MIPSInst_FT(ir));
2170                         DPFROMREG(fs, MIPSInst_FS(ir));
2171                         rv.d = ieee754dp_fmax(fs, ft);
2172                         goto copcsr;
2173                 }
2174
2175                 case fmaxa_op: {
2176                         union ieee754dp fs, ft;
2177
2178                         if (!cpu_has_mips_r6)
2179                                 return SIGILL;
2180
2181                         DPFROMREG(ft, MIPSInst_FT(ir));
2182                         DPFROMREG(fs, MIPSInst_FS(ir));
2183                         rv.d = ieee754dp_fmaxa(fs, ft);
2184                         goto copcsr;
2185                 }
2186
2187                 case fabs_op:
2188                         handler.u = ieee754dp_abs;
2189                         goto dcopuop;
2190
2191                 case fneg_op:
2192                         handler.u = ieee754dp_neg;
2193                         goto dcopuop;
2194
2195                 case fmov_op:
2196                         /* an easy one */
2197                         DPFROMREG(rv.d, MIPSInst_FS(ir));
2198                         goto copcsr;
2199
2200                         /* binary op on handler */
2201 dcopbop:
2202                         DPFROMREG(fs, MIPSInst_FS(ir));
2203                         DPFROMREG(ft, MIPSInst_FT(ir));
2204
2205                         rv.d = (*handler.b) (fs, ft);
2206                         goto copcsr;
2207 dcopuop:
2208                         DPFROMREG(fs, MIPSInst_FS(ir));
2209                         rv.d = (*handler.u) (fs);
2210                         goto copcsr;
2211
2212                 /*
2213                  * unary conv ops
2214                  */
2215                 case fcvts_op:
2216                         DPFROMREG(fs, MIPSInst_FS(ir));
2217                         rv.s = ieee754sp_fdp(fs);
2218                         rfmt = s_fmt;
2219                         goto copcsr;
2220
2221                 case fcvtd_op:
2222                         return SIGILL;  /* not defined */
2223
2224                 case fcvtw_op:
2225                         DPFROMREG(fs, MIPSInst_FS(ir));
2226                         rv.w = ieee754dp_tint(fs);      /* wrong */
2227                         rfmt = w_fmt;
2228                         goto copcsr;
2229
2230                 case fround_op:
2231                 case ftrunc_op:
2232                 case fceil_op:
2233                 case ffloor_op:
2234                         if (!cpu_has_mips_2_3_4_5_r)
2235                                 return SIGILL;
2236
2237                         oldrm = ieee754_csr.rm;
2238                         DPFROMREG(fs, MIPSInst_FS(ir));
2239                         ieee754_csr.rm = MIPSInst_FUNC(ir);
2240                         rv.w = ieee754dp_tint(fs);
2241                         ieee754_csr.rm = oldrm;
2242                         rfmt = w_fmt;
2243                         goto copcsr;
2244
2245                 case fcvtl_op:
2246                         if (!cpu_has_mips_3_4_5_64_r2_r6)
2247                                 return SIGILL;
2248
2249                         DPFROMREG(fs, MIPSInst_FS(ir));
2250                         rv.l = ieee754dp_tlong(fs);
2251                         rfmt = l_fmt;
2252                         goto copcsr;
2253
2254                 case froundl_op:
2255                 case ftruncl_op:
2256                 case fceill_op:
2257                 case ffloorl_op:
2258                         if (!cpu_has_mips_3_4_5_64_r2_r6)
2259                                 return SIGILL;
2260
2261                         oldrm = ieee754_csr.rm;
2262                         DPFROMREG(fs, MIPSInst_FS(ir));
2263                         ieee754_csr.rm = MIPSInst_FUNC(ir);
2264                         rv.l = ieee754dp_tlong(fs);
2265                         ieee754_csr.rm = oldrm;
2266                         rfmt = l_fmt;
2267                         goto copcsr;
2268
2269                 default:
2270                         if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
2271                                 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
2272                                 union ieee754dp fs, ft;
2273
2274                                 DPFROMREG(fs, MIPSInst_FS(ir));
2275                                 DPFROMREG(ft, MIPSInst_FT(ir));
2276                                 rv.w = ieee754dp_cmp(fs, ft,
2277                                         cmptab[cmpop & 0x7], cmpop & 0x8);
2278                                 rfmt = -1;
2279                                 if ((cmpop & 0x8)
2280                                         &&
2281                                         ieee754_cxtest
2282                                         (IEEE754_INVALID_OPERATION))
2283                                         rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2284                                 else
2285                                         goto copcsr;
2286
2287                         }
2288                         else {
2289                                 return SIGILL;
2290                         }
2291                         break;
2292                 }
2293                 break;
2294         }
2295
2296         case w_fmt: {
2297                 union ieee754dp fs;
2298
2299                 switch (MIPSInst_FUNC(ir)) {
2300                 case fcvts_op:
2301                         /* convert word to single precision real */
2302                         SPFROMREG(fs, MIPSInst_FS(ir));
2303                         rv.s = ieee754sp_fint(fs.bits);
2304                         rfmt = s_fmt;
2305                         goto copcsr;
2306                 case fcvtd_op:
2307                         /* convert word to double precision real */
2308                         SPFROMREG(fs, MIPSInst_FS(ir));
2309                         rv.d = ieee754dp_fint(fs.bits);
2310                         rfmt = d_fmt;
2311                         goto copcsr;
2312                 default: {
2313                         /* Emulating the new CMP.condn.fmt R6 instruction */
2314 #define CMPOP_MASK      0x7
2315 #define SIGN_BIT        (0x1 << 3)
2316 #define PREDICATE_BIT   (0x1 << 4)
2317
2318                         int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2319                         int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2320                         union ieee754sp fs, ft;
2321
2322                         /* This is an R6 only instruction */
2323                         if (!cpu_has_mips_r6 ||
2324                             (MIPSInst_FUNC(ir) & 0x20))
2325                                 return SIGILL;
2326
2327                         /* fmt is w_fmt for single precision so fix it */
2328                         rfmt = s_fmt;
2329                         /* default to false */
2330                         rv.w = 0;
2331
2332                         /* CMP.condn.S */
2333                         SPFROMREG(fs, MIPSInst_FS(ir));
2334                         SPFROMREG(ft, MIPSInst_FT(ir));
2335
2336                         /* positive predicates */
2337                         if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2338                                 if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
2339                                                   sig))
2340                                     rv.w = -1; /* true, all 1s */
2341                                 if ((sig) &&
2342                                     ieee754_cxtest(IEEE754_INVALID_OPERATION))
2343                                         rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2344                                 else
2345                                         goto copcsr;
2346                         } else {
2347                                 /* negative predicates */
2348                                 switch (cmpop) {
2349                                 case 1:
2350                                 case 2:
2351                                 case 3:
2352                                         if (ieee754sp_cmp(fs, ft,
2353                                                           negative_cmptab[cmpop],
2354                                                           sig))
2355                                                 rv.w = -1; /* true, all 1s */
2356                                         if (sig &&
2357                                             ieee754_cxtest(IEEE754_INVALID_OPERATION))
2358                                                 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2359                                         else
2360                                                 goto copcsr;
2361                                         break;
2362                                 default:
2363                                         /* Reserved R6 ops */
2364                                         return SIGILL;
2365                                 }
2366                         }
2367                         break;
2368                         }
2369                 }
2370         }
2371
2372         case l_fmt:
2373
2374                 if (!cpu_has_mips_3_4_5_64_r2_r6)
2375                         return SIGILL;
2376
2377                 DIFROMREG(bits, MIPSInst_FS(ir));
2378
2379                 switch (MIPSInst_FUNC(ir)) {
2380                 case fcvts_op:
2381                         /* convert long to single precision real */
2382                         rv.s = ieee754sp_flong(bits);
2383                         rfmt = s_fmt;
2384                         goto copcsr;
2385                 case fcvtd_op:
2386                         /* convert long to double precision real */
2387                         rv.d = ieee754dp_flong(bits);
2388                         rfmt = d_fmt;
2389                         goto copcsr;
2390                 default: {
2391                         /* Emulating the new CMP.condn.fmt R6 instruction */
2392                         int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2393                         int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2394                         union ieee754dp fs, ft;
2395
2396                         if (!cpu_has_mips_r6 ||
2397                             (MIPSInst_FUNC(ir) & 0x20))
2398                                 return SIGILL;
2399
2400                         /* fmt is l_fmt for double precision so fix it */
2401                         rfmt = d_fmt;
2402                         /* default to false */
2403                         rv.l = 0;
2404
2405                         /* CMP.condn.D */
2406                         DPFROMREG(fs, MIPSInst_FS(ir));
2407                         DPFROMREG(ft, MIPSInst_FT(ir));
2408
2409                         /* positive predicates */
2410                         if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2411                                 if (ieee754dp_cmp(fs, ft,
2412                                                   cmptab[cmpop], sig))
2413                                     rv.l = -1LL; /* true, all 1s */
2414                                 if (sig &&
2415                                     ieee754_cxtest(IEEE754_INVALID_OPERATION))
2416                                         rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2417                                 else
2418                                         goto copcsr;
2419                         } else {
2420                                 /* negative predicates */
2421                                 switch (cmpop) {
2422                                 case 1:
2423                                 case 2:
2424                                 case 3:
2425                                         if (ieee754dp_cmp(fs, ft,
2426                                                           negative_cmptab[cmpop],
2427                                                           sig))
2428                                                 rv.l = -1LL; /* true, all 1s */
2429                                         if (sig &&
2430                                             ieee754_cxtest(IEEE754_INVALID_OPERATION))
2431                                                 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2432                                         else
2433                                                 goto copcsr;
2434                                         break;
2435                                 default:
2436                                         /* Reserved R6 ops */
2437                                         return SIGILL;
2438                                 }
2439                         }
2440                         break;
2441                         }
2442                 }
2443         default:
2444                 return SIGILL;
2445         }
2446
2447         /*
2448          * Update the fpu CSR register for this operation.
2449          * If an exception is required, generate a tidy SIGFPE exception,
2450          * without updating the result register.
2451          * Note: cause exception bits do not accumulate, they are rewritten
2452          * for each op; only the flag/sticky bits accumulate.
2453          */
2454         ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
2455         if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
2456                 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
2457                 return SIGFPE;
2458         }
2459
2460         /*
2461          * Now we can safely write the result back to the register file.
2462          */
2463         switch (rfmt) {
2464         case -1:
2465
2466                 if (cpu_has_mips_4_5_r)
2467                         cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
2468                 else
2469                         cbit = FPU_CSR_COND;
2470                 if (rv.w)
2471                         ctx->fcr31 |= cbit;
2472                 else
2473                         ctx->fcr31 &= ~cbit;
2474                 break;
2475
2476         case d_fmt:
2477                 DPTOREG(rv.d, MIPSInst_FD(ir));
2478                 break;
2479         case s_fmt:
2480                 SPTOREG(rv.s, MIPSInst_FD(ir));
2481                 break;
2482         case w_fmt:
2483                 SITOREG(rv.w, MIPSInst_FD(ir));
2484                 break;
2485         case l_fmt:
2486                 if (!cpu_has_mips_3_4_5_64_r2_r6)
2487                         return SIGILL;
2488
2489                 DITOREG(rv.l, MIPSInst_FD(ir));
2490                 break;
2491         default:
2492                 return SIGILL;
2493         }
2494
2495         return 0;
2496 }
2497
2498 /*
2499  * Emulate FPU instructions.
2500  *
2501  * If we use FPU hardware, then we have been typically called to handle
2502  * an unimplemented operation, such as where an operand is a NaN or
2503  * denormalized.  In that case exit the emulation loop after a single
2504  * iteration so as to let hardware execute any subsequent instructions.
2505  *
2506  * If we have no FPU hardware or it has been disabled, then continue
2507  * emulating floating-point instructions until one of these conditions
2508  * has occurred:
2509  *
2510  * - a non-FPU instruction has been encountered,
2511  *
2512  * - an attempt to emulate has ended with a signal,
2513  *
2514  * - the ISA mode has been switched.
2515  *
2516  * We need to terminate the emulation loop if we got switched to the
2517  * MIPS16 mode, whether supported or not, so that we do not attempt
2518  * to emulate a MIPS16 instruction as a regular MIPS FPU instruction.
2519  * Similarly if we got switched to the microMIPS mode and only the
2520  * regular MIPS mode is supported, so that we do not attempt to emulate
2521  * a microMIPS instruction as a regular MIPS FPU instruction.  Or if
2522  * we got switched to the regular MIPS mode and only the microMIPS mode
2523  * is supported, so that we do not attempt to emulate a regular MIPS
2524  * instruction that should cause an Address Error exception instead.
2525  * For simplicity we always terminate upon an ISA mode switch.
2526  */
2527 int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2528         int has_fpu, void *__user *fault_addr)
2529 {
2530         unsigned long oldepc, prevepc;
2531         struct mm_decoded_insn dec_insn;
2532         u16 instr[4];
2533         u16 *instr_ptr;
2534         int sig = 0;
2535
2536         oldepc = xcp->cp0_epc;
2537         do {
2538                 prevepc = xcp->cp0_epc;
2539
2540                 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2541                         /*
2542                          * Get next 2 microMIPS instructions and convert them
2543                          * into 32-bit instructions.
2544                          */
2545                         if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2546                             (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2547                             (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2548                             (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2549                                 MIPS_FPU_EMU_INC_STATS(errors);
2550                                 return SIGBUS;
2551                         }
2552                         instr_ptr = instr;
2553
2554                         /* Get first instruction. */
2555                         if (mm_insn_16bit(*instr_ptr)) {
2556                                 /* Duplicate the half-word. */
2557                                 dec_insn.insn = (*instr_ptr << 16) |
2558                                         (*instr_ptr);
2559                                 /* 16-bit instruction. */
2560                                 dec_insn.pc_inc = 2;
2561                                 instr_ptr += 1;
2562                         } else {
2563                                 dec_insn.insn = (*instr_ptr << 16) |
2564                                         *(instr_ptr+1);
2565                                 /* 32-bit instruction. */
2566                                 dec_insn.pc_inc = 4;
2567                                 instr_ptr += 2;
2568                         }
2569                         /* Get second instruction. */
2570                         if (mm_insn_16bit(*instr_ptr)) {
2571                                 /* Duplicate the half-word. */
2572                                 dec_insn.next_insn = (*instr_ptr << 16) |
2573                                         (*instr_ptr);
2574                                 /* 16-bit instruction. */
2575                                 dec_insn.next_pc_inc = 2;
2576                         } else {
2577                                 dec_insn.next_insn = (*instr_ptr << 16) |
2578                                         *(instr_ptr+1);
2579                                 /* 32-bit instruction. */
2580                                 dec_insn.next_pc_inc = 4;
2581                         }
2582                         dec_insn.micro_mips_mode = 1;
2583                 } else {
2584                         if ((get_user(dec_insn.insn,
2585                             (mips_instruction __user *) xcp->cp0_epc)) ||
2586                             (get_user(dec_insn.next_insn,
2587                             (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2588                                 MIPS_FPU_EMU_INC_STATS(errors);
2589                                 return SIGBUS;
2590                         }
2591                         dec_insn.pc_inc = 4;
2592                         dec_insn.next_pc_inc = 4;
2593                         dec_insn.micro_mips_mode = 0;
2594                 }
2595
2596                 if ((dec_insn.insn == 0) ||
2597                    ((dec_insn.pc_inc == 2) &&
2598                    ((dec_insn.insn & 0xffff) == MM_NOP16)))
2599                         xcp->cp0_epc += dec_insn.pc_inc;        /* Skip NOPs */
2600                 else {
2601                         /*
2602                          * The 'ieee754_csr' is an alias of ctx->fcr31.
2603                          * No need to copy ctx->fcr31 to ieee754_csr.
2604                          */
2605                         sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
2606                 }
2607
2608                 if (has_fpu)
2609                         break;
2610                 if (sig)
2611                         break;
2612                 /*
2613                  * We have to check for the ISA bit explicitly here,
2614                  * because `get_isa16_mode' may return 0 if support
2615                  * for code compression has been globally disabled,
2616                  * or otherwise we may produce the wrong signal or
2617                  * even proceed successfully where we must not.
2618                  */
2619                 if ((xcp->cp0_epc ^ prevepc) & 0x1)
2620                         break;
2621
2622                 cond_resched();
2623         } while (xcp->cp0_epc > prevepc);
2624
2625         /* SIGILL indicates a non-fpu instruction */
2626         if (sig == SIGILL && xcp->cp0_epc != oldepc)
2627                 /* but if EPC has advanced, then ignore it */
2628                 sig = 0;
2629
2630         return sig;
2631 }