blob: 890f77927d628b0c203c92c7712415a9c52a819f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * cp1emu.c: a MIPS coprocessor 1 (fpu) instruction emulator
3 *
4 * MIPS floating point support
5 * Copyright (C) 1994-2000 Algorithmics Ltd.
6 * http://www.algor.co.uk
7 *
8 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
9 * Copyright (C) 2000 MIPS Technologies, Inc.
10 *
11 * This program is free software; you can distribute it and/or modify it
12 * under the terms of the GNU General Public License (Version 2) as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 *
24 * A complete emulator for MIPS coprocessor 1 instructions. This is
25 * required for #float(switch) or #float(trap), where it catches all
26 * COP1 instructions via the "CoProcessor Unusable" exception.
27 *
28 * More surprisingly it is also required for #float(ieee), to help out
29 * the hardware fpu at the boundaries of the IEEE-754 representation
30 * (denormalised values, infinities, underflow, etc). It is made
31 * quite nasty because emulation of some non-COP1 instructions is
32 * required, e.g. in branch delay slots.
33 *
34 * Note if you know that you won't have an fpu, then you'll get much
35 * better performance by compiling with -msoft-float!
36 */
37#include <linux/sched.h>
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +090038#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/inst.h>
41#include <asm/bootinfo.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/processor.h>
43#include <asm/ptrace.h>
44#include <asm/signal.h>
45#include <asm/mipsregs.h>
46#include <asm/fpu_emulator.h>
47#include <asm/uaccess.h>
48#include <asm/branch.h>
49
50#include "ieee754.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* Strap kernel emulator for full MIPS IV emulation */
53
54#ifdef __mips
55#undef __mips
56#endif
57#define __mips 4
58
59/* Function which emulates a floating point instruction. */
60
Atsushi Nemotoeae89072006-05-16 01:26:03 +090061static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 mips_instruction);
63
64#if __mips >= 4 && __mips != 32
65static int fpux_emu(struct pt_regs *,
Atsushi Nemotoeae89072006-05-16 01:26:03 +090066 struct mips_fpu_struct *, mips_instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#endif
68
Atsushi Nemotoeae89072006-05-16 01:26:03 +090069/* Further private data for which no space exists in mips_fpu_struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Ralf Baechle4a99d1e2005-05-11 12:02:48 +000071struct mips_fpu_emulator_stats fpuemustats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/* Control registers */
74
75#define FPCREG_RID 0 /* $0 = revision id */
76#define FPCREG_CSR 31 /* $31 = csr */
77
78/* Convert Mips rounding mode (0..3) to IEEE library modes. */
79static const unsigned char ieee_rm[4] = {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000080 [FPU_CSR_RN] = IEEE754_RN,
81 [FPU_CSR_RZ] = IEEE754_RZ,
82 [FPU_CSR_RU] = IEEE754_RU,
83 [FPU_CSR_RD] = IEEE754_RD,
84};
85/* Convert IEEE library modes to Mips rounding mode (0..3). */
86static const unsigned char mips_rm[4] = {
87 [IEEE754_RN] = FPU_CSR_RN,
88 [IEEE754_RZ] = FPU_CSR_RZ,
89 [IEEE754_RD] = FPU_CSR_RD,
90 [IEEE754_RU] = FPU_CSR_RU,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
93#if __mips >= 4
94/* convert condition code register number to csr bit */
95static const unsigned int fpucondbit[8] = {
96 FPU_CSR_COND0,
97 FPU_CSR_COND1,
98 FPU_CSR_COND2,
99 FPU_CSR_COND3,
100 FPU_CSR_COND4,
101 FPU_CSR_COND5,
102 FPU_CSR_COND6,
103 FPU_CSR_COND7
104};
105#endif
106
107
108/*
109 * Redundant with logic already in kernel/branch.c,
110 * embedded in compute_return_epc. At some point,
111 * a single subroutine should be used across both
112 * modules.
113 */
114static int isBranchInstr(mips_instruction * i)
115{
116 switch (MIPSInst_OPCODE(*i)) {
117 case spec_op:
118 switch (MIPSInst_FUNC(*i)) {
119 case jalr_op:
120 case jr_op:
121 return 1;
122 }
123 break;
124
125 case bcond_op:
126 switch (MIPSInst_RT(*i)) {
127 case bltz_op:
128 case bgez_op:
129 case bltzl_op:
130 case bgezl_op:
131 case bltzal_op:
132 case bgezal_op:
133 case bltzall_op:
134 case bgezall_op:
135 return 1;
136 }
137 break;
138
139 case j_op:
140 case jal_op:
141 case jalx_op:
142 case beq_op:
143 case bne_op:
144 case blez_op:
145 case bgtz_op:
146 case beql_op:
147 case bnel_op:
148 case blezl_op:
149 case bgtzl_op:
150 return 1;
151
152 case cop0_op:
153 case cop1_op:
154 case cop2_op:
155 case cop1x_op:
156 if (MIPSInst_RS(*i) == bc_op)
157 return 1;
158 break;
159 }
160
161 return 0;
162}
163
164/*
165 * In the Linux kernel, we support selection of FPR format on the
166 * basis of the Status.FR bit. This does imply that, if a full 32
167 * FPRs are desired, there needs to be a flip-flop that can be written
168 * to one at that bit position. In any case, O32 MIPS ABI uses
169 * only the even FPRs (Status.FR = 0).
170 */
171
172#define CP0_STATUS_FR_SUPPORT
173
174#ifdef CP0_STATUS_FR_SUPPORT
175#define FR_BIT ST0_FR
176#else
177#define FR_BIT 0
178#endif
179
Ralf Baechle21a151d2007-10-11 23:46:15 +0100180#define SIFROMREG(si, x) ((si) = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 (xcp->cp0_status & FR_BIT) || !(x & 1) ? \
182 (int)ctx->fpr[x] : \
183 (int)(ctx->fpr[x & ~1] >> 32 ))
Ralf Baechle21a151d2007-10-11 23:46:15 +0100184#define SITOREG(si, x) (ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 (xcp->cp0_status & FR_BIT) || !(x & 1) ? \
186 ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
187 ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
188
Ralf Baechle21a151d2007-10-11 23:46:15 +0100189#define DIFROMREG(di, x) ((di) = \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)])
Ralf Baechle21a151d2007-10-11 23:46:15 +0100191#define DITOREG(di, x) (ctx->fpr[x & ~((xcp->cp0_status & FR_BIT) == 0)] \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 = (di))
193
Ralf Baechle21a151d2007-10-11 23:46:15 +0100194#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
195#define SPTOREG(sp, x) SITOREG((sp).bits, x)
196#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
197#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199/*
200 * Emulate the single floating point instruction pointed at by EPC.
201 * Two instructions if the instruction is in a branch delay slot.
202 */
203
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900204static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 mips_instruction ir;
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900207 unsigned long emulpc, contpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned int cond;
209
Ralf Baechle3fccc012005-10-23 13:58:21 +0100210 if (get_user(ir, (mips_instruction __user *) xcp->cp0_epc)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000211 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return SIGBUS;
213 }
214
215 /* XXX NEC Vr54xx bug workaround */
216 if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir))
217 xcp->cp0_cause &= ~CAUSEF_BD;
218
219 if (xcp->cp0_cause & CAUSEF_BD) {
220 /*
221 * The instruction to be emulated is in a branch delay slot
222 * which means that we have to emulate the branch instruction
223 * BEFORE we do the cop1 instruction.
224 *
225 * This branch could be a COP1 branch, but in that case we
226 * would have had a trap for that instruction, and would not
227 * come through this route.
228 *
229 * Linux MIPS branch emulator operates on context, updating the
230 * cp0_epc.
231 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900232 emulpc = xcp->cp0_epc + 4; /* Snapshot emulation target */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (__compute_return_epc(xcp)) {
235#ifdef CP1DBG
236 printk("failed to emulate branch at %p\n",
Ralf Baechle333d1f62005-02-28 17:55:57 +0000237 (void *) (xcp->cp0_epc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#endif
239 return SIGILL;
240 }
Ralf Baechle3fccc012005-10-23 13:58:21 +0100241 if (get_user(ir, (mips_instruction __user *) emulpc)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000242 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return SIGBUS;
244 }
245 /* __compute_return_epc() will have updated cp0_epc */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900246 contpc = xcp->cp0_epc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* In order not to confuse ptrace() et al, tweak context */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900248 xcp->cp0_epc = emulpc - 4;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000249 } else {
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900250 emulpc = xcp->cp0_epc;
251 contpc = xcp->cp0_epc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
254 emul:
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000255 fpuemustats.emulated++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 switch (MIPSInst_OPCODE(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 case ldc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +0100258 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 MIPSInst_SIMM(ir));
260 u64 val;
261
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000262 fpuemustats.loads++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (get_user(val, va)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000264 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return SIGBUS;
266 }
267 DITOREG(val, MIPSInst_RT(ir));
268 break;
269 }
270
271 case sdc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +0100272 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 MIPSInst_SIMM(ir));
274 u64 val;
275
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000276 fpuemustats.stores++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 DIFROMREG(val, MIPSInst_RT(ir));
278 if (put_user(val, va)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000279 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return SIGBUS;
281 }
282 break;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 case lwc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +0100286 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 MIPSInst_SIMM(ir));
288 u32 val;
289
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000290 fpuemustats.loads++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (get_user(val, va)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000292 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return SIGBUS;
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 SITOREG(val, MIPSInst_RT(ir));
296 break;
297 }
298
299 case swc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +0100300 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 MIPSInst_SIMM(ir));
302 u32 val;
303
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000304 fpuemustats.stores++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 SIFROMREG(val, MIPSInst_RT(ir));
306 if (put_user(val, va)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000307 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return SIGBUS;
309 }
310 break;
311 }
312
313 case cop1_op:
314 switch (MIPSInst_RS(ir)) {
315
Ralf Baechle4b724ef2005-10-23 15:05:47 +0100316#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 case dmfc_op:
318 /* copregister fs -> gpr[rt] */
319 if (MIPSInst_RT(ir) != 0) {
320 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
321 MIPSInst_RD(ir));
322 }
323 break;
324
325 case dmtc_op:
326 /* copregister fs <- rt */
327 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
328 break;
329#endif
330
331 case mfc_op:
332 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (MIPSInst_RT(ir) != 0) {
334 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
335 MIPSInst_RD(ir));
336 }
337 break;
338
339 case mtc_op:
340 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
342 break;
343
344 case cfc_op:{
345 /* cop control register rd -> gpr[rt] */
346 u32 value;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (MIPSInst_RD(ir) == FPCREG_CSR) {
349 value = ctx->fcr31;
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000350 value = (value & ~0x3) | mips_rm[value & 0x3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#ifdef CSRTRACE
352 printk("%p gpr[%d]<-csr=%08x\n",
Ralf Baechle333d1f62005-02-28 17:55:57 +0000353 (void *) (xcp->cp0_epc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 MIPSInst_RT(ir), value);
355#endif
356 }
357 else if (MIPSInst_RD(ir) == FPCREG_RID)
358 value = 0;
359 else
360 value = 0;
361 if (MIPSInst_RT(ir))
362 xcp->regs[MIPSInst_RT(ir)] = value;
363 break;
364 }
365
366 case ctc_op:{
367 /* copregister rd <- rt */
368 u32 value;
369
370 if (MIPSInst_RT(ir) == 0)
371 value = 0;
372 else
373 value = xcp->regs[MIPSInst_RT(ir)];
374
375 /* we only have one writable control reg
376 */
377 if (MIPSInst_RD(ir) == FPCREG_CSR) {
378#ifdef CSRTRACE
379 printk("%p gpr[%d]->csr=%08x\n",
Ralf Baechle333d1f62005-02-28 17:55:57 +0000380 (void *) (xcp->cp0_epc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 MIPSInst_RT(ir), value);
382#endif
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000383 value &= (FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
384 ctx->fcr31 &= ~(FPU_CSR_FLUSH | FPU_CSR_ALL_E | FPU_CSR_ALL_S | 0x03);
385 /* convert to ieee library modes */
386 ctx->fcr31 |= (value & ~0x3) | ieee_rm[value & 0x3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
389 return SIGFPE;
390 }
391 break;
392 }
393
394 case bc_op:{
395 int likely = 0;
396
397 if (xcp->cp0_cause & CAUSEF_BD)
398 return SIGILL;
399
400#if __mips >= 4
401 cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
402#else
403 cond = ctx->fcr31 & FPU_CSR_COND;
404#endif
405 switch (MIPSInst_RT(ir) & 3) {
406 case bcfl_op:
407 likely = 1;
408 case bcf_op:
409 cond = !cond;
410 break;
411 case bctl_op:
412 likely = 1;
413 case bct_op:
414 break;
415 default:
416 /* thats an illegal instruction */
417 return SIGILL;
418 }
419
420 xcp->cp0_cause |= CAUSEF_BD;
421 if (cond) {
422 /* branch taken: emulate dslot
423 * instruction
424 */
425 xcp->cp0_epc += 4;
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900426 contpc = (xcp->cp0_epc +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 (MIPSInst_SIMM(ir) << 2));
428
Ralf Baechle3fccc012005-10-23 13:58:21 +0100429 if (get_user(ir,
430 (mips_instruction __user *) xcp->cp0_epc)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000431 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return SIGBUS;
433 }
434
435 switch (MIPSInst_OPCODE(ir)) {
436 case lwc1_op:
437 case swc1_op:
Ralf Baechle4b724ef2005-10-23 15:05:47 +0100438#if (__mips >= 2 || defined(__mips64))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 case ldc1_op:
440 case sdc1_op:
441#endif
442 case cop1_op:
443#if __mips >= 4 && __mips != 32
444 case cop1x_op:
445#endif
446 /* its one of ours */
447 goto emul;
448#if __mips >= 4
449 case spec_op:
450 if (MIPSInst_FUNC(ir) == movc_op)
451 goto emul;
452 break;
453#endif
454 }
455
456 /*
457 * Single step the non-cp1
458 * instruction in the dslot
459 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900460 return mips_dsemul(xcp, ir, contpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 else {
463 /* branch not taken */
464 if (likely) {
465 /*
466 * branch likely nullifies
467 * dslot if not taken
468 */
469 xcp->cp0_epc += 4;
470 contpc += 4;
471 /*
472 * else continue & execute
473 * dslot as normal insn
474 */
475 }
476 }
477 break;
478 }
479
480 default:
481 if (!(MIPSInst_RS(ir) & 0x10))
482 return SIGILL;
483 {
484 int sig;
485
486 /* a real fpu computation instruction */
487 if ((sig = fpu_emu(xcp, ctx, ir)))
488 return sig;
489 }
490 }
491 break;
492
493#if __mips >= 4 && __mips != 32
494 case cop1x_op:{
495 int sig;
496
497 if ((sig = fpux_emu(xcp, ctx, ir)))
498 return sig;
499 break;
500 }
501#endif
502
503#if __mips >= 4
504 case spec_op:
505 if (MIPSInst_FUNC(ir) != movc_op)
506 return SIGILL;
507 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
508 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
509 xcp->regs[MIPSInst_RD(ir)] =
510 xcp->regs[MIPSInst_RS(ir)];
511 break;
512#endif
513
514 default:
515 return SIGILL;
516 }
517
518 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900519 xcp->cp0_epc = contpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 xcp->cp0_cause &= ~CAUSEF_BD;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return 0;
523}
524
525/*
526 * Conversion table from MIPS compare ops 48-63
527 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
528 */
529static const unsigned char cmptab[8] = {
530 0, /* cmp_0 (sig) cmp_sf */
531 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
532 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
533 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
534 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
535 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
536 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
537 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
538};
539
540
541#if __mips >= 4 && __mips != 32
542
543/*
544 * Additional MIPS4 instructions
545 */
546
547#define DEF3OP(name, p, f1, f2, f3) \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100548static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 ieee754##p t) \
550{ \
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000551 struct _ieee754_csr ieee754_csr_save; \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100552 s = f1(s, t); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 ieee754_csr_save = ieee754_csr; \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100554 s = f2(s, r); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ieee754_csr_save.cx |= ieee754_csr.cx; \
556 ieee754_csr_save.sx |= ieee754_csr.sx; \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100557 s = f3(s); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 ieee754_csr.cx |= ieee754_csr_save.cx; \
559 ieee754_csr.sx |= ieee754_csr_save.sx; \
560 return s; \
561}
562
563static ieee754dp fpemu_dp_recip(ieee754dp d)
564{
565 return ieee754dp_div(ieee754dp_one(0), d);
566}
567
568static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
569{
570 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
571}
572
573static ieee754sp fpemu_sp_recip(ieee754sp s)
574{
575 return ieee754sp_div(ieee754sp_one(0), s);
576}
577
578static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
579{
580 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
581}
582
Ralf Baechle21a151d2007-10-11 23:46:15 +0100583DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
584DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
586DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +0100587DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
588DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
590DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
591
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900592static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 mips_instruction ir)
594{
595 unsigned rcsr = 0; /* resulting csr */
596
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000597 fpuemustats.cp1xops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 switch (MIPSInst_FMA_FFMT(ir)) {
600 case s_fmt:{ /* 0 */
601
602 ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
603 ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +0100604 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 u32 val;
606
607 switch (MIPSInst_FUNC(ir)) {
608 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +0100609 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 xcp->regs[MIPSInst_FT(ir)]);
611
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000612 fpuemustats.loads++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (get_user(val, va)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000614 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return SIGBUS;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 SITOREG(val, MIPSInst_FD(ir));
618 break;
619
620 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +0100621 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 xcp->regs[MIPSInst_FT(ir)]);
623
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000624 fpuemustats.stores++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 SIFROMREG(val, MIPSInst_FS(ir));
627 if (put_user(val, va)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000628 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return SIGBUS;
630 }
631 break;
632
633 case madd_s_op:
634 handler = fpemu_sp_madd;
635 goto scoptop;
636 case msub_s_op:
637 handler = fpemu_sp_msub;
638 goto scoptop;
639 case nmadd_s_op:
640 handler = fpemu_sp_nmadd;
641 goto scoptop;
642 case nmsub_s_op:
643 handler = fpemu_sp_nmsub;
644 goto scoptop;
645
646 scoptop:
647 SPFROMREG(fr, MIPSInst_FR(ir));
648 SPFROMREG(fs, MIPSInst_FS(ir));
649 SPFROMREG(ft, MIPSInst_FT(ir));
650 fd = (*handler) (fr, fs, ft);
651 SPTOREG(fd, MIPSInst_FD(ir));
652
653 copcsr:
654 if (ieee754_cxtest(IEEE754_INEXACT))
655 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
656 if (ieee754_cxtest(IEEE754_UNDERFLOW))
657 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
658 if (ieee754_cxtest(IEEE754_OVERFLOW))
659 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
660 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
661 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
662
663 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
665 /*printk ("SIGFPE: fpu csr = %08x\n",
666 ctx->fcr31); */
667 return SIGFPE;
668 }
669
670 break;
671
672 default:
673 return SIGILL;
674 }
675 break;
676 }
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 case d_fmt:{ /* 1 */
679 ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
680 ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +0100681 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 u64 val;
683
684 switch (MIPSInst_FUNC(ir)) {
685 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +0100686 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 xcp->regs[MIPSInst_FT(ir)]);
688
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000689 fpuemustats.loads++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (get_user(val, va)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000691 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return SIGBUS;
693 }
694 DITOREG(val, MIPSInst_FD(ir));
695 break;
696
697 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +0100698 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 xcp->regs[MIPSInst_FT(ir)]);
700
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000701 fpuemustats.stores++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 DIFROMREG(val, MIPSInst_FS(ir));
703 if (put_user(val, va)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000704 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return SIGBUS;
706 }
707 break;
708
709 case madd_d_op:
710 handler = fpemu_dp_madd;
711 goto dcoptop;
712 case msub_d_op:
713 handler = fpemu_dp_msub;
714 goto dcoptop;
715 case nmadd_d_op:
716 handler = fpemu_dp_nmadd;
717 goto dcoptop;
718 case nmsub_d_op:
719 handler = fpemu_dp_nmsub;
720 goto dcoptop;
721
722 dcoptop:
723 DPFROMREG(fr, MIPSInst_FR(ir));
724 DPFROMREG(fs, MIPSInst_FS(ir));
725 DPFROMREG(ft, MIPSInst_FT(ir));
726 fd = (*handler) (fr, fs, ft);
727 DPTOREG(fd, MIPSInst_FD(ir));
728 goto copcsr;
729
730 default:
731 return SIGILL;
732 }
733 break;
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 case 0x7: /* 7 */
737 if (MIPSInst_FUNC(ir) != pfetch_op) {
738 return SIGILL;
739 }
740 /* ignore prefx operation */
741 break;
742
743 default:
744 return SIGILL;
745 }
746
747 return 0;
748}
749#endif
750
751
752
753/*
754 * Emulate a single COP1 arithmetic instruction.
755 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900756static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 mips_instruction ir)
758{
759 int rfmt; /* resulting format */
760 unsigned rcsr = 0; /* resulting csr */
761 unsigned cond;
762 union {
763 ieee754dp d;
764 ieee754sp s;
765 int w;
Yoichi Yuasa766160c2005-09-03 15:56:22 -0700766#ifdef __mips64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 s64 l;
768#endif
769 } rv; /* resulting value */
770
Ralf Baechle4a99d1e2005-05-11 12:02:48 +0000771 fpuemustats.cp1ops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
773 case s_fmt:{ /* 0 */
774 union {
775 ieee754sp(*b) (ieee754sp, ieee754sp);
776 ieee754sp(*u) (ieee754sp);
777 } handler;
778
779 switch (MIPSInst_FUNC(ir)) {
780 /* binary ops */
781 case fadd_op:
782 handler.b = ieee754sp_add;
783 goto scopbop;
784 case fsub_op:
785 handler.b = ieee754sp_sub;
786 goto scopbop;
787 case fmul_op:
788 handler.b = ieee754sp_mul;
789 goto scopbop;
790 case fdiv_op:
791 handler.b = ieee754sp_div;
792 goto scopbop;
793
794 /* unary ops */
Ralf Baechle587cb982005-09-15 08:52:34 +0000795#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 case fsqrt_op:
797 handler.u = ieee754sp_sqrt;
798 goto scopuop;
799#endif
800#if __mips >= 4 && __mips != 32
801 case frsqrt_op:
802 handler.u = fpemu_sp_rsqrt;
803 goto scopuop;
804 case frecip_op:
805 handler.u = fpemu_sp_recip;
806 goto scopuop;
807#endif
808#if __mips >= 4
809 case fmovc_op:
810 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
811 if (((ctx->fcr31 & cond) != 0) !=
812 ((MIPSInst_FT(ir) & 1) != 0))
813 return 0;
814 SPFROMREG(rv.s, MIPSInst_FS(ir));
815 break;
816 case fmovz_op:
817 if (xcp->regs[MIPSInst_FT(ir)] != 0)
818 return 0;
819 SPFROMREG(rv.s, MIPSInst_FS(ir));
820 break;
821 case fmovn_op:
822 if (xcp->regs[MIPSInst_FT(ir)] == 0)
823 return 0;
824 SPFROMREG(rv.s, MIPSInst_FS(ir));
825 break;
826#endif
827 case fabs_op:
828 handler.u = ieee754sp_abs;
829 goto scopuop;
830 case fneg_op:
831 handler.u = ieee754sp_neg;
832 goto scopuop;
833 case fmov_op:
834 /* an easy one */
835 SPFROMREG(rv.s, MIPSInst_FS(ir));
836 goto copcsr;
837
838 /* binary op on handler */
839 scopbop:
840 {
841 ieee754sp fs, ft;
842
843 SPFROMREG(fs, MIPSInst_FS(ir));
844 SPFROMREG(ft, MIPSInst_FT(ir));
845
846 rv.s = (*handler.b) (fs, ft);
847 goto copcsr;
848 }
849 scopuop:
850 {
851 ieee754sp fs;
852
853 SPFROMREG(fs, MIPSInst_FS(ir));
854 rv.s = (*handler.u) (fs);
855 goto copcsr;
856 }
857 copcsr:
858 if (ieee754_cxtest(IEEE754_INEXACT))
859 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
860 if (ieee754_cxtest(IEEE754_UNDERFLOW))
861 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
862 if (ieee754_cxtest(IEEE754_OVERFLOW))
863 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
864 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
865 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
866 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
867 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
868 break;
869
870 /* unary conv ops */
871 case fcvts_op:
872 return SIGILL; /* not defined */
873 case fcvtd_op:{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 ieee754sp fs;
875
876 SPFROMREG(fs, MIPSInst_FS(ir));
877 rv.d = ieee754dp_fsp(fs);
878 rfmt = d_fmt;
879 goto copcsr;
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 case fcvtw_op:{
882 ieee754sp fs;
883
884 SPFROMREG(fs, MIPSInst_FS(ir));
885 rv.w = ieee754sp_tint(fs);
886 rfmt = w_fmt;
887 goto copcsr;
888 }
889
Ralf Baechle587cb982005-09-15 08:52:34 +0000890#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 case fround_op:
892 case ftrunc_op:
893 case fceil_op:
894 case ffloor_op:{
895 unsigned int oldrm = ieee754_csr.rm;
896 ieee754sp fs;
897
898 SPFROMREG(fs, MIPSInst_FS(ir));
899 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
900 rv.w = ieee754sp_tint(fs);
901 ieee754_csr.rm = oldrm;
902 rfmt = w_fmt;
903 goto copcsr;
904 }
905#endif /* __mips >= 2 */
906
Ralf Baechle4b724ef2005-10-23 15:05:47 +0100907#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 case fcvtl_op:{
909 ieee754sp fs;
910
911 SPFROMREG(fs, MIPSInst_FS(ir));
912 rv.l = ieee754sp_tlong(fs);
913 rfmt = l_fmt;
914 goto copcsr;
915 }
916
917 case froundl_op:
918 case ftruncl_op:
919 case fceill_op:
920 case ffloorl_op:{
921 unsigned int oldrm = ieee754_csr.rm;
922 ieee754sp fs;
923
924 SPFROMREG(fs, MIPSInst_FS(ir));
925 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
926 rv.l = ieee754sp_tlong(fs);
927 ieee754_csr.rm = oldrm;
928 rfmt = l_fmt;
929 goto copcsr;
930 }
Ralf Baechle4b724ef2005-10-23 15:05:47 +0100931#endif /* defined(__mips64) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 default:
934 if (MIPSInst_FUNC(ir) >= fcmp_op) {
935 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
936 ieee754sp fs, ft;
937
938 SPFROMREG(fs, MIPSInst_FS(ir));
939 SPFROMREG(ft, MIPSInst_FT(ir));
940 rv.w = ieee754sp_cmp(fs, ft,
941 cmptab[cmpop & 0x7], cmpop & 0x8);
942 rfmt = -1;
943 if ((cmpop & 0x8) && ieee754_cxtest
944 (IEEE754_INVALID_OPERATION))
945 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
946 else
947 goto copcsr;
948
949 }
950 else {
951 return SIGILL;
952 }
953 break;
954 }
955 break;
956 }
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 case d_fmt:{
959 union {
960 ieee754dp(*b) (ieee754dp, ieee754dp);
961 ieee754dp(*u) (ieee754dp);
962 } handler;
963
964 switch (MIPSInst_FUNC(ir)) {
965 /* binary ops */
966 case fadd_op:
967 handler.b = ieee754dp_add;
968 goto dcopbop;
969 case fsub_op:
970 handler.b = ieee754dp_sub;
971 goto dcopbop;
972 case fmul_op:
973 handler.b = ieee754dp_mul;
974 goto dcopbop;
975 case fdiv_op:
976 handler.b = ieee754dp_div;
977 goto dcopbop;
978
979 /* unary ops */
Ralf Baechle587cb982005-09-15 08:52:34 +0000980#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 case fsqrt_op:
982 handler.u = ieee754dp_sqrt;
983 goto dcopuop;
984#endif
985#if __mips >= 4 && __mips != 32
986 case frsqrt_op:
987 handler.u = fpemu_dp_rsqrt;
988 goto dcopuop;
989 case frecip_op:
990 handler.u = fpemu_dp_recip;
991 goto dcopuop;
992#endif
993#if __mips >= 4
994 case fmovc_op:
995 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
996 if (((ctx->fcr31 & cond) != 0) !=
997 ((MIPSInst_FT(ir) & 1) != 0))
998 return 0;
999 DPFROMREG(rv.d, MIPSInst_FS(ir));
1000 break;
1001 case fmovz_op:
1002 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1003 return 0;
1004 DPFROMREG(rv.d, MIPSInst_FS(ir));
1005 break;
1006 case fmovn_op:
1007 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1008 return 0;
1009 DPFROMREG(rv.d, MIPSInst_FS(ir));
1010 break;
1011#endif
1012 case fabs_op:
1013 handler.u = ieee754dp_abs;
1014 goto dcopuop;
1015
1016 case fneg_op:
1017 handler.u = ieee754dp_neg;
1018 goto dcopuop;
1019
1020 case fmov_op:
1021 /* an easy one */
1022 DPFROMREG(rv.d, MIPSInst_FS(ir));
1023 goto copcsr;
1024
1025 /* binary op on handler */
1026 dcopbop:{
1027 ieee754dp fs, ft;
1028
1029 DPFROMREG(fs, MIPSInst_FS(ir));
1030 DPFROMREG(ft, MIPSInst_FT(ir));
1031
1032 rv.d = (*handler.b) (fs, ft);
1033 goto copcsr;
1034 }
1035 dcopuop:{
1036 ieee754dp fs;
1037
1038 DPFROMREG(fs, MIPSInst_FS(ir));
1039 rv.d = (*handler.u) (fs);
1040 goto copcsr;
1041 }
1042
1043 /* unary conv ops */
1044 case fcvts_op:{
1045 ieee754dp fs;
1046
1047 DPFROMREG(fs, MIPSInst_FS(ir));
1048 rv.s = ieee754sp_fdp(fs);
1049 rfmt = s_fmt;
1050 goto copcsr;
1051 }
1052 case fcvtd_op:
1053 return SIGILL; /* not defined */
1054
1055 case fcvtw_op:{
1056 ieee754dp fs;
1057
1058 DPFROMREG(fs, MIPSInst_FS(ir));
1059 rv.w = ieee754dp_tint(fs); /* wrong */
1060 rfmt = w_fmt;
1061 goto copcsr;
1062 }
1063
Ralf Baechle587cb982005-09-15 08:52:34 +00001064#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 case fround_op:
1066 case ftrunc_op:
1067 case fceil_op:
1068 case ffloor_op:{
1069 unsigned int oldrm = ieee754_csr.rm;
1070 ieee754dp fs;
1071
1072 DPFROMREG(fs, MIPSInst_FS(ir));
1073 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1074 rv.w = ieee754dp_tint(fs);
1075 ieee754_csr.rm = oldrm;
1076 rfmt = w_fmt;
1077 goto copcsr;
1078 }
1079#endif
1080
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001081#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 case fcvtl_op:{
1083 ieee754dp fs;
1084
1085 DPFROMREG(fs, MIPSInst_FS(ir));
1086 rv.l = ieee754dp_tlong(fs);
1087 rfmt = l_fmt;
1088 goto copcsr;
1089 }
1090
1091 case froundl_op:
1092 case ftruncl_op:
1093 case fceill_op:
1094 case ffloorl_op:{
1095 unsigned int oldrm = ieee754_csr.rm;
1096 ieee754dp fs;
1097
1098 DPFROMREG(fs, MIPSInst_FS(ir));
1099 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1100 rv.l = ieee754dp_tlong(fs);
1101 ieee754_csr.rm = oldrm;
1102 rfmt = l_fmt;
1103 goto copcsr;
1104 }
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001105#endif /* __mips >= 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 default:
1108 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1109 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1110 ieee754dp fs, ft;
1111
1112 DPFROMREG(fs, MIPSInst_FS(ir));
1113 DPFROMREG(ft, MIPSInst_FT(ir));
1114 rv.w = ieee754dp_cmp(fs, ft,
1115 cmptab[cmpop & 0x7], cmpop & 0x8);
1116 rfmt = -1;
1117 if ((cmpop & 0x8)
1118 &&
1119 ieee754_cxtest
1120 (IEEE754_INVALID_OPERATION))
1121 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1122 else
1123 goto copcsr;
1124
1125 }
1126 else {
1127 return SIGILL;
1128 }
1129 break;
1130 }
1131 break;
1132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 case w_fmt:{
1135 ieee754sp fs;
1136
1137 switch (MIPSInst_FUNC(ir)) {
1138 case fcvts_op:
1139 /* convert word to single precision real */
1140 SPFROMREG(fs, MIPSInst_FS(ir));
1141 rv.s = ieee754sp_fint(fs.bits);
1142 rfmt = s_fmt;
1143 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 case fcvtd_op:
1145 /* convert word to double precision real */
1146 SPFROMREG(fs, MIPSInst_FS(ir));
1147 rv.d = ieee754dp_fint(fs.bits);
1148 rfmt = d_fmt;
1149 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 default:
1151 return SIGILL;
1152 }
1153 break;
1154 }
1155
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001156#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 case l_fmt:{
1158 switch (MIPSInst_FUNC(ir)) {
1159 case fcvts_op:
1160 /* convert long to single precision real */
1161 rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1162 rfmt = s_fmt;
1163 goto copcsr;
1164 case fcvtd_op:
1165 /* convert long to double precision real */
1166 rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1167 rfmt = d_fmt;
1168 goto copcsr;
1169 default:
1170 return SIGILL;
1171 }
1172 break;
1173 }
1174#endif
1175
1176 default:
1177 return SIGILL;
1178 }
1179
1180 /*
1181 * Update the fpu CSR register for this operation.
1182 * If an exception is required, generate a tidy SIGFPE exception,
1183 * without updating the result register.
1184 * Note: cause exception bits do not accumulate, they are rewritten
1185 * for each op; only the flag/sticky bits accumulate.
1186 */
1187 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1188 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1189 /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
1190 return SIGFPE;
1191 }
1192
1193 /*
1194 * Now we can safely write the result back to the register file.
1195 */
1196 switch (rfmt) {
1197 case -1:{
1198#if __mips >= 4
1199 cond = fpucondbit[MIPSInst_FD(ir) >> 2];
1200#else
1201 cond = FPU_CSR_COND;
1202#endif
1203 if (rv.w)
1204 ctx->fcr31 |= cond;
1205 else
1206 ctx->fcr31 &= ~cond;
1207 break;
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 case d_fmt:
1210 DPTOREG(rv.d, MIPSInst_FD(ir));
1211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 case s_fmt:
1213 SPTOREG(rv.s, MIPSInst_FD(ir));
1214 break;
1215 case w_fmt:
1216 SITOREG(rv.w, MIPSInst_FD(ir));
1217 break;
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001218#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 case l_fmt:
1220 DITOREG(rv.l, MIPSInst_FD(ir));
1221 break;
1222#endif
1223 default:
1224 return SIGILL;
1225 }
1226
1227 return 0;
1228}
1229
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09001230int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1231 int has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Ralf Baechle333d1f62005-02-28 17:55:57 +00001233 unsigned long oldepc, prevepc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 mips_instruction insn;
1235 int sig = 0;
1236
1237 oldepc = xcp->cp0_epc;
1238 do {
1239 prevepc = xcp->cp0_epc;
1240
Ralf Baechle3fccc012005-10-23 13:58:21 +01001241 if (get_user(insn, (mips_instruction __user *) xcp->cp0_epc)) {
Ralf Baechle4a99d1e2005-05-11 12:02:48 +00001242 fpuemustats.errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return SIGBUS;
1244 }
1245 if (insn == 0)
1246 xcp->cp0_epc += 4; /* skip nops */
1247 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00001248 /*
1249 * The 'ieee754_csr' is an alias of
1250 * ctx->fcr31. No need to copy ctx->fcr31 to
1251 * ieee754_csr. But ieee754_csr.rm is ieee
1252 * library modes. (not mips rounding mode)
1253 */
1254 /* convert to ieee library modes */
1255 ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 sig = cop1Emulate(xcp, ctx);
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00001257 /* revert to mips rounding mode */
1258 ieee754_csr.rm = mips_rm[ieee754_csr.rm];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09001261 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 break;
1263 if (sig)
1264 break;
1265
1266 cond_resched();
1267 } while (xcp->cp0_epc > prevepc);
1268
1269 /* SIGILL indicates a non-fpu instruction */
1270 if (sig == SIGILL && xcp->cp0_epc != oldepc)
1271 /* but if epc has advanced, then ignore it */
1272 sig = 0;
1273
1274 return sig;
1275}
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +09001276
1277#ifdef CONFIG_DEBUG_FS
1278extern struct dentry *mips_debugfs_dir;
1279static int __init debugfs_fpuemu(void)
1280{
1281 struct dentry *d, *dir;
1282 int i;
1283 static struct {
1284 const char *name;
1285 unsigned int *v;
1286 } vars[] __initdata = {
1287 { "emulated", &fpuemustats.emulated },
1288 { "loads", &fpuemustats.loads },
1289 { "stores", &fpuemustats.stores },
1290 { "cp1ops", &fpuemustats.cp1ops },
1291 { "cp1xops", &fpuemustats.cp1xops },
1292 { "errors", &fpuemustats.errors },
1293 };
1294
1295 if (!mips_debugfs_dir)
1296 return -ENODEV;
1297 dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir);
Zhaoleiecab1f42008-10-17 19:12:30 +08001298 if (!dir)
1299 return -ENOMEM;
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +09001300 for (i = 0; i < ARRAY_SIZE(vars); i++) {
1301 d = debugfs_create_u32(vars[i].name, S_IRUGO, dir, vars[i].v);
Zhaoleiecab1f42008-10-17 19:12:30 +08001302 if (!d)
1303 return -ENOMEM;
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +09001304 }
1305 return 0;
1306}
1307__initcall(debugfs_fpuemu);
1308#endif