blob: 7aa42b2caf89d00e95ad62053047bcee1ca7e697 [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
Maciej W. Rozyckif6843622015-04-03 23:27:26 +010048#include <asm/cpu-info.h>
Ralf Baechlecd8ee342014-04-16 02:09:53 +020049#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/fpu_emulator.h>
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050051#include <asm/fpu.h>
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +000052#include <asm/mips-r2-to-r6-emul.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include "ieee754.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/* Function which emulates a floating point instruction. */
57
Atsushi Nemotoeae89072006-05-16 01:26:03 +090058static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 mips_instruction);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int fpux_emu(struct pt_regs *,
David Daney515b0292010-10-21 16:32:26 -070062 struct mips_fpu_struct *, mips_instruction, void *__user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* Control registers */
65
66#define FPCREG_RID 0 /* $0 = revision id */
67#define FPCREG_CSR 31 /* $31 = csr */
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* convert condition code register number to csr bit */
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +000070const unsigned int fpucondbit[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 FPU_CSR_COND0,
72 FPU_CSR_COND1,
73 FPU_CSR_COND2,
74 FPU_CSR_COND3,
75 FPU_CSR_COND4,
76 FPU_CSR_COND5,
77 FPU_CSR_COND6,
78 FPU_CSR_COND7
79};
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050081/* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
82static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
83static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
84static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
85static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
86
87/*
88 * This functions translates a 32-bit microMIPS instruction
89 * into a 32-bit MIPS32 instruction. Returns 0 on success
90 * and SIGILL otherwise.
91 */
92static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
93{
94 union mips_instruction insn = *insn_ptr;
95 union mips_instruction mips32_insn = insn;
96 int func, fmt, op;
97
98 switch (insn.mm_i_format.opcode) {
99 case mm_ldc132_op:
100 mips32_insn.mm_i_format.opcode = ldc1_op;
101 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
102 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
103 break;
104 case mm_lwc132_op:
105 mips32_insn.mm_i_format.opcode = lwc1_op;
106 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
107 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
108 break;
109 case mm_sdc132_op:
110 mips32_insn.mm_i_format.opcode = sdc1_op;
111 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
112 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
113 break;
114 case mm_swc132_op:
115 mips32_insn.mm_i_format.opcode = swc1_op;
116 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
117 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
118 break;
119 case mm_pool32i_op:
120 /* NOTE: offset is << by 1 if in microMIPS mode. */
121 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
122 (insn.mm_i_format.rt == mm_bc1t_op)) {
123 mips32_insn.fb_format.opcode = cop1_op;
124 mips32_insn.fb_format.bc = bc_op;
125 mips32_insn.fb_format.flag =
126 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
127 } else
128 return SIGILL;
129 break;
130 case mm_pool32f_op:
131 switch (insn.mm_fp0_format.func) {
132 case mm_32f_01_op:
133 case mm_32f_11_op:
134 case mm_32f_02_op:
135 case mm_32f_12_op:
136 case mm_32f_41_op:
137 case mm_32f_51_op:
138 case mm_32f_42_op:
139 case mm_32f_52_op:
140 op = insn.mm_fp0_format.func;
141 if (op == mm_32f_01_op)
142 func = madd_s_op;
143 else if (op == mm_32f_11_op)
144 func = madd_d_op;
145 else if (op == mm_32f_02_op)
146 func = nmadd_s_op;
147 else if (op == mm_32f_12_op)
148 func = nmadd_d_op;
149 else if (op == mm_32f_41_op)
150 func = msub_s_op;
151 else if (op == mm_32f_51_op)
152 func = msub_d_op;
153 else if (op == mm_32f_42_op)
154 func = nmsub_s_op;
155 else
156 func = nmsub_d_op;
157 mips32_insn.fp6_format.opcode = cop1x_op;
158 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
159 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
160 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
161 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
162 mips32_insn.fp6_format.func = func;
163 break;
164 case mm_32f_10_op:
165 func = -1; /* Invalid */
166 op = insn.mm_fp5_format.op & 0x7;
167 if (op == mm_ldxc1_op)
168 func = ldxc1_op;
169 else if (op == mm_sdxc1_op)
170 func = sdxc1_op;
171 else if (op == mm_lwxc1_op)
172 func = lwxc1_op;
173 else if (op == mm_swxc1_op)
174 func = swxc1_op;
175
176 if (func != -1) {
177 mips32_insn.r_format.opcode = cop1x_op;
178 mips32_insn.r_format.rs =
179 insn.mm_fp5_format.base;
180 mips32_insn.r_format.rt =
181 insn.mm_fp5_format.index;
182 mips32_insn.r_format.rd = 0;
183 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
184 mips32_insn.r_format.func = func;
185 } else
186 return SIGILL;
187 break;
188 case mm_32f_40_op:
189 op = -1; /* Invalid */
190 if (insn.mm_fp2_format.op == mm_fmovt_op)
191 op = 1;
192 else if (insn.mm_fp2_format.op == mm_fmovf_op)
193 op = 0;
194 if (op != -1) {
195 mips32_insn.fp0_format.opcode = cop1_op;
196 mips32_insn.fp0_format.fmt =
197 sdps_format[insn.mm_fp2_format.fmt];
198 mips32_insn.fp0_format.ft =
199 (insn.mm_fp2_format.cc<<2) + op;
200 mips32_insn.fp0_format.fs =
201 insn.mm_fp2_format.fs;
202 mips32_insn.fp0_format.fd =
203 insn.mm_fp2_format.fd;
204 mips32_insn.fp0_format.func = fmovc_op;
205 } else
206 return SIGILL;
207 break;
208 case mm_32f_60_op:
209 func = -1; /* Invalid */
210 if (insn.mm_fp0_format.op == mm_fadd_op)
211 func = fadd_op;
212 else if (insn.mm_fp0_format.op == mm_fsub_op)
213 func = fsub_op;
214 else if (insn.mm_fp0_format.op == mm_fmul_op)
215 func = fmul_op;
216 else if (insn.mm_fp0_format.op == mm_fdiv_op)
217 func = fdiv_op;
218 if (func != -1) {
219 mips32_insn.fp0_format.opcode = cop1_op;
220 mips32_insn.fp0_format.fmt =
221 sdps_format[insn.mm_fp0_format.fmt];
222 mips32_insn.fp0_format.ft =
223 insn.mm_fp0_format.ft;
224 mips32_insn.fp0_format.fs =
225 insn.mm_fp0_format.fs;
226 mips32_insn.fp0_format.fd =
227 insn.mm_fp0_format.fd;
228 mips32_insn.fp0_format.func = func;
229 } else
230 return SIGILL;
231 break;
232 case mm_32f_70_op:
233 func = -1; /* Invalid */
234 if (insn.mm_fp0_format.op == mm_fmovn_op)
235 func = fmovn_op;
236 else if (insn.mm_fp0_format.op == mm_fmovz_op)
237 func = fmovz_op;
238 if (func != -1) {
239 mips32_insn.fp0_format.opcode = cop1_op;
240 mips32_insn.fp0_format.fmt =
241 sdps_format[insn.mm_fp0_format.fmt];
242 mips32_insn.fp0_format.ft =
243 insn.mm_fp0_format.ft;
244 mips32_insn.fp0_format.fs =
245 insn.mm_fp0_format.fs;
246 mips32_insn.fp0_format.fd =
247 insn.mm_fp0_format.fd;
248 mips32_insn.fp0_format.func = func;
249 } else
250 return SIGILL;
251 break;
252 case mm_32f_73_op: /* POOL32FXF */
253 switch (insn.mm_fp1_format.op) {
254 case mm_movf0_op:
255 case mm_movf1_op:
256 case mm_movt0_op:
257 case mm_movt1_op:
258 if ((insn.mm_fp1_format.op & 0x7f) ==
259 mm_movf0_op)
260 op = 0;
261 else
262 op = 1;
263 mips32_insn.r_format.opcode = spec_op;
264 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
265 mips32_insn.r_format.rt =
266 (insn.mm_fp4_format.cc << 2) + op;
267 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
268 mips32_insn.r_format.re = 0;
269 mips32_insn.r_format.func = movc_op;
270 break;
271 case mm_fcvtd0_op:
272 case mm_fcvtd1_op:
273 case mm_fcvts0_op:
274 case mm_fcvts1_op:
275 if ((insn.mm_fp1_format.op & 0x7f) ==
276 mm_fcvtd0_op) {
277 func = fcvtd_op;
278 fmt = swl_format[insn.mm_fp3_format.fmt];
279 } else {
280 func = fcvts_op;
281 fmt = dwl_format[insn.mm_fp3_format.fmt];
282 }
283 mips32_insn.fp0_format.opcode = cop1_op;
284 mips32_insn.fp0_format.fmt = fmt;
285 mips32_insn.fp0_format.ft = 0;
286 mips32_insn.fp0_format.fs =
287 insn.mm_fp3_format.fs;
288 mips32_insn.fp0_format.fd =
289 insn.mm_fp3_format.rt;
290 mips32_insn.fp0_format.func = func;
291 break;
292 case mm_fmov0_op:
293 case mm_fmov1_op:
294 case mm_fabs0_op:
295 case mm_fabs1_op:
296 case mm_fneg0_op:
297 case mm_fneg1_op:
298 if ((insn.mm_fp1_format.op & 0x7f) ==
299 mm_fmov0_op)
300 func = fmov_op;
301 else if ((insn.mm_fp1_format.op & 0x7f) ==
302 mm_fabs0_op)
303 func = fabs_op;
304 else
305 func = fneg_op;
306 mips32_insn.fp0_format.opcode = cop1_op;
307 mips32_insn.fp0_format.fmt =
308 sdps_format[insn.mm_fp3_format.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_ffloorl_op:
317 case mm_ffloorw_op:
318 case mm_fceill_op:
319 case mm_fceilw_op:
320 case mm_ftruncl_op:
321 case mm_ftruncw_op:
322 case mm_froundl_op:
323 case mm_froundw_op:
324 case mm_fcvtl_op:
325 case mm_fcvtw_op:
326 if (insn.mm_fp1_format.op == mm_ffloorl_op)
327 func = ffloorl_op;
328 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
329 func = ffloor_op;
330 else if (insn.mm_fp1_format.op == mm_fceill_op)
331 func = fceill_op;
332 else if (insn.mm_fp1_format.op == mm_fceilw_op)
333 func = fceil_op;
334 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
335 func = ftruncl_op;
336 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
337 func = ftrunc_op;
338 else if (insn.mm_fp1_format.op == mm_froundl_op)
339 func = froundl_op;
340 else if (insn.mm_fp1_format.op == mm_froundw_op)
341 func = fround_op;
342 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
343 func = fcvtl_op;
344 else
345 func = fcvtw_op;
346 mips32_insn.fp0_format.opcode = cop1_op;
347 mips32_insn.fp0_format.fmt =
348 sd_format[insn.mm_fp1_format.fmt];
349 mips32_insn.fp0_format.ft = 0;
350 mips32_insn.fp0_format.fs =
351 insn.mm_fp1_format.fs;
352 mips32_insn.fp0_format.fd =
353 insn.mm_fp1_format.rt;
354 mips32_insn.fp0_format.func = func;
355 break;
356 case mm_frsqrt_op:
357 case mm_fsqrt_op:
358 case mm_frecip_op:
359 if (insn.mm_fp1_format.op == mm_frsqrt_op)
360 func = frsqrt_op;
361 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
362 func = fsqrt_op;
363 else
364 func = frecip_op;
365 mips32_insn.fp0_format.opcode = cop1_op;
366 mips32_insn.fp0_format.fmt =
367 sdps_format[insn.mm_fp1_format.fmt];
368 mips32_insn.fp0_format.ft = 0;
369 mips32_insn.fp0_format.fs =
370 insn.mm_fp1_format.fs;
371 mips32_insn.fp0_format.fd =
372 insn.mm_fp1_format.rt;
373 mips32_insn.fp0_format.func = func;
374 break;
375 case mm_mfc1_op:
376 case mm_mtc1_op:
377 case mm_cfc1_op:
378 case mm_ctc1_op:
Steven J. Hill9355e592013-11-07 12:48:29 +0000379 case mm_mfhc1_op:
380 case mm_mthc1_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500381 if (insn.mm_fp1_format.op == mm_mfc1_op)
382 op = mfc_op;
383 else if (insn.mm_fp1_format.op == mm_mtc1_op)
384 op = mtc_op;
385 else if (insn.mm_fp1_format.op == mm_cfc1_op)
386 op = cfc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000387 else if (insn.mm_fp1_format.op == mm_ctc1_op)
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500388 op = ctc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000389 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
390 op = mfhc_op;
391 else
392 op = mthc_op;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500393 mips32_insn.fp1_format.opcode = cop1_op;
394 mips32_insn.fp1_format.op = op;
395 mips32_insn.fp1_format.rt =
396 insn.mm_fp1_format.rt;
397 mips32_insn.fp1_format.fs =
398 insn.mm_fp1_format.fs;
399 mips32_insn.fp1_format.fd = 0;
400 mips32_insn.fp1_format.func = 0;
401 break;
402 default:
403 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500404 }
405 break;
406 case mm_32f_74_op: /* c.cond.fmt */
407 mips32_insn.fp0_format.opcode = cop1_op;
408 mips32_insn.fp0_format.fmt =
409 sdps_format[insn.mm_fp4_format.fmt];
410 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
411 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
412 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
413 mips32_insn.fp0_format.func =
414 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
415 break;
416 default:
417 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500418 }
419 break;
420 default:
421 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500422 }
423
424 *insn_ptr = mips32_insn;
425 return 0;
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
429 * Redundant with logic already in kernel/branch.c,
430 * embedded in compute_return_epc. At some point,
431 * a single subroutine should be used across both
432 * modules.
433 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500434static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
435 unsigned long *contpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500437 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
438 unsigned int fcr31;
439 unsigned int bit = 0;
440
441 switch (insn.i_format.opcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 case spec_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500443 switch (insn.r_format.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 case jalr_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500445 regs->regs[insn.r_format.rd] =
446 regs->cp0_epc + dec_insn.pc_inc +
447 dec_insn.next_pc_inc;
448 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 case jr_op:
Markos Chandras5f9f41c2014-11-25 15:54:14 +0000450 /* For R6, JR already emulated in jalr_op */
451 if (NO_R6EMU && insn.r_format.opcode == jr_op)
452 break;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500453 *contpc = regs->regs[insn.r_format.rs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 1;
455 }
456 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 case bcond_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500458 switch (insn.i_format.rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 case bltzal_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 case bltzall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000461 if (NO_R6EMU && (insn.i_format.rs ||
462 insn.i_format.rt == bltzall_op))
463 break;
464
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500465 regs->regs[31] = regs->cp0_epc +
466 dec_insn.pc_inc +
467 dec_insn.next_pc_inc;
468 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500469 case bltzl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000470 if (NO_R6EMU)
471 break;
472 case bltz_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500473 if ((long)regs->regs[insn.i_format.rs] < 0)
474 *contpc = regs->cp0_epc +
475 dec_insn.pc_inc +
476 (insn.i_format.simmediate << 2);
477 else
478 *contpc = regs->cp0_epc +
479 dec_insn.pc_inc +
480 dec_insn.next_pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500482 case bgezal_op:
483 case bgezall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000484 if (NO_R6EMU && (insn.i_format.rs ||
485 insn.i_format.rt == bgezall_op))
486 break;
487
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500488 regs->regs[31] = regs->cp0_epc +
489 dec_insn.pc_inc +
490 dec_insn.next_pc_inc;
491 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500492 case bgezl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000493 if (NO_R6EMU)
494 break;
495 case bgez_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500496 if ((long)regs->regs[insn.i_format.rs] >= 0)
497 *contpc = regs->cp0_epc +
498 dec_insn.pc_inc +
499 (insn.i_format.simmediate << 2);
500 else
501 *contpc = regs->cp0_epc +
502 dec_insn.pc_inc +
503 dec_insn.next_pc_inc;
504 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 case jalx_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500508 set_isa16_mode(bit);
509 case jal_op:
510 regs->regs[31] = regs->cp0_epc +
511 dec_insn.pc_inc +
512 dec_insn.next_pc_inc;
513 /* Fall through */
514 case j_op:
515 *contpc = regs->cp0_epc + dec_insn.pc_inc;
516 *contpc >>= 28;
517 *contpc <<= 28;
518 *contpc |= (insn.j_format.target << 2);
519 /* Set microMIPS mode bit: XOR for jalx. */
520 *contpc ^= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500522 case beql_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000523 if (NO_R6EMU)
524 break;
525 case beq_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500526 if (regs->regs[insn.i_format.rs] ==
527 regs->regs[insn.i_format.rt])
528 *contpc = regs->cp0_epc +
529 dec_insn.pc_inc +
530 (insn.i_format.simmediate << 2);
531 else
532 *contpc = regs->cp0_epc +
533 dec_insn.pc_inc +
534 dec_insn.next_pc_inc;
535 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500536 case bnel_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000537 if (NO_R6EMU)
538 break;
539 case bne_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500540 if (regs->regs[insn.i_format.rs] !=
541 regs->regs[insn.i_format.rt])
542 *contpc = regs->cp0_epc +
543 dec_insn.pc_inc +
544 (insn.i_format.simmediate << 2);
545 else
546 *contpc = regs->cp0_epc +
547 dec_insn.pc_inc +
548 dec_insn.next_pc_inc;
549 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500550 case blezl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000551 if (NO_R6EMU)
552 break;
553 case blez_op:
Markos Chandrasa8ff66f2014-11-26 12:57:54 +0000554
555 /*
556 * Compact branches for R6 for the
557 * blez and blezl opcodes.
558 * BLEZ | rs = 0 | rt != 0 == BLEZALC
559 * BLEZ | rs = rt != 0 == BGEZALC
560 * BLEZ | rs != 0 | rt != 0 == BGEUC
561 * BLEZL | rs = 0 | rt != 0 == BLEZC
562 * BLEZL | rs = rt != 0 == BGEZC
563 * BLEZL | rs != 0 | rt != 0 == BGEC
564 *
565 * For real BLEZ{,L}, rt is always 0.
566 */
567 if (cpu_has_mips_r6 && insn.i_format.rt) {
568 if ((insn.i_format.opcode == blez_op) &&
569 ((!insn.i_format.rs && insn.i_format.rt) ||
570 (insn.i_format.rs == insn.i_format.rt)))
571 regs->regs[31] = regs->cp0_epc +
572 dec_insn.pc_inc;
573 *contpc = regs->cp0_epc + dec_insn.pc_inc +
574 dec_insn.next_pc_inc;
575
576 return 1;
577 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500578 if ((long)regs->regs[insn.i_format.rs] <= 0)
579 *contpc = regs->cp0_epc +
580 dec_insn.pc_inc +
581 (insn.i_format.simmediate << 2);
582 else
583 *contpc = regs->cp0_epc +
584 dec_insn.pc_inc +
585 dec_insn.next_pc_inc;
586 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500587 case bgtzl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000588 if (NO_R6EMU)
589 break;
590 case bgtz_op:
Markos Chandrasf1b44062014-11-26 13:05:09 +0000591 /*
592 * Compact branches for R6 for the
593 * bgtz and bgtzl opcodes.
594 * BGTZ | rs = 0 | rt != 0 == BGTZALC
595 * BGTZ | rs = rt != 0 == BLTZALC
596 * BGTZ | rs != 0 | rt != 0 == BLTUC
597 * BGTZL | rs = 0 | rt != 0 == BGTZC
598 * BGTZL | rs = rt != 0 == BLTZC
599 * BGTZL | rs != 0 | rt != 0 == BLTC
600 *
601 * *ZALC varint for BGTZ &&& rt != 0
602 * For real GTZ{,L}, rt is always 0.
603 */
604 if (cpu_has_mips_r6 && insn.i_format.rt) {
605 if ((insn.i_format.opcode == blez_op) &&
606 ((!insn.i_format.rs && insn.i_format.rt) ||
607 (insn.i_format.rs == insn.i_format.rt)))
608 regs->regs[31] = regs->cp0_epc +
609 dec_insn.pc_inc;
610 *contpc = regs->cp0_epc + dec_insn.pc_inc +
611 dec_insn.next_pc_inc;
612
613 return 1;
614 }
615
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500616 if ((long)regs->regs[insn.i_format.rs] > 0)
617 *contpc = regs->cp0_epc +
618 dec_insn.pc_inc +
619 (insn.i_format.simmediate << 2);
620 else
621 *contpc = regs->cp0_epc +
622 dec_insn.pc_inc +
623 dec_insn.next_pc_inc;
624 return 1;
Markos Chandrasc893ce32014-11-26 14:08:52 +0000625 case cbcond0_op:
Markos Chandras10d962d2014-11-26 15:03:54 +0000626 case cbcond1_op:
Markos Chandrasc893ce32014-11-26 14:08:52 +0000627 if (!cpu_has_mips_r6)
628 break;
629 if (insn.i_format.rt && !insn.i_format.rs)
630 regs->regs[31] = regs->cp0_epc + 4;
631 *contpc = regs->cp0_epc + dec_insn.pc_inc +
632 dec_insn.next_pc_inc;
633
634 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700635#ifdef CONFIG_CPU_CAVIUM_OCTEON
636 case lwc2_op: /* This is bbit0 on Octeon */
637 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
638 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
639 else
640 *contpc = regs->cp0_epc + 8;
641 return 1;
642 case ldc2_op: /* This is bbit032 on Octeon */
643 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
644 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
645 else
646 *contpc = regs->cp0_epc + 8;
647 return 1;
648 case swc2_op: /* This is bbit1 on Octeon */
649 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
650 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
651 else
652 *contpc = regs->cp0_epc + 8;
653 return 1;
654 case sdc2_op: /* This is bbit132 on Octeon */
655 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
656 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
657 else
658 *contpc = regs->cp0_epc + 8;
659 return 1;
Markos Chandras8467ca02014-11-26 13:56:51 +0000660#else
661 case bc6_op:
662 /*
663 * Only valid for MIPS R6 but we can still end up
664 * here from a broken userland so just tell emulator
665 * this is not a branch and let it break later on.
666 */
667 if (!cpu_has_mips_r6)
668 break;
669 *contpc = regs->cp0_epc + dec_insn.pc_inc +
670 dec_insn.next_pc_inc;
671
672 return 1;
Markos Chandras84fef632014-11-26 15:43:11 +0000673 case balc6_op:
674 if (!cpu_has_mips_r6)
675 break;
676 regs->regs[31] = regs->cp0_epc + 4;
677 *contpc = regs->cp0_epc + dec_insn.pc_inc +
678 dec_insn.next_pc_inc;
679
680 return 1;
Markos Chandras69b9a2f2014-11-27 09:32:25 +0000681 case beqzcjic_op:
682 if (!cpu_has_mips_r6)
683 break;
684 *contpc = regs->cp0_epc + dec_insn.pc_inc +
685 dec_insn.next_pc_inc;
686
687 return 1;
Markos Chandras28d6f932015-01-08 11:55:20 +0000688 case bnezcjialc_op:
689 if (!cpu_has_mips_r6)
690 break;
691 if (!insn.i_format.rs)
692 regs->regs[31] = regs->cp0_epc + 4;
693 *contpc = regs->cp0_epc + dec_insn.pc_inc +
694 dec_insn.next_pc_inc;
695
696 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 case cop0_op:
699 case cop1_op:
Markos Chandrasc8a34582014-11-26 10:10:18 +0000700 /* Need to check for R6 bc1nez and bc1eqz branches */
701 if (cpu_has_mips_r6 &&
702 ((insn.i_format.rs == bc1eqz_op) ||
703 (insn.i_format.rs == bc1nez_op))) {
704 bit = 0;
705 switch (insn.i_format.rs) {
706 case bc1eqz_op:
707 if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
708 bit = 1;
709 break;
710 case bc1nez_op:
711 if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
712 bit = 1;
713 break;
714 }
715 if (bit)
716 *contpc = regs->cp0_epc +
717 dec_insn.pc_inc +
718 (insn.i_format.simmediate << 2);
719 else
720 *contpc = regs->cp0_epc +
721 dec_insn.pc_inc +
722 dec_insn.next_pc_inc;
723
724 return 1;
725 }
726 /* R2/R6 compatible cop1 instruction. Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 case cop2_op:
728 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500729 if (insn.i_format.rs == bc_op) {
730 preempt_disable();
731 if (is_fpu_owner())
Manuel Lauss842dfc12014-11-07 14:13:54 +0100732 fcr31 = read_32bit_cp1_register(CP1_STATUS);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500733 else
734 fcr31 = current->thread.fpu.fcr31;
735 preempt_enable();
736
737 bit = (insn.i_format.rt >> 2);
738 bit += (bit != 0);
739 bit += 23;
740 switch (insn.i_format.rt & 3) {
741 case 0: /* bc1f */
742 case 2: /* bc1fl */
743 if (~fcr31 & (1 << bit))
744 *contpc = regs->cp0_epc +
745 dec_insn.pc_inc +
746 (insn.i_format.simmediate << 2);
747 else
748 *contpc = regs->cp0_epc +
749 dec_insn.pc_inc +
750 dec_insn.next_pc_inc;
751 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500752 case 1: /* bc1t */
753 case 3: /* bc1tl */
754 if (fcr31 & (1 << bit))
755 *contpc = regs->cp0_epc +
756 dec_insn.pc_inc +
757 (insn.i_format.simmediate << 2);
758 else
759 *contpc = regs->cp0_epc +
760 dec_insn.pc_inc +
761 dec_insn.next_pc_inc;
762 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500763 }
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 break;
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return 0;
768}
769
770/*
771 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100772 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800773 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000774 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200775 * FPU emu is slow and bulky and optimizing this function offers fairly
776 * sizeable benefits so we try to be clever and make this function return
777 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000778 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 */
David Daneyda0bac32009-11-02 11:33:46 -0800780static inline int cop1_64bit(struct pt_regs *xcp)
781{
Ralf Baechle08a07902014-04-19 13:11:37 +0200782 if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
783 return 1;
784 else if (config_enabled(CONFIG_32BIT) &&
785 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
786 return 0;
787
Paul Burton597ce172013-11-22 13:12:07 +0000788 return !test_thread_flag(TIF_32BIT_FPREGS);
David Daneyda0bac32009-11-02 11:33:46 -0800789}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Paul Burton4227a2d2014-09-11 08:30:20 +0100791static inline bool hybrid_fprs(void)
792{
793 return test_thread_flag(TIF_HYBRID_FPREGS);
794}
795
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200796#define SIFROMREG(si, x) \
797do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100798 if (cop1_64bit(xcp) && !hybrid_fprs()) \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100799 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000800 else \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100801 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000802} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800803
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200804#define SITOREG(si, x) \
805do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100806 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000807 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000808 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000809 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
810 set_fpr32(&ctx->fpr[x], i, 0); \
811 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000812 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000813 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000814} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Paul Burtonc8c0da62014-09-24 10:45:37 +0100816#define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000817
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200818#define SITOHREG(si, x) \
819do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000820 unsigned i; \
821 set_fpr32(&ctx->fpr[x], 1, si); \
822 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
823 set_fpr32(&ctx->fpr[x], i, 0); \
824} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000825
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200826#define DIFROMREG(di, x) \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000827 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
828
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200829#define DITOREG(di, x) \
830do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000831 unsigned fpr, i; \
832 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
833 set_fpr64(&ctx->fpr[fpr], 0, di); \
834 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
835 set_fpr64(&ctx->fpr[fpr], i, 0); \
836} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Ralf Baechle21a151d2007-10-11 23:46:15 +0100838#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
839#define SPTOREG(sp, x) SITOREG((sp).bits, x)
840#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
841#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843/*
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100844 * Emulate a CFC1 instruction.
845 */
846static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
847 mips_instruction ir)
848{
849 u32 value;
850
851 if (MIPSInst_RD(ir) == FPCREG_CSR) {
852 value = ctx->fcr31;
853 pr_debug("%p gpr[%d]<-csr=%08x\n",
854 (void *)xcp->cp0_epc,
855 MIPSInst_RT(ir), value);
856 } else if (MIPSInst_RD(ir) == FPCREG_RID)
Maciej W. Rozyckif6843622015-04-03 23:27:26 +0100857 value = current_cpu_data.fpu_id;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100858 else
859 value = 0;
860 if (MIPSInst_RT(ir))
861 xcp->regs[MIPSInst_RT(ir)] = value;
862}
863
864/*
865 * Emulate a CTC1 instruction.
866 */
867static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
868 mips_instruction ir)
869{
870 u32 value;
871
872 if (MIPSInst_RT(ir) == 0)
873 value = 0;
874 else
875 value = xcp->regs[MIPSInst_RT(ir)];
876
877 /* we only have one writable control reg
878 */
879 if (MIPSInst_RD(ir) == FPCREG_CSR) {
880 pr_debug("%p gpr[%d]->csr=%08x\n",
881 (void *)xcp->cp0_epc,
882 MIPSInst_RT(ir), value);
883
884 /* Don't write reserved bits. */
885 ctx->fcr31 = value & ~FPU_CSR_RSVD;
886 }
887}
888
889/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 * Emulate the single floating point instruction pointed at by EPC.
891 * Two instructions if the instruction is in a branch delay slot.
892 */
893
David Daney515b0292010-10-21 16:32:26 -0700894static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500895 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500897 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200898 unsigned int cond, cbit;
899 mips_instruction ir;
900 int likely, pc_inc;
901 u32 __user *wva;
902 u64 __user *dva;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200903 u32 wval;
904 u64 dval;
905 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Ralf Baechle70e4c232014-04-30 11:09:44 +0200907 /*
908 * These are giving gcc a gentle hint about what to expect in
909 * dec_inst in order to do better optimization.
910 */
911 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
912 unreachable();
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200915 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500916 if (dec_insn.micro_mips_mode) {
917 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200918 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500919 } else {
920 if (!isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200921 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500922 }
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200925 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /*
927 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +0100928 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 * BEFORE we do the cop1 instruction.
930 *
931 * This branch could be a COP1 branch, but in that case we
932 * would have had a trap for that instruction, and would not
933 * come through this route.
934 *
935 * Linux MIPS branch emulator operates on context, updating the
936 * cp0_epc.
937 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500938 ir = dec_insn.next_insn; /* process delay slot instr */
939 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +0000940 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500941 ir = dec_insn.insn; /* process current instr */
942 pc_inc = dec_insn.pc_inc;
943 }
944
945 /*
946 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
947 * instructions, we want to convert microMIPS FPU instructions
948 * into MIPS32 instructions so that we could reuse all of the
949 * FPU emulation code.
950 *
951 * NOTE: We cannot do this for branch instructions since they
952 * are not a subset. Example: Cannot emulate a 16-bit
953 * aligned target address with a MIPS32 instruction.
954 */
955 if (dec_insn.micro_mips_mode) {
956 /*
957 * If next instruction is a 16-bit instruction, then it
958 * it cannot be a FPU instruction. This could happen
959 * since we can be called for non-FPU instructions.
960 */
961 if ((pc_inc == 2) ||
962 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
963 == SIGILL))
964 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200967emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +0200968 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -0800969 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 switch (MIPSInst_OPCODE(ir)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200971 case ldc1_op:
972 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
973 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800974 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -0700975
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200976 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800977 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200978 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return SIGBUS;
980 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200981 if (__get_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -0700982 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200983 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -0700984 return SIGSEGV;
985 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200986 DITOREG(dval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200989 case sdc1_op:
990 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
991 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -0800992 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200993 DIFROMREG(dval, MIPSInst_RT(ir));
994 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -0800995 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200996 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return SIGBUS;
998 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200999 if (__put_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -07001000 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001001 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -07001002 return SIGSEGV;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001006 case lwc1_op:
1007 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1008 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001009 MIPS_FPU_EMU_INC_STATS(loads);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001010 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001011 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001012 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return SIGBUS;
1014 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001015 if (__get_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -07001016 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001017 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -07001018 return SIGSEGV;
1019 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001020 SITOREG(wval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001023 case swc1_op:
1024 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1025 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001026 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001027 SIFROMREG(wval, MIPSInst_RT(ir));
1028 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001029 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001030 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return SIGBUS;
1032 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001033 if (__put_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -07001034 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001035 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -07001036 return SIGSEGV;
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 case cop1_op:
1041 switch (MIPSInst_RS(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 case dmfc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001043 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1044 return SIGILL;
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 /* copregister fs -> gpr[rt] */
1047 if (MIPSInst_RT(ir) != 0) {
1048 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1049 MIPSInst_RD(ir));
1050 }
1051 break;
1052
1053 case dmtc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001054 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1055 return SIGILL;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /* copregister fs <- rt */
1058 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001061 case mfhc_op:
1062 if (!cpu_has_mips_r2)
1063 goto sigill;
1064
1065 /* copregister rd -> gpr[rt] */
1066 if (MIPSInst_RT(ir) != 0) {
1067 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1068 MIPSInst_RD(ir));
1069 }
1070 break;
1071
1072 case mthc_op:
1073 if (!cpu_has_mips_r2)
1074 goto sigill;
1075
1076 /* copregister rd <- gpr[rt] */
1077 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1078 break;
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 case mfc_op:
1081 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (MIPSInst_RT(ir) != 0) {
1083 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1084 MIPSInst_RD(ir));
1085 }
1086 break;
1087
1088 case mtc_op:
1089 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1091 break;
1092
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001093 case cfc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /* cop control register rd -> gpr[rt] */
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +01001095 cop1_cfc(xcp, ctx, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001098 case ctc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* copregister rd <- rt */
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +01001100 cop1_ctc(xcp, ctx, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1102 return SIGFPE;
1103 }
1104 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001106 case bc_op:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001107 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return SIGILL;
1109
Ralf Baechle08a07902014-04-19 13:11:37 +02001110 if (cpu_has_mips_4_5_r)
1111 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1112 else
1113 cbit = FPU_CSR_COND;
1114 cond = ctx->fcr31 & cbit;
1115
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001116 likely = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 switch (MIPSInst_RT(ir) & 3) {
1118 case bcfl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001119 if (cpu_has_mips_2_3_4_5_r)
1120 likely = 1;
1121 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 case bcf_op:
1123 cond = !cond;
1124 break;
1125 case bctl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001126 if (cpu_has_mips_2_3_4_5_r)
1127 likely = 1;
1128 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 case bct_op:
1130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001133 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (cond) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001135 /*
1136 * Branch taken: emulate dslot instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001138 unsigned long bcpc;
1139
1140 /*
1141 * Remember EPC at the branch to point back
1142 * at so that any delay-slot instruction
1143 * signal is not silently ignored.
1144 */
1145 bcpc = xcp->cp0_epc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001146 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001148 contpc = MIPSInst_SIMM(ir);
1149 ir = dec_insn.next_insn;
1150 if (dec_insn.micro_mips_mode) {
1151 contpc = (xcp->cp0_epc + (contpc << 1));
1152
1153 /* If 16-bit instruction, not FPU. */
1154 if ((dec_insn.next_pc_inc == 2) ||
1155 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1156
1157 /*
1158 * Since this instruction will
1159 * be put on the stack with
1160 * 32-bit words, get around
1161 * this problem by putting a
1162 * NOP16 as the second one.
1163 */
1164 if (dec_insn.next_pc_inc == 2)
1165 ir = (ir & (~0xffff)) | MM_NOP16;
1166
1167 /*
1168 * Single step the non-CP1
1169 * instruction in the dslot.
1170 */
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001171 sig = mips_dsemul(xcp, ir,
1172 contpc);
1173 if (sig)
1174 xcp->cp0_epc = bcpc;
1175 /*
1176 * SIGILL forces out of
1177 * the emulation loop.
1178 */
1179 return sig ? sig : SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001180 }
1181 } else
1182 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 switch (MIPSInst_OPCODE(ir)) {
1185 case lwc1_op:
1186 case swc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001187 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 case ldc1_op:
1190 case sdc1_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001191 if (cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001192 goto emul;
1193
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001194 goto bc_sigill;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001195
Ralf Baechle08a07902014-04-19 13:11:37 +02001196 case cop1_op:
1197 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001198
Ralf Baechle08a07902014-04-19 13:11:37 +02001199 case cop1x_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001200 if (cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001201 /* its one of ours */
1202 goto emul;
1203
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001204 goto bc_sigill;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 case spec_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001207 switch (MIPSInst_FUNC(ir)) {
1208 case movc_op:
1209 if (cpu_has_mips_4_5_r)
1210 goto emul;
Ralf Baechle08a07902014-04-19 13:11:37 +02001211
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001212 goto bc_sigill;
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 break;
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001215
1216 bc_sigill:
1217 xcp->cp0_epc = bcpc;
1218 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
1221 /*
1222 * Single step the non-cp1
1223 * instruction in the dslot
1224 */
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001225 sig = mips_dsemul(xcp, ir, contpc);
1226 if (sig)
1227 xcp->cp0_epc = bcpc;
1228 /* SIGILL forces out of the emulation loop. */
1229 return sig ? sig : SIGILL;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001230 } else if (likely) { /* branch not taken */
Maciej W. Rozycki5d77cf22015-04-03 23:24:24 +01001231 /*
1232 * branch likely nullifies
1233 * dslot if not taken
1234 */
1235 xcp->cp0_epc += dec_insn.pc_inc;
1236 contpc += dec_insn.pc_inc;
1237 /*
1238 * else continue & execute
1239 * dslot as normal insn
1240 */
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 default:
1245 if (!(MIPSInst_RS(ir) & 0x10))
1246 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001248 /* a real fpu computation instruction */
1249 if ((sig = fpu_emu(xcp, ctx, ir)))
1250 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 break;
1253
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001254 case cop1x_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001255 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001256 return SIGILL;
1257
1258 sig = fpux_emu(xcp, ctx, ir, fault_addr);
David Daney515b0292010-10-21 16:32:26 -07001259 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return sig;
1261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001264 if (!cpu_has_mips_4_5_r)
1265 return SIGILL;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (MIPSInst_FUNC(ir) != movc_op)
1268 return SIGILL;
1269 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1270 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1271 xcp->regs[MIPSInst_RD(ir)] =
1272 xcp->regs[MIPSInst_RS(ir)];
1273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 default:
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001275sigill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return SIGILL;
1277 }
1278
1279 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001280 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001281 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 return 0;
1284}
1285
1286/*
1287 * Conversion table from MIPS compare ops 48-63
1288 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1289 */
1290static const unsigned char cmptab[8] = {
1291 0, /* cmp_0 (sig) cmp_sf */
1292 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1293 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1294 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1295 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1296 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1297 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1298 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1299};
1300
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/*
1303 * Additional MIPS4 instructions
1304 */
1305
Ralf Baechle47fa0c02014-04-16 11:00:12 +02001306#define DEF3OP(name, p, f1, f2, f3) \
1307static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1308 union ieee754##p s, union ieee754##p t) \
1309{ \
1310 struct _ieee754_csr ieee754_csr_save; \
1311 s = f1(s, t); \
1312 ieee754_csr_save = ieee754_csr; \
1313 s = f2(s, r); \
1314 ieee754_csr_save.cx |= ieee754_csr.cx; \
1315 ieee754_csr_save.sx |= ieee754_csr.sx; \
1316 s = f3(s); \
1317 ieee754_csr.cx |= ieee754_csr_save.cx; \
1318 ieee754_csr.sx |= ieee754_csr_save.sx; \
1319 return s; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001322static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 return ieee754dp_div(ieee754dp_one(0), d);
1325}
1326
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001327static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1330}
1331
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001332static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 return ieee754sp_div(ieee754sp_one(0), s);
1335}
1336
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001337static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1340}
1341
Ralf Baechle21a151d2007-10-11 23:46:15 +01001342DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1343DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1345DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001346DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1347DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1349DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1350
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001351static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001352 mips_instruction ir, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 unsigned rcsr = 0; /* resulting csr */
1355
David Daneyb6ee75e2009-11-05 11:34:26 -08001356 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 switch (MIPSInst_FMA_FFMT(ir)) {
1359 case s_fmt:{ /* 0 */
1360
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001361 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1362 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001363 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 u32 val;
1365
1366 switch (MIPSInst_FUNC(ir)) {
1367 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001368 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 xcp->regs[MIPSInst_FT(ir)]);
1370
David Daneyb6ee75e2009-11-05 11:34:26 -08001371 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001372 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001373 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001374 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return SIGBUS;
1376 }
David Daney515b0292010-10-21 16:32:26 -07001377 if (__get_user(val, va)) {
1378 MIPS_FPU_EMU_INC_STATS(errors);
1379 *fault_addr = va;
1380 return SIGSEGV;
1381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 SITOREG(val, MIPSInst_FD(ir));
1383 break;
1384
1385 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001386 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 xcp->regs[MIPSInst_FT(ir)]);
1388
David Daneyb6ee75e2009-11-05 11:34:26 -08001389 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001392 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1393 MIPS_FPU_EMU_INC_STATS(errors);
1394 *fault_addr = va;
1395 return SIGBUS;
1396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001398 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001399 *fault_addr = va;
1400 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402 break;
1403
1404 case madd_s_op:
1405 handler = fpemu_sp_madd;
1406 goto scoptop;
1407 case msub_s_op:
1408 handler = fpemu_sp_msub;
1409 goto scoptop;
1410 case nmadd_s_op:
1411 handler = fpemu_sp_nmadd;
1412 goto scoptop;
1413 case nmsub_s_op:
1414 handler = fpemu_sp_nmsub;
1415 goto scoptop;
1416
1417 scoptop:
1418 SPFROMREG(fr, MIPSInst_FR(ir));
1419 SPFROMREG(fs, MIPSInst_FS(ir));
1420 SPFROMREG(ft, MIPSInst_FT(ir));
1421 fd = (*handler) (fr, fs, ft);
1422 SPTOREG(fd, MIPSInst_FD(ir));
1423
1424 copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001425 if (ieee754_cxtest(IEEE754_INEXACT)) {
1426 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001428 }
1429 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1430 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001432 }
1433 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1434 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001436 }
1437 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1438 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001444 /*printk ("SIGFPE: FPU csr = %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 ctx->fcr31); */
1446 return SIGFPE;
1447 }
1448
1449 break;
1450
1451 default:
1452 return SIGILL;
1453 }
1454 break;
1455 }
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001458 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1459 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001460 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 u64 val;
1462
1463 switch (MIPSInst_FUNC(ir)) {
1464 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001465 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 xcp->regs[MIPSInst_FT(ir)]);
1467
David Daneyb6ee75e2009-11-05 11:34:26 -08001468 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001469 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001470 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001471 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return SIGBUS;
1473 }
David Daney515b0292010-10-21 16:32:26 -07001474 if (__get_user(val, va)) {
1475 MIPS_FPU_EMU_INC_STATS(errors);
1476 *fault_addr = va;
1477 return SIGSEGV;
1478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 DITOREG(val, MIPSInst_FD(ir));
1480 break;
1481
1482 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001483 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 xcp->regs[MIPSInst_FT(ir)]);
1485
David Daneyb6ee75e2009-11-05 11:34:26 -08001486 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001488 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001489 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001490 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return SIGBUS;
1492 }
David Daney515b0292010-10-21 16:32:26 -07001493 if (__put_user(val, va)) {
1494 MIPS_FPU_EMU_INC_STATS(errors);
1495 *fault_addr = va;
1496 return SIGSEGV;
1497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 break;
1499
1500 case madd_d_op:
1501 handler = fpemu_dp_madd;
1502 goto dcoptop;
1503 case msub_d_op:
1504 handler = fpemu_dp_msub;
1505 goto dcoptop;
1506 case nmadd_d_op:
1507 handler = fpemu_dp_nmadd;
1508 goto dcoptop;
1509 case nmsub_d_op:
1510 handler = fpemu_dp_nmsub;
1511 goto dcoptop;
1512
1513 dcoptop:
1514 DPFROMREG(fr, MIPSInst_FR(ir));
1515 DPFROMREG(fs, MIPSInst_FS(ir));
1516 DPFROMREG(ft, MIPSInst_FT(ir));
1517 fd = (*handler) (fr, fs, ft);
1518 DPTOREG(fd, MIPSInst_FD(ir));
1519 goto copcsr;
1520
1521 default:
1522 return SIGILL;
1523 }
1524 break;
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001527 case 0x3:
1528 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 /* ignore prefx operation */
1532 break;
1533
1534 default:
1535 return SIGILL;
1536 }
1537
1538 return 0;
1539}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541
1542
1543/*
1544 * Emulate a single COP1 arithmetic instruction.
1545 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001546static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 mips_instruction ir)
1548{
1549 int rfmt; /* resulting format */
1550 unsigned rcsr = 0; /* resulting csr */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001551 unsigned int oldrm;
1552 unsigned int cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 unsigned cond;
1554 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001555 union ieee754dp d;
1556 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 s64 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 } rv; /* resulting value */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001560 u64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
David Daneyb6ee75e2009-11-05 11:34:26 -08001562 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001564 case s_fmt: { /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001566 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1567 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 } handler;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001569 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 switch (MIPSInst_FUNC(ir)) {
1572 /* binary ops */
1573 case fadd_op:
1574 handler.b = ieee754sp_add;
1575 goto scopbop;
1576 case fsub_op:
1577 handler.b = ieee754sp_sub;
1578 goto scopbop;
1579 case fmul_op:
1580 handler.b = ieee754sp_mul;
1581 goto scopbop;
1582 case fdiv_op:
1583 handler.b = ieee754sp_div;
1584 goto scopbop;
1585
1586 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 case fsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001588 if (!cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001589 return SIGILL;
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 handler.u = ieee754sp_sqrt;
1592 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001593
Ralf Baechle08a07902014-04-19 13:11:37 +02001594 /*
1595 * Note that on some MIPS IV implementations such as the
1596 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1597 * achieve full IEEE-754 accuracy - however this emulator does.
1598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 case frsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001600 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001601 return SIGILL;
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 handler.u = fpemu_sp_rsqrt;
1604 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 case frecip_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001607 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001608 return SIGILL;
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 handler.u = fpemu_sp_recip;
1611 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001614 if (!cpu_has_mips_4_5_r)
1615 return SIGILL;
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1618 if (((ctx->fcr31 & cond) != 0) !=
1619 ((MIPSInst_FT(ir) & 1) != 0))
1620 return 0;
1621 SPFROMREG(rv.s, MIPSInst_FS(ir));
1622 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001625 if (!cpu_has_mips_4_5_r)
1626 return SIGILL;
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1629 return 0;
1630 SPFROMREG(rv.s, MIPSInst_FS(ir));
1631 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001634 if (!cpu_has_mips_4_5_r)
1635 return SIGILL;
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1638 return 0;
1639 SPFROMREG(rv.s, MIPSInst_FS(ir));
1640 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 case fabs_op:
1643 handler.u = ieee754sp_abs;
1644 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 case fneg_op:
1647 handler.u = ieee754sp_neg;
1648 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 case fmov_op:
1651 /* an easy one */
1652 SPFROMREG(rv.s, MIPSInst_FS(ir));
1653 goto copcsr;
1654
1655 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001656scopbop:
1657 SPFROMREG(fs, MIPSInst_FS(ir));
1658 SPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001660 rv.s = (*handler.b) (fs, ft);
1661 goto copcsr;
1662scopuop:
1663 SPFROMREG(fs, MIPSInst_FS(ir));
1664 rv.s = (*handler.u) (fs);
1665 goto copcsr;
1666copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001667 if (ieee754_cxtest(IEEE754_INEXACT)) {
1668 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001670 }
1671 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1672 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001674 }
1675 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1676 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001678 }
1679 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1680 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001682 }
1683 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1684 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 break;
1688
1689 /* unary conv ops */
1690 case fcvts_op:
1691 return SIGILL; /* not defined */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001693 case fcvtd_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 SPFROMREG(fs, MIPSInst_FS(ir));
1695 rv.d = ieee754dp_fsp(fs);
1696 rfmt = d_fmt;
1697 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001699 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 SPFROMREG(fs, MIPSInst_FS(ir));
1701 rv.w = ieee754sp_tint(fs);
1702 rfmt = w_fmt;
1703 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 case fround_op:
1706 case ftrunc_op:
1707 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001708 case ffloor_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001709 if (!cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001710 return SIGILL;
1711
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001712 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 SPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01001714 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 rv.w = ieee754sp_tint(fs);
1716 ieee754_csr.rm = oldrm;
1717 rfmt = w_fmt;
1718 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001720 case fcvtl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001721 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001722 return SIGILL;
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 SPFROMREG(fs, MIPSInst_FS(ir));
1725 rv.l = ieee754sp_tlong(fs);
1726 rfmt = l_fmt;
1727 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 case froundl_op:
1730 case ftruncl_op:
1731 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001732 case ffloorl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001733 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001734 return SIGILL;
1735
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001736 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 SPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01001738 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 rv.l = ieee754sp_tlong(fs);
1740 ieee754_csr.rm = oldrm;
1741 rfmt = l_fmt;
1742 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 default:
1745 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1746 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001747 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 SPFROMREG(fs, MIPSInst_FS(ir));
1750 SPFROMREG(ft, MIPSInst_FT(ir));
1751 rv.w = ieee754sp_cmp(fs, ft,
1752 cmptab[cmpop & 0x7], cmpop & 0x8);
1753 rfmt = -1;
1754 if ((cmpop & 0x8) && ieee754_cxtest
1755 (IEEE754_INVALID_OPERATION))
1756 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1757 else
1758 goto copcsr;
1759
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001760 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 break;
1763 }
1764 break;
1765 }
1766
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001767 case d_fmt: {
1768 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001770 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
1771 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 } handler;
1773
1774 switch (MIPSInst_FUNC(ir)) {
1775 /* binary ops */
1776 case fadd_op:
1777 handler.b = ieee754dp_add;
1778 goto dcopbop;
1779 case fsub_op:
1780 handler.b = ieee754dp_sub;
1781 goto dcopbop;
1782 case fmul_op:
1783 handler.b = ieee754dp_mul;
1784 goto dcopbop;
1785 case fdiv_op:
1786 handler.b = ieee754dp_div;
1787 goto dcopbop;
1788
1789 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001791 if (!cpu_has_mips_2_3_4_5_r)
1792 return SIGILL;
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 handler.u = ieee754dp_sqrt;
1795 goto dcopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001796 /*
1797 * Note that on some MIPS IV implementations such as the
1798 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1799 * achieve full IEEE-754 accuracy - however this emulator does.
1800 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 case frsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001802 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001803 return SIGILL;
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 handler.u = fpemu_dp_rsqrt;
1806 goto dcopuop;
1807 case frecip_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001808 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001809 return SIGILL;
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 handler.u = fpemu_dp_recip;
1812 goto dcopuop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001814 if (!cpu_has_mips_4_5_r)
1815 return SIGILL;
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1818 if (((ctx->fcr31 & cond) != 0) !=
1819 ((MIPSInst_FT(ir) & 1) != 0))
1820 return 0;
1821 DPFROMREG(rv.d, MIPSInst_FS(ir));
1822 break;
1823 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001824 if (!cpu_has_mips_4_5_r)
1825 return SIGILL;
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1828 return 0;
1829 DPFROMREG(rv.d, MIPSInst_FS(ir));
1830 break;
1831 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001832 if (!cpu_has_mips_4_5_r)
1833 return SIGILL;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1836 return 0;
1837 DPFROMREG(rv.d, MIPSInst_FS(ir));
1838 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 case fabs_op:
1840 handler.u = ieee754dp_abs;
1841 goto dcopuop;
1842
1843 case fneg_op:
1844 handler.u = ieee754dp_neg;
1845 goto dcopuop;
1846
1847 case fmov_op:
1848 /* an easy one */
1849 DPFROMREG(rv.d, MIPSInst_FS(ir));
1850 goto copcsr;
1851
1852 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001853dcopbop:
1854 DPFROMREG(fs, MIPSInst_FS(ir));
1855 DPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001857 rv.d = (*handler.b) (fs, ft);
1858 goto copcsr;
1859dcopuop:
1860 DPFROMREG(fs, MIPSInst_FS(ir));
1861 rv.d = (*handler.u) (fs);
1862 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001864 /*
1865 * unary conv ops
1866 */
1867 case fcvts_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 DPFROMREG(fs, MIPSInst_FS(ir));
1869 rv.s = ieee754sp_fdp(fs);
1870 rfmt = s_fmt;
1871 goto copcsr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 case fcvtd_op:
1874 return SIGILL; /* not defined */
1875
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001876 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 DPFROMREG(fs, MIPSInst_FS(ir));
1878 rv.w = ieee754dp_tint(fs); /* wrong */
1879 rfmt = w_fmt;
1880 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 case fround_op:
1883 case ftrunc_op:
1884 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001885 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001886 if (!cpu_has_mips_2_3_4_5_r)
1887 return SIGILL;
1888
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001889 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 DPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01001891 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 rv.w = ieee754dp_tint(fs);
1893 ieee754_csr.rm = oldrm;
1894 rfmt = w_fmt;
1895 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001897 case fcvtl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001898 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001899 return SIGILL;
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 DPFROMREG(fs, MIPSInst_FS(ir));
1902 rv.l = ieee754dp_tlong(fs);
1903 rfmt = l_fmt;
1904 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 case froundl_op:
1907 case ftruncl_op:
1908 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001909 case ffloorl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001910 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001911 return SIGILL;
1912
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001913 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 DPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01001915 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 rv.l = ieee754dp_tlong(fs);
1917 ieee754_csr.rm = oldrm;
1918 rfmt = l_fmt;
1919 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 default:
1922 if (MIPSInst_FUNC(ir) >= fcmp_op) {
1923 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001924 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 DPFROMREG(fs, MIPSInst_FS(ir));
1927 DPFROMREG(ft, MIPSInst_FT(ir));
1928 rv.w = ieee754dp_cmp(fs, ft,
1929 cmptab[cmpop & 0x7], cmpop & 0x8);
1930 rfmt = -1;
1931 if ((cmpop & 0x8)
1932 &&
1933 ieee754_cxtest
1934 (IEEE754_INVALID_OPERATION))
1935 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1936 else
1937 goto copcsr;
1938
1939 }
1940 else {
1941 return SIGILL;
1942 }
1943 break;
1944 }
1945 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001947 case w_fmt:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 switch (MIPSInst_FUNC(ir)) {
1949 case fcvts_op:
1950 /* convert word to single precision real */
1951 SPFROMREG(fs, MIPSInst_FS(ir));
1952 rv.s = ieee754sp_fint(fs.bits);
1953 rfmt = s_fmt;
1954 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 case fcvtd_op:
1956 /* convert word to double precision real */
1957 SPFROMREG(fs, MIPSInst_FS(ir));
1958 rv.d = ieee754dp_fint(fs.bits);
1959 rfmt = d_fmt;
1960 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 default:
1962 return SIGILL;
1963 }
1964 break;
1965 }
1966
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001967 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02001968
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001969 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001970 return SIGILL;
1971
Paul Burtonbbd426f2014-02-13 11:26:41 +00001972 DIFROMREG(bits, MIPSInst_FS(ir));
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 switch (MIPSInst_FUNC(ir)) {
1975 case fcvts_op:
1976 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001977 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 rfmt = s_fmt;
1979 goto copcsr;
1980 case fcvtd_op:
1981 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00001982 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 rfmt = d_fmt;
1984 goto copcsr;
1985 default:
1986 return SIGILL;
1987 }
1988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 default:
1991 return SIGILL;
1992 }
1993
1994 /*
1995 * Update the fpu CSR register for this operation.
1996 * If an exception is required, generate a tidy SIGFPE exception,
1997 * without updating the result register.
1998 * Note: cause exception bits do not accumulate, they are rewritten
1999 * for each op; only the flag/sticky bits accumulate.
2000 */
2001 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
2002 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002003 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return SIGFPE;
2005 }
2006
2007 /*
2008 * Now we can safely write the result back to the register file.
2009 */
2010 switch (rfmt) {
Ralf Baechle08a07902014-04-19 13:11:37 +02002011 case -1:
2012
2013 if (cpu_has_mips_4_5_r)
Rob Kendrickc3b9b942014-07-23 10:03:58 +01002014 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 else
Ralf Baechle08a07902014-04-19 13:11:37 +02002016 cbit = FPU_CSR_COND;
2017 if (rv.w)
2018 ctx->fcr31 |= cbit;
2019 else
2020 ctx->fcr31 &= ~cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 break;
Ralf Baechle08a07902014-04-19 13:11:37 +02002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 case d_fmt:
2024 DPTOREG(rv.d, MIPSInst_FD(ir));
2025 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 case s_fmt:
2027 SPTOREG(rv.s, MIPSInst_FD(ir));
2028 break;
2029 case w_fmt:
2030 SITOREG(rv.w, MIPSInst_FD(ir));
2031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 case l_fmt:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002033 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002034 return SIGILL;
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 DITOREG(rv.l, MIPSInst_FD(ir));
2037 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 default:
2039 return SIGILL;
2040 }
2041
2042 return 0;
2043}
2044
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002045int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07002046 int has_fpu, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Ralf Baechle333d1f62005-02-28 17:55:57 +00002048 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002049 struct mm_decoded_insn dec_insn;
2050 u16 instr[4];
2051 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int sig = 0;
2053
2054 oldepc = xcp->cp0_epc;
2055 do {
2056 prevepc = xcp->cp0_epc;
2057
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002058 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2059 /*
2060 * Get next 2 microMIPS instructions and convert them
2061 * into 32-bit instructions.
2062 */
2063 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2064 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2065 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2066 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2067 MIPS_FPU_EMU_INC_STATS(errors);
2068 return SIGBUS;
2069 }
2070 instr_ptr = instr;
2071
2072 /* Get first instruction. */
2073 if (mm_insn_16bit(*instr_ptr)) {
2074 /* Duplicate the half-word. */
2075 dec_insn.insn = (*instr_ptr << 16) |
2076 (*instr_ptr);
2077 /* 16-bit instruction. */
2078 dec_insn.pc_inc = 2;
2079 instr_ptr += 1;
2080 } else {
2081 dec_insn.insn = (*instr_ptr << 16) |
2082 *(instr_ptr+1);
2083 /* 32-bit instruction. */
2084 dec_insn.pc_inc = 4;
2085 instr_ptr += 2;
2086 }
2087 /* Get second instruction. */
2088 if (mm_insn_16bit(*instr_ptr)) {
2089 /* Duplicate the half-word. */
2090 dec_insn.next_insn = (*instr_ptr << 16) |
2091 (*instr_ptr);
2092 /* 16-bit instruction. */
2093 dec_insn.next_pc_inc = 2;
2094 } else {
2095 dec_insn.next_insn = (*instr_ptr << 16) |
2096 *(instr_ptr+1);
2097 /* 32-bit instruction. */
2098 dec_insn.next_pc_inc = 4;
2099 }
2100 dec_insn.micro_mips_mode = 1;
2101 } else {
2102 if ((get_user(dec_insn.insn,
2103 (mips_instruction __user *) xcp->cp0_epc)) ||
2104 (get_user(dec_insn.next_insn,
2105 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2106 MIPS_FPU_EMU_INC_STATS(errors);
2107 return SIGBUS;
2108 }
2109 dec_insn.pc_inc = 4;
2110 dec_insn.next_pc_inc = 4;
2111 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002113
2114 if ((dec_insn.insn == 0) ||
2115 ((dec_insn.pc_inc == 2) &&
2116 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2117 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002119 /*
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01002120 * The 'ieee754_csr' is an alias of ctx->fcr31.
2121 * No need to copy ctx->fcr31 to ieee754_csr.
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002122 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002123 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002126 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 break;
2128 if (sig)
2129 break;
2130
2131 cond_resched();
2132 } while (xcp->cp0_epc > prevepc);
2133
2134 /* SIGILL indicates a non-fpu instruction */
2135 if (sig == SIGILL && xcp->cp0_epc != oldepc)
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002136 /* but if EPC has advanced, then ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 sig = 0;
2138
2139 return sig;
2140}