blob: d6d67e2a0434db9c3283bfcc46a57af97cc5421a [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;
651#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 case cop0_op:
653 case cop1_op:
Markos Chandrasc8a34582014-11-26 10:10:18 +0000654 /* Need to check for R6 bc1nez and bc1eqz branches */
655 if (cpu_has_mips_r6 &&
656 ((insn.i_format.rs == bc1eqz_op) ||
657 (insn.i_format.rs == bc1nez_op))) {
658 bit = 0;
659 switch (insn.i_format.rs) {
660 case bc1eqz_op:
661 if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
662 bit = 1;
663 break;
664 case bc1nez_op:
665 if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
666 bit = 1;
667 break;
668 }
669 if (bit)
670 *contpc = regs->cp0_epc +
671 dec_insn.pc_inc +
672 (insn.i_format.simmediate << 2);
673 else
674 *contpc = regs->cp0_epc +
675 dec_insn.pc_inc +
676 dec_insn.next_pc_inc;
677
678 return 1;
679 }
680 /* R2/R6 compatible cop1 instruction. Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 case cop2_op:
682 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500683 if (insn.i_format.rs == bc_op) {
684 preempt_disable();
685 if (is_fpu_owner())
Manuel Lauss842dfc12014-11-07 14:13:54 +0100686 fcr31 = read_32bit_cp1_register(CP1_STATUS);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500687 else
688 fcr31 = current->thread.fpu.fcr31;
689 preempt_enable();
690
691 bit = (insn.i_format.rt >> 2);
692 bit += (bit != 0);
693 bit += 23;
694 switch (insn.i_format.rt & 3) {
695 case 0: /* bc1f */
696 case 2: /* bc1fl */
697 if (~fcr31 & (1 << bit))
698 *contpc = regs->cp0_epc +
699 dec_insn.pc_inc +
700 (insn.i_format.simmediate << 2);
701 else
702 *contpc = regs->cp0_epc +
703 dec_insn.pc_inc +
704 dec_insn.next_pc_inc;
705 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500706 case 1: /* bc1t */
707 case 3: /* bc1tl */
708 if (fcr31 & (1 << bit))
709 *contpc = regs->cp0_epc +
710 dec_insn.pc_inc +
711 (insn.i_format.simmediate << 2);
712 else
713 *contpc = regs->cp0_epc +
714 dec_insn.pc_inc +
715 dec_insn.next_pc_inc;
716 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500717 }
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 break;
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return 0;
722}
723
724/*
725 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100726 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800727 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000728 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200729 * FPU emu is slow and bulky and optimizing this function offers fairly
730 * sizeable benefits so we try to be clever and make this function return
731 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000732 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 */
David Daneyda0bac32009-11-02 11:33:46 -0800734static inline int cop1_64bit(struct pt_regs *xcp)
735{
Ralf Baechle08a07902014-04-19 13:11:37 +0200736 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
737 return 1;
738 else if (config_enabled(CONFIG_32BIT) &&
739 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
740 return 0;
741
Paul Burton597ce172013-11-22 13:12:07 +0000742 return !test_thread_flag(TIF_32BIT_FPREGS);
David Daneyda0bac32009-11-02 11:33:46 -0800743}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Paul Burton4227a2d2014-09-11 08:30:20 +0100745static inline bool hybrid_fprs(void)
746{
747 return test_thread_flag(TIF_HYBRID_FPREGS);
748}
749
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200750#define SIFROMREG(si, x) \
751do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100752 if (cop1_64bit(xcp) && !hybrid_fprs()) \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100753 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000754 else \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100755 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000756} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800757
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200758#define SITOREG(si, x) \
759do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100760 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000761 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000762 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000763 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
764 set_fpr32(&ctx->fpr[x], i, 0); \
765 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000766 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000767 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000768} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Paul Burtonc8c0da62014-09-24 10:45:37 +0100770#define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000771
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200772#define SITOHREG(si, x) \
773do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000774 unsigned i; \
775 set_fpr32(&ctx->fpr[x], 1, si); \
776 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
777 set_fpr32(&ctx->fpr[x], i, 0); \
778} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000779
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200780#define DIFROMREG(di, x) \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000781 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
782
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200783#define DITOREG(di, x) \
784do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000785 unsigned fpr, i; \
786 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
787 set_fpr64(&ctx->fpr[fpr], 0, di); \
788 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
789 set_fpr64(&ctx->fpr[fpr], i, 0); \
790} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Ralf Baechle21a151d2007-10-11 23:46:15 +0100792#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
793#define SPTOREG(sp, x) SITOREG((sp).bits, x)
794#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
795#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797/*
798 * Emulate the single floating point instruction pointed at by EPC.
799 * Two instructions if the instruction is in a branch delay slot.
800 */
801
David Daney515b0292010-10-21 16:32:26 -0700802static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500803 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500805 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200806 unsigned int cond, cbit;
807 mips_instruction ir;
808 int likely, pc_inc;
809 u32 __user *wva;
810 u64 __user *dva;
811 u32 value;
812 u32 wval;
813 u64 dval;
814 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Ralf Baechle70e4c232014-04-30 11:09:44 +0200816 /*
817 * These are giving gcc a gentle hint about what to expect in
818 * dec_inst in order to do better optimization.
819 */
820 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
821 unreachable();
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200824 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500825 if (dec_insn.micro_mips_mode) {
826 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200827 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500828 } else {
829 if (!isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200830 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500831 }
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200834 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /*
836 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +0100837 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 * BEFORE we do the cop1 instruction.
839 *
840 * This branch could be a COP1 branch, but in that case we
841 * would have had a trap for that instruction, and would not
842 * come through this route.
843 *
844 * Linux MIPS branch emulator operates on context, updating the
845 * cp0_epc.
846 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500847 ir = dec_insn.next_insn; /* process delay slot instr */
848 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000849 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500850 ir = dec_insn.insn; /* process current instr */
851 pc_inc = dec_insn.pc_inc;
852 }
853
854 /*
855 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
856 * instructions, we want to convert microMIPS FPU instructions
857 * into MIPS32 instructions so that we could reuse all of the
858 * FPU emulation code.
859 *
860 * NOTE: We cannot do this for branch instructions since they
861 * are not a subset. Example: Cannot emulate a 16-bit
862 * aligned target address with a MIPS32 instruction.
863 */
864 if (dec_insn.micro_mips_mode) {
865 /*
866 * If next instruction is a 16-bit instruction, then it
867 * it cannot be a FPU instruction. This could happen
868 * since we can be called for non-FPU instructions.
869 */
870 if ((pc_inc == 2) ||
871 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
872 == SIGILL))
873 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200876emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200877 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -0800878 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 switch (MIPSInst_OPCODE(ir)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200880 case ldc1_op:
881 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
882 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800883 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -0700884
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200885 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800886 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200887 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return SIGBUS;
889 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200890 if (__get_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700891 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200892 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700893 return SIGSEGV;
894 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200895 DITOREG(dval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200898 case sdc1_op:
899 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
900 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800901 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200902 DIFROMREG(dval, MIPSInst_RT(ir));
903 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800904 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200905 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return SIGBUS;
907 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200908 if (__put_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700909 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200910 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700911 return SIGSEGV;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200915 case lwc1_op:
916 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
917 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800918 MIPS_FPU_EMU_INC_STATS(loads);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200919 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800920 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200921 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return SIGBUS;
923 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200924 if (__get_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -0700925 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200926 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -0700927 return SIGSEGV;
928 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200929 SITOREG(wval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200932 case swc1_op:
933 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
934 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800935 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200936 SIFROMREG(wval, MIPSInst_RT(ir));
937 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800938 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200939 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return SIGBUS;
941 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200942 if (__put_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -0700943 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200944 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -0700945 return SIGSEGV;
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 case cop1_op:
950 switch (MIPSInst_RS(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 case dmfc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +0200952 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
953 return SIGILL;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /* copregister fs -> gpr[rt] */
956 if (MIPSInst_RT(ir) != 0) {
957 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
958 MIPSInst_RD(ir));
959 }
960 break;
961
962 case dmtc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +0200963 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
964 return SIGILL;
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* copregister fs <- rt */
967 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
968 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000970 case mfhc_op:
971 if (!cpu_has_mips_r2)
972 goto sigill;
973
974 /* copregister rd -> gpr[rt] */
975 if (MIPSInst_RT(ir) != 0) {
976 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
977 MIPSInst_RD(ir));
978 }
979 break;
980
981 case mthc_op:
982 if (!cpu_has_mips_r2)
983 goto sigill;
984
985 /* copregister rd <- gpr[rt] */
986 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
987 break;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 case mfc_op:
990 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (MIPSInst_RT(ir) != 0) {
992 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
993 MIPSInst_RD(ir));
994 }
995 break;
996
997 case mtc_op:
998 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1000 break;
1001
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001002 case cfc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /* cop control register rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1005 value = ctx->fcr31;
Ralf Baechle56a64732014-04-30 11:21:55 +02001006 value = (value & ~FPU_CSR_RM) | modeindex(value);
Ralf Baechle92df0f82014-04-19 14:03:37 +02001007 pr_debug("%p gpr[%d]<-csr=%08x\n",
1008 (void *) (xcp->cp0_epc),
1009 MIPSInst_RT(ir), value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011 else if (MIPSInst_RD(ir) == FPCREG_RID)
1012 value = 0;
1013 else
1014 value = 0;
1015 if (MIPSInst_RT(ir))
1016 xcp->regs[MIPSInst_RT(ir)] = value;
1017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001019 case ctc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (MIPSInst_RT(ir) == 0)
1022 value = 0;
1023 else
1024 value = xcp->regs[MIPSInst_RT(ir)];
1025
1026 /* we only have one writable control reg
1027 */
1028 if (MIPSInst_RD(ir) == FPCREG_CSR) {
Ralf Baechle92df0f82014-04-19 14:03:37 +02001029 pr_debug("%p gpr[%d]->csr=%08x\n",
1030 (void *) (xcp->cp0_epc),
1031 MIPSInst_RT(ir), value);
Shane McDonald95e8f632010-05-06 23:26:57 -06001032
1033 /*
1034 * Don't write reserved bits,
1035 * and convert to ieee library modes
1036 */
Ralf Baechle56a64732014-04-30 11:21:55 +02001037 ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
1038 modeindex(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
1040 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1041 return SIGFPE;
1042 }
1043 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001045 case bc_op:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001046 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return SIGILL;
1048
Ralf Baechle08a07902014-04-19 13:11:37 +02001049 if (cpu_has_mips_4_5_r)
1050 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1051 else
1052 cbit = FPU_CSR_COND;
1053 cond = ctx->fcr31 & cbit;
1054
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001055 likely = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 switch (MIPSInst_RT(ir) & 3) {
1057 case bcfl_op:
1058 likely = 1;
1059 case bcf_op:
1060 cond = !cond;
1061 break;
1062 case bctl_op:
1063 likely = 1;
1064 case bct_op:
1065 break;
1066 default:
1067 /* thats an illegal instruction */
1068 return SIGILL;
1069 }
1070
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001071 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (cond) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001073 /*
1074 * Branch taken: emulate dslot instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001076 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001078 contpc = MIPSInst_SIMM(ir);
1079 ir = dec_insn.next_insn;
1080 if (dec_insn.micro_mips_mode) {
1081 contpc = (xcp->cp0_epc + (contpc << 1));
1082
1083 /* If 16-bit instruction, not FPU. */
1084 if ((dec_insn.next_pc_inc == 2) ||
1085 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1086
1087 /*
1088 * Since this instruction will
1089 * be put on the stack with
1090 * 32-bit words, get around
1091 * this problem by putting a
1092 * NOP16 as the second one.
1093 */
1094 if (dec_insn.next_pc_inc == 2)
1095 ir = (ir & (~0xffff)) | MM_NOP16;
1096
1097 /*
1098 * Single step the non-CP1
1099 * instruction in the dslot.
1100 */
1101 return mips_dsemul(xcp, ir, contpc);
1102 }
1103 } else
1104 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 switch (MIPSInst_OPCODE(ir)) {
1107 case lwc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001108 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 case swc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001111 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 case ldc1_op:
1114 case sdc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001115 if (cpu_has_mips_2_3_4_5 ||
1116 cpu_has_mips64)
1117 goto emul;
1118
1119 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001121
Ralf Baechle08a07902014-04-19 13:11:37 +02001122 case cop1_op:
1123 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001124
Ralf Baechle08a07902014-04-19 13:11:37 +02001125 case cop1x_op:
Markos Chandrasa5466d72014-10-21 10:21:54 +01001126 if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
Ralf Baechle08a07902014-04-19 13:11:37 +02001127 /* its one of ours */
1128 goto emul;
1129
1130 return SIGILL;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001133 if (!cpu_has_mips_4_5_r)
1134 return SIGILL;
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (MIPSInst_FUNC(ir) == movc_op)
1137 goto emul;
1138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
1141 /*
1142 * Single step the non-cp1
1143 * instruction in the dslot
1144 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001145 return mips_dsemul(xcp, ir, contpc);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001146 } else if (likely) { /* branch not taken */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /*
1148 * branch likely nullifies
1149 * dslot if not taken
1150 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001151 xcp->cp0_epc += dec_insn.pc_inc;
1152 contpc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * else continue & execute
1155 * dslot as normal insn
1156 */
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 default:
1161 if (!(MIPSInst_RS(ir) & 0x10))
1162 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001164 /* a real fpu computation instruction */
1165 if ((sig = fpu_emu(xcp, ctx, ir)))
1166 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 break;
1169
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001170 case cop1x_op:
Markos Chandrasa5466d72014-10-21 10:21:54 +01001171 if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
Ralf Baechle08a07902014-04-19 13:11:37 +02001172 return SIGILL;
1173
1174 sig = fpux_emu(xcp, ctx, ir, fault_addr);
David Daney515b0292010-10-21 16:32:26 -07001175 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return sig;
1177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
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 return SIGILL;
1185 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1186 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1187 xcp->regs[MIPSInst_RD(ir)] =
1188 xcp->regs[MIPSInst_RS(ir)];
1189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 default:
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001191sigill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return SIGILL;
1193 }
1194
1195 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001196 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001197 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return 0;
1200}
1201
1202/*
1203 * Conversion table from MIPS compare ops 48-63
1204 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1205 */
1206static const unsigned char cmptab[8] = {
1207 0, /* cmp_0 (sig) cmp_sf */
1208 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1209 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1210 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1211 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1212 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1213 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1214 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1215};
1216
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/*
1219 * Additional MIPS4 instructions
1220 */
1221
Ralf Baechle47fa0c02014-04-16 11:00:12 +02001222#define DEF3OP(name, p, f1, f2, f3) \
1223static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1224 union ieee754##p s, union ieee754##p t) \
1225{ \
1226 struct _ieee754_csr ieee754_csr_save; \
1227 s = f1(s, t); \
1228 ieee754_csr_save = ieee754_csr; \
1229 s = f2(s, r); \
1230 ieee754_csr_save.cx |= ieee754_csr.cx; \
1231 ieee754_csr_save.sx |= ieee754_csr.sx; \
1232 s = f3(s); \
1233 ieee754_csr.cx |= ieee754_csr_save.cx; \
1234 ieee754_csr.sx |= ieee754_csr_save.sx; \
1235 return s; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001238static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 return ieee754dp_div(ieee754dp_one(0), d);
1241}
1242
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001243static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1246}
1247
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001248static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 return ieee754sp_div(ieee754sp_one(0), s);
1251}
1252
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001253static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1256}
1257
Ralf Baechle21a151d2007-10-11 23:46:15 +01001258DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1259DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1261DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001262DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1263DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1265DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1266
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001267static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001268 mips_instruction ir, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 unsigned rcsr = 0; /* resulting csr */
1271
David Daneyb6ee75e2009-11-05 11:34:26 -08001272 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 switch (MIPSInst_FMA_FFMT(ir)) {
1275 case s_fmt:{ /* 0 */
1276
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001277 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1278 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001279 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 u32 val;
1281
1282 switch (MIPSInst_FUNC(ir)) {
1283 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001284 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 xcp->regs[MIPSInst_FT(ir)]);
1286
David Daneyb6ee75e2009-11-05 11:34:26 -08001287 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001288 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001289 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001290 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return SIGBUS;
1292 }
David Daney515b0292010-10-21 16:32:26 -07001293 if (__get_user(val, va)) {
1294 MIPS_FPU_EMU_INC_STATS(errors);
1295 *fault_addr = va;
1296 return SIGSEGV;
1297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 SITOREG(val, MIPSInst_FD(ir));
1299 break;
1300
1301 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001302 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 xcp->regs[MIPSInst_FT(ir)]);
1304
David Daneyb6ee75e2009-11-05 11:34:26 -08001305 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001308 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1309 MIPS_FPU_EMU_INC_STATS(errors);
1310 *fault_addr = va;
1311 return SIGBUS;
1312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001314 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001315 *fault_addr = va;
1316 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 break;
1319
1320 case madd_s_op:
1321 handler = fpemu_sp_madd;
1322 goto scoptop;
1323 case msub_s_op:
1324 handler = fpemu_sp_msub;
1325 goto scoptop;
1326 case nmadd_s_op:
1327 handler = fpemu_sp_nmadd;
1328 goto scoptop;
1329 case nmsub_s_op:
1330 handler = fpemu_sp_nmsub;
1331 goto scoptop;
1332
1333 scoptop:
1334 SPFROMREG(fr, MIPSInst_FR(ir));
1335 SPFROMREG(fs, MIPSInst_FS(ir));
1336 SPFROMREG(ft, MIPSInst_FT(ir));
1337 fd = (*handler) (fr, fs, ft);
1338 SPTOREG(fd, MIPSInst_FD(ir));
1339
1340 copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001341 if (ieee754_cxtest(IEEE754_INEXACT)) {
1342 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001344 }
1345 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1346 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001348 }
1349 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1350 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001352 }
1353 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1354 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001360 /*printk ("SIGFPE: FPU csr = %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 ctx->fcr31); */
1362 return SIGFPE;
1363 }
1364
1365 break;
1366
1367 default:
1368 return SIGILL;
1369 }
1370 break;
1371 }
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001374 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1375 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001376 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 u64 val;
1378
1379 switch (MIPSInst_FUNC(ir)) {
1380 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001381 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 xcp->regs[MIPSInst_FT(ir)]);
1383
David Daneyb6ee75e2009-11-05 11:34:26 -08001384 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001385 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001386 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001387 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return SIGBUS;
1389 }
David Daney515b0292010-10-21 16:32:26 -07001390 if (__get_user(val, va)) {
1391 MIPS_FPU_EMU_INC_STATS(errors);
1392 *fault_addr = va;
1393 return SIGSEGV;
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 DITOREG(val, MIPSInst_FD(ir));
1396 break;
1397
1398 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001399 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 xcp->regs[MIPSInst_FT(ir)]);
1401
David Daneyb6ee75e2009-11-05 11:34:26 -08001402 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001404 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001405 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001406 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return SIGBUS;
1408 }
David Daney515b0292010-10-21 16:32:26 -07001409 if (__put_user(val, va)) {
1410 MIPS_FPU_EMU_INC_STATS(errors);
1411 *fault_addr = va;
1412 return SIGSEGV;
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 break;
1415
1416 case madd_d_op:
1417 handler = fpemu_dp_madd;
1418 goto dcoptop;
1419 case msub_d_op:
1420 handler = fpemu_dp_msub;
1421 goto dcoptop;
1422 case nmadd_d_op:
1423 handler = fpemu_dp_nmadd;
1424 goto dcoptop;
1425 case nmsub_d_op:
1426 handler = fpemu_dp_nmsub;
1427 goto dcoptop;
1428
1429 dcoptop:
1430 DPFROMREG(fr, MIPSInst_FR(ir));
1431 DPFROMREG(fs, MIPSInst_FS(ir));
1432 DPFROMREG(ft, MIPSInst_FT(ir));
1433 fd = (*handler) (fr, fs, ft);
1434 DPTOREG(fd, MIPSInst_FD(ir));
1435 goto copcsr;
1436
1437 default:
1438 return SIGILL;
1439 }
1440 break;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001443 case 0x3:
1444 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* ignore prefx operation */
1448 break;
1449
1450 default:
1451 return SIGILL;
1452 }
1453
1454 return 0;
1455}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457
1458
1459/*
1460 * Emulate a single COP1 arithmetic instruction.
1461 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001462static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 mips_instruction ir)
1464{
1465 int rfmt; /* resulting format */
1466 unsigned rcsr = 0; /* resulting csr */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001467 unsigned int oldrm;
1468 unsigned int cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 unsigned cond;
1470 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001471 union ieee754dp d;
1472 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 s64 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 } rv; /* resulting value */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001476 u64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
David Daneyb6ee75e2009-11-05 11:34:26 -08001478 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001480 case s_fmt: { /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001482 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1483 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 } handler;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001485 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 switch (MIPSInst_FUNC(ir)) {
1488 /* binary ops */
1489 case fadd_op:
1490 handler.b = ieee754sp_add;
1491 goto scopbop;
1492 case fsub_op:
1493 handler.b = ieee754sp_sub;
1494 goto scopbop;
1495 case fmul_op:
1496 handler.b = ieee754sp_mul;
1497 goto scopbop;
1498 case fdiv_op:
1499 handler.b = ieee754sp_div;
1500 goto scopbop;
1501
1502 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001504 if (!cpu_has_mips_4_5_r)
1505 return SIGILL;
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 handler.u = ieee754sp_sqrt;
1508 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001509
Ralf Baechle08a07902014-04-19 13:11:37 +02001510 /*
1511 * Note that on some MIPS IV implementations such as the
1512 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1513 * achieve full IEEE-754 accuracy - however this emulator does.
1514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001516 if (!cpu_has_mips_4_5_r2)
1517 return SIGILL;
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 handler.u = fpemu_sp_rsqrt;
1520 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001523 if (!cpu_has_mips_4_5_r2)
1524 return SIGILL;
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 handler.u = fpemu_sp_recip;
1527 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001530 if (!cpu_has_mips_4_5_r)
1531 return SIGILL;
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1534 if (((ctx->fcr31 & cond) != 0) !=
1535 ((MIPSInst_FT(ir) & 1) != 0))
1536 return 0;
1537 SPFROMREG(rv.s, MIPSInst_FS(ir));
1538 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001541 if (!cpu_has_mips_4_5_r)
1542 return SIGILL;
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1545 return 0;
1546 SPFROMREG(rv.s, MIPSInst_FS(ir));
1547 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001550 if (!cpu_has_mips_4_5_r)
1551 return SIGILL;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1554 return 0;
1555 SPFROMREG(rv.s, MIPSInst_FS(ir));
1556 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 case fabs_op:
1559 handler.u = ieee754sp_abs;
1560 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 case fneg_op:
1563 handler.u = ieee754sp_neg;
1564 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 case fmov_op:
1567 /* an easy one */
1568 SPFROMREG(rv.s, MIPSInst_FS(ir));
1569 goto copcsr;
1570
1571 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001572scopbop:
1573 SPFROMREG(fs, MIPSInst_FS(ir));
1574 SPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001576 rv.s = (*handler.b) (fs, ft);
1577 goto copcsr;
1578scopuop:
1579 SPFROMREG(fs, MIPSInst_FS(ir));
1580 rv.s = (*handler.u) (fs);
1581 goto copcsr;
1582copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001583 if (ieee754_cxtest(IEEE754_INEXACT)) {
1584 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001586 }
1587 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1588 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001590 }
1591 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1592 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001594 }
1595 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1596 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001598 }
1599 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1600 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 break;
1604
1605 /* unary conv ops */
1606 case fcvts_op:
1607 return SIGILL; /* not defined */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001609 case fcvtd_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 SPFROMREG(fs, MIPSInst_FS(ir));
1611 rv.d = ieee754dp_fsp(fs);
1612 rfmt = d_fmt;
1613 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001615 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 SPFROMREG(fs, MIPSInst_FS(ir));
1617 rv.w = ieee754sp_tint(fs);
1618 rfmt = w_fmt;
1619 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 case fround_op:
1622 case ftrunc_op:
1623 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001624 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001625 if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
1626 return SIGILL;
1627
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001628 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 SPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001630 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 rv.w = ieee754sp_tint(fs);
1632 ieee754_csr.rm = oldrm;
1633 rfmt = w_fmt;
1634 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001636 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001637 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1638 return SIGILL;
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 SPFROMREG(fs, MIPSInst_FS(ir));
1641 rv.l = ieee754sp_tlong(fs);
1642 rfmt = l_fmt;
1643 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 case froundl_op:
1646 case ftruncl_op:
1647 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001648 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001649 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1650 return SIGILL;
1651
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001652 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 SPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001654 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 rv.l = ieee754sp_tlong(fs);
1656 ieee754_csr.rm = oldrm;
1657 rfmt = l_fmt;
1658 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 default:
1661 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1662 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001663 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 SPFROMREG(fs, MIPSInst_FS(ir));
1666 SPFROMREG(ft, MIPSInst_FT(ir));
1667 rv.w = ieee754sp_cmp(fs, ft,
1668 cmptab[cmpop & 0x7], cmpop & 0x8);
1669 rfmt = -1;
1670 if ((cmpop & 0x8) && ieee754_cxtest
1671 (IEEE754_INVALID_OPERATION))
1672 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1673 else
1674 goto copcsr;
1675
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001676 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 break;
1679 }
1680 break;
1681 }
1682
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001683 case d_fmt: {
1684 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001686 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1687 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 } handler;
1689
1690 switch (MIPSInst_FUNC(ir)) {
1691 /* binary ops */
1692 case fadd_op:
1693 handler.b = ieee754dp_add;
1694 goto dcopbop;
1695 case fsub_op:
1696 handler.b = ieee754dp_sub;
1697 goto dcopbop;
1698 case fmul_op:
1699 handler.b = ieee754dp_mul;
1700 goto dcopbop;
1701 case fdiv_op:
1702 handler.b = ieee754dp_div;
1703 goto dcopbop;
1704
1705 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001707 if (!cpu_has_mips_2_3_4_5_r)
1708 return SIGILL;
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 handler.u = ieee754dp_sqrt;
1711 goto dcopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001712 /*
1713 * Note that on some MIPS IV implementations such as the
1714 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1715 * achieve full IEEE-754 accuracy - however this emulator does.
1716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001718 if (!cpu_has_mips_4_5_r2)
1719 return SIGILL;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 handler.u = fpemu_dp_rsqrt;
1722 goto dcopuop;
1723 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001724 if (!cpu_has_mips_4_5_r2)
1725 return SIGILL;
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 handler.u = fpemu_dp_recip;
1728 goto dcopuop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001730 if (!cpu_has_mips_4_5_r)
1731 return SIGILL;
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1734 if (((ctx->fcr31 & cond) != 0) !=
1735 ((MIPSInst_FT(ir) & 1) != 0))
1736 return 0;
1737 DPFROMREG(rv.d, MIPSInst_FS(ir));
1738 break;
1739 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001740 if (!cpu_has_mips_4_5_r)
1741 return SIGILL;
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1744 return 0;
1745 DPFROMREG(rv.d, MIPSInst_FS(ir));
1746 break;
1747 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001748 if (!cpu_has_mips_4_5_r)
1749 return SIGILL;
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1752 return 0;
1753 DPFROMREG(rv.d, MIPSInst_FS(ir));
1754 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 case fabs_op:
1756 handler.u = ieee754dp_abs;
1757 goto dcopuop;
1758
1759 case fneg_op:
1760 handler.u = ieee754dp_neg;
1761 goto dcopuop;
1762
1763 case fmov_op:
1764 /* an easy one */
1765 DPFROMREG(rv.d, MIPSInst_FS(ir));
1766 goto copcsr;
1767
1768 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001769dcopbop:
1770 DPFROMREG(fs, MIPSInst_FS(ir));
1771 DPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001773 rv.d = (*handler.b) (fs, ft);
1774 goto copcsr;
1775dcopuop:
1776 DPFROMREG(fs, MIPSInst_FS(ir));
1777 rv.d = (*handler.u) (fs);
1778 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001780 /*
1781 * unary conv ops
1782 */
1783 case fcvts_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 DPFROMREG(fs, MIPSInst_FS(ir));
1785 rv.s = ieee754sp_fdp(fs);
1786 rfmt = s_fmt;
1787 goto copcsr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 case fcvtd_op:
1790 return SIGILL; /* not defined */
1791
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001792 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 DPFROMREG(fs, MIPSInst_FS(ir));
1794 rv.w = ieee754dp_tint(fs); /* wrong */
1795 rfmt = w_fmt;
1796 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 case fround_op:
1799 case ftrunc_op:
1800 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001801 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001802 if (!cpu_has_mips_2_3_4_5_r)
1803 return SIGILL;
1804
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001805 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 DPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001807 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 rv.w = ieee754dp_tint(fs);
1809 ieee754_csr.rm = oldrm;
1810 rfmt = w_fmt;
1811 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001813 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001814 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1815 return SIGILL;
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 DPFROMREG(fs, MIPSInst_FS(ir));
1818 rv.l = ieee754dp_tlong(fs);
1819 rfmt = l_fmt;
1820 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 case froundl_op:
1823 case ftruncl_op:
1824 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001825 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001826 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1827 return SIGILL;
1828
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001829 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 DPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001831 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 rv.l = ieee754dp_tlong(fs);
1833 ieee754_csr.rm = oldrm;
1834 rfmt = l_fmt;
1835 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 default:
1838 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1839 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001840 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 DPFROMREG(fs, MIPSInst_FS(ir));
1843 DPFROMREG(ft, MIPSInst_FT(ir));
1844 rv.w = ieee754dp_cmp(fs, ft,
1845 cmptab[cmpop & 0x7], cmpop & 0x8);
1846 rfmt = -1;
1847 if ((cmpop & 0x8)
1848 &&
1849 ieee754_cxtest
1850 (IEEE754_INVALID_OPERATION))
1851 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1852 else
1853 goto copcsr;
1854
1855 }
1856 else {
1857 return SIGILL;
1858 }
1859 break;
1860 }
1861 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001863 case w_fmt:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 switch (MIPSInst_FUNC(ir)) {
1865 case fcvts_op:
1866 /* convert word to single precision real */
1867 SPFROMREG(fs, MIPSInst_FS(ir));
1868 rv.s = ieee754sp_fint(fs.bits);
1869 rfmt = s_fmt;
1870 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 case fcvtd_op:
1872 /* convert word to double precision real */
1873 SPFROMREG(fs, MIPSInst_FS(ir));
1874 rv.d = ieee754dp_fint(fs.bits);
1875 rfmt = d_fmt;
1876 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 default:
1878 return SIGILL;
1879 }
1880 break;
1881 }
1882
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001883 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001884
1885 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1886 return SIGILL;
1887
Paul Burtonbbd426f2014-02-13 11:26:41 +00001888 DIFROMREG(bits, MIPSInst_FS(ir));
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 switch (MIPSInst_FUNC(ir)) {
1891 case fcvts_op:
1892 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001893 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 rfmt = s_fmt;
1895 goto copcsr;
1896 case fcvtd_op:
1897 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001898 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 rfmt = d_fmt;
1900 goto copcsr;
1901 default:
1902 return SIGILL;
1903 }
1904 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 default:
1907 return SIGILL;
1908 }
1909
1910 /*
1911 * Update the fpu CSR register for this operation.
1912 * If an exception is required, generate a tidy SIGFPE exception,
1913 * without updating the result register.
1914 * Note: cause exception bits do not accumulate, they are rewritten
1915 * for each op; only the flag/sticky bits accumulate.
1916 */
1917 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1918 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001919 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return SIGFPE;
1921 }
1922
1923 /*
1924 * Now we can safely write the result back to the register file.
1925 */
1926 switch (rfmt) {
Ralf Baechle08a07902014-04-19 13:11:37 +02001927 case -1:
1928
1929 if (cpu_has_mips_4_5_r)
Rob Kendrickc3b9b942014-07-23 10:03:58 +01001930 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 else
Ralf Baechle08a07902014-04-19 13:11:37 +02001932 cbit = FPU_CSR_COND;
1933 if (rv.w)
1934 ctx->fcr31 |= cbit;
1935 else
1936 ctx->fcr31 &= ~cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 break;
Ralf Baechle08a07902014-04-19 13:11:37 +02001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 case d_fmt:
1940 DPTOREG(rv.d, MIPSInst_FD(ir));
1941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 case s_fmt:
1943 SPTOREG(rv.s, MIPSInst_FD(ir));
1944 break;
1945 case w_fmt:
1946 SITOREG(rv.w, MIPSInst_FD(ir));
1947 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001949 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1950 return SIGILL;
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 DITOREG(rv.l, MIPSInst_FD(ir));
1953 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 default:
1955 return SIGILL;
1956 }
1957
1958 return 0;
1959}
1960
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09001961int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001962 int has_fpu, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
Ralf Baechle333d1f62005-02-28 17:55:57 +00001964 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001965 struct mm_decoded_insn dec_insn;
1966 u16 instr[4];
1967 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 int sig = 0;
1969
1970 oldepc = xcp->cp0_epc;
1971 do {
1972 prevepc = xcp->cp0_epc;
1973
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001974 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
1975 /*
1976 * Get next 2 microMIPS instructions and convert them
1977 * into 32-bit instructions.
1978 */
1979 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
1980 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
1981 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
1982 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
1983 MIPS_FPU_EMU_INC_STATS(errors);
1984 return SIGBUS;
1985 }
1986 instr_ptr = instr;
1987
1988 /* Get first instruction. */
1989 if (mm_insn_16bit(*instr_ptr)) {
1990 /* Duplicate the half-word. */
1991 dec_insn.insn = (*instr_ptr << 16) |
1992 (*instr_ptr);
1993 /* 16-bit instruction. */
1994 dec_insn.pc_inc = 2;
1995 instr_ptr += 1;
1996 } else {
1997 dec_insn.insn = (*instr_ptr << 16) |
1998 *(instr_ptr+1);
1999 /* 32-bit instruction. */
2000 dec_insn.pc_inc = 4;
2001 instr_ptr += 2;
2002 }
2003 /* Get second instruction. */
2004 if (mm_insn_16bit(*instr_ptr)) {
2005 /* Duplicate the half-word. */
2006 dec_insn.next_insn = (*instr_ptr << 16) |
2007 (*instr_ptr);
2008 /* 16-bit instruction. */
2009 dec_insn.next_pc_inc = 2;
2010 } else {
2011 dec_insn.next_insn = (*instr_ptr << 16) |
2012 *(instr_ptr+1);
2013 /* 32-bit instruction. */
2014 dec_insn.next_pc_inc = 4;
2015 }
2016 dec_insn.micro_mips_mode = 1;
2017 } else {
2018 if ((get_user(dec_insn.insn,
2019 (mips_instruction __user *) xcp->cp0_epc)) ||
2020 (get_user(dec_insn.next_insn,
2021 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2022 MIPS_FPU_EMU_INC_STATS(errors);
2023 return SIGBUS;
2024 }
2025 dec_insn.pc_inc = 4;
2026 dec_insn.next_pc_inc = 4;
2027 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002029
2030 if ((dec_insn.insn == 0) ||
2031 ((dec_insn.pc_inc == 2) &&
2032 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2033 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002035 /*
2036 * The 'ieee754_csr' is an alias of
Ralf Baechle70342282013-01-22 12:59:30 +01002037 * ctx->fcr31. No need to copy ctx->fcr31 to
2038 * ieee754_csr. But ieee754_csr.rm is ieee
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002039 * library modes. (not mips rounding mode)
2040 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002041 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002044 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 break;
2046 if (sig)
2047 break;
2048
2049 cond_resched();
2050 } while (xcp->cp0_epc > prevepc);
2051
2052 /* SIGILL indicates a non-fpu instruction */
2053 if (sig == SIGILL && xcp->cp0_epc != oldepc)
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002054 /* but if EPC has advanced, then ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 sig = 0;
2056
2057 return sig;
2058}