blob: 6e7920b208229e16a38cf8f11213802a1f164321 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002 * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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.,
Ralf Baechle3f7cac42014-04-26 01:49:14 +020021 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
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
Ralf Baechle3f7cac42014-04-26 01:49:14 +020028 * the hardware FPU at the boundaries of the IEEE-754 representation
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * (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 *
Ralf Baechle3f7cac42014-04-26 01:49:14 +020033 * Note if you know that you won't have an FPU, then you'll get much
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * 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 Baechle08a07902014-04-19 13:11:37 +020038#include <linux/kconfig.h>
Ralf Baechle85c51c52014-04-16 02:46:11 +020039#include <linux/percpu-defs.h>
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +080040#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ralf Baechlecd8ee342014-04-16 02:09:53 +020042#include <asm/branch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/inst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/ptrace.h>
45#include <asm/signal.h>
Ralf Baechlecd8ee342014-04-16 02:09:53 +020046#include <asm/uaccess.h>
47
48#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/fpu_emulator.h>
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050050#include <asm/fpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include "ieee754.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Function which emulates a floating point instruction. */
55
Atsushi Nemotoeae89072006-05-16 01:26:03 +090056static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 mips_instruction);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int fpux_emu(struct pt_regs *,
David Daney515b0292010-10-21 16:32:26 -070060 struct mips_fpu_struct *, mips_instruction, void *__user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* Control registers */
63
64#define FPCREG_RID 0 /* $0 = revision id */
65#define FPCREG_CSR 31 /* $31 = csr */
66
Shane McDonald95e8f632010-05-06 23:26:57 -060067/* Determine rounding mode from the RM bits of the FCSR */
68#define modeindex(v) ((v) & FPU_CSR_RM)
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* convert condition code register number to csr bit */
71static const unsigned int fpucondbit[8] = {
72 FPU_CSR_COND0,
73 FPU_CSR_COND1,
74 FPU_CSR_COND2,
75 FPU_CSR_COND3,
76 FPU_CSR_COND4,
77 FPU_CSR_COND5,
78 FPU_CSR_COND6,
79 FPU_CSR_COND7
80};
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050082/* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
83static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
84static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
85static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
86static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
87
88/*
89 * This functions translates a 32-bit microMIPS instruction
90 * into a 32-bit MIPS32 instruction. Returns 0 on success
91 * and SIGILL otherwise.
92 */
93static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
94{
95 union mips_instruction insn = *insn_ptr;
96 union mips_instruction mips32_insn = insn;
97 int func, fmt, op;
98
99 switch (insn.mm_i_format.opcode) {
100 case mm_ldc132_op:
101 mips32_insn.mm_i_format.opcode = ldc1_op;
102 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
103 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
104 break;
105 case mm_lwc132_op:
106 mips32_insn.mm_i_format.opcode = lwc1_op;
107 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
108 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
109 break;
110 case mm_sdc132_op:
111 mips32_insn.mm_i_format.opcode = sdc1_op;
112 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
113 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
114 break;
115 case mm_swc132_op:
116 mips32_insn.mm_i_format.opcode = swc1_op;
117 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
118 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
119 break;
120 case mm_pool32i_op:
121 /* NOTE: offset is << by 1 if in microMIPS mode. */
122 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
123 (insn.mm_i_format.rt == mm_bc1t_op)) {
124 mips32_insn.fb_format.opcode = cop1_op;
125 mips32_insn.fb_format.bc = bc_op;
126 mips32_insn.fb_format.flag =
127 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
128 } else
129 return SIGILL;
130 break;
131 case mm_pool32f_op:
132 switch (insn.mm_fp0_format.func) {
133 case mm_32f_01_op:
134 case mm_32f_11_op:
135 case mm_32f_02_op:
136 case mm_32f_12_op:
137 case mm_32f_41_op:
138 case mm_32f_51_op:
139 case mm_32f_42_op:
140 case mm_32f_52_op:
141 op = insn.mm_fp0_format.func;
142 if (op == mm_32f_01_op)
143 func = madd_s_op;
144 else if (op == mm_32f_11_op)
145 func = madd_d_op;
146 else if (op == mm_32f_02_op)
147 func = nmadd_s_op;
148 else if (op == mm_32f_12_op)
149 func = nmadd_d_op;
150 else if (op == mm_32f_41_op)
151 func = msub_s_op;
152 else if (op == mm_32f_51_op)
153 func = msub_d_op;
154 else if (op == mm_32f_42_op)
155 func = nmsub_s_op;
156 else
157 func = nmsub_d_op;
158 mips32_insn.fp6_format.opcode = cop1x_op;
159 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
160 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
161 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
162 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
163 mips32_insn.fp6_format.func = func;
164 break;
165 case mm_32f_10_op:
166 func = -1; /* Invalid */
167 op = insn.mm_fp5_format.op & 0x7;
168 if (op == mm_ldxc1_op)
169 func = ldxc1_op;
170 else if (op == mm_sdxc1_op)
171 func = sdxc1_op;
172 else if (op == mm_lwxc1_op)
173 func = lwxc1_op;
174 else if (op == mm_swxc1_op)
175 func = swxc1_op;
176
177 if (func != -1) {
178 mips32_insn.r_format.opcode = cop1x_op;
179 mips32_insn.r_format.rs =
180 insn.mm_fp5_format.base;
181 mips32_insn.r_format.rt =
182 insn.mm_fp5_format.index;
183 mips32_insn.r_format.rd = 0;
184 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
185 mips32_insn.r_format.func = func;
186 } else
187 return SIGILL;
188 break;
189 case mm_32f_40_op:
190 op = -1; /* Invalid */
191 if (insn.mm_fp2_format.op == mm_fmovt_op)
192 op = 1;
193 else if (insn.mm_fp2_format.op == mm_fmovf_op)
194 op = 0;
195 if (op != -1) {
196 mips32_insn.fp0_format.opcode = cop1_op;
197 mips32_insn.fp0_format.fmt =
198 sdps_format[insn.mm_fp2_format.fmt];
199 mips32_insn.fp0_format.ft =
200 (insn.mm_fp2_format.cc<<2) + op;
201 mips32_insn.fp0_format.fs =
202 insn.mm_fp2_format.fs;
203 mips32_insn.fp0_format.fd =
204 insn.mm_fp2_format.fd;
205 mips32_insn.fp0_format.func = fmovc_op;
206 } else
207 return SIGILL;
208 break;
209 case mm_32f_60_op:
210 func = -1; /* Invalid */
211 if (insn.mm_fp0_format.op == mm_fadd_op)
212 func = fadd_op;
213 else if (insn.mm_fp0_format.op == mm_fsub_op)
214 func = fsub_op;
215 else if (insn.mm_fp0_format.op == mm_fmul_op)
216 func = fmul_op;
217 else if (insn.mm_fp0_format.op == mm_fdiv_op)
218 func = fdiv_op;
219 if (func != -1) {
220 mips32_insn.fp0_format.opcode = cop1_op;
221 mips32_insn.fp0_format.fmt =
222 sdps_format[insn.mm_fp0_format.fmt];
223 mips32_insn.fp0_format.ft =
224 insn.mm_fp0_format.ft;
225 mips32_insn.fp0_format.fs =
226 insn.mm_fp0_format.fs;
227 mips32_insn.fp0_format.fd =
228 insn.mm_fp0_format.fd;
229 mips32_insn.fp0_format.func = func;
230 } else
231 return SIGILL;
232 break;
233 case mm_32f_70_op:
234 func = -1; /* Invalid */
235 if (insn.mm_fp0_format.op == mm_fmovn_op)
236 func = fmovn_op;
237 else if (insn.mm_fp0_format.op == mm_fmovz_op)
238 func = fmovz_op;
239 if (func != -1) {
240 mips32_insn.fp0_format.opcode = cop1_op;
241 mips32_insn.fp0_format.fmt =
242 sdps_format[insn.mm_fp0_format.fmt];
243 mips32_insn.fp0_format.ft =
244 insn.mm_fp0_format.ft;
245 mips32_insn.fp0_format.fs =
246 insn.mm_fp0_format.fs;
247 mips32_insn.fp0_format.fd =
248 insn.mm_fp0_format.fd;
249 mips32_insn.fp0_format.func = func;
250 } else
251 return SIGILL;
252 break;
253 case mm_32f_73_op: /* POOL32FXF */
254 switch (insn.mm_fp1_format.op) {
255 case mm_movf0_op:
256 case mm_movf1_op:
257 case mm_movt0_op:
258 case mm_movt1_op:
259 if ((insn.mm_fp1_format.op & 0x7f) ==
260 mm_movf0_op)
261 op = 0;
262 else
263 op = 1;
264 mips32_insn.r_format.opcode = spec_op;
265 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
266 mips32_insn.r_format.rt =
267 (insn.mm_fp4_format.cc << 2) + op;
268 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
269 mips32_insn.r_format.re = 0;
270 mips32_insn.r_format.func = movc_op;
271 break;
272 case mm_fcvtd0_op:
273 case mm_fcvtd1_op:
274 case mm_fcvts0_op:
275 case mm_fcvts1_op:
276 if ((insn.mm_fp1_format.op & 0x7f) ==
277 mm_fcvtd0_op) {
278 func = fcvtd_op;
279 fmt = swl_format[insn.mm_fp3_format.fmt];
280 } else {
281 func = fcvts_op;
282 fmt = dwl_format[insn.mm_fp3_format.fmt];
283 }
284 mips32_insn.fp0_format.opcode = cop1_op;
285 mips32_insn.fp0_format.fmt = fmt;
286 mips32_insn.fp0_format.ft = 0;
287 mips32_insn.fp0_format.fs =
288 insn.mm_fp3_format.fs;
289 mips32_insn.fp0_format.fd =
290 insn.mm_fp3_format.rt;
291 mips32_insn.fp0_format.func = func;
292 break;
293 case mm_fmov0_op:
294 case mm_fmov1_op:
295 case mm_fabs0_op:
296 case mm_fabs1_op:
297 case mm_fneg0_op:
298 case mm_fneg1_op:
299 if ((insn.mm_fp1_format.op & 0x7f) ==
300 mm_fmov0_op)
301 func = fmov_op;
302 else if ((insn.mm_fp1_format.op & 0x7f) ==
303 mm_fabs0_op)
304 func = fabs_op;
305 else
306 func = fneg_op;
307 mips32_insn.fp0_format.opcode = cop1_op;
308 mips32_insn.fp0_format.fmt =
309 sdps_format[insn.mm_fp3_format.fmt];
310 mips32_insn.fp0_format.ft = 0;
311 mips32_insn.fp0_format.fs =
312 insn.mm_fp3_format.fs;
313 mips32_insn.fp0_format.fd =
314 insn.mm_fp3_format.rt;
315 mips32_insn.fp0_format.func = func;
316 break;
317 case mm_ffloorl_op:
318 case mm_ffloorw_op:
319 case mm_fceill_op:
320 case mm_fceilw_op:
321 case mm_ftruncl_op:
322 case mm_ftruncw_op:
323 case mm_froundl_op:
324 case mm_froundw_op:
325 case mm_fcvtl_op:
326 case mm_fcvtw_op:
327 if (insn.mm_fp1_format.op == mm_ffloorl_op)
328 func = ffloorl_op;
329 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
330 func = ffloor_op;
331 else if (insn.mm_fp1_format.op == mm_fceill_op)
332 func = fceill_op;
333 else if (insn.mm_fp1_format.op == mm_fceilw_op)
334 func = fceil_op;
335 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
336 func = ftruncl_op;
337 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
338 func = ftrunc_op;
339 else if (insn.mm_fp1_format.op == mm_froundl_op)
340 func = froundl_op;
341 else if (insn.mm_fp1_format.op == mm_froundw_op)
342 func = fround_op;
343 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
344 func = fcvtl_op;
345 else
346 func = fcvtw_op;
347 mips32_insn.fp0_format.opcode = cop1_op;
348 mips32_insn.fp0_format.fmt =
349 sd_format[insn.mm_fp1_format.fmt];
350 mips32_insn.fp0_format.ft = 0;
351 mips32_insn.fp0_format.fs =
352 insn.mm_fp1_format.fs;
353 mips32_insn.fp0_format.fd =
354 insn.mm_fp1_format.rt;
355 mips32_insn.fp0_format.func = func;
356 break;
357 case mm_frsqrt_op:
358 case mm_fsqrt_op:
359 case mm_frecip_op:
360 if (insn.mm_fp1_format.op == mm_frsqrt_op)
361 func = frsqrt_op;
362 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
363 func = fsqrt_op;
364 else
365 func = frecip_op;
366 mips32_insn.fp0_format.opcode = cop1_op;
367 mips32_insn.fp0_format.fmt =
368 sdps_format[insn.mm_fp1_format.fmt];
369 mips32_insn.fp0_format.ft = 0;
370 mips32_insn.fp0_format.fs =
371 insn.mm_fp1_format.fs;
372 mips32_insn.fp0_format.fd =
373 insn.mm_fp1_format.rt;
374 mips32_insn.fp0_format.func = func;
375 break;
376 case mm_mfc1_op:
377 case mm_mtc1_op:
378 case mm_cfc1_op:
379 case mm_ctc1_op:
Steven J. Hill9355e592013-11-07 12:48:29 +0000380 case mm_mfhc1_op:
381 case mm_mthc1_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500382 if (insn.mm_fp1_format.op == mm_mfc1_op)
383 op = mfc_op;
384 else if (insn.mm_fp1_format.op == mm_mtc1_op)
385 op = mtc_op;
386 else if (insn.mm_fp1_format.op == mm_cfc1_op)
387 op = cfc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000388 else if (insn.mm_fp1_format.op == mm_ctc1_op)
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500389 op = ctc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000390 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
391 op = mfhc_op;
392 else
393 op = mthc_op;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500394 mips32_insn.fp1_format.opcode = cop1_op;
395 mips32_insn.fp1_format.op = op;
396 mips32_insn.fp1_format.rt =
397 insn.mm_fp1_format.rt;
398 mips32_insn.fp1_format.fs =
399 insn.mm_fp1_format.fs;
400 mips32_insn.fp1_format.fd = 0;
401 mips32_insn.fp1_format.func = 0;
402 break;
403 default:
404 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500405 }
406 break;
407 case mm_32f_74_op: /* c.cond.fmt */
408 mips32_insn.fp0_format.opcode = cop1_op;
409 mips32_insn.fp0_format.fmt =
410 sdps_format[insn.mm_fp4_format.fmt];
411 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
412 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
413 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
414 mips32_insn.fp0_format.func =
415 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
416 break;
417 default:
418 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500419 }
420 break;
421 default:
422 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500423 }
424
425 *insn_ptr = mips32_insn;
426 return 0;
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/*
430 * Redundant with logic already in kernel/branch.c,
431 * embedded in compute_return_epc. At some point,
432 * a single subroutine should be used across both
433 * modules.
434 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500435static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
436 unsigned long *contpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500438 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
439 unsigned int fcr31;
440 unsigned int bit = 0;
441
442 switch (insn.i_format.opcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 case spec_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500444 switch (insn.r_format.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 case jalr_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500446 regs->regs[insn.r_format.rd] =
447 regs->cp0_epc + dec_insn.pc_inc +
448 dec_insn.next_pc_inc;
449 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 case jr_op:
Markos Chandras5f9f41c2014-11-25 15:54:14 +0000451 /* For R6, JR already emulated in jalr_op */
452 if (NO_R6EMU && insn.r_format.opcode == jr_op)
453 break;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500454 *contpc = regs->regs[insn.r_format.rs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 1;
456 }
457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 case bcond_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500459 switch (insn.i_format.rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 case bltzal_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 case bltzall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000462 if (NO_R6EMU && (insn.i_format.rs ||
463 insn.i_format.rt == bltzall_op))
464 break;
465
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500466 regs->regs[31] = regs->cp0_epc +
467 dec_insn.pc_inc +
468 dec_insn.next_pc_inc;
469 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500470 case bltzl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000471 if (NO_R6EMU)
472 break;
473 case bltz_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500474 if ((long)regs->regs[insn.i_format.rs] < 0)
475 *contpc = regs->cp0_epc +
476 dec_insn.pc_inc +
477 (insn.i_format.simmediate << 2);
478 else
479 *contpc = regs->cp0_epc +
480 dec_insn.pc_inc +
481 dec_insn.next_pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500483 case bgezal_op:
484 case bgezall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000485 if (NO_R6EMU && (insn.i_format.rs ||
486 insn.i_format.rt == bgezall_op))
487 break;
488
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500489 regs->regs[31] = regs->cp0_epc +
490 dec_insn.pc_inc +
491 dec_insn.next_pc_inc;
492 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500493 case bgezl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000494 if (NO_R6EMU)
495 break;
496 case bgez_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500497 if ((long)regs->regs[insn.i_format.rs] >= 0)
498 *contpc = regs->cp0_epc +
499 dec_insn.pc_inc +
500 (insn.i_format.simmediate << 2);
501 else
502 *contpc = regs->cp0_epc +
503 dec_insn.pc_inc +
504 dec_insn.next_pc_inc;
505 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 case jalx_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500509 set_isa16_mode(bit);
510 case jal_op:
511 regs->regs[31] = regs->cp0_epc +
512 dec_insn.pc_inc +
513 dec_insn.next_pc_inc;
514 /* Fall through */
515 case j_op:
516 *contpc = regs->cp0_epc + dec_insn.pc_inc;
517 *contpc >>= 28;
518 *contpc <<= 28;
519 *contpc |= (insn.j_format.target << 2);
520 /* Set microMIPS mode bit: XOR for jalx. */
521 *contpc ^= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500523 case beql_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000524 if (NO_R6EMU)
525 break;
526 case beq_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500527 if (regs->regs[insn.i_format.rs] ==
528 regs->regs[insn.i_format.rt])
529 *contpc = regs->cp0_epc +
530 dec_insn.pc_inc +
531 (insn.i_format.simmediate << 2);
532 else
533 *contpc = regs->cp0_epc +
534 dec_insn.pc_inc +
535 dec_insn.next_pc_inc;
536 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500537 case bnel_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000538 if (NO_R6EMU)
539 break;
540 case bne_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500541 if (regs->regs[insn.i_format.rs] !=
542 regs->regs[insn.i_format.rt])
543 *contpc = regs->cp0_epc +
544 dec_insn.pc_inc +
545 (insn.i_format.simmediate << 2);
546 else
547 *contpc = regs->cp0_epc +
548 dec_insn.pc_inc +
549 dec_insn.next_pc_inc;
550 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500551 case blezl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000552 if (NO_R6EMU)
553 break;
554 case blez_op:
Markos Chandrasa8ff66f2014-11-26 12:57:54 +0000555
556 /*
557 * Compact branches for R6 for the
558 * blez and blezl opcodes.
559 * BLEZ | rs = 0 | rt != 0 == BLEZALC
560 * BLEZ | rs = rt != 0 == BGEZALC
561 * BLEZ | rs != 0 | rt != 0 == BGEUC
562 * BLEZL | rs = 0 | rt != 0 == BLEZC
563 * BLEZL | rs = rt != 0 == BGEZC
564 * BLEZL | rs != 0 | rt != 0 == BGEC
565 *
566 * For real BLEZ{,L}, rt is always 0.
567 */
568 if (cpu_has_mips_r6 && insn.i_format.rt) {
569 if ((insn.i_format.opcode == blez_op) &&
570 ((!insn.i_format.rs && insn.i_format.rt) ||
571 (insn.i_format.rs == insn.i_format.rt)))
572 regs->regs[31] = regs->cp0_epc +
573 dec_insn.pc_inc;
574 *contpc = regs->cp0_epc + dec_insn.pc_inc +
575 dec_insn.next_pc_inc;
576
577 return 1;
578 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500579 if ((long)regs->regs[insn.i_format.rs] <= 0)
580 *contpc = regs->cp0_epc +
581 dec_insn.pc_inc +
582 (insn.i_format.simmediate << 2);
583 else
584 *contpc = regs->cp0_epc +
585 dec_insn.pc_inc +
586 dec_insn.next_pc_inc;
587 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500588 case bgtzl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000589 if (NO_R6EMU)
590 break;
591 case bgtz_op:
Markos Chandrasf1b44062014-11-26 13:05:09 +0000592 /*
593 * Compact branches for R6 for the
594 * bgtz and bgtzl opcodes.
595 * BGTZ | rs = 0 | rt != 0 == BGTZALC
596 * BGTZ | rs = rt != 0 == BLTZALC
597 * BGTZ | rs != 0 | rt != 0 == BLTUC
598 * BGTZL | rs = 0 | rt != 0 == BGTZC
599 * BGTZL | rs = rt != 0 == BLTZC
600 * BGTZL | rs != 0 | rt != 0 == BLTC
601 *
602 * *ZALC varint for BGTZ &&& rt != 0
603 * For real GTZ{,L}, rt is always 0.
604 */
605 if (cpu_has_mips_r6 && insn.i_format.rt) {
606 if ((insn.i_format.opcode == blez_op) &&
607 ((!insn.i_format.rs && insn.i_format.rt) ||
608 (insn.i_format.rs == insn.i_format.rt)))
609 regs->regs[31] = regs->cp0_epc +
610 dec_insn.pc_inc;
611 *contpc = regs->cp0_epc + dec_insn.pc_inc +
612 dec_insn.next_pc_inc;
613
614 return 1;
615 }
616
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500617 if ((long)regs->regs[insn.i_format.rs] > 0)
618 *contpc = regs->cp0_epc +
619 dec_insn.pc_inc +
620 (insn.i_format.simmediate << 2);
621 else
622 *contpc = regs->cp0_epc +
623 dec_insn.pc_inc +
624 dec_insn.next_pc_inc;
625 return 1;
Markos Chandrasc893ce32014-11-26 14:08:52 +0000626 case cbcond0_op:
Markos Chandras10d962d2014-11-26 15:03:54 +0000627 case cbcond1_op:
Markos Chandrasc893ce32014-11-26 14:08:52 +0000628 if (!cpu_has_mips_r6)
629 break;
630 if (insn.i_format.rt && !insn.i_format.rs)
631 regs->regs[31] = regs->cp0_epc + 4;
632 *contpc = regs->cp0_epc + dec_insn.pc_inc +
633 dec_insn.next_pc_inc;
634
635 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700636#ifdef CONFIG_CPU_CAVIUM_OCTEON
637 case lwc2_op: /* This is bbit0 on Octeon */
638 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
639 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
640 else
641 *contpc = regs->cp0_epc + 8;
642 return 1;
643 case ldc2_op: /* This is bbit032 on Octeon */
644 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
645 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
646 else
647 *contpc = regs->cp0_epc + 8;
648 return 1;
649 case swc2_op: /* This is bbit1 on Octeon */
650 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
651 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
652 else
653 *contpc = regs->cp0_epc + 8;
654 return 1;
655 case sdc2_op: /* This is bbit132 on Octeon */
656 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
657 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
658 else
659 *contpc = regs->cp0_epc + 8;
660 return 1;
Markos Chandras8467ca02014-11-26 13:56:51 +0000661#else
662 case bc6_op:
663 /*
664 * Only valid for MIPS R6 but we can still end up
665 * here from a broken userland so just tell emulator
666 * this is not a branch and let it break later on.
667 */
668 if (!cpu_has_mips_r6)
669 break;
670 *contpc = regs->cp0_epc + dec_insn.pc_inc +
671 dec_insn.next_pc_inc;
672
673 return 1;
Markos Chandras84fef632014-11-26 15:43:11 +0000674 case balc6_op:
675 if (!cpu_has_mips_r6)
676 break;
677 regs->regs[31] = regs->cp0_epc + 4;
678 *contpc = regs->cp0_epc + dec_insn.pc_inc +
679 dec_insn.next_pc_inc;
680
681 return 1;
Markos Chandras69b9a2f2014-11-27 09:32:25 +0000682 case beqzcjic_op:
683 if (!cpu_has_mips_r6)
684 break;
685 *contpc = regs->cp0_epc + dec_insn.pc_inc +
686 dec_insn.next_pc_inc;
687
688 return 1;
Markos Chandras28d6f932015-01-08 11:55:20 +0000689 case bnezcjialc_op:
690 if (!cpu_has_mips_r6)
691 break;
692 if (!insn.i_format.rs)
693 regs->regs[31] = regs->cp0_epc + 4;
694 *contpc = regs->cp0_epc + dec_insn.pc_inc +
695 dec_insn.next_pc_inc;
696
697 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700698#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 case cop0_op:
700 case cop1_op:
Markos Chandrasc8a34582014-11-26 10:10:18 +0000701 /* Need to check for R6 bc1nez and bc1eqz branches */
702 if (cpu_has_mips_r6 &&
703 ((insn.i_format.rs == bc1eqz_op) ||
704 (insn.i_format.rs == bc1nez_op))) {
705 bit = 0;
706 switch (insn.i_format.rs) {
707 case bc1eqz_op:
708 if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
709 bit = 1;
710 break;
711 case bc1nez_op:
712 if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
713 bit = 1;
714 break;
715 }
716 if (bit)
717 *contpc = regs->cp0_epc +
718 dec_insn.pc_inc +
719 (insn.i_format.simmediate << 2);
720 else
721 *contpc = regs->cp0_epc +
722 dec_insn.pc_inc +
723 dec_insn.next_pc_inc;
724
725 return 1;
726 }
727 /* R2/R6 compatible cop1 instruction. Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 case cop2_op:
729 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500730 if (insn.i_format.rs == bc_op) {
731 preempt_disable();
732 if (is_fpu_owner())
Manuel Lauss842dfc12014-11-07 14:13:54 +0100733 fcr31 = read_32bit_cp1_register(CP1_STATUS);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500734 else
735 fcr31 = current->thread.fpu.fcr31;
736 preempt_enable();
737
738 bit = (insn.i_format.rt >> 2);
739 bit += (bit != 0);
740 bit += 23;
741 switch (insn.i_format.rt & 3) {
742 case 0: /* bc1f */
743 case 2: /* bc1fl */
744 if (~fcr31 & (1 << bit))
745 *contpc = regs->cp0_epc +
746 dec_insn.pc_inc +
747 (insn.i_format.simmediate << 2);
748 else
749 *contpc = regs->cp0_epc +
750 dec_insn.pc_inc +
751 dec_insn.next_pc_inc;
752 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500753 case 1: /* bc1t */
754 case 3: /* bc1tl */
755 if (fcr31 & (1 << bit))
756 *contpc = regs->cp0_epc +
757 dec_insn.pc_inc +
758 (insn.i_format.simmediate << 2);
759 else
760 *contpc = regs->cp0_epc +
761 dec_insn.pc_inc +
762 dec_insn.next_pc_inc;
763 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500764 }
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
769}
770
771/*
772 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100773 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800774 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000775 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200776 * FPU emu is slow and bulky and optimizing this function offers fairly
777 * sizeable benefits so we try to be clever and make this function return
778 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000779 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
David Daneyda0bac32009-11-02 11:33:46 -0800781static inline int cop1_64bit(struct pt_regs *xcp)
782{
Ralf Baechle08a07902014-04-19 13:11:37 +0200783 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
784 return 1;
785 else if (config_enabled(CONFIG_32BIT) &&
786 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
787 return 0;
788
Paul Burton597ce172013-11-22 13:12:07 +0000789 return !test_thread_flag(TIF_32BIT_FPREGS);
David Daneyda0bac32009-11-02 11:33:46 -0800790}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Paul Burton4227a2d2014-09-11 08:30:20 +0100792static inline bool hybrid_fprs(void)
793{
794 return test_thread_flag(TIF_HYBRID_FPREGS);
795}
796
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200797#define SIFROMREG(si, x) \
798do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100799 if (cop1_64bit(xcp) && !hybrid_fprs()) \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100800 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000801 else \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100802 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000803} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800804
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200805#define SITOREG(si, x) \
806do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100807 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000808 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000809 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000810 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
811 set_fpr32(&ctx->fpr[x], i, 0); \
812 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000813 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000814 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000815} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Paul Burtonc8c0da62014-09-24 10:45:37 +0100817#define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000818
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200819#define SITOHREG(si, x) \
820do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000821 unsigned i; \
822 set_fpr32(&ctx->fpr[x], 1, si); \
823 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
824 set_fpr32(&ctx->fpr[x], i, 0); \
825} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000826
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200827#define DIFROMREG(di, x) \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000828 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
829
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200830#define DITOREG(di, x) \
831do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000832 unsigned fpr, i; \
833 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
834 set_fpr64(&ctx->fpr[fpr], 0, di); \
835 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
836 set_fpr64(&ctx->fpr[fpr], i, 0); \
837} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Ralf Baechle21a151d2007-10-11 23:46:15 +0100839#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
840#define SPTOREG(sp, x) SITOREG((sp).bits, x)
841#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
842#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844/*
845 * Emulate the single floating point instruction pointed at by EPC.
846 * Two instructions if the instruction is in a branch delay slot.
847 */
848
David Daney515b0292010-10-21 16:32:26 -0700849static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500850 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500852 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200853 unsigned int cond, cbit;
854 mips_instruction ir;
855 int likely, pc_inc;
856 u32 __user *wva;
857 u64 __user *dva;
858 u32 value;
859 u32 wval;
860 u64 dval;
861 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Ralf Baechle70e4c232014-04-30 11:09:44 +0200863 /*
864 * These are giving gcc a gentle hint about what to expect in
865 * dec_inst in order to do better optimization.
866 */
867 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
868 unreachable();
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200871 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500872 if (dec_insn.micro_mips_mode) {
873 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200874 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500875 } else {
876 if (!isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200877 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500878 }
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200881 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /*
883 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +0100884 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * BEFORE we do the cop1 instruction.
886 *
887 * This branch could be a COP1 branch, but in that case we
888 * would have had a trap for that instruction, and would not
889 * come through this route.
890 *
891 * Linux MIPS branch emulator operates on context, updating the
892 * cp0_epc.
893 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500894 ir = dec_insn.next_insn; /* process delay slot instr */
895 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000896 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500897 ir = dec_insn.insn; /* process current instr */
898 pc_inc = dec_insn.pc_inc;
899 }
900
901 /*
902 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
903 * instructions, we want to convert microMIPS FPU instructions
904 * into MIPS32 instructions so that we could reuse all of the
905 * FPU emulation code.
906 *
907 * NOTE: We cannot do this for branch instructions since they
908 * are not a subset. Example: Cannot emulate a 16-bit
909 * aligned target address with a MIPS32 instruction.
910 */
911 if (dec_insn.micro_mips_mode) {
912 /*
913 * If next instruction is a 16-bit instruction, then it
914 * it cannot be a FPU instruction. This could happen
915 * since we can be called for non-FPU instructions.
916 */
917 if ((pc_inc == 2) ||
918 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
919 == SIGILL))
920 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200923emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200924 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -0800925 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 switch (MIPSInst_OPCODE(ir)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200927 case ldc1_op:
928 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
929 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800930 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -0700931
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200932 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800933 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200934 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return SIGBUS;
936 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200937 if (__get_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700938 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200939 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700940 return SIGSEGV;
941 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200942 DITOREG(dval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200945 case sdc1_op:
946 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
947 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800948 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200949 DIFROMREG(dval, MIPSInst_RT(ir));
950 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800951 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200952 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return SIGBUS;
954 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200955 if (__put_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700956 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200957 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700958 return SIGSEGV;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200962 case lwc1_op:
963 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
964 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800965 MIPS_FPU_EMU_INC_STATS(loads);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200966 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800967 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200968 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return SIGBUS;
970 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200971 if (__get_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -0700972 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200973 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -0700974 return SIGSEGV;
975 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200976 SITOREG(wval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200979 case swc1_op:
980 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
981 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800982 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200983 SIFROMREG(wval, MIPSInst_RT(ir));
984 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800985 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200986 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return SIGBUS;
988 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200989 if (__put_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -0700990 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200991 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -0700992 return SIGSEGV;
993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 case cop1_op:
997 switch (MIPSInst_RS(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 case dmfc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +0200999 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1000 return SIGILL;
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* copregister fs -> gpr[rt] */
1003 if (MIPSInst_RT(ir) != 0) {
1004 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1005 MIPSInst_RD(ir));
1006 }
1007 break;
1008
1009 case dmtc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001010 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1011 return SIGILL;
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* copregister fs <- rt */
1014 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1015 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001017 case mfhc_op:
1018 if (!cpu_has_mips_r2)
1019 goto sigill;
1020
1021 /* copregister rd -> gpr[rt] */
1022 if (MIPSInst_RT(ir) != 0) {
1023 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1024 MIPSInst_RD(ir));
1025 }
1026 break;
1027
1028 case mthc_op:
1029 if (!cpu_has_mips_r2)
1030 goto sigill;
1031
1032 /* copregister rd <- gpr[rt] */
1033 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1034 break;
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 case mfc_op:
1037 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (MIPSInst_RT(ir) != 0) {
1039 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1040 MIPSInst_RD(ir));
1041 }
1042 break;
1043
1044 case mtc_op:
1045 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1047 break;
1048
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001049 case cfc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* cop control register rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1052 value = ctx->fcr31;
Ralf Baechle56a64732014-04-30 11:21:55 +02001053 value = (value & ~FPU_CSR_RM) | modeindex(value);
Ralf Baechle92df0f82014-04-19 14:03:37 +02001054 pr_debug("%p gpr[%d]<-csr=%08x\n",
1055 (void *) (xcp->cp0_epc),
1056 MIPSInst_RT(ir), value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058 else if (MIPSInst_RD(ir) == FPCREG_RID)
1059 value = 0;
1060 else
1061 value = 0;
1062 if (MIPSInst_RT(ir))
1063 xcp->regs[MIPSInst_RT(ir)] = value;
1064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001066 case ctc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (MIPSInst_RT(ir) == 0)
1069 value = 0;
1070 else
1071 value = xcp->regs[MIPSInst_RT(ir)];
1072
1073 /* we only have one writable control reg
1074 */
1075 if (MIPSInst_RD(ir) == FPCREG_CSR) {
Ralf Baechle92df0f82014-04-19 14:03:37 +02001076 pr_debug("%p gpr[%d]->csr=%08x\n",
1077 (void *) (xcp->cp0_epc),
1078 MIPSInst_RT(ir), value);
Shane McDonald95e8f632010-05-06 23:26:57 -06001079
1080 /*
1081 * Don't write reserved bits,
1082 * and convert to ieee library modes
1083 */
Ralf Baechle56a64732014-04-30 11:21:55 +02001084 ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
1085 modeindex(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1088 return SIGFPE;
1089 }
1090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001092 case bc_op:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001093 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return SIGILL;
1095
Ralf Baechle08a07902014-04-19 13:11:37 +02001096 if (cpu_has_mips_4_5_r)
1097 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1098 else
1099 cbit = FPU_CSR_COND;
1100 cond = ctx->fcr31 & cbit;
1101
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001102 likely = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 switch (MIPSInst_RT(ir) & 3) {
1104 case bcfl_op:
1105 likely = 1;
1106 case bcf_op:
1107 cond = !cond;
1108 break;
1109 case bctl_op:
1110 likely = 1;
1111 case bct_op:
1112 break;
1113 default:
1114 /* thats an illegal instruction */
1115 return SIGILL;
1116 }
1117
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001118 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (cond) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001120 /*
1121 * Branch taken: emulate dslot instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001123 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001125 contpc = MIPSInst_SIMM(ir);
1126 ir = dec_insn.next_insn;
1127 if (dec_insn.micro_mips_mode) {
1128 contpc = (xcp->cp0_epc + (contpc << 1));
1129
1130 /* If 16-bit instruction, not FPU. */
1131 if ((dec_insn.next_pc_inc == 2) ||
1132 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1133
1134 /*
1135 * Since this instruction will
1136 * be put on the stack with
1137 * 32-bit words, get around
1138 * this problem by putting a
1139 * NOP16 as the second one.
1140 */
1141 if (dec_insn.next_pc_inc == 2)
1142 ir = (ir & (~0xffff)) | MM_NOP16;
1143
1144 /*
1145 * Single step the non-CP1
1146 * instruction in the dslot.
1147 */
1148 return mips_dsemul(xcp, ir, contpc);
1149 }
1150 } else
1151 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 switch (MIPSInst_OPCODE(ir)) {
1154 case lwc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001155 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 case swc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001158 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 case ldc1_op:
1161 case sdc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001162 if (cpu_has_mips_2_3_4_5 ||
1163 cpu_has_mips64)
1164 goto emul;
1165
1166 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001168
Ralf Baechle08a07902014-04-19 13:11:37 +02001169 case cop1_op:
1170 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001171
Ralf Baechle08a07902014-04-19 13:11:37 +02001172 case cop1x_op:
Markos Chandrasa5466d72014-10-21 10:21:54 +01001173 if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
Ralf Baechle08a07902014-04-19 13:11:37 +02001174 /* its one of ours */
1175 goto emul;
1176
1177 return SIGILL;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001180 if (!cpu_has_mips_4_5_r)
1181 return SIGILL;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (MIPSInst_FUNC(ir) == movc_op)
1184 goto emul;
1185 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
1188 /*
1189 * Single step the non-cp1
1190 * instruction in the dslot
1191 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001192 return mips_dsemul(xcp, ir, contpc);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001193 } else if (likely) { /* branch not taken */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /*
1195 * branch likely nullifies
1196 * dslot if not taken
1197 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001198 xcp->cp0_epc += dec_insn.pc_inc;
1199 contpc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /*
1201 * else continue & execute
1202 * dslot as normal insn
1203 */
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 default:
1208 if (!(MIPSInst_RS(ir) & 0x10))
1209 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001211 /* a real fpu computation instruction */
1212 if ((sig = fpu_emu(xcp, ctx, ir)))
1213 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215 break;
1216
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001217 case cop1x_op:
Markos Chandrasa5466d72014-10-21 10:21:54 +01001218 if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
Ralf Baechle08a07902014-04-19 13:11:37 +02001219 return SIGILL;
1220
1221 sig = fpux_emu(xcp, ctx, ir, fault_addr);
David Daney515b0292010-10-21 16:32:26 -07001222 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return sig;
1224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001227 if (!cpu_has_mips_4_5_r)
1228 return SIGILL;
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (MIPSInst_FUNC(ir) != movc_op)
1231 return SIGILL;
1232 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1233 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1234 xcp->regs[MIPSInst_RD(ir)] =
1235 xcp->regs[MIPSInst_RS(ir)];
1236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 default:
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001238sigill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return SIGILL;
1240 }
1241
1242 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001243 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001244 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return 0;
1247}
1248
1249/*
1250 * Conversion table from MIPS compare ops 48-63
1251 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1252 */
1253static const unsigned char cmptab[8] = {
1254 0, /* cmp_0 (sig) cmp_sf */
1255 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1256 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1257 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1258 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1259 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1260 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1261 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1262};
1263
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265/*
1266 * Additional MIPS4 instructions
1267 */
1268
Ralf Baechle47fa0c02014-04-16 11:00:12 +02001269#define DEF3OP(name, p, f1, f2, f3) \
1270static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1271 union ieee754##p s, union ieee754##p t) \
1272{ \
1273 struct _ieee754_csr ieee754_csr_save; \
1274 s = f1(s, t); \
1275 ieee754_csr_save = ieee754_csr; \
1276 s = f2(s, r); \
1277 ieee754_csr_save.cx |= ieee754_csr.cx; \
1278 ieee754_csr_save.sx |= ieee754_csr.sx; \
1279 s = f3(s); \
1280 ieee754_csr.cx |= ieee754_csr_save.cx; \
1281 ieee754_csr.sx |= ieee754_csr_save.sx; \
1282 return s; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001285static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 return ieee754dp_div(ieee754dp_one(0), d);
1288}
1289
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001290static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1293}
1294
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001295static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 return ieee754sp_div(ieee754sp_one(0), s);
1298}
1299
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001300static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1303}
1304
Ralf Baechle21a151d2007-10-11 23:46:15 +01001305DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1306DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1308DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001309DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1310DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1312DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1313
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001314static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001315 mips_instruction ir, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
1317 unsigned rcsr = 0; /* resulting csr */
1318
David Daneyb6ee75e2009-11-05 11:34:26 -08001319 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 switch (MIPSInst_FMA_FFMT(ir)) {
1322 case s_fmt:{ /* 0 */
1323
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001324 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1325 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001326 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 u32 val;
1328
1329 switch (MIPSInst_FUNC(ir)) {
1330 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001331 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 xcp->regs[MIPSInst_FT(ir)]);
1333
David Daneyb6ee75e2009-11-05 11:34:26 -08001334 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001335 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001336 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001337 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return SIGBUS;
1339 }
David Daney515b0292010-10-21 16:32:26 -07001340 if (__get_user(val, va)) {
1341 MIPS_FPU_EMU_INC_STATS(errors);
1342 *fault_addr = va;
1343 return SIGSEGV;
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 SITOREG(val, MIPSInst_FD(ir));
1346 break;
1347
1348 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001349 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 xcp->regs[MIPSInst_FT(ir)]);
1351
David Daneyb6ee75e2009-11-05 11:34:26 -08001352 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001355 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1356 MIPS_FPU_EMU_INC_STATS(errors);
1357 *fault_addr = va;
1358 return SIGBUS;
1359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001361 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001362 *fault_addr = va;
1363 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 break;
1366
1367 case madd_s_op:
1368 handler = fpemu_sp_madd;
1369 goto scoptop;
1370 case msub_s_op:
1371 handler = fpemu_sp_msub;
1372 goto scoptop;
1373 case nmadd_s_op:
1374 handler = fpemu_sp_nmadd;
1375 goto scoptop;
1376 case nmsub_s_op:
1377 handler = fpemu_sp_nmsub;
1378 goto scoptop;
1379
1380 scoptop:
1381 SPFROMREG(fr, MIPSInst_FR(ir));
1382 SPFROMREG(fs, MIPSInst_FS(ir));
1383 SPFROMREG(ft, MIPSInst_FT(ir));
1384 fd = (*handler) (fr, fs, ft);
1385 SPTOREG(fd, MIPSInst_FD(ir));
1386
1387 copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001388 if (ieee754_cxtest(IEEE754_INEXACT)) {
1389 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001391 }
1392 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1393 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001395 }
1396 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1397 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001399 }
1400 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1401 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001407 /*printk ("SIGFPE: FPU csr = %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 ctx->fcr31); */
1409 return SIGFPE;
1410 }
1411
1412 break;
1413
1414 default:
1415 return SIGILL;
1416 }
1417 break;
1418 }
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001421 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1422 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001423 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 u64 val;
1425
1426 switch (MIPSInst_FUNC(ir)) {
1427 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001428 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 xcp->regs[MIPSInst_FT(ir)]);
1430
David Daneyb6ee75e2009-11-05 11:34:26 -08001431 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001432 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001433 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001434 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return SIGBUS;
1436 }
David Daney515b0292010-10-21 16:32:26 -07001437 if (__get_user(val, va)) {
1438 MIPS_FPU_EMU_INC_STATS(errors);
1439 *fault_addr = va;
1440 return SIGSEGV;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 DITOREG(val, MIPSInst_FD(ir));
1443 break;
1444
1445 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001446 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 xcp->regs[MIPSInst_FT(ir)]);
1448
David Daneyb6ee75e2009-11-05 11:34:26 -08001449 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001451 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001452 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001453 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return SIGBUS;
1455 }
David Daney515b0292010-10-21 16:32:26 -07001456 if (__put_user(val, va)) {
1457 MIPS_FPU_EMU_INC_STATS(errors);
1458 *fault_addr = va;
1459 return SIGSEGV;
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 break;
1462
1463 case madd_d_op:
1464 handler = fpemu_dp_madd;
1465 goto dcoptop;
1466 case msub_d_op:
1467 handler = fpemu_dp_msub;
1468 goto dcoptop;
1469 case nmadd_d_op:
1470 handler = fpemu_dp_nmadd;
1471 goto dcoptop;
1472 case nmsub_d_op:
1473 handler = fpemu_dp_nmsub;
1474 goto dcoptop;
1475
1476 dcoptop:
1477 DPFROMREG(fr, MIPSInst_FR(ir));
1478 DPFROMREG(fs, MIPSInst_FS(ir));
1479 DPFROMREG(ft, MIPSInst_FT(ir));
1480 fd = (*handler) (fr, fs, ft);
1481 DPTOREG(fd, MIPSInst_FD(ir));
1482 goto copcsr;
1483
1484 default:
1485 return SIGILL;
1486 }
1487 break;
1488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001490 case 0x3:
1491 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 /* ignore prefx operation */
1495 break;
1496
1497 default:
1498 return SIGILL;
1499 }
1500
1501 return 0;
1502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504
1505
1506/*
1507 * Emulate a single COP1 arithmetic instruction.
1508 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001509static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 mips_instruction ir)
1511{
1512 int rfmt; /* resulting format */
1513 unsigned rcsr = 0; /* resulting csr */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001514 unsigned int oldrm;
1515 unsigned int cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 unsigned cond;
1517 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001518 union ieee754dp d;
1519 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 s64 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 } rv; /* resulting value */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001523 u64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
David Daneyb6ee75e2009-11-05 11:34:26 -08001525 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001527 case s_fmt: { /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001529 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1530 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 } handler;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001532 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 switch (MIPSInst_FUNC(ir)) {
1535 /* binary ops */
1536 case fadd_op:
1537 handler.b = ieee754sp_add;
1538 goto scopbop;
1539 case fsub_op:
1540 handler.b = ieee754sp_sub;
1541 goto scopbop;
1542 case fmul_op:
1543 handler.b = ieee754sp_mul;
1544 goto scopbop;
1545 case fdiv_op:
1546 handler.b = ieee754sp_div;
1547 goto scopbop;
1548
1549 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001551 if (!cpu_has_mips_4_5_r)
1552 return SIGILL;
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 handler.u = ieee754sp_sqrt;
1555 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001556
Ralf Baechle08a07902014-04-19 13:11:37 +02001557 /*
1558 * Note that on some MIPS IV implementations such as the
1559 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1560 * achieve full IEEE-754 accuracy - however this emulator does.
1561 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001563 if (!cpu_has_mips_4_5_r2)
1564 return SIGILL;
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 handler.u = fpemu_sp_rsqrt;
1567 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001570 if (!cpu_has_mips_4_5_r2)
1571 return SIGILL;
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 handler.u = fpemu_sp_recip;
1574 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001577 if (!cpu_has_mips_4_5_r)
1578 return SIGILL;
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1581 if (((ctx->fcr31 & cond) != 0) !=
1582 ((MIPSInst_FT(ir) & 1) != 0))
1583 return 0;
1584 SPFROMREG(rv.s, MIPSInst_FS(ir));
1585 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001588 if (!cpu_has_mips_4_5_r)
1589 return SIGILL;
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1592 return 0;
1593 SPFROMREG(rv.s, MIPSInst_FS(ir));
1594 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001597 if (!cpu_has_mips_4_5_r)
1598 return SIGILL;
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1601 return 0;
1602 SPFROMREG(rv.s, MIPSInst_FS(ir));
1603 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 case fabs_op:
1606 handler.u = ieee754sp_abs;
1607 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 case fneg_op:
1610 handler.u = ieee754sp_neg;
1611 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 case fmov_op:
1614 /* an easy one */
1615 SPFROMREG(rv.s, MIPSInst_FS(ir));
1616 goto copcsr;
1617
1618 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001619scopbop:
1620 SPFROMREG(fs, MIPSInst_FS(ir));
1621 SPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001623 rv.s = (*handler.b) (fs, ft);
1624 goto copcsr;
1625scopuop:
1626 SPFROMREG(fs, MIPSInst_FS(ir));
1627 rv.s = (*handler.u) (fs);
1628 goto copcsr;
1629copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001630 if (ieee754_cxtest(IEEE754_INEXACT)) {
1631 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001633 }
1634 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1635 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001637 }
1638 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1639 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001641 }
1642 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1643 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001645 }
1646 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1647 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 break;
1651
1652 /* unary conv ops */
1653 case fcvts_op:
1654 return SIGILL; /* not defined */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001656 case fcvtd_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 SPFROMREG(fs, MIPSInst_FS(ir));
1658 rv.d = ieee754dp_fsp(fs);
1659 rfmt = d_fmt;
1660 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001662 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 SPFROMREG(fs, MIPSInst_FS(ir));
1664 rv.w = ieee754sp_tint(fs);
1665 rfmt = w_fmt;
1666 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 case fround_op:
1669 case ftrunc_op:
1670 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001671 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001672 if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
1673 return SIGILL;
1674
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001675 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 SPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001677 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 rv.w = ieee754sp_tint(fs);
1679 ieee754_csr.rm = oldrm;
1680 rfmt = w_fmt;
1681 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001683 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001684 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1685 return SIGILL;
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 SPFROMREG(fs, MIPSInst_FS(ir));
1688 rv.l = ieee754sp_tlong(fs);
1689 rfmt = l_fmt;
1690 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 case froundl_op:
1693 case ftruncl_op:
1694 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001695 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001696 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1697 return SIGILL;
1698
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001699 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 SPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001701 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 rv.l = ieee754sp_tlong(fs);
1703 ieee754_csr.rm = oldrm;
1704 rfmt = l_fmt;
1705 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 default:
1708 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1709 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001710 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 SPFROMREG(fs, MIPSInst_FS(ir));
1713 SPFROMREG(ft, MIPSInst_FT(ir));
1714 rv.w = ieee754sp_cmp(fs, ft,
1715 cmptab[cmpop & 0x7], cmpop & 0x8);
1716 rfmt = -1;
1717 if ((cmpop & 0x8) && ieee754_cxtest
1718 (IEEE754_INVALID_OPERATION))
1719 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1720 else
1721 goto copcsr;
1722
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001723 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 break;
1726 }
1727 break;
1728 }
1729
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001730 case d_fmt: {
1731 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001733 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1734 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 } handler;
1736
1737 switch (MIPSInst_FUNC(ir)) {
1738 /* binary ops */
1739 case fadd_op:
1740 handler.b = ieee754dp_add;
1741 goto dcopbop;
1742 case fsub_op:
1743 handler.b = ieee754dp_sub;
1744 goto dcopbop;
1745 case fmul_op:
1746 handler.b = ieee754dp_mul;
1747 goto dcopbop;
1748 case fdiv_op:
1749 handler.b = ieee754dp_div;
1750 goto dcopbop;
1751
1752 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001754 if (!cpu_has_mips_2_3_4_5_r)
1755 return SIGILL;
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 handler.u = ieee754dp_sqrt;
1758 goto dcopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001759 /*
1760 * Note that on some MIPS IV implementations such as the
1761 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1762 * achieve full IEEE-754 accuracy - however this emulator does.
1763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001765 if (!cpu_has_mips_4_5_r2)
1766 return SIGILL;
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 handler.u = fpemu_dp_rsqrt;
1769 goto dcopuop;
1770 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001771 if (!cpu_has_mips_4_5_r2)
1772 return SIGILL;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 handler.u = fpemu_dp_recip;
1775 goto dcopuop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001777 if (!cpu_has_mips_4_5_r)
1778 return SIGILL;
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1781 if (((ctx->fcr31 & cond) != 0) !=
1782 ((MIPSInst_FT(ir) & 1) != 0))
1783 return 0;
1784 DPFROMREG(rv.d, MIPSInst_FS(ir));
1785 break;
1786 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001787 if (!cpu_has_mips_4_5_r)
1788 return SIGILL;
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1791 return 0;
1792 DPFROMREG(rv.d, MIPSInst_FS(ir));
1793 break;
1794 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001795 if (!cpu_has_mips_4_5_r)
1796 return SIGILL;
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1799 return 0;
1800 DPFROMREG(rv.d, MIPSInst_FS(ir));
1801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 case fabs_op:
1803 handler.u = ieee754dp_abs;
1804 goto dcopuop;
1805
1806 case fneg_op:
1807 handler.u = ieee754dp_neg;
1808 goto dcopuop;
1809
1810 case fmov_op:
1811 /* an easy one */
1812 DPFROMREG(rv.d, MIPSInst_FS(ir));
1813 goto copcsr;
1814
1815 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001816dcopbop:
1817 DPFROMREG(fs, MIPSInst_FS(ir));
1818 DPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001820 rv.d = (*handler.b) (fs, ft);
1821 goto copcsr;
1822dcopuop:
1823 DPFROMREG(fs, MIPSInst_FS(ir));
1824 rv.d = (*handler.u) (fs);
1825 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001827 /*
1828 * unary conv ops
1829 */
1830 case fcvts_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 DPFROMREG(fs, MIPSInst_FS(ir));
1832 rv.s = ieee754sp_fdp(fs);
1833 rfmt = s_fmt;
1834 goto copcsr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 case fcvtd_op:
1837 return SIGILL; /* not defined */
1838
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001839 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 DPFROMREG(fs, MIPSInst_FS(ir));
1841 rv.w = ieee754dp_tint(fs); /* wrong */
1842 rfmt = w_fmt;
1843 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 case fround_op:
1846 case ftrunc_op:
1847 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001848 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001849 if (!cpu_has_mips_2_3_4_5_r)
1850 return SIGILL;
1851
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001852 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 DPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001854 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 rv.w = ieee754dp_tint(fs);
1856 ieee754_csr.rm = oldrm;
1857 rfmt = w_fmt;
1858 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001860 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001861 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1862 return SIGILL;
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 DPFROMREG(fs, MIPSInst_FS(ir));
1865 rv.l = ieee754dp_tlong(fs);
1866 rfmt = l_fmt;
1867 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 case froundl_op:
1870 case ftruncl_op:
1871 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001872 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001873 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1874 return SIGILL;
1875
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001876 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 DPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001878 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 rv.l = ieee754dp_tlong(fs);
1880 ieee754_csr.rm = oldrm;
1881 rfmt = l_fmt;
1882 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 default:
1885 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1886 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001887 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 DPFROMREG(fs, MIPSInst_FS(ir));
1890 DPFROMREG(ft, MIPSInst_FT(ir));
1891 rv.w = ieee754dp_cmp(fs, ft,
1892 cmptab[cmpop & 0x7], cmpop & 0x8);
1893 rfmt = -1;
1894 if ((cmpop & 0x8)
1895 &&
1896 ieee754_cxtest
1897 (IEEE754_INVALID_OPERATION))
1898 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1899 else
1900 goto copcsr;
1901
1902 }
1903 else {
1904 return SIGILL;
1905 }
1906 break;
1907 }
1908 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001910 case w_fmt:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 switch (MIPSInst_FUNC(ir)) {
1912 case fcvts_op:
1913 /* convert word to single precision real */
1914 SPFROMREG(fs, MIPSInst_FS(ir));
1915 rv.s = ieee754sp_fint(fs.bits);
1916 rfmt = s_fmt;
1917 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 case fcvtd_op:
1919 /* convert word to double precision real */
1920 SPFROMREG(fs, MIPSInst_FS(ir));
1921 rv.d = ieee754dp_fint(fs.bits);
1922 rfmt = d_fmt;
1923 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 default:
1925 return SIGILL;
1926 }
1927 break;
1928 }
1929
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001930 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001931
1932 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1933 return SIGILL;
1934
Paul Burtonbbd426f2014-02-13 11:26:41 +00001935 DIFROMREG(bits, MIPSInst_FS(ir));
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 switch (MIPSInst_FUNC(ir)) {
1938 case fcvts_op:
1939 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001940 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 rfmt = s_fmt;
1942 goto copcsr;
1943 case fcvtd_op:
1944 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001945 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 rfmt = d_fmt;
1947 goto copcsr;
1948 default:
1949 return SIGILL;
1950 }
1951 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 default:
1954 return SIGILL;
1955 }
1956
1957 /*
1958 * Update the fpu CSR register for this operation.
1959 * If an exception is required, generate a tidy SIGFPE exception,
1960 * without updating the result register.
1961 * Note: cause exception bits do not accumulate, they are rewritten
1962 * for each op; only the flag/sticky bits accumulate.
1963 */
1964 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1965 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001966 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return SIGFPE;
1968 }
1969
1970 /*
1971 * Now we can safely write the result back to the register file.
1972 */
1973 switch (rfmt) {
Ralf Baechle08a07902014-04-19 13:11:37 +02001974 case -1:
1975
1976 if (cpu_has_mips_4_5_r)
Rob Kendrickc3b9b942014-07-23 10:03:58 +01001977 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 else
Ralf Baechle08a07902014-04-19 13:11:37 +02001979 cbit = FPU_CSR_COND;
1980 if (rv.w)
1981 ctx->fcr31 |= cbit;
1982 else
1983 ctx->fcr31 &= ~cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 break;
Ralf Baechle08a07902014-04-19 13:11:37 +02001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 case d_fmt:
1987 DPTOREG(rv.d, MIPSInst_FD(ir));
1988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 case s_fmt:
1990 SPTOREG(rv.s, MIPSInst_FD(ir));
1991 break;
1992 case w_fmt:
1993 SITOREG(rv.w, MIPSInst_FD(ir));
1994 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001996 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1997 return SIGILL;
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 DITOREG(rv.l, MIPSInst_FD(ir));
2000 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 default:
2002 return SIGILL;
2003 }
2004
2005 return 0;
2006}
2007
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002008int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07002009 int has_fpu, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Ralf Baechle333d1f62005-02-28 17:55:57 +00002011 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002012 struct mm_decoded_insn dec_insn;
2013 u16 instr[4];
2014 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 int sig = 0;
2016
2017 oldepc = xcp->cp0_epc;
2018 do {
2019 prevepc = xcp->cp0_epc;
2020
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002021 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2022 /*
2023 * Get next 2 microMIPS instructions and convert them
2024 * into 32-bit instructions.
2025 */
2026 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2027 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2028 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2029 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2030 MIPS_FPU_EMU_INC_STATS(errors);
2031 return SIGBUS;
2032 }
2033 instr_ptr = instr;
2034
2035 /* Get first instruction. */
2036 if (mm_insn_16bit(*instr_ptr)) {
2037 /* Duplicate the half-word. */
2038 dec_insn.insn = (*instr_ptr << 16) |
2039 (*instr_ptr);
2040 /* 16-bit instruction. */
2041 dec_insn.pc_inc = 2;
2042 instr_ptr += 1;
2043 } else {
2044 dec_insn.insn = (*instr_ptr << 16) |
2045 *(instr_ptr+1);
2046 /* 32-bit instruction. */
2047 dec_insn.pc_inc = 4;
2048 instr_ptr += 2;
2049 }
2050 /* Get second instruction. */
2051 if (mm_insn_16bit(*instr_ptr)) {
2052 /* Duplicate the half-word. */
2053 dec_insn.next_insn = (*instr_ptr << 16) |
2054 (*instr_ptr);
2055 /* 16-bit instruction. */
2056 dec_insn.next_pc_inc = 2;
2057 } else {
2058 dec_insn.next_insn = (*instr_ptr << 16) |
2059 *(instr_ptr+1);
2060 /* 32-bit instruction. */
2061 dec_insn.next_pc_inc = 4;
2062 }
2063 dec_insn.micro_mips_mode = 1;
2064 } else {
2065 if ((get_user(dec_insn.insn,
2066 (mips_instruction __user *) xcp->cp0_epc)) ||
2067 (get_user(dec_insn.next_insn,
2068 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2069 MIPS_FPU_EMU_INC_STATS(errors);
2070 return SIGBUS;
2071 }
2072 dec_insn.pc_inc = 4;
2073 dec_insn.next_pc_inc = 4;
2074 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002076
2077 if ((dec_insn.insn == 0) ||
2078 ((dec_insn.pc_inc == 2) &&
2079 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2080 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002082 /*
2083 * The 'ieee754_csr' is an alias of
Ralf Baechle70342282013-01-22 12:59:30 +01002084 * ctx->fcr31. No need to copy ctx->fcr31 to
2085 * ieee754_csr. But ieee754_csr.rm is ieee
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002086 * library modes. (not mips rounding mode)
2087 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002088 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
2090
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002091 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 break;
2093 if (sig)
2094 break;
2095
2096 cond_resched();
2097 } while (xcp->cp0_epc > prevepc);
2098
2099 /* SIGILL indicates a non-fpu instruction */
2100 if (sig == SIGILL && xcp->cp0_epc != oldepc)
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002101 /* but if EPC has advanced, then ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 sig = 0;
2103
2104 return sig;
2105}