xref: /linux/arch/mips/math-emu/cp1emu.c (revision 9d5a634946f5efd64e4acbcb67c22e63234bc233)
1*9d5a6349SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
33f7cac41SRalf Baechle  * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * MIPS floating point support
61da177e4SLinus Torvalds  * Copyright (C) 1994-2000 Algorithmics Ltd.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
91da177e4SLinus Torvalds  * Copyright (C) 2000  MIPS Technologies, Inc.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * A complete emulator for MIPS coprocessor 1 instructions.  This is
121da177e4SLinus Torvalds  * required for #float(switch) or #float(trap), where it catches all
131da177e4SLinus Torvalds  * COP1 instructions via the "CoProcessor Unusable" exception.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * More surprisingly it is also required for #float(ieee), to help out
163f7cac41SRalf Baechle  * the hardware FPU at the boundaries of the IEEE-754 representation
171da177e4SLinus Torvalds  * (denormalised values, infinities, underflow, etc).  It is made
181da177e4SLinus Torvalds  * quite nasty because emulation of some non-COP1 instructions is
191da177e4SLinus Torvalds  * required, e.g. in branch delay slots.
201da177e4SLinus Torvalds  *
213f7cac41SRalf Baechle  * Note if you know that you won't have an FPU, then you'll get much
221da177e4SLinus Torvalds  * better performance by compiling with -msoft-float!
231da177e4SLinus Torvalds  */
241da177e4SLinus Torvalds #include <linux/sched.h>
2583fd38caSAtsushi Nemoto #include <linux/debugfs.h>
2685c51c51SRalf Baechle #include <linux/percpu-defs.h>
277f788d2dSDeng-Cheng Zhu #include <linux/perf_event.h>
281da177e4SLinus Torvalds 
29cd8ee345SRalf Baechle #include <asm/branch.h>
301da177e4SLinus Torvalds #include <asm/inst.h>
311da177e4SLinus Torvalds #include <asm/ptrace.h>
321da177e4SLinus Torvalds #include <asm/signal.h>
337c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
34cd8ee345SRalf Baechle 
35f6843626SMaciej W. Rozycki #include <asm/cpu-info.h>
36cd8ee345SRalf Baechle #include <asm/processor.h>
371da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
38102cedc3SLeonid Yegoshin #include <asm/fpu.h>
39b0a668fbSLeonid Yegoshin #include <asm/mips-r2-to-r6-emul.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include "ieee754.h"
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */
441da177e4SLinus Torvalds 
45eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
461da177e4SLinus Torvalds 	mips_instruction);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *,
49445a58ceSPaul Burton 	struct mips_fpu_struct *, mips_instruction, void __user **);
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds /* Control registers */
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #define FPCREG_RID	0	/* $0  = revision id */
54c491cfa2SMaciej W. Rozycki #define FPCREG_FCCR	25	/* $25 = fccr */
55c491cfa2SMaciej W. Rozycki #define FPCREG_FEXR	26	/* $26 = fexr */
56c491cfa2SMaciej W. Rozycki #define FPCREG_FENR	28	/* $28 = fenr */
571da177e4SLinus Torvalds #define FPCREG_CSR	31	/* $31 = csr */
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* convert condition code register number to csr bit */
60b0a668fbSLeonid Yegoshin const unsigned int fpucondbit[8] = {
61c491cfa2SMaciej W. Rozycki 	FPU_CSR_COND,
621da177e4SLinus Torvalds 	FPU_CSR_COND1,
631da177e4SLinus Torvalds 	FPU_CSR_COND2,
641da177e4SLinus Torvalds 	FPU_CSR_COND3,
651da177e4SLinus Torvalds 	FPU_CSR_COND4,
661da177e4SLinus Torvalds 	FPU_CSR_COND5,
671da177e4SLinus Torvalds 	FPU_CSR_COND6,
681da177e4SLinus Torvalds 	FPU_CSR_COND7
691da177e4SLinus Torvalds };
701da177e4SLinus Torvalds 
71102cedc3SLeonid Yegoshin /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
72102cedc3SLeonid Yegoshin static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
73102cedc3SLeonid Yegoshin static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
74102cedc3SLeonid Yegoshin static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
75102cedc3SLeonid Yegoshin static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
76102cedc3SLeonid Yegoshin 
77102cedc3SLeonid Yegoshin /*
78102cedc3SLeonid Yegoshin  * This functions translates a 32-bit microMIPS instruction
79102cedc3SLeonid Yegoshin  * into a 32-bit MIPS32 instruction. Returns 0 on success
80102cedc3SLeonid Yegoshin  * and SIGILL otherwise.
81102cedc3SLeonid Yegoshin  */
82102cedc3SLeonid Yegoshin static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
83102cedc3SLeonid Yegoshin {
84102cedc3SLeonid Yegoshin 	union mips_instruction insn = *insn_ptr;
85102cedc3SLeonid Yegoshin 	union mips_instruction mips32_insn = insn;
86102cedc3SLeonid Yegoshin 	int func, fmt, op;
87102cedc3SLeonid Yegoshin 
88102cedc3SLeonid Yegoshin 	switch (insn.mm_i_format.opcode) {
89102cedc3SLeonid Yegoshin 	case mm_ldc132_op:
90102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = ldc1_op;
91102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
92102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
93102cedc3SLeonid Yegoshin 		break;
94102cedc3SLeonid Yegoshin 	case mm_lwc132_op:
95102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = lwc1_op;
96102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
97102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
98102cedc3SLeonid Yegoshin 		break;
99102cedc3SLeonid Yegoshin 	case mm_sdc132_op:
100102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = sdc1_op;
101102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
102102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
103102cedc3SLeonid Yegoshin 		break;
104102cedc3SLeonid Yegoshin 	case mm_swc132_op:
105102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = swc1_op;
106102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
107102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
108102cedc3SLeonid Yegoshin 		break;
109102cedc3SLeonid Yegoshin 	case mm_pool32i_op:
110102cedc3SLeonid Yegoshin 		/* NOTE: offset is << by 1 if in microMIPS mode. */
111102cedc3SLeonid Yegoshin 		if ((insn.mm_i_format.rt == mm_bc1f_op) ||
112102cedc3SLeonid Yegoshin 		    (insn.mm_i_format.rt == mm_bc1t_op)) {
113102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.opcode = cop1_op;
114102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.bc = bc_op;
115102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.flag =
116102cedc3SLeonid Yegoshin 				(insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
117102cedc3SLeonid Yegoshin 		} else
118102cedc3SLeonid Yegoshin 			return SIGILL;
119102cedc3SLeonid Yegoshin 		break;
120102cedc3SLeonid Yegoshin 	case mm_pool32f_op:
121102cedc3SLeonid Yegoshin 		switch (insn.mm_fp0_format.func) {
122102cedc3SLeonid Yegoshin 		case mm_32f_01_op:
123102cedc3SLeonid Yegoshin 		case mm_32f_11_op:
124102cedc3SLeonid Yegoshin 		case mm_32f_02_op:
125102cedc3SLeonid Yegoshin 		case mm_32f_12_op:
126102cedc3SLeonid Yegoshin 		case mm_32f_41_op:
127102cedc3SLeonid Yegoshin 		case mm_32f_51_op:
128102cedc3SLeonid Yegoshin 		case mm_32f_42_op:
129102cedc3SLeonid Yegoshin 		case mm_32f_52_op:
130102cedc3SLeonid Yegoshin 			op = insn.mm_fp0_format.func;
131102cedc3SLeonid Yegoshin 			if (op == mm_32f_01_op)
132102cedc3SLeonid Yegoshin 				func = madd_s_op;
133102cedc3SLeonid Yegoshin 			else if (op == mm_32f_11_op)
134102cedc3SLeonid Yegoshin 				func = madd_d_op;
135102cedc3SLeonid Yegoshin 			else if (op == mm_32f_02_op)
136102cedc3SLeonid Yegoshin 				func = nmadd_s_op;
137102cedc3SLeonid Yegoshin 			else if (op == mm_32f_12_op)
138102cedc3SLeonid Yegoshin 				func = nmadd_d_op;
139102cedc3SLeonid Yegoshin 			else if (op == mm_32f_41_op)
140102cedc3SLeonid Yegoshin 				func = msub_s_op;
141102cedc3SLeonid Yegoshin 			else if (op == mm_32f_51_op)
142102cedc3SLeonid Yegoshin 				func = msub_d_op;
143102cedc3SLeonid Yegoshin 			else if (op == mm_32f_42_op)
144102cedc3SLeonid Yegoshin 				func = nmsub_s_op;
145102cedc3SLeonid Yegoshin 			else
146102cedc3SLeonid Yegoshin 				func = nmsub_d_op;
147102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.opcode = cop1x_op;
148102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
149102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
150102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
151102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
152102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.func = func;
153102cedc3SLeonid Yegoshin 			break;
154102cedc3SLeonid Yegoshin 		case mm_32f_10_op:
155102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
156102cedc3SLeonid Yegoshin 			op = insn.mm_fp5_format.op & 0x7;
157102cedc3SLeonid Yegoshin 			if (op == mm_ldxc1_op)
158102cedc3SLeonid Yegoshin 				func = ldxc1_op;
159102cedc3SLeonid Yegoshin 			else if (op == mm_sdxc1_op)
160102cedc3SLeonid Yegoshin 				func = sdxc1_op;
161102cedc3SLeonid Yegoshin 			else if (op == mm_lwxc1_op)
162102cedc3SLeonid Yegoshin 				func = lwxc1_op;
163102cedc3SLeonid Yegoshin 			else if (op == mm_swxc1_op)
164102cedc3SLeonid Yegoshin 				func = swxc1_op;
165102cedc3SLeonid Yegoshin 
166102cedc3SLeonid Yegoshin 			if (func != -1) {
167102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = cop1x_op;
168102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs =
169102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.base;
170102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
171102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.index;
172102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = 0;
173102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = insn.mm_fp5_format.fd;
174102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = func;
175102cedc3SLeonid Yegoshin 			} else
176102cedc3SLeonid Yegoshin 				return SIGILL;
177102cedc3SLeonid Yegoshin 			break;
178102cedc3SLeonid Yegoshin 		case mm_32f_40_op:
179102cedc3SLeonid Yegoshin 			op = -1;	/* Invalid */
180102cedc3SLeonid Yegoshin 			if (insn.mm_fp2_format.op == mm_fmovt_op)
181102cedc3SLeonid Yegoshin 				op = 1;
182102cedc3SLeonid Yegoshin 			else if (insn.mm_fp2_format.op == mm_fmovf_op)
183102cedc3SLeonid Yegoshin 				op = 0;
184102cedc3SLeonid Yegoshin 			if (op != -1) {
185102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
186102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
187102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp2_format.fmt];
188102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
189102cedc3SLeonid Yegoshin 					(insn.mm_fp2_format.cc<<2) + op;
190102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
191102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fs;
192102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
193102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fd;
194102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = fmovc_op;
195102cedc3SLeonid Yegoshin 			} else
196102cedc3SLeonid Yegoshin 				return SIGILL;
197102cedc3SLeonid Yegoshin 			break;
198102cedc3SLeonid Yegoshin 		case mm_32f_60_op:
199102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
200102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fadd_op)
201102cedc3SLeonid Yegoshin 				func = fadd_op;
202102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fsub_op)
203102cedc3SLeonid Yegoshin 				func = fsub_op;
204102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmul_op)
205102cedc3SLeonid Yegoshin 				func = fmul_op;
206102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fdiv_op)
207102cedc3SLeonid Yegoshin 				func = fdiv_op;
208102cedc3SLeonid Yegoshin 			if (func != -1) {
209102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
210102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
211102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
212102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
213102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
214102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
215102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
216102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
217102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
218102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
219102cedc3SLeonid Yegoshin 			} else
220102cedc3SLeonid Yegoshin 				return SIGILL;
221102cedc3SLeonid Yegoshin 			break;
222102cedc3SLeonid Yegoshin 		case mm_32f_70_op:
223102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
224102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fmovn_op)
225102cedc3SLeonid Yegoshin 				func = fmovn_op;
226102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmovz_op)
227102cedc3SLeonid Yegoshin 				func = fmovz_op;
228102cedc3SLeonid Yegoshin 			if (func != -1) {
229102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
230102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
231102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
232102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
233102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
234102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
235102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
236102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
237102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
238102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
239102cedc3SLeonid Yegoshin 			} else
240102cedc3SLeonid Yegoshin 				return SIGILL;
241102cedc3SLeonid Yegoshin 			break;
242102cedc3SLeonid Yegoshin 		case mm_32f_73_op:    /* POOL32FXF */
243102cedc3SLeonid Yegoshin 			switch (insn.mm_fp1_format.op) {
244102cedc3SLeonid Yegoshin 			case mm_movf0_op:
245102cedc3SLeonid Yegoshin 			case mm_movf1_op:
246102cedc3SLeonid Yegoshin 			case mm_movt0_op:
247102cedc3SLeonid Yegoshin 			case mm_movt1_op:
248102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
249102cedc3SLeonid Yegoshin 				    mm_movf0_op)
250102cedc3SLeonid Yegoshin 					op = 0;
251102cedc3SLeonid Yegoshin 				else
252102cedc3SLeonid Yegoshin 					op = 1;
253102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = spec_op;
254102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
255102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
256102cedc3SLeonid Yegoshin 					(insn.mm_fp4_format.cc << 2) + op;
257102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
258102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = 0;
259102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = movc_op;
260102cedc3SLeonid Yegoshin 				break;
261102cedc3SLeonid Yegoshin 			case mm_fcvtd0_op:
262102cedc3SLeonid Yegoshin 			case mm_fcvtd1_op:
263102cedc3SLeonid Yegoshin 			case mm_fcvts0_op:
264102cedc3SLeonid Yegoshin 			case mm_fcvts1_op:
265102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
266102cedc3SLeonid Yegoshin 				    mm_fcvtd0_op) {
267102cedc3SLeonid Yegoshin 					func = fcvtd_op;
268102cedc3SLeonid Yegoshin 					fmt = swl_format[insn.mm_fp3_format.fmt];
269102cedc3SLeonid Yegoshin 				} else {
270102cedc3SLeonid Yegoshin 					func = fcvts_op;
271102cedc3SLeonid Yegoshin 					fmt = dwl_format[insn.mm_fp3_format.fmt];
272102cedc3SLeonid Yegoshin 				}
273102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
274102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt = fmt;
275102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
276102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
277102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
278102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
279102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
280102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
281102cedc3SLeonid Yegoshin 				break;
282102cedc3SLeonid Yegoshin 			case mm_fmov0_op:
283102cedc3SLeonid Yegoshin 			case mm_fmov1_op:
284102cedc3SLeonid Yegoshin 			case mm_fabs0_op:
285102cedc3SLeonid Yegoshin 			case mm_fabs1_op:
286102cedc3SLeonid Yegoshin 			case mm_fneg0_op:
287102cedc3SLeonid Yegoshin 			case mm_fneg1_op:
288102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
289102cedc3SLeonid Yegoshin 				    mm_fmov0_op)
290102cedc3SLeonid Yegoshin 					func = fmov_op;
291102cedc3SLeonid Yegoshin 				else if ((insn.mm_fp1_format.op & 0x7f) ==
292102cedc3SLeonid Yegoshin 					 mm_fabs0_op)
293102cedc3SLeonid Yegoshin 					func = fabs_op;
294102cedc3SLeonid Yegoshin 				else
295102cedc3SLeonid Yegoshin 					func = fneg_op;
296102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
297102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
298102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp3_format.fmt];
299102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
300102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
301102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
302102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
303102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
304102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
305102cedc3SLeonid Yegoshin 				break;
306102cedc3SLeonid Yegoshin 			case mm_ffloorl_op:
307102cedc3SLeonid Yegoshin 			case mm_ffloorw_op:
308102cedc3SLeonid Yegoshin 			case mm_fceill_op:
309102cedc3SLeonid Yegoshin 			case mm_fceilw_op:
310102cedc3SLeonid Yegoshin 			case mm_ftruncl_op:
311102cedc3SLeonid Yegoshin 			case mm_ftruncw_op:
312102cedc3SLeonid Yegoshin 			case mm_froundl_op:
313102cedc3SLeonid Yegoshin 			case mm_froundw_op:
314102cedc3SLeonid Yegoshin 			case mm_fcvtl_op:
315102cedc3SLeonid Yegoshin 			case mm_fcvtw_op:
316102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_ffloorl_op)
317102cedc3SLeonid Yegoshin 					func = ffloorl_op;
318102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ffloorw_op)
319102cedc3SLeonid Yegoshin 					func = ffloor_op;
320102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceill_op)
321102cedc3SLeonid Yegoshin 					func = fceill_op;
322102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceilw_op)
323102cedc3SLeonid Yegoshin 					func = fceil_op;
324102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncl_op)
325102cedc3SLeonid Yegoshin 					func = ftruncl_op;
326102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncw_op)
327102cedc3SLeonid Yegoshin 					func = ftrunc_op;
328102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundl_op)
329102cedc3SLeonid Yegoshin 					func = froundl_op;
330102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundw_op)
331102cedc3SLeonid Yegoshin 					func = fround_op;
332102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fcvtl_op)
333102cedc3SLeonid Yegoshin 					func = fcvtl_op;
334102cedc3SLeonid Yegoshin 				else
335102cedc3SLeonid Yegoshin 					func = fcvtw_op;
336102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
337102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
338102cedc3SLeonid Yegoshin 					sd_format[insn.mm_fp1_format.fmt];
339102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
340102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
341102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
342102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
343102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
344102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
345102cedc3SLeonid Yegoshin 				break;
346102cedc3SLeonid Yegoshin 			case mm_frsqrt_op:
347102cedc3SLeonid Yegoshin 			case mm_fsqrt_op:
348102cedc3SLeonid Yegoshin 			case mm_frecip_op:
349102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_frsqrt_op)
350102cedc3SLeonid Yegoshin 					func = frsqrt_op;
351102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fsqrt_op)
352102cedc3SLeonid Yegoshin 					func = fsqrt_op;
353102cedc3SLeonid Yegoshin 				else
354102cedc3SLeonid Yegoshin 					func = frecip_op;
355102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
356102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
357102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp1_format.fmt];
358102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
359102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
360102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
361102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
362102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
363102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
364102cedc3SLeonid Yegoshin 				break;
365102cedc3SLeonid Yegoshin 			case mm_mfc1_op:
366102cedc3SLeonid Yegoshin 			case mm_mtc1_op:
367102cedc3SLeonid Yegoshin 			case mm_cfc1_op:
368102cedc3SLeonid Yegoshin 			case mm_ctc1_op:
3699355e59cSSteven J. Hill 			case mm_mfhc1_op:
3709355e59cSSteven J. Hill 			case mm_mthc1_op:
371102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_mfc1_op)
372102cedc3SLeonid Yegoshin 					op = mfc_op;
373102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_mtc1_op)
374102cedc3SLeonid Yegoshin 					op = mtc_op;
375102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_cfc1_op)
376102cedc3SLeonid Yegoshin 					op = cfc_op;
3779355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_ctc1_op)
378102cedc3SLeonid Yegoshin 					op = ctc_op;
3799355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_mfhc1_op)
3809355e59cSSteven J. Hill 					op = mfhc_op;
3819355e59cSSteven J. Hill 				else
3829355e59cSSteven J. Hill 					op = mthc_op;
383102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.opcode = cop1_op;
384102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.op = op;
385102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.rt =
386102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
387102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fs =
388102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
389102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fd = 0;
390102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.func = 0;
391102cedc3SLeonid Yegoshin 				break;
392102cedc3SLeonid Yegoshin 			default:
393102cedc3SLeonid Yegoshin 				return SIGILL;
394102cedc3SLeonid Yegoshin 			}
395102cedc3SLeonid Yegoshin 			break;
396102cedc3SLeonid Yegoshin 		case mm_32f_74_op:	/* c.cond.fmt */
397102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.opcode = cop1_op;
398102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fmt =
399102cedc3SLeonid Yegoshin 				sdps_format[insn.mm_fp4_format.fmt];
400102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
401102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
402102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
403102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.func =
404102cedc3SLeonid Yegoshin 				insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
405102cedc3SLeonid Yegoshin 			break;
406102cedc3SLeonid Yegoshin 		default:
407102cedc3SLeonid Yegoshin 			return SIGILL;
408102cedc3SLeonid Yegoshin 		}
409102cedc3SLeonid Yegoshin 		break;
410102cedc3SLeonid Yegoshin 	default:
411102cedc3SLeonid Yegoshin 		return SIGILL;
412102cedc3SLeonid Yegoshin 	}
413102cedc3SLeonid Yegoshin 
414102cedc3SLeonid Yegoshin 	*insn_ptr = mips32_insn;
415102cedc3SLeonid Yegoshin 	return 0;
416102cedc3SLeonid Yegoshin }
417102cedc3SLeonid Yegoshin 
4181da177e4SLinus Torvalds /*
4191da177e4SLinus Torvalds  * Redundant with logic already in kernel/branch.c,
4201da177e4SLinus Torvalds  * embedded in compute_return_epc.  At some point,
4211da177e4SLinus Torvalds  * a single subroutine should be used across both
4221da177e4SLinus Torvalds  * modules.
4231da177e4SLinus Torvalds  */
424432c6bacSPaul Burton int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
425102cedc3SLeonid Yegoshin 		  unsigned long *contpc)
4261da177e4SLinus Torvalds {
427102cedc3SLeonid Yegoshin 	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
428102cedc3SLeonid Yegoshin 	unsigned int fcr31;
429102cedc3SLeonid Yegoshin 	unsigned int bit = 0;
4308bcd84a4SDouglas Leung 	unsigned int bit0;
4318bcd84a4SDouglas Leung 	union fpureg *fpr;
432102cedc3SLeonid Yegoshin 
433102cedc3SLeonid Yegoshin 	switch (insn.i_format.opcode) {
4341da177e4SLinus Torvalds 	case spec_op:
435102cedc3SLeonid Yegoshin 		switch (insn.r_format.func) {
4361da177e4SLinus Torvalds 		case jalr_op:
437ab4a92e6SPaul Burton 			if (insn.r_format.rd != 0) {
438102cedc3SLeonid Yegoshin 				regs->regs[insn.r_format.rd] =
439102cedc3SLeonid Yegoshin 					regs->cp0_epc + dec_insn.pc_inc +
440102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
441ab4a92e6SPaul Burton 			}
4422a14b21aSAleksandar Markovic 			/* fall through */
4431da177e4SLinus Torvalds 		case jr_op:
4445f9f41c4SMarkos Chandras 			/* For R6, JR already emulated in jalr_op */
445143fefc8SMarkos Chandras 			if (NO_R6EMU && insn.r_format.func == jr_op)
4465f9f41c4SMarkos Chandras 				break;
447102cedc3SLeonid Yegoshin 			*contpc = regs->regs[insn.r_format.rs];
4481da177e4SLinus Torvalds 			return 1;
4491da177e4SLinus Torvalds 		}
4501da177e4SLinus Torvalds 		break;
4511da177e4SLinus Torvalds 	case bcond_op:
452102cedc3SLeonid Yegoshin 		switch (insn.i_format.rt) {
4531da177e4SLinus Torvalds 		case bltzal_op:
4541da177e4SLinus Torvalds 		case bltzall_op:
455319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
456319824eaSMarkos Chandras 			    insn.i_format.rt == bltzall_op))
457319824eaSMarkos Chandras 				break;
458319824eaSMarkos Chandras 
459102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
460102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
461102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
4622a14b21aSAleksandar Markovic 			/* fall through */
463102cedc3SLeonid Yegoshin 		case bltzl_op:
464319824eaSMarkos Chandras 			if (NO_R6EMU)
465319824eaSMarkos Chandras 				break;
4662a14b21aSAleksandar Markovic 			/* fall through */
467319824eaSMarkos Chandras 		case bltz_op:
468102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] < 0)
469102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
470102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
471102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
472102cedc3SLeonid Yegoshin 			else
473102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
474102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
475102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
4761da177e4SLinus Torvalds 			return 1;
477102cedc3SLeonid Yegoshin 		case bgezal_op:
478102cedc3SLeonid Yegoshin 		case bgezall_op:
479319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
480319824eaSMarkos Chandras 			    insn.i_format.rt == bgezall_op))
481319824eaSMarkos Chandras 				break;
482319824eaSMarkos Chandras 
483102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
484102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
485102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
4862a14b21aSAleksandar Markovic 			/* fall through */
487102cedc3SLeonid Yegoshin 		case bgezl_op:
488319824eaSMarkos Chandras 			if (NO_R6EMU)
489319824eaSMarkos Chandras 				break;
4902a14b21aSAleksandar Markovic 			/* fall through */
491319824eaSMarkos Chandras 		case bgez_op:
492102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] >= 0)
493102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
494102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
495102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
496102cedc3SLeonid Yegoshin 			else
497102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
498102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
499102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
500102cedc3SLeonid Yegoshin 			return 1;
5011da177e4SLinus Torvalds 		}
5021da177e4SLinus Torvalds 		break;
5031da177e4SLinus Torvalds 	case jalx_op:
504102cedc3SLeonid Yegoshin 		set_isa16_mode(bit);
5052a14b21aSAleksandar Markovic 		/* fall through */
506102cedc3SLeonid Yegoshin 	case jal_op:
507102cedc3SLeonid Yegoshin 		regs->regs[31] = regs->cp0_epc +
508102cedc3SLeonid Yegoshin 			dec_insn.pc_inc +
509102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc;
5102a14b21aSAleksandar Markovic 		/* fall through */
511102cedc3SLeonid Yegoshin 	case j_op:
512102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
513102cedc3SLeonid Yegoshin 		*contpc >>= 28;
514102cedc3SLeonid Yegoshin 		*contpc <<= 28;
515102cedc3SLeonid Yegoshin 		*contpc |= (insn.j_format.target << 2);
516102cedc3SLeonid Yegoshin 		/* Set microMIPS mode bit: XOR for jalx. */
517102cedc3SLeonid Yegoshin 		*contpc ^= bit;
5181da177e4SLinus Torvalds 		return 1;
519102cedc3SLeonid Yegoshin 	case beql_op:
520319824eaSMarkos Chandras 		if (NO_R6EMU)
521319824eaSMarkos Chandras 			break;
5222a14b21aSAleksandar Markovic 		/* fall through */
523319824eaSMarkos Chandras 	case beq_op:
524102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] ==
525102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
526102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
527102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
528102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
529102cedc3SLeonid Yegoshin 		else
530102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
531102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
532102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
533102cedc3SLeonid Yegoshin 		return 1;
534102cedc3SLeonid Yegoshin 	case bnel_op:
535319824eaSMarkos Chandras 		if (NO_R6EMU)
536319824eaSMarkos Chandras 			break;
5372a14b21aSAleksandar Markovic 		/* fall through */
538319824eaSMarkos Chandras 	case bne_op:
539102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] !=
540102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
541102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
542102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
543102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
544102cedc3SLeonid Yegoshin 		else
545102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
546102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
547102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
548102cedc3SLeonid Yegoshin 		return 1;
549102cedc3SLeonid Yegoshin 	case blezl_op:
550e9d92d22SMarkos Chandras 		if (!insn.i_format.rt && NO_R6EMU)
551319824eaSMarkos Chandras 			break;
5522a14b21aSAleksandar Markovic 		/* fall through */
553319824eaSMarkos Chandras 	case blez_op:
554a8ff66f5SMarkos Chandras 
555a8ff66f5SMarkos Chandras 		/*
556a8ff66f5SMarkos Chandras 		 * Compact branches for R6 for the
557a8ff66f5SMarkos Chandras 		 * blez and blezl opcodes.
558a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = 0 | rt != 0  == BLEZALC
559a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = rt != 0      == BGEZALC
560a8ff66f5SMarkos Chandras 		 * BLEZ  | rs != 0 | rt != 0 == BGEUC
561a8ff66f5SMarkos Chandras 		 * BLEZL | rs = 0 | rt != 0  == BLEZC
562a8ff66f5SMarkos Chandras 		 * BLEZL | rs = rt != 0      == BGEZC
563a8ff66f5SMarkos Chandras 		 * BLEZL | rs != 0 | rt != 0 == BGEC
564a8ff66f5SMarkos Chandras 		 *
565a8ff66f5SMarkos Chandras 		 * For real BLEZ{,L}, rt is always 0.
566a8ff66f5SMarkos Chandras 		 */
567a8ff66f5SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
568a8ff66f5SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
569a8ff66f5SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
570a8ff66f5SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
571a8ff66f5SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
572a8ff66f5SMarkos Chandras 					dec_insn.pc_inc;
573a8ff66f5SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
574a8ff66f5SMarkos Chandras 				dec_insn.next_pc_inc;
575a8ff66f5SMarkos Chandras 
576a8ff66f5SMarkos Chandras 			return 1;
577a8ff66f5SMarkos Chandras 		}
578102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] <= 0)
579102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
580102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
581102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
582102cedc3SLeonid Yegoshin 		else
583102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
584102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
585102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
586102cedc3SLeonid Yegoshin 		return 1;
587102cedc3SLeonid Yegoshin 	case bgtzl_op:
588e9d92d22SMarkos Chandras 		if (!insn.i_format.rt && NO_R6EMU)
589319824eaSMarkos Chandras 			break;
5902a14b21aSAleksandar Markovic 		/* fall through */
591319824eaSMarkos Chandras 	case bgtz_op:
592f1b44067SMarkos Chandras 		/*
593f1b44067SMarkos Chandras 		 * Compact branches for R6 for the
594f1b44067SMarkos Chandras 		 * bgtz and bgtzl opcodes.
595f1b44067SMarkos Chandras 		 * BGTZ  | rs = 0 | rt != 0  == BGTZALC
596f1b44067SMarkos Chandras 		 * BGTZ  | rs = rt != 0      == BLTZALC
597f1b44067SMarkos Chandras 		 * BGTZ  | rs != 0 | rt != 0 == BLTUC
598f1b44067SMarkos Chandras 		 * BGTZL | rs = 0 | rt != 0  == BGTZC
599f1b44067SMarkos Chandras 		 * BGTZL | rs = rt != 0      == BLTZC
600f1b44067SMarkos Chandras 		 * BGTZL | rs != 0 | rt != 0 == BLTC
601f1b44067SMarkos Chandras 		 *
602f1b44067SMarkos Chandras 		 * *ZALC varint for BGTZ &&& rt != 0
603f1b44067SMarkos Chandras 		 * For real GTZ{,L}, rt is always 0.
604f1b44067SMarkos Chandras 		 */
605f1b44067SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
606f1b44067SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
607f1b44067SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
608f1b44067SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
609f1b44067SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
610f1b44067SMarkos Chandras 					dec_insn.pc_inc;
611f1b44067SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
612f1b44067SMarkos Chandras 				dec_insn.next_pc_inc;
613f1b44067SMarkos Chandras 
614f1b44067SMarkos Chandras 			return 1;
615f1b44067SMarkos Chandras 		}
616f1b44067SMarkos Chandras 
617102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] > 0)
618102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
619102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
620102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
621102cedc3SLeonid Yegoshin 		else
622102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
623102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
624102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
625102cedc3SLeonid Yegoshin 		return 1;
6261b492600SPaul Burton 	case pop10_op:
6271b492600SPaul Burton 	case pop30_op:
628c893ce38SMarkos Chandras 		if (!cpu_has_mips_r6)
629c893ce38SMarkos Chandras 			break;
630c893ce38SMarkos Chandras 		if (insn.i_format.rt && !insn.i_format.rs)
631c893ce38SMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
632c893ce38SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
633c893ce38SMarkos Chandras 			dec_insn.next_pc_inc;
634c893ce38SMarkos Chandras 
635c893ce38SMarkos Chandras 		return 1;
636c26d4219SDavid Daney #ifdef CONFIG_CPU_CAVIUM_OCTEON
637c26d4219SDavid Daney 	case lwc2_op: /* This is bbit0 on Octeon */
638c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
639c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
640c26d4219SDavid Daney 		else
641c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
642c26d4219SDavid Daney 		return 1;
643c26d4219SDavid Daney 	case ldc2_op: /* This is bbit032 on Octeon */
644c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
645c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
646c26d4219SDavid Daney 		else
647c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
648c26d4219SDavid Daney 		return 1;
649c26d4219SDavid Daney 	case swc2_op: /* This is bbit1 on Octeon */
650c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
651c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
652c26d4219SDavid Daney 		else
653c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
654c26d4219SDavid Daney 		return 1;
655c26d4219SDavid Daney 	case sdc2_op: /* This is bbit132 on Octeon */
656c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
657c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
658c26d4219SDavid Daney 		else
659c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
660c26d4219SDavid Daney 		return 1;
6618467ca01SMarkos Chandras #else
6628467ca01SMarkos Chandras 	case bc6_op:
6638467ca01SMarkos Chandras 		/*
6648467ca01SMarkos Chandras 		 * Only valid for MIPS R6 but we can still end up
6658467ca01SMarkos Chandras 		 * here from a broken userland so just tell emulator
6668467ca01SMarkos Chandras 		 * this is not a branch and let it break later on.
6678467ca01SMarkos Chandras 		 */
6688467ca01SMarkos Chandras 		if  (!cpu_has_mips_r6)
6698467ca01SMarkos Chandras 			break;
6708467ca01SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
6718467ca01SMarkos Chandras 			dec_insn.next_pc_inc;
6728467ca01SMarkos Chandras 
6738467ca01SMarkos Chandras 		return 1;
67484fef630SMarkos Chandras 	case balc6_op:
67584fef630SMarkos Chandras 		if (!cpu_has_mips_r6)
67684fef630SMarkos Chandras 			break;
67784fef630SMarkos Chandras 		regs->regs[31] = regs->cp0_epc + 4;
67884fef630SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
67984fef630SMarkos Chandras 			dec_insn.next_pc_inc;
68084fef630SMarkos Chandras 
68184fef630SMarkos Chandras 		return 1;
6821c66b79bSPaul Burton 	case pop66_op:
68369b9a2fdSMarkos Chandras 		if (!cpu_has_mips_r6)
68469b9a2fdSMarkos Chandras 			break;
68569b9a2fdSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
68669b9a2fdSMarkos Chandras 			dec_insn.next_pc_inc;
68769b9a2fdSMarkos Chandras 
68869b9a2fdSMarkos Chandras 		return 1;
6891c66b79bSPaul Burton 	case pop76_op:
69028d6f93dSMarkos Chandras 		if (!cpu_has_mips_r6)
69128d6f93dSMarkos Chandras 			break;
69228d6f93dSMarkos Chandras 		if (!insn.i_format.rs)
69328d6f93dSMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
69428d6f93dSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
69528d6f93dSMarkos Chandras 			dec_insn.next_pc_inc;
69628d6f93dSMarkos Chandras 
69728d6f93dSMarkos Chandras 		return 1;
698c26d4219SDavid Daney #endif
6991da177e4SLinus Torvalds 	case cop0_op:
7001da177e4SLinus Torvalds 	case cop1_op:
701c8a34581SMarkos Chandras 		/* Need to check for R6 bc1nez and bc1eqz branches */
702c8a34581SMarkos Chandras 		if (cpu_has_mips_r6 &&
703c8a34581SMarkos Chandras 		    ((insn.i_format.rs == bc1eqz_op) ||
704c8a34581SMarkos Chandras 		     (insn.i_format.rs == bc1nez_op))) {
705c8a34581SMarkos Chandras 			bit = 0;
7068bcd84a4SDouglas Leung 			fpr = &current->thread.fpu.fpr[insn.i_format.rt];
7078bcd84a4SDouglas Leung 			bit0 = get_fpr32(fpr, 0) & 0x1;
708c8a34581SMarkos Chandras 			switch (insn.i_format.rs) {
709c8a34581SMarkos Chandras 			case bc1eqz_op:
7108bcd84a4SDouglas Leung 				bit = bit0 == 0;
711c8a34581SMarkos Chandras 				break;
712c8a34581SMarkos Chandras 			case bc1nez_op:
7138bcd84a4SDouglas Leung 				bit = bit0 != 0;
714c8a34581SMarkos Chandras 				break;
715c8a34581SMarkos Chandras 			}
716c8a34581SMarkos Chandras 			if (bit)
717c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
718c8a34581SMarkos Chandras 					dec_insn.pc_inc +
719c8a34581SMarkos Chandras 					(insn.i_format.simmediate << 2);
720c8a34581SMarkos Chandras 			else
721c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
722c8a34581SMarkos Chandras 					dec_insn.pc_inc +
723c8a34581SMarkos Chandras 					dec_insn.next_pc_inc;
724c8a34581SMarkos Chandras 
725c8a34581SMarkos Chandras 			return 1;
726c8a34581SMarkos Chandras 		}
7272a14b21aSAleksandar Markovic 		/* R2/R6 compatible cop1 instruction */
7282a14b21aSAleksandar Markovic 		/* fall through */
7291da177e4SLinus Torvalds 	case cop2_op:
7301da177e4SLinus Torvalds 	case cop1x_op:
731102cedc3SLeonid Yegoshin 		if (insn.i_format.rs == bc_op) {
732102cedc3SLeonid Yegoshin 			preempt_disable();
733102cedc3SLeonid Yegoshin 			if (is_fpu_owner())
734842dfc11SManuel Lauss 			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
735102cedc3SLeonid Yegoshin 			else
736102cedc3SLeonid Yegoshin 				fcr31 = current->thread.fpu.fcr31;
737102cedc3SLeonid Yegoshin 			preempt_enable();
738102cedc3SLeonid Yegoshin 
739102cedc3SLeonid Yegoshin 			bit = (insn.i_format.rt >> 2);
740102cedc3SLeonid Yegoshin 			bit += (bit != 0);
741102cedc3SLeonid Yegoshin 			bit += 23;
742102cedc3SLeonid Yegoshin 			switch (insn.i_format.rt & 3) {
743102cedc3SLeonid Yegoshin 			case 0:	/* bc1f */
744102cedc3SLeonid Yegoshin 			case 2:	/* bc1fl */
745102cedc3SLeonid Yegoshin 				if (~fcr31 & (1 << bit))
746102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
747102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
748102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
749102cedc3SLeonid Yegoshin 				else
750102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
751102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
752102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
753102cedc3SLeonid Yegoshin 				return 1;
754102cedc3SLeonid Yegoshin 			case 1:	/* bc1t */
755102cedc3SLeonid Yegoshin 			case 3:	/* bc1tl */
756102cedc3SLeonid Yegoshin 				if (fcr31 & (1 << bit))
757102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
758102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
759102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
760102cedc3SLeonid Yegoshin 				else
761102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
762102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
763102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
7641da177e4SLinus Torvalds 				return 1;
7651da177e4SLinus Torvalds 			}
766102cedc3SLeonid Yegoshin 		}
767102cedc3SLeonid Yegoshin 		break;
768102cedc3SLeonid Yegoshin 	}
7691da177e4SLinus Torvalds 	return 0;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds /*
7731da177e4SLinus Torvalds  * In the Linux kernel, we support selection of FPR format on the
774da0bac33SDavid Daney  * basis of the Status.FR bit.	If an FPU is not present, the FR bit
775da0bac33SDavid Daney  * is hardwired to zero, which would imply a 32-bit FPU even for
776597ce172SPaul Burton  * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
77751d943f0SRalf Baechle  * FPU emu is slow and bulky and optimizing this function offers fairly
77851d943f0SRalf Baechle  * sizeable benefits so we try to be clever and make this function return
77951d943f0SRalf Baechle  * a constant whenever possible, that is on 64-bit kernels without O32
780597ce172SPaul Burton  * compatibility enabled and on 32-bit without 64-bit FPU support.
7811da177e4SLinus Torvalds  */
782da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp)
783da0bac33SDavid Daney {
78497f2645fSMasahiro Yamada 	if (IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_MIPS32_O32))
78551d943f0SRalf Baechle 		return 1;
78697f2645fSMasahiro Yamada 	else if (IS_ENABLED(CONFIG_32BIT) &&
78797f2645fSMasahiro Yamada 		 !IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
788da0bac33SDavid Daney 		return 0;
78908a07904SRalf Baechle 
790597ce172SPaul Burton 	return !test_thread_flag(TIF_32BIT_FPREGS);
791da0bac33SDavid Daney }
7921da177e4SLinus Torvalds 
7934227a2d4SPaul Burton static inline bool hybrid_fprs(void)
7944227a2d4SPaul Burton {
7954227a2d4SPaul Burton 	return test_thread_flag(TIF_HYBRID_FPREGS);
7964227a2d4SPaul Burton }
7974227a2d4SPaul Burton 
79847fa0c02SRalf Baechle #define SIFROMREG(si, x)						\
79947fa0c02SRalf Baechle do {									\
8004227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs())				\
801c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[x], 0);			\
802bbd426f5SPaul Burton 	else								\
803c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);	\
804bbd426f5SPaul Burton } while (0)
805da0bac33SDavid Daney 
80647fa0c02SRalf Baechle #define SITOREG(si, x)							\
80747fa0c02SRalf Baechle do {									\
8084227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs()) {			\
809a58f85b5SAleksandar Markovic 		unsigned int i;						\
810bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[x], 0, si);				\
811ef1c47afSPaul Burton 		for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)	\
812ef1c47afSPaul Burton 			set_fpr32(&ctx->fpr[x], i, 0);			\
813ef1c47afSPaul Burton 	} else {							\
814bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);		\
815ef1c47afSPaul Burton 	}								\
816bbd426f5SPaul Burton } while (0)
8171da177e4SLinus Torvalds 
818c8c0da6bSPaul Burton #define SIFROMHREG(si, x)	((si) = (int)get_fpr32(&ctx->fpr[x], 1))
819ef1c47afSPaul Burton 
82047fa0c02SRalf Baechle #define SITOHREG(si, x)							\
82147fa0c02SRalf Baechle do {									\
822a58f85b5SAleksandar Markovic 	unsigned int i;							\
823ef1c47afSPaul Burton 	set_fpr32(&ctx->fpr[x], 1, si);					\
824ef1c47afSPaul Burton 	for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)		\
825ef1c47afSPaul Burton 		set_fpr32(&ctx->fpr[x], i, 0);				\
826ef1c47afSPaul Burton } while (0)
8271ac94400SLeonid Yegoshin 
828bbd426f5SPaul Burton #define DIFROMREG(di, x)						\
8298535f2baSManuel Lauss 	((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) ^ 1)], 0))
830bbd426f5SPaul Burton 
83147fa0c02SRalf Baechle #define DITOREG(di, x)							\
83247fa0c02SRalf Baechle do {									\
833a58f85b5SAleksandar Markovic 	unsigned int fpr, i;						\
8348535f2baSManuel Lauss 	fpr = (x) & ~(cop1_64bit(xcp) ^ 1);				\
835ef1c47afSPaul Burton 	set_fpr64(&ctx->fpr[fpr], 0, di);				\
836ef1c47afSPaul Burton 	for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++)		\
837ef1c47afSPaul Burton 		set_fpr64(&ctx->fpr[fpr], i, 0);			\
838ef1c47afSPaul Burton } while (0)
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
8411da177e4SLinus Torvalds #define SPTOREG(sp, x)	SITOREG((sp).bits, x)
8421da177e4SLinus Torvalds #define DPFROMREG(dp, x)	DIFROMREG((dp).bits, x)
8431da177e4SLinus Torvalds #define DPTOREG(dp, x)	DITOREG((dp).bits, x)
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds /*
846d4f5b088SMaciej W. Rozycki  * Emulate a CFC1 instruction.
847d4f5b088SMaciej W. Rozycki  */
848d4f5b088SMaciej W. Rozycki static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
849d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
850d4f5b088SMaciej W. Rozycki {
851c491cfa2SMaciej W. Rozycki 	u32 fcr31 = ctx->fcr31;
852c491cfa2SMaciej W. Rozycki 	u32 value = 0;
853d4f5b088SMaciej W. Rozycki 
854c491cfa2SMaciej W. Rozycki 	switch (MIPSInst_RD(ir)) {
855c491cfa2SMaciej W. Rozycki 	case FPCREG_CSR:
856c491cfa2SMaciej W. Rozycki 		value = fcr31;
857d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-csr=%08x\n",
858c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
859c491cfa2SMaciej W. Rozycki 		break;
860c491cfa2SMaciej W. Rozycki 
861c491cfa2SMaciej W. Rozycki 	case FPCREG_FENR:
862c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
863c491cfa2SMaciej W. Rozycki 			break;
864c491cfa2SMaciej W. Rozycki 		value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
865c491cfa2SMaciej W. Rozycki 			MIPS_FENR_FS;
866c491cfa2SMaciej W. Rozycki 		value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
867c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-enr=%08x\n",
868c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
869c491cfa2SMaciej W. Rozycki 		break;
870c491cfa2SMaciej W. Rozycki 
871c491cfa2SMaciej W. Rozycki 	case FPCREG_FEXR:
872c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
873c491cfa2SMaciej W. Rozycki 			break;
874c491cfa2SMaciej W. Rozycki 		value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
875c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-exr=%08x\n",
876c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
877c491cfa2SMaciej W. Rozycki 		break;
878c491cfa2SMaciej W. Rozycki 
879c491cfa2SMaciej W. Rozycki 	case FPCREG_FCCR:
880c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
881c491cfa2SMaciej W. Rozycki 			break;
882c491cfa2SMaciej W. Rozycki 		value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
883c491cfa2SMaciej W. Rozycki 			MIPS_FCCR_COND0;
884c491cfa2SMaciej W. Rozycki 		value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
885c491cfa2SMaciej W. Rozycki 			 (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
886c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-ccr=%08x\n",
887c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
888c491cfa2SMaciej W. Rozycki 		break;
889c491cfa2SMaciej W. Rozycki 
890c491cfa2SMaciej W. Rozycki 	case FPCREG_RID:
89103dce595SMaciej W. Rozycki 		value = boot_cpu_data.fpu_id;
892c491cfa2SMaciej W. Rozycki 		break;
893c491cfa2SMaciej W. Rozycki 
894c491cfa2SMaciej W. Rozycki 	default:
895c491cfa2SMaciej W. Rozycki 		break;
896c491cfa2SMaciej W. Rozycki 	}
897c491cfa2SMaciej W. Rozycki 
898d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir))
899d4f5b088SMaciej W. Rozycki 		xcp->regs[MIPSInst_RT(ir)] = value;
900d4f5b088SMaciej W. Rozycki }
901d4f5b088SMaciej W. Rozycki 
902d4f5b088SMaciej W. Rozycki /*
903d4f5b088SMaciej W. Rozycki  * Emulate a CTC1 instruction.
904d4f5b088SMaciej W. Rozycki  */
905d4f5b088SMaciej W. Rozycki static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
906d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
907d4f5b088SMaciej W. Rozycki {
908c491cfa2SMaciej W. Rozycki 	u32 fcr31 = ctx->fcr31;
909d4f5b088SMaciej W. Rozycki 	u32 value;
9109b26616cSMaciej W. Rozycki 	u32 mask;
911d4f5b088SMaciej W. Rozycki 
912d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir) == 0)
913d4f5b088SMaciej W. Rozycki 		value = 0;
914d4f5b088SMaciej W. Rozycki 	else
915d4f5b088SMaciej W. Rozycki 		value = xcp->regs[MIPSInst_RT(ir)];
916d4f5b088SMaciej W. Rozycki 
917c491cfa2SMaciej W. Rozycki 	switch (MIPSInst_RD(ir)) {
918c491cfa2SMaciej W. Rozycki 	case FPCREG_CSR:
919d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->csr=%08x\n",
920c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
921d4f5b088SMaciej W. Rozycki 
9229b26616cSMaciej W. Rozycki 		/* Preserve read-only bits.  */
92303dce595SMaciej W. Rozycki 		mask = boot_cpu_data.fpu_msk31;
9249b26616cSMaciej W. Rozycki 		fcr31 = (value & ~mask) | (fcr31 & mask);
925c491cfa2SMaciej W. Rozycki 		break;
926c491cfa2SMaciej W. Rozycki 
927c491cfa2SMaciej W. Rozycki 	case FPCREG_FENR:
928c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
929c491cfa2SMaciej W. Rozycki 			break;
930c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->enr=%08x\n",
931c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
932c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
933c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
934c491cfa2SMaciej W. Rozycki 			 FPU_CSR_FS;
935c491cfa2SMaciej W. Rozycki 		fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
936c491cfa2SMaciej W. Rozycki 		break;
937c491cfa2SMaciej W. Rozycki 
938c491cfa2SMaciej W. Rozycki 	case FPCREG_FEXR:
939c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
940c491cfa2SMaciej W. Rozycki 			break;
941c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->exr=%08x\n",
942c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
943c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
944c491cfa2SMaciej W. Rozycki 		fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
945c491cfa2SMaciej W. Rozycki 		break;
946c491cfa2SMaciej W. Rozycki 
947c491cfa2SMaciej W. Rozycki 	case FPCREG_FCCR:
948c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
949c491cfa2SMaciej W. Rozycki 			break;
950c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->ccr=%08x\n",
951c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
952c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
953c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
954c491cfa2SMaciej W. Rozycki 			 FPU_CSR_COND;
955c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
956c491cfa2SMaciej W. Rozycki 			 FPU_CSR_CONDX;
957c491cfa2SMaciej W. Rozycki 		break;
958c491cfa2SMaciej W. Rozycki 
959c491cfa2SMaciej W. Rozycki 	default:
960c491cfa2SMaciej W. Rozycki 		break;
961d4f5b088SMaciej W. Rozycki 	}
962c491cfa2SMaciej W. Rozycki 
963c491cfa2SMaciej W. Rozycki 	ctx->fcr31 = fcr31;
964d4f5b088SMaciej W. Rozycki }
965d4f5b088SMaciej W. Rozycki 
966d4f5b088SMaciej W. Rozycki /*
9671da177e4SLinus Torvalds  * Emulate the single floating point instruction pointed at by EPC.
9681da177e4SLinus Torvalds  * Two instructions if the instruction is in a branch delay slot.
9691da177e4SLinus Torvalds  */
9701da177e4SLinus Torvalds 
971515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
972445a58ceSPaul Burton 		struct mm_decoded_insn dec_insn, void __user **fault_addr)
9731da177e4SLinus Torvalds {
974102cedc3SLeonid Yegoshin 	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
97593583e17SPaul Burton 	unsigned int cond, cbit, bit0;
9763f7cac41SRalf Baechle 	mips_instruction ir;
9773f7cac41SRalf Baechle 	int likely, pc_inc;
97893583e17SPaul Burton 	union fpureg *fpr;
9793f7cac41SRalf Baechle 	u32 __user *wva;
9803f7cac41SRalf Baechle 	u64 __user *dva;
9813f7cac41SRalf Baechle 	u32 wval;
9823f7cac41SRalf Baechle 	u64 dval;
9833f7cac41SRalf Baechle 	int sig;
9841da177e4SLinus Torvalds 
98570e4c234SRalf Baechle 	/*
98670e4c234SRalf Baechle 	 * These are giving gcc a gentle hint about what to expect in
98770e4c234SRalf Baechle 	 * dec_inst in order to do better optimization.
98870e4c234SRalf Baechle 	 */
98970e4c234SRalf Baechle 	if (!cpu_has_mmips && dec_insn.micro_mips_mode)
99070e4c234SRalf Baechle 		unreachable();
99170e4c234SRalf Baechle 
9921da177e4SLinus Torvalds 	/* XXX NEC Vr54xx bug workaround */
993e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
994102cedc3SLeonid Yegoshin 		if (dec_insn.micro_mips_mode) {
995102cedc3SLeonid Yegoshin 			if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
996e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
997102cedc3SLeonid Yegoshin 		} else {
998102cedc3SLeonid Yegoshin 			if (!isBranchInstr(xcp, dec_insn, &contpc))
999e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
1000102cedc3SLeonid Yegoshin 		}
1001102cedc3SLeonid Yegoshin 	}
10021da177e4SLinus Torvalds 
1003e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
10041da177e4SLinus Torvalds 		/*
10051da177e4SLinus Torvalds 		 * The instruction to be emulated is in a branch delay slot
10061da177e4SLinus Torvalds 		 * which means that we have to	emulate the branch instruction
10071da177e4SLinus Torvalds 		 * BEFORE we do the cop1 instruction.
10081da177e4SLinus Torvalds 		 *
10091da177e4SLinus Torvalds 		 * This branch could be a COP1 branch, but in that case we
10101da177e4SLinus Torvalds 		 * would have had a trap for that instruction, and would not
10111da177e4SLinus Torvalds 		 * come through this route.
10121da177e4SLinus Torvalds 		 *
10131da177e4SLinus Torvalds 		 * Linux MIPS branch emulator operates on context, updating the
10141da177e4SLinus Torvalds 		 * cp0_epc.
10151da177e4SLinus Torvalds 		 */
1016102cedc3SLeonid Yegoshin 		ir = dec_insn.next_insn;  /* process delay slot instr */
1017102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.next_pc_inc;
1018333d1f67SRalf Baechle 	} else {
1019102cedc3SLeonid Yegoshin 		ir = dec_insn.insn;       /* process current instr */
1020102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.pc_inc;
1021102cedc3SLeonid Yegoshin 	}
1022102cedc3SLeonid Yegoshin 
1023102cedc3SLeonid Yegoshin 	/*
1024102cedc3SLeonid Yegoshin 	 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
1025102cedc3SLeonid Yegoshin 	 * instructions, we want to convert microMIPS FPU instructions
1026102cedc3SLeonid Yegoshin 	 * into MIPS32 instructions so that we could reuse all of the
1027102cedc3SLeonid Yegoshin 	 * FPU emulation code.
1028102cedc3SLeonid Yegoshin 	 *
1029102cedc3SLeonid Yegoshin 	 * NOTE: We cannot do this for branch instructions since they
1030102cedc3SLeonid Yegoshin 	 *       are not a subset. Example: Cannot emulate a 16-bit
1031102cedc3SLeonid Yegoshin 	 *       aligned target address with a MIPS32 instruction.
1032102cedc3SLeonid Yegoshin 	 */
1033102cedc3SLeonid Yegoshin 	if (dec_insn.micro_mips_mode) {
1034102cedc3SLeonid Yegoshin 		/*
1035102cedc3SLeonid Yegoshin 		 * If next instruction is a 16-bit instruction, then it
1036102cedc3SLeonid Yegoshin 		 * it cannot be a FPU instruction. This could happen
1037102cedc3SLeonid Yegoshin 		 * since we can be called for non-FPU instructions.
1038102cedc3SLeonid Yegoshin 		 */
1039102cedc3SLeonid Yegoshin 		if ((pc_inc == 2) ||
1040102cedc3SLeonid Yegoshin 			(microMIPS32_to_MIPS32((union mips_instruction *)&ir)
1041102cedc3SLeonid Yegoshin 			 == SIGILL))
1042102cedc3SLeonid Yegoshin 			return SIGILL;
10431da177e4SLinus Torvalds 	}
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds emul:
1046a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
1047b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(emulated);
10481da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(ir)) {
10493f7cac41SRalf Baechle 	case ldc1_op:
10503f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10511da177e4SLinus Torvalds 				     MIPSInst_SIMM(ir));
1052b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
1053515b029dSDavid Daney 
105496d4f267SLinus Torvalds 		if (!access_ok(dva, sizeof(u64))) {
1055b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10563f7cac41SRalf Baechle 			*fault_addr = dva;
10571da177e4SLinus Torvalds 			return SIGBUS;
10581da177e4SLinus Torvalds 		}
10593f7cac41SRalf Baechle 		if (__get_user(dval, dva)) {
1060515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10613f7cac41SRalf Baechle 			*fault_addr = dva;
1062515b029dSDavid Daney 			return SIGSEGV;
1063515b029dSDavid Daney 		}
10643f7cac41SRalf Baechle 		DITOREG(dval, MIPSInst_RT(ir));
10651da177e4SLinus Torvalds 		break;
10661da177e4SLinus Torvalds 
10673f7cac41SRalf Baechle 	case sdc1_op:
10683f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10691da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1070b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
10713f7cac41SRalf Baechle 		DIFROMREG(dval, MIPSInst_RT(ir));
107296d4f267SLinus Torvalds 		if (!access_ok(dva, sizeof(u64))) {
1073b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10743f7cac41SRalf Baechle 			*fault_addr = dva;
10751da177e4SLinus Torvalds 			return SIGBUS;
10761da177e4SLinus Torvalds 		}
10773f7cac41SRalf Baechle 		if (__put_user(dval, dva)) {
1078515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10793f7cac41SRalf Baechle 			*fault_addr = dva;
1080515b029dSDavid Daney 			return SIGSEGV;
1081515b029dSDavid Daney 		}
10821da177e4SLinus Torvalds 		break;
10831da177e4SLinus Torvalds 
10843f7cac41SRalf Baechle 	case lwc1_op:
10853f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10861da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1087b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
108896d4f267SLinus Torvalds 		if (!access_ok(wva, sizeof(u32))) {
1089b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10903f7cac41SRalf Baechle 			*fault_addr = wva;
10911da177e4SLinus Torvalds 			return SIGBUS;
10921da177e4SLinus Torvalds 		}
10933f7cac41SRalf Baechle 		if (__get_user(wval, wva)) {
1094515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10953f7cac41SRalf Baechle 			*fault_addr = wva;
1096515b029dSDavid Daney 			return SIGSEGV;
1097515b029dSDavid Daney 		}
10983f7cac41SRalf Baechle 		SITOREG(wval, MIPSInst_RT(ir));
10991da177e4SLinus Torvalds 		break;
11001da177e4SLinus Torvalds 
11013f7cac41SRalf Baechle 	case swc1_op:
11023f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
11031da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1104b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
11053f7cac41SRalf Baechle 		SIFROMREG(wval, MIPSInst_RT(ir));
110696d4f267SLinus Torvalds 		if (!access_ok(wva, sizeof(u32))) {
1107b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11083f7cac41SRalf Baechle 			*fault_addr = wva;
11091da177e4SLinus Torvalds 			return SIGBUS;
11101da177e4SLinus Torvalds 		}
11113f7cac41SRalf Baechle 		if (__put_user(wval, wva)) {
1112515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11133f7cac41SRalf Baechle 			*fault_addr = wva;
1114515b029dSDavid Daney 			return SIGSEGV;
1115515b029dSDavid Daney 		}
11161da177e4SLinus Torvalds 		break;
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	case cop1_op:
11191da177e4SLinus Torvalds 		switch (MIPSInst_RS(ir)) {
11201da177e4SLinus Torvalds 		case dmfc_op:
112108a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
112208a07904SRalf Baechle 				return SIGILL;
112308a07904SRalf Baechle 
11241da177e4SLinus Torvalds 			/* copregister fs -> gpr[rt] */
11251da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
11261da177e4SLinus Torvalds 				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
11271da177e4SLinus Torvalds 					MIPSInst_RD(ir));
11281da177e4SLinus Torvalds 			}
11291da177e4SLinus Torvalds 			break;
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 		case dmtc_op:
113208a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
113308a07904SRalf Baechle 				return SIGILL;
113408a07904SRalf Baechle 
11351da177e4SLinus Torvalds 			/* copregister fs <- rt */
11361da177e4SLinus Torvalds 			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11371da177e4SLinus Torvalds 			break;
11381da177e4SLinus Torvalds 
11391ac94400SLeonid Yegoshin 		case mfhc_op:
1140e8f80cc1SMarkos Chandras 			if (!cpu_has_mips_r2_r6)
114170f743d1SMaciej W. Rozycki 				return SIGILL;
11421ac94400SLeonid Yegoshin 
11431ac94400SLeonid Yegoshin 			/* copregister rd -> gpr[rt] */
11441ac94400SLeonid Yegoshin 			if (MIPSInst_RT(ir) != 0) {
11451ac94400SLeonid Yegoshin 				SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
11461ac94400SLeonid Yegoshin 					MIPSInst_RD(ir));
11471ac94400SLeonid Yegoshin 			}
11481ac94400SLeonid Yegoshin 			break;
11491ac94400SLeonid Yegoshin 
11501ac94400SLeonid Yegoshin 		case mthc_op:
1151e8f80cc1SMarkos Chandras 			if (!cpu_has_mips_r2_r6)
115270f743d1SMaciej W. Rozycki 				return SIGILL;
11531ac94400SLeonid Yegoshin 
11541ac94400SLeonid Yegoshin 			/* copregister rd <- gpr[rt] */
11551ac94400SLeonid Yegoshin 			SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11561ac94400SLeonid Yegoshin 			break;
11571ac94400SLeonid Yegoshin 
11581da177e4SLinus Torvalds 		case mfc_op:
11591da177e4SLinus Torvalds 			/* copregister rd -> gpr[rt] */
11601da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
11611da177e4SLinus Torvalds 				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
11621da177e4SLinus Torvalds 					MIPSInst_RD(ir));
11631da177e4SLinus Torvalds 			}
11641da177e4SLinus Torvalds 			break;
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 		case mtc_op:
11671da177e4SLinus Torvalds 			/* copregister rd <- rt */
11681da177e4SLinus Torvalds 			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11691da177e4SLinus Torvalds 			break;
11701da177e4SLinus Torvalds 
11713f7cac41SRalf Baechle 		case cfc_op:
11721da177e4SLinus Torvalds 			/* cop control register rd -> gpr[rt] */
1173d4f5b088SMaciej W. Rozycki 			cop1_cfc(xcp, ctx, ir);
11741da177e4SLinus Torvalds 			break;
11751da177e4SLinus Torvalds 
11763f7cac41SRalf Baechle 		case ctc_op:
11771da177e4SLinus Torvalds 			/* copregister rd <- rt */
1178d4f5b088SMaciej W. Rozycki 			cop1_ctc(xcp, ctx, ir);
11791da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
11801da177e4SLinus Torvalds 				return SIGFPE;
11811da177e4SLinus Torvalds 			}
11821da177e4SLinus Torvalds 			break;
11831da177e4SLinus Torvalds 
1184c909ca71SMarkos Chandras 		case bc1eqz_op:
1185c909ca71SMarkos Chandras 		case bc1nez_op:
1186c909ca71SMarkos Chandras 			if (!cpu_has_mips_r6 || delay_slot(xcp))
1187c909ca71SMarkos Chandras 				return SIGILL;
1188c909ca71SMarkos Chandras 
118961100500SAleksandar Markovic 			likely = 0;
119061100500SAleksandar Markovic 			cond = 0;
119193583e17SPaul Burton 			fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
119293583e17SPaul Burton 			bit0 = get_fpr32(fpr, 0) & 0x1;
1193c909ca71SMarkos Chandras 			switch (MIPSInst_RS(ir)) {
1194c909ca71SMarkos Chandras 			case bc1eqz_op:
1195454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(bc1eqz);
119693583e17SPaul Burton 				cond = bit0 == 0;
1197c909ca71SMarkos Chandras 				break;
1198c909ca71SMarkos Chandras 			case bc1nez_op:
1199454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(bc1nez);
120093583e17SPaul Burton 				cond = bit0 != 0;
1201c909ca71SMarkos Chandras 				break;
1202c909ca71SMarkos Chandras 			}
1203c909ca71SMarkos Chandras 			goto branch_common;
1204c909ca71SMarkos Chandras 
12053f7cac41SRalf Baechle 		case bc_op:
1206e7e9cae5SRalf Baechle 			if (delay_slot(xcp))
12071da177e4SLinus Torvalds 				return SIGILL;
12081da177e4SLinus Torvalds 
120908a07904SRalf Baechle 			if (cpu_has_mips_4_5_r)
121008a07904SRalf Baechle 				cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
121108a07904SRalf Baechle 			else
121208a07904SRalf Baechle 				cbit = FPU_CSR_COND;
121308a07904SRalf Baechle 			cond = ctx->fcr31 & cbit;
121408a07904SRalf Baechle 
12153f7cac41SRalf Baechle 			likely = 0;
12161da177e4SLinus Torvalds 			switch (MIPSInst_RT(ir) & 3) {
12171da177e4SLinus Torvalds 			case bcfl_op:
12182d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
12191da177e4SLinus Torvalds 					likely = 1;
12202a14b21aSAleksandar Markovic 				/* fall through */
12211da177e4SLinus Torvalds 			case bcf_op:
12221da177e4SLinus Torvalds 				cond = !cond;
12231da177e4SLinus Torvalds 				break;
12241da177e4SLinus Torvalds 			case bctl_op:
12252d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
12261da177e4SLinus Torvalds 					likely = 1;
12272a14b21aSAleksandar Markovic 				/* fall through */
12281da177e4SLinus Torvalds 			case bct_op:
12291da177e4SLinus Torvalds 				break;
12301da177e4SLinus Torvalds 			}
1231c909ca71SMarkos Chandras branch_common:
1232ae5f3f5bSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(branches);
1233e7e9cae5SRalf Baechle 			set_delay_slot(xcp);
12341da177e4SLinus Torvalds 			if (cond) {
12353f7cac41SRalf Baechle 				/*
12363f7cac41SRalf Baechle 				 * Branch taken: emulate dslot instruction
12371da177e4SLinus Torvalds 				 */
12389ab4471cSMaciej W. Rozycki 				unsigned long bcpc;
12399ab4471cSMaciej W. Rozycki 
12409ab4471cSMaciej W. Rozycki 				/*
12419ab4471cSMaciej W. Rozycki 				 * Remember EPC at the branch to point back
12429ab4471cSMaciej W. Rozycki 				 * at so that any delay-slot instruction
12439ab4471cSMaciej W. Rozycki 				 * signal is not silently ignored.
12449ab4471cSMaciej W. Rozycki 				 */
12459ab4471cSMaciej W. Rozycki 				bcpc = xcp->cp0_epc;
1246102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
12471da177e4SLinus Torvalds 
1248102cedc3SLeonid Yegoshin 				contpc = MIPSInst_SIMM(ir);
1249102cedc3SLeonid Yegoshin 				ir = dec_insn.next_insn;
1250102cedc3SLeonid Yegoshin 				if (dec_insn.micro_mips_mode) {
1251102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 1));
1252102cedc3SLeonid Yegoshin 
1253102cedc3SLeonid Yegoshin 					/* If 16-bit instruction, not FPU. */
1254102cedc3SLeonid Yegoshin 					if ((dec_insn.next_pc_inc == 2) ||
1255102cedc3SLeonid Yegoshin 						(microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1256102cedc3SLeonid Yegoshin 
1257102cedc3SLeonid Yegoshin 						/*
1258102cedc3SLeonid Yegoshin 						 * Since this instruction will
1259102cedc3SLeonid Yegoshin 						 * be put on the stack with
1260102cedc3SLeonid Yegoshin 						 * 32-bit words, get around
1261102cedc3SLeonid Yegoshin 						 * this problem by putting a
1262102cedc3SLeonid Yegoshin 						 * NOP16 as the second one.
1263102cedc3SLeonid Yegoshin 						 */
1264102cedc3SLeonid Yegoshin 						if (dec_insn.next_pc_inc == 2)
1265102cedc3SLeonid Yegoshin 							ir = (ir & (~0xffff)) | MM_NOP16;
1266102cedc3SLeonid Yegoshin 
1267102cedc3SLeonid Yegoshin 						/*
1268102cedc3SLeonid Yegoshin 						 * Single step the non-CP1
1269102cedc3SLeonid Yegoshin 						 * instruction in the dslot.
1270102cedc3SLeonid Yegoshin 						 */
12719ab4471cSMaciej W. Rozycki 						sig = mips_dsemul(xcp, ir,
1272432c6bacSPaul Burton 								  bcpc, contpc);
1273e4553573SMaciej W. Rozycki 						if (sig < 0)
1274e4553573SMaciej W. Rozycki 							break;
12759ab4471cSMaciej W. Rozycki 						if (sig)
12769ab4471cSMaciej W. Rozycki 							xcp->cp0_epc = bcpc;
12779ab4471cSMaciej W. Rozycki 						/*
12789ab4471cSMaciej W. Rozycki 						 * SIGILL forces out of
12799ab4471cSMaciej W. Rozycki 						 * the emulation loop.
12809ab4471cSMaciej W. Rozycki 						 */
12819ab4471cSMaciej W. Rozycki 						return sig ? sig : SIGILL;
1282515b029dSDavid Daney 					}
1283102cedc3SLeonid Yegoshin 				} else
1284102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 2));
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 				switch (MIPSInst_OPCODE(ir)) {
12871da177e4SLinus Torvalds 				case lwc1_op:
12881da177e4SLinus Torvalds 				case swc1_op:
128908a07904SRalf Baechle 					goto emul;
12903f7cac41SRalf Baechle 
12911da177e4SLinus Torvalds 				case ldc1_op:
12921da177e4SLinus Torvalds 				case sdc1_op:
12932d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_2_3_4_5_r)
129408a07904SRalf Baechle 						goto emul;
129508a07904SRalf Baechle 
12969ab4471cSMaciej W. Rozycki 					goto bc_sigill;
12973f7cac41SRalf Baechle 
12981da177e4SLinus Torvalds 				case cop1_op:
129908a07904SRalf Baechle 					goto emul;
13003f7cac41SRalf Baechle 
13011da177e4SLinus Torvalds 				case cop1x_op:
13022d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_4_5_64_r2_r6)
13031da177e4SLinus Torvalds 						/* its one of ours */
13041da177e4SLinus Torvalds 						goto emul;
130508a07904SRalf Baechle 
13069ab4471cSMaciej W. Rozycki 					goto bc_sigill;
13073f7cac41SRalf Baechle 
13081da177e4SLinus Torvalds 				case spec_op:
13092d83fea7SMaciej W. Rozycki 					switch (MIPSInst_FUNC(ir)) {
13102d83fea7SMaciej W. Rozycki 					case movc_op:
13112d83fea7SMaciej W. Rozycki 						if (cpu_has_mips_4_5_r)
13121da177e4SLinus Torvalds 							goto emul;
13132d83fea7SMaciej W. Rozycki 
13149ab4471cSMaciej W. Rozycki 						goto bc_sigill;
13152d83fea7SMaciej W. Rozycki 					}
13161da177e4SLinus Torvalds 					break;
13179ab4471cSMaciej W. Rozycki 
13189ab4471cSMaciej W. Rozycki 				bc_sigill:
13199ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
13209ab4471cSMaciej W. Rozycki 					return SIGILL;
13211da177e4SLinus Torvalds 				}
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 				/*
13241da177e4SLinus Torvalds 				 * Single step the non-cp1
13251da177e4SLinus Torvalds 				 * instruction in the dslot
13261da177e4SLinus Torvalds 				 */
1327432c6bacSPaul Burton 				sig = mips_dsemul(xcp, ir, bcpc, contpc);
1328e4553573SMaciej W. Rozycki 				if (sig < 0)
1329e4553573SMaciej W. Rozycki 					break;
13309ab4471cSMaciej W. Rozycki 				if (sig)
13319ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
13329ab4471cSMaciej W. Rozycki 				/* SIGILL forces out of the emulation loop.  */
13339ab4471cSMaciej W. Rozycki 				return sig ? sig : SIGILL;
13343f7cac41SRalf Baechle 			} else if (likely) {	/* branch not taken */
13351da177e4SLinus Torvalds 				/*
13361da177e4SLinus Torvalds 				 * branch likely nullifies
13371da177e4SLinus Torvalds 				 * dslot if not taken
13381da177e4SLinus Torvalds 				 */
1339102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
1340102cedc3SLeonid Yegoshin 				contpc += dec_insn.pc_inc;
13411da177e4SLinus Torvalds 				/*
13421da177e4SLinus Torvalds 				 * else continue & execute
13431da177e4SLinus Torvalds 				 * dslot as normal insn
13441da177e4SLinus Torvalds 				 */
13451da177e4SLinus Torvalds 			}
13461da177e4SLinus Torvalds 			break;
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 		default:
13491da177e4SLinus Torvalds 			if (!(MIPSInst_RS(ir) & 0x10))
13501da177e4SLinus Torvalds 				return SIGILL;
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 			/* a real fpu computation instruction */
13538904d5b1SAleksandar Markovic 			sig = fpu_emu(xcp, ctx, ir);
13548904d5b1SAleksandar Markovic 			if (sig)
13551da177e4SLinus Torvalds 				return sig;
13561da177e4SLinus Torvalds 		}
13571da177e4SLinus Torvalds 		break;
13581da177e4SLinus Torvalds 
13593f7cac41SRalf Baechle 	case cop1x_op:
13602d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_4_5_64_r2_r6)
136108a07904SRalf Baechle 			return SIGILL;
136208a07904SRalf Baechle 
136308a07904SRalf Baechle 		sig = fpux_emu(xcp, ctx, ir, fault_addr);
1364515b029dSDavid Daney 		if (sig)
13651da177e4SLinus Torvalds 			return sig;
13661da177e4SLinus Torvalds 		break;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	case spec_op:
136908a07904SRalf Baechle 		if (!cpu_has_mips_4_5_r)
137008a07904SRalf Baechle 			return SIGILL;
137108a07904SRalf Baechle 
13721da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != movc_op)
13731da177e4SLinus Torvalds 			return SIGILL;
13741da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
13751da177e4SLinus Torvalds 		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
13761da177e4SLinus Torvalds 			xcp->regs[MIPSInst_RD(ir)] =
13771da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RS(ir)];
13781da177e4SLinus Torvalds 		break;
13791da177e4SLinus Torvalds 	default:
13801da177e4SLinus Torvalds 		return SIGILL;
13811da177e4SLinus Torvalds 	}
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 	/* we did it !! */
1384e70dfc10SAtsushi Nemoto 	xcp->cp0_epc = contpc;
1385e7e9cae5SRalf Baechle 	clear_delay_slot(xcp);
1386333d1f67SRalf Baechle 
13871da177e4SLinus Torvalds 	return 0;
13881da177e4SLinus Torvalds }
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds /*
13911da177e4SLinus Torvalds  * Conversion table from MIPS compare ops 48-63
13921da177e4SLinus Torvalds  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
13931da177e4SLinus Torvalds  */
13941da177e4SLinus Torvalds static const unsigned char cmptab[8] = {
13951da177e4SLinus Torvalds 	0,			/* cmp_0 (sig) cmp_sf */
13961da177e4SLinus Torvalds 	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
13971da177e4SLinus Torvalds 	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
13981da177e4SLinus Torvalds 	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
13991da177e4SLinus Torvalds 	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
14001da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
14011da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
14021da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
14031da177e4SLinus Torvalds };
14041da177e4SLinus Torvalds 
1405f8c3c671SMarkos Chandras static const unsigned char negative_cmptab[8] = {
1406f8c3c671SMarkos Chandras 	0, /* Reserved */
1407f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
1408f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
1409f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT,
1410f8c3c671SMarkos Chandras 	/* Reserved */
1411f8c3c671SMarkos Chandras };
1412f8c3c671SMarkos Chandras 
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds /*
14151da177e4SLinus Torvalds  * Additional MIPS4 instructions
14161da177e4SLinus Torvalds  */
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3)					\
141947fa0c02SRalf Baechle static union ieee754##p fpemu_##p##_##name(union ieee754##p r,		\
142047fa0c02SRalf Baechle 	union ieee754##p s, union ieee754##p t)				\
14211da177e4SLinus Torvalds {									\
1422cd21dfcfSRalf Baechle 	struct _ieee754_csr ieee754_csr_save;				\
14231da177e4SLinus Torvalds 	s = f1(s, t);							\
14241da177e4SLinus Torvalds 	ieee754_csr_save = ieee754_csr;					\
14251da177e4SLinus Torvalds 	s = f2(s, r);							\
14261da177e4SLinus Torvalds 	ieee754_csr_save.cx |= ieee754_csr.cx;				\
14271da177e4SLinus Torvalds 	ieee754_csr_save.sx |= ieee754_csr.sx;				\
14281da177e4SLinus Torvalds 	s = f3(s);							\
14291da177e4SLinus Torvalds 	ieee754_csr.cx |= ieee754_csr_save.cx;				\
14301da177e4SLinus Torvalds 	ieee754_csr.sx |= ieee754_csr_save.sx;				\
14311da177e4SLinus Torvalds 	return s;							\
14321da177e4SLinus Torvalds }
14331da177e4SLinus Torvalds 
14342209bcb1SRalf Baechle static union ieee754dp fpemu_dp_recip(union ieee754dp d)
14351da177e4SLinus Torvalds {
14361da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), d);
14371da177e4SLinus Torvalds }
14381da177e4SLinus Torvalds 
14392209bcb1SRalf Baechle static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
14401da177e4SLinus Torvalds {
14411da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
14442209bcb1SRalf Baechle static union ieee754sp fpemu_sp_recip(union ieee754sp s)
14451da177e4SLinus Torvalds {
14461da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), s);
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
14492209bcb1SRalf Baechle static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
14501da177e4SLinus Torvalds {
14511da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
14551da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
14561da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
14571da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
14581da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
14591da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
14601da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
14611da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
14621da177e4SLinus Torvalds 
1463eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1464445a58ceSPaul Burton 	mips_instruction ir, void __user **fault_addr)
14651da177e4SLinus Torvalds {
1466a58f85b5SAleksandar Markovic 	unsigned int rcsr = 0;	/* resulting csr */
14671da177e4SLinus Torvalds 
1468b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1xops);
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 	switch (MIPSInst_FMA_FFMT(ir)) {
14711da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
14721da177e4SLinus Torvalds 
14732209bcb1SRalf Baechle 		union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
14742209bcb1SRalf Baechle 		union ieee754sp fd, fr, fs, ft;
14753fccc015SRalf Baechle 		u32 __user *va;
14761da177e4SLinus Torvalds 		u32 val;
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
14791da177e4SLinus Torvalds 		case lwxc1_op:
14803fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14811da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14821da177e4SLinus Torvalds 
1483b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
148496d4f267SLinus Torvalds 			if (!access_ok(va, sizeof(u32))) {
1485b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1486515b029dSDavid Daney 				*fault_addr = va;
14871da177e4SLinus Torvalds 				return SIGBUS;
14881da177e4SLinus Torvalds 			}
1489515b029dSDavid Daney 			if (__get_user(val, va)) {
1490515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1491515b029dSDavid Daney 				*fault_addr = va;
1492515b029dSDavid Daney 				return SIGSEGV;
1493515b029dSDavid Daney 			}
14941da177e4SLinus Torvalds 			SITOREG(val, MIPSInst_FD(ir));
14951da177e4SLinus Torvalds 			break;
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 		case swxc1_op:
14983fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14991da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15001da177e4SLinus Torvalds 
1501b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 			SIFROMREG(val, MIPSInst_FS(ir));
150496d4f267SLinus Torvalds 			if (!access_ok(va, sizeof(u32))) {
1505515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1506515b029dSDavid Daney 				*fault_addr = va;
1507515b029dSDavid Daney 				return SIGBUS;
1508515b029dSDavid Daney 			}
15091da177e4SLinus Torvalds 			if (put_user(val, va)) {
1510b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1511515b029dSDavid Daney 				*fault_addr = va;
1512515b029dSDavid Daney 				return SIGSEGV;
15131da177e4SLinus Torvalds 			}
15141da177e4SLinus Torvalds 			break;
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds 		case madd_s_op:
15171da177e4SLinus Torvalds 			handler = fpemu_sp_madd;
15181da177e4SLinus Torvalds 			goto scoptop;
15191da177e4SLinus Torvalds 		case msub_s_op:
15201da177e4SLinus Torvalds 			handler = fpemu_sp_msub;
15211da177e4SLinus Torvalds 			goto scoptop;
15221da177e4SLinus Torvalds 		case nmadd_s_op:
15231da177e4SLinus Torvalds 			handler = fpemu_sp_nmadd;
15241da177e4SLinus Torvalds 			goto scoptop;
15251da177e4SLinus Torvalds 		case nmsub_s_op:
15261da177e4SLinus Torvalds 			handler = fpemu_sp_nmsub;
15271da177e4SLinus Torvalds 			goto scoptop;
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds 		      scoptop:
15301da177e4SLinus Torvalds 			SPFROMREG(fr, MIPSInst_FR(ir));
15311da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
15321da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
15331da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
15341da177e4SLinus Torvalds 			SPTOREG(fd, MIPSInst_FD(ir));
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds 		      copcsr:
1537c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1538c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
15391da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1540c4103526SDeng-Cheng Zhu 			}
1541c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1542c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
15431da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1544c4103526SDeng-Cheng Zhu 			}
1545c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1546c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
15471da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1548c4103526SDeng-Cheng Zhu 			}
1549c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1550c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
15511da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1552c4103526SDeng-Cheng Zhu 			}
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
15551da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
15563f7cac41SRalf Baechle 				/*printk ("SIGFPE: FPU csr = %08x\n",
15571da177e4SLinus Torvalds 				   ctx->fcr31); */
15581da177e4SLinus Torvalds 				return SIGFPE;
15591da177e4SLinus Torvalds 			}
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 			break;
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 		default:
15641da177e4SLinus Torvalds 			return SIGILL;
15651da177e4SLinus Torvalds 		}
15661da177e4SLinus Torvalds 		break;
15671da177e4SLinus Torvalds 	}
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	case d_fmt:{		/* 1 */
15702209bcb1SRalf Baechle 		union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
15712209bcb1SRalf Baechle 		union ieee754dp fd, fr, fs, ft;
15723fccc015SRalf Baechle 		u64 __user *va;
15731da177e4SLinus Torvalds 		u64 val;
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
15761da177e4SLinus Torvalds 		case ldxc1_op:
15773fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15781da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15791da177e4SLinus Torvalds 
1580b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
158196d4f267SLinus Torvalds 			if (!access_ok(va, sizeof(u64))) {
1582b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1583515b029dSDavid Daney 				*fault_addr = va;
15841da177e4SLinus Torvalds 				return SIGBUS;
15851da177e4SLinus Torvalds 			}
1586515b029dSDavid Daney 			if (__get_user(val, va)) {
1587515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1588515b029dSDavid Daney 				*fault_addr = va;
1589515b029dSDavid Daney 				return SIGSEGV;
1590515b029dSDavid Daney 			}
15911da177e4SLinus Torvalds 			DITOREG(val, MIPSInst_FD(ir));
15921da177e4SLinus Torvalds 			break;
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 		case sdxc1_op:
15953fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15961da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15971da177e4SLinus Torvalds 
1598b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
15991da177e4SLinus Torvalds 			DIFROMREG(val, MIPSInst_FS(ir));
160096d4f267SLinus Torvalds 			if (!access_ok(va, sizeof(u64))) {
1601b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1602515b029dSDavid Daney 				*fault_addr = va;
16031da177e4SLinus Torvalds 				return SIGBUS;
16041da177e4SLinus Torvalds 			}
1605515b029dSDavid Daney 			if (__put_user(val, va)) {
1606515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1607515b029dSDavid Daney 				*fault_addr = va;
1608515b029dSDavid Daney 				return SIGSEGV;
1609515b029dSDavid Daney 			}
16101da177e4SLinus Torvalds 			break;
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 		case madd_d_op:
16131da177e4SLinus Torvalds 			handler = fpemu_dp_madd;
16141da177e4SLinus Torvalds 			goto dcoptop;
16151da177e4SLinus Torvalds 		case msub_d_op:
16161da177e4SLinus Torvalds 			handler = fpemu_dp_msub;
16171da177e4SLinus Torvalds 			goto dcoptop;
16181da177e4SLinus Torvalds 		case nmadd_d_op:
16191da177e4SLinus Torvalds 			handler = fpemu_dp_nmadd;
16201da177e4SLinus Torvalds 			goto dcoptop;
16211da177e4SLinus Torvalds 		case nmsub_d_op:
16221da177e4SLinus Torvalds 			handler = fpemu_dp_nmsub;
16231da177e4SLinus Torvalds 			goto dcoptop;
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds 		      dcoptop:
16261da177e4SLinus Torvalds 			DPFROMREG(fr, MIPSInst_FR(ir));
16271da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
16281da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
16291da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
16301da177e4SLinus Torvalds 			DPTOREG(fd, MIPSInst_FD(ir));
16311da177e4SLinus Torvalds 			goto copcsr;
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 		default:
16341da177e4SLinus Torvalds 			return SIGILL;
16351da177e4SLinus Torvalds 		}
16361da177e4SLinus Torvalds 		break;
16371da177e4SLinus Torvalds 	}
16381da177e4SLinus Torvalds 
163951061b88SDeng-Cheng Zhu 	case 0x3:
164051061b88SDeng-Cheng Zhu 		if (MIPSInst_FUNC(ir) != pfetch_op)
16411da177e4SLinus Torvalds 			return SIGILL;
164251061b88SDeng-Cheng Zhu 
16431da177e4SLinus Torvalds 		/* ignore prefx operation */
16441da177e4SLinus Torvalds 		break;
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds 	default:
16471da177e4SLinus Torvalds 		return SIGILL;
16481da177e4SLinus Torvalds 	}
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	return 0;
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds /*
16561da177e4SLinus Torvalds  * Emulate a single COP1 arithmetic instruction.
16571da177e4SLinus Torvalds  */
1658eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
16591da177e4SLinus Torvalds 	mips_instruction ir)
16601da177e4SLinus Torvalds {
16611da177e4SLinus Torvalds 	int rfmt;		/* resulting format */
1662a58f85b5SAleksandar Markovic 	unsigned int rcsr = 0;	/* resulting csr */
16633f7cac41SRalf Baechle 	unsigned int oldrm;
16643f7cac41SRalf Baechle 	unsigned int cbit;
1665a58f85b5SAleksandar Markovic 	unsigned int cond;
16661da177e4SLinus Torvalds 	union {
16672209bcb1SRalf Baechle 		union ieee754dp d;
16682209bcb1SRalf Baechle 		union ieee754sp s;
16691da177e4SLinus Torvalds 		int w;
16701da177e4SLinus Torvalds 		s64 l;
16711da177e4SLinus Torvalds 	} rv;			/* resulting value */
16723f7cac41SRalf Baechle 	u64 bits;
16731da177e4SLinus Torvalds 
1674b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1ops);
16751da177e4SLinus Torvalds 	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
16761da177e4SLinus Torvalds 	case s_fmt: {		/* 0 */
16771da177e4SLinus Torvalds 		union {
16782209bcb1SRalf Baechle 			union ieee754sp(*b) (union ieee754sp, union ieee754sp);
16792209bcb1SRalf Baechle 			union ieee754sp(*u) (union ieee754sp);
16801da177e4SLinus Torvalds 		} handler;
16814b820d95SPaul Burton 		union ieee754sp fd, fs, ft;
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
16841da177e4SLinus Torvalds 			/* binary ops */
16851da177e4SLinus Torvalds 		case fadd_op:
1686454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(add_s);
16871da177e4SLinus Torvalds 			handler.b = ieee754sp_add;
16881da177e4SLinus Torvalds 			goto scopbop;
16891da177e4SLinus Torvalds 		case fsub_op:
1690454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sub_s);
16911da177e4SLinus Torvalds 			handler.b = ieee754sp_sub;
16921da177e4SLinus Torvalds 			goto scopbop;
16931da177e4SLinus Torvalds 		case fmul_op:
1694454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mul_s);
16951da177e4SLinus Torvalds 			handler.b = ieee754sp_mul;
16961da177e4SLinus Torvalds 			goto scopbop;
16971da177e4SLinus Torvalds 		case fdiv_op:
1698454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(div_s);
16991da177e4SLinus Torvalds 			handler.b = ieee754sp_div;
17001da177e4SLinus Torvalds 			goto scopbop;
17011da177e4SLinus Torvalds 
17021da177e4SLinus Torvalds 			/* unary  ops */
17031da177e4SLinus Torvalds 		case fsqrt_op:
17042d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
170508a07904SRalf Baechle 				return SIGILL;
170608a07904SRalf Baechle 
1707454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sqrt_s);
17081da177e4SLinus Torvalds 			handler.u = ieee754sp_sqrt;
17091da177e4SLinus Torvalds 			goto scopuop;
17103f7cac41SRalf Baechle 
171108a07904SRalf Baechle 		/*
171208a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
171308a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
171408a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
171508a07904SRalf Baechle 		 */
17161da177e4SLinus Torvalds 		case frsqrt_op:
17172d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
171808a07904SRalf Baechle 				return SIGILL;
171908a07904SRalf Baechle 
1720454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rsqrt_s);
17211da177e4SLinus Torvalds 			handler.u = fpemu_sp_rsqrt;
17221da177e4SLinus Torvalds 			goto scopuop;
17233f7cac41SRalf Baechle 
17241da177e4SLinus Torvalds 		case frecip_op:
17252d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
172608a07904SRalf Baechle 				return SIGILL;
172708a07904SRalf Baechle 
1728454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(recip_s);
17291da177e4SLinus Torvalds 			handler.u = fpemu_sp_recip;
17301da177e4SLinus Torvalds 			goto scopuop;
173108a07904SRalf Baechle 
17321da177e4SLinus Torvalds 		case fmovc_op:
173308a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
173408a07904SRalf Baechle 				return SIGILL;
173508a07904SRalf Baechle 
17361da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
17371da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
17381da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
17391da177e4SLinus Torvalds 				return 0;
17401da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17411da177e4SLinus Torvalds 			break;
17423f7cac41SRalf Baechle 
17431da177e4SLinus Torvalds 		case fmovz_op:
174408a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
174508a07904SRalf Baechle 				return SIGILL;
174608a07904SRalf Baechle 
17471da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
17481da177e4SLinus Torvalds 				return 0;
17491da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17501da177e4SLinus Torvalds 			break;
17513f7cac41SRalf Baechle 
17521da177e4SLinus Torvalds 		case fmovn_op:
175308a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
175408a07904SRalf Baechle 				return SIGILL;
175508a07904SRalf Baechle 
17561da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
17571da177e4SLinus Torvalds 				return 0;
17581da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17591da177e4SLinus Torvalds 			break;
17603f7cac41SRalf Baechle 
176167613f02SMarkos Chandras 		case fseleqz_op:
176267613f02SMarkos Chandras 			if (!cpu_has_mips_r6)
176367613f02SMarkos Chandras 				return SIGILL;
176467613f02SMarkos Chandras 
1765454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(seleqz_s);
176667613f02SMarkos Chandras 			SPFROMREG(rv.s, MIPSInst_FT(ir));
176767613f02SMarkos Chandras 			if (rv.w & 0x1)
176867613f02SMarkos Chandras 				rv.w = 0;
176967613f02SMarkos Chandras 			else
177067613f02SMarkos Chandras 				SPFROMREG(rv.s, MIPSInst_FS(ir));
177167613f02SMarkos Chandras 			break;
177267613f02SMarkos Chandras 
1773130fe357SMarkos Chandras 		case fselnez_op:
1774130fe357SMarkos Chandras 			if (!cpu_has_mips_r6)
1775130fe357SMarkos Chandras 				return SIGILL;
1776130fe357SMarkos Chandras 
1777454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(selnez_s);
1778130fe357SMarkos Chandras 			SPFROMREG(rv.s, MIPSInst_FT(ir));
1779130fe357SMarkos Chandras 			if (rv.w & 0x1)
1780130fe357SMarkos Chandras 				SPFROMREG(rv.s, MIPSInst_FS(ir));
1781130fe357SMarkos Chandras 			else
1782130fe357SMarkos Chandras 				rv.w = 0;
1783130fe357SMarkos Chandras 			break;
1784130fe357SMarkos Chandras 
1785e24c3becSMarkos Chandras 		case fmaddf_op: {
1786e24c3becSMarkos Chandras 			union ieee754sp ft, fs, fd;
1787e24c3becSMarkos Chandras 
1788e24c3becSMarkos Chandras 			if (!cpu_has_mips_r6)
1789e24c3becSMarkos Chandras 				return SIGILL;
1790e24c3becSMarkos Chandras 
1791454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maddf_s);
1792e24c3becSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1793e24c3becSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1794e24c3becSMarkos Chandras 			SPFROMREG(fd, MIPSInst_FD(ir));
1795e24c3becSMarkos Chandras 			rv.s = ieee754sp_maddf(fd, fs, ft);
1796409fcaceSAleksandar Markovic 			goto copcsr;
1797e24c3becSMarkos Chandras 		}
1798e24c3becSMarkos Chandras 
179983d43305SMarkos Chandras 		case fmsubf_op: {
180083d43305SMarkos Chandras 			union ieee754sp ft, fs, fd;
180183d43305SMarkos Chandras 
180283d43305SMarkos Chandras 			if (!cpu_has_mips_r6)
180383d43305SMarkos Chandras 				return SIGILL;
180483d43305SMarkos Chandras 
1805454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(msubf_s);
180683d43305SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
180783d43305SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
180883d43305SMarkos Chandras 			SPFROMREG(fd, MIPSInst_FD(ir));
180983d43305SMarkos Chandras 			rv.s = ieee754sp_msubf(fd, fs, ft);
1810409fcaceSAleksandar Markovic 			goto copcsr;
181183d43305SMarkos Chandras 		}
181283d43305SMarkos Chandras 
1813400bd2e4SMarkos Chandras 		case frint_op: {
1814400bd2e4SMarkos Chandras 			union ieee754sp fs;
1815400bd2e4SMarkos Chandras 
1816400bd2e4SMarkos Chandras 			if (!cpu_has_mips_r6)
1817400bd2e4SMarkos Chandras 				return SIGILL;
1818400bd2e4SMarkos Chandras 
1819454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rint_s);
1820400bd2e4SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18213ec404d8SAleksandar Markovic 			rv.s = ieee754sp_rint(fs);
1822400bd2e4SMarkos Chandras 			goto copcsr;
1823400bd2e4SMarkos Chandras 		}
1824400bd2e4SMarkos Chandras 
182538db37baSMarkos Chandras 		case fclass_op: {
182638db37baSMarkos Chandras 			union ieee754sp fs;
182738db37baSMarkos Chandras 
182838db37baSMarkos Chandras 			if (!cpu_has_mips_r6)
182938db37baSMarkos Chandras 				return SIGILL;
183038db37baSMarkos Chandras 
1831454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(class_s);
183238db37baSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
183338db37baSMarkos Chandras 			rv.w = ieee754sp_2008class(fs);
183438db37baSMarkos Chandras 			rfmt = w_fmt;
1835409fcaceSAleksandar Markovic 			goto copcsr;
183638db37baSMarkos Chandras 		}
183738db37baSMarkos Chandras 
18384e9561b2SMarkos Chandras 		case fmin_op: {
18394e9561b2SMarkos Chandras 			union ieee754sp fs, ft;
18404e9561b2SMarkos Chandras 
18414e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
18424e9561b2SMarkos Chandras 				return SIGILL;
18434e9561b2SMarkos Chandras 
1844454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(min_s);
18454e9561b2SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
18464e9561b2SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18474e9561b2SMarkos Chandras 			rv.s = ieee754sp_fmin(fs, ft);
1848409fcaceSAleksandar Markovic 			goto copcsr;
18494e9561b2SMarkos Chandras 		}
18504e9561b2SMarkos Chandras 
18514e9561b2SMarkos Chandras 		case fmina_op: {
18524e9561b2SMarkos Chandras 			union ieee754sp fs, ft;
18534e9561b2SMarkos Chandras 
18544e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
18554e9561b2SMarkos Chandras 				return SIGILL;
18564e9561b2SMarkos Chandras 
1857454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mina_s);
18584e9561b2SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
18594e9561b2SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18604e9561b2SMarkos Chandras 			rv.s = ieee754sp_fmina(fs, ft);
1861409fcaceSAleksandar Markovic 			goto copcsr;
18624e9561b2SMarkos Chandras 		}
18634e9561b2SMarkos Chandras 
1864a79f5f9bSMarkos Chandras 		case fmax_op: {
1865a79f5f9bSMarkos Chandras 			union ieee754sp fs, ft;
1866a79f5f9bSMarkos Chandras 
1867a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
1868a79f5f9bSMarkos Chandras 				return SIGILL;
1869a79f5f9bSMarkos Chandras 
1870454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(max_s);
1871a79f5f9bSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1872a79f5f9bSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1873a79f5f9bSMarkos Chandras 			rv.s = ieee754sp_fmax(fs, ft);
1874409fcaceSAleksandar Markovic 			goto copcsr;
1875a79f5f9bSMarkos Chandras 		}
1876a79f5f9bSMarkos Chandras 
1877a79f5f9bSMarkos Chandras 		case fmaxa_op: {
1878a79f5f9bSMarkos Chandras 			union ieee754sp fs, ft;
1879a79f5f9bSMarkos Chandras 
1880a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
1881a79f5f9bSMarkos Chandras 				return SIGILL;
1882a79f5f9bSMarkos Chandras 
1883454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maxa_s);
1884a79f5f9bSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1885a79f5f9bSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1886a79f5f9bSMarkos Chandras 			rv.s = ieee754sp_fmaxa(fs, ft);
1887409fcaceSAleksandar Markovic 			goto copcsr;
1888a79f5f9bSMarkos Chandras 		}
1889a79f5f9bSMarkos Chandras 
18901da177e4SLinus Torvalds 		case fabs_op:
1891454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(abs_s);
18921da177e4SLinus Torvalds 			handler.u = ieee754sp_abs;
18931da177e4SLinus Torvalds 			goto scopuop;
18943f7cac41SRalf Baechle 
18951da177e4SLinus Torvalds 		case fneg_op:
1896454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(neg_s);
18971da177e4SLinus Torvalds 			handler.u = ieee754sp_neg;
18981da177e4SLinus Torvalds 			goto scopuop;
18993f7cac41SRalf Baechle 
19001da177e4SLinus Torvalds 		case fmov_op:
19011da177e4SLinus Torvalds 			/* an easy one */
1902454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mov_s);
19031da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
19041da177e4SLinus Torvalds 			goto copcsr;
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 			/* binary op on handler */
19071da177e4SLinus Torvalds scopbop:
19081da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19091da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 			rv.s = (*handler.b) (fs, ft);
19121da177e4SLinus Torvalds 			goto copcsr;
19131da177e4SLinus Torvalds scopuop:
19141da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19151da177e4SLinus Torvalds 			rv.s = (*handler.u) (fs);
19161da177e4SLinus Torvalds 			goto copcsr;
19171da177e4SLinus Torvalds copcsr:
1918c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1919c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
19201da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1921c4103526SDeng-Cheng Zhu 			}
1922c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1923c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
19241da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1925c4103526SDeng-Cheng Zhu 			}
1926c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1927c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
19281da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1929c4103526SDeng-Cheng Zhu 			}
1930c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1931c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
19321da177e4SLinus Torvalds 				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1933c4103526SDeng-Cheng Zhu 			}
1934c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1935c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
19361da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1937c4103526SDeng-Cheng Zhu 			}
19381da177e4SLinus Torvalds 			break;
19391da177e4SLinus Torvalds 
19401da177e4SLinus Torvalds 			/* unary conv ops */
19411da177e4SLinus Torvalds 		case fcvts_op:
19421da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
19431da177e4SLinus Torvalds 
19443f7cac41SRalf Baechle 		case fcvtd_op:
1945454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_d_s);
19461da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19471da177e4SLinus Torvalds 			rv.d = ieee754dp_fsp(fs);
19481da177e4SLinus Torvalds 			rfmt = d_fmt;
19491da177e4SLinus Torvalds 			goto copcsr;
19501da177e4SLinus Torvalds 
19513f7cac41SRalf Baechle 		case fcvtw_op:
1952454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_w_s);
19531da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19541da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
19551da177e4SLinus Torvalds 			rfmt = w_fmt;
19561da177e4SLinus Torvalds 			goto copcsr;
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 		case fround_op:
19591da177e4SLinus Torvalds 		case ftrunc_op:
19601da177e4SLinus Torvalds 		case fceil_op:
19613f7cac41SRalf Baechle 		case ffloor_op:
19622d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
196308a07904SRalf Baechle 				return SIGILL;
196408a07904SRalf Baechle 
1965454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceil_op)
1966454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_w_s);
1967454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloor_op)
1968454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_w_s);
1969454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fround_op)
1970454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_w_s);
1971454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftrunc_op)
1972454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_w_s);
1973454854acSAleksandar Markovic 
19743f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
19751da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19762cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
19771da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
19781da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
19791da177e4SLinus Torvalds 			rfmt = w_fmt;
19801da177e4SLinus Torvalds 			goto copcsr;
19811da177e4SLinus Torvalds 
19824b820d95SPaul Burton 		case fsel_op:
19834b820d95SPaul Burton 			if (!cpu_has_mips_r6)
19844b820d95SPaul Burton 				return SIGILL;
19854b820d95SPaul Burton 
1986454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sel_s);
19874b820d95SPaul Burton 			SPFROMREG(fd, MIPSInst_FD(ir));
19884b820d95SPaul Burton 			if (fd.bits & 0x1)
19894b820d95SPaul Burton 				SPFROMREG(rv.s, MIPSInst_FT(ir));
19904b820d95SPaul Burton 			else
19914b820d95SPaul Burton 				SPFROMREG(rv.s, MIPSInst_FS(ir));
19924b820d95SPaul Burton 			break;
19934b820d95SPaul Burton 
19943f7cac41SRalf Baechle 		case fcvtl_op:
19952d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
199608a07904SRalf Baechle 				return SIGILL;
199708a07904SRalf Baechle 
1998454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_l_s);
19991da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
20001da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
20011da177e4SLinus Torvalds 			rfmt = l_fmt;
20021da177e4SLinus Torvalds 			goto copcsr;
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds 		case froundl_op:
20051da177e4SLinus Torvalds 		case ftruncl_op:
20061da177e4SLinus Torvalds 		case fceill_op:
20073f7cac41SRalf Baechle 		case ffloorl_op:
20082d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
200908a07904SRalf Baechle 				return SIGILL;
201008a07904SRalf Baechle 
2011454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceill_op)
2012454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_l_s);
2013454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloorl_op)
2014454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_l_s);
2015454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == froundl_op)
2016454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_l_s);
2017454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftruncl_op)
2018454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_l_s);
2019454854acSAleksandar Markovic 
20203f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
20211da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
20222cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
20231da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
20241da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
20251da177e4SLinus Torvalds 			rfmt = l_fmt;
20261da177e4SLinus Torvalds 			goto copcsr;
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 		default:
2029f8c3c671SMarkos Chandras 			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
2030a58f85b5SAleksandar Markovic 				unsigned int cmpop;
20312209bcb1SRalf Baechle 				union ieee754sp fs, ft;
20321da177e4SLinus Torvalds 
2033a58f85b5SAleksandar Markovic 				cmpop = MIPSInst_FUNC(ir) - fcmp_op;
20341da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
20351da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
20361da177e4SLinus Torvalds 				rv.w = ieee754sp_cmp(fs, ft,
20371da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
20381da177e4SLinus Torvalds 				rfmt = -1;
20391da177e4SLinus Torvalds 				if ((cmpop & 0x8) && ieee754_cxtest
20401da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
20411da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
20421da177e4SLinus Torvalds 				else
20431da177e4SLinus Torvalds 					goto copcsr;
20441da177e4SLinus Torvalds 
20453f7cac41SRalf Baechle 			} else
20461da177e4SLinus Torvalds 				return SIGILL;
20471da177e4SLinus Torvalds 			break;
20481da177e4SLinus Torvalds 		}
20491da177e4SLinus Torvalds 		break;
20501da177e4SLinus Torvalds 	}
20511da177e4SLinus Torvalds 
20521da177e4SLinus Torvalds 	case d_fmt: {
20534b820d95SPaul Burton 		union ieee754dp fd, fs, ft;
20541da177e4SLinus Torvalds 		union {
20552209bcb1SRalf Baechle 			union ieee754dp(*b) (union ieee754dp, union ieee754dp);
20562209bcb1SRalf Baechle 			union ieee754dp(*u) (union ieee754dp);
20571da177e4SLinus Torvalds 		} handler;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
20601da177e4SLinus Torvalds 			/* binary ops */
20611da177e4SLinus Torvalds 		case fadd_op:
2062454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(add_d);
20631da177e4SLinus Torvalds 			handler.b = ieee754dp_add;
20641da177e4SLinus Torvalds 			goto dcopbop;
20651da177e4SLinus Torvalds 		case fsub_op:
2066454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sub_d);
20671da177e4SLinus Torvalds 			handler.b = ieee754dp_sub;
20681da177e4SLinus Torvalds 			goto dcopbop;
20691da177e4SLinus Torvalds 		case fmul_op:
2070454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mul_d);
20711da177e4SLinus Torvalds 			handler.b = ieee754dp_mul;
20721da177e4SLinus Torvalds 			goto dcopbop;
20731da177e4SLinus Torvalds 		case fdiv_op:
2074454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(div_d);
20751da177e4SLinus Torvalds 			handler.b = ieee754dp_div;
20761da177e4SLinus Torvalds 			goto dcopbop;
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 			/* unary  ops */
20791da177e4SLinus Torvalds 		case fsqrt_op:
208008a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
208108a07904SRalf Baechle 				return SIGILL;
208208a07904SRalf Baechle 
2083454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sqrt_d);
20841da177e4SLinus Torvalds 			handler.u = ieee754dp_sqrt;
20851da177e4SLinus Torvalds 			goto dcopuop;
208608a07904SRalf Baechle 		/*
208708a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
208808a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
208908a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
209008a07904SRalf Baechle 		 */
20911da177e4SLinus Torvalds 		case frsqrt_op:
20922d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
209308a07904SRalf Baechle 				return SIGILL;
209408a07904SRalf Baechle 
2095454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rsqrt_d);
20961da177e4SLinus Torvalds 			handler.u = fpemu_dp_rsqrt;
20971da177e4SLinus Torvalds 			goto dcopuop;
20981da177e4SLinus Torvalds 		case frecip_op:
20992d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
210008a07904SRalf Baechle 				return SIGILL;
210108a07904SRalf Baechle 
2102454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(recip_d);
21031da177e4SLinus Torvalds 			handler.u = fpemu_dp_recip;
21041da177e4SLinus Torvalds 			goto dcopuop;
21051da177e4SLinus Torvalds 		case fmovc_op:
210608a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
210708a07904SRalf Baechle 				return SIGILL;
210808a07904SRalf Baechle 
21091da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
21101da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
21111da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
21121da177e4SLinus Torvalds 				return 0;
21131da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
21141da177e4SLinus Torvalds 			break;
21151da177e4SLinus Torvalds 		case fmovz_op:
211608a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
211708a07904SRalf Baechle 				return SIGILL;
211808a07904SRalf Baechle 
21191da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
21201da177e4SLinus Torvalds 				return 0;
21211da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
21221da177e4SLinus Torvalds 			break;
21231da177e4SLinus Torvalds 		case fmovn_op:
212408a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
212508a07904SRalf Baechle 				return SIGILL;
212608a07904SRalf Baechle 
21271da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
21281da177e4SLinus Torvalds 				return 0;
21291da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
21301da177e4SLinus Torvalds 			break;
213167613f02SMarkos Chandras 
213267613f02SMarkos Chandras 		case fseleqz_op:
213367613f02SMarkos Chandras 			if (!cpu_has_mips_r6)
213467613f02SMarkos Chandras 				return SIGILL;
213567613f02SMarkos Chandras 
2136454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(seleqz_d);
213767613f02SMarkos Chandras 			DPFROMREG(rv.d, MIPSInst_FT(ir));
213867613f02SMarkos Chandras 			if (rv.l & 0x1)
213967613f02SMarkos Chandras 				rv.l = 0;
214067613f02SMarkos Chandras 			else
214167613f02SMarkos Chandras 				DPFROMREG(rv.d, MIPSInst_FS(ir));
214267613f02SMarkos Chandras 			break;
214367613f02SMarkos Chandras 
2144130fe357SMarkos Chandras 		case fselnez_op:
2145130fe357SMarkos Chandras 			if (!cpu_has_mips_r6)
2146130fe357SMarkos Chandras 				return SIGILL;
2147130fe357SMarkos Chandras 
2148454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(selnez_d);
2149130fe357SMarkos Chandras 			DPFROMREG(rv.d, MIPSInst_FT(ir));
2150130fe357SMarkos Chandras 			if (rv.l & 0x1)
2151130fe357SMarkos Chandras 				DPFROMREG(rv.d, MIPSInst_FS(ir));
2152130fe357SMarkos Chandras 			else
2153130fe357SMarkos Chandras 				rv.l = 0;
2154130fe357SMarkos Chandras 			break;
2155130fe357SMarkos Chandras 
2156e24c3becSMarkos Chandras 		case fmaddf_op: {
2157e24c3becSMarkos Chandras 			union ieee754dp ft, fs, fd;
2158e24c3becSMarkos Chandras 
2159e24c3becSMarkos Chandras 			if (!cpu_has_mips_r6)
2160e24c3becSMarkos Chandras 				return SIGILL;
2161e24c3becSMarkos Chandras 
2162454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maddf_d);
2163e24c3becSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2164e24c3becSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2165e24c3becSMarkos Chandras 			DPFROMREG(fd, MIPSInst_FD(ir));
2166e24c3becSMarkos Chandras 			rv.d = ieee754dp_maddf(fd, fs, ft);
2167409fcaceSAleksandar Markovic 			goto copcsr;
2168e24c3becSMarkos Chandras 		}
2169e24c3becSMarkos Chandras 
217083d43305SMarkos Chandras 		case fmsubf_op: {
217183d43305SMarkos Chandras 			union ieee754dp ft, fs, fd;
217283d43305SMarkos Chandras 
217383d43305SMarkos Chandras 			if (!cpu_has_mips_r6)
217483d43305SMarkos Chandras 				return SIGILL;
217583d43305SMarkos Chandras 
2176454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(msubf_d);
217783d43305SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
217883d43305SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
217983d43305SMarkos Chandras 			DPFROMREG(fd, MIPSInst_FD(ir));
218083d43305SMarkos Chandras 			rv.d = ieee754dp_msubf(fd, fs, ft);
2181409fcaceSAleksandar Markovic 			goto copcsr;
218283d43305SMarkos Chandras 		}
218383d43305SMarkos Chandras 
2184400bd2e4SMarkos Chandras 		case frint_op: {
2185400bd2e4SMarkos Chandras 			union ieee754dp fs;
2186400bd2e4SMarkos Chandras 
2187400bd2e4SMarkos Chandras 			if (!cpu_has_mips_r6)
2188400bd2e4SMarkos Chandras 				return SIGILL;
2189400bd2e4SMarkos Chandras 
2190454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rint_d);
2191400bd2e4SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
21923ec404d8SAleksandar Markovic 			rv.d = ieee754dp_rint(fs);
2193400bd2e4SMarkos Chandras 			goto copcsr;
2194400bd2e4SMarkos Chandras 		}
2195400bd2e4SMarkos Chandras 
219638db37baSMarkos Chandras 		case fclass_op: {
219738db37baSMarkos Chandras 			union ieee754dp fs;
219838db37baSMarkos Chandras 
219938db37baSMarkos Chandras 			if (!cpu_has_mips_r6)
220038db37baSMarkos Chandras 				return SIGILL;
220138db37baSMarkos Chandras 
2202454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(class_d);
220338db37baSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2204e1231dd6SAleksandar Markovic 			rv.l = ieee754dp_2008class(fs);
2205e1231dd6SAleksandar Markovic 			rfmt = l_fmt;
2206409fcaceSAleksandar Markovic 			goto copcsr;
220738db37baSMarkos Chandras 		}
220838db37baSMarkos Chandras 
22094e9561b2SMarkos Chandras 		case fmin_op: {
22104e9561b2SMarkos Chandras 			union ieee754dp fs, ft;
22114e9561b2SMarkos Chandras 
22124e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
22134e9561b2SMarkos Chandras 				return SIGILL;
22144e9561b2SMarkos Chandras 
2215454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(min_d);
22164e9561b2SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
22174e9561b2SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
22184e9561b2SMarkos Chandras 			rv.d = ieee754dp_fmin(fs, ft);
2219409fcaceSAleksandar Markovic 			goto copcsr;
22204e9561b2SMarkos Chandras 		}
22214e9561b2SMarkos Chandras 
22224e9561b2SMarkos Chandras 		case fmina_op: {
22234e9561b2SMarkos Chandras 			union ieee754dp fs, ft;
22244e9561b2SMarkos Chandras 
22254e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
22264e9561b2SMarkos Chandras 				return SIGILL;
22274e9561b2SMarkos Chandras 
2228454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mina_d);
22294e9561b2SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
22304e9561b2SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
22314e9561b2SMarkos Chandras 			rv.d = ieee754dp_fmina(fs, ft);
2232409fcaceSAleksandar Markovic 			goto copcsr;
22334e9561b2SMarkos Chandras 		}
22344e9561b2SMarkos Chandras 
2235a79f5f9bSMarkos Chandras 		case fmax_op: {
2236a79f5f9bSMarkos Chandras 			union ieee754dp fs, ft;
2237a79f5f9bSMarkos Chandras 
2238a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
2239a79f5f9bSMarkos Chandras 				return SIGILL;
2240a79f5f9bSMarkos Chandras 
2241454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(max_d);
2242a79f5f9bSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2243a79f5f9bSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2244a79f5f9bSMarkos Chandras 			rv.d = ieee754dp_fmax(fs, ft);
2245409fcaceSAleksandar Markovic 			goto copcsr;
2246a79f5f9bSMarkos Chandras 		}
2247a79f5f9bSMarkos Chandras 
2248a79f5f9bSMarkos Chandras 		case fmaxa_op: {
2249a79f5f9bSMarkos Chandras 			union ieee754dp fs, ft;
2250a79f5f9bSMarkos Chandras 
2251a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
2252a79f5f9bSMarkos Chandras 				return SIGILL;
2253a79f5f9bSMarkos Chandras 
2254454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maxa_d);
2255a79f5f9bSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2256a79f5f9bSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2257a79f5f9bSMarkos Chandras 			rv.d = ieee754dp_fmaxa(fs, ft);
2258409fcaceSAleksandar Markovic 			goto copcsr;
2259a79f5f9bSMarkos Chandras 		}
2260a79f5f9bSMarkos Chandras 
22611da177e4SLinus Torvalds 		case fabs_op:
2262454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(abs_d);
22631da177e4SLinus Torvalds 			handler.u = ieee754dp_abs;
22641da177e4SLinus Torvalds 			goto dcopuop;
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 		case fneg_op:
2267454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(neg_d);
22681da177e4SLinus Torvalds 			handler.u = ieee754dp_neg;
22691da177e4SLinus Torvalds 			goto dcopuop;
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 		case fmov_op:
22721da177e4SLinus Torvalds 			/* an easy one */
2273454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mov_d);
22741da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
22751da177e4SLinus Torvalds 			goto copcsr;
22761da177e4SLinus Torvalds 
22771da177e4SLinus Torvalds 			/* binary op on handler */
22783f7cac41SRalf Baechle dcopbop:
22791da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22801da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds 			rv.d = (*handler.b) (fs, ft);
22831da177e4SLinus Torvalds 			goto copcsr;
22843f7cac41SRalf Baechle dcopuop:
22851da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22861da177e4SLinus Torvalds 			rv.d = (*handler.u) (fs);
22871da177e4SLinus Torvalds 			goto copcsr;
22881da177e4SLinus Torvalds 
22893f7cac41SRalf Baechle 		/*
22903f7cac41SRalf Baechle 		 * unary conv ops
22913f7cac41SRalf Baechle 		 */
22923f7cac41SRalf Baechle 		case fcvts_op:
2293454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_s_d);
22941da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22951da177e4SLinus Torvalds 			rv.s = ieee754sp_fdp(fs);
22961da177e4SLinus Torvalds 			rfmt = s_fmt;
22971da177e4SLinus Torvalds 			goto copcsr;
22983f7cac41SRalf Baechle 
22991da177e4SLinus Torvalds 		case fcvtd_op:
23001da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
23011da177e4SLinus Torvalds 
23023f7cac41SRalf Baechle 		case fcvtw_op:
2303454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_w_d);
23041da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23051da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);	/* wrong */
23061da177e4SLinus Torvalds 			rfmt = w_fmt;
23071da177e4SLinus Torvalds 			goto copcsr;
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds 		case fround_op:
23101da177e4SLinus Torvalds 		case ftrunc_op:
23111da177e4SLinus Torvalds 		case fceil_op:
23123f7cac41SRalf Baechle 		case ffloor_op:
231308a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
231408a07904SRalf Baechle 				return SIGILL;
231508a07904SRalf Baechle 
2316454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceil_op)
2317454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_w_d);
2318454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloor_op)
2319454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_w_d);
2320454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fround_op)
2321454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_w_d);
2322454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftrunc_op)
2323454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_w_d);
2324454854acSAleksandar Markovic 
23253f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
23261da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23272cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
23281da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);
23291da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
23301da177e4SLinus Torvalds 			rfmt = w_fmt;
23311da177e4SLinus Torvalds 			goto copcsr;
23321da177e4SLinus Torvalds 
23334b820d95SPaul Burton 		case fsel_op:
23344b820d95SPaul Burton 			if (!cpu_has_mips_r6)
23354b820d95SPaul Burton 				return SIGILL;
23364b820d95SPaul Burton 
2337454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sel_d);
23384b820d95SPaul Burton 			DPFROMREG(fd, MIPSInst_FD(ir));
23394b820d95SPaul Burton 			if (fd.bits & 0x1)
23404b820d95SPaul Burton 				DPFROMREG(rv.d, MIPSInst_FT(ir));
23414b820d95SPaul Burton 			else
23424b820d95SPaul Burton 				DPFROMREG(rv.d, MIPSInst_FS(ir));
23434b820d95SPaul Burton 			break;
23444b820d95SPaul Burton 
23453f7cac41SRalf Baechle 		case fcvtl_op:
23462d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
234708a07904SRalf Baechle 				return SIGILL;
234808a07904SRalf Baechle 
2349454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_l_d);
23501da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23511da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
23521da177e4SLinus Torvalds 			rfmt = l_fmt;
23531da177e4SLinus Torvalds 			goto copcsr;
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds 		case froundl_op:
23561da177e4SLinus Torvalds 		case ftruncl_op:
23571da177e4SLinus Torvalds 		case fceill_op:
23583f7cac41SRalf Baechle 		case ffloorl_op:
23592d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
236008a07904SRalf Baechle 				return SIGILL;
236108a07904SRalf Baechle 
2362454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceill_op)
2363454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_l_d);
2364454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloorl_op)
2365454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_l_d);
2366454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == froundl_op)
2367454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_l_d);
2368454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftruncl_op)
2369454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_l_d);
2370454854acSAleksandar Markovic 
23713f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
23721da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23732cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
23741da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
23751da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
23761da177e4SLinus Torvalds 			rfmt = l_fmt;
23771da177e4SLinus Torvalds 			goto copcsr;
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds 		default:
2380f8c3c671SMarkos Chandras 			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
2381a58f85b5SAleksandar Markovic 				unsigned int cmpop;
23822209bcb1SRalf Baechle 				union ieee754dp fs, ft;
23831da177e4SLinus Torvalds 
2384a58f85b5SAleksandar Markovic 				cmpop = MIPSInst_FUNC(ir) - fcmp_op;
23851da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
23861da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
23871da177e4SLinus Torvalds 				rv.w = ieee754dp_cmp(fs, ft,
23881da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
23891da177e4SLinus Torvalds 				rfmt = -1;
23901da177e4SLinus Torvalds 				if ((cmpop & 0x8)
23911da177e4SLinus Torvalds 					&&
23921da177e4SLinus Torvalds 					ieee754_cxtest
23931da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
23941da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
23951da177e4SLinus Torvalds 				else
23961da177e4SLinus Torvalds 					goto copcsr;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 			}
23991da177e4SLinus Torvalds 			else {
24001da177e4SLinus Torvalds 				return SIGILL;
24011da177e4SLinus Torvalds 			}
24021da177e4SLinus Torvalds 			break;
24031da177e4SLinus Torvalds 		}
24041da177e4SLinus Torvalds 		break;
2405bbdd8147SMarkos Chandras 	}
24061da177e4SLinus Torvalds 
2407bbdd8147SMarkos Chandras 	case w_fmt: {
2408bbdd8147SMarkos Chandras 		union ieee754dp fs;
2409bbdd8147SMarkos Chandras 
24101da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
24111da177e4SLinus Torvalds 		case fcvts_op:
24121da177e4SLinus Torvalds 			/* convert word to single precision real */
2413454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_s_w);
24141da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
24151da177e4SLinus Torvalds 			rv.s = ieee754sp_fint(fs.bits);
24161da177e4SLinus Torvalds 			rfmt = s_fmt;
24171da177e4SLinus Torvalds 			goto copcsr;
24181da177e4SLinus Torvalds 		case fcvtd_op:
24191da177e4SLinus Torvalds 			/* convert word to double precision real */
2420454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_d_w);
24211da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
24221da177e4SLinus Torvalds 			rv.d = ieee754dp_fint(fs.bits);
24231da177e4SLinus Torvalds 			rfmt = d_fmt;
24241da177e4SLinus Torvalds 			goto copcsr;
2425f8c3c671SMarkos Chandras 		default: {
2426f8c3c671SMarkos Chandras 			/* Emulating the new CMP.condn.fmt R6 instruction */
2427f8c3c671SMarkos Chandras #define CMPOP_MASK	0x7
2428f8c3c671SMarkos Chandras #define SIGN_BIT	(0x1 << 3)
2429f8c3c671SMarkos Chandras #define PREDICATE_BIT	(0x1 << 4)
2430f8c3c671SMarkos Chandras 
2431f8c3c671SMarkos Chandras 			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2432f8c3c671SMarkos Chandras 			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2433f8c3c671SMarkos Chandras 			union ieee754sp fs, ft;
2434f8c3c671SMarkos Chandras 
2435f8c3c671SMarkos Chandras 			/* This is an R6 only instruction */
2436f8c3c671SMarkos Chandras 			if (!cpu_has_mips_r6 ||
2437f8c3c671SMarkos Chandras 			    (MIPSInst_FUNC(ir) & 0x20))
2438f8c3c671SMarkos Chandras 				return SIGILL;
2439f8c3c671SMarkos Chandras 
2440454854acSAleksandar Markovic 			if (!sig) {
2441454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2442454854acSAleksandar Markovic 					switch (cmpop) {
2443454854acSAleksandar Markovic 					case 0:
2444454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_af_s);
2445454854acSAleksandar Markovic 					break;
2446454854acSAleksandar Markovic 					case 1:
2447454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_un_s);
2448454854acSAleksandar Markovic 					break;
2449454854acSAleksandar Markovic 					case 2:
2450454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_eq_s);
2451454854acSAleksandar Markovic 					break;
2452454854acSAleksandar Markovic 					case 3:
2453454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ueq_s);
2454454854acSAleksandar Markovic 					break;
2455454854acSAleksandar Markovic 					case 4:
2456454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_lt_s);
2457454854acSAleksandar Markovic 					break;
2458454854acSAleksandar Markovic 					case 5:
2459454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ult_s);
2460454854acSAleksandar Markovic 					break;
2461454854acSAleksandar Markovic 					case 6:
2462454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_le_s);
2463454854acSAleksandar Markovic 					break;
2464454854acSAleksandar Markovic 					case 7:
2465454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ule_s);
2466454854acSAleksandar Markovic 					break;
2467454854acSAleksandar Markovic 					}
2468454854acSAleksandar Markovic 				} else {
2469454854acSAleksandar Markovic 					switch (cmpop) {
2470454854acSAleksandar Markovic 					case 1:
2471454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_or_s);
2472454854acSAleksandar Markovic 					break;
2473454854acSAleksandar Markovic 					case 2:
2474454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_une_s);
2475454854acSAleksandar Markovic 					break;
2476454854acSAleksandar Markovic 					case 3:
2477454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ne_s);
2478454854acSAleksandar Markovic 					break;
2479454854acSAleksandar Markovic 					}
2480454854acSAleksandar Markovic 				}
2481454854acSAleksandar Markovic 			} else {
2482454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2483454854acSAleksandar Markovic 					switch (cmpop) {
2484454854acSAleksandar Markovic 					case 0:
2485454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_saf_s);
2486454854acSAleksandar Markovic 					break;
2487454854acSAleksandar Markovic 					case 1:
2488454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sun_s);
2489454854acSAleksandar Markovic 					break;
2490454854acSAleksandar Markovic 					case 2:
2491454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_seq_s);
2492454854acSAleksandar Markovic 					break;
2493454854acSAleksandar Markovic 					case 3:
2494454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sueq_s);
2495454854acSAleksandar Markovic 					break;
2496454854acSAleksandar Markovic 					case 4:
2497454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_slt_s);
2498454854acSAleksandar Markovic 					break;
2499454854acSAleksandar Markovic 					case 5:
2500454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sult_s);
2501454854acSAleksandar Markovic 					break;
2502454854acSAleksandar Markovic 					case 6:
2503454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sle_s);
2504454854acSAleksandar Markovic 					break;
2505454854acSAleksandar Markovic 					case 7:
2506454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sule_s);
2507454854acSAleksandar Markovic 					break;
2508454854acSAleksandar Markovic 					}
2509454854acSAleksandar Markovic 				} else {
2510454854acSAleksandar Markovic 					switch (cmpop) {
2511454854acSAleksandar Markovic 					case 1:
2512454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sor_s);
2513454854acSAleksandar Markovic 					break;
2514454854acSAleksandar Markovic 					case 2:
2515454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sune_s);
2516454854acSAleksandar Markovic 					break;
2517454854acSAleksandar Markovic 					case 3:
2518454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sne_s);
2519454854acSAleksandar Markovic 					break;
2520454854acSAleksandar Markovic 					}
2521454854acSAleksandar Markovic 				}
2522454854acSAleksandar Markovic 			}
2523454854acSAleksandar Markovic 
2524f8c3c671SMarkos Chandras 			/* fmt is w_fmt for single precision so fix it */
2525f8c3c671SMarkos Chandras 			rfmt = s_fmt;
2526f8c3c671SMarkos Chandras 			/* default to false */
2527f8c3c671SMarkos Chandras 			rv.w = 0;
2528f8c3c671SMarkos Chandras 
2529f8c3c671SMarkos Chandras 			/* CMP.condn.S */
2530f8c3c671SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
2531f8c3c671SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
2532f8c3c671SMarkos Chandras 
2533f8c3c671SMarkos Chandras 			/* positive predicates */
2534f8c3c671SMarkos Chandras 			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2535f8c3c671SMarkos Chandras 				if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
2536f8c3c671SMarkos Chandras 						  sig))
2537f8c3c671SMarkos Chandras 				    rv.w = -1; /* true, all 1s */
2538f8c3c671SMarkos Chandras 				if ((sig) &&
2539f8c3c671SMarkos Chandras 				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2540f8c3c671SMarkos Chandras 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2541f8c3c671SMarkos Chandras 				else
2542f8c3c671SMarkos Chandras 					goto copcsr;
2543f8c3c671SMarkos Chandras 			} else {
2544f8c3c671SMarkos Chandras 				/* negative predicates */
2545f8c3c671SMarkos Chandras 				switch (cmpop) {
2546f8c3c671SMarkos Chandras 				case 1:
2547f8c3c671SMarkos Chandras 				case 2:
2548f8c3c671SMarkos Chandras 				case 3:
2549f8c3c671SMarkos Chandras 					if (ieee754sp_cmp(fs, ft,
2550f8c3c671SMarkos Chandras 							  negative_cmptab[cmpop],
2551f8c3c671SMarkos Chandras 							  sig))
2552f8c3c671SMarkos Chandras 						rv.w = -1; /* true, all 1s */
2553f8c3c671SMarkos Chandras 					if (sig &&
2554f8c3c671SMarkos Chandras 					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2555f8c3c671SMarkos Chandras 						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2556f8c3c671SMarkos Chandras 					else
2557f8c3c671SMarkos Chandras 						goto copcsr;
2558f8c3c671SMarkos Chandras 					break;
25591da177e4SLinus Torvalds 				default:
2560f8c3c671SMarkos Chandras 					/* Reserved R6 ops */
25611da177e4SLinus Torvalds 					return SIGILL;
25621da177e4SLinus Torvalds 				}
2563f8c3c671SMarkos Chandras 			}
25641da177e4SLinus Torvalds 			break;
25651da177e4SLinus Torvalds 			}
2566f8c3c671SMarkos Chandras 		}
25671ff8560aSAleksandar Markovic 		break;
2568f8c3c671SMarkos Chandras 	}
25691da177e4SLinus Torvalds 
25703f7cac41SRalf Baechle 	case l_fmt:
257108a07904SRalf Baechle 
25722d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
257308a07904SRalf Baechle 			return SIGILL;
257408a07904SRalf Baechle 
2575bbd426f5SPaul Burton 		DIFROMREG(bits, MIPSInst_FS(ir));
2576bbd426f5SPaul Burton 
25771da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
25781da177e4SLinus Torvalds 		case fcvts_op:
25791da177e4SLinus Torvalds 			/* convert long to single precision real */
2580454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_s_l);
2581bbd426f5SPaul Burton 			rv.s = ieee754sp_flong(bits);
25821da177e4SLinus Torvalds 			rfmt = s_fmt;
25831da177e4SLinus Torvalds 			goto copcsr;
25841da177e4SLinus Torvalds 		case fcvtd_op:
25851da177e4SLinus Torvalds 			/* convert long to double precision real */
2586454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_d_l);
2587bbd426f5SPaul Burton 			rv.d = ieee754dp_flong(bits);
25881da177e4SLinus Torvalds 			rfmt = d_fmt;
25891da177e4SLinus Torvalds 			goto copcsr;
2590f8c3c671SMarkos Chandras 		default: {
2591f8c3c671SMarkos Chandras 			/* Emulating the new CMP.condn.fmt R6 instruction */
2592f8c3c671SMarkos Chandras 			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2593f8c3c671SMarkos Chandras 			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2594f8c3c671SMarkos Chandras 			union ieee754dp fs, ft;
2595f8c3c671SMarkos Chandras 
2596f8c3c671SMarkos Chandras 			if (!cpu_has_mips_r6 ||
2597f8c3c671SMarkos Chandras 			    (MIPSInst_FUNC(ir) & 0x20))
2598f8c3c671SMarkos Chandras 				return SIGILL;
2599f8c3c671SMarkos Chandras 
2600454854acSAleksandar Markovic 			if (!sig) {
2601454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2602454854acSAleksandar Markovic 					switch (cmpop) {
2603454854acSAleksandar Markovic 					case 0:
2604454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_af_d);
2605454854acSAleksandar Markovic 					break;
2606454854acSAleksandar Markovic 					case 1:
2607454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_un_d);
2608454854acSAleksandar Markovic 					break;
2609454854acSAleksandar Markovic 					case 2:
2610454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_eq_d);
2611454854acSAleksandar Markovic 					break;
2612454854acSAleksandar Markovic 					case 3:
2613454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ueq_d);
2614454854acSAleksandar Markovic 					break;
2615454854acSAleksandar Markovic 					case 4:
2616454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_lt_d);
2617454854acSAleksandar Markovic 					break;
2618454854acSAleksandar Markovic 					case 5:
2619454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ult_d);
2620454854acSAleksandar Markovic 					break;
2621454854acSAleksandar Markovic 					case 6:
2622454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_le_d);
2623454854acSAleksandar Markovic 					break;
2624454854acSAleksandar Markovic 					case 7:
2625454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ule_d);
2626454854acSAleksandar Markovic 					break;
2627454854acSAleksandar Markovic 					}
2628454854acSAleksandar Markovic 				} else {
2629454854acSAleksandar Markovic 					switch (cmpop) {
2630454854acSAleksandar Markovic 					case 1:
2631454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_or_d);
2632454854acSAleksandar Markovic 					break;
2633454854acSAleksandar Markovic 					case 2:
2634454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_une_d);
2635454854acSAleksandar Markovic 					break;
2636454854acSAleksandar Markovic 					case 3:
2637454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ne_d);
2638454854acSAleksandar Markovic 					break;
2639454854acSAleksandar Markovic 					}
2640454854acSAleksandar Markovic 				}
2641454854acSAleksandar Markovic 			} else {
2642454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2643454854acSAleksandar Markovic 					switch (cmpop) {
2644454854acSAleksandar Markovic 					case 0:
2645454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_saf_d);
2646454854acSAleksandar Markovic 					break;
2647454854acSAleksandar Markovic 					case 1:
2648454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sun_d);
2649454854acSAleksandar Markovic 					break;
2650454854acSAleksandar Markovic 					case 2:
2651454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_seq_d);
2652454854acSAleksandar Markovic 					break;
2653454854acSAleksandar Markovic 					case 3:
2654454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sueq_d);
2655454854acSAleksandar Markovic 					break;
2656454854acSAleksandar Markovic 					case 4:
2657454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_slt_d);
2658454854acSAleksandar Markovic 					break;
2659454854acSAleksandar Markovic 					case 5:
2660454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sult_d);
2661454854acSAleksandar Markovic 					break;
2662454854acSAleksandar Markovic 					case 6:
2663454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sle_d);
2664454854acSAleksandar Markovic 					break;
2665454854acSAleksandar Markovic 					case 7:
2666454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sule_d);
2667454854acSAleksandar Markovic 					break;
2668454854acSAleksandar Markovic 					}
2669454854acSAleksandar Markovic 				} else {
2670454854acSAleksandar Markovic 					switch (cmpop) {
2671454854acSAleksandar Markovic 					case 1:
2672454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sor_d);
2673454854acSAleksandar Markovic 					break;
2674454854acSAleksandar Markovic 					case 2:
2675454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sune_d);
2676454854acSAleksandar Markovic 					break;
2677454854acSAleksandar Markovic 					case 3:
2678454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sne_d);
2679454854acSAleksandar Markovic 					break;
2680454854acSAleksandar Markovic 					}
2681454854acSAleksandar Markovic 				}
2682454854acSAleksandar Markovic 			}
2683454854acSAleksandar Markovic 
2684f8c3c671SMarkos Chandras 			/* fmt is l_fmt for double precision so fix it */
2685f8c3c671SMarkos Chandras 			rfmt = d_fmt;
2686f8c3c671SMarkos Chandras 			/* default to false */
2687f8c3c671SMarkos Chandras 			rv.l = 0;
2688f8c3c671SMarkos Chandras 
2689f8c3c671SMarkos Chandras 			/* CMP.condn.D */
2690f8c3c671SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2691f8c3c671SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2692f8c3c671SMarkos Chandras 
2693f8c3c671SMarkos Chandras 			/* positive predicates */
2694f8c3c671SMarkos Chandras 			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2695f8c3c671SMarkos Chandras 				if (ieee754dp_cmp(fs, ft,
2696f8c3c671SMarkos Chandras 						  cmptab[cmpop], sig))
2697f8c3c671SMarkos Chandras 				    rv.l = -1LL; /* true, all 1s */
2698f8c3c671SMarkos Chandras 				if (sig &&
2699f8c3c671SMarkos Chandras 				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2700f8c3c671SMarkos Chandras 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2701f8c3c671SMarkos Chandras 				else
2702f8c3c671SMarkos Chandras 					goto copcsr;
2703f8c3c671SMarkos Chandras 			} else {
2704f8c3c671SMarkos Chandras 				/* negative predicates */
2705f8c3c671SMarkos Chandras 				switch (cmpop) {
2706f8c3c671SMarkos Chandras 				case 1:
2707f8c3c671SMarkos Chandras 				case 2:
2708f8c3c671SMarkos Chandras 				case 3:
2709f8c3c671SMarkos Chandras 					if (ieee754dp_cmp(fs, ft,
2710f8c3c671SMarkos Chandras 							  negative_cmptab[cmpop],
2711f8c3c671SMarkos Chandras 							  sig))
2712f8c3c671SMarkos Chandras 						rv.l = -1LL; /* true, all 1s */
2713f8c3c671SMarkos Chandras 					if (sig &&
2714f8c3c671SMarkos Chandras 					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2715f8c3c671SMarkos Chandras 						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2716f8c3c671SMarkos Chandras 					else
2717f8c3c671SMarkos Chandras 						goto copcsr;
2718f8c3c671SMarkos Chandras 					break;
27191da177e4SLinus Torvalds 				default:
2720f8c3c671SMarkos Chandras 					/* Reserved R6 ops */
27211da177e4SLinus Torvalds 					return SIGILL;
27221da177e4SLinus Torvalds 				}
2723f8c3c671SMarkos Chandras 			}
27241da177e4SLinus Torvalds 			break;
2725f8c3c671SMarkos Chandras 			}
2726f8c3c671SMarkos Chandras 		}
27271ff8560aSAleksandar Markovic 		break;
27281ff8560aSAleksandar Markovic 
27291da177e4SLinus Torvalds 	default:
27301da177e4SLinus Torvalds 		return SIGILL;
27311da177e4SLinus Torvalds 	}
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds 	/*
27341da177e4SLinus Torvalds 	 * Update the fpu CSR register for this operation.
27351da177e4SLinus Torvalds 	 * If an exception is required, generate a tidy SIGFPE exception,
27361da177e4SLinus Torvalds 	 * without updating the result register.
27371da177e4SLinus Torvalds 	 * Note: cause exception bits do not accumulate, they are rewritten
27381da177e4SLinus Torvalds 	 * for each op; only the flag/sticky bits accumulate.
27391da177e4SLinus Torvalds 	 */
27401da177e4SLinus Torvalds 	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
27411da177e4SLinus Torvalds 	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
27423f7cac41SRalf Baechle 		/*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
27431da177e4SLinus Torvalds 		return SIGFPE;
27441da177e4SLinus Torvalds 	}
27451da177e4SLinus Torvalds 
27461da177e4SLinus Torvalds 	/*
27471da177e4SLinus Torvalds 	 * Now we can safely write the result back to the register file.
27481da177e4SLinus Torvalds 	 */
27491da177e4SLinus Torvalds 	switch (rfmt) {
275008a07904SRalf Baechle 	case -1:
275108a07904SRalf Baechle 
275208a07904SRalf Baechle 		if (cpu_has_mips_4_5_r)
2753c3b9b945SRob Kendrick 			cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
27541da177e4SLinus Torvalds 		else
275508a07904SRalf Baechle 			cbit = FPU_CSR_COND;
275608a07904SRalf Baechle 		if (rv.w)
275708a07904SRalf Baechle 			ctx->fcr31 |= cbit;
275808a07904SRalf Baechle 		else
275908a07904SRalf Baechle 			ctx->fcr31 &= ~cbit;
27601da177e4SLinus Torvalds 		break;
276108a07904SRalf Baechle 
27621da177e4SLinus Torvalds 	case d_fmt:
27631da177e4SLinus Torvalds 		DPTOREG(rv.d, MIPSInst_FD(ir));
27641da177e4SLinus Torvalds 		break;
27651da177e4SLinus Torvalds 	case s_fmt:
27661da177e4SLinus Torvalds 		SPTOREG(rv.s, MIPSInst_FD(ir));
27671da177e4SLinus Torvalds 		break;
27681da177e4SLinus Torvalds 	case w_fmt:
27691da177e4SLinus Torvalds 		SITOREG(rv.w, MIPSInst_FD(ir));
27701da177e4SLinus Torvalds 		break;
27711da177e4SLinus Torvalds 	case l_fmt:
27722d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
277308a07904SRalf Baechle 			return SIGILL;
277408a07904SRalf Baechle 
27751da177e4SLinus Torvalds 		DITOREG(rv.l, MIPSInst_FD(ir));
27761da177e4SLinus Torvalds 		break;
27771da177e4SLinus Torvalds 	default:
27781da177e4SLinus Torvalds 		return SIGILL;
27791da177e4SLinus Torvalds 	}
27801da177e4SLinus Torvalds 
27811da177e4SLinus Torvalds 	return 0;
27821da177e4SLinus Torvalds }
27831da177e4SLinus Torvalds 
278413769ebaSMaciej W. Rozycki /*
278513769ebaSMaciej W. Rozycki  * Emulate FPU instructions.
278613769ebaSMaciej W. Rozycki  *
278713769ebaSMaciej W. Rozycki  * If we use FPU hardware, then we have been typically called to handle
278813769ebaSMaciej W. Rozycki  * an unimplemented operation, such as where an operand is a NaN or
278913769ebaSMaciej W. Rozycki  * denormalized.  In that case exit the emulation loop after a single
279013769ebaSMaciej W. Rozycki  * iteration so as to let hardware execute any subsequent instructions.
279113769ebaSMaciej W. Rozycki  *
279213769ebaSMaciej W. Rozycki  * If we have no FPU hardware or it has been disabled, then continue
279313769ebaSMaciej W. Rozycki  * emulating floating-point instructions until one of these conditions
279413769ebaSMaciej W. Rozycki  * has occurred:
279513769ebaSMaciej W. Rozycki  *
279613769ebaSMaciej W. Rozycki  * - a non-FPU instruction has been encountered,
279713769ebaSMaciej W. Rozycki  *
279813769ebaSMaciej W. Rozycki  * - an attempt to emulate has ended with a signal,
279913769ebaSMaciej W. Rozycki  *
280013769ebaSMaciej W. Rozycki  * - the ISA mode has been switched.
280113769ebaSMaciej W. Rozycki  *
280213769ebaSMaciej W. Rozycki  * We need to terminate the emulation loop if we got switched to the
280313769ebaSMaciej W. Rozycki  * MIPS16 mode, whether supported or not, so that we do not attempt
280413769ebaSMaciej W. Rozycki  * to emulate a MIPS16 instruction as a regular MIPS FPU instruction.
280513769ebaSMaciej W. Rozycki  * Similarly if we got switched to the microMIPS mode and only the
280613769ebaSMaciej W. Rozycki  * regular MIPS mode is supported, so that we do not attempt to emulate
280713769ebaSMaciej W. Rozycki  * a microMIPS instruction as a regular MIPS FPU instruction.  Or if
280813769ebaSMaciej W. Rozycki  * we got switched to the regular MIPS mode and only the microMIPS mode
280913769ebaSMaciej W. Rozycki  * is supported, so that we do not attempt to emulate a regular MIPS
281013769ebaSMaciej W. Rozycki  * instruction that should cause an Address Error exception instead.
281113769ebaSMaciej W. Rozycki  * For simplicity we always terminate upon an ISA mode switch.
281213769ebaSMaciej W. Rozycki  */
2813e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2814445a58ceSPaul Burton 	int has_fpu, void __user **fault_addr)
28151da177e4SLinus Torvalds {
2816333d1f67SRalf Baechle 	unsigned long oldepc, prevepc;
2817102cedc3SLeonid Yegoshin 	struct mm_decoded_insn dec_insn;
2818102cedc3SLeonid Yegoshin 	u16 instr[4];
2819102cedc3SLeonid Yegoshin 	u16 *instr_ptr;
28201da177e4SLinus Torvalds 	int sig = 0;
28211da177e4SLinus Torvalds 
28221975ed43SPaul Burton 	/*
28231975ed43SPaul Burton 	 * Initialize context if it hasn't been used already, otherwise ensure
28241975ed43SPaul Burton 	 * it has been saved to struct thread_struct.
28251975ed43SPaul Burton 	 */
28261975ed43SPaul Burton 	if (!init_fp_ctx(current))
28271975ed43SPaul Burton 		lose_fpu(1);
28281975ed43SPaul Burton 
28291da177e4SLinus Torvalds 	oldepc = xcp->cp0_epc;
28301da177e4SLinus Torvalds 	do {
28311da177e4SLinus Torvalds 		prevepc = xcp->cp0_epc;
28321da177e4SLinus Torvalds 
2833102cedc3SLeonid Yegoshin 		if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2834102cedc3SLeonid Yegoshin 			/*
2835102cedc3SLeonid Yegoshin 			 * Get next 2 microMIPS instructions and convert them
2836102cedc3SLeonid Yegoshin 			 * into 32-bit instructions.
2837102cedc3SLeonid Yegoshin 			 */
2838102cedc3SLeonid Yegoshin 			if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2839102cedc3SLeonid Yegoshin 			    (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2840102cedc3SLeonid Yegoshin 			    (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2841102cedc3SLeonid Yegoshin 			    (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2842b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
28431da177e4SLinus Torvalds 				return SIGBUS;
28441da177e4SLinus Torvalds 			}
2845102cedc3SLeonid Yegoshin 			instr_ptr = instr;
2846102cedc3SLeonid Yegoshin 
2847102cedc3SLeonid Yegoshin 			/* Get first instruction. */
2848102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2849102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2850102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2851102cedc3SLeonid Yegoshin 					(*instr_ptr);
2852102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2853102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 2;
2854102cedc3SLeonid Yegoshin 				instr_ptr += 1;
2855102cedc3SLeonid Yegoshin 			} else {
2856102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2857102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2858102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2859102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 4;
2860102cedc3SLeonid Yegoshin 				instr_ptr += 2;
2861515b029dSDavid Daney 			}
2862102cedc3SLeonid Yegoshin 			/* Get second instruction. */
2863102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2864102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2865102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2866102cedc3SLeonid Yegoshin 					(*instr_ptr);
2867102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2868102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 2;
2869102cedc3SLeonid Yegoshin 			} else {
2870102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2871102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2872102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2873102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 4;
2874102cedc3SLeonid Yegoshin 			}
2875102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 1;
2876102cedc3SLeonid Yegoshin 		} else {
2877102cedc3SLeonid Yegoshin 			if ((get_user(dec_insn.insn,
2878102cedc3SLeonid Yegoshin 			    (mips_instruction __user *) xcp->cp0_epc)) ||
2879102cedc3SLeonid Yegoshin 			    (get_user(dec_insn.next_insn,
2880102cedc3SLeonid Yegoshin 			    (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2881102cedc3SLeonid Yegoshin 				MIPS_FPU_EMU_INC_STATS(errors);
2882102cedc3SLeonid Yegoshin 				return SIGBUS;
2883102cedc3SLeonid Yegoshin 			}
2884102cedc3SLeonid Yegoshin 			dec_insn.pc_inc = 4;
2885102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc = 4;
2886102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 0;
2887102cedc3SLeonid Yegoshin 		}
2888102cedc3SLeonid Yegoshin 
2889102cedc3SLeonid Yegoshin 		if ((dec_insn.insn == 0) ||
2890102cedc3SLeonid Yegoshin 		   ((dec_insn.pc_inc == 2) &&
2891102cedc3SLeonid Yegoshin 		   ((dec_insn.insn & 0xffff) == MM_NOP16)))
2892102cedc3SLeonid Yegoshin 			xcp->cp0_epc += dec_insn.pc_inc;	/* Skip NOPs */
28931da177e4SLinus Torvalds 		else {
2894cd21dfcfSRalf Baechle 			/*
28952cfcf8a8SMaciej W. Rozycki 			 * The 'ieee754_csr' is an alias of ctx->fcr31.
28962cfcf8a8SMaciej W. Rozycki 			 * No need to copy ctx->fcr31 to ieee754_csr.
2897cd21dfcfSRalf Baechle 			 */
2898102cedc3SLeonid Yegoshin 			sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
28991da177e4SLinus Torvalds 		}
29001da177e4SLinus Torvalds 
2901e04582b7SAtsushi Nemoto 		if (has_fpu)
29021da177e4SLinus Torvalds 			break;
29031da177e4SLinus Torvalds 		if (sig)
29041da177e4SLinus Torvalds 			break;
290513769ebaSMaciej W. Rozycki 		/*
290613769ebaSMaciej W. Rozycki 		 * We have to check for the ISA bit explicitly here,
290713769ebaSMaciej W. Rozycki 		 * because `get_isa16_mode' may return 0 if support
290813769ebaSMaciej W. Rozycki 		 * for code compression has been globally disabled,
290913769ebaSMaciej W. Rozycki 		 * or otherwise we may produce the wrong signal or
291013769ebaSMaciej W. Rozycki 		 * even proceed successfully where we must not.
291113769ebaSMaciej W. Rozycki 		 */
291213769ebaSMaciej W. Rozycki 		if ((xcp->cp0_epc ^ prevepc) & 0x1)
291313769ebaSMaciej W. Rozycki 			break;
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds 		cond_resched();
29161da177e4SLinus Torvalds 	} while (xcp->cp0_epc > prevepc);
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds 	/* SIGILL indicates a non-fpu instruction */
29191da177e4SLinus Torvalds 	if (sig == SIGILL && xcp->cp0_epc != oldepc)
29203f7cac41SRalf Baechle 		/* but if EPC has advanced, then ignore it */
29211da177e4SLinus Torvalds 		sig = 0;
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds 	return sig;
29241da177e4SLinus Torvalds }
2925