blob: 7f373a2858b547e6a927c12a7b6fb1873d36ac91 [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;
David Daneyc26d4212013-08-19 12:10:34 -0700626#ifdef CONFIG_CPU_CAVIUM_OCTEON
627 case lwc2_op: /* This is bbit0 on Octeon */
628 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
629 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
630 else
631 *contpc = regs->cp0_epc + 8;
632 return 1;
633 case ldc2_op: /* This is bbit032 on Octeon */
634 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
635 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
636 else
637 *contpc = regs->cp0_epc + 8;
638 return 1;
639 case swc2_op: /* This is bbit1 on Octeon */
640 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
641 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
642 else
643 *contpc = regs->cp0_epc + 8;
644 return 1;
645 case sdc2_op: /* This is bbit132 on Octeon */
646 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
647 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
648 else
649 *contpc = regs->cp0_epc + 8;
650 return 1;
Markos Chandras8467ca02014-11-26 13:56:51 +0000651#else
652 case bc6_op:
653 /*
654 * Only valid for MIPS R6 but we can still end up
655 * here from a broken userland so just tell emulator
656 * this is not a branch and let it break later on.
657 */
658 if (!cpu_has_mips_r6)
659 break;
660 *contpc = regs->cp0_epc + dec_insn.pc_inc +
661 dec_insn.next_pc_inc;
662
663 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700664#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 case cop0_op:
666 case cop1_op:
Markos Chandrasc8a34582014-11-26 10:10:18 +0000667 /* Need to check for R6 bc1nez and bc1eqz branches */
668 if (cpu_has_mips_r6 &&
669 ((insn.i_format.rs == bc1eqz_op) ||
670 (insn.i_format.rs == bc1nez_op))) {
671 bit = 0;
672 switch (insn.i_format.rs) {
673 case bc1eqz_op:
674 if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
675 bit = 1;
676 break;
677 case bc1nez_op:
678 if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
679 bit = 1;
680 break;
681 }
682 if (bit)
683 *contpc = regs->cp0_epc +
684 dec_insn.pc_inc +
685 (insn.i_format.simmediate << 2);
686 else
687 *contpc = regs->cp0_epc +
688 dec_insn.pc_inc +
689 dec_insn.next_pc_inc;
690
691 return 1;
692 }
693 /* R2/R6 compatible cop1 instruction. Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 case cop2_op:
695 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500696 if (insn.i_format.rs == bc_op) {
697 preempt_disable();
698 if (is_fpu_owner())
Manuel Lauss842dfc12014-11-07 14:13:54 +0100699 fcr31 = read_32bit_cp1_register(CP1_STATUS);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500700 else
701 fcr31 = current->thread.fpu.fcr31;
702 preempt_enable();
703
704 bit = (insn.i_format.rt >> 2);
705 bit += (bit != 0);
706 bit += 23;
707 switch (insn.i_format.rt & 3) {
708 case 0: /* bc1f */
709 case 2: /* bc1fl */
710 if (~fcr31 & (1 << bit))
711 *contpc = regs->cp0_epc +
712 dec_insn.pc_inc +
713 (insn.i_format.simmediate << 2);
714 else
715 *contpc = regs->cp0_epc +
716 dec_insn.pc_inc +
717 dec_insn.next_pc_inc;
718 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500719 case 1: /* bc1t */
720 case 3: /* bc1tl */
721 if (fcr31 & (1 << bit))
722 *contpc = regs->cp0_epc +
723 dec_insn.pc_inc +
724 (insn.i_format.simmediate << 2);
725 else
726 *contpc = regs->cp0_epc +
727 dec_insn.pc_inc +
728 dec_insn.next_pc_inc;
729 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500730 }
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return 0;
735}
736
737/*
738 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100739 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800740 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000741 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200742 * FPU emu is slow and bulky and optimizing this function offers fairly
743 * sizeable benefits so we try to be clever and make this function return
744 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000745 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
David Daneyda0bac32009-11-02 11:33:46 -0800747static inline int cop1_64bit(struct pt_regs *xcp)
748{
Ralf Baechle08a07902014-04-19 13:11:37 +0200749 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
750 return 1;
751 else if (config_enabled(CONFIG_32BIT) &&
752 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
753 return 0;
754
Paul Burton597ce172013-11-22 13:12:07 +0000755 return !test_thread_flag(TIF_32BIT_FPREGS);
David Daneyda0bac32009-11-02 11:33:46 -0800756}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Paul Burton4227a2d2014-09-11 08:30:20 +0100758static inline bool hybrid_fprs(void)
759{
760 return test_thread_flag(TIF_HYBRID_FPREGS);
761}
762
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200763#define SIFROMREG(si, x) \
764do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100765 if (cop1_64bit(xcp) && !hybrid_fprs()) \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100766 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000767 else \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100768 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000769} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800770
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200771#define SITOREG(si, x) \
772do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100773 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000774 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000775 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000776 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
777 set_fpr32(&ctx->fpr[x], i, 0); \
778 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000779 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000780 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000781} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Paul Burtonc8c0da62014-09-24 10:45:37 +0100783#define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000784
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200785#define SITOHREG(si, x) \
786do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000787 unsigned i; \
788 set_fpr32(&ctx->fpr[x], 1, si); \
789 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
790 set_fpr32(&ctx->fpr[x], i, 0); \
791} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000792
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200793#define DIFROMREG(di, x) \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000794 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
795
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200796#define DITOREG(di, x) \
797do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000798 unsigned fpr, i; \
799 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
800 set_fpr64(&ctx->fpr[fpr], 0, di); \
801 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
802 set_fpr64(&ctx->fpr[fpr], i, 0); \
803} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Ralf Baechle21a151d2007-10-11 23:46:15 +0100805#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
806#define SPTOREG(sp, x) SITOREG((sp).bits, x)
807#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
808#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810/*
811 * Emulate the single floating point instruction pointed at by EPC.
812 * Two instructions if the instruction is in a branch delay slot.
813 */
814
David Daney515b0292010-10-21 16:32:26 -0700815static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500816 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500818 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200819 unsigned int cond, cbit;
820 mips_instruction ir;
821 int likely, pc_inc;
822 u32 __user *wva;
823 u64 __user *dva;
824 u32 value;
825 u32 wval;
826 u64 dval;
827 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Ralf Baechle70e4c232014-04-30 11:09:44 +0200829 /*
830 * These are giving gcc a gentle hint about what to expect in
831 * dec_inst in order to do better optimization.
832 */
833 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
834 unreachable();
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200837 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500838 if (dec_insn.micro_mips_mode) {
839 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200840 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500841 } else {
842 if (!isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200843 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500844 }
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200847 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /*
849 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +0100850 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 * BEFORE we do the cop1 instruction.
852 *
853 * This branch could be a COP1 branch, but in that case we
854 * would have had a trap for that instruction, and would not
855 * come through this route.
856 *
857 * Linux MIPS branch emulator operates on context, updating the
858 * cp0_epc.
859 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500860 ir = dec_insn.next_insn; /* process delay slot instr */
861 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000862 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500863 ir = dec_insn.insn; /* process current instr */
864 pc_inc = dec_insn.pc_inc;
865 }
866
867 /*
868 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
869 * instructions, we want to convert microMIPS FPU instructions
870 * into MIPS32 instructions so that we could reuse all of the
871 * FPU emulation code.
872 *
873 * NOTE: We cannot do this for branch instructions since they
874 * are not a subset. Example: Cannot emulate a 16-bit
875 * aligned target address with a MIPS32 instruction.
876 */
877 if (dec_insn.micro_mips_mode) {
878 /*
879 * If next instruction is a 16-bit instruction, then it
880 * it cannot be a FPU instruction. This could happen
881 * since we can be called for non-FPU instructions.
882 */
883 if ((pc_inc == 2) ||
884 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
885 == SIGILL))
886 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200889emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200890 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -0800891 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 switch (MIPSInst_OPCODE(ir)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200893 case ldc1_op:
894 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
895 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800896 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -0700897
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200898 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800899 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200900 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return SIGBUS;
902 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200903 if (__get_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700904 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200905 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700906 return SIGSEGV;
907 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200908 DITOREG(dval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200911 case sdc1_op:
912 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
913 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800914 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200915 DIFROMREG(dval, MIPSInst_RT(ir));
916 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800917 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200918 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return SIGBUS;
920 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200921 if (__put_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700922 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200923 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700924 return SIGSEGV;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200928 case lwc1_op:
929 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
930 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800931 MIPS_FPU_EMU_INC_STATS(loads);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200932 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800933 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200934 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return SIGBUS;
936 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200937 if (__get_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -0700938 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200939 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -0700940 return SIGSEGV;
941 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200942 SITOREG(wval, 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 swc1_op:
946 wva = (u32 __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 SIFROMREG(wval, MIPSInst_RT(ir));
950 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800951 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200952 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return SIGBUS;
954 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200955 if (__put_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -0700956 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200957 *fault_addr = wva;
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
962 case cop1_op:
963 switch (MIPSInst_RS(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 case dmfc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +0200965 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
966 return SIGILL;
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* copregister fs -> gpr[rt] */
969 if (MIPSInst_RT(ir) != 0) {
970 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
971 MIPSInst_RD(ir));
972 }
973 break;
974
975 case dmtc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +0200976 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
977 return SIGILL;
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* copregister fs <- rt */
980 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000983 case mfhc_op:
984 if (!cpu_has_mips_r2)
985 goto sigill;
986
987 /* copregister rd -> gpr[rt] */
988 if (MIPSInst_RT(ir) != 0) {
989 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
990 MIPSInst_RD(ir));
991 }
992 break;
993
994 case mthc_op:
995 if (!cpu_has_mips_r2)
996 goto sigill;
997
998 /* copregister rd <- gpr[rt] */
999 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1000 break;
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 case mfc_op:
1003 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (MIPSInst_RT(ir) != 0) {
1005 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1006 MIPSInst_RD(ir));
1007 }
1008 break;
1009
1010 case mtc_op:
1011 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1013 break;
1014
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001015 case cfc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /* cop control register rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1018 value = ctx->fcr31;
Ralf Baechle56a64732014-04-30 11:21:55 +02001019 value = (value & ~FPU_CSR_RM) | modeindex(value);
Ralf Baechle92df0f82014-04-19 14:03:37 +02001020 pr_debug("%p gpr[%d]<-csr=%08x\n",
1021 (void *) (xcp->cp0_epc),
1022 MIPSInst_RT(ir), value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024 else if (MIPSInst_RD(ir) == FPCREG_RID)
1025 value = 0;
1026 else
1027 value = 0;
1028 if (MIPSInst_RT(ir))
1029 xcp->regs[MIPSInst_RT(ir)] = value;
1030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001032 case ctc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (MIPSInst_RT(ir) == 0)
1035 value = 0;
1036 else
1037 value = xcp->regs[MIPSInst_RT(ir)];
1038
1039 /* we only have one writable control reg
1040 */
1041 if (MIPSInst_RD(ir) == FPCREG_CSR) {
Ralf Baechle92df0f82014-04-19 14:03:37 +02001042 pr_debug("%p gpr[%d]->csr=%08x\n",
1043 (void *) (xcp->cp0_epc),
1044 MIPSInst_RT(ir), value);
Shane McDonald95e8f632010-05-06 23:26:57 -06001045
1046 /*
1047 * Don't write reserved bits,
1048 * and convert to ieee library modes
1049 */
Ralf Baechle56a64732014-04-30 11:21:55 +02001050 ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
1051 modeindex(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1054 return SIGFPE;
1055 }
1056 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001058 case bc_op:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001059 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return SIGILL;
1061
Ralf Baechle08a07902014-04-19 13:11:37 +02001062 if (cpu_has_mips_4_5_r)
1063 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1064 else
1065 cbit = FPU_CSR_COND;
1066 cond = ctx->fcr31 & cbit;
1067
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001068 likely = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 switch (MIPSInst_RT(ir) & 3) {
1070 case bcfl_op:
1071 likely = 1;
1072 case bcf_op:
1073 cond = !cond;
1074 break;
1075 case bctl_op:
1076 likely = 1;
1077 case bct_op:
1078 break;
1079 default:
1080 /* thats an illegal instruction */
1081 return SIGILL;
1082 }
1083
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001084 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (cond) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001086 /*
1087 * Branch taken: emulate dslot instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001089 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001091 contpc = MIPSInst_SIMM(ir);
1092 ir = dec_insn.next_insn;
1093 if (dec_insn.micro_mips_mode) {
1094 contpc = (xcp->cp0_epc + (contpc << 1));
1095
1096 /* If 16-bit instruction, not FPU. */
1097 if ((dec_insn.next_pc_inc == 2) ||
1098 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1099
1100 /*
1101 * Since this instruction will
1102 * be put on the stack with
1103 * 32-bit words, get around
1104 * this problem by putting a
1105 * NOP16 as the second one.
1106 */
1107 if (dec_insn.next_pc_inc == 2)
1108 ir = (ir & (~0xffff)) | MM_NOP16;
1109
1110 /*
1111 * Single step the non-CP1
1112 * instruction in the dslot.
1113 */
1114 return mips_dsemul(xcp, ir, contpc);
1115 }
1116 } else
1117 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 switch (MIPSInst_OPCODE(ir)) {
1120 case lwc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001121 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 case swc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001124 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 case ldc1_op:
1127 case sdc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001128 if (cpu_has_mips_2_3_4_5 ||
1129 cpu_has_mips64)
1130 goto emul;
1131
1132 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001134
Ralf Baechle08a07902014-04-19 13:11:37 +02001135 case cop1_op:
1136 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001137
Ralf Baechle08a07902014-04-19 13:11:37 +02001138 case cop1x_op:
Markos Chandrasa5466d72014-10-21 10:21:54 +01001139 if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
Ralf Baechle08a07902014-04-19 13:11:37 +02001140 /* its one of ours */
1141 goto emul;
1142
1143 return SIGILL;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001146 if (!cpu_has_mips_4_5_r)
1147 return SIGILL;
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (MIPSInst_FUNC(ir) == movc_op)
1150 goto emul;
1151 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153
1154 /*
1155 * Single step the non-cp1
1156 * instruction in the dslot
1157 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001158 return mips_dsemul(xcp, ir, contpc);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001159 } else if (likely) { /* branch not taken */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /*
1161 * branch likely nullifies
1162 * dslot if not taken
1163 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001164 xcp->cp0_epc += dec_insn.pc_inc;
1165 contpc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /*
1167 * else continue & execute
1168 * dslot as normal insn
1169 */
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 default:
1174 if (!(MIPSInst_RS(ir) & 0x10))
1175 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001177 /* a real fpu computation instruction */
1178 if ((sig = fpu_emu(xcp, ctx, ir)))
1179 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 break;
1182
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001183 case cop1x_op:
Markos Chandrasa5466d72014-10-21 10:21:54 +01001184 if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
Ralf Baechle08a07902014-04-19 13:11:37 +02001185 return SIGILL;
1186
1187 sig = fpux_emu(xcp, ctx, ir, fault_addr);
David Daney515b0292010-10-21 16:32:26 -07001188 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return sig;
1190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001193 if (!cpu_has_mips_4_5_r)
1194 return SIGILL;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (MIPSInst_FUNC(ir) != movc_op)
1197 return SIGILL;
1198 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1199 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1200 xcp->regs[MIPSInst_RD(ir)] =
1201 xcp->regs[MIPSInst_RS(ir)];
1202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 default:
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001204sigill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return SIGILL;
1206 }
1207
1208 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001209 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001210 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return 0;
1213}
1214
1215/*
1216 * Conversion table from MIPS compare ops 48-63
1217 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1218 */
1219static const unsigned char cmptab[8] = {
1220 0, /* cmp_0 (sig) cmp_sf */
1221 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1222 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1223 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1224 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1225 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1226 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1227 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1228};
1229
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/*
1232 * Additional MIPS4 instructions
1233 */
1234
Ralf Baechle47fa0c02014-04-16 11:00:12 +02001235#define DEF3OP(name, p, f1, f2, f3) \
1236static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1237 union ieee754##p s, union ieee754##p t) \
1238{ \
1239 struct _ieee754_csr ieee754_csr_save; \
1240 s = f1(s, t); \
1241 ieee754_csr_save = ieee754_csr; \
1242 s = f2(s, r); \
1243 ieee754_csr_save.cx |= ieee754_csr.cx; \
1244 ieee754_csr_save.sx |= ieee754_csr.sx; \
1245 s = f3(s); \
1246 ieee754_csr.cx |= ieee754_csr_save.cx; \
1247 ieee754_csr.sx |= ieee754_csr_save.sx; \
1248 return s; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001251static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 return ieee754dp_div(ieee754dp_one(0), d);
1254}
1255
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001256static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1259}
1260
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001261static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 return ieee754sp_div(ieee754sp_one(0), s);
1264}
1265
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001266static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1269}
1270
Ralf Baechle21a151d2007-10-11 23:46:15 +01001271DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1272DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1274DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001275DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1276DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1278DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1279
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001280static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001281 mips_instruction ir, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 unsigned rcsr = 0; /* resulting csr */
1284
David Daneyb6ee75e2009-11-05 11:34:26 -08001285 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 switch (MIPSInst_FMA_FFMT(ir)) {
1288 case s_fmt:{ /* 0 */
1289
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001290 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1291 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001292 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 u32 val;
1294
1295 switch (MIPSInst_FUNC(ir)) {
1296 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001297 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 xcp->regs[MIPSInst_FT(ir)]);
1299
David Daneyb6ee75e2009-11-05 11:34:26 -08001300 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001301 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001302 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001303 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return SIGBUS;
1305 }
David Daney515b0292010-10-21 16:32:26 -07001306 if (__get_user(val, va)) {
1307 MIPS_FPU_EMU_INC_STATS(errors);
1308 *fault_addr = va;
1309 return SIGSEGV;
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 SITOREG(val, MIPSInst_FD(ir));
1312 break;
1313
1314 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001315 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 xcp->regs[MIPSInst_FT(ir)]);
1317
David Daneyb6ee75e2009-11-05 11:34:26 -08001318 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001321 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1322 MIPS_FPU_EMU_INC_STATS(errors);
1323 *fault_addr = va;
1324 return SIGBUS;
1325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001327 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001328 *fault_addr = va;
1329 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331 break;
1332
1333 case madd_s_op:
1334 handler = fpemu_sp_madd;
1335 goto scoptop;
1336 case msub_s_op:
1337 handler = fpemu_sp_msub;
1338 goto scoptop;
1339 case nmadd_s_op:
1340 handler = fpemu_sp_nmadd;
1341 goto scoptop;
1342 case nmsub_s_op:
1343 handler = fpemu_sp_nmsub;
1344 goto scoptop;
1345
1346 scoptop:
1347 SPFROMREG(fr, MIPSInst_FR(ir));
1348 SPFROMREG(fs, MIPSInst_FS(ir));
1349 SPFROMREG(ft, MIPSInst_FT(ir));
1350 fd = (*handler) (fr, fs, ft);
1351 SPTOREG(fd, MIPSInst_FD(ir));
1352
1353 copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001354 if (ieee754_cxtest(IEEE754_INEXACT)) {
1355 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001357 }
1358 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1359 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001361 }
1362 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1363 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001365 }
1366 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1367 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001373 /*printk ("SIGFPE: FPU csr = %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 ctx->fcr31); */
1375 return SIGFPE;
1376 }
1377
1378 break;
1379
1380 default:
1381 return SIGILL;
1382 }
1383 break;
1384 }
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001387 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1388 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001389 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 u64 val;
1391
1392 switch (MIPSInst_FUNC(ir)) {
1393 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001394 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 xcp->regs[MIPSInst_FT(ir)]);
1396
David Daneyb6ee75e2009-11-05 11:34:26 -08001397 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001398 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001399 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001400 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return SIGBUS;
1402 }
David Daney515b0292010-10-21 16:32:26 -07001403 if (__get_user(val, va)) {
1404 MIPS_FPU_EMU_INC_STATS(errors);
1405 *fault_addr = va;
1406 return SIGSEGV;
1407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 DITOREG(val, MIPSInst_FD(ir));
1409 break;
1410
1411 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001412 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 xcp->regs[MIPSInst_FT(ir)]);
1414
David Daneyb6ee75e2009-11-05 11:34:26 -08001415 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001417 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001418 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001419 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return SIGBUS;
1421 }
David Daney515b0292010-10-21 16:32:26 -07001422 if (__put_user(val, va)) {
1423 MIPS_FPU_EMU_INC_STATS(errors);
1424 *fault_addr = va;
1425 return SIGSEGV;
1426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 break;
1428
1429 case madd_d_op:
1430 handler = fpemu_dp_madd;
1431 goto dcoptop;
1432 case msub_d_op:
1433 handler = fpemu_dp_msub;
1434 goto dcoptop;
1435 case nmadd_d_op:
1436 handler = fpemu_dp_nmadd;
1437 goto dcoptop;
1438 case nmsub_d_op:
1439 handler = fpemu_dp_nmsub;
1440 goto dcoptop;
1441
1442 dcoptop:
1443 DPFROMREG(fr, MIPSInst_FR(ir));
1444 DPFROMREG(fs, MIPSInst_FS(ir));
1445 DPFROMREG(ft, MIPSInst_FT(ir));
1446 fd = (*handler) (fr, fs, ft);
1447 DPTOREG(fd, MIPSInst_FD(ir));
1448 goto copcsr;
1449
1450 default:
1451 return SIGILL;
1452 }
1453 break;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001456 case 0x3:
1457 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* ignore prefx operation */
1461 break;
1462
1463 default:
1464 return SIGILL;
1465 }
1466
1467 return 0;
1468}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470
1471
1472/*
1473 * Emulate a single COP1 arithmetic instruction.
1474 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001475static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 mips_instruction ir)
1477{
1478 int rfmt; /* resulting format */
1479 unsigned rcsr = 0; /* resulting csr */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001480 unsigned int oldrm;
1481 unsigned int cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 unsigned cond;
1483 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001484 union ieee754dp d;
1485 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 s64 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 } rv; /* resulting value */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001489 u64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
David Daneyb6ee75e2009-11-05 11:34:26 -08001491 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001493 case s_fmt: { /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001495 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1496 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 } handler;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001498 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 switch (MIPSInst_FUNC(ir)) {
1501 /* binary ops */
1502 case fadd_op:
1503 handler.b = ieee754sp_add;
1504 goto scopbop;
1505 case fsub_op:
1506 handler.b = ieee754sp_sub;
1507 goto scopbop;
1508 case fmul_op:
1509 handler.b = ieee754sp_mul;
1510 goto scopbop;
1511 case fdiv_op:
1512 handler.b = ieee754sp_div;
1513 goto scopbop;
1514
1515 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001517 if (!cpu_has_mips_4_5_r)
1518 return SIGILL;
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 handler.u = ieee754sp_sqrt;
1521 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001522
Ralf Baechle08a07902014-04-19 13:11:37 +02001523 /*
1524 * Note that on some MIPS IV implementations such as the
1525 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1526 * achieve full IEEE-754 accuracy - however this emulator does.
1527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001529 if (!cpu_has_mips_4_5_r2)
1530 return SIGILL;
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 handler.u = fpemu_sp_rsqrt;
1533 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001536 if (!cpu_has_mips_4_5_r2)
1537 return SIGILL;
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 handler.u = fpemu_sp_recip;
1540 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001543 if (!cpu_has_mips_4_5_r)
1544 return SIGILL;
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1547 if (((ctx->fcr31 & cond) != 0) !=
1548 ((MIPSInst_FT(ir) & 1) != 0))
1549 return 0;
1550 SPFROMREG(rv.s, MIPSInst_FS(ir));
1551 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001554 if (!cpu_has_mips_4_5_r)
1555 return SIGILL;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1558 return 0;
1559 SPFROMREG(rv.s, MIPSInst_FS(ir));
1560 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001563 if (!cpu_has_mips_4_5_r)
1564 return SIGILL;
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1567 return 0;
1568 SPFROMREG(rv.s, MIPSInst_FS(ir));
1569 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 case fabs_op:
1572 handler.u = ieee754sp_abs;
1573 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 case fneg_op:
1576 handler.u = ieee754sp_neg;
1577 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 case fmov_op:
1580 /* an easy one */
1581 SPFROMREG(rv.s, MIPSInst_FS(ir));
1582 goto copcsr;
1583
1584 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001585scopbop:
1586 SPFROMREG(fs, MIPSInst_FS(ir));
1587 SPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001589 rv.s = (*handler.b) (fs, ft);
1590 goto copcsr;
1591scopuop:
1592 SPFROMREG(fs, MIPSInst_FS(ir));
1593 rv.s = (*handler.u) (fs);
1594 goto copcsr;
1595copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001596 if (ieee754_cxtest(IEEE754_INEXACT)) {
1597 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001599 }
1600 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1601 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001603 }
1604 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1605 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001607 }
1608 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1609 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001611 }
1612 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1613 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 break;
1617
1618 /* unary conv ops */
1619 case fcvts_op:
1620 return SIGILL; /* not defined */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001622 case fcvtd_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 SPFROMREG(fs, MIPSInst_FS(ir));
1624 rv.d = ieee754dp_fsp(fs);
1625 rfmt = d_fmt;
1626 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001628 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 SPFROMREG(fs, MIPSInst_FS(ir));
1630 rv.w = ieee754sp_tint(fs);
1631 rfmt = w_fmt;
1632 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 case fround_op:
1635 case ftrunc_op:
1636 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001637 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001638 if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
1639 return SIGILL;
1640
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001641 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 SPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001643 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 rv.w = ieee754sp_tint(fs);
1645 ieee754_csr.rm = oldrm;
1646 rfmt = w_fmt;
1647 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001649 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001650 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1651 return SIGILL;
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 SPFROMREG(fs, MIPSInst_FS(ir));
1654 rv.l = ieee754sp_tlong(fs);
1655 rfmt = l_fmt;
1656 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 case froundl_op:
1659 case ftruncl_op:
1660 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001661 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001662 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1663 return SIGILL;
1664
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001665 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 SPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001667 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 rv.l = ieee754sp_tlong(fs);
1669 ieee754_csr.rm = oldrm;
1670 rfmt = l_fmt;
1671 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 default:
1674 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1675 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001676 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 SPFROMREG(fs, MIPSInst_FS(ir));
1679 SPFROMREG(ft, MIPSInst_FT(ir));
1680 rv.w = ieee754sp_cmp(fs, ft,
1681 cmptab[cmpop & 0x7], cmpop & 0x8);
1682 rfmt = -1;
1683 if ((cmpop & 0x8) && ieee754_cxtest
1684 (IEEE754_INVALID_OPERATION))
1685 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1686 else
1687 goto copcsr;
1688
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001689 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 break;
1692 }
1693 break;
1694 }
1695
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001696 case d_fmt: {
1697 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001699 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1700 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 } handler;
1702
1703 switch (MIPSInst_FUNC(ir)) {
1704 /* binary ops */
1705 case fadd_op:
1706 handler.b = ieee754dp_add;
1707 goto dcopbop;
1708 case fsub_op:
1709 handler.b = ieee754dp_sub;
1710 goto dcopbop;
1711 case fmul_op:
1712 handler.b = ieee754dp_mul;
1713 goto dcopbop;
1714 case fdiv_op:
1715 handler.b = ieee754dp_div;
1716 goto dcopbop;
1717
1718 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001720 if (!cpu_has_mips_2_3_4_5_r)
1721 return SIGILL;
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 handler.u = ieee754dp_sqrt;
1724 goto dcopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001725 /*
1726 * Note that on some MIPS IV implementations such as the
1727 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1728 * achieve full IEEE-754 accuracy - however this emulator does.
1729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001731 if (!cpu_has_mips_4_5_r2)
1732 return SIGILL;
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 handler.u = fpemu_dp_rsqrt;
1735 goto dcopuop;
1736 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001737 if (!cpu_has_mips_4_5_r2)
1738 return SIGILL;
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 handler.u = fpemu_dp_recip;
1741 goto dcopuop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001743 if (!cpu_has_mips_4_5_r)
1744 return SIGILL;
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1747 if (((ctx->fcr31 & cond) != 0) !=
1748 ((MIPSInst_FT(ir) & 1) != 0))
1749 return 0;
1750 DPFROMREG(rv.d, MIPSInst_FS(ir));
1751 break;
1752 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001753 if (!cpu_has_mips_4_5_r)
1754 return SIGILL;
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1757 return 0;
1758 DPFROMREG(rv.d, MIPSInst_FS(ir));
1759 break;
1760 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001761 if (!cpu_has_mips_4_5_r)
1762 return SIGILL;
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1765 return 0;
1766 DPFROMREG(rv.d, MIPSInst_FS(ir));
1767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 case fabs_op:
1769 handler.u = ieee754dp_abs;
1770 goto dcopuop;
1771
1772 case fneg_op:
1773 handler.u = ieee754dp_neg;
1774 goto dcopuop;
1775
1776 case fmov_op:
1777 /* an easy one */
1778 DPFROMREG(rv.d, MIPSInst_FS(ir));
1779 goto copcsr;
1780
1781 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001782dcopbop:
1783 DPFROMREG(fs, MIPSInst_FS(ir));
1784 DPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001786 rv.d = (*handler.b) (fs, ft);
1787 goto copcsr;
1788dcopuop:
1789 DPFROMREG(fs, MIPSInst_FS(ir));
1790 rv.d = (*handler.u) (fs);
1791 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001793 /*
1794 * unary conv ops
1795 */
1796 case fcvts_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 DPFROMREG(fs, MIPSInst_FS(ir));
1798 rv.s = ieee754sp_fdp(fs);
1799 rfmt = s_fmt;
1800 goto copcsr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 case fcvtd_op:
1803 return SIGILL; /* not defined */
1804
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001805 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 DPFROMREG(fs, MIPSInst_FS(ir));
1807 rv.w = ieee754dp_tint(fs); /* wrong */
1808 rfmt = w_fmt;
1809 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 case fround_op:
1812 case ftrunc_op:
1813 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001814 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001815 if (!cpu_has_mips_2_3_4_5_r)
1816 return SIGILL;
1817
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001818 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 DPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001820 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 rv.w = ieee754dp_tint(fs);
1822 ieee754_csr.rm = oldrm;
1823 rfmt = w_fmt;
1824 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001826 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001827 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1828 return SIGILL;
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 DPFROMREG(fs, MIPSInst_FS(ir));
1831 rv.l = ieee754dp_tlong(fs);
1832 rfmt = l_fmt;
1833 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 case froundl_op:
1836 case ftruncl_op:
1837 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001838 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001839 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1840 return SIGILL;
1841
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001842 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 DPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001844 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 rv.l = ieee754dp_tlong(fs);
1846 ieee754_csr.rm = oldrm;
1847 rfmt = l_fmt;
1848 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 default:
1851 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1852 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001853 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 DPFROMREG(fs, MIPSInst_FS(ir));
1856 DPFROMREG(ft, MIPSInst_FT(ir));
1857 rv.w = ieee754dp_cmp(fs, ft,
1858 cmptab[cmpop & 0x7], cmpop & 0x8);
1859 rfmt = -1;
1860 if ((cmpop & 0x8)
1861 &&
1862 ieee754_cxtest
1863 (IEEE754_INVALID_OPERATION))
1864 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1865 else
1866 goto copcsr;
1867
1868 }
1869 else {
1870 return SIGILL;
1871 }
1872 break;
1873 }
1874 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001876 case w_fmt:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 switch (MIPSInst_FUNC(ir)) {
1878 case fcvts_op:
1879 /* convert word to single precision real */
1880 SPFROMREG(fs, MIPSInst_FS(ir));
1881 rv.s = ieee754sp_fint(fs.bits);
1882 rfmt = s_fmt;
1883 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 case fcvtd_op:
1885 /* convert word to double precision real */
1886 SPFROMREG(fs, MIPSInst_FS(ir));
1887 rv.d = ieee754dp_fint(fs.bits);
1888 rfmt = d_fmt;
1889 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 default:
1891 return SIGILL;
1892 }
1893 break;
1894 }
1895
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001896 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001897
1898 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1899 return SIGILL;
1900
Paul Burtonbbd426f2014-02-13 11:26:41 +00001901 DIFROMREG(bits, MIPSInst_FS(ir));
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 switch (MIPSInst_FUNC(ir)) {
1904 case fcvts_op:
1905 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001906 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 rfmt = s_fmt;
1908 goto copcsr;
1909 case fcvtd_op:
1910 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001911 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 rfmt = d_fmt;
1913 goto copcsr;
1914 default:
1915 return SIGILL;
1916 }
1917 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 default:
1920 return SIGILL;
1921 }
1922
1923 /*
1924 * Update the fpu CSR register for this operation.
1925 * If an exception is required, generate a tidy SIGFPE exception,
1926 * without updating the result register.
1927 * Note: cause exception bits do not accumulate, they are rewritten
1928 * for each op; only the flag/sticky bits accumulate.
1929 */
1930 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1931 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001932 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return SIGFPE;
1934 }
1935
1936 /*
1937 * Now we can safely write the result back to the register file.
1938 */
1939 switch (rfmt) {
Ralf Baechle08a07902014-04-19 13:11:37 +02001940 case -1:
1941
1942 if (cpu_has_mips_4_5_r)
Rob Kendrickc3b9b942014-07-23 10:03:58 +01001943 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 else
Ralf Baechle08a07902014-04-19 13:11:37 +02001945 cbit = FPU_CSR_COND;
1946 if (rv.w)
1947 ctx->fcr31 |= cbit;
1948 else
1949 ctx->fcr31 &= ~cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 break;
Ralf Baechle08a07902014-04-19 13:11:37 +02001951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 case d_fmt:
1953 DPTOREG(rv.d, MIPSInst_FD(ir));
1954 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 case s_fmt:
1956 SPTOREG(rv.s, MIPSInst_FD(ir));
1957 break;
1958 case w_fmt:
1959 SITOREG(rv.w, MIPSInst_FD(ir));
1960 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001962 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1963 return SIGILL;
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 DITOREG(rv.l, MIPSInst_FD(ir));
1966 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 default:
1968 return SIGILL;
1969 }
1970
1971 return 0;
1972}
1973
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09001974int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001975 int has_fpu, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Ralf Baechle333d1f62005-02-28 17:55:57 +00001977 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001978 struct mm_decoded_insn dec_insn;
1979 u16 instr[4];
1980 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 int sig = 0;
1982
1983 oldepc = xcp->cp0_epc;
1984 do {
1985 prevepc = xcp->cp0_epc;
1986
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001987 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
1988 /*
1989 * Get next 2 microMIPS instructions and convert them
1990 * into 32-bit instructions.
1991 */
1992 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
1993 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
1994 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
1995 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
1996 MIPS_FPU_EMU_INC_STATS(errors);
1997 return SIGBUS;
1998 }
1999 instr_ptr = instr;
2000
2001 /* Get first instruction. */
2002 if (mm_insn_16bit(*instr_ptr)) {
2003 /* Duplicate the half-word. */
2004 dec_insn.insn = (*instr_ptr << 16) |
2005 (*instr_ptr);
2006 /* 16-bit instruction. */
2007 dec_insn.pc_inc = 2;
2008 instr_ptr += 1;
2009 } else {
2010 dec_insn.insn = (*instr_ptr << 16) |
2011 *(instr_ptr+1);
2012 /* 32-bit instruction. */
2013 dec_insn.pc_inc = 4;
2014 instr_ptr += 2;
2015 }
2016 /* Get second instruction. */
2017 if (mm_insn_16bit(*instr_ptr)) {
2018 /* Duplicate the half-word. */
2019 dec_insn.next_insn = (*instr_ptr << 16) |
2020 (*instr_ptr);
2021 /* 16-bit instruction. */
2022 dec_insn.next_pc_inc = 2;
2023 } else {
2024 dec_insn.next_insn = (*instr_ptr << 16) |
2025 *(instr_ptr+1);
2026 /* 32-bit instruction. */
2027 dec_insn.next_pc_inc = 4;
2028 }
2029 dec_insn.micro_mips_mode = 1;
2030 } else {
2031 if ((get_user(dec_insn.insn,
2032 (mips_instruction __user *) xcp->cp0_epc)) ||
2033 (get_user(dec_insn.next_insn,
2034 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2035 MIPS_FPU_EMU_INC_STATS(errors);
2036 return SIGBUS;
2037 }
2038 dec_insn.pc_inc = 4;
2039 dec_insn.next_pc_inc = 4;
2040 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002042
2043 if ((dec_insn.insn == 0) ||
2044 ((dec_insn.pc_inc == 2) &&
2045 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2046 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002048 /*
2049 * The 'ieee754_csr' is an alias of
Ralf Baechle70342282013-01-22 12:59:30 +01002050 * ctx->fcr31. No need to copy ctx->fcr31 to
2051 * ieee754_csr. But ieee754_csr.rm is ieee
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002052 * library modes. (not mips rounding mode)
2053 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002054 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002057 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 break;
2059 if (sig)
2060 break;
2061
2062 cond_resched();
2063 } while (xcp->cp0_epc > prevepc);
2064
2065 /* SIGILL indicates a non-fpu instruction */
2066 if (sig == SIGILL && xcp->cp0_epc != oldepc)
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002067 /* but if EPC has advanced, then ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 sig = 0;
2069
2070 return sig;
2071}