blob: 6258291354eb8df25896f4b0ec654f17ef29f210 [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
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050070/* microMIPS bitfields */
71#define MM_POOL32A_MINOR_MASK 0x3f
72#define MM_POOL32A_MINOR_SHIFT 0x6
73#define MM_MIPS32_COND_FC 0x30
74
Ralf Baechle3f7cac42014-04-26 01:49:14 +020075/* Convert MIPS rounding mode (0..3) to IEEE library modes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static const unsigned char ieee_rm[4] = {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000077 [FPU_CSR_RN] = IEEE754_RN,
78 [FPU_CSR_RZ] = IEEE754_RZ,
79 [FPU_CSR_RU] = IEEE754_RU,
80 [FPU_CSR_RD] = IEEE754_RD,
81};
Ralf Baechle3f7cac42014-04-26 01:49:14 +020082/* Convert IEEE library modes to MIPS rounding mode (0..3). */
Ralf Baechlecd21dfc2005-04-28 13:39:10 +000083static const unsigned char mips_rm[4] = {
84 [IEEE754_RN] = FPU_CSR_RN,
85 [IEEE754_RZ] = FPU_CSR_RZ,
86 [IEEE754_RD] = FPU_CSR_RD,
87 [IEEE754_RU] = FPU_CSR_RU,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* convert condition code register number to csr bit */
91static const unsigned int fpucondbit[8] = {
92 FPU_CSR_COND0,
93 FPU_CSR_COND1,
94 FPU_CSR_COND2,
95 FPU_CSR_COND3,
96 FPU_CSR_COND4,
97 FPU_CSR_COND5,
98 FPU_CSR_COND6,
99 FPU_CSR_COND7
100};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500102/* (microMIPS) Convert 16-bit register encoding to 32-bit register encoding. */
103static const unsigned int reg16to32map[8] = {16, 17, 2, 3, 4, 5, 6, 7};
104
105/* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
106static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
107static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
108static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
109static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
110
111/*
112 * This functions translates a 32-bit microMIPS instruction
113 * into a 32-bit MIPS32 instruction. Returns 0 on success
114 * and SIGILL otherwise.
115 */
116static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
117{
118 union mips_instruction insn = *insn_ptr;
119 union mips_instruction mips32_insn = insn;
120 int func, fmt, op;
121
122 switch (insn.mm_i_format.opcode) {
123 case mm_ldc132_op:
124 mips32_insn.mm_i_format.opcode = ldc1_op;
125 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
126 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
127 break;
128 case mm_lwc132_op:
129 mips32_insn.mm_i_format.opcode = lwc1_op;
130 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
131 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
132 break;
133 case mm_sdc132_op:
134 mips32_insn.mm_i_format.opcode = sdc1_op;
135 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
136 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
137 break;
138 case mm_swc132_op:
139 mips32_insn.mm_i_format.opcode = swc1_op;
140 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
141 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
142 break;
143 case mm_pool32i_op:
144 /* NOTE: offset is << by 1 if in microMIPS mode. */
145 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
146 (insn.mm_i_format.rt == mm_bc1t_op)) {
147 mips32_insn.fb_format.opcode = cop1_op;
148 mips32_insn.fb_format.bc = bc_op;
149 mips32_insn.fb_format.flag =
150 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
151 } else
152 return SIGILL;
153 break;
154 case mm_pool32f_op:
155 switch (insn.mm_fp0_format.func) {
156 case mm_32f_01_op:
157 case mm_32f_11_op:
158 case mm_32f_02_op:
159 case mm_32f_12_op:
160 case mm_32f_41_op:
161 case mm_32f_51_op:
162 case mm_32f_42_op:
163 case mm_32f_52_op:
164 op = insn.mm_fp0_format.func;
165 if (op == mm_32f_01_op)
166 func = madd_s_op;
167 else if (op == mm_32f_11_op)
168 func = madd_d_op;
169 else if (op == mm_32f_02_op)
170 func = nmadd_s_op;
171 else if (op == mm_32f_12_op)
172 func = nmadd_d_op;
173 else if (op == mm_32f_41_op)
174 func = msub_s_op;
175 else if (op == mm_32f_51_op)
176 func = msub_d_op;
177 else if (op == mm_32f_42_op)
178 func = nmsub_s_op;
179 else
180 func = nmsub_d_op;
181 mips32_insn.fp6_format.opcode = cop1x_op;
182 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
183 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
184 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
185 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
186 mips32_insn.fp6_format.func = func;
187 break;
188 case mm_32f_10_op:
189 func = -1; /* Invalid */
190 op = insn.mm_fp5_format.op & 0x7;
191 if (op == mm_ldxc1_op)
192 func = ldxc1_op;
193 else if (op == mm_sdxc1_op)
194 func = sdxc1_op;
195 else if (op == mm_lwxc1_op)
196 func = lwxc1_op;
197 else if (op == mm_swxc1_op)
198 func = swxc1_op;
199
200 if (func != -1) {
201 mips32_insn.r_format.opcode = cop1x_op;
202 mips32_insn.r_format.rs =
203 insn.mm_fp5_format.base;
204 mips32_insn.r_format.rt =
205 insn.mm_fp5_format.index;
206 mips32_insn.r_format.rd = 0;
207 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
208 mips32_insn.r_format.func = func;
209 } else
210 return SIGILL;
211 break;
212 case mm_32f_40_op:
213 op = -1; /* Invalid */
214 if (insn.mm_fp2_format.op == mm_fmovt_op)
215 op = 1;
216 else if (insn.mm_fp2_format.op == mm_fmovf_op)
217 op = 0;
218 if (op != -1) {
219 mips32_insn.fp0_format.opcode = cop1_op;
220 mips32_insn.fp0_format.fmt =
221 sdps_format[insn.mm_fp2_format.fmt];
222 mips32_insn.fp0_format.ft =
223 (insn.mm_fp2_format.cc<<2) + op;
224 mips32_insn.fp0_format.fs =
225 insn.mm_fp2_format.fs;
226 mips32_insn.fp0_format.fd =
227 insn.mm_fp2_format.fd;
228 mips32_insn.fp0_format.func = fmovc_op;
229 } else
230 return SIGILL;
231 break;
232 case mm_32f_60_op:
233 func = -1; /* Invalid */
234 if (insn.mm_fp0_format.op == mm_fadd_op)
235 func = fadd_op;
236 else if (insn.mm_fp0_format.op == mm_fsub_op)
237 func = fsub_op;
238 else if (insn.mm_fp0_format.op == mm_fmul_op)
239 func = fmul_op;
240 else if (insn.mm_fp0_format.op == mm_fdiv_op)
241 func = fdiv_op;
242 if (func != -1) {
243 mips32_insn.fp0_format.opcode = cop1_op;
244 mips32_insn.fp0_format.fmt =
245 sdps_format[insn.mm_fp0_format.fmt];
246 mips32_insn.fp0_format.ft =
247 insn.mm_fp0_format.ft;
248 mips32_insn.fp0_format.fs =
249 insn.mm_fp0_format.fs;
250 mips32_insn.fp0_format.fd =
251 insn.mm_fp0_format.fd;
252 mips32_insn.fp0_format.func = func;
253 } else
254 return SIGILL;
255 break;
256 case mm_32f_70_op:
257 func = -1; /* Invalid */
258 if (insn.mm_fp0_format.op == mm_fmovn_op)
259 func = fmovn_op;
260 else if (insn.mm_fp0_format.op == mm_fmovz_op)
261 func = fmovz_op;
262 if (func != -1) {
263 mips32_insn.fp0_format.opcode = cop1_op;
264 mips32_insn.fp0_format.fmt =
265 sdps_format[insn.mm_fp0_format.fmt];
266 mips32_insn.fp0_format.ft =
267 insn.mm_fp0_format.ft;
268 mips32_insn.fp0_format.fs =
269 insn.mm_fp0_format.fs;
270 mips32_insn.fp0_format.fd =
271 insn.mm_fp0_format.fd;
272 mips32_insn.fp0_format.func = func;
273 } else
274 return SIGILL;
275 break;
276 case mm_32f_73_op: /* POOL32FXF */
277 switch (insn.mm_fp1_format.op) {
278 case mm_movf0_op:
279 case mm_movf1_op:
280 case mm_movt0_op:
281 case mm_movt1_op:
282 if ((insn.mm_fp1_format.op & 0x7f) ==
283 mm_movf0_op)
284 op = 0;
285 else
286 op = 1;
287 mips32_insn.r_format.opcode = spec_op;
288 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
289 mips32_insn.r_format.rt =
290 (insn.mm_fp4_format.cc << 2) + op;
291 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
292 mips32_insn.r_format.re = 0;
293 mips32_insn.r_format.func = movc_op;
294 break;
295 case mm_fcvtd0_op:
296 case mm_fcvtd1_op:
297 case mm_fcvts0_op:
298 case mm_fcvts1_op:
299 if ((insn.mm_fp1_format.op & 0x7f) ==
300 mm_fcvtd0_op) {
301 func = fcvtd_op;
302 fmt = swl_format[insn.mm_fp3_format.fmt];
303 } else {
304 func = fcvts_op;
305 fmt = dwl_format[insn.mm_fp3_format.fmt];
306 }
307 mips32_insn.fp0_format.opcode = cop1_op;
308 mips32_insn.fp0_format.fmt = fmt;
309 mips32_insn.fp0_format.ft = 0;
310 mips32_insn.fp0_format.fs =
311 insn.mm_fp3_format.fs;
312 mips32_insn.fp0_format.fd =
313 insn.mm_fp3_format.rt;
314 mips32_insn.fp0_format.func = func;
315 break;
316 case mm_fmov0_op:
317 case mm_fmov1_op:
318 case mm_fabs0_op:
319 case mm_fabs1_op:
320 case mm_fneg0_op:
321 case mm_fneg1_op:
322 if ((insn.mm_fp1_format.op & 0x7f) ==
323 mm_fmov0_op)
324 func = fmov_op;
325 else if ((insn.mm_fp1_format.op & 0x7f) ==
326 mm_fabs0_op)
327 func = fabs_op;
328 else
329 func = fneg_op;
330 mips32_insn.fp0_format.opcode = cop1_op;
331 mips32_insn.fp0_format.fmt =
332 sdps_format[insn.mm_fp3_format.fmt];
333 mips32_insn.fp0_format.ft = 0;
334 mips32_insn.fp0_format.fs =
335 insn.mm_fp3_format.fs;
336 mips32_insn.fp0_format.fd =
337 insn.mm_fp3_format.rt;
338 mips32_insn.fp0_format.func = func;
339 break;
340 case mm_ffloorl_op:
341 case mm_ffloorw_op:
342 case mm_fceill_op:
343 case mm_fceilw_op:
344 case mm_ftruncl_op:
345 case mm_ftruncw_op:
346 case mm_froundl_op:
347 case mm_froundw_op:
348 case mm_fcvtl_op:
349 case mm_fcvtw_op:
350 if (insn.mm_fp1_format.op == mm_ffloorl_op)
351 func = ffloorl_op;
352 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
353 func = ffloor_op;
354 else if (insn.mm_fp1_format.op == mm_fceill_op)
355 func = fceill_op;
356 else if (insn.mm_fp1_format.op == mm_fceilw_op)
357 func = fceil_op;
358 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
359 func = ftruncl_op;
360 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
361 func = ftrunc_op;
362 else if (insn.mm_fp1_format.op == mm_froundl_op)
363 func = froundl_op;
364 else if (insn.mm_fp1_format.op == mm_froundw_op)
365 func = fround_op;
366 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
367 func = fcvtl_op;
368 else
369 func = fcvtw_op;
370 mips32_insn.fp0_format.opcode = cop1_op;
371 mips32_insn.fp0_format.fmt =
372 sd_format[insn.mm_fp1_format.fmt];
373 mips32_insn.fp0_format.ft = 0;
374 mips32_insn.fp0_format.fs =
375 insn.mm_fp1_format.fs;
376 mips32_insn.fp0_format.fd =
377 insn.mm_fp1_format.rt;
378 mips32_insn.fp0_format.func = func;
379 break;
380 case mm_frsqrt_op:
381 case mm_fsqrt_op:
382 case mm_frecip_op:
383 if (insn.mm_fp1_format.op == mm_frsqrt_op)
384 func = frsqrt_op;
385 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
386 func = fsqrt_op;
387 else
388 func = frecip_op;
389 mips32_insn.fp0_format.opcode = cop1_op;
390 mips32_insn.fp0_format.fmt =
391 sdps_format[insn.mm_fp1_format.fmt];
392 mips32_insn.fp0_format.ft = 0;
393 mips32_insn.fp0_format.fs =
394 insn.mm_fp1_format.fs;
395 mips32_insn.fp0_format.fd =
396 insn.mm_fp1_format.rt;
397 mips32_insn.fp0_format.func = func;
398 break;
399 case mm_mfc1_op:
400 case mm_mtc1_op:
401 case mm_cfc1_op:
402 case mm_ctc1_op:
Steven J. Hill9355e592013-11-07 12:48:29 +0000403 case mm_mfhc1_op:
404 case mm_mthc1_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500405 if (insn.mm_fp1_format.op == mm_mfc1_op)
406 op = mfc_op;
407 else if (insn.mm_fp1_format.op == mm_mtc1_op)
408 op = mtc_op;
409 else if (insn.mm_fp1_format.op == mm_cfc1_op)
410 op = cfc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000411 else if (insn.mm_fp1_format.op == mm_ctc1_op)
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500412 op = ctc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000413 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
414 op = mfhc_op;
415 else
416 op = mthc_op;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500417 mips32_insn.fp1_format.opcode = cop1_op;
418 mips32_insn.fp1_format.op = op;
419 mips32_insn.fp1_format.rt =
420 insn.mm_fp1_format.rt;
421 mips32_insn.fp1_format.fs =
422 insn.mm_fp1_format.fs;
423 mips32_insn.fp1_format.fd = 0;
424 mips32_insn.fp1_format.func = 0;
425 break;
426 default:
427 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500428 }
429 break;
430 case mm_32f_74_op: /* c.cond.fmt */
431 mips32_insn.fp0_format.opcode = cop1_op;
432 mips32_insn.fp0_format.fmt =
433 sdps_format[insn.mm_fp4_format.fmt];
434 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
435 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
436 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
437 mips32_insn.fp0_format.func =
438 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
439 break;
440 default:
441 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500442 }
443 break;
444 default:
445 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500446 }
447
448 *insn_ptr = mips32_insn;
449 return 0;
450}
451
452int mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
453 unsigned long *contpc)
454{
455 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
456 int bc_false = 0;
457 unsigned int fcr31;
458 unsigned int bit;
459
David Daneyfe6d2902013-05-24 20:54:09 +0000460 if (!cpu_has_mmips)
461 return 0;
462
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500463 switch (insn.mm_i_format.opcode) {
464 case mm_pool32a_op:
465 if ((insn.mm_i_format.simmediate & MM_POOL32A_MINOR_MASK) ==
466 mm_pool32axf_op) {
467 switch (insn.mm_i_format.simmediate >>
468 MM_POOL32A_MINOR_SHIFT) {
469 case mm_jalr_op:
470 case mm_jalrhb_op:
471 case mm_jalrs_op:
472 case mm_jalrshb_op:
473 if (insn.mm_i_format.rt != 0) /* Not mm_jr */
474 regs->regs[insn.mm_i_format.rt] =
475 regs->cp0_epc +
476 dec_insn.pc_inc +
477 dec_insn.next_pc_inc;
478 *contpc = regs->regs[insn.mm_i_format.rs];
479 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500480 }
481 }
482 break;
483 case mm_pool32i_op:
484 switch (insn.mm_i_format.rt) {
485 case mm_bltzals_op:
486 case mm_bltzal_op:
487 regs->regs[31] = regs->cp0_epc +
488 dec_insn.pc_inc +
489 dec_insn.next_pc_inc;
490 /* Fall through */
491 case mm_bltz_op:
492 if ((long)regs->regs[insn.mm_i_format.rs] < 0)
493 *contpc = regs->cp0_epc +
494 dec_insn.pc_inc +
495 (insn.mm_i_format.simmediate << 1);
496 else
497 *contpc = regs->cp0_epc +
498 dec_insn.pc_inc +
499 dec_insn.next_pc_inc;
500 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500501 case mm_bgezals_op:
502 case mm_bgezal_op:
503 regs->regs[31] = regs->cp0_epc +
504 dec_insn.pc_inc +
505 dec_insn.next_pc_inc;
506 /* Fall through */
507 case mm_bgez_op:
508 if ((long)regs->regs[insn.mm_i_format.rs] >= 0)
509 *contpc = regs->cp0_epc +
510 dec_insn.pc_inc +
511 (insn.mm_i_format.simmediate << 1);
512 else
513 *contpc = regs->cp0_epc +
514 dec_insn.pc_inc +
515 dec_insn.next_pc_inc;
516 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500517 case mm_blez_op:
518 if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
519 *contpc = regs->cp0_epc +
520 dec_insn.pc_inc +
521 (insn.mm_i_format.simmediate << 1);
522 else
523 *contpc = regs->cp0_epc +
524 dec_insn.pc_inc +
525 dec_insn.next_pc_inc;
526 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500527 case mm_bgtz_op:
528 if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
529 *contpc = regs->cp0_epc +
530 dec_insn.pc_inc +
531 (insn.mm_i_format.simmediate << 1);
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 mm_bc2f_op:
538 case mm_bc1f_op:
539 bc_false = 1;
540 /* Fall through */
541 case mm_bc2t_op:
542 case mm_bc1t_op:
543 preempt_disable();
544 if (is_fpu_owner())
545 asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
546 else
547 fcr31 = current->thread.fpu.fcr31;
548 preempt_enable();
549
550 if (bc_false)
551 fcr31 = ~fcr31;
552
553 bit = (insn.mm_i_format.rs >> 2);
554 bit += (bit != 0);
555 bit += 23;
556 if (fcr31 & (1 << bit))
557 *contpc = regs->cp0_epc +
558 dec_insn.pc_inc +
559 (insn.mm_i_format.simmediate << 1);
560 else
561 *contpc = regs->cp0_epc +
562 dec_insn.pc_inc + dec_insn.next_pc_inc;
563 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500564 }
565 break;
566 case mm_pool16c_op:
567 switch (insn.mm_i_format.rt) {
568 case mm_jalr16_op:
569 case mm_jalrs16_op:
570 regs->regs[31] = regs->cp0_epc +
571 dec_insn.pc_inc + dec_insn.next_pc_inc;
572 /* Fall through */
573 case mm_jr16_op:
574 *contpc = regs->regs[insn.mm_i_format.rs];
575 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500576 }
577 break;
578 case mm_beqz16_op:
579 if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] == 0)
580 *contpc = regs->cp0_epc +
581 dec_insn.pc_inc +
582 (insn.mm_b1_format.simmediate << 1);
583 else
584 *contpc = regs->cp0_epc +
585 dec_insn.pc_inc + dec_insn.next_pc_inc;
586 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500587 case mm_bnez16_op:
588 if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] != 0)
589 *contpc = regs->cp0_epc +
590 dec_insn.pc_inc +
591 (insn.mm_b1_format.simmediate << 1);
592 else
593 *contpc = regs->cp0_epc +
594 dec_insn.pc_inc + dec_insn.next_pc_inc;
595 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500596 case mm_b16_op:
597 *contpc = regs->cp0_epc + dec_insn.pc_inc +
598 (insn.mm_b0_format.simmediate << 1);
599 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500600 case mm_beq32_op:
601 if (regs->regs[insn.mm_i_format.rs] ==
602 regs->regs[insn.mm_i_format.rt])
603 *contpc = regs->cp0_epc +
604 dec_insn.pc_inc +
605 (insn.mm_i_format.simmediate << 1);
606 else
607 *contpc = regs->cp0_epc +
608 dec_insn.pc_inc +
609 dec_insn.next_pc_inc;
610 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500611 case mm_bne32_op:
612 if (regs->regs[insn.mm_i_format.rs] !=
613 regs->regs[insn.mm_i_format.rt])
614 *contpc = regs->cp0_epc +
615 dec_insn.pc_inc +
616 (insn.mm_i_format.simmediate << 1);
617 else
618 *contpc = regs->cp0_epc +
619 dec_insn.pc_inc + dec_insn.next_pc_inc;
620 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500621 case mm_jalx32_op:
622 regs->regs[31] = regs->cp0_epc +
623 dec_insn.pc_inc + dec_insn.next_pc_inc;
624 *contpc = regs->cp0_epc + dec_insn.pc_inc;
625 *contpc >>= 28;
626 *contpc <<= 28;
627 *contpc |= (insn.j_format.target << 2);
628 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500629 case mm_jals32_op:
630 case mm_jal32_op:
631 regs->regs[31] = regs->cp0_epc +
632 dec_insn.pc_inc + dec_insn.next_pc_inc;
633 /* Fall through */
634 case mm_j32_op:
635 *contpc = regs->cp0_epc + dec_insn.pc_inc;
636 *contpc >>= 27;
637 *contpc <<= 27;
638 *contpc |= (insn.j_format.target << 1);
639 set_isa16_mode(*contpc);
640 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500641 }
642 return 0;
643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645/*
646 * Redundant with logic already in kernel/branch.c,
647 * embedded in compute_return_epc. At some point,
648 * a single subroutine should be used across both
649 * modules.
650 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500651static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
652 unsigned long *contpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500654 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
655 unsigned int fcr31;
656 unsigned int bit = 0;
657
658 switch (insn.i_format.opcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 case spec_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500660 switch (insn.r_format.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 case jalr_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500662 regs->regs[insn.r_format.rd] =
663 regs->cp0_epc + dec_insn.pc_inc +
664 dec_insn.next_pc_inc;
665 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 case jr_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500667 *contpc = regs->regs[insn.r_format.rs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 1;
669 }
670 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 case bcond_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500672 switch (insn.i_format.rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 case bltzal_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 case bltzall_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500675 regs->regs[31] = regs->cp0_epc +
676 dec_insn.pc_inc +
677 dec_insn.next_pc_inc;
678 /* Fall through */
679 case bltz_op:
680 case bltzl_op:
681 if ((long)regs->regs[insn.i_format.rs] < 0)
682 *contpc = regs->cp0_epc +
683 dec_insn.pc_inc +
684 (insn.i_format.simmediate << 2);
685 else
686 *contpc = regs->cp0_epc +
687 dec_insn.pc_inc +
688 dec_insn.next_pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500690 case bgezal_op:
691 case bgezall_op:
692 regs->regs[31] = regs->cp0_epc +
693 dec_insn.pc_inc +
694 dec_insn.next_pc_inc;
695 /* Fall through */
696 case bgez_op:
697 case bgezl_op:
698 if ((long)regs->regs[insn.i_format.rs] >= 0)
699 *contpc = regs->cp0_epc +
700 dec_insn.pc_inc +
701 (insn.i_format.simmediate << 2);
702 else
703 *contpc = regs->cp0_epc +
704 dec_insn.pc_inc +
705 dec_insn.next_pc_inc;
706 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 case jalx_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500710 set_isa16_mode(bit);
711 case jal_op:
712 regs->regs[31] = regs->cp0_epc +
713 dec_insn.pc_inc +
714 dec_insn.next_pc_inc;
715 /* Fall through */
716 case j_op:
717 *contpc = regs->cp0_epc + dec_insn.pc_inc;
718 *contpc >>= 28;
719 *contpc <<= 28;
720 *contpc |= (insn.j_format.target << 2);
721 /* Set microMIPS mode bit: XOR for jalx. */
722 *contpc ^= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500724 case beq_op:
725 case beql_op:
726 if (regs->regs[insn.i_format.rs] ==
727 regs->regs[insn.i_format.rt])
728 *contpc = regs->cp0_epc +
729 dec_insn.pc_inc +
730 (insn.i_format.simmediate << 2);
731 else
732 *contpc = regs->cp0_epc +
733 dec_insn.pc_inc +
734 dec_insn.next_pc_inc;
735 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500736 case bne_op:
737 case bnel_op:
738 if (regs->regs[insn.i_format.rs] !=
739 regs->regs[insn.i_format.rt])
740 *contpc = regs->cp0_epc +
741 dec_insn.pc_inc +
742 (insn.i_format.simmediate << 2);
743 else
744 *contpc = regs->cp0_epc +
745 dec_insn.pc_inc +
746 dec_insn.next_pc_inc;
747 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500748 case blez_op:
749 case blezl_op:
750 if ((long)regs->regs[insn.i_format.rs] <= 0)
751 *contpc = regs->cp0_epc +
752 dec_insn.pc_inc +
753 (insn.i_format.simmediate << 2);
754 else
755 *contpc = regs->cp0_epc +
756 dec_insn.pc_inc +
757 dec_insn.next_pc_inc;
758 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500759 case bgtz_op:
760 case bgtzl_op:
761 if ((long)regs->regs[insn.i_format.rs] > 0)
762 *contpc = regs->cp0_epc +
763 dec_insn.pc_inc +
764 (insn.i_format.simmediate << 2);
765 else
766 *contpc = regs->cp0_epc +
767 dec_insn.pc_inc +
768 dec_insn.next_pc_inc;
769 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700770#ifdef CONFIG_CPU_CAVIUM_OCTEON
771 case lwc2_op: /* This is bbit0 on Octeon */
772 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
773 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
774 else
775 *contpc = regs->cp0_epc + 8;
776 return 1;
777 case ldc2_op: /* This is bbit032 on Octeon */
778 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
779 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
780 else
781 *contpc = regs->cp0_epc + 8;
782 return 1;
783 case swc2_op: /* This is bbit1 on Octeon */
784 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
785 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
786 else
787 *contpc = regs->cp0_epc + 8;
788 return 1;
789 case sdc2_op: /* This is bbit132 on Octeon */
790 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
791 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
792 else
793 *contpc = regs->cp0_epc + 8;
794 return 1;
795#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 case cop0_op:
797 case cop1_op:
798 case cop2_op:
799 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500800 if (insn.i_format.rs == bc_op) {
801 preempt_disable();
802 if (is_fpu_owner())
803 asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
804 else
805 fcr31 = current->thread.fpu.fcr31;
806 preempt_enable();
807
808 bit = (insn.i_format.rt >> 2);
809 bit += (bit != 0);
810 bit += 23;
811 switch (insn.i_format.rt & 3) {
812 case 0: /* bc1f */
813 case 2: /* bc1fl */
814 if (~fcr31 & (1 << bit))
815 *contpc = regs->cp0_epc +
816 dec_insn.pc_inc +
817 (insn.i_format.simmediate << 2);
818 else
819 *contpc = regs->cp0_epc +
820 dec_insn.pc_inc +
821 dec_insn.next_pc_inc;
822 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500823 case 1: /* bc1t */
824 case 3: /* bc1tl */
825 if (fcr31 & (1 << bit))
826 *contpc = regs->cp0_epc +
827 dec_insn.pc_inc +
828 (insn.i_format.simmediate << 2);
829 else
830 *contpc = regs->cp0_epc +
831 dec_insn.pc_inc +
832 dec_insn.next_pc_inc;
833 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500834 }
835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return 0;
839}
840
841/*
842 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100843 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800844 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000845 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200846 * FPU emu is slow and bulky and optimizing this function offers fairly
847 * sizeable benefits so we try to be clever and make this function return
848 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000849 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 */
David Daneyda0bac32009-11-02 11:33:46 -0800851static inline int cop1_64bit(struct pt_regs *xcp)
852{
Ralf Baechle08a07902014-04-19 13:11:37 +0200853 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
854 return 1;
855 else if (config_enabled(CONFIG_32BIT) &&
856 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
857 return 0;
858
Paul Burton597ce172013-11-22 13:12:07 +0000859 return !test_thread_flag(TIF_32BIT_FPREGS);
David Daneyda0bac32009-11-02 11:33:46 -0800860}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200862#define SIFROMREG(si, x) \
863do { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000864 if (cop1_64bit(xcp)) \
865 (si) = get_fpr32(&ctx->fpr[x], 0); \
866 else \
867 (si) = get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
868} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800869
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200870#define SITOREG(si, x) \
871do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000872 if (cop1_64bit(xcp)) { \
873 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000874 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000875 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
876 set_fpr32(&ctx->fpr[x], i, 0); \
877 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000878 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000879 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000880} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Paul Burtonbbd426f2014-02-13 11:26:41 +0000882#define SIFROMHREG(si, x) ((si) = get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000883
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200884#define SITOHREG(si, x) \
885do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000886 unsigned i; \
887 set_fpr32(&ctx->fpr[x], 1, si); \
888 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
889 set_fpr32(&ctx->fpr[x], i, 0); \
890} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000891
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200892#define DIFROMREG(di, x) \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000893 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
894
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200895#define DITOREG(di, x) \
896do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000897 unsigned fpr, i; \
898 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
899 set_fpr64(&ctx->fpr[fpr], 0, di); \
900 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
901 set_fpr64(&ctx->fpr[fpr], i, 0); \
902} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Ralf Baechle21a151d2007-10-11 23:46:15 +0100904#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
905#define SPTOREG(sp, x) SITOREG((sp).bits, x)
906#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
907#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909/*
910 * Emulate the single floating point instruction pointed at by EPC.
911 * Two instructions if the instruction is in a branch delay slot.
912 */
913
David Daney515b0292010-10-21 16:32:26 -0700914static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500915 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500917 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200918 unsigned int cond, cbit;
919 mips_instruction ir;
920 int likely, pc_inc;
921 u32 __user *wva;
922 u64 __user *dva;
923 u32 value;
924 u32 wval;
925 u64 dval;
926 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200929 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500930 if (dec_insn.micro_mips_mode) {
931 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200932 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500933 } else {
934 if (!isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200935 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500936 }
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200939 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /*
941 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +0100942 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * BEFORE we do the cop1 instruction.
944 *
945 * This branch could be a COP1 branch, but in that case we
946 * would have had a trap for that instruction, and would not
947 * come through this route.
948 *
949 * Linux MIPS branch emulator operates on context, updating the
950 * cp0_epc.
951 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500952 ir = dec_insn.next_insn; /* process delay slot instr */
953 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000954 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500955 ir = dec_insn.insn; /* process current instr */
956 pc_inc = dec_insn.pc_inc;
957 }
958
959 /*
960 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
961 * instructions, we want to convert microMIPS FPU instructions
962 * into MIPS32 instructions so that we could reuse all of the
963 * FPU emulation code.
964 *
965 * NOTE: We cannot do this for branch instructions since they
966 * are not a subset. Example: Cannot emulate a 16-bit
967 * aligned target address with a MIPS32 instruction.
968 */
969 if (dec_insn.micro_mips_mode) {
970 /*
971 * If next instruction is a 16-bit instruction, then it
972 * it cannot be a FPU instruction. This could happen
973 * since we can be called for non-FPU instructions.
974 */
975 if ((pc_inc == 2) ||
976 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
977 == SIGILL))
978 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200981emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200982 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -0800983 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 switch (MIPSInst_OPCODE(ir)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200985 case ldc1_op:
986 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
987 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800988 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -0700989
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200990 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800991 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200992 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return SIGBUS;
994 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200995 if (__get_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700996 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200997 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700998 return SIGSEGV;
999 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001000 DITOREG(dval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001003 case sdc1_op:
1004 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1005 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001006 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001007 DIFROMREG(dval, MIPSInst_RT(ir));
1008 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001009 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001010 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return SIGBUS;
1012 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001013 if (__put_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -07001014 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001015 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -07001016 return SIGSEGV;
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001020 case lwc1_op:
1021 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1022 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001023 MIPS_FPU_EMU_INC_STATS(loads);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001024 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001025 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001026 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return SIGBUS;
1028 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001029 if (__get_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -07001030 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001031 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -07001032 return SIGSEGV;
1033 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001034 SITOREG(wval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001037 case swc1_op:
1038 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1039 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001040 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001041 SIFROMREG(wval, MIPSInst_RT(ir));
1042 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001043 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001044 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return SIGBUS;
1046 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001047 if (__put_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -07001048 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001049 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -07001050 return SIGSEGV;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 case cop1_op:
1055 switch (MIPSInst_RS(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 case dmfc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001057 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1058 return SIGILL;
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /* copregister fs -> gpr[rt] */
1061 if (MIPSInst_RT(ir) != 0) {
1062 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1063 MIPSInst_RD(ir));
1064 }
1065 break;
1066
1067 case dmtc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001068 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1069 return SIGILL;
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* copregister fs <- rt */
1072 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1073 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001075 case mfhc_op:
1076 if (!cpu_has_mips_r2)
1077 goto sigill;
1078
1079 /* copregister rd -> gpr[rt] */
1080 if (MIPSInst_RT(ir) != 0) {
1081 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1082 MIPSInst_RD(ir));
1083 }
1084 break;
1085
1086 case mthc_op:
1087 if (!cpu_has_mips_r2)
1088 goto sigill;
1089
1090 /* copregister rd <- gpr[rt] */
1091 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1092 break;
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 case mfc_op:
1095 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (MIPSInst_RT(ir) != 0) {
1097 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1098 MIPSInst_RD(ir));
1099 }
1100 break;
1101
1102 case mtc_op:
1103 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1105 break;
1106
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001107 case cfc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /* cop control register rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (MIPSInst_RD(ir) == FPCREG_CSR) {
1110 value = ctx->fcr31;
Shane McDonald3f135532010-05-07 00:02:09 -06001111 value = (value & ~FPU_CSR_RM) |
1112 mips_rm[modeindex(value)];
Ralf Baechle92df0f82014-04-19 14:03:37 +02001113 pr_debug("%p gpr[%d]<-csr=%08x\n",
1114 (void *) (xcp->cp0_epc),
1115 MIPSInst_RT(ir), value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117 else if (MIPSInst_RD(ir) == FPCREG_RID)
1118 value = 0;
1119 else
1120 value = 0;
1121 if (MIPSInst_RT(ir))
1122 xcp->regs[MIPSInst_RT(ir)] = value;
1123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001125 case ctc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (MIPSInst_RT(ir) == 0)
1128 value = 0;
1129 else
1130 value = xcp->regs[MIPSInst_RT(ir)];
1131
1132 /* we only have one writable control reg
1133 */
1134 if (MIPSInst_RD(ir) == FPCREG_CSR) {
Ralf Baechle92df0f82014-04-19 14:03:37 +02001135 pr_debug("%p gpr[%d]->csr=%08x\n",
1136 (void *) (xcp->cp0_epc),
1137 MIPSInst_RT(ir), value);
Shane McDonald95e8f632010-05-06 23:26:57 -06001138
1139 /*
1140 * Don't write reserved bits,
1141 * and convert to ieee library modes
1142 */
1143 ctx->fcr31 = (value &
1144 ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
1145 ieee_rm[modeindex(value)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1148 return SIGFPE;
1149 }
1150 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001152 case bc_op:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001153 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return SIGILL;
1155
Ralf Baechle08a07902014-04-19 13:11:37 +02001156 if (cpu_has_mips_4_5_r)
1157 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1158 else
1159 cbit = FPU_CSR_COND;
1160 cond = ctx->fcr31 & cbit;
1161
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001162 likely = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 switch (MIPSInst_RT(ir) & 3) {
1164 case bcfl_op:
1165 likely = 1;
1166 case bcf_op:
1167 cond = !cond;
1168 break;
1169 case bctl_op:
1170 likely = 1;
1171 case bct_op:
1172 break;
1173 default:
1174 /* thats an illegal instruction */
1175 return SIGILL;
1176 }
1177
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001178 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (cond) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001180 /*
1181 * Branch taken: emulate dslot instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001183 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001185 contpc = MIPSInst_SIMM(ir);
1186 ir = dec_insn.next_insn;
1187 if (dec_insn.micro_mips_mode) {
1188 contpc = (xcp->cp0_epc + (contpc << 1));
1189
1190 /* If 16-bit instruction, not FPU. */
1191 if ((dec_insn.next_pc_inc == 2) ||
1192 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1193
1194 /*
1195 * Since this instruction will
1196 * be put on the stack with
1197 * 32-bit words, get around
1198 * this problem by putting a
1199 * NOP16 as the second one.
1200 */
1201 if (dec_insn.next_pc_inc == 2)
1202 ir = (ir & (~0xffff)) | MM_NOP16;
1203
1204 /*
1205 * Single step the non-CP1
1206 * instruction in the dslot.
1207 */
1208 return mips_dsemul(xcp, ir, contpc);
1209 }
1210 } else
1211 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 switch (MIPSInst_OPCODE(ir)) {
1214 case lwc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001215 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 case swc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001218 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 case ldc1_op:
1221 case sdc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001222 if (cpu_has_mips_2_3_4_5 ||
1223 cpu_has_mips64)
1224 goto emul;
1225
1226 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001228
Ralf Baechle08a07902014-04-19 13:11:37 +02001229 case cop1_op:
1230 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001231
Ralf Baechle08a07902014-04-19 13:11:37 +02001232 case cop1x_op:
1233 if (cpu_has_mips_4_5 || cpu_has_mips64)
1234 /* its one of ours */
1235 goto emul;
1236
1237 return SIGILL;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001240 if (!cpu_has_mips_4_5_r)
1241 return SIGILL;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (MIPSInst_FUNC(ir) == movc_op)
1244 goto emul;
1245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
1248 /*
1249 * Single step the non-cp1
1250 * instruction in the dslot
1251 */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001252 return mips_dsemul(xcp, ir, contpc);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001253 } else if (likely) { /* branch not taken */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * branch likely nullifies
1256 * dslot if not taken
1257 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001258 xcp->cp0_epc += dec_insn.pc_inc;
1259 contpc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /*
1261 * else continue & execute
1262 * dslot as normal insn
1263 */
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 default:
1268 if (!(MIPSInst_RS(ir) & 0x10))
1269 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001271 /* a real fpu computation instruction */
1272 if ((sig = fpu_emu(xcp, ctx, ir)))
1273 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 break;
1276
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001277 case cop1x_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001278 if (!cpu_has_mips_4_5 && !cpu_has_mips64)
1279 return SIGILL;
1280
1281 sig = fpux_emu(xcp, ctx, ir, fault_addr);
David Daney515b0292010-10-21 16:32:26 -07001282 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return sig;
1284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001287 if (!cpu_has_mips_4_5_r)
1288 return SIGILL;
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (MIPSInst_FUNC(ir) != movc_op)
1291 return SIGILL;
1292 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1293 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1294 xcp->regs[MIPSInst_RD(ir)] =
1295 xcp->regs[MIPSInst_RS(ir)];
1296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 default:
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001298sigill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return SIGILL;
1300 }
1301
1302 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001303 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001304 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return 0;
1307}
1308
1309/*
1310 * Conversion table from MIPS compare ops 48-63
1311 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1312 */
1313static const unsigned char cmptab[8] = {
1314 0, /* cmp_0 (sig) cmp_sf */
1315 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1316 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1317 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1318 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1319 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1320 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1321 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1322};
1323
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325/*
1326 * Additional MIPS4 instructions
1327 */
1328
Ralf Baechle47fa0c02014-04-16 11:00:12 +02001329#define DEF3OP(name, p, f1, f2, f3) \
1330static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1331 union ieee754##p s, union ieee754##p t) \
1332{ \
1333 struct _ieee754_csr ieee754_csr_save; \
1334 s = f1(s, t); \
1335 ieee754_csr_save = ieee754_csr; \
1336 s = f2(s, r); \
1337 ieee754_csr_save.cx |= ieee754_csr.cx; \
1338 ieee754_csr_save.sx |= ieee754_csr.sx; \
1339 s = f3(s); \
1340 ieee754_csr.cx |= ieee754_csr_save.cx; \
1341 ieee754_csr.sx |= ieee754_csr_save.sx; \
1342 return s; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001345static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 return ieee754dp_div(ieee754dp_one(0), d);
1348}
1349
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001350static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1353}
1354
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001355static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 return ieee754sp_div(ieee754sp_one(0), s);
1358}
1359
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001360static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1363}
1364
Ralf Baechle21a151d2007-10-11 23:46:15 +01001365DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1366DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1368DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001369DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1370DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1372DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1373
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001374static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001375 mips_instruction ir, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 unsigned rcsr = 0; /* resulting csr */
1378
David Daneyb6ee75e2009-11-05 11:34:26 -08001379 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 switch (MIPSInst_FMA_FFMT(ir)) {
1382 case s_fmt:{ /* 0 */
1383
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001384 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1385 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001386 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 u32 val;
1388
1389 switch (MIPSInst_FUNC(ir)) {
1390 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001391 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 xcp->regs[MIPSInst_FT(ir)]);
1393
David Daneyb6ee75e2009-11-05 11:34:26 -08001394 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001395 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001396 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001397 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return SIGBUS;
1399 }
David Daney515b0292010-10-21 16:32:26 -07001400 if (__get_user(val, va)) {
1401 MIPS_FPU_EMU_INC_STATS(errors);
1402 *fault_addr = va;
1403 return SIGSEGV;
1404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 SITOREG(val, MIPSInst_FD(ir));
1406 break;
1407
1408 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001409 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 xcp->regs[MIPSInst_FT(ir)]);
1411
David Daneyb6ee75e2009-11-05 11:34:26 -08001412 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001415 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1416 MIPS_FPU_EMU_INC_STATS(errors);
1417 *fault_addr = va;
1418 return SIGBUS;
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001421 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001422 *fault_addr = va;
1423 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425 break;
1426
1427 case madd_s_op:
1428 handler = fpemu_sp_madd;
1429 goto scoptop;
1430 case msub_s_op:
1431 handler = fpemu_sp_msub;
1432 goto scoptop;
1433 case nmadd_s_op:
1434 handler = fpemu_sp_nmadd;
1435 goto scoptop;
1436 case nmsub_s_op:
1437 handler = fpemu_sp_nmsub;
1438 goto scoptop;
1439
1440 scoptop:
1441 SPFROMREG(fr, MIPSInst_FR(ir));
1442 SPFROMREG(fs, MIPSInst_FS(ir));
1443 SPFROMREG(ft, MIPSInst_FT(ir));
1444 fd = (*handler) (fr, fs, ft);
1445 SPTOREG(fd, MIPSInst_FD(ir));
1446
1447 copcsr:
1448 if (ieee754_cxtest(IEEE754_INEXACT))
1449 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1450 if (ieee754_cxtest(IEEE754_UNDERFLOW))
1451 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1452 if (ieee754_cxtest(IEEE754_OVERFLOW))
1453 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1454 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
1455 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1456
1457 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001459 /*printk ("SIGFPE: FPU csr = %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 ctx->fcr31); */
1461 return SIGFPE;
1462 }
1463
1464 break;
1465
1466 default:
1467 return SIGILL;
1468 }
1469 break;
1470 }
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001473 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1474 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001475 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 u64 val;
1477
1478 switch (MIPSInst_FUNC(ir)) {
1479 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001480 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 xcp->regs[MIPSInst_FT(ir)]);
1482
David Daneyb6ee75e2009-11-05 11:34:26 -08001483 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001484 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001485 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001486 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return SIGBUS;
1488 }
David Daney515b0292010-10-21 16:32:26 -07001489 if (__get_user(val, va)) {
1490 MIPS_FPU_EMU_INC_STATS(errors);
1491 *fault_addr = va;
1492 return SIGSEGV;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 DITOREG(val, MIPSInst_FD(ir));
1495 break;
1496
1497 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001498 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 xcp->regs[MIPSInst_FT(ir)]);
1500
David Daneyb6ee75e2009-11-05 11:34:26 -08001501 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001503 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001504 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001505 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return SIGBUS;
1507 }
David Daney515b0292010-10-21 16:32:26 -07001508 if (__put_user(val, va)) {
1509 MIPS_FPU_EMU_INC_STATS(errors);
1510 *fault_addr = va;
1511 return SIGSEGV;
1512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 break;
1514
1515 case madd_d_op:
1516 handler = fpemu_dp_madd;
1517 goto dcoptop;
1518 case msub_d_op:
1519 handler = fpemu_dp_msub;
1520 goto dcoptop;
1521 case nmadd_d_op:
1522 handler = fpemu_dp_nmadd;
1523 goto dcoptop;
1524 case nmsub_d_op:
1525 handler = fpemu_dp_nmsub;
1526 goto dcoptop;
1527
1528 dcoptop:
1529 DPFROMREG(fr, MIPSInst_FR(ir));
1530 DPFROMREG(fs, MIPSInst_FS(ir));
1531 DPFROMREG(ft, MIPSInst_FT(ir));
1532 fd = (*handler) (fr, fs, ft);
1533 DPTOREG(fd, MIPSInst_FD(ir));
1534 goto copcsr;
1535
1536 default:
1537 return SIGILL;
1538 }
1539 break;
1540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001542 case 0x3:
1543 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /* ignore prefx operation */
1547 break;
1548
1549 default:
1550 return SIGILL;
1551 }
1552
1553 return 0;
1554}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556
1557
1558/*
1559 * Emulate a single COP1 arithmetic instruction.
1560 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001561static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 mips_instruction ir)
1563{
1564 int rfmt; /* resulting format */
1565 unsigned rcsr = 0; /* resulting csr */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001566 unsigned int oldrm;
1567 unsigned int cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 unsigned cond;
1569 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001570 union ieee754dp d;
1571 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 s64 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 } rv; /* resulting value */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001575 u64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
David Daneyb6ee75e2009-11-05 11:34:26 -08001577 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001579 case s_fmt: { /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001581 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1582 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 } handler;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001584 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 switch (MIPSInst_FUNC(ir)) {
1587 /* binary ops */
1588 case fadd_op:
1589 handler.b = ieee754sp_add;
1590 goto scopbop;
1591 case fsub_op:
1592 handler.b = ieee754sp_sub;
1593 goto scopbop;
1594 case fmul_op:
1595 handler.b = ieee754sp_mul;
1596 goto scopbop;
1597 case fdiv_op:
1598 handler.b = ieee754sp_div;
1599 goto scopbop;
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_4_5_r)
1604 return SIGILL;
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 handler.u = ieee754sp_sqrt;
1607 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001608
Ralf Baechle08a07902014-04-19 13:11:37 +02001609 /*
1610 * Note that on some MIPS IV implementations such as the
1611 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1612 * achieve full IEEE-754 accuracy - however this emulator does.
1613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001615 if (!cpu_has_mips_4_5_r2)
1616 return SIGILL;
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 handler.u = fpemu_sp_rsqrt;
1619 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001622 if (!cpu_has_mips_4_5_r2)
1623 return SIGILL;
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 handler.u = fpemu_sp_recip;
1626 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001629 if (!cpu_has_mips_4_5_r)
1630 return SIGILL;
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1633 if (((ctx->fcr31 & cond) != 0) !=
1634 ((MIPSInst_FT(ir) & 1) != 0))
1635 return 0;
1636 SPFROMREG(rv.s, MIPSInst_FS(ir));
1637 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001640 if (!cpu_has_mips_4_5_r)
1641 return SIGILL;
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1644 return 0;
1645 SPFROMREG(rv.s, MIPSInst_FS(ir));
1646 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001649 if (!cpu_has_mips_4_5_r)
1650 return SIGILL;
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1653 return 0;
1654 SPFROMREG(rv.s, MIPSInst_FS(ir));
1655 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 case fabs_op:
1658 handler.u = ieee754sp_abs;
1659 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 case fneg_op:
1662 handler.u = ieee754sp_neg;
1663 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 case fmov_op:
1666 /* an easy one */
1667 SPFROMREG(rv.s, MIPSInst_FS(ir));
1668 goto copcsr;
1669
1670 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001671scopbop:
1672 SPFROMREG(fs, MIPSInst_FS(ir));
1673 SPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001675 rv.s = (*handler.b) (fs, ft);
1676 goto copcsr;
1677scopuop:
1678 SPFROMREG(fs, MIPSInst_FS(ir));
1679 rv.s = (*handler.u) (fs);
1680 goto copcsr;
1681copcsr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (ieee754_cxtest(IEEE754_INEXACT))
1683 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1684 if (ieee754_cxtest(IEEE754_UNDERFLOW))
1685 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1686 if (ieee754_cxtest(IEEE754_OVERFLOW))
1687 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1688 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
1689 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1690 if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
1691 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1692 break;
1693
1694 /* unary conv ops */
1695 case fcvts_op:
1696 return SIGILL; /* not defined */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001698 case fcvtd_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 SPFROMREG(fs, MIPSInst_FS(ir));
1700 rv.d = ieee754dp_fsp(fs);
1701 rfmt = d_fmt;
1702 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001704 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 SPFROMREG(fs, MIPSInst_FS(ir));
1706 rv.w = ieee754sp_tint(fs);
1707 rfmt = w_fmt;
1708 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 case fround_op:
1711 case ftrunc_op:
1712 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001713 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001714 if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
1715 return SIGILL;
1716
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001717 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 SPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001719 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 rv.w = ieee754sp_tint(fs);
1721 ieee754_csr.rm = oldrm;
1722 rfmt = w_fmt;
1723 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001725 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001726 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1727 return SIGILL;
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 SPFROMREG(fs, MIPSInst_FS(ir));
1730 rv.l = ieee754sp_tlong(fs);
1731 rfmt = l_fmt;
1732 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 case froundl_op:
1735 case ftruncl_op:
1736 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001737 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001738 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1739 return SIGILL;
1740
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001741 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 SPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001743 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 rv.l = ieee754sp_tlong(fs);
1745 ieee754_csr.rm = oldrm;
1746 rfmt = l_fmt;
1747 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 default:
1750 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1751 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001752 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 SPFROMREG(fs, MIPSInst_FS(ir));
1755 SPFROMREG(ft, MIPSInst_FT(ir));
1756 rv.w = ieee754sp_cmp(fs, ft,
1757 cmptab[cmpop & 0x7], cmpop & 0x8);
1758 rfmt = -1;
1759 if ((cmpop & 0x8) && ieee754_cxtest
1760 (IEEE754_INVALID_OPERATION))
1761 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1762 else
1763 goto copcsr;
1764
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001765 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 break;
1768 }
1769 break;
1770 }
1771
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001772 case d_fmt: {
1773 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001775 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1776 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 } handler;
1778
1779 switch (MIPSInst_FUNC(ir)) {
1780 /* binary ops */
1781 case fadd_op:
1782 handler.b = ieee754dp_add;
1783 goto dcopbop;
1784 case fsub_op:
1785 handler.b = ieee754dp_sub;
1786 goto dcopbop;
1787 case fmul_op:
1788 handler.b = ieee754dp_mul;
1789 goto dcopbop;
1790 case fdiv_op:
1791 handler.b = ieee754dp_div;
1792 goto dcopbop;
1793
1794 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001796 if (!cpu_has_mips_2_3_4_5_r)
1797 return SIGILL;
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 handler.u = ieee754dp_sqrt;
1800 goto dcopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001801 /*
1802 * Note that on some MIPS IV implementations such as the
1803 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1804 * achieve full IEEE-754 accuracy - however this emulator does.
1805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 case frsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001807 if (!cpu_has_mips_4_5_r2)
1808 return SIGILL;
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 handler.u = fpemu_dp_rsqrt;
1811 goto dcopuop;
1812 case frecip_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001813 if (!cpu_has_mips_4_5_r2)
1814 return SIGILL;
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 handler.u = fpemu_dp_recip;
1817 goto dcopuop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001819 if (!cpu_has_mips_4_5_r)
1820 return SIGILL;
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1823 if (((ctx->fcr31 & cond) != 0) !=
1824 ((MIPSInst_FT(ir) & 1) != 0))
1825 return 0;
1826 DPFROMREG(rv.d, MIPSInst_FS(ir));
1827 break;
1828 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001829 if (!cpu_has_mips_4_5_r)
1830 return SIGILL;
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1833 return 0;
1834 DPFROMREG(rv.d, MIPSInst_FS(ir));
1835 break;
1836 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001837 if (!cpu_has_mips_4_5_r)
1838 return SIGILL;
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1841 return 0;
1842 DPFROMREG(rv.d, MIPSInst_FS(ir));
1843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 case fabs_op:
1845 handler.u = ieee754dp_abs;
1846 goto dcopuop;
1847
1848 case fneg_op:
1849 handler.u = ieee754dp_neg;
1850 goto dcopuop;
1851
1852 case fmov_op:
1853 /* an easy one */
1854 DPFROMREG(rv.d, MIPSInst_FS(ir));
1855 goto copcsr;
1856
1857 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001858dcopbop:
1859 DPFROMREG(fs, MIPSInst_FS(ir));
1860 DPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001862 rv.d = (*handler.b) (fs, ft);
1863 goto copcsr;
1864dcopuop:
1865 DPFROMREG(fs, MIPSInst_FS(ir));
1866 rv.d = (*handler.u) (fs);
1867 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001869 /*
1870 * unary conv ops
1871 */
1872 case fcvts_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 DPFROMREG(fs, MIPSInst_FS(ir));
1874 rv.s = ieee754sp_fdp(fs);
1875 rfmt = s_fmt;
1876 goto copcsr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 case fcvtd_op:
1879 return SIGILL; /* not defined */
1880
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001881 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 DPFROMREG(fs, MIPSInst_FS(ir));
1883 rv.w = ieee754dp_tint(fs); /* wrong */
1884 rfmt = w_fmt;
1885 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 case fround_op:
1888 case ftrunc_op:
1889 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001890 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001891 if (!cpu_has_mips_2_3_4_5_r)
1892 return SIGILL;
1893
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001894 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 DPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001896 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 rv.w = ieee754dp_tint(fs);
1898 ieee754_csr.rm = oldrm;
1899 rfmt = w_fmt;
1900 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001902 case fcvtl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001903 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1904 return SIGILL;
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 DPFROMREG(fs, MIPSInst_FS(ir));
1907 rv.l = ieee754dp_tlong(fs);
1908 rfmt = l_fmt;
1909 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 case froundl_op:
1912 case ftruncl_op:
1913 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001914 case ffloorl_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001915 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1916 return SIGILL;
1917
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001918 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 DPFROMREG(fs, MIPSInst_FS(ir));
Shane McDonald3f135532010-05-07 00:02:09 -06001920 ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 rv.l = ieee754dp_tlong(fs);
1922 ieee754_csr.rm = oldrm;
1923 rfmt = l_fmt;
1924 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 default:
1927 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1928 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001929 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 DPFROMREG(fs, MIPSInst_FS(ir));
1932 DPFROMREG(ft, MIPSInst_FT(ir));
1933 rv.w = ieee754dp_cmp(fs, ft,
1934 cmptab[cmpop & 0x7], cmpop & 0x8);
1935 rfmt = -1;
1936 if ((cmpop & 0x8)
1937 &&
1938 ieee754_cxtest
1939 (IEEE754_INVALID_OPERATION))
1940 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1941 else
1942 goto copcsr;
1943
1944 }
1945 else {
1946 return SIGILL;
1947 }
1948 break;
1949 }
1950 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001952 case w_fmt:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 switch (MIPSInst_FUNC(ir)) {
1954 case fcvts_op:
1955 /* convert word to single precision real */
1956 SPFROMREG(fs, MIPSInst_FS(ir));
1957 rv.s = ieee754sp_fint(fs.bits);
1958 rfmt = s_fmt;
1959 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 case fcvtd_op:
1961 /* convert word to double precision real */
1962 SPFROMREG(fs, MIPSInst_FS(ir));
1963 rv.d = ieee754dp_fint(fs.bits);
1964 rfmt = d_fmt;
1965 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 default:
1967 return SIGILL;
1968 }
1969 break;
1970 }
1971
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001972 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001973
1974 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1975 return SIGILL;
1976
Paul Burtonbbd426f2014-02-13 11:26:41 +00001977 DIFROMREG(bits, MIPSInst_FS(ir));
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 switch (MIPSInst_FUNC(ir)) {
1980 case fcvts_op:
1981 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001982 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 rfmt = s_fmt;
1984 goto copcsr;
1985 case fcvtd_op:
1986 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001987 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 rfmt = d_fmt;
1989 goto copcsr;
1990 default:
1991 return SIGILL;
1992 }
1993 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 default:
1996 return SIGILL;
1997 }
1998
1999 /*
2000 * Update the fpu CSR register for this operation.
2001 * If an exception is required, generate a tidy SIGFPE exception,
2002 * without updating the result register.
2003 * Note: cause exception bits do not accumulate, they are rewritten
2004 * for each op; only the flag/sticky bits accumulate.
2005 */
2006 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
2007 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002008 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return SIGFPE;
2010 }
2011
2012 /*
2013 * Now we can safely write the result back to the register file.
2014 */
2015 switch (rfmt) {
Ralf Baechle08a07902014-04-19 13:11:37 +02002016 case -1:
2017
2018 if (cpu_has_mips_4_5_r)
2019 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 else
Ralf Baechle08a07902014-04-19 13:11:37 +02002021 cbit = FPU_CSR_COND;
2022 if (rv.w)
2023 ctx->fcr31 |= cbit;
2024 else
2025 ctx->fcr31 &= ~cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 break;
Ralf Baechle08a07902014-04-19 13:11:37 +02002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 case d_fmt:
2029 DPTOREG(rv.d, MIPSInst_FD(ir));
2030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 case s_fmt:
2032 SPTOREG(rv.s, MIPSInst_FD(ir));
2033 break;
2034 case w_fmt:
2035 SITOREG(rv.w, MIPSInst_FD(ir));
2036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02002038 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
2039 return SIGILL;
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 DITOREG(rv.l, MIPSInst_FD(ir));
2042 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 default:
2044 return SIGILL;
2045 }
2046
2047 return 0;
2048}
2049
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002050int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07002051 int has_fpu, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Ralf Baechle333d1f62005-02-28 17:55:57 +00002053 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002054 struct mm_decoded_insn dec_insn;
2055 u16 instr[4];
2056 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int sig = 0;
2058
2059 oldepc = xcp->cp0_epc;
2060 do {
2061 prevepc = xcp->cp0_epc;
2062
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002063 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2064 /*
2065 * Get next 2 microMIPS instructions and convert them
2066 * into 32-bit instructions.
2067 */
2068 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2069 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2070 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2071 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2072 MIPS_FPU_EMU_INC_STATS(errors);
2073 return SIGBUS;
2074 }
2075 instr_ptr = instr;
2076
2077 /* Get first instruction. */
2078 if (mm_insn_16bit(*instr_ptr)) {
2079 /* Duplicate the half-word. */
2080 dec_insn.insn = (*instr_ptr << 16) |
2081 (*instr_ptr);
2082 /* 16-bit instruction. */
2083 dec_insn.pc_inc = 2;
2084 instr_ptr += 1;
2085 } else {
2086 dec_insn.insn = (*instr_ptr << 16) |
2087 *(instr_ptr+1);
2088 /* 32-bit instruction. */
2089 dec_insn.pc_inc = 4;
2090 instr_ptr += 2;
2091 }
2092 /* Get second instruction. */
2093 if (mm_insn_16bit(*instr_ptr)) {
2094 /* Duplicate the half-word. */
2095 dec_insn.next_insn = (*instr_ptr << 16) |
2096 (*instr_ptr);
2097 /* 16-bit instruction. */
2098 dec_insn.next_pc_inc = 2;
2099 } else {
2100 dec_insn.next_insn = (*instr_ptr << 16) |
2101 *(instr_ptr+1);
2102 /* 32-bit instruction. */
2103 dec_insn.next_pc_inc = 4;
2104 }
2105 dec_insn.micro_mips_mode = 1;
2106 } else {
2107 if ((get_user(dec_insn.insn,
2108 (mips_instruction __user *) xcp->cp0_epc)) ||
2109 (get_user(dec_insn.next_insn,
2110 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2111 MIPS_FPU_EMU_INC_STATS(errors);
2112 return SIGBUS;
2113 }
2114 dec_insn.pc_inc = 4;
2115 dec_insn.next_pc_inc = 4;
2116 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002118
2119 if ((dec_insn.insn == 0) ||
2120 ((dec_insn.pc_inc == 2) &&
2121 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2122 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002124 /*
2125 * The 'ieee754_csr' is an alias of
Ralf Baechle70342282013-01-22 12:59:30 +01002126 * ctx->fcr31. No need to copy ctx->fcr31 to
2127 * ieee754_csr. But ieee754_csr.rm is ieee
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002128 * library modes. (not mips rounding mode)
2129 */
2130 /* convert to ieee library modes */
2131 ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002132 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002133 /* revert to mips rounding mode */
2134 ieee754_csr.rm = mips_rm[ieee754_csr.rm];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
2136
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002137 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 break;
2139 if (sig)
2140 break;
2141
2142 cond_resched();
2143 } while (xcp->cp0_epc > prevepc);
2144
2145 /* SIGILL indicates a non-fpu instruction */
2146 if (sig == SIGILL && xcp->cp0_epc != oldepc)
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002147 /* but if EPC has advanced, then ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 sig = 0;
2149
2150 return sig;
2151}