blob: f2338d1c0b4889a0bf216f1af9ac9e8adedb3ec1 [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>
David Daneyb6ee75e2009-11-05 11:34:26 -080038#include <linux/module.h>
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +090039#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/inst.h>
42#include <asm/bootinfo.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/processor.h>
44#include <asm/ptrace.h>
45#include <asm/signal.h>
46#include <asm/mipsregs.h>
47#include <asm/fpu_emulator.h>
48#include <asm/uaccess.h>
49#include <asm/branch.h>
50
51#include "ieee754.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* Strap kernel emulator for full MIPS IV emulation */
54
55#ifdef __mips
56#undef __mips
57#endif
58#define __mips 4
59
60/* Function which emulates a floating point instruction. */
61
Atsushi Nemotoeae89072006-05-16 01:26:03 +090062static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 mips_instruction);
64
65#if __mips >= 4 && __mips != 32
66static int fpux_emu(struct pt_regs *,
Atsushi Nemotoeae89072006-05-16 01:26:03 +090067 struct mips_fpu_struct *, mips_instruction);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#endif
69
Atsushi Nemotoeae89072006-05-16 01:26:03 +090070/* Further private data for which no space exists in mips_fpu_struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
David Daneyb6ee75e2009-11-05 11:34:26 -080072#ifdef CONFIG_DEBUG_FS
73DEFINE_PER_CPU(struct mips_fpu_emulator_stats, fpuemustats);
74#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76/* Control registers */
77
78#define FPCREG_RID 0 /* $0 = revision id */
79#define FPCREG_CSR 31 /* $31 = csr */
80
Shane McDonald95e8f632010-05-06 23:26:57 -060081/* Determine rounding mode from the RM bits of the FCSR */
82#define modeindex(v) ((v) & FPU_CSR_RM)
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* Convert Mips rounding mode (0..3) to IEEE library modes. */
85static const unsigned char ieee_rm[4] = {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000086 [FPU_CSR_RN] = IEEE754_RN,
87 [FPU_CSR_RZ] = IEEE754_RZ,
88 [FPU_CSR_RU] = IEEE754_RU,
89 [FPU_CSR_RD] = IEEE754_RD,
90};
91/* Convert IEEE library modes to Mips rounding mode (0..3). */
92static const unsigned char mips_rm[4] = {
93 [IEEE754_RN] = FPU_CSR_RN,
94 [IEEE754_RZ] = FPU_CSR_RZ,
95 [IEEE754_RD] = FPU_CSR_RD,
96 [IEEE754_RU] = FPU_CSR_RU,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
98
99#if __mips >= 4
100/* convert condition code register number to csr bit */
101static const unsigned int fpucondbit[8] = {
102 FPU_CSR_COND0,
103 FPU_CSR_COND1,
104 FPU_CSR_COND2,
105 FPU_CSR_COND3,
106 FPU_CSR_COND4,
107 FPU_CSR_COND5,
108 FPU_CSR_COND6,
109 FPU_CSR_COND7
110};
111#endif
112
113
114/*
115 * Redundant with logic already in kernel/branch.c,
116 * embedded in compute_return_epc. At some point,
117 * a single subroutine should be used across both
118 * modules.
119 */
120static int isBranchInstr(mips_instruction * i)
121{
122 switch (MIPSInst_OPCODE(*i)) {
123 case spec_op:
124 switch (MIPSInst_FUNC(*i)) {
125 case jalr_op:
126 case jr_op:
127 return 1;
128 }
129 break;
130
131 case bcond_op:
132 switch (MIPSInst_RT(*i)) {
133 case bltz_op:
134 case bgez_op:
135 case bltzl_op:
136 case bgezl_op:
137 case bltzal_op:
138 case bgezal_op:
139 case bltzall_op:
140 case bgezall_op:
141 return 1;
142 }
143 break;
144
145 case j_op:
146 case jal_op:
147 case jalx_op:
148 case beq_op:
149 case bne_op:
150 case blez_op:
151 case bgtz_op:
152 case beql_op:
153 case bnel_op:
154 case blezl_op:
155 case bgtzl_op:
156 return 1;
157
158 case cop0_op:
159 case cop1_op:
160 case cop2_op:
161 case cop1x_op:
162 if (MIPSInst_RS(*i) == bc_op)
163 return 1;
164 break;
165 }
166
167 return 0;
168}
169
170/*
171 * In the Linux kernel, we support selection of FPR format on the
David Daneyda0bac32009-11-02 11:33:46 -0800172 * basis of the Status.FR bit. If an FPU is not present, the FR bit
173 * is hardwired to zero, which would imply a 32-bit FPU even for
174 * 64-bit CPUs. For 64-bit kernels with no FPU we use TIF_32BIT_REGS
175 * as a proxy for the FR bit so that a 64-bit FPU is emulated. In any
176 * case, for a 32-bit kernel which uses the O32 MIPS ABI, only the
177 * even FPRs are used (Status.FR = 0).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
David Daneyda0bac32009-11-02 11:33:46 -0800179static inline int cop1_64bit(struct pt_regs *xcp)
180{
181 if (cpu_has_fpu)
182 return xcp->cp0_status & ST0_FR;
183#ifdef CONFIG_64BIT
184 return !test_thread_flag(TIF_32BIT_REGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#else
David Daneyda0bac32009-11-02 11:33:46 -0800186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#endif
David Daneyda0bac32009-11-02 11:33:46 -0800188}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
David Daneyda0bac32009-11-02 11:33:46 -0800190#define SIFROMREG(si, x) ((si) = cop1_64bit(xcp) || !(x & 1) ? \
191 (int)ctx->fpr[x] : (int)(ctx->fpr[x & ~1] >> 32))
192
193#define SITOREG(si, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = \
194 cop1_64bit(xcp) || !(x & 1) ? \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 ctx->fpr[x & ~1] >> 32 << 32 | (u32)(si) : \
196 ctx->fpr[x & ~1] << 32 >> 32 | (u64)(si) << 32)
197
David Daneyda0bac32009-11-02 11:33:46 -0800198#define DIFROMREG(di, x) ((di) = ctx->fpr[x & ~(cop1_64bit(xcp) == 0)])
199#define DITOREG(di, x) (ctx->fpr[x & ~(cop1_64bit(xcp) == 0)] = (di))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Ralf Baechle21a151d2007-10-11 23:46:15 +0100201#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
202#define SPTOREG(sp, x) SITOREG((sp).bits, x)
203#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
204#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206/*
207 * Emulate the single floating point instruction pointed at by EPC.
208 * Two instructions if the instruction is in a branch delay slot.
209 */
210
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900211static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 mips_instruction ir;
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900214 unsigned long emulpc, contpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 unsigned int cond;
216
Ralf Baechle3fccc012005-10-23 13:58:21 +0100217 if (get_user(ir, (mips_instruction __user *) xcp->cp0_epc)) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800218 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return SIGBUS;
220 }
221
222 /* XXX NEC Vr54xx bug workaround */
223 if ((xcp->cp0_cause & CAUSEF_BD) && !isBranchInstr(&ir))
224 xcp->cp0_cause &= ~CAUSEF_BD;
225
226 if (xcp->cp0_cause & CAUSEF_BD) {
227 /*
228 * The instruction to be emulated is in a branch delay slot
229 * which means that we have to emulate the branch instruction
230 * BEFORE we do the cop1 instruction.
231 *
232 * This branch could be a COP1 branch, but in that case we
233 * would have had a trap for that instruction, and would not
234 * come through this route.
235 *
236 * Linux MIPS branch emulator operates on context, updating the
237 * cp0_epc.
238 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900239 emulpc = xcp->cp0_epc + 4; /* Snapshot emulation target */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (__compute_return_epc(xcp)) {
242#ifdef CP1DBG
243 printk("failed to emulate branch at %p\n",
Ralf Baechle333d1f62005-02-28 17:55:57 +0000244 (void *) (xcp->cp0_epc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#endif
246 return SIGILL;
247 }
Ralf Baechle3fccc012005-10-23 13:58:21 +0100248 if (get_user(ir, (mips_instruction __user *) emulpc)) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800249 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return SIGBUS;
251 }
252 /* __compute_return_epc() will have updated cp0_epc */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900253 contpc = xcp->cp0_epc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /* In order not to confuse ptrace() et al, tweak context */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900255 xcp->cp0_epc = emulpc - 4;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000256 } else {
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900257 emulpc = xcp->cp0_epc;
258 contpc = xcp->cp0_epc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
261 emul:
David Daneyb6ee75e2009-11-05 11:34:26 -0800262 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 switch (MIPSInst_OPCODE(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 case ldc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +0100265 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 MIPSInst_SIMM(ir));
267 u64 val;
268
David Daneyb6ee75e2009-11-05 11:34:26 -0800269 MIPS_FPU_EMU_INC_STATS(loads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (get_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800271 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return SIGBUS;
273 }
274 DITOREG(val, MIPSInst_RT(ir));
275 break;
276 }
277
278 case sdc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +0100279 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 MIPSInst_SIMM(ir));
281 u64 val;
282
David Daneyb6ee75e2009-11-05 11:34:26 -0800283 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 DIFROMREG(val, MIPSInst_RT(ir));
285 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800286 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return SIGBUS;
288 }
289 break;
290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 case lwc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +0100293 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 MIPSInst_SIMM(ir));
295 u32 val;
296
David Daneyb6ee75e2009-11-05 11:34:26 -0800297 MIPS_FPU_EMU_INC_STATS(loads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (get_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800299 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return SIGBUS;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 SITOREG(val, MIPSInst_RT(ir));
303 break;
304 }
305
306 case swc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +0100307 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 MIPSInst_SIMM(ir));
309 u32 val;
310
David Daneyb6ee75e2009-11-05 11:34:26 -0800311 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 SIFROMREG(val, MIPSInst_RT(ir));
313 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800314 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return SIGBUS;
316 }
317 break;
318 }
319
320 case cop1_op:
321 switch (MIPSInst_RS(ir)) {
322
Ralf Baechle4b724ef2005-10-23 15:05:47 +0100323#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 case dmfc_op:
325 /* copregister fs -> gpr[rt] */
326 if (MIPSInst_RT(ir) != 0) {
327 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
328 MIPSInst_RD(ir));
329 }
330 break;
331
332 case dmtc_op:
333 /* copregister fs <- rt */
334 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
335 break;
336#endif
337
338 case mfc_op:
339 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (MIPSInst_RT(ir) != 0) {
341 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
342 MIPSInst_RD(ir));
343 }
344 break;
345
346 case mtc_op:
347 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
349 break;
350
351 case cfc_op:{
352 /* cop control register rd -> gpr[rt] */
353 u32 value;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (MIPSInst_RD(ir) == FPCREG_CSR) {
356 value = ctx->fcr31;
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000357 value = (value & ~0x3) | mips_rm[value & 0x3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#ifdef CSRTRACE
359 printk("%p gpr[%d]<-csr=%08x\n",
Ralf Baechle333d1f62005-02-28 17:55:57 +0000360 (void *) (xcp->cp0_epc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 MIPSInst_RT(ir), value);
362#endif
363 }
364 else if (MIPSInst_RD(ir) == FPCREG_RID)
365 value = 0;
366 else
367 value = 0;
368 if (MIPSInst_RT(ir))
369 xcp->regs[MIPSInst_RT(ir)] = value;
370 break;
371 }
372
373 case ctc_op:{
374 /* copregister rd <- rt */
375 u32 value;
376
377 if (MIPSInst_RT(ir) == 0)
378 value = 0;
379 else
380 value = xcp->regs[MIPSInst_RT(ir)];
381
382 /* we only have one writable control reg
383 */
384 if (MIPSInst_RD(ir) == FPCREG_CSR) {
385#ifdef CSRTRACE
386 printk("%p gpr[%d]->csr=%08x\n",
Ralf Baechle333d1f62005-02-28 17:55:57 +0000387 (void *) (xcp->cp0_epc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 MIPSInst_RT(ir), value);
389#endif
Shane McDonald95e8f632010-05-06 23:26:57 -0600390
391 /*
392 * Don't write reserved bits,
393 * and convert to ieee library modes
394 */
395 ctx->fcr31 = (value &
396 ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
397 ieee_rm[modeindex(value)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
400 return SIGFPE;
401 }
402 break;
403 }
404
405 case bc_op:{
406 int likely = 0;
407
408 if (xcp->cp0_cause & CAUSEF_BD)
409 return SIGILL;
410
411#if __mips >= 4
412 cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
413#else
414 cond = ctx->fcr31 & FPU_CSR_COND;
415#endif
416 switch (MIPSInst_RT(ir) & 3) {
417 case bcfl_op:
418 likely = 1;
419 case bcf_op:
420 cond = !cond;
421 break;
422 case bctl_op:
423 likely = 1;
424 case bct_op:
425 break;
426 default:
427 /* thats an illegal instruction */
428 return SIGILL;
429 }
430
431 xcp->cp0_cause |= CAUSEF_BD;
432 if (cond) {
433 /* branch taken: emulate dslot
434 * instruction
435 */
436 xcp->cp0_epc += 4;
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900437 contpc = (xcp->cp0_epc +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 (MIPSInst_SIMM(ir) << 2));
439
Ralf Baechle3fccc012005-10-23 13:58:21 +0100440 if (get_user(ir,
441 (mips_instruction __user *) xcp->cp0_epc)) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800442 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return SIGBUS;
444 }
445
446 switch (MIPSInst_OPCODE(ir)) {
447 case lwc1_op:
448 case swc1_op:
Ralf Baechle4b724ef2005-10-23 15:05:47 +0100449#if (__mips >= 2 || defined(__mips64))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 case ldc1_op:
451 case sdc1_op:
452#endif
453 case cop1_op:
454#if __mips >= 4 && __mips != 32
455 case cop1x_op:
456#endif
457 /* its one of ours */
458 goto emul;
459#if __mips >= 4
460 case spec_op:
461 if (MIPSInst_FUNC(ir) == movc_op)
462 goto emul;
463 break;
464#endif
465 }
466
467 /*
468 * Single step the non-cp1
469 * instruction in the dslot
470 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900471 return mips_dsemul(xcp, ir, contpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 else {
474 /* branch not taken */
475 if (likely) {
476 /*
477 * branch likely nullifies
478 * dslot if not taken
479 */
480 xcp->cp0_epc += 4;
481 contpc += 4;
482 /*
483 * else continue & execute
484 * dslot as normal insn
485 */
486 }
487 }
488 break;
489 }
490
491 default:
492 if (!(MIPSInst_RS(ir) & 0x10))
493 return SIGILL;
494 {
495 int sig;
496
497 /* a real fpu computation instruction */
498 if ((sig = fpu_emu(xcp, ctx, ir)))
499 return sig;
500 }
501 }
502 break;
503
504#if __mips >= 4 && __mips != 32
505 case cop1x_op:{
506 int sig;
507
508 if ((sig = fpux_emu(xcp, ctx, ir)))
509 return sig;
510 break;
511 }
512#endif
513
514#if __mips >= 4
515 case spec_op:
516 if (MIPSInst_FUNC(ir) != movc_op)
517 return SIGILL;
518 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
519 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
520 xcp->regs[MIPSInst_RD(ir)] =
521 xcp->regs[MIPSInst_RS(ir)];
522 break;
523#endif
524
525 default:
526 return SIGILL;
527 }
528
529 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +0900530 xcp->cp0_epc = contpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 xcp->cp0_cause &= ~CAUSEF_BD;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return 0;
534}
535
536/*
537 * Conversion table from MIPS compare ops 48-63
538 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
539 */
540static const unsigned char cmptab[8] = {
541 0, /* cmp_0 (sig) cmp_sf */
542 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
543 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
544 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
545 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
546 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
547 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
548 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
549};
550
551
552#if __mips >= 4 && __mips != 32
553
554/*
555 * Additional MIPS4 instructions
556 */
557
558#define DEF3OP(name, p, f1, f2, f3) \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100559static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 ieee754##p t) \
561{ \
Ralf Baechlecd21dfc2005-04-28 13:39:10 +0000562 struct _ieee754_csr ieee754_csr_save; \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100563 s = f1(s, t); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 ieee754_csr_save = ieee754_csr; \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100565 s = f2(s, r); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 ieee754_csr_save.cx |= ieee754_csr.cx; \
567 ieee754_csr_save.sx |= ieee754_csr.sx; \
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100568 s = f3(s); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 ieee754_csr.cx |= ieee754_csr_save.cx; \
570 ieee754_csr.sx |= ieee754_csr_save.sx; \
571 return s; \
572}
573
574static ieee754dp fpemu_dp_recip(ieee754dp d)
575{
576 return ieee754dp_div(ieee754dp_one(0), d);
577}
578
579static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
580{
581 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
582}
583
584static ieee754sp fpemu_sp_recip(ieee754sp s)
585{
586 return ieee754sp_div(ieee754sp_one(0), s);
587}
588
589static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
590{
591 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
592}
593
Ralf Baechle21a151d2007-10-11 23:46:15 +0100594DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
595DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
597DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +0100598DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
599DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
601DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
602
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900603static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 mips_instruction ir)
605{
606 unsigned rcsr = 0; /* resulting csr */
607
David Daneyb6ee75e2009-11-05 11:34:26 -0800608 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 switch (MIPSInst_FMA_FFMT(ir)) {
611 case s_fmt:{ /* 0 */
612
613 ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
614 ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +0100615 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 u32 val;
617
618 switch (MIPSInst_FUNC(ir)) {
619 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +0100620 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 xcp->regs[MIPSInst_FT(ir)]);
622
David Daneyb6ee75e2009-11-05 11:34:26 -0800623 MIPS_FPU_EMU_INC_STATS(loads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (get_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800625 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return SIGBUS;
627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 SITOREG(val, MIPSInst_FD(ir));
629 break;
630
631 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +0100632 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 xcp->regs[MIPSInst_FT(ir)]);
634
David Daneyb6ee75e2009-11-05 11:34:26 -0800635 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 SIFROMREG(val, MIPSInst_FS(ir));
638 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800639 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return SIGBUS;
641 }
642 break;
643
644 case madd_s_op:
645 handler = fpemu_sp_madd;
646 goto scoptop;
647 case msub_s_op:
648 handler = fpemu_sp_msub;
649 goto scoptop;
650 case nmadd_s_op:
651 handler = fpemu_sp_nmadd;
652 goto scoptop;
653 case nmsub_s_op:
654 handler = fpemu_sp_nmsub;
655 goto scoptop;
656
657 scoptop:
658 SPFROMREG(fr, MIPSInst_FR(ir));
659 SPFROMREG(fs, MIPSInst_FS(ir));
660 SPFROMREG(ft, MIPSInst_FT(ir));
661 fd = (*handler) (fr, fs, ft);
662 SPTOREG(fd, MIPSInst_FD(ir));
663
664 copcsr:
665 if (ieee754_cxtest(IEEE754_INEXACT))
666 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
667 if (ieee754_cxtest(IEEE754_UNDERFLOW))
668 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
669 if (ieee754_cxtest(IEEE754_OVERFLOW))
670 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
671 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
672 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
673
674 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
676 /*printk ("SIGFPE: fpu csr = %08x\n",
677 ctx->fcr31); */
678 return SIGFPE;
679 }
680
681 break;
682
683 default:
684 return SIGILL;
685 }
686 break;
687 }
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 case d_fmt:{ /* 1 */
690 ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
691 ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +0100692 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 u64 val;
694
695 switch (MIPSInst_FUNC(ir)) {
696 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +0100697 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 xcp->regs[MIPSInst_FT(ir)]);
699
David Daneyb6ee75e2009-11-05 11:34:26 -0800700 MIPS_FPU_EMU_INC_STATS(loads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (get_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800702 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return SIGBUS;
704 }
705 DITOREG(val, MIPSInst_FD(ir));
706 break;
707
708 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +0100709 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 xcp->regs[MIPSInst_FT(ir)]);
711
David Daneyb6ee75e2009-11-05 11:34:26 -0800712 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 DIFROMREG(val, MIPSInst_FS(ir));
714 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800715 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return SIGBUS;
717 }
718 break;
719
720 case madd_d_op:
721 handler = fpemu_dp_madd;
722 goto dcoptop;
723 case msub_d_op:
724 handler = fpemu_dp_msub;
725 goto dcoptop;
726 case nmadd_d_op:
727 handler = fpemu_dp_nmadd;
728 goto dcoptop;
729 case nmsub_d_op:
730 handler = fpemu_dp_nmsub;
731 goto dcoptop;
732
733 dcoptop:
734 DPFROMREG(fr, MIPSInst_FR(ir));
735 DPFROMREG(fs, MIPSInst_FS(ir));
736 DPFROMREG(ft, MIPSInst_FT(ir));
737 fd = (*handler) (fr, fs, ft);
738 DPTOREG(fd, MIPSInst_FD(ir));
739 goto copcsr;
740
741 default:
742 return SIGILL;
743 }
744 break;
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 case 0x7: /* 7 */
748 if (MIPSInst_FUNC(ir) != pfetch_op) {
749 return SIGILL;
750 }
751 /* ignore prefx operation */
752 break;
753
754 default:
755 return SIGILL;
756 }
757
758 return 0;
759}
760#endif
761
762
763
764/*
765 * Emulate a single COP1 arithmetic instruction.
766 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900767static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 mips_instruction ir)
769{
770 int rfmt; /* resulting format */
771 unsigned rcsr = 0; /* resulting csr */
772 unsigned cond;
773 union {
774 ieee754dp d;
775 ieee754sp s;
776 int w;
Yoichi Yuasa766160c2005-09-03 15:56:22 -0700777#ifdef __mips64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 s64 l;
779#endif
780 } rv; /* resulting value */
781
David Daneyb6ee75e2009-11-05 11:34:26 -0800782 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
784 case s_fmt:{ /* 0 */
785 union {
786 ieee754sp(*b) (ieee754sp, ieee754sp);
787 ieee754sp(*u) (ieee754sp);
788 } handler;
789
790 switch (MIPSInst_FUNC(ir)) {
791 /* binary ops */
792 case fadd_op:
793 handler.b = ieee754sp_add;
794 goto scopbop;
795 case fsub_op:
796 handler.b = ieee754sp_sub;
797 goto scopbop;
798 case fmul_op:
799 handler.b = ieee754sp_mul;
800 goto scopbop;
801 case fdiv_op:
802 handler.b = ieee754sp_div;
803 goto scopbop;
804
805 /* unary ops */
Ralf Baechle587cb982005-09-15 08:52:34 +0000806#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 case fsqrt_op:
808 handler.u = ieee754sp_sqrt;
809 goto scopuop;
810#endif
811#if __mips >= 4 && __mips != 32
812 case frsqrt_op:
813 handler.u = fpemu_sp_rsqrt;
814 goto scopuop;
815 case frecip_op:
816 handler.u = fpemu_sp_recip;
817 goto scopuop;
818#endif
819#if __mips >= 4
820 case fmovc_op:
821 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
822 if (((ctx->fcr31 & cond) != 0) !=
823 ((MIPSInst_FT(ir) & 1) != 0))
824 return 0;
825 SPFROMREG(rv.s, MIPSInst_FS(ir));
826 break;
827 case fmovz_op:
828 if (xcp->regs[MIPSInst_FT(ir)] != 0)
829 return 0;
830 SPFROMREG(rv.s, MIPSInst_FS(ir));
831 break;
832 case fmovn_op:
833 if (xcp->regs[MIPSInst_FT(ir)] == 0)
834 return 0;
835 SPFROMREG(rv.s, MIPSInst_FS(ir));
836 break;
837#endif
838 case fabs_op:
839 handler.u = ieee754sp_abs;
840 goto scopuop;
841 case fneg_op:
842 handler.u = ieee754sp_neg;
843 goto scopuop;
844 case fmov_op:
845 /* an easy one */
846 SPFROMREG(rv.s, MIPSInst_FS(ir));
847 goto copcsr;
848
849 /* binary op on handler */
850 scopbop:
851 {
852 ieee754sp fs, ft;
853
854 SPFROMREG(fs, MIPSInst_FS(ir));
855 SPFROMREG(ft, MIPSInst_FT(ir));
856
857 rv.s = (*handler.b) (fs, ft);
858 goto copcsr;
859 }
860 scopuop:
861 {
862 ieee754sp fs;
863
864 SPFROMREG(fs, MIPSInst_FS(ir));
865 rv.s = (*handler.u) (fs);
866 goto copcsr;
867 }
868 copcsr:
869 if (ieee754_cxtest(IEEE754_INEXACT))
870 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
871 if (ieee754_cxtest(IEEE754_UNDERFLOW))
872 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
873 if (ieee754_cxtest(IEEE754_OVERFLOW))
874 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
875 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
876 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
877 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
878 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
879 break;
880
881 /* unary conv ops */
882 case fcvts_op:
883 return SIGILL; /* not defined */
884 case fcvtd_op:{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 ieee754sp fs;
886
887 SPFROMREG(fs, MIPSInst_FS(ir));
888 rv.d = ieee754dp_fsp(fs);
889 rfmt = d_fmt;
890 goto copcsr;
891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 case fcvtw_op:{
893 ieee754sp fs;
894
895 SPFROMREG(fs, MIPSInst_FS(ir));
896 rv.w = ieee754sp_tint(fs);
897 rfmt = w_fmt;
898 goto copcsr;
899 }
900
Ralf Baechle587cb982005-09-15 08:52:34 +0000901#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 case fround_op:
903 case ftrunc_op:
904 case fceil_op:
905 case ffloor_op:{
906 unsigned int oldrm = ieee754_csr.rm;
907 ieee754sp fs;
908
909 SPFROMREG(fs, MIPSInst_FS(ir));
910 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
911 rv.w = ieee754sp_tint(fs);
912 ieee754_csr.rm = oldrm;
913 rfmt = w_fmt;
914 goto copcsr;
915 }
916#endif /* __mips >= 2 */
917
Ralf Baechle4b724ef2005-10-23 15:05:47 +0100918#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 case fcvtl_op:{
920 ieee754sp fs;
921
922 SPFROMREG(fs, MIPSInst_FS(ir));
923 rv.l = ieee754sp_tlong(fs);
924 rfmt = l_fmt;
925 goto copcsr;
926 }
927
928 case froundl_op:
929 case ftruncl_op:
930 case fceill_op:
931 case ffloorl_op:{
932 unsigned int oldrm = ieee754_csr.rm;
933 ieee754sp fs;
934
935 SPFROMREG(fs, MIPSInst_FS(ir));
936 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
937 rv.l = ieee754sp_tlong(fs);
938 ieee754_csr.rm = oldrm;
939 rfmt = l_fmt;
940 goto copcsr;
941 }
Ralf Baechle4b724ef2005-10-23 15:05:47 +0100942#endif /* defined(__mips64) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 default:
945 if (MIPSInst_FUNC(ir) >= fcmp_op) {
946 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
947 ieee754sp fs, ft;
948
949 SPFROMREG(fs, MIPSInst_FS(ir));
950 SPFROMREG(ft, MIPSInst_FT(ir));
951 rv.w = ieee754sp_cmp(fs, ft,
952 cmptab[cmpop & 0x7], cmpop & 0x8);
953 rfmt = -1;
954 if ((cmpop & 0x8) && ieee754_cxtest
955 (IEEE754_INVALID_OPERATION))
956 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
957 else
958 goto copcsr;
959
960 }
961 else {
962 return SIGILL;
963 }
964 break;
965 }
966 break;
967 }
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 case d_fmt:{
970 union {
971 ieee754dp(*b) (ieee754dp, ieee754dp);
972 ieee754dp(*u) (ieee754dp);
973 } handler;
974
975 switch (MIPSInst_FUNC(ir)) {
976 /* binary ops */
977 case fadd_op:
978 handler.b = ieee754dp_add;
979 goto dcopbop;
980 case fsub_op:
981 handler.b = ieee754dp_sub;
982 goto dcopbop;
983 case fmul_op:
984 handler.b = ieee754dp_mul;
985 goto dcopbop;
986 case fdiv_op:
987 handler.b = ieee754dp_div;
988 goto dcopbop;
989
990 /* unary ops */
Ralf Baechle587cb982005-09-15 08:52:34 +0000991#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 case fsqrt_op:
993 handler.u = ieee754dp_sqrt;
994 goto dcopuop;
995#endif
996#if __mips >= 4 && __mips != 32
997 case frsqrt_op:
998 handler.u = fpemu_dp_rsqrt;
999 goto dcopuop;
1000 case frecip_op:
1001 handler.u = fpemu_dp_recip;
1002 goto dcopuop;
1003#endif
1004#if __mips >= 4
1005 case fmovc_op:
1006 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1007 if (((ctx->fcr31 & cond) != 0) !=
1008 ((MIPSInst_FT(ir) & 1) != 0))
1009 return 0;
1010 DPFROMREG(rv.d, MIPSInst_FS(ir));
1011 break;
1012 case fmovz_op:
1013 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1014 return 0;
1015 DPFROMREG(rv.d, MIPSInst_FS(ir));
1016 break;
1017 case fmovn_op:
1018 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1019 return 0;
1020 DPFROMREG(rv.d, MIPSInst_FS(ir));
1021 break;
1022#endif
1023 case fabs_op:
1024 handler.u = ieee754dp_abs;
1025 goto dcopuop;
1026
1027 case fneg_op:
1028 handler.u = ieee754dp_neg;
1029 goto dcopuop;
1030
1031 case fmov_op:
1032 /* an easy one */
1033 DPFROMREG(rv.d, MIPSInst_FS(ir));
1034 goto copcsr;
1035
1036 /* binary op on handler */
1037 dcopbop:{
1038 ieee754dp fs, ft;
1039
1040 DPFROMREG(fs, MIPSInst_FS(ir));
1041 DPFROMREG(ft, MIPSInst_FT(ir));
1042
1043 rv.d = (*handler.b) (fs, ft);
1044 goto copcsr;
1045 }
1046 dcopuop:{
1047 ieee754dp fs;
1048
1049 DPFROMREG(fs, MIPSInst_FS(ir));
1050 rv.d = (*handler.u) (fs);
1051 goto copcsr;
1052 }
1053
1054 /* unary conv ops */
1055 case fcvts_op:{
1056 ieee754dp fs;
1057
1058 DPFROMREG(fs, MIPSInst_FS(ir));
1059 rv.s = ieee754sp_fdp(fs);
1060 rfmt = s_fmt;
1061 goto copcsr;
1062 }
1063 case fcvtd_op:
1064 return SIGILL; /* not defined */
1065
1066 case fcvtw_op:{
1067 ieee754dp fs;
1068
1069 DPFROMREG(fs, MIPSInst_FS(ir));
1070 rv.w = ieee754dp_tint(fs); /* wrong */
1071 rfmt = w_fmt;
1072 goto copcsr;
1073 }
1074
Ralf Baechle587cb982005-09-15 08:52:34 +00001075#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 case fround_op:
1077 case ftrunc_op:
1078 case fceil_op:
1079 case ffloor_op:{
1080 unsigned int oldrm = ieee754_csr.rm;
1081 ieee754dp fs;
1082
1083 DPFROMREG(fs, MIPSInst_FS(ir));
1084 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1085 rv.w = ieee754dp_tint(fs);
1086 ieee754_csr.rm = oldrm;
1087 rfmt = w_fmt;
1088 goto copcsr;
1089 }
1090#endif
1091
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001092#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 case fcvtl_op:{
1094 ieee754dp fs;
1095
1096 DPFROMREG(fs, MIPSInst_FS(ir));
1097 rv.l = ieee754dp_tlong(fs);
1098 rfmt = l_fmt;
1099 goto copcsr;
1100 }
1101
1102 case froundl_op:
1103 case ftruncl_op:
1104 case fceill_op:
1105 case ffloorl_op:{
1106 unsigned int oldrm = ieee754_csr.rm;
1107 ieee754dp fs;
1108
1109 DPFROMREG(fs, MIPSInst_FS(ir));
1110 ieee754_csr.rm = ieee_rm[MIPSInst_FUNC(ir) & 0x3];
1111 rv.l = ieee754dp_tlong(fs);
1112 ieee754_csr.rm = oldrm;
1113 rfmt = l_fmt;
1114 goto copcsr;
1115 }
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001116#endif /* __mips >= 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 default:
1119 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1120 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
1121 ieee754dp fs, ft;
1122
1123 DPFROMREG(fs, MIPSInst_FS(ir));
1124 DPFROMREG(ft, MIPSInst_FT(ir));
1125 rv.w = ieee754dp_cmp(fs, ft,
1126 cmptab[cmpop & 0x7], cmpop & 0x8);
1127 rfmt = -1;
1128 if ((cmpop & 0x8)
1129 &&
1130 ieee754_cxtest
1131 (IEEE754_INVALID_OPERATION))
1132 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1133 else
1134 goto copcsr;
1135
1136 }
1137 else {
1138 return SIGILL;
1139 }
1140 break;
1141 }
1142 break;
1143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 case w_fmt:{
1146 ieee754sp fs;
1147
1148 switch (MIPSInst_FUNC(ir)) {
1149 case fcvts_op:
1150 /* convert word to single precision real */
1151 SPFROMREG(fs, MIPSInst_FS(ir));
1152 rv.s = ieee754sp_fint(fs.bits);
1153 rfmt = s_fmt;
1154 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 case fcvtd_op:
1156 /* convert word to double precision real */
1157 SPFROMREG(fs, MIPSInst_FS(ir));
1158 rv.d = ieee754dp_fint(fs.bits);
1159 rfmt = d_fmt;
1160 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 default:
1162 return SIGILL;
1163 }
1164 break;
1165 }
1166
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001167#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 case l_fmt:{
1169 switch (MIPSInst_FUNC(ir)) {
1170 case fcvts_op:
1171 /* convert long to single precision real */
1172 rv.s = ieee754sp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1173 rfmt = s_fmt;
1174 goto copcsr;
1175 case fcvtd_op:
1176 /* convert long to double precision real */
1177 rv.d = ieee754dp_flong(ctx->fpr[MIPSInst_FS(ir)]);
1178 rfmt = d_fmt;
1179 goto copcsr;
1180 default:
1181 return SIGILL;
1182 }
1183 break;
1184 }
1185#endif
1186
1187 default:
1188 return SIGILL;
1189 }
1190
1191 /*
1192 * Update the fpu CSR register for this operation.
1193 * If an exception is required, generate a tidy SIGFPE exception,
1194 * without updating the result register.
1195 * Note: cause exception bits do not accumulate, they are rewritten
1196 * for each op; only the flag/sticky bits accumulate.
1197 */
1198 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1199 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1200 /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
1201 return SIGFPE;
1202 }
1203
1204 /*
1205 * Now we can safely write the result back to the register file.
1206 */
1207 switch (rfmt) {
1208 case -1:{
1209#if __mips >= 4
1210 cond = fpucondbit[MIPSInst_FD(ir) >> 2];
1211#else
1212 cond = FPU_CSR_COND;
1213#endif
1214 if (rv.w)
1215 ctx->fcr31 |= cond;
1216 else
1217 ctx->fcr31 &= ~cond;
1218 break;
1219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 case d_fmt:
1221 DPTOREG(rv.d, MIPSInst_FD(ir));
1222 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 case s_fmt:
1224 SPTOREG(rv.s, MIPSInst_FD(ir));
1225 break;
1226 case w_fmt:
1227 SITOREG(rv.w, MIPSInst_FD(ir));
1228 break;
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001229#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 case l_fmt:
1231 DITOREG(rv.l, MIPSInst_FD(ir));
1232 break;
1233#endif
1234 default:
1235 return SIGILL;
1236 }
1237
1238 return 0;
1239}
1240
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09001241int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1242 int has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Ralf Baechle333d1f62005-02-28 17:55:57 +00001244 unsigned long oldepc, prevepc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 mips_instruction insn;
1246 int sig = 0;
1247
1248 oldepc = xcp->cp0_epc;
1249 do {
1250 prevepc = xcp->cp0_epc;
1251
Ralf Baechle3fccc012005-10-23 13:58:21 +01001252 if (get_user(insn, (mips_instruction __user *) xcp->cp0_epc)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001253 MIPS_FPU_EMU_INC_STATS(errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return SIGBUS;
1255 }
1256 if (insn == 0)
1257 xcp->cp0_epc += 4; /* skip nops */
1258 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00001259 /*
1260 * The 'ieee754_csr' is an alias of
1261 * ctx->fcr31. No need to copy ctx->fcr31 to
1262 * ieee754_csr. But ieee754_csr.rm is ieee
1263 * library modes. (not mips rounding mode)
1264 */
1265 /* convert to ieee library modes */
1266 ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 sig = cop1Emulate(xcp, ctx);
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00001268 /* revert to mips rounding mode */
1269 ieee754_csr.rm = mips_rm[ieee754_csr.rm];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09001272 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 break;
1274 if (sig)
1275 break;
1276
1277 cond_resched();
1278 } while (xcp->cp0_epc > prevepc);
1279
1280 /* SIGILL indicates a non-fpu instruction */
1281 if (sig == SIGILL && xcp->cp0_epc != oldepc)
1282 /* but if epc has advanced, then ignore it */
1283 sig = 0;
1284
1285 return sig;
1286}
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +09001287
1288#ifdef CONFIG_DEBUG_FS
David Daneyb6ee75e2009-11-05 11:34:26 -08001289
1290static int fpuemu_stat_get(void *data, u64 *val)
1291{
1292 int cpu;
1293 unsigned long sum = 0;
1294 for_each_online_cpu(cpu) {
1295 struct mips_fpu_emulator_stats *ps;
1296 local_t *pv;
1297 ps = &per_cpu(fpuemustats, cpu);
1298 pv = (void *)ps + (unsigned long)data;
1299 sum += local_read(pv);
1300 }
1301 *val = sum;
1302 return 0;
1303}
1304DEFINE_SIMPLE_ATTRIBUTE(fops_fpuemu_stat, fpuemu_stat_get, NULL, "%llu\n");
1305
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +09001306extern struct dentry *mips_debugfs_dir;
1307static int __init debugfs_fpuemu(void)
1308{
1309 struct dentry *d, *dir;
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +09001310
1311 if (!mips_debugfs_dir)
1312 return -ENODEV;
1313 dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir);
Zhaoleiecab1f42008-10-17 19:12:30 +08001314 if (!dir)
1315 return -ENOMEM;
David Daneyb6ee75e2009-11-05 11:34:26 -08001316
1317#define FPU_STAT_CREATE(M) \
1318 do { \
1319 d = debugfs_create_file(#M , S_IRUGO, dir, \
1320 (void *)offsetof(struct mips_fpu_emulator_stats, M), \
1321 &fops_fpuemu_stat); \
1322 if (!d) \
1323 return -ENOMEM; \
1324 } while (0)
1325
1326 FPU_STAT_CREATE(emulated);
1327 FPU_STAT_CREATE(loads);
1328 FPU_STAT_CREATE(stores);
1329 FPU_STAT_CREATE(cp1ops);
1330 FPU_STAT_CREATE(cp1xops);
1331 FPU_STAT_CREATE(errors);
1332
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +09001333 return 0;
1334}
1335__initcall(debugfs_fpuemu);
1336#endif