blob: c4b855e7b0e0dd4ea371a74b20052ad641c8bc5c [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 * 59 Temple Place - Suite 330, Boston MA 02111-1307, 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>
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +090037#include <linux/debugfs.h>
Ralf Baechle85c51c52014-04-16 02:46:11 +020038#include <linux/percpu-defs.h>
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +080039#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Ralf Baechlecd8ee342014-04-16 02:09:53 +020041#include <asm/branch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/inst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/ptrace.h>
44#include <asm/signal.h>
Ralf Baechlecd8ee342014-04-16 02:09:53 +020045#include <asm/uaccess.h>
46
47#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/fpu_emulator.h>
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050049#include <asm/fpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
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 *,
David Daney515b0292010-10-21 16:32:26 -070067 struct mips_fpu_struct *, mips_instruction, void *__user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#endif
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* Control registers */
71
72#define FPCREG_RID 0 /* $0 = revision id */
73#define FPCREG_CSR 31 /* $31 = csr */
74
Shane McDonald95e8f632010-05-06 23:26:57 -060075/* Determine rounding mode from the RM bits of the FCSR */
76#define modeindex(v) ((v) & FPU_CSR_RM)
77
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050078/* microMIPS bitfields */
79#define MM_POOL32A_MINOR_MASK 0x3f
80#define MM_POOL32A_MINOR_SHIFT 0x6
81#define MM_MIPS32_COND_FC 0x30
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Convert Mips rounding mode (0..3) to IEEE library modes. */
84static const unsigned char ieee_rm[4] = {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000085 [FPU_CSR_RN] = IEEE754_RN,
86 [FPU_CSR_RZ] = IEEE754_RZ,
87 [FPU_CSR_RU] = IEEE754_RU,
88 [FPU_CSR_RD] = IEEE754_RD,
89};
90/* Convert IEEE library modes to Mips rounding mode (0..3). */
91static const unsigned char mips_rm[4] = {
92 [IEEE754_RN] = FPU_CSR_RN,
93 [IEEE754_RZ] = FPU_CSR_RZ,
94 [IEEE754_RD] = FPU_CSR_RD,
95 [IEEE754_RU] = FPU_CSR_RU,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98#if __mips >= 4
99/* convert condition code register number to csr bit */
100static const unsigned int fpucondbit[8] = {
101 FPU_CSR_COND0,
102 FPU_CSR_COND1,
103 FPU_CSR_COND2,
104 FPU_CSR_COND3,
105 FPU_CSR_COND4,
106 FPU_CSR_COND5,
107 FPU_CSR_COND6,
108 FPU_CSR_COND7
109};
110#endif
111
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500112/* (microMIPS) Convert 16-bit register encoding to 32-bit register encoding. */
113static const unsigned int reg16to32map[8] = {16, 17, 2, 3, 4, 5, 6, 7};
114
115/* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
116static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
117static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
118static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
119static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
120
121/*
122 * This functions translates a 32-bit microMIPS instruction
123 * into a 32-bit MIPS32 instruction. Returns 0 on success
124 * and SIGILL otherwise.
125 */
126static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
127{
128 union mips_instruction insn = *insn_ptr;
129 union mips_instruction mips32_insn = insn;
130 int func, fmt, op;
131
132 switch (insn.mm_i_format.opcode) {
133 case mm_ldc132_op:
134 mips32_insn.mm_i_format.opcode = ldc1_op;
135 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
136 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
137 break;
138 case mm_lwc132_op:
139 mips32_insn.mm_i_format.opcode = lwc1_op;
140 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
141 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
142 break;
143 case mm_sdc132_op:
144 mips32_insn.mm_i_format.opcode = sdc1_op;
145 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
146 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
147 break;
148 case mm_swc132_op:
149 mips32_insn.mm_i_format.opcode = swc1_op;
150 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
151 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
152 break;
153 case mm_pool32i_op:
154 /* NOTE: offset is << by 1 if in microMIPS mode. */
155 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
156 (insn.mm_i_format.rt == mm_bc1t_op)) {
157 mips32_insn.fb_format.opcode = cop1_op;
158 mips32_insn.fb_format.bc = bc_op;
159 mips32_insn.fb_format.flag =
160 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
161 } else
162 return SIGILL;
163 break;
164 case mm_pool32f_op:
165 switch (insn.mm_fp0_format.func) {
166 case mm_32f_01_op:
167 case mm_32f_11_op:
168 case mm_32f_02_op:
169 case mm_32f_12_op:
170 case mm_32f_41_op:
171 case mm_32f_51_op:
172 case mm_32f_42_op:
173 case mm_32f_52_op:
174 op = insn.mm_fp0_format.func;
175 if (op == mm_32f_01_op)
176 func = madd_s_op;
177 else if (op == mm_32f_11_op)
178 func = madd_d_op;
179 else if (op == mm_32f_02_op)
180 func = nmadd_s_op;
181 else if (op == mm_32f_12_op)
182 func = nmadd_d_op;
183 else if (op == mm_32f_41_op)
184 func = msub_s_op;
185 else if (op == mm_32f_51_op)
186 func = msub_d_op;
187 else if (op == mm_32f_42_op)
188 func = nmsub_s_op;
189 else
190 func = nmsub_d_op;
191 mips32_insn.fp6_format.opcode = cop1x_op;
192 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
193 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
194 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
195 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
196 mips32_insn.fp6_format.func = func;
197 break;
198 case mm_32f_10_op:
199 func = -1; /* Invalid */
200 op = insn.mm_fp5_format.op & 0x7;
201 if (op == mm_ldxc1_op)
202 func = ldxc1_op;
203 else if (op == mm_sdxc1_op)
204 func = sdxc1_op;
205 else if (op == mm_lwxc1_op)
206 func = lwxc1_op;
207 else if (op == mm_swxc1_op)
208 func = swxc1_op;
209
210 if (func != -1) {
211 mips32_insn.r_format.opcode = cop1x_op;
212 mips32_insn.r_format.rs =
213 insn.mm_fp5_format.base;
214 mips32_insn.r_format.rt =
215 insn.mm_fp5_format.index;
216 mips32_insn.r_format.rd = 0;
217 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
218 mips32_insn.r_format.func = func;
219 } else
220 return SIGILL;
221 break;
222 case mm_32f_40_op:
223 op = -1; /* Invalid */
224 if (insn.mm_fp2_format.op == mm_fmovt_op)
225 op = 1;
226 else if (insn.mm_fp2_format.op == mm_fmovf_op)
227 op = 0;
228 if (op != -1) {
229 mips32_insn.fp0_format.opcode = cop1_op;
230 mips32_insn.fp0_format.fmt =
231 sdps_format[insn.mm_fp2_format.fmt];
232 mips32_insn.fp0_format.ft =
233 (insn.mm_fp2_format.cc<<2) + op;
234 mips32_insn.fp0_format.fs =
235 insn.mm_fp2_format.fs;
236 mips32_insn.fp0_format.fd =
237 insn.mm_fp2_format.fd;
238 mips32_insn.fp0_format.func = fmovc_op;
239 } else
240 return SIGILL;
241 break;
242 case mm_32f_60_op:
243 func = -1; /* Invalid */
244 if (insn.mm_fp0_format.op == mm_fadd_op)
245 func = fadd_op;
246 else if (insn.mm_fp0_format.op == mm_fsub_op)
247 func = fsub_op;
248 else if (insn.mm_fp0_format.op == mm_fmul_op)
249 func = fmul_op;
250 else if (insn.mm_fp0_format.op == mm_fdiv_op)
251 func = fdiv_op;
252 if (func != -1) {
253 mips32_insn.fp0_format.opcode = cop1_op;
254 mips32_insn.fp0_format.fmt =
255 sdps_format[insn.mm_fp0_format.fmt];
256 mips32_insn.fp0_format.ft =
257 insn.mm_fp0_format.ft;
258 mips32_insn.fp0_format.fs =
259 insn.mm_fp0_format.fs;
260 mips32_insn.fp0_format.fd =
261 insn.mm_fp0_format.fd;
262 mips32_insn.fp0_format.func = func;
263 } else
264 return SIGILL;
265 break;
266 case mm_32f_70_op:
267 func = -1; /* Invalid */
268 if (insn.mm_fp0_format.op == mm_fmovn_op)
269 func = fmovn_op;
270 else if (insn.mm_fp0_format.op == mm_fmovz_op)
271 func = fmovz_op;
272 if (func != -1) {
273 mips32_insn.fp0_format.opcode = cop1_op;
274 mips32_insn.fp0_format.fmt =
275 sdps_format[insn.mm_fp0_format.fmt];
276 mips32_insn.fp0_format.ft =
277 insn.mm_fp0_format.ft;
278 mips32_insn.fp0_format.fs =
279 insn.mm_fp0_format.fs;
280 mips32_insn.fp0_format.fd =
281 insn.mm_fp0_format.fd;
282 mips32_insn.fp0_format.func = func;
283 } else
284 return SIGILL;
285 break;
286 case mm_32f_73_op: /* POOL32FXF */
287 switch (insn.mm_fp1_format.op) {
288 case mm_movf0_op:
289 case mm_movf1_op:
290 case mm_movt0_op:
291 case mm_movt1_op:
292 if ((insn.mm_fp1_format.op & 0x7f) ==
293 mm_movf0_op)
294 op = 0;
295 else
296 op = 1;
297 mips32_insn.r_format.opcode = spec_op;
298 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
299 mips32_insn.r_format.rt =
300 (insn.mm_fp4_format.cc << 2) + op;
301 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
302 mips32_insn.r_format.re = 0;
303 mips32_insn.r_format.func = movc_op;
304 break;
305 case mm_fcvtd0_op:
306 case mm_fcvtd1_op:
307 case mm_fcvts0_op:
308 case mm_fcvts1_op:
309 if ((insn.mm_fp1_format.op & 0x7f) ==
310 mm_fcvtd0_op) {
311 func = fcvtd_op;
312 fmt = swl_format[insn.mm_fp3_format.fmt];
313 } else {
314 func = fcvts_op;
315 fmt = dwl_format[insn.mm_fp3_format.fmt];
316 }
317 mips32_insn.fp0_format.opcode = cop1_op;
318 mips32_insn.fp0_format.fmt = fmt;
319 mips32_insn.fp0_format.ft = 0;
320 mips32_insn.fp0_format.fs =
321 insn.mm_fp3_format.fs;
322 mips32_insn.fp0_format.fd =
323 insn.mm_fp3_format.rt;
324 mips32_insn.fp0_format.func = func;
325 break;
326 case mm_fmov0_op:
327 case mm_fmov1_op:
328 case mm_fabs0_op:
329 case mm_fabs1_op:
330 case mm_fneg0_op:
331 case mm_fneg1_op:
332 if ((insn.mm_fp1_format.op & 0x7f) ==
333 mm_fmov0_op)
334 func = fmov_op;
335 else if ((insn.mm_fp1_format.op & 0x7f) ==
336 mm_fabs0_op)
337 func = fabs_op;
338 else
339 func = fneg_op;
340 mips32_insn.fp0_format.opcode = cop1_op;
341 mips32_insn.fp0_format.fmt =
342 sdps_format[insn.mm_fp3_format.fmt];
343 mips32_insn.fp0_format.ft = 0;
344 mips32_insn.fp0_format.fs =
345 insn.mm_fp3_format.fs;
346 mips32_insn.fp0_format.fd =
347 insn.mm_fp3_format.rt;
348 mips32_insn.fp0_format.func = func;
349 break;
350 case mm_ffloorl_op:
351 case mm_ffloorw_op:
352 case mm_fceill_op:
353 case mm_fceilw_op:
354 case mm_ftruncl_op:
355 case mm_ftruncw_op:
356 case mm_froundl_op:
357 case mm_froundw_op:
358 case mm_fcvtl_op:
359 case mm_fcvtw_op:
360 if (insn.mm_fp1_format.op == mm_ffloorl_op)
361 func = ffloorl_op;
362 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
363 func = ffloor_op;
364 else if (insn.mm_fp1_format.op == mm_fceill_op)
365 func = fceill_op;
366 else if (insn.mm_fp1_format.op == mm_fceilw_op)
367 func = fceil_op;
368 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
369 func = ftruncl_op;
370 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
371 func = ftrunc_op;
372 else if (insn.mm_fp1_format.op == mm_froundl_op)
373 func = froundl_op;
374 else if (insn.mm_fp1_format.op == mm_froundw_op)
375 func = fround_op;
376 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
377 func = fcvtl_op;
378 else
379 func = fcvtw_op;
380 mips32_insn.fp0_format.opcode = cop1_op;
381 mips32_insn.fp0_format.fmt =
382 sd_format[insn.mm_fp1_format.fmt];
383 mips32_insn.fp0_format.ft = 0;
384 mips32_insn.fp0_format.fs =
385 insn.mm_fp1_format.fs;
386 mips32_insn.fp0_format.fd =
387 insn.mm_fp1_format.rt;
388 mips32_insn.fp0_format.func = func;
389 break;
390 case mm_frsqrt_op:
391 case mm_fsqrt_op:
392 case mm_frecip_op:
393 if (insn.mm_fp1_format.op == mm_frsqrt_op)
394 func = frsqrt_op;
395 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
396 func = fsqrt_op;
397 else
398 func = frecip_op;
399 mips32_insn.fp0_format.opcode = cop1_op;
400 mips32_insn.fp0_format.fmt =
401 sdps_format[insn.mm_fp1_format.fmt];
402 mips32_insn.fp0_format.ft = 0;
403 mips32_insn.fp0_format.fs =
404 insn.mm_fp1_format.fs;
405 mips32_insn.fp0_format.fd =
406 insn.mm_fp1_format.rt;
407 mips32_insn.fp0_format.func = func;
408 break;
409 case mm_mfc1_op:
410 case mm_mtc1_op:
411 case mm_cfc1_op:
412 case mm_ctc1_op:
Steven J. Hill9355e592013-11-07 12:48:29 +0000413 case mm_mfhc1_op:
414 case mm_mthc1_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500415 if (insn.mm_fp1_format.op == mm_mfc1_op)
416 op = mfc_op;
417 else if (insn.mm_fp1_format.op == mm_mtc1_op)
418 op = mtc_op;
419 else if (insn.mm_fp1_format.op == mm_cfc1_op)
420 op = cfc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000421 else if (insn.mm_fp1_format.op == mm_ctc1_op)
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500422 op = ctc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000423 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
424 op = mfhc_op;
425 else
426 op = mthc_op;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500427 mips32_insn.fp1_format.opcode = cop1_op;
428 mips32_insn.fp1_format.op = op;
429 mips32_insn.fp1_format.rt =
430 insn.mm_fp1_format.rt;
431 mips32_insn.fp1_format.fs =
432 insn.mm_fp1_format.fs;
433 mips32_insn.fp1_format.fd = 0;
434 mips32_insn.fp1_format.func = 0;
435 break;
436 default:
437 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500438 }
439 break;
440 case mm_32f_74_op: /* c.cond.fmt */
441 mips32_insn.fp0_format.opcode = cop1_op;
442 mips32_insn.fp0_format.fmt =
443 sdps_format[insn.mm_fp4_format.fmt];
444 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
445 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
446 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
447 mips32_insn.fp0_format.func =
448 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
449 break;
450 default:
451 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500452 }
453 break;
454 default:
455 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500456 }
457
458 *insn_ptr = mips32_insn;
459 return 0;
460}
461
462int mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
463 unsigned long *contpc)
464{
465 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
466 int bc_false = 0;
467 unsigned int fcr31;
468 unsigned int bit;
469
David Daneyfe6d2902013-05-24 20:54:09 +0000470 if (!cpu_has_mmips)
471 return 0;
472
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500473 switch (insn.mm_i_format.opcode) {
474 case mm_pool32a_op:
475 if ((insn.mm_i_format.simmediate & MM_POOL32A_MINOR_MASK) ==
476 mm_pool32axf_op) {
477 switch (insn.mm_i_format.simmediate >>
478 MM_POOL32A_MINOR_SHIFT) {
479 case mm_jalr_op:
480 case mm_jalrhb_op:
481 case mm_jalrs_op:
482 case mm_jalrshb_op:
483 if (insn.mm_i_format.rt != 0) /* Not mm_jr */
484 regs->regs[insn.mm_i_format.rt] =
485 regs->cp0_epc +
486 dec_insn.pc_inc +
487 dec_insn.next_pc_inc;
488 *contpc = regs->regs[insn.mm_i_format.rs];
489 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500490 }
491 }
492 break;
493 case mm_pool32i_op:
494 switch (insn.mm_i_format.rt) {
495 case mm_bltzals_op:
496 case mm_bltzal_op:
497 regs->regs[31] = regs->cp0_epc +
498 dec_insn.pc_inc +
499 dec_insn.next_pc_inc;
500 /* Fall through */
501 case mm_bltz_op:
502 if ((long)regs->regs[insn.mm_i_format.rs] < 0)
503 *contpc = regs->cp0_epc +
504 dec_insn.pc_inc +
505 (insn.mm_i_format.simmediate << 1);
506 else
507 *contpc = regs->cp0_epc +
508 dec_insn.pc_inc +
509 dec_insn.next_pc_inc;
510 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500511 case mm_bgezals_op:
512 case mm_bgezal_op:
513 regs->regs[31] = regs->cp0_epc +
514 dec_insn.pc_inc +
515 dec_insn.next_pc_inc;
516 /* Fall through */
517 case mm_bgez_op:
518 if ((long)regs->regs[insn.mm_i_format.rs] >= 0)
519 *contpc = regs->cp0_epc +
520 dec_insn.pc_inc +
521 (insn.mm_i_format.simmediate << 1);
522 else
523 *contpc = regs->cp0_epc +
524 dec_insn.pc_inc +
525 dec_insn.next_pc_inc;
526 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500527 case mm_blez_op:
528 if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
529 *contpc = regs->cp0_epc +
530 dec_insn.pc_inc +
531 (insn.mm_i_format.simmediate << 1);
532 else
533 *contpc = regs->cp0_epc +
534 dec_insn.pc_inc +
535 dec_insn.next_pc_inc;
536 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500537 case mm_bgtz_op:
538 if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
539 *contpc = regs->cp0_epc +
540 dec_insn.pc_inc +
541 (insn.mm_i_format.simmediate << 1);
542 else
543 *contpc = regs->cp0_epc +
544 dec_insn.pc_inc +
545 dec_insn.next_pc_inc;
546 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500547 case mm_bc2f_op:
548 case mm_bc1f_op:
549 bc_false = 1;
550 /* Fall through */
551 case mm_bc2t_op:
552 case mm_bc1t_op:
553 preempt_disable();
554 if (is_fpu_owner())
555 asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
556 else
557 fcr31 = current->thread.fpu.fcr31;
558 preempt_enable();
559
560 if (bc_false)
561 fcr31 = ~fcr31;
562
563 bit = (insn.mm_i_format.rs >> 2);
564 bit += (bit != 0);
565 bit += 23;
566 if (fcr31 & (1 << bit))
567 *contpc = regs->cp0_epc +
568 dec_insn.pc_inc +
569 (insn.mm_i_format.simmediate << 1);
570 else
571 *contpc = regs->cp0_epc +
572 dec_insn.pc_inc + dec_insn.next_pc_inc;
573 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500574 }
575 break;
576 case mm_pool16c_op:
577 switch (insn.mm_i_format.rt) {
578 case mm_jalr16_op:
579 case mm_jalrs16_op:
580 regs->regs[31] = regs->cp0_epc +
581 dec_insn.pc_inc + dec_insn.next_pc_inc;
582 /* Fall through */
583 case mm_jr16_op:
584 *contpc = regs->regs[insn.mm_i_format.rs];
585 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500586 }
587 break;
588 case mm_beqz16_op:
589 if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] == 0)
590 *contpc = regs->cp0_epc +
591 dec_insn.pc_inc +
592 (insn.mm_b1_format.simmediate << 1);
593 else
594 *contpc = regs->cp0_epc +
595 dec_insn.pc_inc + dec_insn.next_pc_inc;
596 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500597 case mm_bnez16_op:
598 if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] != 0)
599 *contpc = regs->cp0_epc +
600 dec_insn.pc_inc +
601 (insn.mm_b1_format.simmediate << 1);
602 else
603 *contpc = regs->cp0_epc +
604 dec_insn.pc_inc + dec_insn.next_pc_inc;
605 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500606 case mm_b16_op:
607 *contpc = regs->cp0_epc + dec_insn.pc_inc +
608 (insn.mm_b0_format.simmediate << 1);
609 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500610 case mm_beq32_op:
611 if (regs->regs[insn.mm_i_format.rs] ==
612 regs->regs[insn.mm_i_format.rt])
613 *contpc = regs->cp0_epc +
614 dec_insn.pc_inc +
615 (insn.mm_i_format.simmediate << 1);
616 else
617 *contpc = regs->cp0_epc +
618 dec_insn.pc_inc +
619 dec_insn.next_pc_inc;
620 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500621 case mm_bne32_op:
622 if (regs->regs[insn.mm_i_format.rs] !=
623 regs->regs[insn.mm_i_format.rt])
624 *contpc = regs->cp0_epc +
625 dec_insn.pc_inc +
626 (insn.mm_i_format.simmediate << 1);
627 else
628 *contpc = regs->cp0_epc +
629 dec_insn.pc_inc + dec_insn.next_pc_inc;
630 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500631 case mm_jalx32_op:
632 regs->regs[31] = regs->cp0_epc +
633 dec_insn.pc_inc + dec_insn.next_pc_inc;
634 *contpc = regs->cp0_epc + dec_insn.pc_inc;
635 *contpc >>= 28;
636 *contpc <<= 28;
637 *contpc |= (insn.j_format.target << 2);
638 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500639 case mm_jals32_op:
640 case mm_jal32_op:
641 regs->regs[31] = regs->cp0_epc +
642 dec_insn.pc_inc + dec_insn.next_pc_inc;
643 /* Fall through */
644 case mm_j32_op:
645 *contpc = regs->cp0_epc + dec_insn.pc_inc;
646 *contpc >>= 27;
647 *contpc <<= 27;
648 *contpc |= (insn.j_format.target << 1);
649 set_isa16_mode(*contpc);
650 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500651 }
652 return 0;
653}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655/*
656 * Redundant with logic already in kernel/branch.c,
657 * embedded in compute_return_epc. At some point,
658 * a single subroutine should be used across both
659 * modules.
660 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500661static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
662 unsigned long *contpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500664 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
665 unsigned int fcr31;
666 unsigned int bit = 0;
667
668 switch (insn.i_format.opcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 case spec_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500670 switch (insn.r_format.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 case jalr_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500672 regs->regs[insn.r_format.rd] =
673 regs->cp0_epc + dec_insn.pc_inc +
674 dec_insn.next_pc_inc;
675 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 case jr_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500677 *contpc = regs->regs[insn.r_format.rs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 1;
679 }
680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 case bcond_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500682 switch (insn.i_format.rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 case bltzal_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 case bltzall_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500685 regs->regs[31] = regs->cp0_epc +
686 dec_insn.pc_inc +
687 dec_insn.next_pc_inc;
688 /* Fall through */
689 case bltz_op:
690 case bltzl_op:
691 if ((long)regs->regs[insn.i_format.rs] < 0)
692 *contpc = regs->cp0_epc +
693 dec_insn.pc_inc +
694 (insn.i_format.simmediate << 2);
695 else
696 *contpc = regs->cp0_epc +
697 dec_insn.pc_inc +
698 dec_insn.next_pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500700 case bgezal_op:
701 case bgezall_op:
702 regs->regs[31] = regs->cp0_epc +
703 dec_insn.pc_inc +
704 dec_insn.next_pc_inc;
705 /* Fall through */
706 case bgez_op:
707 case bgezl_op:
708 if ((long)regs->regs[insn.i_format.rs] >= 0)
709 *contpc = regs->cp0_epc +
710 dec_insn.pc_inc +
711 (insn.i_format.simmediate << 2);
712 else
713 *contpc = regs->cp0_epc +
714 dec_insn.pc_inc +
715 dec_insn.next_pc_inc;
716 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 case jalx_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500720 set_isa16_mode(bit);
721 case jal_op:
722 regs->regs[31] = regs->cp0_epc +
723 dec_insn.pc_inc +
724 dec_insn.next_pc_inc;
725 /* Fall through */
726 case j_op:
727 *contpc = regs->cp0_epc + dec_insn.pc_inc;
728 *contpc >>= 28;
729 *contpc <<= 28;
730 *contpc |= (insn.j_format.target << 2);
731 /* Set microMIPS mode bit: XOR for jalx. */
732 *contpc ^= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500734 case beq_op:
735 case beql_op:
736 if (regs->regs[insn.i_format.rs] ==
737 regs->regs[insn.i_format.rt])
738 *contpc = regs->cp0_epc +
739 dec_insn.pc_inc +
740 (insn.i_format.simmediate << 2);
741 else
742 *contpc = regs->cp0_epc +
743 dec_insn.pc_inc +
744 dec_insn.next_pc_inc;
745 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500746 case bne_op:
747 case bnel_op:
748 if (regs->regs[insn.i_format.rs] !=
749 regs->regs[insn.i_format.rt])
750 *contpc = regs->cp0_epc +
751 dec_insn.pc_inc +
752 (insn.i_format.simmediate << 2);
753 else
754 *contpc = regs->cp0_epc +
755 dec_insn.pc_inc +
756 dec_insn.next_pc_inc;
757 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500758 case blez_op:
759 case blezl_op:
760 if ((long)regs->regs[insn.i_format.rs] <= 0)
761 *contpc = regs->cp0_epc +
762 dec_insn.pc_inc +
763 (insn.i_format.simmediate << 2);
764 else
765 *contpc = regs->cp0_epc +
766 dec_insn.pc_inc +
767 dec_insn.next_pc_inc;
768 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500769 case bgtz_op:
770 case bgtzl_op:
771 if ((long)regs->regs[insn.i_format.rs] > 0)
772 *contpc = regs->cp0_epc +
773 dec_insn.pc_inc +
774 (insn.i_format.simmediate << 2);
775 else
776 *contpc = regs->cp0_epc +
777 dec_insn.pc_inc +
778 dec_insn.next_pc_inc;
779 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700780#ifdef CONFIG_CPU_CAVIUM_OCTEON
781 case lwc2_op: /* This is bbit0 on Octeon */
782 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
783 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
784 else
785 *contpc = regs->cp0_epc + 8;
786 return 1;
787 case ldc2_op: /* This is bbit032 on Octeon */
788 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
789 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
790 else
791 *contpc = regs->cp0_epc + 8;
792 return 1;
793 case swc2_op: /* This is bbit1 on Octeon */
794 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
795 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
796 else
797 *contpc = regs->cp0_epc + 8;
798 return 1;
799 case sdc2_op: /* This is bbit132 on Octeon */
800 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
801 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
802 else
803 *contpc = regs->cp0_epc + 8;
804 return 1;
805#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 case cop0_op:
807 case cop1_op:
808 case cop2_op:
809 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500810 if (insn.i_format.rs == bc_op) {
811 preempt_disable();
812 if (is_fpu_owner())
813 asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
814 else
815 fcr31 = current->thread.fpu.fcr31;
816 preempt_enable();
817
818 bit = (insn.i_format.rt >> 2);
819 bit += (bit != 0);
820 bit += 23;
821 switch (insn.i_format.rt & 3) {
822 case 0: /* bc1f */
823 case 2: /* bc1fl */
824 if (~fcr31 & (1 << bit))
825 *contpc = regs->cp0_epc +
826 dec_insn.pc_inc +
827 (insn.i_format.simmediate << 2);
828 else
829 *contpc = regs->cp0_epc +
830 dec_insn.pc_inc +
831 dec_insn.next_pc_inc;
832 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500833 case 1: /* bc1t */
834 case 3: /* bc1tl */
835 if (fcr31 & (1 << bit))
836 *contpc = regs->cp0_epc +
837 dec_insn.pc_inc +
838 (insn.i_format.simmediate << 2);
839 else
840 *contpc = regs->cp0_epc +
841 dec_insn.pc_inc +
842 dec_insn.next_pc_inc;
843 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500844 }
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 break;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 0;
849}
850
851/*
852 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100853 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800854 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000855 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200856 * FPU emu is slow and bulky and optimizing this function offers fairly
857 * sizeable benefits so we try to be clever and make this function return
858 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000859 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 */
David Daneyda0bac32009-11-02 11:33:46 -0800861static inline int cop1_64bit(struct pt_regs *xcp)
862{
Ralf Baechle51d943f2012-08-15 19:42:19 +0200863#if defined(CONFIG_64BIT) && !defined(CONFIG_MIPS32_O32)
864 return 1;
Paul Burton597ce172013-11-22 13:12:07 +0000865#elif defined(CONFIG_32BIT) && !defined(CONFIG_MIPS_O32_FP64_SUPPORT)
David Daneyda0bac32009-11-02 11:33:46 -0800866 return 0;
Paul Burton597ce172013-11-22 13:12:07 +0000867#else
868 return !test_thread_flag(TIF_32BIT_FPREGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869#endif
David Daneyda0bac32009-11-02 11:33:46 -0800870}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Paul Burtonbbd426f2014-02-13 11:26:41 +0000872#define SIFROMREG(si, x) do { \
873 if (cop1_64bit(xcp)) \
874 (si) = get_fpr32(&ctx->fpr[x], 0); \
875 else \
876 (si) = get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
877} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800878
Paul Burtonbbd426f2014-02-13 11:26:41 +0000879#define SITOREG(si, x) do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000880 if (cop1_64bit(xcp)) { \
881 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000882 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000883 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
884 set_fpr32(&ctx->fpr[x], i, 0); \
885 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000886 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000887 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000888} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Paul Burtonbbd426f2014-02-13 11:26:41 +0000890#define SIFROMHREG(si, x) ((si) = get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000891
892#define SITOHREG(si, x) do { \
893 unsigned i; \
894 set_fpr32(&ctx->fpr[x], 1, si); \
895 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
896 set_fpr32(&ctx->fpr[x], i, 0); \
897} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000898
Paul Burtonbbd426f2014-02-13 11:26:41 +0000899#define DIFROMREG(di, x) \
900 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
901
Paul Burtonef1c47a2014-01-27 17:14:47 +0000902#define DITOREG(di, x) do { \
903 unsigned fpr, i; \
904 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
905 set_fpr64(&ctx->fpr[fpr], 0, di); \
906 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
907 set_fpr64(&ctx->fpr[fpr], i, 0); \
908} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Ralf Baechle21a151d2007-10-11 23:46:15 +0100910#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
911#define SPTOREG(sp, x) SITOREG((sp).bits, x)
912#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
913#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915/*
916 * Emulate the single floating point instruction pointed at by EPC.
917 * Two instructions if the instruction is in a branch delay slot.
918 */
919
David Daney515b0292010-10-21 16:32:26 -0700920static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500921 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 mips_instruction ir;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500924 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 unsigned int cond;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500926 int pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200929 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500930 if (dec_insn.micro_mips_mode) {
931 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200932 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500933 } else {
934 if (!isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200935 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500936 }
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200939 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /*
941 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +0100942 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * BEFORE we do the cop1 instruction.
944 *
945 * This branch could be a COP1 branch, but in that case we
946 * would have had a trap for that instruction, and would not
947 * come through this route.
948 *
949 * Linux MIPS branch emulator operates on context, updating the
950 * cp0_epc.
951 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500952 ir = dec_insn.next_insn; /* process delay slot instr */
953 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000954 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500955 ir = dec_insn.insn; /* process current instr */
956 pc_inc = dec_insn.pc_inc;
957 }
958
959 /*
960 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
961 * instructions, we want to convert microMIPS FPU instructions
962 * into MIPS32 instructions so that we could reuse all of the
963 * FPU emulation code.
964 *
965 * NOTE: We cannot do this for branch instructions since they
966 * are not a subset. Example: Cannot emulate a 16-bit
967 * aligned target address with a MIPS32 instruction.
968 */
969 if (dec_insn.micro_mips_mode) {
970 /*
971 * If next instruction is a 16-bit instruction, then it
972 * it cannot be a FPU instruction. This could happen
973 * since we can be called for non-FPU instructions.
974 */
975 if ((pc_inc == 2) ||
976 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
977 == SIGILL))
978 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
981 emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200982 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -0800983 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 switch (MIPSInst_OPCODE(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 case ldc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +0100986 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 MIPSInst_SIMM(ir));
988 u64 val;
989
David Daneyb6ee75e2009-11-05 11:34:26 -0800990 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -0700991
992 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800993 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -0700994 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return SIGBUS;
996 }
David Daney515b0292010-10-21 16:32:26 -0700997 if (__get_user(val, va)) {
998 MIPS_FPU_EMU_INC_STATS(errors);
999 *fault_addr = va;
1000 return SIGSEGV;
1001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 DITOREG(val, MIPSInst_RT(ir));
1003 break;
1004 }
1005
1006 case sdc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +01001007 u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 MIPSInst_SIMM(ir));
1009 u64 val;
1010
David Daneyb6ee75e2009-11-05 11:34:26 -08001011 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 DIFROMREG(val, MIPSInst_RT(ir));
David Daney515b0292010-10-21 16:32:26 -07001013 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001014 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001015 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return SIGBUS;
1017 }
David Daney515b0292010-10-21 16:32:26 -07001018 if (__put_user(val, va)) {
1019 MIPS_FPU_EMU_INC_STATS(errors);
1020 *fault_addr = va;
1021 return SIGSEGV;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 break;
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 case lwc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +01001027 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 MIPSInst_SIMM(ir));
1029 u32 val;
1030
David Daneyb6ee75e2009-11-05 11:34:26 -08001031 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001032 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001033 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001034 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return SIGBUS;
1036 }
David Daney515b0292010-10-21 16:32:26 -07001037 if (__get_user(val, va)) {
1038 MIPS_FPU_EMU_INC_STATS(errors);
1039 *fault_addr = va;
1040 return SIGSEGV;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 SITOREG(val, MIPSInst_RT(ir));
1043 break;
1044 }
1045
1046 case swc1_op:{
Ralf Baechle3fccc012005-10-23 13:58:21 +01001047 u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 MIPSInst_SIMM(ir));
1049 u32 val;
1050
David Daneyb6ee75e2009-11-05 11:34:26 -08001051 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 SIFROMREG(val, MIPSInst_RT(ir));
David Daney515b0292010-10-21 16:32:26 -07001053 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001054 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001055 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return SIGBUS;
1057 }
David Daney515b0292010-10-21 16:32:26 -07001058 if (__put_user(val, va)) {
1059 MIPS_FPU_EMU_INC_STATS(errors);
1060 *fault_addr = va;
1061 return SIGSEGV;
1062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 break;
1064 }
1065
1066 case cop1_op:
1067 switch (MIPSInst_RS(ir)) {
1068
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001069#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 case dmfc_op:
1071 /* copregister fs -> gpr[rt] */
1072 if (MIPSInst_RT(ir) != 0) {
1073 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1074 MIPSInst_RD(ir));
1075 }
1076 break;
1077
1078 case dmtc_op:
1079 /* copregister fs <- rt */
1080 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1081 break;
1082#endif
1083
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001084 case mfhc_op:
1085 if (!cpu_has_mips_r2)
1086 goto sigill;
1087
1088 /* copregister rd -> gpr[rt] */
1089 if (MIPSInst_RT(ir) != 0) {
1090 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1091 MIPSInst_RD(ir));
1092 }
1093 break;
1094
1095 case mthc_op:
1096 if (!cpu_has_mips_r2)
1097 goto sigill;
1098
1099 /* copregister rd <- gpr[rt] */
1100 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1101 break;
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 case mfc_op:
1104 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (MIPSInst_RT(ir) != 0) {
1106 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1107 MIPSInst_RD(ir));
1108 }
1109 break;
1110
1111 case mtc_op:
1112 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1114 break;
1115
1116 case cfc_op:{
1117 /* cop control register rd -> gpr[rt] */
1118 u32 value;
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1121 value = ctx->fcr31;
Shane McDonald3f135532010-05-07 00:02:09 -06001122 value = (value & ~FPU_CSR_RM) |
1123 mips_rm[modeindex(value)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124#ifdef CSRTRACE
1125 printk("%p gpr[%d]<-csr=%08x\n",
Ralf Baechle333d1f62005-02-28 17:55:57 +00001126 (void *) (xcp->cp0_epc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 MIPSInst_RT(ir), value);
1128#endif
1129 }
1130 else if (MIPSInst_RD(ir) == FPCREG_RID)
1131 value = 0;
1132 else
1133 value = 0;
1134 if (MIPSInst_RT(ir))
1135 xcp->regs[MIPSInst_RT(ir)] = value;
1136 break;
1137 }
1138
1139 case ctc_op:{
1140 /* copregister rd <- rt */
1141 u32 value;
1142
1143 if (MIPSInst_RT(ir) == 0)
1144 value = 0;
1145 else
1146 value = xcp->regs[MIPSInst_RT(ir)];
1147
1148 /* we only have one writable control reg
1149 */
1150 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1151#ifdef CSRTRACE
1152 printk("%p gpr[%d]->csr=%08x\n",
Ralf Baechle333d1f62005-02-28 17:55:57 +00001153 (void *) (xcp->cp0_epc),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 MIPSInst_RT(ir), value);
1155#endif
Shane McDonald95e8f632010-05-06 23:26:57 -06001156
1157 /*
1158 * Don't write reserved bits,
1159 * and convert to ieee library modes
1160 */
1161 ctx->fcr31 = (value &
1162 ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
1163 ieee_rm[modeindex(value)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1166 return SIGFPE;
1167 }
1168 break;
1169 }
1170
1171 case bc_op:{
1172 int likely = 0;
1173
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001174 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return SIGILL;
1176
1177#if __mips >= 4
1178 cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
1179#else
1180 cond = ctx->fcr31 & FPU_CSR_COND;
1181#endif
1182 switch (MIPSInst_RT(ir) & 3) {
1183 case bcfl_op:
1184 likely = 1;
1185 case bcf_op:
1186 cond = !cond;
1187 break;
1188 case bctl_op:
1189 likely = 1;
1190 case bct_op:
1191 break;
1192 default:
1193 /* thats an illegal instruction */
1194 return SIGILL;
1195 }
1196
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001197 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (cond) {
1199 /* branch taken: emulate dslot
1200 * instruction
1201 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001202 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001204 contpc = MIPSInst_SIMM(ir);
1205 ir = dec_insn.next_insn;
1206 if (dec_insn.micro_mips_mode) {
1207 contpc = (xcp->cp0_epc + (contpc << 1));
1208
1209 /* If 16-bit instruction, not FPU. */
1210 if ((dec_insn.next_pc_inc == 2) ||
1211 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1212
1213 /*
1214 * Since this instruction will
1215 * be put on the stack with
1216 * 32-bit words, get around
1217 * this problem by putting a
1218 * NOP16 as the second one.
1219 */
1220 if (dec_insn.next_pc_inc == 2)
1221 ir = (ir & (~0xffff)) | MM_NOP16;
1222
1223 /*
1224 * Single step the non-CP1
1225 * instruction in the dslot.
1226 */
1227 return mips_dsemul(xcp, ir, contpc);
1228 }
1229 } else
1230 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 switch (MIPSInst_OPCODE(ir)) {
1233 case lwc1_op:
1234 case swc1_op:
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001235#if (__mips >= 2 || defined(__mips64))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 case ldc1_op:
1237 case sdc1_op:
1238#endif
1239 case cop1_op:
1240#if __mips >= 4 && __mips != 32
1241 case cop1x_op:
1242#endif
1243 /* its one of ours */
1244 goto emul;
1245#if __mips >= 4
1246 case spec_op:
1247 if (MIPSInst_FUNC(ir) == movc_op)
1248 goto emul;
1249 break;
1250#endif
1251 }
1252
1253 /*
1254 * Single step the non-cp1
1255 * instruction in the dslot
1256 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001257 return mips_dsemul(xcp, ir, contpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259 else {
1260 /* branch not taken */
1261 if (likely) {
1262 /*
1263 * branch likely nullifies
1264 * dslot if not taken
1265 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001266 xcp->cp0_epc += dec_insn.pc_inc;
1267 contpc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /*
1269 * else continue & execute
1270 * dslot as normal insn
1271 */
1272 }
1273 }
1274 break;
1275 }
1276
1277 default:
1278 if (!(MIPSInst_RS(ir) & 0x10))
1279 return SIGILL;
1280 {
1281 int sig;
1282
1283 /* a real fpu computation instruction */
1284 if ((sig = fpu_emu(xcp, ctx, ir)))
1285 return sig;
1286 }
1287 }
1288 break;
1289
1290#if __mips >= 4 && __mips != 32
1291 case cop1x_op:{
David Daney515b0292010-10-21 16:32:26 -07001292 int sig = fpux_emu(xcp, ctx, ir, fault_addr);
1293 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return sig;
1295 break;
1296 }
1297#endif
1298
1299#if __mips >= 4
1300 case spec_op:
1301 if (MIPSInst_FUNC(ir) != movc_op)
1302 return SIGILL;
1303 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1304 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1305 xcp->regs[MIPSInst_RD(ir)] =
1306 xcp->regs[MIPSInst_RS(ir)];
1307 break;
1308#endif
1309
1310 default:
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001311sigill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return SIGILL;
1313 }
1314
1315 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001316 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001317 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return 0;
1320}
1321
1322/*
1323 * Conversion table from MIPS compare ops 48-63
1324 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1325 */
1326static const unsigned char cmptab[8] = {
1327 0, /* cmp_0 (sig) cmp_sf */
1328 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1329 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1330 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1331 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1332 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1333 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1334 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1335};
1336
1337
1338#if __mips >= 4 && __mips != 32
1339
1340/*
1341 * Additional MIPS4 instructions
1342 */
1343
1344#define DEF3OP(name, p, f1, f2, f3) \
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001345static union ieee754##p fpemu_##p##_##name(union ieee754##p r, union ieee754##p s, \
1346 union ieee754##p t) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{ \
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00001348 struct _ieee754_csr ieee754_csr_save; \
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001349 s = f1(s, t); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 ieee754_csr_save = ieee754_csr; \
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001351 s = f2(s, r); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 ieee754_csr_save.cx |= ieee754_csr.cx; \
1353 ieee754_csr_save.sx |= ieee754_csr.sx; \
Ralf Baechle49a89ef2007-10-11 23:46:15 +01001354 s = f3(s); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 ieee754_csr.cx |= ieee754_csr_save.cx; \
1356 ieee754_csr.sx |= ieee754_csr_save.sx; \
1357 return s; \
1358}
1359
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001360static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 return ieee754dp_div(ieee754dp_one(0), d);
1363}
1364
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001365static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1368}
1369
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001370static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 return ieee754sp_div(ieee754sp_one(0), s);
1373}
1374
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001375static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1378}
1379
Ralf Baechle21a151d2007-10-11 23:46:15 +01001380DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1381DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1383DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001384DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1385DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1387DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1388
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001389static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001390 mips_instruction ir, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 unsigned rcsr = 0; /* resulting csr */
1393
David Daneyb6ee75e2009-11-05 11:34:26 -08001394 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 switch (MIPSInst_FMA_FFMT(ir)) {
1397 case s_fmt:{ /* 0 */
1398
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001399 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1400 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001401 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 u32 val;
1403
1404 switch (MIPSInst_FUNC(ir)) {
1405 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001406 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 xcp->regs[MIPSInst_FT(ir)]);
1408
David Daneyb6ee75e2009-11-05 11:34:26 -08001409 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001410 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001411 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001412 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return SIGBUS;
1414 }
David Daney515b0292010-10-21 16:32:26 -07001415 if (__get_user(val, va)) {
1416 MIPS_FPU_EMU_INC_STATS(errors);
1417 *fault_addr = va;
1418 return SIGSEGV;
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 SITOREG(val, MIPSInst_FD(ir));
1421 break;
1422
1423 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001424 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 xcp->regs[MIPSInst_FT(ir)]);
1426
David Daneyb6ee75e2009-11-05 11:34:26 -08001427 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001430 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1431 MIPS_FPU_EMU_INC_STATS(errors);
1432 *fault_addr = va;
1433 return SIGBUS;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001436 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001437 *fault_addr = va;
1438 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440 break;
1441
1442 case madd_s_op:
1443 handler = fpemu_sp_madd;
1444 goto scoptop;
1445 case msub_s_op:
1446 handler = fpemu_sp_msub;
1447 goto scoptop;
1448 case nmadd_s_op:
1449 handler = fpemu_sp_nmadd;
1450 goto scoptop;
1451 case nmsub_s_op:
1452 handler = fpemu_sp_nmsub;
1453 goto scoptop;
1454
1455 scoptop:
1456 SPFROMREG(fr, MIPSInst_FR(ir));
1457 SPFROMREG(fs, MIPSInst_FS(ir));
1458 SPFROMREG(ft, MIPSInst_FT(ir));
1459 fd = (*handler) (fr, fs, ft);
1460 SPTOREG(fd, MIPSInst_FD(ir));
1461
1462 copcsr:
1463 if (ieee754_cxtest(IEEE754_INEXACT))
1464 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1465 if (ieee754_cxtest(IEEE754_UNDERFLOW))
1466 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1467 if (ieee754_cxtest(IEEE754_OVERFLOW))
1468 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1469 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
1470 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1471
1472 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1474 /*printk ("SIGFPE: fpu csr = %08x\n",
1475 ctx->fcr31); */
1476 return SIGFPE;
1477 }
1478
1479 break;
1480
1481 default:
1482 return SIGILL;
1483 }
1484 break;
1485 }
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001488 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1489 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001490 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 u64 val;
1492
1493 switch (MIPSInst_FUNC(ir)) {
1494 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001495 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 xcp->regs[MIPSInst_FT(ir)]);
1497
David Daneyb6ee75e2009-11-05 11:34:26 -08001498 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001499 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001500 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001501 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return SIGBUS;
1503 }
David Daney515b0292010-10-21 16:32:26 -07001504 if (__get_user(val, va)) {
1505 MIPS_FPU_EMU_INC_STATS(errors);
1506 *fault_addr = va;
1507 return SIGSEGV;
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 DITOREG(val, MIPSInst_FD(ir));
1510 break;
1511
1512 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001513 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 xcp->regs[MIPSInst_FT(ir)]);
1515
David Daneyb6ee75e2009-11-05 11:34:26 -08001516 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001518 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001519 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001520 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return SIGBUS;
1522 }
David Daney515b0292010-10-21 16:32:26 -07001523 if (__put_user(val, va)) {
1524 MIPS_FPU_EMU_INC_STATS(errors);
1525 *fault_addr = va;
1526 return SIGSEGV;
1527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 break;
1529
1530 case madd_d_op:
1531 handler = fpemu_dp_madd;
1532 goto dcoptop;
1533 case msub_d_op:
1534 handler = fpemu_dp_msub;
1535 goto dcoptop;
1536 case nmadd_d_op:
1537 handler = fpemu_dp_nmadd;
1538 goto dcoptop;
1539 case nmsub_d_op:
1540 handler = fpemu_dp_nmsub;
1541 goto dcoptop;
1542
1543 dcoptop:
1544 DPFROMREG(fr, MIPSInst_FR(ir));
1545 DPFROMREG(fs, MIPSInst_FS(ir));
1546 DPFROMREG(ft, MIPSInst_FT(ir));
1547 fd = (*handler) (fr, fs, ft);
1548 DPTOREG(fd, MIPSInst_FD(ir));
1549 goto copcsr;
1550
1551 default:
1552 return SIGILL;
1553 }
1554 break;
1555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001557 case 0x3:
1558 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* ignore prefx operation */
1562 break;
1563
1564 default:
1565 return SIGILL;
1566 }
1567
1568 return 0;
1569}
1570#endif
1571
1572
1573
1574/*
1575 * Emulate a single COP1 arithmetic instruction.
1576 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001577static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 mips_instruction ir)
1579{
1580 int rfmt; /* resulting format */
1581 unsigned rcsr = 0; /* resulting csr */
1582 unsigned cond;
1583 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001584 union ieee754dp d;
1585 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 int w;
Yoichi Yuasa766160c2005-09-03 15:56:22 -07001587#ifdef __mips64
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 s64 l;
1589#endif
1590 } rv; /* resulting value */
1591
David Daneyb6ee75e2009-11-05 11:34:26 -08001592 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
1594 case s_fmt:{ /* 0 */
1595 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001596 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1597 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 } handler;
1599
1600 switch (MIPSInst_FUNC(ir)) {
1601 /* binary ops */
1602 case fadd_op:
1603 handler.b = ieee754sp_add;
1604 goto scopbop;
1605 case fsub_op:
1606 handler.b = ieee754sp_sub;
1607 goto scopbop;
1608 case fmul_op:
1609 handler.b = ieee754sp_mul;
1610 goto scopbop;
1611 case fdiv_op:
1612 handler.b = ieee754sp_div;
1613 goto scopbop;
1614
1615 /* unary ops */
Ralf Baechle587cb982005-09-15 08:52:34 +00001616#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 case fsqrt_op:
1618 handler.u = ieee754sp_sqrt;
1619 goto scopuop;
1620#endif
1621#if __mips >= 4 && __mips != 32
1622 case frsqrt_op:
1623 handler.u = fpemu_sp_rsqrt;
1624 goto scopuop;
1625 case frecip_op:
1626 handler.u = fpemu_sp_recip;
1627 goto scopuop;
1628#endif
1629#if __mips >= 4
1630 case fmovc_op:
1631 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1632 if (((ctx->fcr31 & cond) != 0) !=
1633 ((MIPSInst_FT(ir) & 1) != 0))
1634 return 0;
1635 SPFROMREG(rv.s, MIPSInst_FS(ir));
1636 break;
1637 case fmovz_op:
1638 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1639 return 0;
1640 SPFROMREG(rv.s, MIPSInst_FS(ir));
1641 break;
1642 case fmovn_op:
1643 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1644 return 0;
1645 SPFROMREG(rv.s, MIPSInst_FS(ir));
1646 break;
1647#endif
1648 case fabs_op:
1649 handler.u = ieee754sp_abs;
1650 goto scopuop;
1651 case fneg_op:
1652 handler.u = ieee754sp_neg;
1653 goto scopuop;
1654 case fmov_op:
1655 /* an easy one */
1656 SPFROMREG(rv.s, MIPSInst_FS(ir));
1657 goto copcsr;
1658
1659 /* binary op on handler */
1660 scopbop:
1661 {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001662 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 SPFROMREG(fs, MIPSInst_FS(ir));
1665 SPFROMREG(ft, MIPSInst_FT(ir));
1666
1667 rv.s = (*handler.b) (fs, ft);
1668 goto copcsr;
1669 }
1670 scopuop:
1671 {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001672 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 SPFROMREG(fs, MIPSInst_FS(ir));
1675 rv.s = (*handler.u) (fs);
1676 goto copcsr;
1677 }
1678 copcsr:
1679 if (ieee754_cxtest(IEEE754_INEXACT))
1680 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1681 if (ieee754_cxtest(IEEE754_UNDERFLOW))
1682 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1683 if (ieee754_cxtest(IEEE754_OVERFLOW))
1684 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1685 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
1686 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1687 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
1688 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1689 break;
1690
1691 /* unary conv ops */
1692 case fcvts_op:
1693 return SIGILL; /* not defined */
1694 case fcvtd_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001695 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 SPFROMREG(fs, MIPSInst_FS(ir));
1698 rv.d = ieee754dp_fsp(fs);
1699 rfmt = d_fmt;
1700 goto copcsr;
1701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 case fcvtw_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001703 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 SPFROMREG(fs, MIPSInst_FS(ir));
1706 rv.w = ieee754sp_tint(fs);
1707 rfmt = w_fmt;
1708 goto copcsr;
1709 }
1710
Ralf Baechle587cb982005-09-15 08:52:34 +00001711#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 case fround_op:
1713 case ftrunc_op:
1714 case fceil_op:
1715 case ffloor_op:{
1716 unsigned int oldrm = ieee754_csr.rm;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001717 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 SPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001720 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 rv.w = ieee754sp_tint(fs);
1722 ieee754_csr.rm = oldrm;
1723 rfmt = w_fmt;
1724 goto copcsr;
1725 }
1726#endif /* __mips >= 2 */
1727
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001728#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 case fcvtl_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001730 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 SPFROMREG(fs, MIPSInst_FS(ir));
1733 rv.l = ieee754sp_tlong(fs);
1734 rfmt = l_fmt;
1735 goto copcsr;
1736 }
1737
1738 case froundl_op:
1739 case ftruncl_op:
1740 case fceill_op:
1741 case ffloorl_op:{
1742 unsigned int oldrm = ieee754_csr.rm;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001743 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 SPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001746 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 rv.l = ieee754sp_tlong(fs);
1748 ieee754_csr.rm = oldrm;
1749 rfmt = l_fmt;
1750 goto copcsr;
1751 }
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001752#endif /* defined(__mips64) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 default:
1755 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1756 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001757 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 SPFROMREG(fs, MIPSInst_FS(ir));
1760 SPFROMREG(ft, MIPSInst_FT(ir));
1761 rv.w = ieee754sp_cmp(fs, ft,
1762 cmptab[cmpop & 0x7], cmpop & 0x8);
1763 rfmt = -1;
1764 if ((cmpop & 0x8) && ieee754_cxtest
1765 (IEEE754_INVALID_OPERATION))
1766 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1767 else
1768 goto copcsr;
1769
1770 }
1771 else {
1772 return SIGILL;
1773 }
1774 break;
1775 }
1776 break;
1777 }
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 case d_fmt:{
1780 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001781 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1782 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 } handler;
1784
1785 switch (MIPSInst_FUNC(ir)) {
1786 /* binary ops */
1787 case fadd_op:
1788 handler.b = ieee754dp_add;
1789 goto dcopbop;
1790 case fsub_op:
1791 handler.b = ieee754dp_sub;
1792 goto dcopbop;
1793 case fmul_op:
1794 handler.b = ieee754dp_mul;
1795 goto dcopbop;
1796 case fdiv_op:
1797 handler.b = ieee754dp_div;
1798 goto dcopbop;
1799
1800 /* unary ops */
Ralf Baechle587cb982005-09-15 08:52:34 +00001801#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 case fsqrt_op:
1803 handler.u = ieee754dp_sqrt;
1804 goto dcopuop;
1805#endif
1806#if __mips >= 4 && __mips != 32
1807 case frsqrt_op:
1808 handler.u = fpemu_dp_rsqrt;
1809 goto dcopuop;
1810 case frecip_op:
1811 handler.u = fpemu_dp_recip;
1812 goto dcopuop;
1813#endif
1814#if __mips >= 4
1815 case fmovc_op:
1816 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1817 if (((ctx->fcr31 & cond) != 0) !=
1818 ((MIPSInst_FT(ir) & 1) != 0))
1819 return 0;
1820 DPFROMREG(rv.d, MIPSInst_FS(ir));
1821 break;
1822 case fmovz_op:
1823 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1824 return 0;
1825 DPFROMREG(rv.d, MIPSInst_FS(ir));
1826 break;
1827 case fmovn_op:
1828 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1829 return 0;
1830 DPFROMREG(rv.d, MIPSInst_FS(ir));
1831 break;
1832#endif
1833 case fabs_op:
1834 handler.u = ieee754dp_abs;
1835 goto dcopuop;
1836
1837 case fneg_op:
1838 handler.u = ieee754dp_neg;
1839 goto dcopuop;
1840
1841 case fmov_op:
1842 /* an easy one */
1843 DPFROMREG(rv.d, MIPSInst_FS(ir));
1844 goto copcsr;
1845
1846 /* binary op on handler */
1847 dcopbop:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001848 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 DPFROMREG(fs, MIPSInst_FS(ir));
1851 DPFROMREG(ft, MIPSInst_FT(ir));
1852
1853 rv.d = (*handler.b) (fs, ft);
1854 goto copcsr;
1855 }
1856 dcopuop:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001857 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 DPFROMREG(fs, MIPSInst_FS(ir));
1860 rv.d = (*handler.u) (fs);
1861 goto copcsr;
1862 }
1863
1864 /* unary conv ops */
1865 case fcvts_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001866 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 DPFROMREG(fs, MIPSInst_FS(ir));
1869 rv.s = ieee754sp_fdp(fs);
1870 rfmt = s_fmt;
1871 goto copcsr;
1872 }
1873 case fcvtd_op:
1874 return SIGILL; /* not defined */
1875
1876 case fcvtw_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001877 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 DPFROMREG(fs, MIPSInst_FS(ir));
1880 rv.w = ieee754dp_tint(fs); /* wrong */
1881 rfmt = w_fmt;
1882 goto copcsr;
1883 }
1884
Ralf Baechle587cb982005-09-15 08:52:34 +00001885#if __mips >= 2 || defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 case fround_op:
1887 case ftrunc_op:
1888 case fceil_op:
1889 case ffloor_op:{
1890 unsigned int oldrm = ieee754_csr.rm;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001891 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 DPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001894 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 rv.w = ieee754dp_tint(fs);
1896 ieee754_csr.rm = oldrm;
1897 rfmt = w_fmt;
1898 goto copcsr;
1899 }
1900#endif
1901
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001902#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 case fcvtl_op:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001904 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 DPFROMREG(fs, MIPSInst_FS(ir));
1907 rv.l = ieee754dp_tlong(fs);
1908 rfmt = l_fmt;
1909 goto copcsr;
1910 }
1911
1912 case froundl_op:
1913 case ftruncl_op:
1914 case fceill_op:
1915 case ffloorl_op:{
1916 unsigned int oldrm = ieee754_csr.rm;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001917 union ieee754dp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 DPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001920 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 rv.l = ieee754dp_tlong(fs);
1922 ieee754_csr.rm = oldrm;
1923 rfmt = l_fmt;
1924 goto copcsr;
1925 }
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001926#endif /* __mips >= 3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 default:
1929 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1930 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001931 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 DPFROMREG(fs, MIPSInst_FS(ir));
1934 DPFROMREG(ft, MIPSInst_FT(ir));
1935 rv.w = ieee754dp_cmp(fs, ft,
1936 cmptab[cmpop & 0x7], cmpop & 0x8);
1937 rfmt = -1;
1938 if ((cmpop & 0x8)
1939 &&
1940 ieee754_cxtest
1941 (IEEE754_INVALID_OPERATION))
1942 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1943 else
1944 goto copcsr;
1945
1946 }
1947 else {
1948 return SIGILL;
1949 }
1950 break;
1951 }
1952 break;
1953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 case w_fmt:{
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001956 union ieee754sp fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 switch (MIPSInst_FUNC(ir)) {
1959 case fcvts_op:
1960 /* convert word to single precision real */
1961 SPFROMREG(fs, MIPSInst_FS(ir));
1962 rv.s = ieee754sp_fint(fs.bits);
1963 rfmt = s_fmt;
1964 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 case fcvtd_op:
1966 /* convert word to double precision real */
1967 SPFROMREG(fs, MIPSInst_FS(ir));
1968 rv.d = ieee754dp_fint(fs.bits);
1969 rfmt = d_fmt;
1970 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 default:
1972 return SIGILL;
1973 }
1974 break;
1975 }
1976
Ralf Baechle4b724ef2005-10-23 15:05:47 +01001977#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 case l_fmt:{
Paul Burtonbbd426f2014-02-13 11:26:41 +00001979 u64 bits;
1980 DIFROMREG(bits, MIPSInst_FS(ir));
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 switch (MIPSInst_FUNC(ir)) {
1983 case fcvts_op:
1984 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001985 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 rfmt = s_fmt;
1987 goto copcsr;
1988 case fcvtd_op:
1989 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001990 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 rfmt = d_fmt;
1992 goto copcsr;
1993 default:
1994 return SIGILL;
1995 }
1996 break;
1997 }
1998#endif
1999
2000 default:
2001 return SIGILL;
2002 }
2003
2004 /*
2005 * Update the fpu CSR register for this operation.
2006 * If an exception is required, generate a tidy SIGFPE exception,
2007 * without updating the result register.
2008 * Note: cause exception bits do not accumulate, they are rewritten
2009 * for each op; only the flag/sticky bits accumulate.
2010 */
2011 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
2012 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
2013 /*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
2014 return SIGFPE;
2015 }
2016
2017 /*
2018 * Now we can safely write the result back to the register file.
2019 */
2020 switch (rfmt) {
2021 case -1:{
2022#if __mips >= 4
2023 cond = fpucondbit[MIPSInst_FD(ir) >> 2];
2024#else
2025 cond = FPU_CSR_COND;
2026#endif
2027 if (rv.w)
2028 ctx->fcr31 |= cond;
2029 else
2030 ctx->fcr31 &= ~cond;
2031 break;
2032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 case d_fmt:
2034 DPTOREG(rv.d, MIPSInst_FD(ir));
2035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 case s_fmt:
2037 SPTOREG(rv.s, MIPSInst_FD(ir));
2038 break;
2039 case w_fmt:
2040 SITOREG(rv.w, MIPSInst_FD(ir));
2041 break;
Ralf Baechle4b724ef2005-10-23 15:05:47 +01002042#if defined(__mips64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 case l_fmt:
2044 DITOREG(rv.l, MIPSInst_FD(ir));
2045 break;
2046#endif
2047 default:
2048 return SIGILL;
2049 }
2050
2051 return 0;
2052}
2053
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002054int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07002055 int has_fpu, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Ralf Baechle333d1f62005-02-28 17:55:57 +00002057 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002058 struct mm_decoded_insn dec_insn;
2059 u16 instr[4];
2060 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 int sig = 0;
2062
2063 oldepc = xcp->cp0_epc;
2064 do {
2065 prevepc = xcp->cp0_epc;
2066
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002067 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2068 /*
2069 * Get next 2 microMIPS instructions and convert them
2070 * into 32-bit instructions.
2071 */
2072 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2073 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2074 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2075 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2076 MIPS_FPU_EMU_INC_STATS(errors);
2077 return SIGBUS;
2078 }
2079 instr_ptr = instr;
2080
2081 /* Get first instruction. */
2082 if (mm_insn_16bit(*instr_ptr)) {
2083 /* Duplicate the half-word. */
2084 dec_insn.insn = (*instr_ptr << 16) |
2085 (*instr_ptr);
2086 /* 16-bit instruction. */
2087 dec_insn.pc_inc = 2;
2088 instr_ptr += 1;
2089 } else {
2090 dec_insn.insn = (*instr_ptr << 16) |
2091 *(instr_ptr+1);
2092 /* 32-bit instruction. */
2093 dec_insn.pc_inc = 4;
2094 instr_ptr += 2;
2095 }
2096 /* Get second instruction. */
2097 if (mm_insn_16bit(*instr_ptr)) {
2098 /* Duplicate the half-word. */
2099 dec_insn.next_insn = (*instr_ptr << 16) |
2100 (*instr_ptr);
2101 /* 16-bit instruction. */
2102 dec_insn.next_pc_inc = 2;
2103 } else {
2104 dec_insn.next_insn = (*instr_ptr << 16) |
2105 *(instr_ptr+1);
2106 /* 32-bit instruction. */
2107 dec_insn.next_pc_inc = 4;
2108 }
2109 dec_insn.micro_mips_mode = 1;
2110 } else {
2111 if ((get_user(dec_insn.insn,
2112 (mips_instruction __user *) xcp->cp0_epc)) ||
2113 (get_user(dec_insn.next_insn,
2114 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2115 MIPS_FPU_EMU_INC_STATS(errors);
2116 return SIGBUS;
2117 }
2118 dec_insn.pc_inc = 4;
2119 dec_insn.next_pc_inc = 4;
2120 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002122
2123 if ((dec_insn.insn == 0) ||
2124 ((dec_insn.pc_inc == 2) &&
2125 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2126 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002128 /*
2129 * The 'ieee754_csr' is an alias of
Ralf Baechle70342282013-01-22 12:59:30 +01002130 * ctx->fcr31. No need to copy ctx->fcr31 to
2131 * ieee754_csr. But ieee754_csr.rm is ieee
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002132 * library modes. (not mips rounding mode)
2133 */
2134 /* convert to ieee library modes */
2135 ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002136 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002137 /* revert to mips rounding mode */
2138 ieee754_csr.rm = mips_rm[ieee754_csr.rm];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002141 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 break;
2143 if (sig)
2144 break;
2145
2146 cond_resched();
2147 } while (xcp->cp0_epc > prevepc);
2148
2149 /* SIGILL indicates a non-fpu instruction */
2150 if (sig == SIGILL && xcp->cp0_epc != oldepc)
2151 /* but if epc has advanced, then ignore it */
2152 sig = 0;
2153
2154 return sig;
2155}