blob: cac529a405b881aaf6a0d46cd749a0789bb2c414 [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:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500451 *contpc = regs->regs[insn.r_format.rs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 1;
453 }
454 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 case bcond_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500456 switch (insn.i_format.rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 case bltzal_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 case bltzall_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500459 regs->regs[31] = regs->cp0_epc +
460 dec_insn.pc_inc +
461 dec_insn.next_pc_inc;
462 /* Fall through */
463 case bltz_op:
464 case bltzl_op:
465 if ((long)regs->regs[insn.i_format.rs] < 0)
466 *contpc = regs->cp0_epc +
467 dec_insn.pc_inc +
468 (insn.i_format.simmediate << 2);
469 else
470 *contpc = regs->cp0_epc +
471 dec_insn.pc_inc +
472 dec_insn.next_pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500474 case bgezal_op:
475 case bgezall_op:
476 regs->regs[31] = regs->cp0_epc +
477 dec_insn.pc_inc +
478 dec_insn.next_pc_inc;
479 /* Fall through */
480 case bgez_op:
481 case bgezl_op:
482 if ((long)regs->regs[insn.i_format.rs] >= 0)
483 *contpc = regs->cp0_epc +
484 dec_insn.pc_inc +
485 (insn.i_format.simmediate << 2);
486 else
487 *contpc = regs->cp0_epc +
488 dec_insn.pc_inc +
489 dec_insn.next_pc_inc;
490 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 case jalx_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500494 set_isa16_mode(bit);
495 case jal_op:
496 regs->regs[31] = regs->cp0_epc +
497 dec_insn.pc_inc +
498 dec_insn.next_pc_inc;
499 /* Fall through */
500 case j_op:
501 *contpc = regs->cp0_epc + dec_insn.pc_inc;
502 *contpc >>= 28;
503 *contpc <<= 28;
504 *contpc |= (insn.j_format.target << 2);
505 /* Set microMIPS mode bit: XOR for jalx. */
506 *contpc ^= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500508 case beq_op:
509 case beql_op:
510 if (regs->regs[insn.i_format.rs] ==
511 regs->regs[insn.i_format.rt])
512 *contpc = regs->cp0_epc +
513 dec_insn.pc_inc +
514 (insn.i_format.simmediate << 2);
515 else
516 *contpc = regs->cp0_epc +
517 dec_insn.pc_inc +
518 dec_insn.next_pc_inc;
519 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500520 case bne_op:
521 case bnel_op:
522 if (regs->regs[insn.i_format.rs] !=
523 regs->regs[insn.i_format.rt])
524 *contpc = regs->cp0_epc +
525 dec_insn.pc_inc +
526 (insn.i_format.simmediate << 2);
527 else
528 *contpc = regs->cp0_epc +
529 dec_insn.pc_inc +
530 dec_insn.next_pc_inc;
531 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500532 case blez_op:
533 case blezl_op:
534 if ((long)regs->regs[insn.i_format.rs] <= 0)
535 *contpc = regs->cp0_epc +
536 dec_insn.pc_inc +
537 (insn.i_format.simmediate << 2);
538 else
539 *contpc = regs->cp0_epc +
540 dec_insn.pc_inc +
541 dec_insn.next_pc_inc;
542 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500543 case bgtz_op:
544 case bgtzl_op:
545 if ((long)regs->regs[insn.i_format.rs] > 0)
546 *contpc = regs->cp0_epc +
547 dec_insn.pc_inc +
548 (insn.i_format.simmediate << 2);
549 else
550 *contpc = regs->cp0_epc +
551 dec_insn.pc_inc +
552 dec_insn.next_pc_inc;
553 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700554#ifdef CONFIG_CPU_CAVIUM_OCTEON
555 case lwc2_op: /* This is bbit0 on Octeon */
556 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
557 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
558 else
559 *contpc = regs->cp0_epc + 8;
560 return 1;
561 case ldc2_op: /* This is bbit032 on Octeon */
562 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
563 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
564 else
565 *contpc = regs->cp0_epc + 8;
566 return 1;
567 case swc2_op: /* This is bbit1 on Octeon */
568 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
569 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
570 else
571 *contpc = regs->cp0_epc + 8;
572 return 1;
573 case sdc2_op: /* This is bbit132 on Octeon */
574 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
575 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
576 else
577 *contpc = regs->cp0_epc + 8;
578 return 1;
579#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 case cop0_op:
581 case cop1_op:
582 case cop2_op:
583 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500584 if (insn.i_format.rs == bc_op) {
585 preempt_disable();
586 if (is_fpu_owner())
Manuel Lauss842dfc12014-11-07 14:13:54 +0100587 fcr31 = read_32bit_cp1_register(CP1_STATUS);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500588 else
589 fcr31 = current->thread.fpu.fcr31;
590 preempt_enable();
591
592 bit = (insn.i_format.rt >> 2);
593 bit += (bit != 0);
594 bit += 23;
595 switch (insn.i_format.rt & 3) {
596 case 0: /* bc1f */
597 case 2: /* bc1fl */
598 if (~fcr31 & (1 << bit))
599 *contpc = regs->cp0_epc +
600 dec_insn.pc_inc +
601 (insn.i_format.simmediate << 2);
602 else
603 *contpc = regs->cp0_epc +
604 dec_insn.pc_inc +
605 dec_insn.next_pc_inc;
606 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500607 case 1: /* bc1t */
608 case 3: /* bc1tl */
609 if (fcr31 & (1 << bit))
610 *contpc = regs->cp0_epc +
611 dec_insn.pc_inc +
612 (insn.i_format.simmediate << 2);
613 else
614 *contpc = regs->cp0_epc +
615 dec_insn.pc_inc +
616 dec_insn.next_pc_inc;
617 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500618 }
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623}
624
625/*
626 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100627 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800628 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000629 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200630 * FPU emu is slow and bulky and optimizing this function offers fairly
631 * sizeable benefits so we try to be clever and make this function return
632 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000633 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
David Daneyda0bac32009-11-02 11:33:46 -0800635static inline int cop1_64bit(struct pt_regs *xcp)
636{
Ralf Baechle08a07902014-04-19 13:11:37 +0200637 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
638 return 1;
639 else if (config_enabled(CONFIG_32BIT) &&
640 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
641 return 0;
642
Paul Burton597ce172013-11-22 13:12:07 +0000643 return !test_thread_flag(TIF_32BIT_FPREGS);
David Daneyda0bac32009-11-02 11:33:46 -0800644}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200646#define SIFROMREG(si, x) \
647do { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000648 if (cop1_64bit(xcp)) \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100649 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000650 else \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100651 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000652} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800653
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200654#define SITOREG(si, x) \
655do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000656 if (cop1_64bit(xcp)) { \
657 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000658 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000659 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
660 set_fpr32(&ctx->fpr[x], i, 0); \
661 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000662 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000663 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000664} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Paul Burtonc8c0da62014-09-24 10:45:37 +0100666#define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000667
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200668#define SITOHREG(si, x) \
669do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000670 unsigned i; \
671 set_fpr32(&ctx->fpr[x], 1, si); \
672 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
673 set_fpr32(&ctx->fpr[x], i, 0); \
674} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000675
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200676#define DIFROMREG(di, x) \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000677 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
678
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200679#define DITOREG(di, x) \
680do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000681 unsigned fpr, i; \
682 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
683 set_fpr64(&ctx->fpr[fpr], 0, di); \
684 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
685 set_fpr64(&ctx->fpr[fpr], i, 0); \
686} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Ralf Baechle21a151d2007-10-11 23:46:15 +0100688#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
689#define SPTOREG(sp, x) SITOREG((sp).bits, x)
690#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
691#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693/*
694 * Emulate the single floating point instruction pointed at by EPC.
695 * Two instructions if the instruction is in a branch delay slot.
696 */
697
David Daney515b0292010-10-21 16:32:26 -0700698static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500699 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500701 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200702 unsigned int cond, cbit;
703 mips_instruction ir;
704 int likely, pc_inc;
705 u32 __user *wva;
706 u64 __user *dva;
707 u32 value;
708 u32 wval;
709 u64 dval;
710 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Ralf Baechle70e4c232014-04-30 11:09:44 +0200712 /*
713 * These are giving gcc a gentle hint about what to expect in
714 * dec_inst in order to do better optimization.
715 */
716 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
717 unreachable();
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200720 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500721 if (dec_insn.micro_mips_mode) {
722 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200723 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500724 } else {
725 if (!isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200726 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500727 }
728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200730 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /*
732 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +0100733 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * BEFORE we do the cop1 instruction.
735 *
736 * This branch could be a COP1 branch, but in that case we
737 * would have had a trap for that instruction, and would not
738 * come through this route.
739 *
740 * Linux MIPS branch emulator operates on context, updating the
741 * cp0_epc.
742 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500743 ir = dec_insn.next_insn; /* process delay slot instr */
744 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000745 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500746 ir = dec_insn.insn; /* process current instr */
747 pc_inc = dec_insn.pc_inc;
748 }
749
750 /*
751 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
752 * instructions, we want to convert microMIPS FPU instructions
753 * into MIPS32 instructions so that we could reuse all of the
754 * FPU emulation code.
755 *
756 * NOTE: We cannot do this for branch instructions since they
757 * are not a subset. Example: Cannot emulate a 16-bit
758 * aligned target address with a MIPS32 instruction.
759 */
760 if (dec_insn.micro_mips_mode) {
761 /*
762 * If next instruction is a 16-bit instruction, then it
763 * it cannot be a FPU instruction. This could happen
764 * since we can be called for non-FPU instructions.
765 */
766 if ((pc_inc == 2) ||
767 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
768 == SIGILL))
769 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200772emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200773 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -0800774 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 switch (MIPSInst_OPCODE(ir)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200776 case ldc1_op:
777 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
778 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800779 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -0700780
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200781 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800782 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200783 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return SIGBUS;
785 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200786 if (__get_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700787 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200788 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700789 return SIGSEGV;
790 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200791 DITOREG(dval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200794 case sdc1_op:
795 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
796 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800797 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200798 DIFROMREG(dval, MIPSInst_RT(ir));
799 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800800 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200801 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return SIGBUS;
803 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200804 if (__put_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700805 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200806 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700807 return SIGSEGV;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200811 case lwc1_op:
812 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
813 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800814 MIPS_FPU_EMU_INC_STATS(loads);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200815 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800816 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200817 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return SIGBUS;
819 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200820 if (__get_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -0700821 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200822 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -0700823 return SIGSEGV;
824 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200825 SITOREG(wval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200828 case swc1_op:
829 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
830 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800831 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200832 SIFROMREG(wval, MIPSInst_RT(ir));
833 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800834 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200835 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return SIGBUS;
837 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200838 if (__put_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -0700839 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200840 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -0700841 return SIGSEGV;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 case cop1_op:
846 switch (MIPSInst_RS(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 case dmfc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +0200848 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
849 return SIGILL;
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* copregister fs -> gpr[rt] */
852 if (MIPSInst_RT(ir) != 0) {
853 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
854 MIPSInst_RD(ir));
855 }
856 break;
857
858 case dmtc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +0200859 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
860 return SIGILL;
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* copregister fs <- rt */
863 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
864 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000866 case mfhc_op:
867 if (!cpu_has_mips_r2)
868 goto sigill;
869
870 /* copregister rd -> gpr[rt] */
871 if (MIPSInst_RT(ir) != 0) {
872 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
873 MIPSInst_RD(ir));
874 }
875 break;
876
877 case mthc_op:
878 if (!cpu_has_mips_r2)
879 goto sigill;
880
881 /* copregister rd <- gpr[rt] */
882 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
883 break;
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 case mfc_op:
886 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (MIPSInst_RT(ir) != 0) {
888 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
889 MIPSInst_RD(ir));
890 }
891 break;
892
893 case mtc_op:
894 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
896 break;
897
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200898 case cfc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* cop control register rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (MIPSInst_RD(ir) == FPCREG_CSR) {
901 value = ctx->fcr31;
Ralf Baechle56a64732014-04-30 11:21:55 +0200902 value = (value & ~FPU_CSR_RM) | modeindex(value);
Ralf Baechle92df0f82014-04-19 14:03:37 +0200903 pr_debug("%p gpr[%d]<-csr=%08x\n",
904 (void *) (xcp->cp0_epc),
905 MIPSInst_RT(ir), value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 else if (MIPSInst_RD(ir) == FPCREG_RID)
908 value = 0;
909 else
910 value = 0;
911 if (MIPSInst_RT(ir))
912 xcp->regs[MIPSInst_RT(ir)] = value;
913 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200915 case ctc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (MIPSInst_RT(ir) == 0)
918 value = 0;
919 else
920 value = xcp->regs[MIPSInst_RT(ir)];
921
922 /* we only have one writable control reg
923 */
924 if (MIPSInst_RD(ir) == FPCREG_CSR) {
Ralf Baechle92df0f82014-04-19 14:03:37 +0200925 pr_debug("%p gpr[%d]->csr=%08x\n",
926 (void *) (xcp->cp0_epc),
927 MIPSInst_RT(ir), value);
Shane McDonald95e8f632010-05-06 23:26:57 -0600928
929 /*
930 * Don't write reserved bits,
931 * and convert to ieee library modes
932 */
Ralf Baechle56a64732014-04-30 11:21:55 +0200933 ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
934 modeindex(value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
937 return SIGFPE;
938 }
939 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200941 case bc_op:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200942 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return SIGILL;
944
Ralf Baechle08a07902014-04-19 13:11:37 +0200945 if (cpu_has_mips_4_5_r)
946 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
947 else
948 cbit = FPU_CSR_COND;
949 cond = ctx->fcr31 & cbit;
950
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200951 likely = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 switch (MIPSInst_RT(ir) & 3) {
953 case bcfl_op:
954 likely = 1;
955 case bcf_op:
956 cond = !cond;
957 break;
958 case bctl_op:
959 likely = 1;
960 case bct_op:
961 break;
962 default:
963 /* thats an illegal instruction */
964 return SIGILL;
965 }
966
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200967 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (cond) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200969 /*
970 * Branch taken: emulate dslot instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500972 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500974 contpc = MIPSInst_SIMM(ir);
975 ir = dec_insn.next_insn;
976 if (dec_insn.micro_mips_mode) {
977 contpc = (xcp->cp0_epc + (contpc << 1));
978
979 /* If 16-bit instruction, not FPU. */
980 if ((dec_insn.next_pc_inc == 2) ||
981 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
982
983 /*
984 * Since this instruction will
985 * be put on the stack with
986 * 32-bit words, get around
987 * this problem by putting a
988 * NOP16 as the second one.
989 */
990 if (dec_insn.next_pc_inc == 2)
991 ir = (ir & (~0xffff)) | MM_NOP16;
992
993 /*
994 * Single step the non-CP1
995 * instruction in the dslot.
996 */
997 return mips_dsemul(xcp, ir, contpc);
998 }
999 } else
1000 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 switch (MIPSInst_OPCODE(ir)) {
1003 case lwc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001004 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 case swc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001007 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 case ldc1_op:
1010 case sdc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001011 if (cpu_has_mips_2_3_4_5 ||
1012 cpu_has_mips64)
1013 goto emul;
1014
1015 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001017
Ralf Baechle08a07902014-04-19 13:11:37 +02001018 case cop1_op:
1019 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001020
Ralf Baechle08a07902014-04-19 13:11:37 +02001021 case cop1x_op:
Markos Chandrasa5466d72014-10-21 10:21:54 +01001022 if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
Ralf Baechle08a07902014-04-19 13:11:37 +02001023 /* its one of ours */
1024 goto emul;
1025
1026 return SIGILL;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001029 if (!cpu_has_mips_4_5_r)
1030 return SIGILL;
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (MIPSInst_FUNC(ir) == movc_op)
1033 goto emul;
1034 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
1037 /*
1038 * Single step the non-cp1
1039 * instruction in the dslot
1040 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001041 return mips_dsemul(xcp, ir, contpc);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001042 } else if (likely) { /* branch not taken */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
1044 * branch likely nullifies
1045 * dslot if not taken
1046 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001047 xcp->cp0_epc += dec_insn.pc_inc;
1048 contpc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
1050 * else continue & execute
1051 * dslot as normal insn
1052 */
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 default:
1057 if (!(MIPSInst_RS(ir) & 0x10))
1058 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001060 /* a real fpu computation instruction */
1061 if ((sig = fpu_emu(xcp, ctx, ir)))
1062 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064 break;
1065
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001066 case cop1x_op:
Markos Chandrasa5466d72014-10-21 10:21:54 +01001067 if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
Ralf Baechle08a07902014-04-19 13:11:37 +02001068 return SIGILL;
1069
1070 sig = fpux_emu(xcp, ctx, ir, fault_addr);
David Daney515b0292010-10-21 16:32:26 -07001071 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return sig;
1073 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001076 if (!cpu_has_mips_4_5_r)
1077 return SIGILL;
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (MIPSInst_FUNC(ir) != movc_op)
1080 return SIGILL;
1081 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1082 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1083 xcp->regs[MIPSInst_RD(ir)] =
1084 xcp->regs[MIPSInst_RS(ir)];
1085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 default:
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001087sigill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return SIGILL;
1089 }
1090
1091 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001092 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001093 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return 0;
1096}
1097
1098/*
1099 * Conversion table from MIPS compare ops 48-63
1100 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1101 */
1102static const unsigned char cmptab[8] = {
1103 0, /* cmp_0 (sig) cmp_sf */
1104 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1105 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1106 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1107 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1108 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1109 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1110 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1111};
1112
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/*
1115 * Additional MIPS4 instructions
1116 */
1117
Ralf Baechle47fa0c02014-04-16 11:00:12 +02001118#define DEF3OP(name, p, f1, f2, f3) \
1119static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1120 union ieee754##p s, union ieee754##p t) \
1121{ \
1122 struct _ieee754_csr ieee754_csr_save; \
1123 s = f1(s, t); \
1124 ieee754_csr_save = ieee754_csr; \
1125 s = f2(s, r); \
1126 ieee754_csr_save.cx |= ieee754_csr.cx; \
1127 ieee754_csr_save.sx |= ieee754_csr.sx; \
1128 s = f3(s); \
1129 ieee754_csr.cx |= ieee754_csr_save.cx; \
1130 ieee754_csr.sx |= ieee754_csr_save.sx; \
1131 return s; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001134static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 return ieee754dp_div(ieee754dp_one(0), d);
1137}
1138
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001139static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1142}
1143
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001144static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 return ieee754sp_div(ieee754sp_one(0), s);
1147}
1148
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001149static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1152}
1153
Ralf Baechle21a151d2007-10-11 23:46:15 +01001154DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1155DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1157DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001158DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1159DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1161DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1162
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001163static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001164 mips_instruction ir, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 unsigned rcsr = 0; /* resulting csr */
1167
David Daneyb6ee75e2009-11-05 11:34:26 -08001168 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 switch (MIPSInst_FMA_FFMT(ir)) {
1171 case s_fmt:{ /* 0 */
1172
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001173 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1174 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001175 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 u32 val;
1177
1178 switch (MIPSInst_FUNC(ir)) {
1179 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001180 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 xcp->regs[MIPSInst_FT(ir)]);
1182
David Daneyb6ee75e2009-11-05 11:34:26 -08001183 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001184 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001185 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001186 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return SIGBUS;
1188 }
David Daney515b0292010-10-21 16:32:26 -07001189 if (__get_user(val, va)) {
1190 MIPS_FPU_EMU_INC_STATS(errors);
1191 *fault_addr = va;
1192 return SIGSEGV;
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 SITOREG(val, MIPSInst_FD(ir));
1195 break;
1196
1197 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001198 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 xcp->regs[MIPSInst_FT(ir)]);
1200
David Daneyb6ee75e2009-11-05 11:34:26 -08001201 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001204 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1205 MIPS_FPU_EMU_INC_STATS(errors);
1206 *fault_addr = va;
1207 return SIGBUS;
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001210 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001211 *fault_addr = va;
1212 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214 break;
1215
1216 case madd_s_op:
1217 handler = fpemu_sp_madd;
1218 goto scoptop;
1219 case msub_s_op:
1220 handler = fpemu_sp_msub;
1221 goto scoptop;
1222 case nmadd_s_op:
1223 handler = fpemu_sp_nmadd;
1224 goto scoptop;
1225 case nmsub_s_op:
1226 handler = fpemu_sp_nmsub;
1227 goto scoptop;
1228
1229 scoptop:
1230 SPFROMREG(fr, MIPSInst_FR(ir));
1231 SPFROMREG(fs, MIPSInst_FS(ir));
1232 SPFROMREG(ft, MIPSInst_FT(ir));
1233 fd = (*handler) (fr, fs, ft);
1234 SPTOREG(fd, MIPSInst_FD(ir));
1235
1236 copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001237 if (ieee754_cxtest(IEEE754_INEXACT)) {
1238 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001240 }
1241 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1242 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001244 }
1245 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1246 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001248 }
1249 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1250 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001256 /*printk ("SIGFPE: FPU csr = %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 ctx->fcr31); */
1258 return SIGFPE;
1259 }
1260
1261 break;
1262
1263 default:
1264 return SIGILL;
1265 }
1266 break;
1267 }
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001270 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1271 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001272 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 u64 val;
1274
1275 switch (MIPSInst_FUNC(ir)) {
1276 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001277 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 xcp->regs[MIPSInst_FT(ir)]);
1279
David Daneyb6ee75e2009-11-05 11:34:26 -08001280 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001281 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001282 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001283 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return SIGBUS;
1285 }
David Daney515b0292010-10-21 16:32:26 -07001286 if (__get_user(val, va)) {
1287 MIPS_FPU_EMU_INC_STATS(errors);
1288 *fault_addr = va;
1289 return SIGSEGV;
1290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 DITOREG(val, MIPSInst_FD(ir));
1292 break;
1293
1294 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001295 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 xcp->regs[MIPSInst_FT(ir)]);
1297
David Daneyb6ee75e2009-11-05 11:34:26 -08001298 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001300 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001301 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001302 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return SIGBUS;
1304 }
David Daney515b0292010-10-21 16:32:26 -07001305 if (__put_user(val, va)) {
1306 MIPS_FPU_EMU_INC_STATS(errors);
1307 *fault_addr = va;
1308 return SIGSEGV;
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 break;
1311
1312 case madd_d_op:
1313 handler = fpemu_dp_madd;
1314 goto dcoptop;
1315 case msub_d_op:
1316 handler = fpemu_dp_msub;
1317 goto dcoptop;
1318 case nmadd_d_op:
1319 handler = fpemu_dp_nmadd;
1320 goto dcoptop;
1321 case nmsub_d_op:
1322 handler = fpemu_dp_nmsub;
1323 goto dcoptop;
1324
1325 dcoptop:
1326 DPFROMREG(fr, MIPSInst_FR(ir));
1327 DPFROMREG(fs, MIPSInst_FS(ir));
1328 DPFROMREG(ft, MIPSInst_FT(ir));
1329 fd = (*handler) (fr, fs, ft);
1330 DPTOREG(fd, MIPSInst_FD(ir));
1331 goto copcsr;
1332
1333 default:
1334 return SIGILL;
1335 }
1336 break;
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001339 case 0x3:
1340 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /* ignore prefx operation */
1344 break;
1345
1346 default:
1347 return SIGILL;
1348 }
1349
1350 return 0;
1351}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353
1354
1355/*
1356 * Emulate a single COP1 arithmetic instruction.
1357 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001358static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 mips_instruction ir)
1360{
1361 int rfmt; /* resulting format */
1362 unsigned rcsr = 0; /* resulting csr */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001363 unsigned int oldrm;
1364 unsigned int cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 unsigned cond;
1366 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001367 union ieee754dp d;
1368 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 s64 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 } rv; /* resulting value */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001372 u64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
David Daneyb6ee75e2009-11-05 11:34:26 -08001374 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001376 case s_fmt: { /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001378 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1379 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 } handler;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001381 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 switch (MIPSInst_FUNC(ir)) {
1384 /* binary ops */
1385 case fadd_op:
1386 handler.b = ieee754sp_add;
1387 goto scopbop;
1388 case fsub_op:
1389 handler.b = ieee754sp_sub;
1390 goto scopbop;
1391 case fmul_op:
1392 handler.b = ieee754sp_mul;
1393 goto scopbop;
1394 case fdiv_op:
1395 handler.b = ieee754sp_div;
1396 goto scopbop;
1397
1398 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001400 if (!cpu_has_mips_4_5_r)
1401 return SIGILL;
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 handler.u = ieee754sp_sqrt;
1404 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001405
Ralf Baechle08a07902014-04-19 13:11:37 +02001406 /*
1407 * Note that on some MIPS IV implementations such as the
1408 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1409 * achieve full IEEE-754 accuracy - however this emulator does.
1410 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001412 if (!cpu_has_mips_4_5_r2)
1413 return SIGILL;
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 handler.u = fpemu_sp_rsqrt;
1416 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001419 if (!cpu_has_mips_4_5_r2)
1420 return SIGILL;
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 handler.u = fpemu_sp_recip;
1423 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001426 if (!cpu_has_mips_4_5_r)
1427 return SIGILL;
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1430 if (((ctx->fcr31 & cond) != 0) !=
1431 ((MIPSInst_FT(ir) & 1) != 0))
1432 return 0;
1433 SPFROMREG(rv.s, MIPSInst_FS(ir));
1434 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001437 if (!cpu_has_mips_4_5_r)
1438 return SIGILL;
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1441 return 0;
1442 SPFROMREG(rv.s, MIPSInst_FS(ir));
1443 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001446 if (!cpu_has_mips_4_5_r)
1447 return SIGILL;
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1450 return 0;
1451 SPFROMREG(rv.s, MIPSInst_FS(ir));
1452 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 case fabs_op:
1455 handler.u = ieee754sp_abs;
1456 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 case fneg_op:
1459 handler.u = ieee754sp_neg;
1460 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 case fmov_op:
1463 /* an easy one */
1464 SPFROMREG(rv.s, MIPSInst_FS(ir));
1465 goto copcsr;
1466
1467 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001468scopbop:
1469 SPFROMREG(fs, MIPSInst_FS(ir));
1470 SPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001472 rv.s = (*handler.b) (fs, ft);
1473 goto copcsr;
1474scopuop:
1475 SPFROMREG(fs, MIPSInst_FS(ir));
1476 rv.s = (*handler.u) (fs);
1477 goto copcsr;
1478copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001479 if (ieee754_cxtest(IEEE754_INEXACT)) {
1480 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001482 }
1483 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1484 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001486 }
1487 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1488 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001490 }
1491 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1492 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001494 }
1495 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1496 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 break;
1500
1501 /* unary conv ops */
1502 case fcvts_op:
1503 return SIGILL; /* not defined */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001505 case fcvtd_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 SPFROMREG(fs, MIPSInst_FS(ir));
1507 rv.d = ieee754dp_fsp(fs);
1508 rfmt = d_fmt;
1509 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001511 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 SPFROMREG(fs, MIPSInst_FS(ir));
1513 rv.w = ieee754sp_tint(fs);
1514 rfmt = w_fmt;
1515 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 case fround_op:
1518 case ftrunc_op:
1519 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001520 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001521 if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
1522 return SIGILL;
1523
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001524 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 SPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001526 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 rv.w = ieee754sp_tint(fs);
1528 ieee754_csr.rm = oldrm;
1529 rfmt = w_fmt;
1530 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001532 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001533 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1534 return SIGILL;
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 SPFROMREG(fs, MIPSInst_FS(ir));
1537 rv.l = ieee754sp_tlong(fs);
1538 rfmt = l_fmt;
1539 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 case froundl_op:
1542 case ftruncl_op:
1543 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001544 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001545 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1546 return SIGILL;
1547
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001548 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 SPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001550 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 rv.l = ieee754sp_tlong(fs);
1552 ieee754_csr.rm = oldrm;
1553 rfmt = l_fmt;
1554 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 default:
1557 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1558 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001559 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 SPFROMREG(fs, MIPSInst_FS(ir));
1562 SPFROMREG(ft, MIPSInst_FT(ir));
1563 rv.w = ieee754sp_cmp(fs, ft,
1564 cmptab[cmpop & 0x7], cmpop & 0x8);
1565 rfmt = -1;
1566 if ((cmpop & 0x8) && ieee754_cxtest
1567 (IEEE754_INVALID_OPERATION))
1568 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1569 else
1570 goto copcsr;
1571
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001572 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 break;
1575 }
1576 break;
1577 }
1578
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001579 case d_fmt: {
1580 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001582 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1583 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 } handler;
1585
1586 switch (MIPSInst_FUNC(ir)) {
1587 /* binary ops */
1588 case fadd_op:
1589 handler.b = ieee754dp_add;
1590 goto dcopbop;
1591 case fsub_op:
1592 handler.b = ieee754dp_sub;
1593 goto dcopbop;
1594 case fmul_op:
1595 handler.b = ieee754dp_mul;
1596 goto dcopbop;
1597 case fdiv_op:
1598 handler.b = ieee754dp_div;
1599 goto dcopbop;
1600
1601 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001603 if (!cpu_has_mips_2_3_4_5_r)
1604 return SIGILL;
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 handler.u = ieee754dp_sqrt;
1607 goto dcopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001608 /*
1609 * Note that on some MIPS IV implementations such as the
1610 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1611 * achieve full IEEE-754 accuracy - however this emulator does.
1612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001614 if (!cpu_has_mips_4_5_r2)
1615 return SIGILL;
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 handler.u = fpemu_dp_rsqrt;
1618 goto dcopuop;
1619 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001620 if (!cpu_has_mips_4_5_r2)
1621 return SIGILL;
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 handler.u = fpemu_dp_recip;
1624 goto dcopuop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001626 if (!cpu_has_mips_4_5_r)
1627 return SIGILL;
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1630 if (((ctx->fcr31 & cond) != 0) !=
1631 ((MIPSInst_FT(ir) & 1) != 0))
1632 return 0;
1633 DPFROMREG(rv.d, MIPSInst_FS(ir));
1634 break;
1635 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001636 if (!cpu_has_mips_4_5_r)
1637 return SIGILL;
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1640 return 0;
1641 DPFROMREG(rv.d, MIPSInst_FS(ir));
1642 break;
1643 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001644 if (!cpu_has_mips_4_5_r)
1645 return SIGILL;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1648 return 0;
1649 DPFROMREG(rv.d, MIPSInst_FS(ir));
1650 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 case fabs_op:
1652 handler.u = ieee754dp_abs;
1653 goto dcopuop;
1654
1655 case fneg_op:
1656 handler.u = ieee754dp_neg;
1657 goto dcopuop;
1658
1659 case fmov_op:
1660 /* an easy one */
1661 DPFROMREG(rv.d, MIPSInst_FS(ir));
1662 goto copcsr;
1663
1664 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001665dcopbop:
1666 DPFROMREG(fs, MIPSInst_FS(ir));
1667 DPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001669 rv.d = (*handler.b) (fs, ft);
1670 goto copcsr;
1671dcopuop:
1672 DPFROMREG(fs, MIPSInst_FS(ir));
1673 rv.d = (*handler.u) (fs);
1674 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001676 /*
1677 * unary conv ops
1678 */
1679 case fcvts_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 DPFROMREG(fs, MIPSInst_FS(ir));
1681 rv.s = ieee754sp_fdp(fs);
1682 rfmt = s_fmt;
1683 goto copcsr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 case fcvtd_op:
1686 return SIGILL; /* not defined */
1687
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001688 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 DPFROMREG(fs, MIPSInst_FS(ir));
1690 rv.w = ieee754dp_tint(fs); /* wrong */
1691 rfmt = w_fmt;
1692 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 case fround_op:
1695 case ftrunc_op:
1696 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001697 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001698 if (!cpu_has_mips_2_3_4_5_r)
1699 return SIGILL;
1700
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001701 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 DPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001703 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 rv.w = ieee754dp_tint(fs);
1705 ieee754_csr.rm = oldrm;
1706 rfmt = w_fmt;
1707 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001709 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001710 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1711 return SIGILL;
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 DPFROMREG(fs, MIPSInst_FS(ir));
1714 rv.l = ieee754dp_tlong(fs);
1715 rfmt = l_fmt;
1716 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 case froundl_op:
1719 case ftruncl_op:
1720 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001721 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001722 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1723 return SIGILL;
1724
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001725 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 DPFROMREG(fs, MIPSInst_FS(ir));
Ralf Baechle56a64732014-04-30 11:21:55 +02001727 ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 rv.l = ieee754dp_tlong(fs);
1729 ieee754_csr.rm = oldrm;
1730 rfmt = l_fmt;
1731 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 default:
1734 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1735 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001736 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 DPFROMREG(fs, MIPSInst_FS(ir));
1739 DPFROMREG(ft, MIPSInst_FT(ir));
1740 rv.w = ieee754dp_cmp(fs, ft,
1741 cmptab[cmpop & 0x7], cmpop & 0x8);
1742 rfmt = -1;
1743 if ((cmpop & 0x8)
1744 &&
1745 ieee754_cxtest
1746 (IEEE754_INVALID_OPERATION))
1747 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1748 else
1749 goto copcsr;
1750
1751 }
1752 else {
1753 return SIGILL;
1754 }
1755 break;
1756 }
1757 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001759 case w_fmt:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 switch (MIPSInst_FUNC(ir)) {
1761 case fcvts_op:
1762 /* convert word to single precision real */
1763 SPFROMREG(fs, MIPSInst_FS(ir));
1764 rv.s = ieee754sp_fint(fs.bits);
1765 rfmt = s_fmt;
1766 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 case fcvtd_op:
1768 /* convert word to double precision real */
1769 SPFROMREG(fs, MIPSInst_FS(ir));
1770 rv.d = ieee754dp_fint(fs.bits);
1771 rfmt = d_fmt;
1772 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 default:
1774 return SIGILL;
1775 }
1776 break;
1777 }
1778
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001779 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001780
1781 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1782 return SIGILL;
1783
Paul Burtonbbd426f2014-02-13 11:26:41 +00001784 DIFROMREG(bits, MIPSInst_FS(ir));
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 switch (MIPSInst_FUNC(ir)) {
1787 case fcvts_op:
1788 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001789 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 rfmt = s_fmt;
1791 goto copcsr;
1792 case fcvtd_op:
1793 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001794 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 rfmt = d_fmt;
1796 goto copcsr;
1797 default:
1798 return SIGILL;
1799 }
1800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 default:
1803 return SIGILL;
1804 }
1805
1806 /*
1807 * Update the fpu CSR register for this operation.
1808 * If an exception is required, generate a tidy SIGFPE exception,
1809 * without updating the result register.
1810 * Note: cause exception bits do not accumulate, they are rewritten
1811 * for each op; only the flag/sticky bits accumulate.
1812 */
1813 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
1814 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001815 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return SIGFPE;
1817 }
1818
1819 /*
1820 * Now we can safely write the result back to the register file.
1821 */
1822 switch (rfmt) {
Ralf Baechle08a07902014-04-19 13:11:37 +02001823 case -1:
1824
1825 if (cpu_has_mips_4_5_r)
Rob Kendrickc3b9b942014-07-23 10:03:58 +01001826 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 else
Ralf Baechle08a07902014-04-19 13:11:37 +02001828 cbit = FPU_CSR_COND;
1829 if (rv.w)
1830 ctx->fcr31 |= cbit;
1831 else
1832 ctx->fcr31 &= ~cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 break;
Ralf Baechle08a07902014-04-19 13:11:37 +02001834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 case d_fmt:
1836 DPTOREG(rv.d, MIPSInst_FD(ir));
1837 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 case s_fmt:
1839 SPTOREG(rv.s, MIPSInst_FD(ir));
1840 break;
1841 case w_fmt:
1842 SITOREG(rv.w, MIPSInst_FD(ir));
1843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001845 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1846 return SIGILL;
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 DITOREG(rv.l, MIPSInst_FD(ir));
1849 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 default:
1851 return SIGILL;
1852 }
1853
1854 return 0;
1855}
1856
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09001857int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001858 int has_fpu, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Ralf Baechle333d1f62005-02-28 17:55:57 +00001860 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001861 struct mm_decoded_insn dec_insn;
1862 u16 instr[4];
1863 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 int sig = 0;
1865
1866 oldepc = xcp->cp0_epc;
1867 do {
1868 prevepc = xcp->cp0_epc;
1869
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001870 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
1871 /*
1872 * Get next 2 microMIPS instructions and convert them
1873 * into 32-bit instructions.
1874 */
1875 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
1876 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
1877 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
1878 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
1879 MIPS_FPU_EMU_INC_STATS(errors);
1880 return SIGBUS;
1881 }
1882 instr_ptr = instr;
1883
1884 /* Get first instruction. */
1885 if (mm_insn_16bit(*instr_ptr)) {
1886 /* Duplicate the half-word. */
1887 dec_insn.insn = (*instr_ptr << 16) |
1888 (*instr_ptr);
1889 /* 16-bit instruction. */
1890 dec_insn.pc_inc = 2;
1891 instr_ptr += 1;
1892 } else {
1893 dec_insn.insn = (*instr_ptr << 16) |
1894 *(instr_ptr+1);
1895 /* 32-bit instruction. */
1896 dec_insn.pc_inc = 4;
1897 instr_ptr += 2;
1898 }
1899 /* Get second instruction. */
1900 if (mm_insn_16bit(*instr_ptr)) {
1901 /* Duplicate the half-word. */
1902 dec_insn.next_insn = (*instr_ptr << 16) |
1903 (*instr_ptr);
1904 /* 16-bit instruction. */
1905 dec_insn.next_pc_inc = 2;
1906 } else {
1907 dec_insn.next_insn = (*instr_ptr << 16) |
1908 *(instr_ptr+1);
1909 /* 32-bit instruction. */
1910 dec_insn.next_pc_inc = 4;
1911 }
1912 dec_insn.micro_mips_mode = 1;
1913 } else {
1914 if ((get_user(dec_insn.insn,
1915 (mips_instruction __user *) xcp->cp0_epc)) ||
1916 (get_user(dec_insn.next_insn,
1917 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
1918 MIPS_FPU_EMU_INC_STATS(errors);
1919 return SIGBUS;
1920 }
1921 dec_insn.pc_inc = 4;
1922 dec_insn.next_pc_inc = 4;
1923 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001925
1926 if ((dec_insn.insn == 0) ||
1927 ((dec_insn.pc_inc == 2) &&
1928 ((dec_insn.insn & 0xffff) == MM_NOP16)))
1929 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00001931 /*
1932 * The 'ieee754_csr' is an alias of
Ralf Baechle70342282013-01-22 12:59:30 +01001933 * ctx->fcr31. No need to copy ctx->fcr31 to
1934 * ieee754_csr. But ieee754_csr.rm is ieee
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00001935 * library modes. (not mips rounding mode)
1936 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001937 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09001940 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 break;
1942 if (sig)
1943 break;
1944
1945 cond_resched();
1946 } while (xcp->cp0_epc > prevepc);
1947
1948 /* SIGILL indicates a non-fpu instruction */
1949 if (sig == SIGILL && xcp->cp0_epc != oldepc)
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001950 /* but if EPC has advanced, then ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 sig = 0;
1952
1953 return sig;
1954}