blob: 9ade60ca08e06b278064c73d6e38b45caa94c3ae [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 Baechle85c51c52014-04-16 02:46:11 +020038#include <linux/percpu-defs.h>
Deng-Cheng Zhu7f788d22010-10-12 19:37:21 +080039#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Ralf Baechlecd8ee342014-04-16 02:09:53 +020041#include <asm/branch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/inst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/ptrace.h>
44#include <asm/signal.h>
Ralf Baechlecd8ee342014-04-16 02:09:53 +020045#include <asm/uaccess.h>
46
Maciej W. Rozyckif6843622015-04-03 23:27:26 +010047#include <asm/cpu-info.h>
Ralf Baechlecd8ee342014-04-16 02:09:53 +020048#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>
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +000051#include <asm/mips-r2-to-r6-emul.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include "ieee754.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* Function which emulates a floating point instruction. */
56
Atsushi Nemotoeae89072006-05-16 01:26:03 +090057static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 mips_instruction);
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int fpux_emu(struct pt_regs *,
David Daney515b0292010-10-21 16:32:26 -070061 struct mips_fpu_struct *, mips_instruction, void *__user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* Control registers */
64
65#define FPCREG_RID 0 /* $0 = revision id */
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +010066#define FPCREG_FCCR 25 /* $25 = fccr */
67#define FPCREG_FEXR 26 /* $26 = fexr */
68#define FPCREG_FENR 28 /* $28 = fenr */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define FPCREG_CSR 31 /* $31 = csr */
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* convert condition code register number to csr bit */
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +000072const unsigned int fpucondbit[8] = {
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +010073 FPU_CSR_COND,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 FPU_CSR_COND1,
75 FPU_CSR_COND2,
76 FPU_CSR_COND3,
77 FPU_CSR_COND4,
78 FPU_CSR_COND5,
79 FPU_CSR_COND6,
80 FPU_CSR_COND7
81};
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Leonid Yegoshin102cedc2013-03-25 12:09:02 -050083/* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
84static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
85static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
86static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
87static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
88
89/*
90 * This functions translates a 32-bit microMIPS instruction
91 * into a 32-bit MIPS32 instruction. Returns 0 on success
92 * and SIGILL otherwise.
93 */
94static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
95{
96 union mips_instruction insn = *insn_ptr;
97 union mips_instruction mips32_insn = insn;
98 int func, fmt, op;
99
100 switch (insn.mm_i_format.opcode) {
101 case mm_ldc132_op:
102 mips32_insn.mm_i_format.opcode = ldc1_op;
103 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
104 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
105 break;
106 case mm_lwc132_op:
107 mips32_insn.mm_i_format.opcode = lwc1_op;
108 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
109 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
110 break;
111 case mm_sdc132_op:
112 mips32_insn.mm_i_format.opcode = sdc1_op;
113 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
114 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
115 break;
116 case mm_swc132_op:
117 mips32_insn.mm_i_format.opcode = swc1_op;
118 mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
119 mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
120 break;
121 case mm_pool32i_op:
122 /* NOTE: offset is << by 1 if in microMIPS mode. */
123 if ((insn.mm_i_format.rt == mm_bc1f_op) ||
124 (insn.mm_i_format.rt == mm_bc1t_op)) {
125 mips32_insn.fb_format.opcode = cop1_op;
126 mips32_insn.fb_format.bc = bc_op;
127 mips32_insn.fb_format.flag =
128 (insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
129 } else
130 return SIGILL;
131 break;
132 case mm_pool32f_op:
133 switch (insn.mm_fp0_format.func) {
134 case mm_32f_01_op:
135 case mm_32f_11_op:
136 case mm_32f_02_op:
137 case mm_32f_12_op:
138 case mm_32f_41_op:
139 case mm_32f_51_op:
140 case mm_32f_42_op:
141 case mm_32f_52_op:
142 op = insn.mm_fp0_format.func;
143 if (op == mm_32f_01_op)
144 func = madd_s_op;
145 else if (op == mm_32f_11_op)
146 func = madd_d_op;
147 else if (op == mm_32f_02_op)
148 func = nmadd_s_op;
149 else if (op == mm_32f_12_op)
150 func = nmadd_d_op;
151 else if (op == mm_32f_41_op)
152 func = msub_s_op;
153 else if (op == mm_32f_51_op)
154 func = msub_d_op;
155 else if (op == mm_32f_42_op)
156 func = nmsub_s_op;
157 else
158 func = nmsub_d_op;
159 mips32_insn.fp6_format.opcode = cop1x_op;
160 mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
161 mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
162 mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
163 mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
164 mips32_insn.fp6_format.func = func;
165 break;
166 case mm_32f_10_op:
167 func = -1; /* Invalid */
168 op = insn.mm_fp5_format.op & 0x7;
169 if (op == mm_ldxc1_op)
170 func = ldxc1_op;
171 else if (op == mm_sdxc1_op)
172 func = sdxc1_op;
173 else if (op == mm_lwxc1_op)
174 func = lwxc1_op;
175 else if (op == mm_swxc1_op)
176 func = swxc1_op;
177
178 if (func != -1) {
179 mips32_insn.r_format.opcode = cop1x_op;
180 mips32_insn.r_format.rs =
181 insn.mm_fp5_format.base;
182 mips32_insn.r_format.rt =
183 insn.mm_fp5_format.index;
184 mips32_insn.r_format.rd = 0;
185 mips32_insn.r_format.re = insn.mm_fp5_format.fd;
186 mips32_insn.r_format.func = func;
187 } else
188 return SIGILL;
189 break;
190 case mm_32f_40_op:
191 op = -1; /* Invalid */
192 if (insn.mm_fp2_format.op == mm_fmovt_op)
193 op = 1;
194 else if (insn.mm_fp2_format.op == mm_fmovf_op)
195 op = 0;
196 if (op != -1) {
197 mips32_insn.fp0_format.opcode = cop1_op;
198 mips32_insn.fp0_format.fmt =
199 sdps_format[insn.mm_fp2_format.fmt];
200 mips32_insn.fp0_format.ft =
201 (insn.mm_fp2_format.cc<<2) + op;
202 mips32_insn.fp0_format.fs =
203 insn.mm_fp2_format.fs;
204 mips32_insn.fp0_format.fd =
205 insn.mm_fp2_format.fd;
206 mips32_insn.fp0_format.func = fmovc_op;
207 } else
208 return SIGILL;
209 break;
210 case mm_32f_60_op:
211 func = -1; /* Invalid */
212 if (insn.mm_fp0_format.op == mm_fadd_op)
213 func = fadd_op;
214 else if (insn.mm_fp0_format.op == mm_fsub_op)
215 func = fsub_op;
216 else if (insn.mm_fp0_format.op == mm_fmul_op)
217 func = fmul_op;
218 else if (insn.mm_fp0_format.op == mm_fdiv_op)
219 func = fdiv_op;
220 if (func != -1) {
221 mips32_insn.fp0_format.opcode = cop1_op;
222 mips32_insn.fp0_format.fmt =
223 sdps_format[insn.mm_fp0_format.fmt];
224 mips32_insn.fp0_format.ft =
225 insn.mm_fp0_format.ft;
226 mips32_insn.fp0_format.fs =
227 insn.mm_fp0_format.fs;
228 mips32_insn.fp0_format.fd =
229 insn.mm_fp0_format.fd;
230 mips32_insn.fp0_format.func = func;
231 } else
232 return SIGILL;
233 break;
234 case mm_32f_70_op:
235 func = -1; /* Invalid */
236 if (insn.mm_fp0_format.op == mm_fmovn_op)
237 func = fmovn_op;
238 else if (insn.mm_fp0_format.op == mm_fmovz_op)
239 func = fmovz_op;
240 if (func != -1) {
241 mips32_insn.fp0_format.opcode = cop1_op;
242 mips32_insn.fp0_format.fmt =
243 sdps_format[insn.mm_fp0_format.fmt];
244 mips32_insn.fp0_format.ft =
245 insn.mm_fp0_format.ft;
246 mips32_insn.fp0_format.fs =
247 insn.mm_fp0_format.fs;
248 mips32_insn.fp0_format.fd =
249 insn.mm_fp0_format.fd;
250 mips32_insn.fp0_format.func = func;
251 } else
252 return SIGILL;
253 break;
254 case mm_32f_73_op: /* POOL32FXF */
255 switch (insn.mm_fp1_format.op) {
256 case mm_movf0_op:
257 case mm_movf1_op:
258 case mm_movt0_op:
259 case mm_movt1_op:
260 if ((insn.mm_fp1_format.op & 0x7f) ==
261 mm_movf0_op)
262 op = 0;
263 else
264 op = 1;
265 mips32_insn.r_format.opcode = spec_op;
266 mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
267 mips32_insn.r_format.rt =
268 (insn.mm_fp4_format.cc << 2) + op;
269 mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
270 mips32_insn.r_format.re = 0;
271 mips32_insn.r_format.func = movc_op;
272 break;
273 case mm_fcvtd0_op:
274 case mm_fcvtd1_op:
275 case mm_fcvts0_op:
276 case mm_fcvts1_op:
277 if ((insn.mm_fp1_format.op & 0x7f) ==
278 mm_fcvtd0_op) {
279 func = fcvtd_op;
280 fmt = swl_format[insn.mm_fp3_format.fmt];
281 } else {
282 func = fcvts_op;
283 fmt = dwl_format[insn.mm_fp3_format.fmt];
284 }
285 mips32_insn.fp0_format.opcode = cop1_op;
286 mips32_insn.fp0_format.fmt = fmt;
287 mips32_insn.fp0_format.ft = 0;
288 mips32_insn.fp0_format.fs =
289 insn.mm_fp3_format.fs;
290 mips32_insn.fp0_format.fd =
291 insn.mm_fp3_format.rt;
292 mips32_insn.fp0_format.func = func;
293 break;
294 case mm_fmov0_op:
295 case mm_fmov1_op:
296 case mm_fabs0_op:
297 case mm_fabs1_op:
298 case mm_fneg0_op:
299 case mm_fneg1_op:
300 if ((insn.mm_fp1_format.op & 0x7f) ==
301 mm_fmov0_op)
302 func = fmov_op;
303 else if ((insn.mm_fp1_format.op & 0x7f) ==
304 mm_fabs0_op)
305 func = fabs_op;
306 else
307 func = fneg_op;
308 mips32_insn.fp0_format.opcode = cop1_op;
309 mips32_insn.fp0_format.fmt =
310 sdps_format[insn.mm_fp3_format.fmt];
311 mips32_insn.fp0_format.ft = 0;
312 mips32_insn.fp0_format.fs =
313 insn.mm_fp3_format.fs;
314 mips32_insn.fp0_format.fd =
315 insn.mm_fp3_format.rt;
316 mips32_insn.fp0_format.func = func;
317 break;
318 case mm_ffloorl_op:
319 case mm_ffloorw_op:
320 case mm_fceill_op:
321 case mm_fceilw_op:
322 case mm_ftruncl_op:
323 case mm_ftruncw_op:
324 case mm_froundl_op:
325 case mm_froundw_op:
326 case mm_fcvtl_op:
327 case mm_fcvtw_op:
328 if (insn.mm_fp1_format.op == mm_ffloorl_op)
329 func = ffloorl_op;
330 else if (insn.mm_fp1_format.op == mm_ffloorw_op)
331 func = ffloor_op;
332 else if (insn.mm_fp1_format.op == mm_fceill_op)
333 func = fceill_op;
334 else if (insn.mm_fp1_format.op == mm_fceilw_op)
335 func = fceil_op;
336 else if (insn.mm_fp1_format.op == mm_ftruncl_op)
337 func = ftruncl_op;
338 else if (insn.mm_fp1_format.op == mm_ftruncw_op)
339 func = ftrunc_op;
340 else if (insn.mm_fp1_format.op == mm_froundl_op)
341 func = froundl_op;
342 else if (insn.mm_fp1_format.op == mm_froundw_op)
343 func = fround_op;
344 else if (insn.mm_fp1_format.op == mm_fcvtl_op)
345 func = fcvtl_op;
346 else
347 func = fcvtw_op;
348 mips32_insn.fp0_format.opcode = cop1_op;
349 mips32_insn.fp0_format.fmt =
350 sd_format[insn.mm_fp1_format.fmt];
351 mips32_insn.fp0_format.ft = 0;
352 mips32_insn.fp0_format.fs =
353 insn.mm_fp1_format.fs;
354 mips32_insn.fp0_format.fd =
355 insn.mm_fp1_format.rt;
356 mips32_insn.fp0_format.func = func;
357 break;
358 case mm_frsqrt_op:
359 case mm_fsqrt_op:
360 case mm_frecip_op:
361 if (insn.mm_fp1_format.op == mm_frsqrt_op)
362 func = frsqrt_op;
363 else if (insn.mm_fp1_format.op == mm_fsqrt_op)
364 func = fsqrt_op;
365 else
366 func = frecip_op;
367 mips32_insn.fp0_format.opcode = cop1_op;
368 mips32_insn.fp0_format.fmt =
369 sdps_format[insn.mm_fp1_format.fmt];
370 mips32_insn.fp0_format.ft = 0;
371 mips32_insn.fp0_format.fs =
372 insn.mm_fp1_format.fs;
373 mips32_insn.fp0_format.fd =
374 insn.mm_fp1_format.rt;
375 mips32_insn.fp0_format.func = func;
376 break;
377 case mm_mfc1_op:
378 case mm_mtc1_op:
379 case mm_cfc1_op:
380 case mm_ctc1_op:
Steven J. Hill9355e592013-11-07 12:48:29 +0000381 case mm_mfhc1_op:
382 case mm_mthc1_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500383 if (insn.mm_fp1_format.op == mm_mfc1_op)
384 op = mfc_op;
385 else if (insn.mm_fp1_format.op == mm_mtc1_op)
386 op = mtc_op;
387 else if (insn.mm_fp1_format.op == mm_cfc1_op)
388 op = cfc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000389 else if (insn.mm_fp1_format.op == mm_ctc1_op)
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500390 op = ctc_op;
Steven J. Hill9355e592013-11-07 12:48:29 +0000391 else if (insn.mm_fp1_format.op == mm_mfhc1_op)
392 op = mfhc_op;
393 else
394 op = mthc_op;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500395 mips32_insn.fp1_format.opcode = cop1_op;
396 mips32_insn.fp1_format.op = op;
397 mips32_insn.fp1_format.rt =
398 insn.mm_fp1_format.rt;
399 mips32_insn.fp1_format.fs =
400 insn.mm_fp1_format.fs;
401 mips32_insn.fp1_format.fd = 0;
402 mips32_insn.fp1_format.func = 0;
403 break;
404 default:
405 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500406 }
407 break;
408 case mm_32f_74_op: /* c.cond.fmt */
409 mips32_insn.fp0_format.opcode = cop1_op;
410 mips32_insn.fp0_format.fmt =
411 sdps_format[insn.mm_fp4_format.fmt];
412 mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
413 mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
414 mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
415 mips32_insn.fp0_format.func =
416 insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
417 break;
418 default:
419 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500420 }
421 break;
422 default:
423 return SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500424 }
425
426 *insn_ptr = mips32_insn;
427 return 0;
428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/*
431 * Redundant with logic already in kernel/branch.c,
432 * embedded in compute_return_epc. At some point,
433 * a single subroutine should be used across both
434 * modules.
435 */
Paul Burton432c6ba2016-07-08 11:06:19 +0100436int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
437 unsigned long *contpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500439 union mips_instruction insn = (union mips_instruction)dec_insn.insn;
440 unsigned int fcr31;
441 unsigned int bit = 0;
442
443 switch (insn.i_format.opcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 case spec_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500445 switch (insn.r_format.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 case jalr_op:
Paul Burtonab4a92e2016-04-21 14:04:55 +0100447 if (insn.r_format.rd != 0) {
448 regs->regs[insn.r_format.rd] =
449 regs->cp0_epc + dec_insn.pc_inc +
450 dec_insn.next_pc_inc;
451 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500452 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 case jr_op:
Markos Chandras5f9f41c2014-11-25 15:54:14 +0000454 /* For R6, JR already emulated in jalr_op */
Markos Chandras143fefc2015-06-24 09:52:01 +0100455 if (NO_R6EMU && insn.r_format.func == jr_op)
Markos Chandras5f9f41c2014-11-25 15:54:14 +0000456 break;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500457 *contpc = regs->regs[insn.r_format.rs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 1;
459 }
460 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 case bcond_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500462 switch (insn.i_format.rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 case bltzal_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 case bltzall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000465 if (NO_R6EMU && (insn.i_format.rs ||
466 insn.i_format.rt == bltzall_op))
467 break;
468
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500469 regs->regs[31] = regs->cp0_epc +
470 dec_insn.pc_inc +
471 dec_insn.next_pc_inc;
472 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500473 case bltzl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000474 if (NO_R6EMU)
475 break;
476 case bltz_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500477 if ((long)regs->regs[insn.i_format.rs] < 0)
478 *contpc = regs->cp0_epc +
479 dec_insn.pc_inc +
480 (insn.i_format.simmediate << 2);
481 else
482 *contpc = regs->cp0_epc +
483 dec_insn.pc_inc +
484 dec_insn.next_pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500486 case bgezal_op:
487 case bgezall_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000488 if (NO_R6EMU && (insn.i_format.rs ||
489 insn.i_format.rt == bgezall_op))
490 break;
491
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500492 regs->regs[31] = regs->cp0_epc +
493 dec_insn.pc_inc +
494 dec_insn.next_pc_inc;
495 /* Fall through */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500496 case bgezl_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000497 if (NO_R6EMU)
498 break;
499 case bgez_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500500 if ((long)regs->regs[insn.i_format.rs] >= 0)
501 *contpc = regs->cp0_epc +
502 dec_insn.pc_inc +
503 (insn.i_format.simmediate << 2);
504 else
505 *contpc = regs->cp0_epc +
506 dec_insn.pc_inc +
507 dec_insn.next_pc_inc;
508 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 case jalx_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500512 set_isa16_mode(bit);
513 case jal_op:
514 regs->regs[31] = regs->cp0_epc +
515 dec_insn.pc_inc +
516 dec_insn.next_pc_inc;
517 /* Fall through */
518 case j_op:
519 *contpc = regs->cp0_epc + dec_insn.pc_inc;
520 *contpc >>= 28;
521 *contpc <<= 28;
522 *contpc |= (insn.j_format.target << 2);
523 /* Set microMIPS mode bit: XOR for jalx. */
524 *contpc ^= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500526 case beql_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000527 if (NO_R6EMU)
528 break;
529 case beq_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500530 if (regs->regs[insn.i_format.rs] ==
531 regs->regs[insn.i_format.rt])
532 *contpc = regs->cp0_epc +
533 dec_insn.pc_inc +
534 (insn.i_format.simmediate << 2);
535 else
536 *contpc = regs->cp0_epc +
537 dec_insn.pc_inc +
538 dec_insn.next_pc_inc;
539 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500540 case bnel_op:
Markos Chandras319824e2014-11-25 16:02:23 +0000541 if (NO_R6EMU)
542 break;
543 case bne_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500544 if (regs->regs[insn.i_format.rs] !=
545 regs->regs[insn.i_format.rt])
546 *contpc = regs->cp0_epc +
547 dec_insn.pc_inc +
548 (insn.i_format.simmediate << 2);
549 else
550 *contpc = regs->cp0_epc +
551 dec_insn.pc_inc +
552 dec_insn.next_pc_inc;
553 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500554 case blezl_op:
Markos Chandrase9d92d22015-06-24 09:52:00 +0100555 if (!insn.i_format.rt && NO_R6EMU)
Markos Chandras319824e2014-11-25 16:02:23 +0000556 break;
557 case blez_op:
Markos Chandrasa8ff66f2014-11-26 12:57:54 +0000558
559 /*
560 * Compact branches for R6 for the
561 * blez and blezl opcodes.
562 * BLEZ | rs = 0 | rt != 0 == BLEZALC
563 * BLEZ | rs = rt != 0 == BGEZALC
564 * BLEZ | rs != 0 | rt != 0 == BGEUC
565 * BLEZL | rs = 0 | rt != 0 == BLEZC
566 * BLEZL | rs = rt != 0 == BGEZC
567 * BLEZL | rs != 0 | rt != 0 == BGEC
568 *
569 * For real BLEZ{,L}, rt is always 0.
570 */
571 if (cpu_has_mips_r6 && insn.i_format.rt) {
572 if ((insn.i_format.opcode == blez_op) &&
573 ((!insn.i_format.rs && insn.i_format.rt) ||
574 (insn.i_format.rs == insn.i_format.rt)))
575 regs->regs[31] = regs->cp0_epc +
576 dec_insn.pc_inc;
577 *contpc = regs->cp0_epc + dec_insn.pc_inc +
578 dec_insn.next_pc_inc;
579
580 return 1;
581 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500582 if ((long)regs->regs[insn.i_format.rs] <= 0)
583 *contpc = regs->cp0_epc +
584 dec_insn.pc_inc +
585 (insn.i_format.simmediate << 2);
586 else
587 *contpc = regs->cp0_epc +
588 dec_insn.pc_inc +
589 dec_insn.next_pc_inc;
590 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500591 case bgtzl_op:
Markos Chandrase9d92d22015-06-24 09:52:00 +0100592 if (!insn.i_format.rt && NO_R6EMU)
Markos Chandras319824e2014-11-25 16:02:23 +0000593 break;
594 case bgtz_op:
Markos Chandrasf1b44062014-11-26 13:05:09 +0000595 /*
596 * Compact branches for R6 for the
597 * bgtz and bgtzl opcodes.
598 * BGTZ | rs = 0 | rt != 0 == BGTZALC
599 * BGTZ | rs = rt != 0 == BLTZALC
600 * BGTZ | rs != 0 | rt != 0 == BLTUC
601 * BGTZL | rs = 0 | rt != 0 == BGTZC
602 * BGTZL | rs = rt != 0 == BLTZC
603 * BGTZL | rs != 0 | rt != 0 == BLTC
604 *
605 * *ZALC varint for BGTZ &&& rt != 0
606 * For real GTZ{,L}, rt is always 0.
607 */
608 if (cpu_has_mips_r6 && insn.i_format.rt) {
609 if ((insn.i_format.opcode == blez_op) &&
610 ((!insn.i_format.rs && insn.i_format.rt) ||
611 (insn.i_format.rs == insn.i_format.rt)))
612 regs->regs[31] = regs->cp0_epc +
613 dec_insn.pc_inc;
614 *contpc = regs->cp0_epc + dec_insn.pc_inc +
615 dec_insn.next_pc_inc;
616
617 return 1;
618 }
619
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500620 if ((long)regs->regs[insn.i_format.rs] > 0)
621 *contpc = regs->cp0_epc +
622 dec_insn.pc_inc +
623 (insn.i_format.simmediate << 2);
624 else
625 *contpc = regs->cp0_epc +
626 dec_insn.pc_inc +
627 dec_insn.next_pc_inc;
628 return 1;
Paul Burton1b492602016-07-04 19:35:08 +0100629 case pop10_op:
630 case pop30_op:
Markos Chandrasc893ce32014-11-26 14:08:52 +0000631 if (!cpu_has_mips_r6)
632 break;
633 if (insn.i_format.rt && !insn.i_format.rs)
634 regs->regs[31] = regs->cp0_epc + 4;
635 *contpc = regs->cp0_epc + dec_insn.pc_inc +
636 dec_insn.next_pc_inc;
637
638 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700639#ifdef CONFIG_CPU_CAVIUM_OCTEON
640 case lwc2_op: /* This is bbit0 on Octeon */
641 if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
642 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
643 else
644 *contpc = regs->cp0_epc + 8;
645 return 1;
646 case ldc2_op: /* This is bbit032 on Octeon */
647 if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
648 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
649 else
650 *contpc = regs->cp0_epc + 8;
651 return 1;
652 case swc2_op: /* This is bbit1 on Octeon */
653 if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
654 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
655 else
656 *contpc = regs->cp0_epc + 8;
657 return 1;
658 case sdc2_op: /* This is bbit132 on Octeon */
659 if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
660 *contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
661 else
662 *contpc = regs->cp0_epc + 8;
663 return 1;
Markos Chandras8467ca02014-11-26 13:56:51 +0000664#else
665 case bc6_op:
666 /*
667 * Only valid for MIPS R6 but we can still end up
668 * here from a broken userland so just tell emulator
669 * this is not a branch and let it break later on.
670 */
671 if (!cpu_has_mips_r6)
672 break;
673 *contpc = regs->cp0_epc + dec_insn.pc_inc +
674 dec_insn.next_pc_inc;
675
676 return 1;
Markos Chandras84fef632014-11-26 15:43:11 +0000677 case balc6_op:
678 if (!cpu_has_mips_r6)
679 break;
680 regs->regs[31] = regs->cp0_epc + 4;
681 *contpc = regs->cp0_epc + dec_insn.pc_inc +
682 dec_insn.next_pc_inc;
683
684 return 1;
Paul Burton1c66b792016-07-04 19:35:07 +0100685 case pop66_op:
Markos Chandras69b9a2f2014-11-27 09:32:25 +0000686 if (!cpu_has_mips_r6)
687 break;
688 *contpc = regs->cp0_epc + dec_insn.pc_inc +
689 dec_insn.next_pc_inc;
690
691 return 1;
Paul Burton1c66b792016-07-04 19:35:07 +0100692 case pop76_op:
Markos Chandras28d6f932015-01-08 11:55:20 +0000693 if (!cpu_has_mips_r6)
694 break;
695 if (!insn.i_format.rs)
696 regs->regs[31] = regs->cp0_epc + 4;
697 *contpc = regs->cp0_epc + dec_insn.pc_inc +
698 dec_insn.next_pc_inc;
699
700 return 1;
David Daneyc26d4212013-08-19 12:10:34 -0700701#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 case cop0_op:
703 case cop1_op:
Markos Chandrasc8a34582014-11-26 10:10:18 +0000704 /* Need to check for R6 bc1nez and bc1eqz branches */
705 if (cpu_has_mips_r6 &&
706 ((insn.i_format.rs == bc1eqz_op) ||
707 (insn.i_format.rs == bc1nez_op))) {
708 bit = 0;
709 switch (insn.i_format.rs) {
710 case bc1eqz_op:
711 if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
712 bit = 1;
713 break;
714 case bc1nez_op:
715 if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
716 bit = 1;
717 break;
718 }
719 if (bit)
720 *contpc = regs->cp0_epc +
721 dec_insn.pc_inc +
722 (insn.i_format.simmediate << 2);
723 else
724 *contpc = regs->cp0_epc +
725 dec_insn.pc_inc +
726 dec_insn.next_pc_inc;
727
728 return 1;
729 }
730 /* R2/R6 compatible cop1 instruction. Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 case cop2_op:
732 case cop1x_op:
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500733 if (insn.i_format.rs == bc_op) {
734 preempt_disable();
735 if (is_fpu_owner())
Manuel Lauss842dfc12014-11-07 14:13:54 +0100736 fcr31 = read_32bit_cp1_register(CP1_STATUS);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500737 else
738 fcr31 = current->thread.fpu.fcr31;
739 preempt_enable();
740
741 bit = (insn.i_format.rt >> 2);
742 bit += (bit != 0);
743 bit += 23;
744 switch (insn.i_format.rt & 3) {
745 case 0: /* bc1f */
746 case 2: /* bc1fl */
747 if (~fcr31 & (1 << bit))
748 *contpc = regs->cp0_epc +
749 dec_insn.pc_inc +
750 (insn.i_format.simmediate << 2);
751 else
752 *contpc = regs->cp0_epc +
753 dec_insn.pc_inc +
754 dec_insn.next_pc_inc;
755 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500756 case 1: /* bc1t */
757 case 3: /* bc1tl */
758 if (fcr31 & (1 << bit))
759 *contpc = regs->cp0_epc +
760 dec_insn.pc_inc +
761 (insn.i_format.simmediate << 2);
762 else
763 *contpc = regs->cp0_epc +
764 dec_insn.pc_inc +
765 dec_insn.next_pc_inc;
766 return 1;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500767 }
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 break;
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return 0;
772}
773
774/*
775 * In the Linux kernel, we support selection of FPR format on the
Ralf Baechle70342282013-01-22 12:59:30 +0100776 * basis of the Status.FR bit. If an FPU is not present, the FR bit
David Daneyda0bac32009-11-02 11:33:46 -0800777 * is hardwired to zero, which would imply a 32-bit FPU even for
Paul Burton597ce172013-11-22 13:12:07 +0000778 * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
Ralf Baechle51d943f2012-08-15 19:42:19 +0200779 * FPU emu is slow and bulky and optimizing this function offers fairly
780 * sizeable benefits so we try to be clever and make this function return
781 * a constant whenever possible, that is on 64-bit kernels without O32
Paul Burton597ce172013-11-22 13:12:07 +0000782 * compatibility enabled and on 32-bit without 64-bit FPU support.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
David Daneyda0bac32009-11-02 11:33:46 -0800784static inline int cop1_64bit(struct pt_regs *xcp)
785{
Masahiro Yamada97f26452016-08-03 13:45:50 -0700786 if (IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_MIPS32_O32))
Ralf Baechle08a07902014-04-19 13:11:37 +0200787 return 1;
Masahiro Yamada97f26452016-08-03 13:45:50 -0700788 else if (IS_ENABLED(CONFIG_32BIT) &&
789 !IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
Ralf Baechle08a07902014-04-19 13:11:37 +0200790 return 0;
791
Paul Burton597ce172013-11-22 13:12:07 +0000792 return !test_thread_flag(TIF_32BIT_FPREGS);
David Daneyda0bac32009-11-02 11:33:46 -0800793}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Paul Burton4227a2d2014-09-11 08:30:20 +0100795static inline bool hybrid_fprs(void)
796{
797 return test_thread_flag(TIF_HYBRID_FPREGS);
798}
799
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200800#define SIFROMREG(si, x) \
801do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100802 if (cop1_64bit(xcp) && !hybrid_fprs()) \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100803 (si) = (int)get_fpr32(&ctx->fpr[x], 0); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000804 else \
Paul Burtonc8c0da62014-09-24 10:45:37 +0100805 (si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1); \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000806} while (0)
David Daneyda0bac32009-11-02 11:33:46 -0800807
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200808#define SITOREG(si, x) \
809do { \
Paul Burton4227a2d2014-09-11 08:30:20 +0100810 if (cop1_64bit(xcp) && !hybrid_fprs()) { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000811 unsigned i; \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000812 set_fpr32(&ctx->fpr[x], 0, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000813 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
814 set_fpr32(&ctx->fpr[x], i, 0); \
815 } else { \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000816 set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si); \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000817 } \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000818} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Paul Burtonc8c0da62014-09-24 10:45:37 +0100820#define SIFROMHREG(si, x) ((si) = (int)get_fpr32(&ctx->fpr[x], 1))
Paul Burtonef1c47a2014-01-27 17:14:47 +0000821
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200822#define SITOHREG(si, x) \
823do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000824 unsigned i; \
825 set_fpr32(&ctx->fpr[x], 1, si); \
826 for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++) \
827 set_fpr32(&ctx->fpr[x], i, 0); \
828} while (0)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +0000829
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200830#define DIFROMREG(di, x) \
Paul Burtonbbd426f2014-02-13 11:26:41 +0000831 ((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
832
Ralf Baechle47fa0c02014-04-16 11:00:12 +0200833#define DITOREG(di, x) \
834do { \
Paul Burtonef1c47a2014-01-27 17:14:47 +0000835 unsigned fpr, i; \
836 fpr = (x) & ~(cop1_64bit(xcp) == 0); \
837 set_fpr64(&ctx->fpr[fpr], 0, di); \
838 for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++) \
839 set_fpr64(&ctx->fpr[fpr], i, 0); \
840} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Ralf Baechle21a151d2007-10-11 23:46:15 +0100842#define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
843#define SPTOREG(sp, x) SITOREG((sp).bits, x)
844#define DPFROMREG(dp, x) DIFROMREG((dp).bits, x)
845#define DPTOREG(dp, x) DITOREG((dp).bits, x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847/*
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100848 * Emulate a CFC1 instruction.
849 */
850static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
851 mips_instruction ir)
852{
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100853 u32 fcr31 = ctx->fcr31;
854 u32 value = 0;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100855
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100856 switch (MIPSInst_RD(ir)) {
857 case FPCREG_CSR:
858 value = fcr31;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100859 pr_debug("%p gpr[%d]<-csr=%08x\n",
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100860 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
861 break;
862
863 case FPCREG_FENR:
864 if (!cpu_has_mips_r)
865 break;
866 value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
867 MIPS_FENR_FS;
868 value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
869 pr_debug("%p gpr[%d]<-enr=%08x\n",
870 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
871 break;
872
873 case FPCREG_FEXR:
874 if (!cpu_has_mips_r)
875 break;
876 value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
877 pr_debug("%p gpr[%d]<-exr=%08x\n",
878 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
879 break;
880
881 case FPCREG_FCCR:
882 if (!cpu_has_mips_r)
883 break;
884 value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
885 MIPS_FCCR_COND0;
886 value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
887 (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
888 pr_debug("%p gpr[%d]<-ccr=%08x\n",
889 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
890 break;
891
892 case FPCREG_RID:
Maciej W. Rozycki03dce592015-05-12 15:20:57 +0100893 value = boot_cpu_data.fpu_id;
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100894 break;
895
896 default:
897 break;
898 }
899
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100900 if (MIPSInst_RT(ir))
901 xcp->regs[MIPSInst_RT(ir)] = value;
902}
903
904/*
905 * Emulate a CTC1 instruction.
906 */
907static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
908 mips_instruction ir)
909{
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100910 u32 fcr31 = ctx->fcr31;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100911 u32 value;
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100912 u32 mask;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100913
914 if (MIPSInst_RT(ir) == 0)
915 value = 0;
916 else
917 value = xcp->regs[MIPSInst_RT(ir)];
918
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100919 switch (MIPSInst_RD(ir)) {
920 case FPCREG_CSR:
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100921 pr_debug("%p gpr[%d]->csr=%08x\n",
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100922 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100923
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100924 /* Preserve read-only bits. */
Maciej W. Rozycki03dce592015-05-12 15:20:57 +0100925 mask = boot_cpu_data.fpu_msk31;
Maciej W. Rozycki9b266162015-04-03 23:27:48 +0100926 fcr31 = (value & ~mask) | (fcr31 & mask);
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100927 break;
928
929 case FPCREG_FENR:
930 if (!cpu_has_mips_r)
931 break;
932 pr_debug("%p gpr[%d]->enr=%08x\n",
933 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
934 fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
935 fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
936 FPU_CSR_FS;
937 fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
938 break;
939
940 case FPCREG_FEXR:
941 if (!cpu_has_mips_r)
942 break;
943 pr_debug("%p gpr[%d]->exr=%08x\n",
944 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
945 fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
946 fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
947 break;
948
949 case FPCREG_FCCR:
950 if (!cpu_has_mips_r)
951 break;
952 pr_debug("%p gpr[%d]->ccr=%08x\n",
953 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
954 fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
955 fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
956 FPU_CSR_COND;
957 fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
958 FPU_CSR_CONDX;
959 break;
960
961 default:
962 break;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100963 }
Maciej W. Rozyckic491cfa2015-04-03 23:27:33 +0100964
965 ctx->fcr31 = fcr31;
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +0100966}
967
968/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * Emulate the single floating point instruction pointed at by EPC.
970 * Two instructions if the instruction is in a branch delay slot.
971 */
972
David Daney515b0292010-10-21 16:32:26 -0700973static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500974 struct mm_decoded_insn dec_insn, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500976 unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
Paul Burton93583e12016-04-21 14:04:45 +0100977 unsigned int cond, cbit, bit0;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200978 mips_instruction ir;
979 int likely, pc_inc;
Paul Burton93583e12016-04-21 14:04:45 +0100980 union fpureg *fpr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200981 u32 __user *wva;
982 u64 __user *dva;
Ralf Baechle3f7cac42014-04-26 01:49:14 +0200983 u32 wval;
984 u64 dval;
985 int sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Ralf Baechle70e4c232014-04-30 11:09:44 +0200987 /*
988 * These are giving gcc a gentle hint about what to expect in
989 * dec_inst in order to do better optimization.
990 */
991 if (!cpu_has_mmips && dec_insn.micro_mips_mode)
992 unreachable();
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* XXX NEC Vr54xx bug workaround */
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200995 if (delay_slot(xcp)) {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500996 if (dec_insn.micro_mips_mode) {
997 if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +0200998 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -0500999 } else {
1000 if (!isBranchInstr(xcp, dec_insn, &contpc))
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001001 clear_delay_slot(xcp);
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001002 }
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001005 if (delay_slot(xcp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /*
1007 * The instruction to be emulated is in a branch delay slot
Ralf Baechle70342282013-01-22 12:59:30 +01001008 * which means that we have to emulate the branch instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 * BEFORE we do the cop1 instruction.
1010 *
1011 * This branch could be a COP1 branch, but in that case we
1012 * would have had a trap for that instruction, and would not
1013 * come through this route.
1014 *
1015 * Linux MIPS branch emulator operates on context, updating the
1016 * cp0_epc.
1017 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001018 ir = dec_insn.next_insn; /* process delay slot instr */
1019 pc_inc = dec_insn.next_pc_inc;
Ralf Baechle333d1f62005-02-28 17:55:57 +00001020 } else {
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001021 ir = dec_insn.insn; /* process current instr */
1022 pc_inc = dec_insn.pc_inc;
1023 }
1024
1025 /*
1026 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
1027 * instructions, we want to convert microMIPS FPU instructions
1028 * into MIPS32 instructions so that we could reuse all of the
1029 * FPU emulation code.
1030 *
1031 * NOTE: We cannot do this for branch instructions since they
1032 * are not a subset. Example: Cannot emulate a 16-bit
1033 * aligned target address with a MIPS32 instruction.
1034 */
1035 if (dec_insn.micro_mips_mode) {
1036 /*
1037 * If next instruction is a 16-bit instruction, then it
1038 * it cannot be a FPU instruction. This could happen
1039 * since we can be called for non-FPU instructions.
1040 */
1041 if ((pc_inc == 2) ||
1042 (microMIPS32_to_MIPS32((union mips_instruction *)&ir)
1043 == SIGILL))
1044 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001047emul:
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001048 perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
David Daneyb6ee75e2009-11-05 11:34:26 -08001049 MIPS_FPU_EMU_INC_STATS(emulated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 switch (MIPSInst_OPCODE(ir)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001051 case ldc1_op:
1052 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1053 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001054 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001055
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001056 if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001057 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001058 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return SIGBUS;
1060 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001061 if (__get_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -07001062 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001063 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -07001064 return SIGSEGV;
1065 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001066 DITOREG(dval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001069 case sdc1_op:
1070 dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1071 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001072 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001073 DIFROMREG(dval, MIPSInst_RT(ir));
1074 if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001075 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001076 *fault_addr = dva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return SIGBUS;
1078 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001079 if (__put_user(dval, dva)) {
David Daney515b0292010-10-21 16:32:26 -07001080 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001081 *fault_addr = dva;
David Daney515b0292010-10-21 16:32:26 -07001082 return SIGSEGV;
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001086 case lwc1_op:
1087 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1088 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001089 MIPS_FPU_EMU_INC_STATS(loads);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001090 if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001091 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001092 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return SIGBUS;
1094 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001095 if (__get_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -07001096 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001097 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -07001098 return SIGSEGV;
1099 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001100 SITOREG(wval, MIPSInst_RT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001103 case swc1_op:
1104 wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
1105 MIPSInst_SIMM(ir));
David Daneyb6ee75e2009-11-05 11:34:26 -08001106 MIPS_FPU_EMU_INC_STATS(stores);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001107 SIFROMREG(wval, MIPSInst_RT(ir));
1108 if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001109 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001110 *fault_addr = wva;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return SIGBUS;
1112 }
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001113 if (__put_user(wval, wva)) {
David Daney515b0292010-10-21 16:32:26 -07001114 MIPS_FPU_EMU_INC_STATS(errors);
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001115 *fault_addr = wva;
David Daney515b0292010-10-21 16:32:26 -07001116 return SIGSEGV;
1117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 case cop1_op:
1121 switch (MIPSInst_RS(ir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 case dmfc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001123 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1124 return SIGILL;
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* copregister fs -> gpr[rt] */
1127 if (MIPSInst_RT(ir) != 0) {
1128 DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1129 MIPSInst_RD(ir));
1130 }
1131 break;
1132
1133 case dmtc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001134 if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
1135 return SIGILL;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /* copregister fs <- rt */
1138 DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001141 case mfhc_op:
Markos Chandrase8f80cc2015-07-17 10:36:03 +01001142 if (!cpu_has_mips_r2_r6)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001143 goto sigill;
1144
1145 /* copregister rd -> gpr[rt] */
1146 if (MIPSInst_RT(ir) != 0) {
1147 SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
1148 MIPSInst_RD(ir));
1149 }
1150 break;
1151
1152 case mthc_op:
Markos Chandrase8f80cc2015-07-17 10:36:03 +01001153 if (!cpu_has_mips_r2_r6)
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001154 goto sigill;
1155
1156 /* copregister rd <- gpr[rt] */
1157 SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1158 break;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 case mfc_op:
1161 /* copregister rd -> gpr[rt] */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (MIPSInst_RT(ir) != 0) {
1163 SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
1164 MIPSInst_RD(ir));
1165 }
1166 break;
1167
1168 case mtc_op:
1169 /* copregister rd <- rt */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
1171 break;
1172
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001173 case cfc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /* cop control register rd -> gpr[rt] */
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +01001175 cop1_cfc(xcp, ctx, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001178 case ctc_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /* copregister rd <- rt */
Maciej W. Rozyckid4f5b082015-04-03 23:25:04 +01001180 cop1_ctc(xcp, ctx, ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
1182 return SIGFPE;
1183 }
1184 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Markos Chandrasc909ca72015-07-17 10:38:32 +01001186 case bc1eqz_op:
1187 case bc1nez_op:
1188 if (!cpu_has_mips_r6 || delay_slot(xcp))
1189 return SIGILL;
1190
1191 cond = likely = 0;
Paul Burton93583e12016-04-21 14:04:45 +01001192 fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
1193 bit0 = get_fpr32(fpr, 0) & 0x1;
Markos Chandrasc909ca72015-07-17 10:38:32 +01001194 switch (MIPSInst_RS(ir)) {
1195 case bc1eqz_op:
Paul Burton93583e12016-04-21 14:04:45 +01001196 cond = bit0 == 0;
Markos Chandrasc909ca72015-07-17 10:38:32 +01001197 break;
1198 case bc1nez_op:
Paul Burton93583e12016-04-21 14:04:45 +01001199 cond = bit0 != 0;
Markos Chandrasc909ca72015-07-17 10:38:32 +01001200 break;
1201 }
1202 goto branch_common;
1203
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001204 case bc_op:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001205 if (delay_slot(xcp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return SIGILL;
1207
Ralf Baechle08a07902014-04-19 13:11:37 +02001208 if (cpu_has_mips_4_5_r)
1209 cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
1210 else
1211 cbit = FPU_CSR_COND;
1212 cond = ctx->fcr31 & cbit;
1213
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001214 likely = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 switch (MIPSInst_RT(ir) & 3) {
1216 case bcfl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001217 if (cpu_has_mips_2_3_4_5_r)
1218 likely = 1;
1219 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 case bcf_op:
1221 cond = !cond;
1222 break;
1223 case bctl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001224 if (cpu_has_mips_2_3_4_5_r)
1225 likely = 1;
1226 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 case bct_op:
1228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
Markos Chandrasc909ca72015-07-17 10:38:32 +01001230branch_common:
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001231 set_delay_slot(xcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (cond) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001233 /*
1234 * Branch taken: emulate dslot instruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001236 unsigned long bcpc;
1237
1238 /*
1239 * Remember EPC at the branch to point back
1240 * at so that any delay-slot instruction
1241 * signal is not silently ignored.
1242 */
1243 bcpc = xcp->cp0_epc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001244 xcp->cp0_epc += dec_insn.pc_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001246 contpc = MIPSInst_SIMM(ir);
1247 ir = dec_insn.next_insn;
1248 if (dec_insn.micro_mips_mode) {
1249 contpc = (xcp->cp0_epc + (contpc << 1));
1250
1251 /* If 16-bit instruction, not FPU. */
1252 if ((dec_insn.next_pc_inc == 2) ||
1253 (microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1254
1255 /*
1256 * Since this instruction will
1257 * be put on the stack with
1258 * 32-bit words, get around
1259 * this problem by putting a
1260 * NOP16 as the second one.
1261 */
1262 if (dec_insn.next_pc_inc == 2)
1263 ir = (ir & (~0xffff)) | MM_NOP16;
1264
1265 /*
1266 * Single step the non-CP1
1267 * instruction in the dslot.
1268 */
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001269 sig = mips_dsemul(xcp, ir,
Paul Burton432c6ba2016-07-08 11:06:19 +01001270 bcpc, contpc);
Maciej W. Rozyckie4553572016-01-22 05:20:26 +00001271 if (sig < 0)
1272 break;
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001273 if (sig)
1274 xcp->cp0_epc = bcpc;
1275 /*
1276 * SIGILL forces out of
1277 * the emulation loop.
1278 */
1279 return sig ? sig : SIGILL;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05001280 }
1281 } else
1282 contpc = (xcp->cp0_epc + (contpc << 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 switch (MIPSInst_OPCODE(ir)) {
1285 case lwc1_op:
1286 case swc1_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001287 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 case ldc1_op:
1290 case sdc1_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001291 if (cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001292 goto emul;
1293
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001294 goto bc_sigill;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001295
Ralf Baechle08a07902014-04-19 13:11:37 +02001296 case cop1_op:
1297 goto emul;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001298
Ralf Baechle08a07902014-04-19 13:11:37 +02001299 case cop1x_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001300 if (cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001301 /* its one of ours */
1302 goto emul;
1303
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001304 goto bc_sigill;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 case spec_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001307 switch (MIPSInst_FUNC(ir)) {
1308 case movc_op:
1309 if (cpu_has_mips_4_5_r)
1310 goto emul;
Ralf Baechle08a07902014-04-19 13:11:37 +02001311
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001312 goto bc_sigill;
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 break;
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001315
1316 bc_sigill:
1317 xcp->cp0_epc = bcpc;
1318 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
1321 /*
1322 * Single step the non-cp1
1323 * instruction in the dslot
1324 */
Paul Burton432c6ba2016-07-08 11:06:19 +01001325 sig = mips_dsemul(xcp, ir, bcpc, contpc);
Maciej W. Rozyckie4553572016-01-22 05:20:26 +00001326 if (sig < 0)
1327 break;
Maciej W. Rozycki9ab44712015-04-03 23:26:56 +01001328 if (sig)
1329 xcp->cp0_epc = bcpc;
1330 /* SIGILL forces out of the emulation loop. */
1331 return sig ? sig : SIGILL;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001332 } else if (likely) { /* branch not taken */
Maciej W. Rozycki5d77cf22015-04-03 23:24:24 +01001333 /*
1334 * branch likely nullifies
1335 * dslot if not taken
1336 */
1337 xcp->cp0_epc += dec_insn.pc_inc;
1338 contpc += dec_insn.pc_inc;
1339 /*
1340 * else continue & execute
1341 * dslot as normal insn
1342 */
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 default:
1347 if (!(MIPSInst_RS(ir) & 0x10))
1348 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001350 /* a real fpu computation instruction */
1351 if ((sig = fpu_emu(xcp, ctx, ir)))
1352 return sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354 break;
1355
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001356 case cop1x_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001357 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001358 return SIGILL;
1359
1360 sig = fpux_emu(xcp, ctx, ir, fault_addr);
David Daney515b0292010-10-21 16:32:26 -07001361 if (sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return sig;
1363 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 case spec_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001366 if (!cpu_has_mips_4_5_r)
1367 return SIGILL;
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (MIPSInst_FUNC(ir) != movc_op)
1370 return SIGILL;
1371 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
1372 if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
1373 xcp->regs[MIPSInst_RD(ir)] =
1374 xcp->regs[MIPSInst_RS(ir)];
1375 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 default:
Leonid Yegoshin1ac944002013-11-07 12:48:28 +00001377sigill:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return SIGILL;
1379 }
1380
1381 /* we did it !! */
Atsushi Nemotoe70dfc12007-07-13 23:02:29 +09001382 xcp->cp0_epc = contpc;
Ralf Baechlee7e9cae2014-04-16 01:59:03 +02001383 clear_delay_slot(xcp);
Ralf Baechle333d1f62005-02-28 17:55:57 +00001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 0;
1386}
1387
1388/*
1389 * Conversion table from MIPS compare ops 48-63
1390 * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
1391 */
1392static const unsigned char cmptab[8] = {
1393 0, /* cmp_0 (sig) cmp_sf */
1394 IEEE754_CUN, /* cmp_un (sig) cmp_ngle */
1395 IEEE754_CEQ, /* cmp_eq (sig) cmp_seq */
1396 IEEE754_CEQ | IEEE754_CUN, /* cmp_ueq (sig) cmp_ngl */
1397 IEEE754_CLT, /* cmp_olt (sig) cmp_lt */
1398 IEEE754_CLT | IEEE754_CUN, /* cmp_ult (sig) cmp_nge */
1399 IEEE754_CLT | IEEE754_CEQ, /* cmp_ole (sig) cmp_le */
1400 IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN, /* cmp_ule (sig) cmp_ngt */
1401};
1402
Markos Chandrasf8c3c672015-08-13 09:56:28 +02001403static const unsigned char negative_cmptab[8] = {
1404 0, /* Reserved */
1405 IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
1406 IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
1407 IEEE754_CLT | IEEE754_CGT,
1408 /* Reserved */
1409};
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412/*
1413 * Additional MIPS4 instructions
1414 */
1415
Ralf Baechle47fa0c02014-04-16 11:00:12 +02001416#define DEF3OP(name, p, f1, f2, f3) \
1417static union ieee754##p fpemu_##p##_##name(union ieee754##p r, \
1418 union ieee754##p s, union ieee754##p t) \
1419{ \
1420 struct _ieee754_csr ieee754_csr_save; \
1421 s = f1(s, t); \
1422 ieee754_csr_save = ieee754_csr; \
1423 s = f2(s, r); \
1424 ieee754_csr_save.cx |= ieee754_csr.cx; \
1425 ieee754_csr_save.sx |= ieee754_csr.sx; \
1426 s = f3(s); \
1427 ieee754_csr.cx |= ieee754_csr_save.cx; \
1428 ieee754_csr.sx |= ieee754_csr_save.sx; \
1429 return s; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001432static union ieee754dp fpemu_dp_recip(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 return ieee754dp_div(ieee754dp_one(0), d);
1435}
1436
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001437static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
1440}
1441
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001442static union ieee754sp fpemu_sp_recip(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 return ieee754sp_div(ieee754sp_one(0), s);
1445}
1446
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001447static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
1450}
1451
Ralf Baechle21a151d2007-10-11 23:46:15 +01001452DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
1453DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
1455DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
Ralf Baechle21a151d2007-10-11 23:46:15 +01001456DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
1457DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
1459DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
1460
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001461static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07001462 mips_instruction ir, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 unsigned rcsr = 0; /* resulting csr */
1465
David Daneyb6ee75e2009-11-05 11:34:26 -08001466 MIPS_FPU_EMU_INC_STATS(cp1xops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 switch (MIPSInst_FMA_FFMT(ir)) {
1469 case s_fmt:{ /* 0 */
1470
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001471 union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
1472 union ieee754sp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001473 u32 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 u32 val;
1475
1476 switch (MIPSInst_FUNC(ir)) {
1477 case lwxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001478 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 xcp->regs[MIPSInst_FT(ir)]);
1480
David Daneyb6ee75e2009-11-05 11:34:26 -08001481 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001482 if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001483 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001484 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return SIGBUS;
1486 }
David Daney515b0292010-10-21 16:32:26 -07001487 if (__get_user(val, va)) {
1488 MIPS_FPU_EMU_INC_STATS(errors);
1489 *fault_addr = va;
1490 return SIGSEGV;
1491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 SITOREG(val, MIPSInst_FD(ir));
1493 break;
1494
1495 case swxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001496 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 xcp->regs[MIPSInst_FT(ir)]);
1498
David Daneyb6ee75e2009-11-05 11:34:26 -08001499 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 SIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001502 if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1503 MIPS_FPU_EMU_INC_STATS(errors);
1504 *fault_addr = va;
1505 return SIGBUS;
1506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (put_user(val, va)) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001508 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001509 *fault_addr = va;
1510 return SIGSEGV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 break;
1513
1514 case madd_s_op:
1515 handler = fpemu_sp_madd;
1516 goto scoptop;
1517 case msub_s_op:
1518 handler = fpemu_sp_msub;
1519 goto scoptop;
1520 case nmadd_s_op:
1521 handler = fpemu_sp_nmadd;
1522 goto scoptop;
1523 case nmsub_s_op:
1524 handler = fpemu_sp_nmsub;
1525 goto scoptop;
1526
1527 scoptop:
1528 SPFROMREG(fr, MIPSInst_FR(ir));
1529 SPFROMREG(fs, MIPSInst_FS(ir));
1530 SPFROMREG(ft, MIPSInst_FT(ir));
1531 fd = (*handler) (fr, fs, ft);
1532 SPTOREG(fd, MIPSInst_FD(ir));
1533
1534 copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001535 if (ieee754_cxtest(IEEE754_INEXACT)) {
1536 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001538 }
1539 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1540 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001542 }
1543 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1544 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001546 }
1547 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1548 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001554 /*printk ("SIGFPE: FPU csr = %08x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 ctx->fcr31); */
1556 return SIGFPE;
1557 }
1558
1559 break;
1560
1561 default:
1562 return SIGILL;
1563 }
1564 break;
1565 }
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 case d_fmt:{ /* 1 */
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001568 union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
1569 union ieee754dp fd, fr, fs, ft;
Ralf Baechle3fccc012005-10-23 13:58:21 +01001570 u64 __user *va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 u64 val;
1572
1573 switch (MIPSInst_FUNC(ir)) {
1574 case ldxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001575 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 xcp->regs[MIPSInst_FT(ir)]);
1577
David Daneyb6ee75e2009-11-05 11:34:26 -08001578 MIPS_FPU_EMU_INC_STATS(loads);
David Daney515b0292010-10-21 16:32:26 -07001579 if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001580 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001581 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return SIGBUS;
1583 }
David Daney515b0292010-10-21 16:32:26 -07001584 if (__get_user(val, va)) {
1585 MIPS_FPU_EMU_INC_STATS(errors);
1586 *fault_addr = va;
1587 return SIGSEGV;
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 DITOREG(val, MIPSInst_FD(ir));
1590 break;
1591
1592 case sdxc1_op:
Ralf Baechle3fccc012005-10-23 13:58:21 +01001593 va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 xcp->regs[MIPSInst_FT(ir)]);
1595
David Daneyb6ee75e2009-11-05 11:34:26 -08001596 MIPS_FPU_EMU_INC_STATS(stores);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 DIFROMREG(val, MIPSInst_FS(ir));
David Daney515b0292010-10-21 16:32:26 -07001598 if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
David Daneyb6ee75e2009-11-05 11:34:26 -08001599 MIPS_FPU_EMU_INC_STATS(errors);
David Daney515b0292010-10-21 16:32:26 -07001600 *fault_addr = va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return SIGBUS;
1602 }
David Daney515b0292010-10-21 16:32:26 -07001603 if (__put_user(val, va)) {
1604 MIPS_FPU_EMU_INC_STATS(errors);
1605 *fault_addr = va;
1606 return SIGSEGV;
1607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 break;
1609
1610 case madd_d_op:
1611 handler = fpemu_dp_madd;
1612 goto dcoptop;
1613 case msub_d_op:
1614 handler = fpemu_dp_msub;
1615 goto dcoptop;
1616 case nmadd_d_op:
1617 handler = fpemu_dp_nmadd;
1618 goto dcoptop;
1619 case nmsub_d_op:
1620 handler = fpemu_dp_nmsub;
1621 goto dcoptop;
1622
1623 dcoptop:
1624 DPFROMREG(fr, MIPSInst_FR(ir));
1625 DPFROMREG(fs, MIPSInst_FS(ir));
1626 DPFROMREG(ft, MIPSInst_FT(ir));
1627 fd = (*handler) (fr, fs, ft);
1628 DPTOREG(fd, MIPSInst_FD(ir));
1629 goto copcsr;
1630
1631 default:
1632 return SIGILL;
1633 }
1634 break;
1635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001637 case 0x3:
1638 if (MIPSInst_FUNC(ir) != pfetch_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return SIGILL;
Deng-Cheng Zhu51061b82014-03-06 17:05:27 -08001640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 /* ignore prefx operation */
1642 break;
1643
1644 default:
1645 return SIGILL;
1646 }
1647
1648 return 0;
1649}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651
1652
1653/*
1654 * Emulate a single COP1 arithmetic instruction.
1655 */
Atsushi Nemotoeae89072006-05-16 01:26:03 +09001656static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 mips_instruction ir)
1658{
1659 int rfmt; /* resulting format */
1660 unsigned rcsr = 0; /* resulting csr */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001661 unsigned int oldrm;
1662 unsigned int cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 unsigned cond;
1664 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001665 union ieee754dp d;
1666 union ieee754sp s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 s64 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 } rv; /* resulting value */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001670 u64 bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
David Daneyb6ee75e2009-11-05 11:34:26 -08001672 MIPS_FPU_EMU_INC_STATS(cp1ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001674 case s_fmt: { /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001676 union ieee754sp(*b) (union ieee754sp, union ieee754sp);
1677 union ieee754sp(*u) (union ieee754sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 } handler;
Paul Burton4b820d92016-04-21 14:04:48 +01001679 union ieee754sp fd, fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 switch (MIPSInst_FUNC(ir)) {
1682 /* binary ops */
1683 case fadd_op:
1684 handler.b = ieee754sp_add;
1685 goto scopbop;
1686 case fsub_op:
1687 handler.b = ieee754sp_sub;
1688 goto scopbop;
1689 case fmul_op:
1690 handler.b = ieee754sp_mul;
1691 goto scopbop;
1692 case fdiv_op:
1693 handler.b = ieee754sp_div;
1694 goto scopbop;
1695
1696 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 case fsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001698 if (!cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001699 return SIGILL;
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 handler.u = ieee754sp_sqrt;
1702 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001703
Ralf Baechle08a07902014-04-19 13:11:37 +02001704 /*
1705 * Note that on some MIPS IV implementations such as the
1706 * R5000 and R8000 the FSQRT and FRECIP instructions do not
1707 * achieve full IEEE-754 accuracy - however this emulator does.
1708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 case frsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001710 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001711 return SIGILL;
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 handler.u = fpemu_sp_rsqrt;
1714 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 case frecip_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001717 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001718 return SIGILL;
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 handler.u = fpemu_sp_recip;
1721 goto scopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001724 if (!cpu_has_mips_4_5_r)
1725 return SIGILL;
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
1728 if (((ctx->fcr31 & cond) != 0) !=
1729 ((MIPSInst_FT(ir) & 1) != 0))
1730 return 0;
1731 SPFROMREG(rv.s, MIPSInst_FS(ir));
1732 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001735 if (!cpu_has_mips_4_5_r)
1736 return SIGILL;
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (xcp->regs[MIPSInst_FT(ir)] != 0)
1739 return 0;
1740 SPFROMREG(rv.s, MIPSInst_FS(ir));
1741 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02001744 if (!cpu_has_mips_4_5_r)
1745 return SIGILL;
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (xcp->regs[MIPSInst_FT(ir)] == 0)
1748 return 0;
1749 SPFROMREG(rv.s, MIPSInst_FS(ir));
1750 break;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001751
Markos Chandras67613f02015-08-13 09:56:29 +02001752 case fseleqz_op:
1753 if (!cpu_has_mips_r6)
1754 return SIGILL;
1755
1756 SPFROMREG(rv.s, MIPSInst_FT(ir));
1757 if (rv.w & 0x1)
1758 rv.w = 0;
1759 else
1760 SPFROMREG(rv.s, MIPSInst_FS(ir));
1761 break;
1762
Markos Chandras130fe352015-08-13 09:56:30 +02001763 case fselnez_op:
1764 if (!cpu_has_mips_r6)
1765 return SIGILL;
1766
1767 SPFROMREG(rv.s, MIPSInst_FT(ir));
1768 if (rv.w & 0x1)
1769 SPFROMREG(rv.s, MIPSInst_FS(ir));
1770 else
1771 rv.w = 0;
1772 break;
1773
Markos Chandrase24c3be2015-08-13 09:56:31 +02001774 case fmaddf_op: {
1775 union ieee754sp ft, fs, fd;
1776
1777 if (!cpu_has_mips_r6)
1778 return SIGILL;
1779
1780 SPFROMREG(ft, MIPSInst_FT(ir));
1781 SPFROMREG(fs, MIPSInst_FS(ir));
1782 SPFROMREG(fd, MIPSInst_FD(ir));
1783 rv.s = ieee754sp_maddf(fd, fs, ft);
1784 break;
1785 }
1786
Markos Chandras83d43302015-08-13 09:56:32 +02001787 case fmsubf_op: {
1788 union ieee754sp ft, fs, fd;
1789
1790 if (!cpu_has_mips_r6)
1791 return SIGILL;
1792
1793 SPFROMREG(ft, MIPSInst_FT(ir));
1794 SPFROMREG(fs, MIPSInst_FS(ir));
1795 SPFROMREG(fd, MIPSInst_FD(ir));
1796 rv.s = ieee754sp_msubf(fd, fs, ft);
1797 break;
1798 }
1799
Markos Chandras400bd2e2015-08-13 09:56:33 +02001800 case frint_op: {
1801 union ieee754sp fs;
1802
1803 if (!cpu_has_mips_r6)
1804 return SIGILL;
1805
1806 SPFROMREG(fs, MIPSInst_FS(ir));
1807 rv.l = ieee754sp_tlong(fs);
1808 rv.s = ieee754sp_flong(rv.l);
1809 goto copcsr;
1810 }
1811
Markos Chandras38db37b2015-08-13 09:56:34 +02001812 case fclass_op: {
1813 union ieee754sp fs;
1814
1815 if (!cpu_has_mips_r6)
1816 return SIGILL;
1817
1818 SPFROMREG(fs, MIPSInst_FS(ir));
1819 rv.w = ieee754sp_2008class(fs);
1820 rfmt = w_fmt;
1821 break;
1822 }
1823
Markos Chandras4e9561b2015-08-13 09:56:35 +02001824 case fmin_op: {
1825 union ieee754sp fs, ft;
1826
1827 if (!cpu_has_mips_r6)
1828 return SIGILL;
1829
1830 SPFROMREG(ft, MIPSInst_FT(ir));
1831 SPFROMREG(fs, MIPSInst_FS(ir));
1832 rv.s = ieee754sp_fmin(fs, ft);
1833 break;
1834 }
1835
1836 case fmina_op: {
1837 union ieee754sp fs, ft;
1838
1839 if (!cpu_has_mips_r6)
1840 return SIGILL;
1841
1842 SPFROMREG(ft, MIPSInst_FT(ir));
1843 SPFROMREG(fs, MIPSInst_FS(ir));
1844 rv.s = ieee754sp_fmina(fs, ft);
1845 break;
1846 }
1847
Markos Chandrasa79f5f92015-08-13 09:56:36 +02001848 case fmax_op: {
1849 union ieee754sp fs, ft;
1850
1851 if (!cpu_has_mips_r6)
1852 return SIGILL;
1853
1854 SPFROMREG(ft, MIPSInst_FT(ir));
1855 SPFROMREG(fs, MIPSInst_FS(ir));
1856 rv.s = ieee754sp_fmax(fs, ft);
1857 break;
1858 }
1859
1860 case fmaxa_op: {
1861 union ieee754sp fs, ft;
1862
1863 if (!cpu_has_mips_r6)
1864 return SIGILL;
1865
1866 SPFROMREG(ft, MIPSInst_FT(ir));
1867 SPFROMREG(fs, MIPSInst_FS(ir));
1868 rv.s = ieee754sp_fmaxa(fs, ft);
1869 break;
1870 }
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 case fabs_op:
1873 handler.u = ieee754sp_abs;
1874 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 case fneg_op:
1877 handler.u = ieee754sp_neg;
1878 goto scopuop;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 case fmov_op:
1881 /* an easy one */
1882 SPFROMREG(rv.s, MIPSInst_FS(ir));
1883 goto copcsr;
1884
1885 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001886scopbop:
1887 SPFROMREG(fs, MIPSInst_FS(ir));
1888 SPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001890 rv.s = (*handler.b) (fs, ft);
1891 goto copcsr;
1892scopuop:
1893 SPFROMREG(fs, MIPSInst_FS(ir));
1894 rv.s = (*handler.u) (fs);
1895 goto copcsr;
1896copcsr:
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001897 if (ieee754_cxtest(IEEE754_INEXACT)) {
1898 MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001900 }
1901 if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1902 MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001904 }
1905 if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1906 MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001908 }
1909 if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1910 MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001912 }
1913 if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1914 MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
Deng-Cheng Zhuc4103522014-05-29 12:26:45 -07001916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 break;
1918
1919 /* unary conv ops */
1920 case fcvts_op:
1921 return SIGILL; /* not defined */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001923 case fcvtd_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 SPFROMREG(fs, MIPSInst_FS(ir));
1925 rv.d = ieee754dp_fsp(fs);
1926 rfmt = d_fmt;
1927 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001929 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 SPFROMREG(fs, MIPSInst_FS(ir));
1931 rv.w = ieee754sp_tint(fs);
1932 rfmt = w_fmt;
1933 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 case fround_op:
1936 case ftrunc_op:
1937 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001938 case ffloor_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001939 if (!cpu_has_mips_2_3_4_5_r)
Ralf Baechle08a07902014-04-19 13:11:37 +02001940 return SIGILL;
1941
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001942 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 SPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01001944 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 rv.w = ieee754sp_tint(fs);
1946 ieee754_csr.rm = oldrm;
1947 rfmt = w_fmt;
1948 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Paul Burton4b820d92016-04-21 14:04:48 +01001950 case fsel_op:
1951 if (!cpu_has_mips_r6)
1952 return SIGILL;
1953
1954 SPFROMREG(fd, MIPSInst_FD(ir));
1955 if (fd.bits & 0x1)
1956 SPFROMREG(rv.s, MIPSInst_FT(ir));
1957 else
1958 SPFROMREG(rv.s, MIPSInst_FS(ir));
1959 break;
1960
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001961 case fcvtl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001962 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001963 return SIGILL;
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 SPFROMREG(fs, MIPSInst_FS(ir));
1966 rv.l = ieee754sp_tlong(fs);
1967 rfmt = l_fmt;
1968 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 case froundl_op:
1971 case ftruncl_op:
1972 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001973 case ffloorl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01001974 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02001975 return SIGILL;
1976
Ralf Baechle3f7cac42014-04-26 01:49:14 +02001977 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 SPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01001979 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 rv.l = ieee754sp_tlong(fs);
1981 ieee754_csr.rm = oldrm;
1982 rfmt = l_fmt;
1983 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 default:
Markos Chandrasf8c3c672015-08-13 09:56:28 +02001986 if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02001988 union ieee754sp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 SPFROMREG(fs, MIPSInst_FS(ir));
1991 SPFROMREG(ft, MIPSInst_FT(ir));
1992 rv.w = ieee754sp_cmp(fs, ft,
1993 cmptab[cmpop & 0x7], cmpop & 0x8);
1994 rfmt = -1;
1995 if ((cmpop & 0x8) && ieee754_cxtest
1996 (IEEE754_INVALID_OPERATION))
1997 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
1998 else
1999 goto copcsr;
2000
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002001 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return SIGILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 break;
2004 }
2005 break;
2006 }
2007
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002008 case d_fmt: {
Paul Burton4b820d92016-04-21 14:04:48 +01002009 union ieee754dp fd, fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 union {
Ralf Baechle2209bcb2014-04-16 01:31:11 +02002011 union ieee754dp(*b) (union ieee754dp, union ieee754dp);
2012 union ieee754dp(*u) (union ieee754dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 } handler;
2014
2015 switch (MIPSInst_FUNC(ir)) {
2016 /* binary ops */
2017 case fadd_op:
2018 handler.b = ieee754dp_add;
2019 goto dcopbop;
2020 case fsub_op:
2021 handler.b = ieee754dp_sub;
2022 goto dcopbop;
2023 case fmul_op:
2024 handler.b = ieee754dp_mul;
2025 goto dcopbop;
2026 case fdiv_op:
2027 handler.b = ieee754dp_div;
2028 goto dcopbop;
2029
2030 /* unary ops */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 case fsqrt_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002032 if (!cpu_has_mips_2_3_4_5_r)
2033 return SIGILL;
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 handler.u = ieee754dp_sqrt;
2036 goto dcopuop;
Ralf Baechle08a07902014-04-19 13:11:37 +02002037 /*
2038 * Note that on some MIPS IV implementations such as the
2039 * R5000 and R8000 the FSQRT and FRECIP instructions do not
2040 * achieve full IEEE-754 accuracy - however this emulator does.
2041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 case frsqrt_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002043 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002044 return SIGILL;
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 handler.u = fpemu_dp_rsqrt;
2047 goto dcopuop;
2048 case frecip_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002049 if (!cpu_has_mips_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002050 return SIGILL;
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 handler.u = fpemu_dp_recip;
2053 goto dcopuop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 case fmovc_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002055 if (!cpu_has_mips_4_5_r)
2056 return SIGILL;
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 cond = fpucondbit[MIPSInst_FT(ir) >> 2];
2059 if (((ctx->fcr31 & cond) != 0) !=
2060 ((MIPSInst_FT(ir) & 1) != 0))
2061 return 0;
2062 DPFROMREG(rv.d, MIPSInst_FS(ir));
2063 break;
2064 case fmovz_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002065 if (!cpu_has_mips_4_5_r)
2066 return SIGILL;
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (xcp->regs[MIPSInst_FT(ir)] != 0)
2069 return 0;
2070 DPFROMREG(rv.d, MIPSInst_FS(ir));
2071 break;
2072 case fmovn_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002073 if (!cpu_has_mips_4_5_r)
2074 return SIGILL;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (xcp->regs[MIPSInst_FT(ir)] == 0)
2077 return 0;
2078 DPFROMREG(rv.d, MIPSInst_FS(ir));
2079 break;
Markos Chandras67613f02015-08-13 09:56:29 +02002080
2081 case fseleqz_op:
2082 if (!cpu_has_mips_r6)
2083 return SIGILL;
2084
2085 DPFROMREG(rv.d, MIPSInst_FT(ir));
2086 if (rv.l & 0x1)
2087 rv.l = 0;
2088 else
2089 DPFROMREG(rv.d, MIPSInst_FS(ir));
2090 break;
2091
Markos Chandras130fe352015-08-13 09:56:30 +02002092 case fselnez_op:
2093 if (!cpu_has_mips_r6)
2094 return SIGILL;
2095
2096 DPFROMREG(rv.d, MIPSInst_FT(ir));
2097 if (rv.l & 0x1)
2098 DPFROMREG(rv.d, MIPSInst_FS(ir));
2099 else
2100 rv.l = 0;
2101 break;
2102
Markos Chandrase24c3be2015-08-13 09:56:31 +02002103 case fmaddf_op: {
2104 union ieee754dp ft, fs, fd;
2105
2106 if (!cpu_has_mips_r6)
2107 return SIGILL;
2108
2109 DPFROMREG(ft, MIPSInst_FT(ir));
2110 DPFROMREG(fs, MIPSInst_FS(ir));
2111 DPFROMREG(fd, MIPSInst_FD(ir));
2112 rv.d = ieee754dp_maddf(fd, fs, ft);
2113 break;
2114 }
2115
Markos Chandras83d43302015-08-13 09:56:32 +02002116 case fmsubf_op: {
2117 union ieee754dp ft, fs, fd;
2118
2119 if (!cpu_has_mips_r6)
2120 return SIGILL;
2121
2122 DPFROMREG(ft, MIPSInst_FT(ir));
2123 DPFROMREG(fs, MIPSInst_FS(ir));
2124 DPFROMREG(fd, MIPSInst_FD(ir));
2125 rv.d = ieee754dp_msubf(fd, fs, ft);
2126 break;
2127 }
2128
Markos Chandras400bd2e2015-08-13 09:56:33 +02002129 case frint_op: {
2130 union ieee754dp fs;
2131
2132 if (!cpu_has_mips_r6)
2133 return SIGILL;
2134
2135 DPFROMREG(fs, MIPSInst_FS(ir));
2136 rv.l = ieee754dp_tlong(fs);
2137 rv.d = ieee754dp_flong(rv.l);
2138 goto copcsr;
2139 }
2140
Markos Chandras38db37b2015-08-13 09:56:34 +02002141 case fclass_op: {
2142 union ieee754dp fs;
2143
2144 if (!cpu_has_mips_r6)
2145 return SIGILL;
2146
2147 DPFROMREG(fs, MIPSInst_FS(ir));
2148 rv.w = ieee754dp_2008class(fs);
2149 rfmt = w_fmt;
2150 break;
2151 }
2152
Markos Chandras4e9561b2015-08-13 09:56:35 +02002153 case fmin_op: {
2154 union ieee754dp fs, ft;
2155
2156 if (!cpu_has_mips_r6)
2157 return SIGILL;
2158
2159 DPFROMREG(ft, MIPSInst_FT(ir));
2160 DPFROMREG(fs, MIPSInst_FS(ir));
2161 rv.d = ieee754dp_fmin(fs, ft);
2162 break;
2163 }
2164
2165 case fmina_op: {
2166 union ieee754dp fs, ft;
2167
2168 if (!cpu_has_mips_r6)
2169 return SIGILL;
2170
2171 DPFROMREG(ft, MIPSInst_FT(ir));
2172 DPFROMREG(fs, MIPSInst_FS(ir));
2173 rv.d = ieee754dp_fmina(fs, ft);
2174 break;
2175 }
2176
Markos Chandrasa79f5f92015-08-13 09:56:36 +02002177 case fmax_op: {
2178 union ieee754dp fs, ft;
2179
2180 if (!cpu_has_mips_r6)
2181 return SIGILL;
2182
2183 DPFROMREG(ft, MIPSInst_FT(ir));
2184 DPFROMREG(fs, MIPSInst_FS(ir));
2185 rv.d = ieee754dp_fmax(fs, ft);
2186 break;
2187 }
2188
2189 case fmaxa_op: {
2190 union ieee754dp fs, ft;
2191
2192 if (!cpu_has_mips_r6)
2193 return SIGILL;
2194
2195 DPFROMREG(ft, MIPSInst_FT(ir));
2196 DPFROMREG(fs, MIPSInst_FS(ir));
2197 rv.d = ieee754dp_fmaxa(fs, ft);
2198 break;
2199 }
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 case fabs_op:
2202 handler.u = ieee754dp_abs;
2203 goto dcopuop;
2204
2205 case fneg_op:
2206 handler.u = ieee754dp_neg;
2207 goto dcopuop;
2208
2209 case fmov_op:
2210 /* an easy one */
2211 DPFROMREG(rv.d, MIPSInst_FS(ir));
2212 goto copcsr;
2213
2214 /* binary op on handler */
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002215dcopbop:
2216 DPFROMREG(fs, MIPSInst_FS(ir));
2217 DPFROMREG(ft, MIPSInst_FT(ir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002219 rv.d = (*handler.b) (fs, ft);
2220 goto copcsr;
2221dcopuop:
2222 DPFROMREG(fs, MIPSInst_FS(ir));
2223 rv.d = (*handler.u) (fs);
2224 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002226 /*
2227 * unary conv ops
2228 */
2229 case fcvts_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 DPFROMREG(fs, MIPSInst_FS(ir));
2231 rv.s = ieee754sp_fdp(fs);
2232 rfmt = s_fmt;
2233 goto copcsr;
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 case fcvtd_op:
2236 return SIGILL; /* not defined */
2237
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002238 case fcvtw_op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 DPFROMREG(fs, MIPSInst_FS(ir));
2240 rv.w = ieee754dp_tint(fs); /* wrong */
2241 rfmt = w_fmt;
2242 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 case fround_op:
2245 case ftrunc_op:
2246 case fceil_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002247 case ffloor_op:
Ralf Baechle08a07902014-04-19 13:11:37 +02002248 if (!cpu_has_mips_2_3_4_5_r)
2249 return SIGILL;
2250
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002251 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 DPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01002253 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 rv.w = ieee754dp_tint(fs);
2255 ieee754_csr.rm = oldrm;
2256 rfmt = w_fmt;
2257 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Paul Burton4b820d92016-04-21 14:04:48 +01002259 case fsel_op:
2260 if (!cpu_has_mips_r6)
2261 return SIGILL;
2262
2263 DPFROMREG(fd, MIPSInst_FD(ir));
2264 if (fd.bits & 0x1)
2265 DPFROMREG(rv.d, MIPSInst_FT(ir));
2266 else
2267 DPFROMREG(rv.d, MIPSInst_FS(ir));
2268 break;
2269
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002270 case fcvtl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002271 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002272 return SIGILL;
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 DPFROMREG(fs, MIPSInst_FS(ir));
2275 rv.l = ieee754dp_tlong(fs);
2276 rfmt = l_fmt;
2277 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 case froundl_op:
2280 case ftruncl_op:
2281 case fceill_op:
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002282 case ffloorl_op:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002283 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002284 return SIGILL;
2285
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002286 oldrm = ieee754_csr.rm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 DPFROMREG(fs, MIPSInst_FS(ir));
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01002288 ieee754_csr.rm = MIPSInst_FUNC(ir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 rv.l = ieee754dp_tlong(fs);
2290 ieee754_csr.rm = oldrm;
2291 rfmt = l_fmt;
2292 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 default:
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002295 if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
Ralf Baechle2209bcb2014-04-16 01:31:11 +02002297 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 DPFROMREG(fs, MIPSInst_FS(ir));
2300 DPFROMREG(ft, MIPSInst_FT(ir));
2301 rv.w = ieee754dp_cmp(fs, ft,
2302 cmptab[cmpop & 0x7], cmpop & 0x8);
2303 rfmt = -1;
2304 if ((cmpop & 0x8)
2305 &&
2306 ieee754_cxtest
2307 (IEEE754_INVALID_OPERATION))
2308 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2309 else
2310 goto copcsr;
2311
2312 }
2313 else {
2314 return SIGILL;
2315 }
2316 break;
2317 }
2318 break;
Markos Chandrasbbdd8142015-07-16 14:06:45 +01002319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Markos Chandrasbbdd8142015-07-16 14:06:45 +01002321 case w_fmt: {
2322 union ieee754dp fs;
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 switch (MIPSInst_FUNC(ir)) {
2325 case fcvts_op:
2326 /* convert word to single precision real */
2327 SPFROMREG(fs, MIPSInst_FS(ir));
2328 rv.s = ieee754sp_fint(fs.bits);
2329 rfmt = s_fmt;
2330 goto copcsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 case fcvtd_op:
2332 /* convert word to double precision real */
2333 SPFROMREG(fs, MIPSInst_FS(ir));
2334 rv.d = ieee754dp_fint(fs.bits);
2335 rfmt = d_fmt;
2336 goto copcsr;
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002337 default: {
2338 /* Emulating the new CMP.condn.fmt R6 instruction */
2339#define CMPOP_MASK 0x7
2340#define SIGN_BIT (0x1 << 3)
2341#define PREDICATE_BIT (0x1 << 4)
2342
2343 int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2344 int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2345 union ieee754sp fs, ft;
2346
2347 /* This is an R6 only instruction */
2348 if (!cpu_has_mips_r6 ||
2349 (MIPSInst_FUNC(ir) & 0x20))
2350 return SIGILL;
2351
2352 /* fmt is w_fmt for single precision so fix it */
2353 rfmt = s_fmt;
2354 /* default to false */
2355 rv.w = 0;
2356
2357 /* CMP.condn.S */
2358 SPFROMREG(fs, MIPSInst_FS(ir));
2359 SPFROMREG(ft, MIPSInst_FT(ir));
2360
2361 /* positive predicates */
2362 if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2363 if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
2364 sig))
2365 rv.w = -1; /* true, all 1s */
2366 if ((sig) &&
2367 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2368 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2369 else
2370 goto copcsr;
2371 } else {
2372 /* negative predicates */
2373 switch (cmpop) {
2374 case 1:
2375 case 2:
2376 case 3:
2377 if (ieee754sp_cmp(fs, ft,
2378 negative_cmptab[cmpop],
2379 sig))
2380 rv.w = -1; /* true, all 1s */
2381 if (sig &&
2382 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2383 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2384 else
2385 goto copcsr;
2386 break;
2387 default:
2388 /* Reserved R6 ops */
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002389 return SIGILL;
2390 }
2391 }
2392 break;
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002397 case l_fmt:
Ralf Baechle08a07902014-04-19 13:11:37 +02002398
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002399 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002400 return SIGILL;
2401
Paul Burtonbbd426f2014-02-13 11:26:41 +00002402 DIFROMREG(bits, MIPSInst_FS(ir));
2403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 switch (MIPSInst_FUNC(ir)) {
2405 case fcvts_op:
2406 /* convert long to single precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00002407 rv.s = ieee754sp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 rfmt = s_fmt;
2409 goto copcsr;
2410 case fcvtd_op:
2411 /* convert long to double precision real */
Paul Burtonbbd426f2014-02-13 11:26:41 +00002412 rv.d = ieee754dp_flong(bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 rfmt = d_fmt;
2414 goto copcsr;
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002415 default: {
2416 /* Emulating the new CMP.condn.fmt R6 instruction */
2417 int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2418 int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2419 union ieee754dp fs, ft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002421 if (!cpu_has_mips_r6 ||
2422 (MIPSInst_FUNC(ir) & 0x20))
2423 return SIGILL;
2424
2425 /* fmt is l_fmt for double precision so fix it */
2426 rfmt = d_fmt;
2427 /* default to false */
2428 rv.l = 0;
2429
2430 /* CMP.condn.D */
2431 DPFROMREG(fs, MIPSInst_FS(ir));
2432 DPFROMREG(ft, MIPSInst_FT(ir));
2433
2434 /* positive predicates */
2435 if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2436 if (ieee754dp_cmp(fs, ft,
2437 cmptab[cmpop], sig))
2438 rv.l = -1LL; /* true, all 1s */
2439 if (sig &&
2440 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2441 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2442 else
2443 goto copcsr;
2444 } else {
2445 /* negative predicates */
2446 switch (cmpop) {
2447 case 1:
2448 case 2:
2449 case 3:
2450 if (ieee754dp_cmp(fs, ft,
2451 negative_cmptab[cmpop],
2452 sig))
2453 rv.l = -1LL; /* true, all 1s */
2454 if (sig &&
2455 ieee754_cxtest(IEEE754_INVALID_OPERATION))
2456 rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2457 else
2458 goto copcsr;
2459 break;
2460 default:
2461 /* Reserved R6 ops */
Markos Chandrasf8c3c672015-08-13 09:56:28 +02002462 return SIGILL;
2463 }
2464 }
2465 break;
2466 }
2467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 default:
2469 return SIGILL;
2470 }
2471
2472 /*
2473 * Update the fpu CSR register for this operation.
2474 * If an exception is required, generate a tidy SIGFPE exception,
2475 * without updating the result register.
2476 * Note: cause exception bits do not accumulate, they are rewritten
2477 * for each op; only the flag/sticky bits accumulate.
2478 */
2479 ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
2480 if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002481 /*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return SIGFPE;
2483 }
2484
2485 /*
2486 * Now we can safely write the result back to the register file.
2487 */
2488 switch (rfmt) {
Ralf Baechle08a07902014-04-19 13:11:37 +02002489 case -1:
2490
2491 if (cpu_has_mips_4_5_r)
Rob Kendrickc3b9b942014-07-23 10:03:58 +01002492 cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 else
Ralf Baechle08a07902014-04-19 13:11:37 +02002494 cbit = FPU_CSR_COND;
2495 if (rv.w)
2496 ctx->fcr31 |= cbit;
2497 else
2498 ctx->fcr31 &= ~cbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 break;
Ralf Baechle08a07902014-04-19 13:11:37 +02002500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 case d_fmt:
2502 DPTOREG(rv.d, MIPSInst_FD(ir));
2503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 case s_fmt:
2505 SPTOREG(rv.s, MIPSInst_FD(ir));
2506 break;
2507 case w_fmt:
2508 SITOREG(rv.w, MIPSInst_FD(ir));
2509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 case l_fmt:
Maciej W. Rozycki2d83fea2015-04-03 23:26:49 +01002511 if (!cpu_has_mips_3_4_5_64_r2_r6)
Ralf Baechle08a07902014-04-19 13:11:37 +02002512 return SIGILL;
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 DITOREG(rv.l, MIPSInst_FD(ir));
2515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 default:
2517 return SIGILL;
2518 }
2519
2520 return 0;
2521}
2522
Maciej W. Rozyckid79354c2017-06-16 00:05:08 +01002523/*
2524 * Emulate FPU instructions.
2525 *
2526 * If we use FPU hardware, then we have been typically called to handle
2527 * an unimplemented operation, such as where an operand is a NaN or
2528 * denormalized. In that case exit the emulation loop after a single
2529 * iteration so as to let hardware execute any subsequent instructions.
2530 *
2531 * If we have no FPU hardware or it has been disabled, then continue
2532 * emulating floating-point instructions until one of these conditions
2533 * has occurred:
2534 *
2535 * - a non-FPU instruction has been encountered,
2536 *
2537 * - an attempt to emulate has ended with a signal,
2538 *
2539 * - the ISA mode has been switched.
2540 *
2541 * We need to terminate the emulation loop if we got switched to the
2542 * MIPS16 mode, whether supported or not, so that we do not attempt
2543 * to emulate a MIPS16 instruction as a regular MIPS FPU instruction.
2544 * Similarly if we got switched to the microMIPS mode and only the
2545 * regular MIPS mode is supported, so that we do not attempt to emulate
2546 * a microMIPS instruction as a regular MIPS FPU instruction. Or if
2547 * we got switched to the regular MIPS mode and only the microMIPS mode
2548 * is supported, so that we do not attempt to emulate a regular MIPS
2549 * instruction that should cause an Address Error exception instead.
2550 * For simplicity we always terminate upon an ISA mode switch.
2551 */
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002552int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
David Daney515b0292010-10-21 16:32:26 -07002553 int has_fpu, void *__user *fault_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Ralf Baechle333d1f62005-02-28 17:55:57 +00002555 unsigned long oldepc, prevepc;
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002556 struct mm_decoded_insn dec_insn;
2557 u16 instr[4];
2558 u16 *instr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 int sig = 0;
2560
2561 oldepc = xcp->cp0_epc;
2562 do {
2563 prevepc = xcp->cp0_epc;
2564
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002565 if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2566 /*
2567 * Get next 2 microMIPS instructions and convert them
2568 * into 32-bit instructions.
2569 */
2570 if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2571 (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2572 (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2573 (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2574 MIPS_FPU_EMU_INC_STATS(errors);
2575 return SIGBUS;
2576 }
2577 instr_ptr = instr;
2578
2579 /* Get first instruction. */
2580 if (mm_insn_16bit(*instr_ptr)) {
2581 /* Duplicate the half-word. */
2582 dec_insn.insn = (*instr_ptr << 16) |
2583 (*instr_ptr);
2584 /* 16-bit instruction. */
2585 dec_insn.pc_inc = 2;
2586 instr_ptr += 1;
2587 } else {
2588 dec_insn.insn = (*instr_ptr << 16) |
2589 *(instr_ptr+1);
2590 /* 32-bit instruction. */
2591 dec_insn.pc_inc = 4;
2592 instr_ptr += 2;
2593 }
2594 /* Get second instruction. */
2595 if (mm_insn_16bit(*instr_ptr)) {
2596 /* Duplicate the half-word. */
2597 dec_insn.next_insn = (*instr_ptr << 16) |
2598 (*instr_ptr);
2599 /* 16-bit instruction. */
2600 dec_insn.next_pc_inc = 2;
2601 } else {
2602 dec_insn.next_insn = (*instr_ptr << 16) |
2603 *(instr_ptr+1);
2604 /* 32-bit instruction. */
2605 dec_insn.next_pc_inc = 4;
2606 }
2607 dec_insn.micro_mips_mode = 1;
2608 } else {
2609 if ((get_user(dec_insn.insn,
2610 (mips_instruction __user *) xcp->cp0_epc)) ||
2611 (get_user(dec_insn.next_insn,
2612 (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2613 MIPS_FPU_EMU_INC_STATS(errors);
2614 return SIGBUS;
2615 }
2616 dec_insn.pc_inc = 4;
2617 dec_insn.next_pc_inc = 4;
2618 dec_insn.micro_mips_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002620
2621 if ((dec_insn.insn == 0) ||
2622 ((dec_insn.pc_inc == 2) &&
2623 ((dec_insn.insn & 0xffff) == MM_NOP16)))
2624 xcp->cp0_epc += dec_insn.pc_inc; /* Skip NOPs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 else {
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002626 /*
Maciej W. Rozycki2cfcf8a2015-04-03 23:24:56 +01002627 * The 'ieee754_csr' is an alias of ctx->fcr31.
2628 * No need to copy ctx->fcr31 to ieee754_csr.
Ralf Baechlecd21dfc2005-04-28 13:39:10 +00002629 */
Leonid Yegoshin102cedc2013-03-25 12:09:02 -05002630 sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632
Atsushi Nemotoe04582b2006-10-09 00:10:01 +09002633 if (has_fpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 break;
2635 if (sig)
2636 break;
Maciej W. Rozyckid79354c2017-06-16 00:05:08 +01002637 /*
2638 * We have to check for the ISA bit explicitly here,
2639 * because `get_isa16_mode' may return 0 if support
2640 * for code compression has been globally disabled,
2641 * or otherwise we may produce the wrong signal or
2642 * even proceed successfully where we must not.
2643 */
2644 if ((xcp->cp0_epc ^ prevepc) & 0x1)
2645 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 cond_resched();
2648 } while (xcp->cp0_epc > prevepc);
2649
2650 /* SIGILL indicates a non-fpu instruction */
2651 if (sig == SIGILL && xcp->cp0_epc != oldepc)
Ralf Baechle3f7cac42014-04-26 01:49:14 +02002652 /* but if EPC has advanced, then ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 sig = 0;
2654
2655 return sig;
2656}