blob: c115d969664bc9056562a81aac8618cb68bdb270 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002 * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * MIPS floating point support
5 * Copyright (C) 1994-2000 Algorithmics Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
8 * Copyright (C) 2000 MIPS Technologies, Inc.
9 *
10 * This program is free software; you can distribute it and/or modify it
11 * under the terms of the GNU General Public License (Version 2) as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
Ralf Baechle3f7cac42014-04-26 01:49:14 +020021 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
23 * A complete emulator for MIPS coprocessor 1 instructions. This is
24 * required for #float(switch) or #float(trap), where it catches all
25 * COP1 instructions via the "CoProcessor Unusable" exception.
26 *
27 * More surprisingly it is also required for #float(ieee), to help out
Ralf Baechle3f7cac42014-04-26 01:49:14 +020028 * the hardware FPU at the boundaries of the IEEE-754 representation
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * (denormalised values, infinities, underflow, etc). It is made
30 * quite nasty because emulation of some non-COP1 instructions is
31 * required, e.g. in branch delay slots.
32 *
Ralf Baechle3f7cac42014-04-26 01:49:14 +020033 * Note if you know that you won't have an FPU, then you'll get much
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * better performance by compiling with -msoft-float!
35 */
36#include <linux/sched.h>
Atsushi Nemoto83fd38c2007-07-07 23:21:49 +090037#include <linux/debugfs.h>
Ralf Baechle08a07902014-04-19 13:11:37 +020038#include <linux/kconfig.h>
Ralf Baechle85c51c52014-04-16 02:46:11 +020039#include <linux/percpu-defs.h>
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +080040#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ralf Baechlecd8ee342014-04-16 02:09:53 +020042#include <asm/branch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/inst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/ptrace.h>
45#include <asm/signal.h>
Ralf Baechlecd8ee342014-04-16 02:09:53 +020046#include <asm/uaccess.h>
47
48#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/fpu_emulator.h>
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050050#include <asm/fpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include "ieee754.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Function which emulates a floating point instruction. */
55
Atsushi Nemotoeae89072006-05-16 01:26:03 +090056static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 mips_instruction);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int fpux_emu(struct pt_regs *,
David Daney515b0292010-10-21 16:32:26 -070060 struct mips_fpu_struct *, mips_instruction, void *__user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* Control registers */
63
64#define FPCREG_RID 0 /* $0 = revision id */
65#define FPCREG_CSR 31 /* $31 = csr */
66
Shane McDonald95e8f632010-05-06 23:26:57 -060067/* Determine rounding mode from the RM bits of the FCSR */
68#define modeindex(v) ((v) & FPU_CSR_RM)
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* convert condition code register number to csr bit */
71static const unsigned int fpucondbit[8] = {
72 FPU_CSR_COND0,
73 FPU_CSR_COND1,
74 FPU_CSR_COND2,
75 FPU_CSR_COND3,
76 FPU_CSR_COND4,
77 FPU_CSR_COND5,
78 FPU_CSR_COND6,
79 FPU_CSR_COND7
80};
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050082/* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
83static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
84static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
85static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
86static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
87
88/*
89 * This functions translates a 32-bit microMIPS instruction
90 * into a 32-bit MIPS32 instruction. Returns 0 on success
91 * and SIGILL otherwise.
92 */
93static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
94{
95 union mips_instruction insn = *insn_ptr;
96 union mips_instruction mips32_insn = insn;
97 int func, fmt, op;
98
99 switch (insn.mm_i_format.opcode) {
100 case mm_ldc132_op:
101 mips32_insn.mm_i_format.opcode = ldc1_op;
102 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
103 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
104 break;
105 case mm_lwc132_op:
106 mips32_insn.mm_i_format.opcode = lwc1_op;
107 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
108 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
109 break;
110 case mm_sdc132_op:
111 mips32_insn.mm_i_format.opcode = sdc1_op;
112 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
113 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
114 break;
115 case mm_swc132_op:
116 mips32_insn.mm_i_format.opcode = swc1_op;
117 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
118 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
119 break;
120 case mm_pool32i_op:
121 /* NOTE: offset is << by 1 if in microMIPS mode. */
122 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
123 (insn.mm_i_format.rt == mm_bc1t_op)) {
124 mips32_insn.fb_format.opcode = cop1_op;
125 mips32_insn.fb_format.bc = bc_op;
126 mips32_insn.fb_format.flag =
127 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
128 } else
129 return SIGILL;
130 break;
131 case mm_pool32f_op:
132 switch (insn.mm_fp0_format.func) {
133 case mm_32f_01_op:
134 case mm_32f_11_op:
135 case mm_32f_02_op:
136 case mm_32f_12_op:
137 case mm_32f_41_op:
138 case mm_32f_51_op:
139 case mm_32f_42_op:
140 case mm_32f_52_op:
141 op = insn.mm_fp0_format.func;
142 if (op == mm_32f_01_op)
143 func = madd_s_op;
144 else if (op == mm_32f_11_op)
145 func = madd_d_op;
146 else if (op == mm_32f_02_op)
147 func = nmadd_s_op;
148 else if (op == mm_32f_12_op)
149 func = nmadd_d_op;
150 else if (op == mm_32f_41_op)
151 func = msub_s_op;
152 else if (op == mm_32f_51_op)
153 func = msub_d_op;
154 else if (op == mm_32f_42_op)
155 func = nmsub_s_op;
156 else
157 func = nmsub_d_op;
158 mips32_insn.fp6_format.opcode = cop1x_op;
159 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
160 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
161 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
162 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
163 mips32_insn.fp6_format.func = func;
164 break;
165 case mm_32f_10_op:
166 func = -1; /* Invalid */
167 op = insn.mm_fp5_format.op & 0x7;
168 if (op == mm_ldxc1_op)
169 func = ldxc1_op;
170 else if (op == mm_sdxc1_op)
171 func = sdxc1_op;
172 else if (op == mm_lwxc1_op)
173 func = lwxc1_op;
174 else if (op == mm_swxc1_op)
175 func = swxc1_op;
176
177 if (func != -1) {
178 mips32_insn.r_format.opcode = cop1x_op;
179 mips32_insn.r_format.rs =
180 insn.mm_fp5_format.base;
181 mips32_insn.r_format.rt =
182 insn.mm_fp5_format.index;
183 mips32_insn.r_format.rd = 0;
184 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
185 mips32_insn.r_format.func = func;
186 } else
187 return SIGILL;
188 break;
189 case mm_32f_40_op:
190 op = -1; /* Invalid */
191 if (insn.mm_fp2_format.op == mm_fmovt_op)
192 op = 1;
193 else if (insn.mm_fp2_format.op == mm_fmovf_op)
194 op = 0;
195 if (op != -1) {
196 mips32_insn.fp0_format.opcode = cop1_op;
197 mips32_insn.fp0_format.fmt =
198 sdps_format[insn.mm_fp2_format.fmt];
199 mips32_insn.fp0_format.ft =
200 (insn.mm_fp2_format.cc<<2) + op;
201 mips32_insn.fp0_format.fs =
202 insn.mm_fp2_format.fs;
203 mips32_insn.fp0_format.fd =
204 insn.mm_fp2_format.fd;
205 mips32_insn.fp0_format.func = fmovc_op;
206 } else
207 return SIGILL;
208 break;
209 case mm_32f_60_op:
210 func = -1; /* Invalid */
211 if (insn.mm_fp0_format.op == mm_fadd_op)
212 func = fadd_op;
213 else if (insn.mm_fp0_format.op == mm_fsub_op)
214 func = fsub_op;
215 else if (insn.mm_fp0_format.op == mm_fmul_op)
216 func = fmul_op;
217 else if (insn.mm_fp0_format.op == mm_fdiv_op)
218 func = fdiv_op;
219 if (func != -1) {
220 mips32_insn.fp0_format.opcode = cop1_op;
221 mips32_insn.fp0_format.fmt =
222 sdps_format[insn.mm_fp0_format.fmt];
223 mips32_insn.fp0_format.ft =
224 insn.mm_fp0_format.ft;
225 mips32_insn.fp0_format.fs =
226 insn.mm_fp0_format.fs;
227 mips32_insn.fp0_format.fd =
228 insn.mm_fp0_format.fd;
229 mips32_insn.fp0_format.func = func;
230 } else
231 return SIGILL;
232 break;
233 case mm_32f_70_op:
234 func = -1; /* Invalid */
235 if (insn.mm_fp0_format.op == mm_fmovn_op)
236 func = fmovn_op;
237 else if (insn.mm_fp0_format.op == mm_fmovz_op)
238 func = fmovz_op;
239 if (func != -1) {
240 mips32_insn.fp0_format.opcode = cop1_op;
241 mips32_insn.fp0_format.fmt =
242 sdps_format[insn.mm_fp0_format.fmt];
243 mips32_insn.fp0_format.ft =
244 insn.mm_fp0_format.ft;
245 mips32_insn.fp0_format.fs =
246 insn.mm_fp0_format.fs;
247 mips32_insn.fp0_format.fd =
248 insn.mm_fp0_format.fd;
249 mips32_insn.fp0_format.func = func;
250 } else
251 return SIGILL;
252 break;
253 case mm_32f_73_op: /* POOL32FXF */
254 switch (insn.mm_fp1_format.op) {
255 case mm_movf0_op:
256 case mm_movf1_op:
257 case mm_movt0_op:
258 case mm_movt1_op:
259 if ((insn.mm_fp1_format.op & 0x7f) ==
260 mm_movf0_op)
261 op = 0;
262 else
263 op = 1;
264 mips32_insn.r_format.opcode = spec_op;
265 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
266 mips32_insn.r_format.rt =
267 (insn.mm_fp4_format.cc << 2) + op;
268 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
269 mips32_insn.r_format.re = 0;
270 mips32_insn.r_format.func = movc_op;
271 break;
272 case mm_fcvtd0_op:
273 case mm_fcvtd1_op:
274 case mm_fcvts0_op:
275 case mm_fcvts1_op:
276 if ((insn.mm_fp1_format.op & 0x7f) ==
277 mm_fcvtd0_op) {
278 func = fcvtd_op;
279 fmt = swl_format[insn.mm_fp3_format.fmt];
280 } else {
281 func = fcvts_op;
282 fmt = dwl_format[insn.mm_fp3_format.fmt];
283 }
284 mips32_insn.fp0_format.opcode = cop1_op;
285 mips32_insn.fp0_format.fmt = fmt;
286 mips32_insn.fp0_format.ft = 0;
287 mips32_insn.fp0_format.fs =
288 insn.mm_fp3_format.fs;
289 mips32_insn.fp0_format.fd =
290 insn.mm_fp3_format.rt;
291 mips32_insn.fp0_format.func = func;
292 break;
293 case mm_fmov0_op:
294 case mm_fmov1_op:
295 case mm_fabs0_op:
296 case mm_fabs1_op:
297 case mm_fneg0_op:
298 case mm_fneg1_op:
299 if ((insn.mm_fp1_format.op & 0x7f) ==
300 mm_fmov0_op)
301 func = fmov_op;
302 else if ((insn.mm_fp1_format.op & 0x7f) ==
303 mm_fabs0_op)
304 func = fabs_op;
305 else
306 func = fneg_op;
307 mips32_insn.fp0_format.opcode = cop1_op;
308 mips32_insn.fp0_format.fmt =
309 sdps_format[insn.mm_fp3_format.fmt];
310 mips32_insn.fp0_format.ft = 0;
311 mips32_insn.fp0_format.fs =
312 insn.mm_fp3_format.fs;
313 mips32_insn.fp0_format.fd =
314 insn.mm_fp3_format.rt;
315 mips32_insn.fp0_format.func = func;
316 break;
317 case mm_ffloorl_op:
318 case mm_ffloorw_op:
319 case mm_fceill_op:
320 case mm_fceilw_op:
321 case mm_ftruncl_op:
322 case mm_ftruncw_op:
323 case mm_froundl_op:
324 case mm_froundw_op:
325 case mm_fcvtl_op:
326 case mm_fcvtw_op:
327 if (insn.mm_fp1_format.op == mm_ffloorl_op)
328 func = ffloorl_op;
329 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
330 func = ffloor_op;
331 else if (insn.mm_fp1_format.op == mm_fceill_op)
332 func = fceill_op;
333 else if (insn.mm_fp1_format.op == mm_fceilw_op)
334 func = fceil_op;
335 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
336 func = ftruncl_op;
337 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
338 func = ftrunc_op;
339 else if (insn.mm_fp1_format.op == mm_froundl_op)
340 func = froundl_op;
341 else if (insn.mm_fp1_format.op == mm_froundw_op)
342 func = fround_op;
343 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
344 func = fcvtl_op;
345 else
346 func = fcvtw_op;
347 mips32_insn.fp0_format.opcode = cop1_op;
348 mips32_insn.fp0_format.fmt =
349 sd_format[insn.mm_fp1_format.fmt];
350 mips32_insn.fp0_format.ft = 0;
351 mips32_insn.fp0_format.fs =
352 insn.mm_fp1_format.fs;
353 mips32_insn.fp0_format.fd =
354 insn.mm_fp1_format.rt;
355 mips32_insn.fp0_format.func = func;
356 break;
357 case mm_frsqrt_op:
358 case mm_fsqrt_op:
359 case mm_frecip_op:
360 if (insn.mm_fp1_format.op == mm_frsqrt_op)
361 func = frsqrt_op;
362 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
363 func = fsqrt_op;
364 else
365 func = frecip_op;
366 mips32_insn.fp0_format.opcode = cop1_op;
367 mips32_insn.fp0_format.fmt =
368 sdps_format[insn.mm_fp1_format.fmt];
369 mips32_insn.fp0_format.ft = 0;
370 mips32_insn.fp0_format.fs =
371 insn.mm_fp1_format.fs;
372 mips32_insn.fp0_format.fd =
373 insn.mm_fp1_format.rt;
374 mips32_insn.fp0_format.func = func;
375 break;
376 case mm_mfc1_op:
377 case mm_mtc1_op:
378 case mm_cfc1_op:
379 case mm_ctc1_op:
Steven J. Hill9355e592013-11-07 12:48:29 +0000380 case mm_mfhc1_op:
381 case mm_mthc1_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500382 if (insn.mm_fp1_format.op == mm_mfc1_op)
383 op = mfc_op;
384 else if (insn.mm_fp1_format.op == mm_mtc1_op)
385 op = mtc_op;
386 else if (insn.mm_fp1_format.op == mm_cfc1_op)
387 op = cfc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000388 else if (insn.mm_fp1_format.op == mm_ctc1_op)
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500389 op = ctc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000390 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
391 op = mfhc_op;
392 else
393 op = mthc_op;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500394 mips32_insn.fp1_format.opcode = cop1_op;
395 mips32_insn.fp1_format.op = op;
396 mips32_insn.fp1_format.rt =
397 insn.mm_fp1_format.rt;
398 mips32_insn.fp1_format.fs =
399 insn.mm_fp1_format.fs;
400 mips32_insn.fp1_format.fd = 0;
401 mips32_insn.fp1_format.func = 0;
402 break;
403 default:
404 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500405 }
406 break;
407 case mm_32f_74_op: /* c.cond.fmt */
408 mips32_insn.fp0_format.opcode = cop1_op;
409 mips32_insn.fp0_format.fmt =
410 sdps_format[insn.mm_fp4_format.fmt];
411 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
412 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
413 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
414 mips32_insn.fp0_format.func =
415 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
416 break;
417 default:
418 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500419 }
420 break;
421 default:
422 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500423 }
424
425 *insn_ptr = mips32_insn;
426 return 0;
427}
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/*
430 * Redundant with logic already in kernel/branch.c,
431 * embedded in compute_return_epc. At some point,
432 * a single subroutine should be used across both
433 * modules.
434 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500435static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
436 unsigned long *contpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500438 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
439 unsigned int fcr31;
440 unsigned int bit = 0;
441
442 switch (insn.i_format.opcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 case spec_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500444 switch (insn.r_format.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 case jalr_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500446 regs->regs[insn.r_format.rd] =
447 regs->cp0_epc + dec_insn.pc_inc +
448 dec_insn.next_pc_inc;
449 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 case jr_op:
Markos Chandras5f9f41c2014-11-25 15:54:14 +0000451 /* For R6, JR already emulated in jalr_op */
452 if (NO_R6EMU && insn.r_format.opcode == jr_op)
453 break;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500454 *contpc = regs->regs[insn.r_format.rs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 1;
456 }
457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 case bcond_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500459 switch (insn.i_format.rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 case bltzal_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 case bltzall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000462 if (NO_R6EMU && (insn.i_format.rs ||
463 insn.i_format.rt == bltzall_op))
464 break;
465
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500466 regs->regs[31] = regs->cp0_epc +
467 dec_insn.pc_inc +
468 dec_insn.next_pc_inc;
469 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500470 case bltzl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000471 if (NO_R6EMU)
472 break;
473 case bltz_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500474 if ((long)regs->regs[insn.i_format.rs] < 0)
475 *contpc = regs->cp0_epc +
476 dec_insn.pc_inc +
477 (insn.i_format.simmediate << 2);
478 else
479 *contpc = regs->cp0_epc +
480 dec_insn.pc_inc +
481 dec_insn.next_pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500483 case bgezal_op:
484 case bgezall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000485 if (NO_R6EMU && (insn.i_format.rs ||
486 insn.i_format.rt == bgezall_op))
487 break;
488
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500489 regs->regs[31] = regs->cp0_epc +
490 dec_insn.pc_inc +
491 dec_insn.next_pc_inc;
492 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500493 case bgezl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000494 if (NO_R6EMU)
495 break;
496 case bgez_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500497 if ((long)regs->regs[insn.i_format.rs] >= 0)
498 *contpc = regs->cp0_epc +
499 dec_insn.pc_inc +
500 (insn.i_format.simmediate << 2);
501 else
502 *contpc = regs->cp0_epc +
503 dec_insn.pc_inc +
504 dec_insn.next_pc_inc;
505 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 case jalx_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500509 set_isa16_mode(bit);
510 case jal_op:
511 regs->regs[31] = regs->cp0_epc +
512 dec_insn.pc_inc +
513 dec_insn.next_pc_inc;
514 /* Fall through */
515 case j_op:
516 *contpc = regs->cp0_epc + dec_insn.pc_inc;
517 *contpc >>= 28;
518 *contpc <<= 28;
519 *contpc |= (insn.j_format.target << 2);
520 /* Set microMIPS mode bit: XOR for jalx. */
521 *contpc ^= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500523 case beql_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000524 if (NO_R6EMU)
525 break;
526 case beq_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500527 if (regs->regs[insn.i_format.rs] ==
528 regs->regs[insn.i_format.rt])
529 *contpc = regs->cp0_epc +
530 dec_insn.pc_inc +
531 (insn.i_format.simmediate << 2);
532 else
533 *contpc = regs->cp0_epc +
534 dec_insn.pc_inc +
535 dec_insn.next_pc_inc;
536 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500537 case bnel_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000538 if (NO_R6EMU)
539 break;
540 case bne_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500541 if (regs->regs[insn.i_format.rs] !=
542 regs->regs[insn.i_format.rt])
543 *contpc = regs->cp0_epc +
544 dec_insn.pc_inc +
545 (insn.i_format.simmediate << 2);
546 else
547 *contpc = regs->cp0_epc +
548 dec_insn.pc_inc +
549 dec_insn.next_pc_inc;
550 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500551 case blezl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000552 if (NO_R6EMU)
553 break;
554 case blez_op:
Markos Chandrasa8ff66f2014-11-26 12:57:54 +0000555
556 /*
557 * Compact branches for R6 for the
558 * blez and blezl opcodes.
559 * BLEZ | rs = 0 | rt != 0 == BLEZALC
560 * BLEZ | rs = rt != 0 == BGEZALC
561 * BLEZ | rs != 0 | rt != 0 == BGEUC
562 * BLEZL | rs = 0 | rt != 0 == BLEZC
563 * BLEZL | rs = rt != 0 == BGEZC
564 * BLEZL | rs != 0 | rt != 0 == BGEC
565 *
566 * For real BLEZ{,L}, rt is always 0.
567 */
568 if (cpu_has_mips_r6 && insn.i_format.rt) {
569 if ((insn.i_format.opcode == blez_op) &&
570 ((!insn.i_format.rs && insn.i_format.rt) ||
571 (insn.i_format.rs == insn.i_format.rt)))
572 regs->regs[31] = regs->cp0_epc +
573 dec_insn.pc_inc;
574 *contpc = regs->cp0_epc + dec_insn.pc_inc +
575 dec_insn.next_pc_inc;
576
577 return 1;
578 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500579 if ((long)regs->regs[insn.i_format.rs] <= 0)
580 *contpc = regs->cp0_epc +
581 dec_insn.pc_inc +
582 (insn.i_format.simmediate << 2);
583 else
584 *contpc = regs->cp0_epc +
585 dec_insn.pc_inc +
586 dec_insn.next_pc_inc;
587 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500588 case bgtzl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000589 if (NO_R6EMU)
590 break;
591 case bgtz_op:
Markos Chandrasf1b44062014-11-26 13:05:09 +0000592 /*
593 * Compact branches for R6 for the
594 * bgtz and bgtzl opcodes.
595 * BGTZ | rs = 0 | rt != 0 == BGTZALC
596 * BGTZ | rs = rt != 0 == BLTZALC
597 * BGTZ | rs != 0 | rt != 0 == BLTUC
598 * BGTZL | rs = 0 | rt != 0 == BGTZC
599 * BGTZL | rs = rt != 0 == BLTZC
600 * BGTZL | rs != 0 | rt != 0 == BLTC
601 *
602 * *ZALC varint for BGTZ &&& rt != 0
603 * For real GTZ{,L}, rt is always 0.
604 */
605 if (cpu_has_mips_r6 && insn.i_format.rt) {
606 if ((insn.i_format.opcode == blez_op) &&
607 ((!insn.i_format.rs && insn.i_format.rt) ||
608 (insn.i_format.rs == insn.i_format.rt)))
609 regs->regs[31] = regs->cp0_epc +
610 dec_insn.pc_inc;
611 *contpc = regs->cp0_epc + dec_insn.pc_inc +
612 dec_insn.next_pc_inc;
613
614 return 1;
615 }
616
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500617 if ((long)regs->regs[insn.i_format.rs] > 0)
618 *contpc = regs->cp0_epc +
619 dec_insn.pc_inc +
620 (insn.i_format.simmediate << 2);
621 else
622 *contpc = regs->cp0_epc +
623 dec_insn.pc_inc +
624 dec_insn.next_pc_inc;
625 return 1;
Markos Chandrasc893ce32014-11-26 14:08:52 +0000626 case cbcond0_op:
627 if (!cpu_has_mips_r6)
628 break;
629 if (insn.i_format.rt && !insn.i_format.rs)
630 regs->regs[31] = regs->cp0_epc + 4;
631 *contpc = regs->cp0_epc + dec_insn.pc_inc +
632 dec_insn.next_pc_inc;
633
634 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700635#ifdef CONFIG_CPU_CAVIUM_OCTEON
636 case lwc2_op: /* This is bbit0 on Octeon */
637 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
638 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
639 else
640 *contpc = regs->cp0_epc + 8;
641 return 1;
642 case ldc2_op: /* This is bbit032 on Octeon */
643 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
644 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
645 else
646 *contpc = regs->cp0_epc + 8;
647 return 1;
648 case swc2_op: /* This is bbit1 on Octeon */
649 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
650 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
651 else
652 *contpc = regs->cp0_epc + 8;
653 return 1;
654 case sdc2_op: /* This is bbit132 on Octeon */
655 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
656 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
657 else
658 *contpc = regs->cp0_epc + 8;
659 return 1;
Markos Chandras8467ca02014-11-26 13:56:51 +0000660#else
661 case bc6_op:
662 /*
663 * Only valid for MIPS R6 but we can still end up
664 * here from a broken userland so just tell emulator
665 * this is not a branch and let it break later on.
666 */
667 if (!cpu_has_mips_r6)
668 break;
669 *contpc = regs->cp0_epc + dec_insn.pc_inc +
670 dec_insn.next_pc_inc;
671
672 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700673#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 case cop0_op:
675 case cop1_op:
Markos Chandrasc8a34582014-11-26 10:10:18 +0000676 /* Need to check for R6 bc1nez and bc1eqz branches */
677 if (cpu_has_mips_r6 &&
678 ((insn.i_format.rs == bc1eqz_op) ||
679 (insn.i_format.rs == bc1nez_op))) {
680 bit = 0;
681 switch (insn.i_format.rs) {
682 case bc1eqz_op:
683 if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
684 bit = 1;
685 break;
686 case bc1nez_op:
687 if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
688 bit = 1;
689 break;
690 }
691 if (bit)
692 *contpc = regs->cp0_epc +
693 dec_insn.pc_inc +
694 (insn.i_format.simmediate << 2);
695 else
696 *contpc = regs->cp0_epc +
697 dec_insn.pc_inc +
698 dec_insn.next_pc_inc;
699
700 return 1;
701 }
702 /* R2/R6 compatible cop1 instruction. Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 case cop2_op:
704 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500705 if (insn.i_format.rs == bc_op) {
706 preempt_disable();
707 if (is_fpu_owner())
Manuel Lauss842dfc12014-11-07 14:13:54 +0100708 fcr31 = read_32bit_cp1_register(CP1_STATUS);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500709 else
710 fcr31 = current->thread.fpu.fcr31;
711 preempt_enable();
712
713 bit = (insn.i_format.rt >> 2);
714 bit += (bit != 0);
715 bit += 23;
716 switch (insn.i_format.rt & 3) {
717 case 0: /* bc1f */
718 case 2: /* bc1fl */
719 if (~fcr31 & (1 << bit))
720 *contpc = regs->cp0_epc +
721 dec_insn.pc_inc +
722 (insn.i_format.simmediate << 2);
723 else
724 *contpc = regs->cp0_epc +
725 dec_insn.pc_inc +
726 dec_insn.next_pc_inc;
727 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500728 case 1: /* bc1t */
729 case 3: /* bc1tl */
730 if (fcr31 & (1 << bit))
731 *contpc = regs->cp0_epc +
732 dec_insn.pc_inc +
733 (insn.i_format.simmediate << 2);
734 else
735 *contpc = regs->cp0_epc +
736 dec_insn.pc_inc +
737 dec_insn.next_pc_inc;
738 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500739 }
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return 0;
744}
745
746/*
747 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100748 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800749 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000750 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200751 * FPU emu is slow and bulky and optimizing this function offers fairly
752 * sizeable benefits so we try to be clever and make this function return
753 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000754 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 */
David Daneyda0bac32009-11-02 11:33:46 -0800756static inline int cop1_64bit(struct pt_regs *xcp)
757{
Ralf Baechle08a07902014-04-19 13:11:37 +0200758 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
759 return 1;
760 else if (config_enabled(CONFIG_32BIT) &&
761 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
762 return 0;
763
Paul Burton597ce172013-11-22 13:12:07 +0000764 return !test_thread_flag(TIF_32BIT_FPREGS);
David Daneyda0bac32009-11-02 11:33:46 -0800765}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Paul Burton4227a2d2014-09-11 08:30:20 +0100767static inline bool hybrid_fprs(void)
768{
769 return test_thread_flag(TIF_HYBRID_FPREGS);
770}
771
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200772#define SIFROMREG(si, x) \
773do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100774 if (cop1_64bit(xcp) && !hybrid_fprs()) \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100775 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000776 else \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100777 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000778} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800779
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200780#define SITOREG(si, x) \
781do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100782 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000783 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000784 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000785 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
786 set_fpr32(&ctx->fpr[x], i, 0); \
787 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000788 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000789 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000790} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Paul Burtonc8c0da62014-09-24 10:45:37 +0100792#define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000793
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200794#define SITOHREG(si, x) \
795do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000796 unsigned i; \
797 set_fpr32(&ctx->fpr[x], 1, si); \
798 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
799 set_fpr32(&ctx->fpr[x], i, 0); \
800} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000801
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200802#define DIFROMREG(di, x) \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000803 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
804
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200805#define DITOREG(di, x) \
806do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000807 unsigned fpr, i; \
808 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
809 set_fpr64(&ctx->fpr[fpr], 0, di); \
810 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
811 set_fpr64(&ctx->fpr[fpr], i, 0); \
812} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Ralf Baechle21a151d2007-10-11 23:46:15 +0100814#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
815#define SPTOREG(sp, x) SITOREG((sp).bits, x)
816#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
817#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819/*
820 * Emulate the single floating point instruction pointed at by EPC.
821 * Two instructions if the instruction is in a branch delay slot.
822 */
823
David Daney515b0292010-10-21 16:32:26 -0700824static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500825 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500827 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200828 unsigned int cond, cbit;
829 mips_instruction ir;
830 int likely, pc_inc;
831 u32 __user *wva;
832 u64 __user *dva;
833 u32 value;
834 u32 wval;
835 u64 dval;
836 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Ralf Baechle70e4c232014-04-30 11:09:44 +0200838 /*
839 * These are giving gcc a gentle hint about what to expect in
840 * dec_inst in order to do better optimization.
841 */
842 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
843 unreachable();
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200846 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500847 if (dec_insn.micro_mips_mode) {
848 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200849 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500850 } else {
851 if (!isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200852 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500853 }
854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200856 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /*
858 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +0100859 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 * BEFORE we do the cop1 instruction.
861 *
862 * This branch could be a COP1 branch, but in that case we
863 * would have had a trap for that instruction, and would not
864 * come through this route.
865 *
866 * Linux MIPS branch emulator operates on context, updating the
867 * cp0_epc.
868 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500869 ir = dec_insn.next_insn; /* process delay slot instr */
870 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000871 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500872 ir = dec_insn.insn; /* process current instr */
873 pc_inc = dec_insn.pc_inc;
874 }
875
876 /*
877 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
878 * instructions, we want to convert microMIPS FPU instructions
879 * into MIPS32 instructions so that we could reuse all of the
880 * FPU emulation code.
881 *
882 * NOTE: We cannot do this for branch instructions since they
883 * are not a subset. Example: Cannot emulate a 16-bit
884 * aligned target address with a MIPS32 instruction.
885 */
886 if (dec_insn.micro_mips_mode) {
887 /*
888 * If next instruction is a 16-bit instruction, then it
889 * it cannot be a FPU instruction. This could happen
890 * since we can be called for non-FPU instructions.
891 */
892 if ((pc_inc == 2) ||
893 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
894 == SIGILL))
895 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200898emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200899 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -0800900 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 switch (MIPSInst_OPCODE(ir)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200902 case ldc1_op:
903 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
904 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800905 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -0700906
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200907 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800908 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200909 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return SIGBUS;
911 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200912 if (__get_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700913 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200914 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700915 return SIGSEGV;
916 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200917 DITOREG(dval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200920 case sdc1_op:
921 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
922 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800923 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200924 DIFROMREG(dval, MIPSInst_RT(ir));
925 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800926 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200927 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return SIGBUS;
929 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200930 if (__put_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700931 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200932 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700933 return SIGSEGV;
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200937 case lwc1_op:
938 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
939 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800940 MIPS_FPU_EMU_INC_STATS(loads);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200941 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800942 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200943 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return SIGBUS;
945 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200946 if (__get_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -0700947 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200948 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -0700949 return SIGSEGV;
950 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200951 SITOREG(wval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200954 case swc1_op:
955 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
956 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800957 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200958 SIFROMREG(wval, MIPSInst_RT(ir));
959 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800960 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200961 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return SIGBUS;
963 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200964 if (__put_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -0700965 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200966 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -0700967 return SIGSEGV;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 case cop1_op:
972 switch (MIPSInst_RS(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 case dmfc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +0200974 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
975 return SIGILL;
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* copregister fs -> gpr[rt] */
978 if (MIPSInst_RT(ir) != 0) {
979 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
980 MIPSInst_RD(ir));
981 }
982 break;
983
984 case dmtc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +0200985 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
986 return SIGILL;
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* copregister fs <- rt */
989 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
990 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000992 case mfhc_op:
993 if (!cpu_has_mips_r2)
994 goto sigill;
995
996 /* copregister rd -> gpr[rt] */
997 if (MIPSInst_RT(ir) != 0) {
998 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
999 MIPSInst_RD(ir));
1000 }
1001 break;
1002
1003 case mthc_op:
1004 if (!cpu_has_mips_r2)
1005 goto sigill;
1006
1007 /* copregister rd <- gpr[rt] */
1008 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1009 break;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 case mfc_op:
1012 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (MIPSInst_RT(ir) != 0) {
1014 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1015 MIPSInst_RD(ir));
1016 }
1017 break;
1018
1019 case mtc_op:
1020 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1022 break;
1023
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001024 case cfc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /* cop control register rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1027 value = ctx->fcr31;
Ralf Baechle56a64732014-04-30 11:21:55 +02001028 value = (value & ~FPU_CSR_RM) | modeindex(value);
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033 else if (MIPSInst_RD(ir) == FPCREG_RID)
1034 value = 0;
1035 else
1036 value = 0;
1037 if (MIPSInst_RT(ir))
1038 xcp->regs[MIPSInst_RT(ir)] = value;
1039 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001041 case ctc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (MIPSInst_RT(ir) == 0)
1044 value = 0;
1045 else
1046 value = xcp->regs[MIPSInst_RT(ir)];
1047
1048 /* we only have one writable control reg
1049 */
1050 if (MIPSInst_RD(ir) == FPCREG_CSR) {
Ralf Baechle92df0f82014-04-19 14:03:37 +02001051 pr_debug("%p gpr[%d]->csr=%08x\n",
1052 (void *) (xcp->cp0_epc),
1053 MIPSInst_RT(ir), value);
Shane McDonald95e8f632010-05-06 23:26:57 -06001054
1055 /*
1056 * Don't write reserved bits,
1057 * and convert to ieee library modes
1058 */
Ralf Baechle56a64732014-04-30 11:21:55 +02001059 ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
1060 modeindex(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1063 return SIGFPE;
1064 }
1065 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001067 case bc_op:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001068 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return SIGILL;
1070
Ralf Baechle08a07902014-04-19 13:11:37 +02001071 if (cpu_has_mips_4_5_r)
1072 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1073 else
1074 cbit = FPU_CSR_COND;
1075 cond = ctx->fcr31 & cbit;
1076
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001077 likely = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 switch (MIPSInst_RT(ir) & 3) {
1079 case bcfl_op:
1080 likely = 1;
1081 case bcf_op:
1082 cond = !cond;
1083 break;
1084 case bctl_op:
1085 likely = 1;
1086 case bct_op:
1087 break;
1088 default:
1089 /* thats an illegal instruction */
1090 return SIGILL;
1091 }
1092
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001093 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (cond) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001095 /*
1096 * Branch taken: emulate dslot instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001098 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001100 contpc = MIPSInst_SIMM(ir);
1101 ir = dec_insn.next_insn;
1102 if (dec_insn.micro_mips_mode) {
1103 contpc = (xcp->cp0_epc + (contpc << 1));
1104
1105 /* If 16-bit instruction, not FPU. */
1106 if ((dec_insn.next_pc_inc == 2) ||
1107 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1108
1109 /*
1110 * Since this instruction will
1111 * be put on the stack with
1112 * 32-bit words, get around
1113 * this problem by putting a
1114 * NOP16 as the second one.
1115 */
1116 if (dec_insn.next_pc_inc == 2)
1117 ir = (ir & (~0xffff)) | MM_NOP16;
1118
1119 /*
1120 * Single step the non-CP1
1121 * instruction in the dslot.
1122 */
1123 return mips_dsemul(xcp, ir, contpc);
1124 }
1125 } else
1126 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 switch (MIPSInst_OPCODE(ir)) {
1129 case lwc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001130 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 case swc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001133 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 case ldc1_op:
1136 case sdc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001137 if (cpu_has_mips_2_3_4_5 ||
1138 cpu_has_mips64)
1139 goto emul;
1140
1141 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001143
Ralf Baechle08a07902014-04-19 13:11:37 +02001144 case cop1_op:
1145 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001146
Ralf Baechle08a07902014-04-19 13:11:37 +02001147 case cop1x_op:
Markos Chandrasa5466d72014-10-21 10:21:54 +01001148 if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
Ralf Baechle08a07902014-04-19 13:11:37 +02001149 /* its one of ours */
1150 goto emul;
1151
1152 return SIGILL;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001155 if (!cpu_has_mips_4_5_r)
1156 return SIGILL;
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (MIPSInst_FUNC(ir) == movc_op)
1159 goto emul;
1160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
1163 /*
1164 * Single step the non-cp1
1165 * instruction in the dslot
1166 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001167 return mips_dsemul(xcp, ir, contpc);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001168 } else if (likely) { /* branch not taken */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /*
1170 * branch likely nullifies
1171 * dslot if not taken
1172 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001173 xcp->cp0_epc += dec_insn.pc_inc;
1174 contpc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /*
1176 * else continue & execute
1177 * dslot as normal insn
1178 */
1179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 default:
1183 if (!(MIPSInst_RS(ir) & 0x10))
1184 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001186 /* a real fpu computation instruction */
1187 if ((sig = fpu_emu(xcp, ctx, ir)))
1188 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 break;
1191
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001192 case cop1x_op:
Markos Chandrasa5466d72014-10-21 10:21:54 +01001193 if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
Ralf Baechle08a07902014-04-19 13:11:37 +02001194 return SIGILL;
1195
1196 sig = fpux_emu(xcp, ctx, ir, fault_addr);
David Daney515b0292010-10-21 16:32:26 -07001197 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return sig;
1199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001202 if (!cpu_has_mips_4_5_r)
1203 return SIGILL;
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (MIPSInst_FUNC(ir) != movc_op)
1206 return SIGILL;
1207 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1208 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1209 xcp->regs[MIPSInst_RD(ir)] =
1210 xcp->regs[MIPSInst_RS(ir)];
1211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 default:
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001213sigill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return SIGILL;
1215 }
1216
1217 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001218 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001219 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return 0;
1222}
1223
1224/*
1225 * Conversion table from MIPS compare ops 48-63
1226 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1227 */
1228static const unsigned char cmptab[8] = {
1229 0, /* cmp_0 (sig) cmp_sf */
1230 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1231 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1232 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1233 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1234 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1235 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1236 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1237};
1238
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240/*
1241 * Additional MIPS4 instructions
1242 */
1243
Ralf Baechle47fa0c02014-04-16 11:00:12 +02001244#define DEF3OP(name, p, f1, f2, f3) \
1245static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1246 union ieee754##p s, union ieee754##p t) \
1247{ \
1248 struct _ieee754_csr ieee754_csr_save; \
1249 s = f1(s, t); \
1250 ieee754_csr_save = ieee754_csr; \
1251 s = f2(s, r); \
1252 ieee754_csr_save.cx |= ieee754_csr.cx; \
1253 ieee754_csr_save.sx |= ieee754_csr.sx; \
1254 s = f3(s); \
1255 ieee754_csr.cx |= ieee754_csr_save.cx; \
1256 ieee754_csr.sx |= ieee754_csr_save.sx; \
1257 return s; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001260static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 return ieee754dp_div(ieee754dp_one(0), d);
1263}
1264
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001265static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1268}
1269
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001270static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 return ieee754sp_div(ieee754sp_one(0), s);
1273}
1274
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001275static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1278}
1279
Ralf Baechle21a151d2007-10-11 23:46:15 +01001280DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1281DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1283DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001284DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1285DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1287DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1288
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001289static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001290 mips_instruction ir, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 unsigned rcsr = 0; /* resulting csr */
1293
David Daneyb6ee75e2009-11-05 11:34:26 -08001294 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 switch (MIPSInst_FMA_FFMT(ir)) {
1297 case s_fmt:{ /* 0 */
1298
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001299 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1300 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001301 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 u32 val;
1303
1304 switch (MIPSInst_FUNC(ir)) {
1305 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001306 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 xcp->regs[MIPSInst_FT(ir)]);
1308
David Daneyb6ee75e2009-11-05 11:34:26 -08001309 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001310 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001311 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001312 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return SIGBUS;
1314 }
David Daney515b0292010-10-21 16:32:26 -07001315 if (__get_user(val, va)) {
1316 MIPS_FPU_EMU_INC_STATS(errors);
1317 *fault_addr = va;
1318 return SIGSEGV;
1319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 SITOREG(val, MIPSInst_FD(ir));
1321 break;
1322
1323 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001324 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 xcp->regs[MIPSInst_FT(ir)]);
1326
David Daneyb6ee75e2009-11-05 11:34:26 -08001327 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001330 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1331 MIPS_FPU_EMU_INC_STATS(errors);
1332 *fault_addr = va;
1333 return SIGBUS;
1334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001336 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001337 *fault_addr = va;
1338 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 break;
1341
1342 case madd_s_op:
1343 handler = fpemu_sp_madd;
1344 goto scoptop;
1345 case msub_s_op:
1346 handler = fpemu_sp_msub;
1347 goto scoptop;
1348 case nmadd_s_op:
1349 handler = fpemu_sp_nmadd;
1350 goto scoptop;
1351 case nmsub_s_op:
1352 handler = fpemu_sp_nmsub;
1353 goto scoptop;
1354
1355 scoptop:
1356 SPFROMREG(fr, MIPSInst_FR(ir));
1357 SPFROMREG(fs, MIPSInst_FS(ir));
1358 SPFROMREG(ft, MIPSInst_FT(ir));
1359 fd = (*handler) (fr, fs, ft);
1360 SPTOREG(fd, MIPSInst_FD(ir));
1361
1362 copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001363 if (ieee754_cxtest(IEEE754_INEXACT)) {
1364 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001366 }
1367 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1368 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001370 }
1371 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1372 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001374 }
1375 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1376 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001382 /*printk ("SIGFPE: FPU csr = %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 ctx->fcr31); */
1384 return SIGFPE;
1385 }
1386
1387 break;
1388
1389 default:
1390 return SIGILL;
1391 }
1392 break;
1393 }
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001396 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1397 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001398 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 u64 val;
1400
1401 switch (MIPSInst_FUNC(ir)) {
1402 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001403 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 xcp->regs[MIPSInst_FT(ir)]);
1405
David Daneyb6ee75e2009-11-05 11:34:26 -08001406 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001407 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001408 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001409 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return SIGBUS;
1411 }
David Daney515b0292010-10-21 16:32:26 -07001412 if (__get_user(val, va)) {
1413 MIPS_FPU_EMU_INC_STATS(errors);
1414 *fault_addr = va;
1415 return SIGSEGV;
1416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 DITOREG(val, MIPSInst_FD(ir));
1418 break;
1419
1420 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001421 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 xcp->regs[MIPSInst_FT(ir)]);
1423
David Daneyb6ee75e2009-11-05 11:34:26 -08001424 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001426 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001427 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001428 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return SIGBUS;
1430 }
David Daney515b0292010-10-21 16:32:26 -07001431 if (__put_user(val, va)) {
1432 MIPS_FPU_EMU_INC_STATS(errors);
1433 *fault_addr = va;
1434 return SIGSEGV;
1435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 break;
1437
1438 case madd_d_op:
1439 handler = fpemu_dp_madd;
1440 goto dcoptop;
1441 case msub_d_op:
1442 handler = fpemu_dp_msub;
1443 goto dcoptop;
1444 case nmadd_d_op:
1445 handler = fpemu_dp_nmadd;
1446 goto dcoptop;
1447 case nmsub_d_op:
1448 handler = fpemu_dp_nmsub;
1449 goto dcoptop;
1450
1451 dcoptop:
1452 DPFROMREG(fr, MIPSInst_FR(ir));
1453 DPFROMREG(fs, MIPSInst_FS(ir));
1454 DPFROMREG(ft, MIPSInst_FT(ir));
1455 fd = (*handler) (fr, fs, ft);
1456 DPTOREG(fd, MIPSInst_FD(ir));
1457 goto copcsr;
1458
1459 default:
1460 return SIGILL;
1461 }
1462 break;
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001465 case 0x3:
1466 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* ignore prefx operation */
1470 break;
1471
1472 default:
1473 return SIGILL;
1474 }
1475
1476 return 0;
1477}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479
1480
1481/*
1482 * Emulate a single COP1 arithmetic instruction.
1483 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001484static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 mips_instruction ir)
1486{
1487 int rfmt; /* resulting format */
1488 unsigned rcsr = 0; /* resulting csr */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001489 unsigned int oldrm;
1490 unsigned int cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 unsigned cond;
1492 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001493 union ieee754dp d;
1494 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 s64 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 } rv; /* resulting value */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001498 u64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
David Daneyb6ee75e2009-11-05 11:34:26 -08001500 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001502 case s_fmt: { /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001504 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1505 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 } handler;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001507 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 switch (MIPSInst_FUNC(ir)) {
1510 /* binary ops */
1511 case fadd_op:
1512 handler.b = ieee754sp_add;
1513 goto scopbop;
1514 case fsub_op:
1515 handler.b = ieee754sp_sub;
1516 goto scopbop;
1517 case fmul_op:
1518 handler.b = ieee754sp_mul;
1519 goto scopbop;
1520 case fdiv_op:
1521 handler.b = ieee754sp_div;
1522 goto scopbop;
1523
1524 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001526 if (!cpu_has_mips_4_5_r)
1527 return SIGILL;
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 handler.u = ieee754sp_sqrt;
1530 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001531
Ralf Baechle08a07902014-04-19 13:11:37 +02001532 /*
1533 * Note that on some MIPS IV implementations such as the
1534 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1535 * achieve full IEEE-754 accuracy - however this emulator does.
1536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001538 if (!cpu_has_mips_4_5_r2)
1539 return SIGILL;
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 handler.u = fpemu_sp_rsqrt;
1542 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001545 if (!cpu_has_mips_4_5_r2)
1546 return SIGILL;
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 handler.u = fpemu_sp_recip;
1549 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001552 if (!cpu_has_mips_4_5_r)
1553 return SIGILL;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1556 if (((ctx->fcr31 & cond) != 0) !=
1557 ((MIPSInst_FT(ir) & 1) != 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 fmovz_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 fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001572 if (!cpu_has_mips_4_5_r)
1573 return SIGILL;
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1576 return 0;
1577 SPFROMREG(rv.s, MIPSInst_FS(ir));
1578 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 case fabs_op:
1581 handler.u = ieee754sp_abs;
1582 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 case fneg_op:
1585 handler.u = ieee754sp_neg;
1586 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 case fmov_op:
1589 /* an easy one */
1590 SPFROMREG(rv.s, MIPSInst_FS(ir));
1591 goto copcsr;
1592
1593 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001594scopbop:
1595 SPFROMREG(fs, MIPSInst_FS(ir));
1596 SPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001598 rv.s = (*handler.b) (fs, ft);
1599 goto copcsr;
1600scopuop:
1601 SPFROMREG(fs, MIPSInst_FS(ir));
1602 rv.s = (*handler.u) (fs);
1603 goto copcsr;
1604copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001605 if (ieee754_cxtest(IEEE754_INEXACT)) {
1606 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001608 }
1609 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1610 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001612 }
1613 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1614 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001616 }
1617 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1618 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001620 }
1621 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1622 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 break;
1626
1627 /* unary conv ops */
1628 case fcvts_op:
1629 return SIGILL; /* not defined */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001631 case fcvtd_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 SPFROMREG(fs, MIPSInst_FS(ir));
1633 rv.d = ieee754dp_fsp(fs);
1634 rfmt = d_fmt;
1635 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001637 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 SPFROMREG(fs, MIPSInst_FS(ir));
1639 rv.w = ieee754sp_tint(fs);
1640 rfmt = w_fmt;
1641 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 case fround_op:
1644 case ftrunc_op:
1645 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001646 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001647 if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
1648 return SIGILL;
1649
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001650 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 SPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001652 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 rv.w = ieee754sp_tint(fs);
1654 ieee754_csr.rm = oldrm;
1655 rfmt = w_fmt;
1656 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001658 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001659 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1660 return SIGILL;
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 SPFROMREG(fs, MIPSInst_FS(ir));
1663 rv.l = ieee754sp_tlong(fs);
1664 rfmt = l_fmt;
1665 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 case froundl_op:
1668 case ftruncl_op:
1669 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001670 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001671 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1672 return SIGILL;
1673
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001674 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 SPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001676 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 rv.l = ieee754sp_tlong(fs);
1678 ieee754_csr.rm = oldrm;
1679 rfmt = l_fmt;
1680 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 default:
1683 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1684 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001685 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 SPFROMREG(fs, MIPSInst_FS(ir));
1688 SPFROMREG(ft, MIPSInst_FT(ir));
1689 rv.w = ieee754sp_cmp(fs, ft,
1690 cmptab[cmpop & 0x7], cmpop & 0x8);
1691 rfmt = -1;
1692 if ((cmpop & 0x8) && ieee754_cxtest
1693 (IEEE754_INVALID_OPERATION))
1694 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1695 else
1696 goto copcsr;
1697
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001698 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 break;
1701 }
1702 break;
1703 }
1704
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001705 case d_fmt: {
1706 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001708 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1709 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 } handler;
1711
1712 switch (MIPSInst_FUNC(ir)) {
1713 /* binary ops */
1714 case fadd_op:
1715 handler.b = ieee754dp_add;
1716 goto dcopbop;
1717 case fsub_op:
1718 handler.b = ieee754dp_sub;
1719 goto dcopbop;
1720 case fmul_op:
1721 handler.b = ieee754dp_mul;
1722 goto dcopbop;
1723 case fdiv_op:
1724 handler.b = ieee754dp_div;
1725 goto dcopbop;
1726
1727 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001729 if (!cpu_has_mips_2_3_4_5_r)
1730 return SIGILL;
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 handler.u = ieee754dp_sqrt;
1733 goto dcopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001734 /*
1735 * Note that on some MIPS IV implementations such as the
1736 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1737 * achieve full IEEE-754 accuracy - however this emulator does.
1738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001740 if (!cpu_has_mips_4_5_r2)
1741 return SIGILL;
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 handler.u = fpemu_dp_rsqrt;
1744 goto dcopuop;
1745 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001746 if (!cpu_has_mips_4_5_r2)
1747 return SIGILL;
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 handler.u = fpemu_dp_recip;
1750 goto dcopuop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001752 if (!cpu_has_mips_4_5_r)
1753 return SIGILL;
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1756 if (((ctx->fcr31 & cond) != 0) !=
1757 ((MIPSInst_FT(ir) & 1) != 0))
1758 return 0;
1759 DPFROMREG(rv.d, MIPSInst_FS(ir));
1760 break;
1761 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001762 if (!cpu_has_mips_4_5_r)
1763 return SIGILL;
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1766 return 0;
1767 DPFROMREG(rv.d, MIPSInst_FS(ir));
1768 break;
1769 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001770 if (!cpu_has_mips_4_5_r)
1771 return SIGILL;
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1774 return 0;
1775 DPFROMREG(rv.d, MIPSInst_FS(ir));
1776 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 case fabs_op:
1778 handler.u = ieee754dp_abs;
1779 goto dcopuop;
1780
1781 case fneg_op:
1782 handler.u = ieee754dp_neg;
1783 goto dcopuop;
1784
1785 case fmov_op:
1786 /* an easy one */
1787 DPFROMREG(rv.d, MIPSInst_FS(ir));
1788 goto copcsr;
1789
1790 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001791dcopbop:
1792 DPFROMREG(fs, MIPSInst_FS(ir));
1793 DPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001795 rv.d = (*handler.b) (fs, ft);
1796 goto copcsr;
1797dcopuop:
1798 DPFROMREG(fs, MIPSInst_FS(ir));
1799 rv.d = (*handler.u) (fs);
1800 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001802 /*
1803 * unary conv ops
1804 */
1805 case fcvts_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 DPFROMREG(fs, MIPSInst_FS(ir));
1807 rv.s = ieee754sp_fdp(fs);
1808 rfmt = s_fmt;
1809 goto copcsr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 case fcvtd_op:
1812 return SIGILL; /* not defined */
1813
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001814 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 DPFROMREG(fs, MIPSInst_FS(ir));
1816 rv.w = ieee754dp_tint(fs); /* wrong */
1817 rfmt = w_fmt;
1818 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 case fround_op:
1821 case ftrunc_op:
1822 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001823 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001824 if (!cpu_has_mips_2_3_4_5_r)
1825 return SIGILL;
1826
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001827 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 DPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001829 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 rv.w = ieee754dp_tint(fs);
1831 ieee754_csr.rm = oldrm;
1832 rfmt = w_fmt;
1833 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001835 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001836 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1837 return SIGILL;
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 DPFROMREG(fs, MIPSInst_FS(ir));
1840 rv.l = ieee754dp_tlong(fs);
1841 rfmt = l_fmt;
1842 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 case froundl_op:
1845 case ftruncl_op:
1846 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001847 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001848 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1849 return SIGILL;
1850
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001851 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 DPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001853 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 rv.l = ieee754dp_tlong(fs);
1855 ieee754_csr.rm = oldrm;
1856 rfmt = l_fmt;
1857 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 default:
1860 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1861 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001862 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 DPFROMREG(fs, MIPSInst_FS(ir));
1865 DPFROMREG(ft, MIPSInst_FT(ir));
1866 rv.w = ieee754dp_cmp(fs, ft,
1867 cmptab[cmpop & 0x7], cmpop & 0x8);
1868 rfmt = -1;
1869 if ((cmpop & 0x8)
1870 &&
1871 ieee754_cxtest
1872 (IEEE754_INVALID_OPERATION))
1873 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1874 else
1875 goto copcsr;
1876
1877 }
1878 else {
1879 return SIGILL;
1880 }
1881 break;
1882 }
1883 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001885 case w_fmt:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 switch (MIPSInst_FUNC(ir)) {
1887 case fcvts_op:
1888 /* convert word to single precision real */
1889 SPFROMREG(fs, MIPSInst_FS(ir));
1890 rv.s = ieee754sp_fint(fs.bits);
1891 rfmt = s_fmt;
1892 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 case fcvtd_op:
1894 /* convert word to double precision real */
1895 SPFROMREG(fs, MIPSInst_FS(ir));
1896 rv.d = ieee754dp_fint(fs.bits);
1897 rfmt = d_fmt;
1898 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 default:
1900 return SIGILL;
1901 }
1902 break;
1903 }
1904
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001905 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001906
1907 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1908 return SIGILL;
1909
Paul Burtonbbd426f2014-02-13 11:26:41 +00001910 DIFROMREG(bits, MIPSInst_FS(ir));
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 switch (MIPSInst_FUNC(ir)) {
1913 case fcvts_op:
1914 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001915 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 rfmt = s_fmt;
1917 goto copcsr;
1918 case fcvtd_op:
1919 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001920 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 rfmt = d_fmt;
1922 goto copcsr;
1923 default:
1924 return SIGILL;
1925 }
1926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 default:
1929 return SIGILL;
1930 }
1931
1932 /*
1933 * Update the fpu CSR register for this operation.
1934 * If an exception is required, generate a tidy SIGFPE exception,
1935 * without updating the result register.
1936 * Note: cause exception bits do not accumulate, they are rewritten
1937 * for each op; only the flag/sticky bits accumulate.
1938 */
1939 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1940 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001941 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return SIGFPE;
1943 }
1944
1945 /*
1946 * Now we can safely write the result back to the register file.
1947 */
1948 switch (rfmt) {
Ralf Baechle08a07902014-04-19 13:11:37 +02001949 case -1:
1950
1951 if (cpu_has_mips_4_5_r)
Rob Kendrickc3b9b942014-07-23 10:03:58 +01001952 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 else
Ralf Baechle08a07902014-04-19 13:11:37 +02001954 cbit = FPU_CSR_COND;
1955 if (rv.w)
1956 ctx->fcr31 |= cbit;
1957 else
1958 ctx->fcr31 &= ~cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 break;
Ralf Baechle08a07902014-04-19 13:11:37 +02001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 case d_fmt:
1962 DPTOREG(rv.d, MIPSInst_FD(ir));
1963 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 case s_fmt:
1965 SPTOREG(rv.s, MIPSInst_FD(ir));
1966 break;
1967 case w_fmt:
1968 SITOREG(rv.w, MIPSInst_FD(ir));
1969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001971 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1972 return SIGILL;
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 DITOREG(rv.l, MIPSInst_FD(ir));
1975 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 default:
1977 return SIGILL;
1978 }
1979
1980 return 0;
1981}
1982
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09001983int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001984 int has_fpu, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Ralf Baechle333d1f62005-02-28 17:55:57 +00001986 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001987 struct mm_decoded_insn dec_insn;
1988 u16 instr[4];
1989 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 int sig = 0;
1991
1992 oldepc = xcp->cp0_epc;
1993 do {
1994 prevepc = xcp->cp0_epc;
1995
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001996 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
1997 /*
1998 * Get next 2 microMIPS instructions and convert them
1999 * into 32-bit instructions.
2000 */
2001 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2002 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2003 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2004 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2005 MIPS_FPU_EMU_INC_STATS(errors);
2006 return SIGBUS;
2007 }
2008 instr_ptr = instr;
2009
2010 /* Get first instruction. */
2011 if (mm_insn_16bit(*instr_ptr)) {
2012 /* Duplicate the half-word. */
2013 dec_insn.insn = (*instr_ptr << 16) |
2014 (*instr_ptr);
2015 /* 16-bit instruction. */
2016 dec_insn.pc_inc = 2;
2017 instr_ptr += 1;
2018 } else {
2019 dec_insn.insn = (*instr_ptr << 16) |
2020 *(instr_ptr+1);
2021 /* 32-bit instruction. */
2022 dec_insn.pc_inc = 4;
2023 instr_ptr += 2;
2024 }
2025 /* Get second instruction. */
2026 if (mm_insn_16bit(*instr_ptr)) {
2027 /* Duplicate the half-word. */
2028 dec_insn.next_insn = (*instr_ptr << 16) |
2029 (*instr_ptr);
2030 /* 16-bit instruction. */
2031 dec_insn.next_pc_inc = 2;
2032 } else {
2033 dec_insn.next_insn = (*instr_ptr << 16) |
2034 *(instr_ptr+1);
2035 /* 32-bit instruction. */
2036 dec_insn.next_pc_inc = 4;
2037 }
2038 dec_insn.micro_mips_mode = 1;
2039 } else {
2040 if ((get_user(dec_insn.insn,
2041 (mips_instruction __user *) xcp->cp0_epc)) ||
2042 (get_user(dec_insn.next_insn,
2043 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2044 MIPS_FPU_EMU_INC_STATS(errors);
2045 return SIGBUS;
2046 }
2047 dec_insn.pc_inc = 4;
2048 dec_insn.next_pc_inc = 4;
2049 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002051
2052 if ((dec_insn.insn == 0) ||
2053 ((dec_insn.pc_inc == 2) &&
2054 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2055 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002057 /*
2058 * The 'ieee754_csr' is an alias of
Ralf Baechle70342282013-01-22 12:59:30 +01002059 * ctx->fcr31. No need to copy ctx->fcr31 to
2060 * ieee754_csr. But ieee754_csr.rm is ieee
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002061 * library modes. (not mips rounding mode)
2062 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002063 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002066 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 break;
2068 if (sig)
2069 break;
2070
2071 cond_resched();
2072 } while (xcp->cp0_epc > prevepc);
2073
2074 /* SIGILL indicates a non-fpu instruction */
2075 if (sig == SIGILL && xcp->cp0_epc != oldepc)
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002076 /* but if EPC has advanced, then ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 sig = 0;
2078
2079 return sig;
2080}