xref: /linux/arch/mips/math-emu/cp1emu.c (revision 1975ed43ce67542befed188f6aed6a82cfaffd0b)
11da177e4SLinus Torvalds /*
23f7cac41SRalf Baechle  * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * MIPS floating point support
51da177e4SLinus Torvalds  * Copyright (C) 1994-2000 Algorithmics Ltd.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
81da177e4SLinus Torvalds  * Copyright (C) 2000  MIPS Technologies, Inc.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  This program is free software; you can distribute it and/or modify it
111da177e4SLinus Torvalds  *  under the terms of the GNU General Public License (Version 2) as
121da177e4SLinus Torvalds  *  published by the Free Software Foundation.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  This program is distributed in the hope it will be useful, but WITHOUT
151da177e4SLinus Torvalds  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
161da177e4SLinus Torvalds  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
171da177e4SLinus Torvalds  *  for more details.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *  You should have received a copy of the GNU General Public License along
201da177e4SLinus Torvalds  *  with this program; if not, write to the Free Software Foundation, Inc.,
213f7cac41SRalf Baechle  *  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * A complete emulator for MIPS coprocessor 1 instructions.  This is
241da177e4SLinus Torvalds  * required for #float(switch) or #float(trap), where it catches all
251da177e4SLinus Torvalds  * COP1 instructions via the "CoProcessor Unusable" exception.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  * More surprisingly it is also required for #float(ieee), to help out
283f7cac41SRalf Baechle  * the hardware FPU at the boundaries of the IEEE-754 representation
291da177e4SLinus Torvalds  * (denormalised values, infinities, underflow, etc).  It is made
301da177e4SLinus Torvalds  * quite nasty because emulation of some non-COP1 instructions is
311da177e4SLinus Torvalds  * required, e.g. in branch delay slots.
321da177e4SLinus Torvalds  *
333f7cac41SRalf Baechle  * Note if you know that you won't have an FPU, then you'll get much
341da177e4SLinus Torvalds  * better performance by compiling with -msoft-float!
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds #include <linux/sched.h>
3783fd38caSAtsushi Nemoto #include <linux/debugfs.h>
3885c51c51SRalf Baechle #include <linux/percpu-defs.h>
397f788d2dSDeng-Cheng Zhu #include <linux/perf_event.h>
401da177e4SLinus Torvalds 
41cd8ee345SRalf Baechle #include <asm/branch.h>
421da177e4SLinus Torvalds #include <asm/inst.h>
431da177e4SLinus Torvalds #include <asm/ptrace.h>
441da177e4SLinus Torvalds #include <asm/signal.h>
457c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
46cd8ee345SRalf Baechle 
47f6843626SMaciej W. Rozycki #include <asm/cpu-info.h>
48cd8ee345SRalf Baechle #include <asm/processor.h>
491da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
50102cedc3SLeonid Yegoshin #include <asm/fpu.h>
51b0a668fbSLeonid Yegoshin #include <asm/mips-r2-to-r6-emul.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include "ieee754.h"
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */
561da177e4SLinus Torvalds 
57eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
581da177e4SLinus Torvalds 	mips_instruction);
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *,
61445a58ceSPaul Burton 	struct mips_fpu_struct *, mips_instruction, void __user **);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds /* Control registers */
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #define FPCREG_RID	0	/* $0  = revision id */
66c491cfa2SMaciej W. Rozycki #define FPCREG_FCCR	25	/* $25 = fccr */
67c491cfa2SMaciej W. Rozycki #define FPCREG_FEXR	26	/* $26 = fexr */
68c491cfa2SMaciej W. Rozycki #define FPCREG_FENR	28	/* $28 = fenr */
691da177e4SLinus Torvalds #define FPCREG_CSR	31	/* $31 = csr */
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /* convert condition code register number to csr bit */
72b0a668fbSLeonid Yegoshin const unsigned int fpucondbit[8] = {
73c491cfa2SMaciej W. Rozycki 	FPU_CSR_COND,
741da177e4SLinus Torvalds 	FPU_CSR_COND1,
751da177e4SLinus Torvalds 	FPU_CSR_COND2,
761da177e4SLinus Torvalds 	FPU_CSR_COND3,
771da177e4SLinus Torvalds 	FPU_CSR_COND4,
781da177e4SLinus Torvalds 	FPU_CSR_COND5,
791da177e4SLinus Torvalds 	FPU_CSR_COND6,
801da177e4SLinus Torvalds 	FPU_CSR_COND7
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds 
83102cedc3SLeonid Yegoshin /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
84102cedc3SLeonid Yegoshin static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
85102cedc3SLeonid Yegoshin static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
86102cedc3SLeonid Yegoshin static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
87102cedc3SLeonid Yegoshin static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
88102cedc3SLeonid Yegoshin 
89102cedc3SLeonid Yegoshin /*
90102cedc3SLeonid Yegoshin  * This functions translates a 32-bit microMIPS instruction
91102cedc3SLeonid Yegoshin  * into a 32-bit MIPS32 instruction. Returns 0 on success
92102cedc3SLeonid Yegoshin  * and SIGILL otherwise.
93102cedc3SLeonid Yegoshin  */
94102cedc3SLeonid Yegoshin static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
95102cedc3SLeonid Yegoshin {
96102cedc3SLeonid Yegoshin 	union mips_instruction insn = *insn_ptr;
97102cedc3SLeonid Yegoshin 	union mips_instruction mips32_insn = insn;
98102cedc3SLeonid Yegoshin 	int func, fmt, op;
99102cedc3SLeonid Yegoshin 
100102cedc3SLeonid Yegoshin 	switch (insn.mm_i_format.opcode) {
101102cedc3SLeonid Yegoshin 	case mm_ldc132_op:
102102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = ldc1_op;
103102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
104102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
105102cedc3SLeonid Yegoshin 		break;
106102cedc3SLeonid Yegoshin 	case mm_lwc132_op:
107102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = lwc1_op;
108102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
109102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
110102cedc3SLeonid Yegoshin 		break;
111102cedc3SLeonid Yegoshin 	case mm_sdc132_op:
112102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = sdc1_op;
113102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
114102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
115102cedc3SLeonid Yegoshin 		break;
116102cedc3SLeonid Yegoshin 	case mm_swc132_op:
117102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = swc1_op;
118102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
119102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
120102cedc3SLeonid Yegoshin 		break;
121102cedc3SLeonid Yegoshin 	case mm_pool32i_op:
122102cedc3SLeonid Yegoshin 		/* NOTE: offset is << by 1 if in microMIPS mode. */
123102cedc3SLeonid Yegoshin 		if ((insn.mm_i_format.rt == mm_bc1f_op) ||
124102cedc3SLeonid Yegoshin 		    (insn.mm_i_format.rt == mm_bc1t_op)) {
125102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.opcode = cop1_op;
126102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.bc = bc_op;
127102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.flag =
128102cedc3SLeonid Yegoshin 				(insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
129102cedc3SLeonid Yegoshin 		} else
130102cedc3SLeonid Yegoshin 			return SIGILL;
131102cedc3SLeonid Yegoshin 		break;
132102cedc3SLeonid Yegoshin 	case mm_pool32f_op:
133102cedc3SLeonid Yegoshin 		switch (insn.mm_fp0_format.func) {
134102cedc3SLeonid Yegoshin 		case mm_32f_01_op:
135102cedc3SLeonid Yegoshin 		case mm_32f_11_op:
136102cedc3SLeonid Yegoshin 		case mm_32f_02_op:
137102cedc3SLeonid Yegoshin 		case mm_32f_12_op:
138102cedc3SLeonid Yegoshin 		case mm_32f_41_op:
139102cedc3SLeonid Yegoshin 		case mm_32f_51_op:
140102cedc3SLeonid Yegoshin 		case mm_32f_42_op:
141102cedc3SLeonid Yegoshin 		case mm_32f_52_op:
142102cedc3SLeonid Yegoshin 			op = insn.mm_fp0_format.func;
143102cedc3SLeonid Yegoshin 			if (op == mm_32f_01_op)
144102cedc3SLeonid Yegoshin 				func = madd_s_op;
145102cedc3SLeonid Yegoshin 			else if (op == mm_32f_11_op)
146102cedc3SLeonid Yegoshin 				func = madd_d_op;
147102cedc3SLeonid Yegoshin 			else if (op == mm_32f_02_op)
148102cedc3SLeonid Yegoshin 				func = nmadd_s_op;
149102cedc3SLeonid Yegoshin 			else if (op == mm_32f_12_op)
150102cedc3SLeonid Yegoshin 				func = nmadd_d_op;
151102cedc3SLeonid Yegoshin 			else if (op == mm_32f_41_op)
152102cedc3SLeonid Yegoshin 				func = msub_s_op;
153102cedc3SLeonid Yegoshin 			else if (op == mm_32f_51_op)
154102cedc3SLeonid Yegoshin 				func = msub_d_op;
155102cedc3SLeonid Yegoshin 			else if (op == mm_32f_42_op)
156102cedc3SLeonid Yegoshin 				func = nmsub_s_op;
157102cedc3SLeonid Yegoshin 			else
158102cedc3SLeonid Yegoshin 				func = nmsub_d_op;
159102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.opcode = cop1x_op;
160102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
161102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
162102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
163102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
164102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.func = func;
165102cedc3SLeonid Yegoshin 			break;
166102cedc3SLeonid Yegoshin 		case mm_32f_10_op:
167102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
168102cedc3SLeonid Yegoshin 			op = insn.mm_fp5_format.op & 0x7;
169102cedc3SLeonid Yegoshin 			if (op == mm_ldxc1_op)
170102cedc3SLeonid Yegoshin 				func = ldxc1_op;
171102cedc3SLeonid Yegoshin 			else if (op == mm_sdxc1_op)
172102cedc3SLeonid Yegoshin 				func = sdxc1_op;
173102cedc3SLeonid Yegoshin 			else if (op == mm_lwxc1_op)
174102cedc3SLeonid Yegoshin 				func = lwxc1_op;
175102cedc3SLeonid Yegoshin 			else if (op == mm_swxc1_op)
176102cedc3SLeonid Yegoshin 				func = swxc1_op;
177102cedc3SLeonid Yegoshin 
178102cedc3SLeonid Yegoshin 			if (func != -1) {
179102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = cop1x_op;
180102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs =
181102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.base;
182102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
183102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.index;
184102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = 0;
185102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = insn.mm_fp5_format.fd;
186102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = func;
187102cedc3SLeonid Yegoshin 			} else
188102cedc3SLeonid Yegoshin 				return SIGILL;
189102cedc3SLeonid Yegoshin 			break;
190102cedc3SLeonid Yegoshin 		case mm_32f_40_op:
191102cedc3SLeonid Yegoshin 			op = -1;	/* Invalid */
192102cedc3SLeonid Yegoshin 			if (insn.mm_fp2_format.op == mm_fmovt_op)
193102cedc3SLeonid Yegoshin 				op = 1;
194102cedc3SLeonid Yegoshin 			else if (insn.mm_fp2_format.op == mm_fmovf_op)
195102cedc3SLeonid Yegoshin 				op = 0;
196102cedc3SLeonid Yegoshin 			if (op != -1) {
197102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
198102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
199102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp2_format.fmt];
200102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
201102cedc3SLeonid Yegoshin 					(insn.mm_fp2_format.cc<<2) + op;
202102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
203102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fs;
204102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
205102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fd;
206102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = fmovc_op;
207102cedc3SLeonid Yegoshin 			} else
208102cedc3SLeonid Yegoshin 				return SIGILL;
209102cedc3SLeonid Yegoshin 			break;
210102cedc3SLeonid Yegoshin 		case mm_32f_60_op:
211102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
212102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fadd_op)
213102cedc3SLeonid Yegoshin 				func = fadd_op;
214102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fsub_op)
215102cedc3SLeonid Yegoshin 				func = fsub_op;
216102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmul_op)
217102cedc3SLeonid Yegoshin 				func = fmul_op;
218102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fdiv_op)
219102cedc3SLeonid Yegoshin 				func = fdiv_op;
220102cedc3SLeonid Yegoshin 			if (func != -1) {
221102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
222102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
223102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
224102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
225102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
226102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
227102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
228102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
229102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
230102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
231102cedc3SLeonid Yegoshin 			} else
232102cedc3SLeonid Yegoshin 				return SIGILL;
233102cedc3SLeonid Yegoshin 			break;
234102cedc3SLeonid Yegoshin 		case mm_32f_70_op:
235102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
236102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fmovn_op)
237102cedc3SLeonid Yegoshin 				func = fmovn_op;
238102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmovz_op)
239102cedc3SLeonid Yegoshin 				func = fmovz_op;
240102cedc3SLeonid Yegoshin 			if (func != -1) {
241102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
242102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
243102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
244102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
245102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
246102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
247102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
248102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
249102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
250102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
251102cedc3SLeonid Yegoshin 			} else
252102cedc3SLeonid Yegoshin 				return SIGILL;
253102cedc3SLeonid Yegoshin 			break;
254102cedc3SLeonid Yegoshin 		case mm_32f_73_op:    /* POOL32FXF */
255102cedc3SLeonid Yegoshin 			switch (insn.mm_fp1_format.op) {
256102cedc3SLeonid Yegoshin 			case mm_movf0_op:
257102cedc3SLeonid Yegoshin 			case mm_movf1_op:
258102cedc3SLeonid Yegoshin 			case mm_movt0_op:
259102cedc3SLeonid Yegoshin 			case mm_movt1_op:
260102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
261102cedc3SLeonid Yegoshin 				    mm_movf0_op)
262102cedc3SLeonid Yegoshin 					op = 0;
263102cedc3SLeonid Yegoshin 				else
264102cedc3SLeonid Yegoshin 					op = 1;
265102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = spec_op;
266102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
267102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
268102cedc3SLeonid Yegoshin 					(insn.mm_fp4_format.cc << 2) + op;
269102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
270102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = 0;
271102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = movc_op;
272102cedc3SLeonid Yegoshin 				break;
273102cedc3SLeonid Yegoshin 			case mm_fcvtd0_op:
274102cedc3SLeonid Yegoshin 			case mm_fcvtd1_op:
275102cedc3SLeonid Yegoshin 			case mm_fcvts0_op:
276102cedc3SLeonid Yegoshin 			case mm_fcvts1_op:
277102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
278102cedc3SLeonid Yegoshin 				    mm_fcvtd0_op) {
279102cedc3SLeonid Yegoshin 					func = fcvtd_op;
280102cedc3SLeonid Yegoshin 					fmt = swl_format[insn.mm_fp3_format.fmt];
281102cedc3SLeonid Yegoshin 				} else {
282102cedc3SLeonid Yegoshin 					func = fcvts_op;
283102cedc3SLeonid Yegoshin 					fmt = dwl_format[insn.mm_fp3_format.fmt];
284102cedc3SLeonid Yegoshin 				}
285102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
286102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt = fmt;
287102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
288102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
289102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
290102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
291102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
292102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
293102cedc3SLeonid Yegoshin 				break;
294102cedc3SLeonid Yegoshin 			case mm_fmov0_op:
295102cedc3SLeonid Yegoshin 			case mm_fmov1_op:
296102cedc3SLeonid Yegoshin 			case mm_fabs0_op:
297102cedc3SLeonid Yegoshin 			case mm_fabs1_op:
298102cedc3SLeonid Yegoshin 			case mm_fneg0_op:
299102cedc3SLeonid Yegoshin 			case mm_fneg1_op:
300102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
301102cedc3SLeonid Yegoshin 				    mm_fmov0_op)
302102cedc3SLeonid Yegoshin 					func = fmov_op;
303102cedc3SLeonid Yegoshin 				else if ((insn.mm_fp1_format.op & 0x7f) ==
304102cedc3SLeonid Yegoshin 					 mm_fabs0_op)
305102cedc3SLeonid Yegoshin 					func = fabs_op;
306102cedc3SLeonid Yegoshin 				else
307102cedc3SLeonid Yegoshin 					func = fneg_op;
308102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
309102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
310102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp3_format.fmt];
311102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
312102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
313102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
314102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
315102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
316102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
317102cedc3SLeonid Yegoshin 				break;
318102cedc3SLeonid Yegoshin 			case mm_ffloorl_op:
319102cedc3SLeonid Yegoshin 			case mm_ffloorw_op:
320102cedc3SLeonid Yegoshin 			case mm_fceill_op:
321102cedc3SLeonid Yegoshin 			case mm_fceilw_op:
322102cedc3SLeonid Yegoshin 			case mm_ftruncl_op:
323102cedc3SLeonid Yegoshin 			case mm_ftruncw_op:
324102cedc3SLeonid Yegoshin 			case mm_froundl_op:
325102cedc3SLeonid Yegoshin 			case mm_froundw_op:
326102cedc3SLeonid Yegoshin 			case mm_fcvtl_op:
327102cedc3SLeonid Yegoshin 			case mm_fcvtw_op:
328102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_ffloorl_op)
329102cedc3SLeonid Yegoshin 					func = ffloorl_op;
330102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ffloorw_op)
331102cedc3SLeonid Yegoshin 					func = ffloor_op;
332102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceill_op)
333102cedc3SLeonid Yegoshin 					func = fceill_op;
334102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceilw_op)
335102cedc3SLeonid Yegoshin 					func = fceil_op;
336102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncl_op)
337102cedc3SLeonid Yegoshin 					func = ftruncl_op;
338102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncw_op)
339102cedc3SLeonid Yegoshin 					func = ftrunc_op;
340102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundl_op)
341102cedc3SLeonid Yegoshin 					func = froundl_op;
342102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundw_op)
343102cedc3SLeonid Yegoshin 					func = fround_op;
344102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fcvtl_op)
345102cedc3SLeonid Yegoshin 					func = fcvtl_op;
346102cedc3SLeonid Yegoshin 				else
347102cedc3SLeonid Yegoshin 					func = fcvtw_op;
348102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
349102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
350102cedc3SLeonid Yegoshin 					sd_format[insn.mm_fp1_format.fmt];
351102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
352102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
353102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
354102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
355102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
356102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
357102cedc3SLeonid Yegoshin 				break;
358102cedc3SLeonid Yegoshin 			case mm_frsqrt_op:
359102cedc3SLeonid Yegoshin 			case mm_fsqrt_op:
360102cedc3SLeonid Yegoshin 			case mm_frecip_op:
361102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_frsqrt_op)
362102cedc3SLeonid Yegoshin 					func = frsqrt_op;
363102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fsqrt_op)
364102cedc3SLeonid Yegoshin 					func = fsqrt_op;
365102cedc3SLeonid Yegoshin 				else
366102cedc3SLeonid Yegoshin 					func = frecip_op;
367102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
368102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
369102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp1_format.fmt];
370102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
371102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
372102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
373102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
374102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
375102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
376102cedc3SLeonid Yegoshin 				break;
377102cedc3SLeonid Yegoshin 			case mm_mfc1_op:
378102cedc3SLeonid Yegoshin 			case mm_mtc1_op:
379102cedc3SLeonid Yegoshin 			case mm_cfc1_op:
380102cedc3SLeonid Yegoshin 			case mm_ctc1_op:
3819355e59cSSteven J. Hill 			case mm_mfhc1_op:
3829355e59cSSteven J. Hill 			case mm_mthc1_op:
383102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_mfc1_op)
384102cedc3SLeonid Yegoshin 					op = mfc_op;
385102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_mtc1_op)
386102cedc3SLeonid Yegoshin 					op = mtc_op;
387102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_cfc1_op)
388102cedc3SLeonid Yegoshin 					op = cfc_op;
3899355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_ctc1_op)
390102cedc3SLeonid Yegoshin 					op = ctc_op;
3919355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_mfhc1_op)
3929355e59cSSteven J. Hill 					op = mfhc_op;
3939355e59cSSteven J. Hill 				else
3949355e59cSSteven J. Hill 					op = mthc_op;
395102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.opcode = cop1_op;
396102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.op = op;
397102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.rt =
398102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
399102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fs =
400102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
401102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fd = 0;
402102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.func = 0;
403102cedc3SLeonid Yegoshin 				break;
404102cedc3SLeonid Yegoshin 			default:
405102cedc3SLeonid Yegoshin 				return SIGILL;
406102cedc3SLeonid Yegoshin 			}
407102cedc3SLeonid Yegoshin 			break;
408102cedc3SLeonid Yegoshin 		case mm_32f_74_op:	/* c.cond.fmt */
409102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.opcode = cop1_op;
410102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fmt =
411102cedc3SLeonid Yegoshin 				sdps_format[insn.mm_fp4_format.fmt];
412102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
413102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
414102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
415102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.func =
416102cedc3SLeonid Yegoshin 				insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
417102cedc3SLeonid Yegoshin 			break;
418102cedc3SLeonid Yegoshin 		default:
419102cedc3SLeonid Yegoshin 			return SIGILL;
420102cedc3SLeonid Yegoshin 		}
421102cedc3SLeonid Yegoshin 		break;
422102cedc3SLeonid Yegoshin 	default:
423102cedc3SLeonid Yegoshin 		return SIGILL;
424102cedc3SLeonid Yegoshin 	}
425102cedc3SLeonid Yegoshin 
426102cedc3SLeonid Yegoshin 	*insn_ptr = mips32_insn;
427102cedc3SLeonid Yegoshin 	return 0;
428102cedc3SLeonid Yegoshin }
429102cedc3SLeonid Yegoshin 
4301da177e4SLinus Torvalds /*
4311da177e4SLinus Torvalds  * Redundant with logic already in kernel/branch.c,
4321da177e4SLinus Torvalds  * embedded in compute_return_epc.  At some point,
4331da177e4SLinus Torvalds  * a single subroutine should be used across both
4341da177e4SLinus Torvalds  * modules.
4351da177e4SLinus Torvalds  */
436432c6bacSPaul Burton int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
437102cedc3SLeonid Yegoshin 		  unsigned long *contpc)
4381da177e4SLinus Torvalds {
439102cedc3SLeonid Yegoshin 	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
440102cedc3SLeonid Yegoshin 	unsigned int fcr31;
441102cedc3SLeonid Yegoshin 	unsigned int bit = 0;
4428bcd84a4SDouglas Leung 	unsigned int bit0;
4438bcd84a4SDouglas Leung 	union fpureg *fpr;
444102cedc3SLeonid Yegoshin 
445102cedc3SLeonid Yegoshin 	switch (insn.i_format.opcode) {
4461da177e4SLinus Torvalds 	case spec_op:
447102cedc3SLeonid Yegoshin 		switch (insn.r_format.func) {
4481da177e4SLinus Torvalds 		case jalr_op:
449ab4a92e6SPaul Burton 			if (insn.r_format.rd != 0) {
450102cedc3SLeonid Yegoshin 				regs->regs[insn.r_format.rd] =
451102cedc3SLeonid Yegoshin 					regs->cp0_epc + dec_insn.pc_inc +
452102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
453ab4a92e6SPaul Burton 			}
4542a14b21aSAleksandar Markovic 			/* fall through */
4551da177e4SLinus Torvalds 		case jr_op:
4565f9f41c4SMarkos Chandras 			/* For R6, JR already emulated in jalr_op */
457143fefc8SMarkos Chandras 			if (NO_R6EMU && insn.r_format.func == jr_op)
4585f9f41c4SMarkos Chandras 				break;
459102cedc3SLeonid Yegoshin 			*contpc = regs->regs[insn.r_format.rs];
4601da177e4SLinus Torvalds 			return 1;
4611da177e4SLinus Torvalds 		}
4621da177e4SLinus Torvalds 		break;
4631da177e4SLinus Torvalds 	case bcond_op:
464102cedc3SLeonid Yegoshin 		switch (insn.i_format.rt) {
4651da177e4SLinus Torvalds 		case bltzal_op:
4661da177e4SLinus Torvalds 		case bltzall_op:
467319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
468319824eaSMarkos Chandras 			    insn.i_format.rt == bltzall_op))
469319824eaSMarkos Chandras 				break;
470319824eaSMarkos Chandras 
471102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
472102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
473102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
4742a14b21aSAleksandar Markovic 			/* fall through */
475102cedc3SLeonid Yegoshin 		case bltzl_op:
476319824eaSMarkos Chandras 			if (NO_R6EMU)
477319824eaSMarkos Chandras 				break;
4782a14b21aSAleksandar Markovic 			/* fall through */
479319824eaSMarkos Chandras 		case bltz_op:
480102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] < 0)
481102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
482102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
483102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
484102cedc3SLeonid Yegoshin 			else
485102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
486102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
487102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
4881da177e4SLinus Torvalds 			return 1;
489102cedc3SLeonid Yegoshin 		case bgezal_op:
490102cedc3SLeonid Yegoshin 		case bgezall_op:
491319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
492319824eaSMarkos Chandras 			    insn.i_format.rt == bgezall_op))
493319824eaSMarkos Chandras 				break;
494319824eaSMarkos Chandras 
495102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
496102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
497102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
4982a14b21aSAleksandar Markovic 			/* fall through */
499102cedc3SLeonid Yegoshin 		case bgezl_op:
500319824eaSMarkos Chandras 			if (NO_R6EMU)
501319824eaSMarkos Chandras 				break;
5022a14b21aSAleksandar Markovic 			/* fall through */
503319824eaSMarkos Chandras 		case bgez_op:
504102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] >= 0)
505102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
506102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
507102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
508102cedc3SLeonid Yegoshin 			else
509102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
510102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
511102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
512102cedc3SLeonid Yegoshin 			return 1;
5131da177e4SLinus Torvalds 		}
5141da177e4SLinus Torvalds 		break;
5151da177e4SLinus Torvalds 	case jalx_op:
516102cedc3SLeonid Yegoshin 		set_isa16_mode(bit);
5172a14b21aSAleksandar Markovic 		/* fall through */
518102cedc3SLeonid Yegoshin 	case jal_op:
519102cedc3SLeonid Yegoshin 		regs->regs[31] = regs->cp0_epc +
520102cedc3SLeonid Yegoshin 			dec_insn.pc_inc +
521102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc;
5222a14b21aSAleksandar Markovic 		/* fall through */
523102cedc3SLeonid Yegoshin 	case j_op:
524102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
525102cedc3SLeonid Yegoshin 		*contpc >>= 28;
526102cedc3SLeonid Yegoshin 		*contpc <<= 28;
527102cedc3SLeonid Yegoshin 		*contpc |= (insn.j_format.target << 2);
528102cedc3SLeonid Yegoshin 		/* Set microMIPS mode bit: XOR for jalx. */
529102cedc3SLeonid Yegoshin 		*contpc ^= bit;
5301da177e4SLinus Torvalds 		return 1;
531102cedc3SLeonid Yegoshin 	case beql_op:
532319824eaSMarkos Chandras 		if (NO_R6EMU)
533319824eaSMarkos Chandras 			break;
5342a14b21aSAleksandar Markovic 		/* fall through */
535319824eaSMarkos Chandras 	case beq_op:
536102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] ==
537102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
538102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
539102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
540102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
541102cedc3SLeonid Yegoshin 		else
542102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
543102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
544102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
545102cedc3SLeonid Yegoshin 		return 1;
546102cedc3SLeonid Yegoshin 	case bnel_op:
547319824eaSMarkos Chandras 		if (NO_R6EMU)
548319824eaSMarkos Chandras 			break;
5492a14b21aSAleksandar Markovic 		/* fall through */
550319824eaSMarkos Chandras 	case bne_op:
551102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] !=
552102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
553102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
554102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
555102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
556102cedc3SLeonid Yegoshin 		else
557102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
558102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
559102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
560102cedc3SLeonid Yegoshin 		return 1;
561102cedc3SLeonid Yegoshin 	case blezl_op:
562e9d92d22SMarkos Chandras 		if (!insn.i_format.rt && NO_R6EMU)
563319824eaSMarkos Chandras 			break;
5642a14b21aSAleksandar Markovic 		/* fall through */
565319824eaSMarkos Chandras 	case blez_op:
566a8ff66f5SMarkos Chandras 
567a8ff66f5SMarkos Chandras 		/*
568a8ff66f5SMarkos Chandras 		 * Compact branches for R6 for the
569a8ff66f5SMarkos Chandras 		 * blez and blezl opcodes.
570a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = 0 | rt != 0  == BLEZALC
571a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = rt != 0      == BGEZALC
572a8ff66f5SMarkos Chandras 		 * BLEZ  | rs != 0 | rt != 0 == BGEUC
573a8ff66f5SMarkos Chandras 		 * BLEZL | rs = 0 | rt != 0  == BLEZC
574a8ff66f5SMarkos Chandras 		 * BLEZL | rs = rt != 0      == BGEZC
575a8ff66f5SMarkos Chandras 		 * BLEZL | rs != 0 | rt != 0 == BGEC
576a8ff66f5SMarkos Chandras 		 *
577a8ff66f5SMarkos Chandras 		 * For real BLEZ{,L}, rt is always 0.
578a8ff66f5SMarkos Chandras 		 */
579a8ff66f5SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
580a8ff66f5SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
581a8ff66f5SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
582a8ff66f5SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
583a8ff66f5SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
584a8ff66f5SMarkos Chandras 					dec_insn.pc_inc;
585a8ff66f5SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
586a8ff66f5SMarkos Chandras 				dec_insn.next_pc_inc;
587a8ff66f5SMarkos Chandras 
588a8ff66f5SMarkos Chandras 			return 1;
589a8ff66f5SMarkos Chandras 		}
590102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] <= 0)
591102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
592102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
593102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
594102cedc3SLeonid Yegoshin 		else
595102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
596102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
597102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
598102cedc3SLeonid Yegoshin 		return 1;
599102cedc3SLeonid Yegoshin 	case bgtzl_op:
600e9d92d22SMarkos Chandras 		if (!insn.i_format.rt && NO_R6EMU)
601319824eaSMarkos Chandras 			break;
6022a14b21aSAleksandar Markovic 		/* fall through */
603319824eaSMarkos Chandras 	case bgtz_op:
604f1b44067SMarkos Chandras 		/*
605f1b44067SMarkos Chandras 		 * Compact branches for R6 for the
606f1b44067SMarkos Chandras 		 * bgtz and bgtzl opcodes.
607f1b44067SMarkos Chandras 		 * BGTZ  | rs = 0 | rt != 0  == BGTZALC
608f1b44067SMarkos Chandras 		 * BGTZ  | rs = rt != 0      == BLTZALC
609f1b44067SMarkos Chandras 		 * BGTZ  | rs != 0 | rt != 0 == BLTUC
610f1b44067SMarkos Chandras 		 * BGTZL | rs = 0 | rt != 0  == BGTZC
611f1b44067SMarkos Chandras 		 * BGTZL | rs = rt != 0      == BLTZC
612f1b44067SMarkos Chandras 		 * BGTZL | rs != 0 | rt != 0 == BLTC
613f1b44067SMarkos Chandras 		 *
614f1b44067SMarkos Chandras 		 * *ZALC varint for BGTZ &&& rt != 0
615f1b44067SMarkos Chandras 		 * For real GTZ{,L}, rt is always 0.
616f1b44067SMarkos Chandras 		 */
617f1b44067SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
618f1b44067SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
619f1b44067SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
620f1b44067SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
621f1b44067SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
622f1b44067SMarkos Chandras 					dec_insn.pc_inc;
623f1b44067SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
624f1b44067SMarkos Chandras 				dec_insn.next_pc_inc;
625f1b44067SMarkos Chandras 
626f1b44067SMarkos Chandras 			return 1;
627f1b44067SMarkos Chandras 		}
628f1b44067SMarkos Chandras 
629102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] > 0)
630102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
631102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
632102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
633102cedc3SLeonid Yegoshin 		else
634102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
635102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
636102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
637102cedc3SLeonid Yegoshin 		return 1;
6381b492600SPaul Burton 	case pop10_op:
6391b492600SPaul Burton 	case pop30_op:
640c893ce38SMarkos Chandras 		if (!cpu_has_mips_r6)
641c893ce38SMarkos Chandras 			break;
642c893ce38SMarkos Chandras 		if (insn.i_format.rt && !insn.i_format.rs)
643c893ce38SMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
644c893ce38SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
645c893ce38SMarkos Chandras 			dec_insn.next_pc_inc;
646c893ce38SMarkos Chandras 
647c893ce38SMarkos Chandras 		return 1;
648c26d4219SDavid Daney #ifdef CONFIG_CPU_CAVIUM_OCTEON
649c26d4219SDavid Daney 	case lwc2_op: /* This is bbit0 on Octeon */
650c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
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 ldc2_op: /* This is bbit032 on Octeon */
656c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
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;
661c26d4219SDavid Daney 	case swc2_op: /* This is bbit1 on Octeon */
662c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
663c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
664c26d4219SDavid Daney 		else
665c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
666c26d4219SDavid Daney 		return 1;
667c26d4219SDavid Daney 	case sdc2_op: /* This is bbit132 on Octeon */
668c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
669c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
670c26d4219SDavid Daney 		else
671c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
672c26d4219SDavid Daney 		return 1;
6738467ca01SMarkos Chandras #else
6748467ca01SMarkos Chandras 	case bc6_op:
6758467ca01SMarkos Chandras 		/*
6768467ca01SMarkos Chandras 		 * Only valid for MIPS R6 but we can still end up
6778467ca01SMarkos Chandras 		 * here from a broken userland so just tell emulator
6788467ca01SMarkos Chandras 		 * this is not a branch and let it break later on.
6798467ca01SMarkos Chandras 		 */
6808467ca01SMarkos Chandras 		if  (!cpu_has_mips_r6)
6818467ca01SMarkos Chandras 			break;
6828467ca01SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
6838467ca01SMarkos Chandras 			dec_insn.next_pc_inc;
6848467ca01SMarkos Chandras 
6858467ca01SMarkos Chandras 		return 1;
68684fef630SMarkos Chandras 	case balc6_op:
68784fef630SMarkos Chandras 		if (!cpu_has_mips_r6)
68884fef630SMarkos Chandras 			break;
68984fef630SMarkos Chandras 		regs->regs[31] = regs->cp0_epc + 4;
69084fef630SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
69184fef630SMarkos Chandras 			dec_insn.next_pc_inc;
69284fef630SMarkos Chandras 
69384fef630SMarkos Chandras 		return 1;
6941c66b79bSPaul Burton 	case pop66_op:
69569b9a2fdSMarkos Chandras 		if (!cpu_has_mips_r6)
69669b9a2fdSMarkos Chandras 			break;
69769b9a2fdSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
69869b9a2fdSMarkos Chandras 			dec_insn.next_pc_inc;
69969b9a2fdSMarkos Chandras 
70069b9a2fdSMarkos Chandras 		return 1;
7011c66b79bSPaul Burton 	case pop76_op:
70228d6f93dSMarkos Chandras 		if (!cpu_has_mips_r6)
70328d6f93dSMarkos Chandras 			break;
70428d6f93dSMarkos Chandras 		if (!insn.i_format.rs)
70528d6f93dSMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
70628d6f93dSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
70728d6f93dSMarkos Chandras 			dec_insn.next_pc_inc;
70828d6f93dSMarkos Chandras 
70928d6f93dSMarkos Chandras 		return 1;
710c26d4219SDavid Daney #endif
7111da177e4SLinus Torvalds 	case cop0_op:
7121da177e4SLinus Torvalds 	case cop1_op:
713c8a34581SMarkos Chandras 		/* Need to check for R6 bc1nez and bc1eqz branches */
714c8a34581SMarkos Chandras 		if (cpu_has_mips_r6 &&
715c8a34581SMarkos Chandras 		    ((insn.i_format.rs == bc1eqz_op) ||
716c8a34581SMarkos Chandras 		     (insn.i_format.rs == bc1nez_op))) {
717c8a34581SMarkos Chandras 			bit = 0;
7188bcd84a4SDouglas Leung 			fpr = &current->thread.fpu.fpr[insn.i_format.rt];
7198bcd84a4SDouglas Leung 			bit0 = get_fpr32(fpr, 0) & 0x1;
720c8a34581SMarkos Chandras 			switch (insn.i_format.rs) {
721c8a34581SMarkos Chandras 			case bc1eqz_op:
7228bcd84a4SDouglas Leung 				bit = bit0 == 0;
723c8a34581SMarkos Chandras 				break;
724c8a34581SMarkos Chandras 			case bc1nez_op:
7258bcd84a4SDouglas Leung 				bit = bit0 != 0;
726c8a34581SMarkos Chandras 				break;
727c8a34581SMarkos Chandras 			}
728c8a34581SMarkos Chandras 			if (bit)
729c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
730c8a34581SMarkos Chandras 					dec_insn.pc_inc +
731c8a34581SMarkos Chandras 					(insn.i_format.simmediate << 2);
732c8a34581SMarkos Chandras 			else
733c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
734c8a34581SMarkos Chandras 					dec_insn.pc_inc +
735c8a34581SMarkos Chandras 					dec_insn.next_pc_inc;
736c8a34581SMarkos Chandras 
737c8a34581SMarkos Chandras 			return 1;
738c8a34581SMarkos Chandras 		}
7392a14b21aSAleksandar Markovic 		/* R2/R6 compatible cop1 instruction */
7402a14b21aSAleksandar Markovic 		/* fall through */
7411da177e4SLinus Torvalds 	case cop2_op:
7421da177e4SLinus Torvalds 	case cop1x_op:
743102cedc3SLeonid Yegoshin 		if (insn.i_format.rs == bc_op) {
744102cedc3SLeonid Yegoshin 			preempt_disable();
745102cedc3SLeonid Yegoshin 			if (is_fpu_owner())
746842dfc11SManuel Lauss 			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
747102cedc3SLeonid Yegoshin 			else
748102cedc3SLeonid Yegoshin 				fcr31 = current->thread.fpu.fcr31;
749102cedc3SLeonid Yegoshin 			preempt_enable();
750102cedc3SLeonid Yegoshin 
751102cedc3SLeonid Yegoshin 			bit = (insn.i_format.rt >> 2);
752102cedc3SLeonid Yegoshin 			bit += (bit != 0);
753102cedc3SLeonid Yegoshin 			bit += 23;
754102cedc3SLeonid Yegoshin 			switch (insn.i_format.rt & 3) {
755102cedc3SLeonid Yegoshin 			case 0:	/* bc1f */
756102cedc3SLeonid Yegoshin 			case 2:	/* bc1fl */
757102cedc3SLeonid Yegoshin 				if (~fcr31 & (1 << bit))
758102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
759102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
760102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
761102cedc3SLeonid Yegoshin 				else
762102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
763102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
764102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
765102cedc3SLeonid Yegoshin 				return 1;
766102cedc3SLeonid Yegoshin 			case 1:	/* bc1t */
767102cedc3SLeonid Yegoshin 			case 3:	/* bc1tl */
768102cedc3SLeonid Yegoshin 				if (fcr31 & (1 << bit))
769102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
770102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
771102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
772102cedc3SLeonid Yegoshin 				else
773102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
774102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
775102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
7761da177e4SLinus Torvalds 				return 1;
7771da177e4SLinus Torvalds 			}
778102cedc3SLeonid Yegoshin 		}
779102cedc3SLeonid Yegoshin 		break;
780102cedc3SLeonid Yegoshin 	}
7811da177e4SLinus Torvalds 	return 0;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds /*
7851da177e4SLinus Torvalds  * In the Linux kernel, we support selection of FPR format on the
786da0bac33SDavid Daney  * basis of the Status.FR bit.	If an FPU is not present, the FR bit
787da0bac33SDavid Daney  * is hardwired to zero, which would imply a 32-bit FPU even for
788597ce172SPaul Burton  * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
78951d943f0SRalf Baechle  * FPU emu is slow and bulky and optimizing this function offers fairly
79051d943f0SRalf Baechle  * sizeable benefits so we try to be clever and make this function return
79151d943f0SRalf Baechle  * a constant whenever possible, that is on 64-bit kernels without O32
792597ce172SPaul Burton  * compatibility enabled and on 32-bit without 64-bit FPU support.
7931da177e4SLinus Torvalds  */
794da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp)
795da0bac33SDavid Daney {
79697f2645fSMasahiro Yamada 	if (IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_MIPS32_O32))
79751d943f0SRalf Baechle 		return 1;
79897f2645fSMasahiro Yamada 	else if (IS_ENABLED(CONFIG_32BIT) &&
79997f2645fSMasahiro Yamada 		 !IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
800da0bac33SDavid Daney 		return 0;
80108a07904SRalf Baechle 
802597ce172SPaul Burton 	return !test_thread_flag(TIF_32BIT_FPREGS);
803da0bac33SDavid Daney }
8041da177e4SLinus Torvalds 
8054227a2d4SPaul Burton static inline bool hybrid_fprs(void)
8064227a2d4SPaul Burton {
8074227a2d4SPaul Burton 	return test_thread_flag(TIF_HYBRID_FPREGS);
8084227a2d4SPaul Burton }
8094227a2d4SPaul Burton 
81047fa0c02SRalf Baechle #define SIFROMREG(si, x)						\
81147fa0c02SRalf Baechle do {									\
8124227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs())				\
813c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[x], 0);			\
814bbd426f5SPaul Burton 	else								\
815c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);	\
816bbd426f5SPaul Burton } while (0)
817da0bac33SDavid Daney 
81847fa0c02SRalf Baechle #define SITOREG(si, x)							\
81947fa0c02SRalf Baechle do {									\
8204227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs()) {			\
821a58f85b5SAleksandar Markovic 		unsigned int i;						\
822bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[x], 0, si);				\
823ef1c47afSPaul Burton 		for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)	\
824ef1c47afSPaul Burton 			set_fpr32(&ctx->fpr[x], i, 0);			\
825ef1c47afSPaul Burton 	} else {							\
826bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);		\
827ef1c47afSPaul Burton 	}								\
828bbd426f5SPaul Burton } while (0)
8291da177e4SLinus Torvalds 
830c8c0da6bSPaul Burton #define SIFROMHREG(si, x)	((si) = (int)get_fpr32(&ctx->fpr[x], 1))
831ef1c47afSPaul Burton 
83247fa0c02SRalf Baechle #define SITOHREG(si, x)							\
83347fa0c02SRalf Baechle do {									\
834a58f85b5SAleksandar Markovic 	unsigned int i;							\
835ef1c47afSPaul Burton 	set_fpr32(&ctx->fpr[x], 1, si);					\
836ef1c47afSPaul Burton 	for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)		\
837ef1c47afSPaul Burton 		set_fpr32(&ctx->fpr[x], i, 0);				\
838ef1c47afSPaul Burton } while (0)
8391ac94400SLeonid Yegoshin 
840bbd426f5SPaul Burton #define DIFROMREG(di, x)						\
8418535f2baSManuel Lauss 	((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) ^ 1)], 0))
842bbd426f5SPaul Burton 
84347fa0c02SRalf Baechle #define DITOREG(di, x)							\
84447fa0c02SRalf Baechle do {									\
845a58f85b5SAleksandar Markovic 	unsigned int fpr, i;						\
8468535f2baSManuel Lauss 	fpr = (x) & ~(cop1_64bit(xcp) ^ 1);				\
847ef1c47afSPaul Burton 	set_fpr64(&ctx->fpr[fpr], 0, di);				\
848ef1c47afSPaul Burton 	for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++)		\
849ef1c47afSPaul Burton 		set_fpr64(&ctx->fpr[fpr], i, 0);			\
850ef1c47afSPaul Burton } while (0)
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
8531da177e4SLinus Torvalds #define SPTOREG(sp, x)	SITOREG((sp).bits, x)
8541da177e4SLinus Torvalds #define DPFROMREG(dp, x)	DIFROMREG((dp).bits, x)
8551da177e4SLinus Torvalds #define DPTOREG(dp, x)	DITOREG((dp).bits, x)
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds /*
858d4f5b088SMaciej W. Rozycki  * Emulate a CFC1 instruction.
859d4f5b088SMaciej W. Rozycki  */
860d4f5b088SMaciej W. Rozycki static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
861d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
862d4f5b088SMaciej W. Rozycki {
863c491cfa2SMaciej W. Rozycki 	u32 fcr31 = ctx->fcr31;
864c491cfa2SMaciej W. Rozycki 	u32 value = 0;
865d4f5b088SMaciej W. Rozycki 
866c491cfa2SMaciej W. Rozycki 	switch (MIPSInst_RD(ir)) {
867c491cfa2SMaciej W. Rozycki 	case FPCREG_CSR:
868c491cfa2SMaciej W. Rozycki 		value = fcr31;
869d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-csr=%08x\n",
870c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
871c491cfa2SMaciej W. Rozycki 		break;
872c491cfa2SMaciej W. Rozycki 
873c491cfa2SMaciej W. Rozycki 	case FPCREG_FENR:
874c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
875c491cfa2SMaciej W. Rozycki 			break;
876c491cfa2SMaciej W. Rozycki 		value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
877c491cfa2SMaciej W. Rozycki 			MIPS_FENR_FS;
878c491cfa2SMaciej W. Rozycki 		value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
879c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-enr=%08x\n",
880c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
881c491cfa2SMaciej W. Rozycki 		break;
882c491cfa2SMaciej W. Rozycki 
883c491cfa2SMaciej W. Rozycki 	case FPCREG_FEXR:
884c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
885c491cfa2SMaciej W. Rozycki 			break;
886c491cfa2SMaciej W. Rozycki 		value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
887c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-exr=%08x\n",
888c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
889c491cfa2SMaciej W. Rozycki 		break;
890c491cfa2SMaciej W. Rozycki 
891c491cfa2SMaciej W. Rozycki 	case FPCREG_FCCR:
892c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
893c491cfa2SMaciej W. Rozycki 			break;
894c491cfa2SMaciej W. Rozycki 		value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
895c491cfa2SMaciej W. Rozycki 			MIPS_FCCR_COND0;
896c491cfa2SMaciej W. Rozycki 		value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
897c491cfa2SMaciej W. Rozycki 			 (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
898c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-ccr=%08x\n",
899c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
900c491cfa2SMaciej W. Rozycki 		break;
901c491cfa2SMaciej W. Rozycki 
902c491cfa2SMaciej W. Rozycki 	case FPCREG_RID:
90303dce595SMaciej W. Rozycki 		value = boot_cpu_data.fpu_id;
904c491cfa2SMaciej W. Rozycki 		break;
905c491cfa2SMaciej W. Rozycki 
906c491cfa2SMaciej W. Rozycki 	default:
907c491cfa2SMaciej W. Rozycki 		break;
908c491cfa2SMaciej W. Rozycki 	}
909c491cfa2SMaciej W. Rozycki 
910d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir))
911d4f5b088SMaciej W. Rozycki 		xcp->regs[MIPSInst_RT(ir)] = value;
912d4f5b088SMaciej W. Rozycki }
913d4f5b088SMaciej W. Rozycki 
914d4f5b088SMaciej W. Rozycki /*
915d4f5b088SMaciej W. Rozycki  * Emulate a CTC1 instruction.
916d4f5b088SMaciej W. Rozycki  */
917d4f5b088SMaciej W. Rozycki static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
918d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
919d4f5b088SMaciej W. Rozycki {
920c491cfa2SMaciej W. Rozycki 	u32 fcr31 = ctx->fcr31;
921d4f5b088SMaciej W. Rozycki 	u32 value;
9229b26616cSMaciej W. Rozycki 	u32 mask;
923d4f5b088SMaciej W. Rozycki 
924d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir) == 0)
925d4f5b088SMaciej W. Rozycki 		value = 0;
926d4f5b088SMaciej W. Rozycki 	else
927d4f5b088SMaciej W. Rozycki 		value = xcp->regs[MIPSInst_RT(ir)];
928d4f5b088SMaciej W. Rozycki 
929c491cfa2SMaciej W. Rozycki 	switch (MIPSInst_RD(ir)) {
930c491cfa2SMaciej W. Rozycki 	case FPCREG_CSR:
931d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->csr=%08x\n",
932c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
933d4f5b088SMaciej W. Rozycki 
9349b26616cSMaciej W. Rozycki 		/* Preserve read-only bits.  */
93503dce595SMaciej W. Rozycki 		mask = boot_cpu_data.fpu_msk31;
9369b26616cSMaciej W. Rozycki 		fcr31 = (value & ~mask) | (fcr31 & mask);
937c491cfa2SMaciej W. Rozycki 		break;
938c491cfa2SMaciej W. Rozycki 
939c491cfa2SMaciej W. Rozycki 	case FPCREG_FENR:
940c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
941c491cfa2SMaciej W. Rozycki 			break;
942c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->enr=%08x\n",
943c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
944c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
945c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
946c491cfa2SMaciej W. Rozycki 			 FPU_CSR_FS;
947c491cfa2SMaciej W. Rozycki 		fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
948c491cfa2SMaciej W. Rozycki 		break;
949c491cfa2SMaciej W. Rozycki 
950c491cfa2SMaciej W. Rozycki 	case FPCREG_FEXR:
951c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
952c491cfa2SMaciej W. Rozycki 			break;
953c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->exr=%08x\n",
954c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
955c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
956c491cfa2SMaciej W. Rozycki 		fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
957c491cfa2SMaciej W. Rozycki 		break;
958c491cfa2SMaciej W. Rozycki 
959c491cfa2SMaciej W. Rozycki 	case FPCREG_FCCR:
960c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
961c491cfa2SMaciej W. Rozycki 			break;
962c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->ccr=%08x\n",
963c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
964c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
965c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
966c491cfa2SMaciej W. Rozycki 			 FPU_CSR_COND;
967c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
968c491cfa2SMaciej W. Rozycki 			 FPU_CSR_CONDX;
969c491cfa2SMaciej W. Rozycki 		break;
970c491cfa2SMaciej W. Rozycki 
971c491cfa2SMaciej W. Rozycki 	default:
972c491cfa2SMaciej W. Rozycki 		break;
973d4f5b088SMaciej W. Rozycki 	}
974c491cfa2SMaciej W. Rozycki 
975c491cfa2SMaciej W. Rozycki 	ctx->fcr31 = fcr31;
976d4f5b088SMaciej W. Rozycki }
977d4f5b088SMaciej W. Rozycki 
978d4f5b088SMaciej W. Rozycki /*
9791da177e4SLinus Torvalds  * Emulate the single floating point instruction pointed at by EPC.
9801da177e4SLinus Torvalds  * Two instructions if the instruction is in a branch delay slot.
9811da177e4SLinus Torvalds  */
9821da177e4SLinus Torvalds 
983515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
984445a58ceSPaul Burton 		struct mm_decoded_insn dec_insn, void __user **fault_addr)
9851da177e4SLinus Torvalds {
986102cedc3SLeonid Yegoshin 	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
98793583e17SPaul Burton 	unsigned int cond, cbit, bit0;
9883f7cac41SRalf Baechle 	mips_instruction ir;
9893f7cac41SRalf Baechle 	int likely, pc_inc;
99093583e17SPaul Burton 	union fpureg *fpr;
9913f7cac41SRalf Baechle 	u32 __user *wva;
9923f7cac41SRalf Baechle 	u64 __user *dva;
9933f7cac41SRalf Baechle 	u32 wval;
9943f7cac41SRalf Baechle 	u64 dval;
9953f7cac41SRalf Baechle 	int sig;
9961da177e4SLinus Torvalds 
99770e4c234SRalf Baechle 	/*
99870e4c234SRalf Baechle 	 * These are giving gcc a gentle hint about what to expect in
99970e4c234SRalf Baechle 	 * dec_inst in order to do better optimization.
100070e4c234SRalf Baechle 	 */
100170e4c234SRalf Baechle 	if (!cpu_has_mmips && dec_insn.micro_mips_mode)
100270e4c234SRalf Baechle 		unreachable();
100370e4c234SRalf Baechle 
10041da177e4SLinus Torvalds 	/* XXX NEC Vr54xx bug workaround */
1005e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
1006102cedc3SLeonid Yegoshin 		if (dec_insn.micro_mips_mode) {
1007102cedc3SLeonid Yegoshin 			if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
1008e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
1009102cedc3SLeonid Yegoshin 		} else {
1010102cedc3SLeonid Yegoshin 			if (!isBranchInstr(xcp, dec_insn, &contpc))
1011e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
1012102cedc3SLeonid Yegoshin 		}
1013102cedc3SLeonid Yegoshin 	}
10141da177e4SLinus Torvalds 
1015e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
10161da177e4SLinus Torvalds 		/*
10171da177e4SLinus Torvalds 		 * The instruction to be emulated is in a branch delay slot
10181da177e4SLinus Torvalds 		 * which means that we have to	emulate the branch instruction
10191da177e4SLinus Torvalds 		 * BEFORE we do the cop1 instruction.
10201da177e4SLinus Torvalds 		 *
10211da177e4SLinus Torvalds 		 * This branch could be a COP1 branch, but in that case we
10221da177e4SLinus Torvalds 		 * would have had a trap for that instruction, and would not
10231da177e4SLinus Torvalds 		 * come through this route.
10241da177e4SLinus Torvalds 		 *
10251da177e4SLinus Torvalds 		 * Linux MIPS branch emulator operates on context, updating the
10261da177e4SLinus Torvalds 		 * cp0_epc.
10271da177e4SLinus Torvalds 		 */
1028102cedc3SLeonid Yegoshin 		ir = dec_insn.next_insn;  /* process delay slot instr */
1029102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.next_pc_inc;
1030333d1f67SRalf Baechle 	} else {
1031102cedc3SLeonid Yegoshin 		ir = dec_insn.insn;       /* process current instr */
1032102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.pc_inc;
1033102cedc3SLeonid Yegoshin 	}
1034102cedc3SLeonid Yegoshin 
1035102cedc3SLeonid Yegoshin 	/*
1036102cedc3SLeonid Yegoshin 	 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
1037102cedc3SLeonid Yegoshin 	 * instructions, we want to convert microMIPS FPU instructions
1038102cedc3SLeonid Yegoshin 	 * into MIPS32 instructions so that we could reuse all of the
1039102cedc3SLeonid Yegoshin 	 * FPU emulation code.
1040102cedc3SLeonid Yegoshin 	 *
1041102cedc3SLeonid Yegoshin 	 * NOTE: We cannot do this for branch instructions since they
1042102cedc3SLeonid Yegoshin 	 *       are not a subset. Example: Cannot emulate a 16-bit
1043102cedc3SLeonid Yegoshin 	 *       aligned target address with a MIPS32 instruction.
1044102cedc3SLeonid Yegoshin 	 */
1045102cedc3SLeonid Yegoshin 	if (dec_insn.micro_mips_mode) {
1046102cedc3SLeonid Yegoshin 		/*
1047102cedc3SLeonid Yegoshin 		 * If next instruction is a 16-bit instruction, then it
1048102cedc3SLeonid Yegoshin 		 * it cannot be a FPU instruction. This could happen
1049102cedc3SLeonid Yegoshin 		 * since we can be called for non-FPU instructions.
1050102cedc3SLeonid Yegoshin 		 */
1051102cedc3SLeonid Yegoshin 		if ((pc_inc == 2) ||
1052102cedc3SLeonid Yegoshin 			(microMIPS32_to_MIPS32((union mips_instruction *)&ir)
1053102cedc3SLeonid Yegoshin 			 == SIGILL))
1054102cedc3SLeonid Yegoshin 			return SIGILL;
10551da177e4SLinus Torvalds 	}
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds emul:
1058a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
1059b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(emulated);
10601da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(ir)) {
10613f7cac41SRalf Baechle 	case ldc1_op:
10623f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10631da177e4SLinus Torvalds 				     MIPSInst_SIMM(ir));
1064b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
1065515b029dSDavid Daney 
10663f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
1067b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10683f7cac41SRalf Baechle 			*fault_addr = dva;
10691da177e4SLinus Torvalds 			return SIGBUS;
10701da177e4SLinus Torvalds 		}
10713f7cac41SRalf Baechle 		if (__get_user(dval, dva)) {
1072515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10733f7cac41SRalf Baechle 			*fault_addr = dva;
1074515b029dSDavid Daney 			return SIGSEGV;
1075515b029dSDavid Daney 		}
10763f7cac41SRalf Baechle 		DITOREG(dval, MIPSInst_RT(ir));
10771da177e4SLinus Torvalds 		break;
10781da177e4SLinus Torvalds 
10793f7cac41SRalf Baechle 	case sdc1_op:
10803f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10811da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1082b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
10833f7cac41SRalf Baechle 		DIFROMREG(dval, MIPSInst_RT(ir));
10843f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
1085b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10863f7cac41SRalf Baechle 			*fault_addr = dva;
10871da177e4SLinus Torvalds 			return SIGBUS;
10881da177e4SLinus Torvalds 		}
10893f7cac41SRalf Baechle 		if (__put_user(dval, dva)) {
1090515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10913f7cac41SRalf Baechle 			*fault_addr = dva;
1092515b029dSDavid Daney 			return SIGSEGV;
1093515b029dSDavid Daney 		}
10941da177e4SLinus Torvalds 		break;
10951da177e4SLinus Torvalds 
10963f7cac41SRalf Baechle 	case lwc1_op:
10973f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10981da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1099b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
11003f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
1101b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11023f7cac41SRalf Baechle 			*fault_addr = wva;
11031da177e4SLinus Torvalds 			return SIGBUS;
11041da177e4SLinus Torvalds 		}
11053f7cac41SRalf Baechle 		if (__get_user(wval, wva)) {
1106515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11073f7cac41SRalf Baechle 			*fault_addr = wva;
1108515b029dSDavid Daney 			return SIGSEGV;
1109515b029dSDavid Daney 		}
11103f7cac41SRalf Baechle 		SITOREG(wval, MIPSInst_RT(ir));
11111da177e4SLinus Torvalds 		break;
11121da177e4SLinus Torvalds 
11133f7cac41SRalf Baechle 	case swc1_op:
11143f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
11151da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1116b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
11173f7cac41SRalf Baechle 		SIFROMREG(wval, MIPSInst_RT(ir));
11183f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
1119b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11203f7cac41SRalf Baechle 			*fault_addr = wva;
11211da177e4SLinus Torvalds 			return SIGBUS;
11221da177e4SLinus Torvalds 		}
11233f7cac41SRalf Baechle 		if (__put_user(wval, wva)) {
1124515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11253f7cac41SRalf Baechle 			*fault_addr = wva;
1126515b029dSDavid Daney 			return SIGSEGV;
1127515b029dSDavid Daney 		}
11281da177e4SLinus Torvalds 		break;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	case cop1_op:
11311da177e4SLinus Torvalds 		switch (MIPSInst_RS(ir)) {
11321da177e4SLinus Torvalds 		case dmfc_op:
113308a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
113408a07904SRalf Baechle 				return SIGILL;
113508a07904SRalf Baechle 
11361da177e4SLinus Torvalds 			/* copregister fs -> gpr[rt] */
11371da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
11381da177e4SLinus Torvalds 				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
11391da177e4SLinus Torvalds 					MIPSInst_RD(ir));
11401da177e4SLinus Torvalds 			}
11411da177e4SLinus Torvalds 			break;
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 		case dmtc_op:
114408a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
114508a07904SRalf Baechle 				return SIGILL;
114608a07904SRalf Baechle 
11471da177e4SLinus Torvalds 			/* copregister fs <- rt */
11481da177e4SLinus Torvalds 			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11491da177e4SLinus Torvalds 			break;
11501da177e4SLinus Torvalds 
11511ac94400SLeonid Yegoshin 		case mfhc_op:
1152e8f80cc1SMarkos Chandras 			if (!cpu_has_mips_r2_r6)
115370f743d1SMaciej W. Rozycki 				return SIGILL;
11541ac94400SLeonid Yegoshin 
11551ac94400SLeonid Yegoshin 			/* copregister rd -> gpr[rt] */
11561ac94400SLeonid Yegoshin 			if (MIPSInst_RT(ir) != 0) {
11571ac94400SLeonid Yegoshin 				SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
11581ac94400SLeonid Yegoshin 					MIPSInst_RD(ir));
11591ac94400SLeonid Yegoshin 			}
11601ac94400SLeonid Yegoshin 			break;
11611ac94400SLeonid Yegoshin 
11621ac94400SLeonid Yegoshin 		case mthc_op:
1163e8f80cc1SMarkos Chandras 			if (!cpu_has_mips_r2_r6)
116470f743d1SMaciej W. Rozycki 				return SIGILL;
11651ac94400SLeonid Yegoshin 
11661ac94400SLeonid Yegoshin 			/* copregister rd <- gpr[rt] */
11671ac94400SLeonid Yegoshin 			SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11681ac94400SLeonid Yegoshin 			break;
11691ac94400SLeonid Yegoshin 
11701da177e4SLinus Torvalds 		case mfc_op:
11711da177e4SLinus Torvalds 			/* copregister rd -> gpr[rt] */
11721da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
11731da177e4SLinus Torvalds 				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
11741da177e4SLinus Torvalds 					MIPSInst_RD(ir));
11751da177e4SLinus Torvalds 			}
11761da177e4SLinus Torvalds 			break;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 		case mtc_op:
11791da177e4SLinus Torvalds 			/* copregister rd <- rt */
11801da177e4SLinus Torvalds 			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11811da177e4SLinus Torvalds 			break;
11821da177e4SLinus Torvalds 
11833f7cac41SRalf Baechle 		case cfc_op:
11841da177e4SLinus Torvalds 			/* cop control register rd -> gpr[rt] */
1185d4f5b088SMaciej W. Rozycki 			cop1_cfc(xcp, ctx, ir);
11861da177e4SLinus Torvalds 			break;
11871da177e4SLinus Torvalds 
11883f7cac41SRalf Baechle 		case ctc_op:
11891da177e4SLinus Torvalds 			/* copregister rd <- rt */
1190d4f5b088SMaciej W. Rozycki 			cop1_ctc(xcp, ctx, ir);
11911da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
11921da177e4SLinus Torvalds 				return SIGFPE;
11931da177e4SLinus Torvalds 			}
11941da177e4SLinus Torvalds 			break;
11951da177e4SLinus Torvalds 
1196c909ca71SMarkos Chandras 		case bc1eqz_op:
1197c909ca71SMarkos Chandras 		case bc1nez_op:
1198c909ca71SMarkos Chandras 			if (!cpu_has_mips_r6 || delay_slot(xcp))
1199c909ca71SMarkos Chandras 				return SIGILL;
1200c909ca71SMarkos Chandras 
120161100500SAleksandar Markovic 			likely = 0;
120261100500SAleksandar Markovic 			cond = 0;
120393583e17SPaul Burton 			fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
120493583e17SPaul Burton 			bit0 = get_fpr32(fpr, 0) & 0x1;
1205c909ca71SMarkos Chandras 			switch (MIPSInst_RS(ir)) {
1206c909ca71SMarkos Chandras 			case bc1eqz_op:
1207454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(bc1eqz);
120893583e17SPaul Burton 				cond = bit0 == 0;
1209c909ca71SMarkos Chandras 				break;
1210c909ca71SMarkos Chandras 			case bc1nez_op:
1211454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(bc1nez);
121293583e17SPaul Burton 				cond = bit0 != 0;
1213c909ca71SMarkos Chandras 				break;
1214c909ca71SMarkos Chandras 			}
1215c909ca71SMarkos Chandras 			goto branch_common;
1216c909ca71SMarkos Chandras 
12173f7cac41SRalf Baechle 		case bc_op:
1218e7e9cae5SRalf Baechle 			if (delay_slot(xcp))
12191da177e4SLinus Torvalds 				return SIGILL;
12201da177e4SLinus Torvalds 
122108a07904SRalf Baechle 			if (cpu_has_mips_4_5_r)
122208a07904SRalf Baechle 				cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
122308a07904SRalf Baechle 			else
122408a07904SRalf Baechle 				cbit = FPU_CSR_COND;
122508a07904SRalf Baechle 			cond = ctx->fcr31 & cbit;
122608a07904SRalf Baechle 
12273f7cac41SRalf Baechle 			likely = 0;
12281da177e4SLinus Torvalds 			switch (MIPSInst_RT(ir) & 3) {
12291da177e4SLinus Torvalds 			case bcfl_op:
12302d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
12311da177e4SLinus Torvalds 					likely = 1;
12322a14b21aSAleksandar Markovic 				/* fall through */
12331da177e4SLinus Torvalds 			case bcf_op:
12341da177e4SLinus Torvalds 				cond = !cond;
12351da177e4SLinus Torvalds 				break;
12361da177e4SLinus Torvalds 			case bctl_op:
12372d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
12381da177e4SLinus Torvalds 					likely = 1;
12392a14b21aSAleksandar Markovic 				/* fall through */
12401da177e4SLinus Torvalds 			case bct_op:
12411da177e4SLinus Torvalds 				break;
12421da177e4SLinus Torvalds 			}
1243c909ca71SMarkos Chandras branch_common:
1244ae5f3f5bSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(branches);
1245e7e9cae5SRalf Baechle 			set_delay_slot(xcp);
12461da177e4SLinus Torvalds 			if (cond) {
12473f7cac41SRalf Baechle 				/*
12483f7cac41SRalf Baechle 				 * Branch taken: emulate dslot instruction
12491da177e4SLinus Torvalds 				 */
12509ab4471cSMaciej W. Rozycki 				unsigned long bcpc;
12519ab4471cSMaciej W. Rozycki 
12529ab4471cSMaciej W. Rozycki 				/*
12539ab4471cSMaciej W. Rozycki 				 * Remember EPC at the branch to point back
12549ab4471cSMaciej W. Rozycki 				 * at so that any delay-slot instruction
12559ab4471cSMaciej W. Rozycki 				 * signal is not silently ignored.
12569ab4471cSMaciej W. Rozycki 				 */
12579ab4471cSMaciej W. Rozycki 				bcpc = xcp->cp0_epc;
1258102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
12591da177e4SLinus Torvalds 
1260102cedc3SLeonid Yegoshin 				contpc = MIPSInst_SIMM(ir);
1261102cedc3SLeonid Yegoshin 				ir = dec_insn.next_insn;
1262102cedc3SLeonid Yegoshin 				if (dec_insn.micro_mips_mode) {
1263102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 1));
1264102cedc3SLeonid Yegoshin 
1265102cedc3SLeonid Yegoshin 					/* If 16-bit instruction, not FPU. */
1266102cedc3SLeonid Yegoshin 					if ((dec_insn.next_pc_inc == 2) ||
1267102cedc3SLeonid Yegoshin 						(microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1268102cedc3SLeonid Yegoshin 
1269102cedc3SLeonid Yegoshin 						/*
1270102cedc3SLeonid Yegoshin 						 * Since this instruction will
1271102cedc3SLeonid Yegoshin 						 * be put on the stack with
1272102cedc3SLeonid Yegoshin 						 * 32-bit words, get around
1273102cedc3SLeonid Yegoshin 						 * this problem by putting a
1274102cedc3SLeonid Yegoshin 						 * NOP16 as the second one.
1275102cedc3SLeonid Yegoshin 						 */
1276102cedc3SLeonid Yegoshin 						if (dec_insn.next_pc_inc == 2)
1277102cedc3SLeonid Yegoshin 							ir = (ir & (~0xffff)) | MM_NOP16;
1278102cedc3SLeonid Yegoshin 
1279102cedc3SLeonid Yegoshin 						/*
1280102cedc3SLeonid Yegoshin 						 * Single step the non-CP1
1281102cedc3SLeonid Yegoshin 						 * instruction in the dslot.
1282102cedc3SLeonid Yegoshin 						 */
12839ab4471cSMaciej W. Rozycki 						sig = mips_dsemul(xcp, ir,
1284432c6bacSPaul Burton 								  bcpc, contpc);
1285e4553573SMaciej W. Rozycki 						if (sig < 0)
1286e4553573SMaciej W. Rozycki 							break;
12879ab4471cSMaciej W. Rozycki 						if (sig)
12889ab4471cSMaciej W. Rozycki 							xcp->cp0_epc = bcpc;
12899ab4471cSMaciej W. Rozycki 						/*
12909ab4471cSMaciej W. Rozycki 						 * SIGILL forces out of
12919ab4471cSMaciej W. Rozycki 						 * the emulation loop.
12929ab4471cSMaciej W. Rozycki 						 */
12939ab4471cSMaciej W. Rozycki 						return sig ? sig : SIGILL;
1294515b029dSDavid Daney 					}
1295102cedc3SLeonid Yegoshin 				} else
1296102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 2));
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 				switch (MIPSInst_OPCODE(ir)) {
12991da177e4SLinus Torvalds 				case lwc1_op:
13001da177e4SLinus Torvalds 				case swc1_op:
130108a07904SRalf Baechle 					goto emul;
13023f7cac41SRalf Baechle 
13031da177e4SLinus Torvalds 				case ldc1_op:
13041da177e4SLinus Torvalds 				case sdc1_op:
13052d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_2_3_4_5_r)
130608a07904SRalf Baechle 						goto emul;
130708a07904SRalf Baechle 
13089ab4471cSMaciej W. Rozycki 					goto bc_sigill;
13093f7cac41SRalf Baechle 
13101da177e4SLinus Torvalds 				case cop1_op:
131108a07904SRalf Baechle 					goto emul;
13123f7cac41SRalf Baechle 
13131da177e4SLinus Torvalds 				case cop1x_op:
13142d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_4_5_64_r2_r6)
13151da177e4SLinus Torvalds 						/* its one of ours */
13161da177e4SLinus Torvalds 						goto emul;
131708a07904SRalf Baechle 
13189ab4471cSMaciej W. Rozycki 					goto bc_sigill;
13193f7cac41SRalf Baechle 
13201da177e4SLinus Torvalds 				case spec_op:
13212d83fea7SMaciej W. Rozycki 					switch (MIPSInst_FUNC(ir)) {
13222d83fea7SMaciej W. Rozycki 					case movc_op:
13232d83fea7SMaciej W. Rozycki 						if (cpu_has_mips_4_5_r)
13241da177e4SLinus Torvalds 							goto emul;
13252d83fea7SMaciej W. Rozycki 
13269ab4471cSMaciej W. Rozycki 						goto bc_sigill;
13272d83fea7SMaciej W. Rozycki 					}
13281da177e4SLinus Torvalds 					break;
13299ab4471cSMaciej W. Rozycki 
13309ab4471cSMaciej W. Rozycki 				bc_sigill:
13319ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
13329ab4471cSMaciej W. Rozycki 					return SIGILL;
13331da177e4SLinus Torvalds 				}
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 				/*
13361da177e4SLinus Torvalds 				 * Single step the non-cp1
13371da177e4SLinus Torvalds 				 * instruction in the dslot
13381da177e4SLinus Torvalds 				 */
1339432c6bacSPaul Burton 				sig = mips_dsemul(xcp, ir, bcpc, contpc);
1340e4553573SMaciej W. Rozycki 				if (sig < 0)
1341e4553573SMaciej W. Rozycki 					break;
13429ab4471cSMaciej W. Rozycki 				if (sig)
13439ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
13449ab4471cSMaciej W. Rozycki 				/* SIGILL forces out of the emulation loop.  */
13459ab4471cSMaciej W. Rozycki 				return sig ? sig : SIGILL;
13463f7cac41SRalf Baechle 			} else if (likely) {	/* branch not taken */
13471da177e4SLinus Torvalds 				/*
13481da177e4SLinus Torvalds 				 * branch likely nullifies
13491da177e4SLinus Torvalds 				 * dslot if not taken
13501da177e4SLinus Torvalds 				 */
1351102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
1352102cedc3SLeonid Yegoshin 				contpc += dec_insn.pc_inc;
13531da177e4SLinus Torvalds 				/*
13541da177e4SLinus Torvalds 				 * else continue & execute
13551da177e4SLinus Torvalds 				 * dslot as normal insn
13561da177e4SLinus Torvalds 				 */
13571da177e4SLinus Torvalds 			}
13581da177e4SLinus Torvalds 			break;
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 		default:
13611da177e4SLinus Torvalds 			if (!(MIPSInst_RS(ir) & 0x10))
13621da177e4SLinus Torvalds 				return SIGILL;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 			/* a real fpu computation instruction */
13658904d5b1SAleksandar Markovic 			sig = fpu_emu(xcp, ctx, ir);
13668904d5b1SAleksandar Markovic 			if (sig)
13671da177e4SLinus Torvalds 				return sig;
13681da177e4SLinus Torvalds 		}
13691da177e4SLinus Torvalds 		break;
13701da177e4SLinus Torvalds 
13713f7cac41SRalf Baechle 	case cop1x_op:
13722d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_4_5_64_r2_r6)
137308a07904SRalf Baechle 			return SIGILL;
137408a07904SRalf Baechle 
137508a07904SRalf Baechle 		sig = fpux_emu(xcp, ctx, ir, fault_addr);
1376515b029dSDavid Daney 		if (sig)
13771da177e4SLinus Torvalds 			return sig;
13781da177e4SLinus Torvalds 		break;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	case spec_op:
138108a07904SRalf Baechle 		if (!cpu_has_mips_4_5_r)
138208a07904SRalf Baechle 			return SIGILL;
138308a07904SRalf Baechle 
13841da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != movc_op)
13851da177e4SLinus Torvalds 			return SIGILL;
13861da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
13871da177e4SLinus Torvalds 		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
13881da177e4SLinus Torvalds 			xcp->regs[MIPSInst_RD(ir)] =
13891da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RS(ir)];
13901da177e4SLinus Torvalds 		break;
13911da177e4SLinus Torvalds 	default:
13921da177e4SLinus Torvalds 		return SIGILL;
13931da177e4SLinus Torvalds 	}
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 	/* we did it !! */
1396e70dfc10SAtsushi Nemoto 	xcp->cp0_epc = contpc;
1397e7e9cae5SRalf Baechle 	clear_delay_slot(xcp);
1398333d1f67SRalf Baechle 
13991da177e4SLinus Torvalds 	return 0;
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds /*
14031da177e4SLinus Torvalds  * Conversion table from MIPS compare ops 48-63
14041da177e4SLinus Torvalds  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
14051da177e4SLinus Torvalds  */
14061da177e4SLinus Torvalds static const unsigned char cmptab[8] = {
14071da177e4SLinus Torvalds 	0,			/* cmp_0 (sig) cmp_sf */
14081da177e4SLinus Torvalds 	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
14091da177e4SLinus Torvalds 	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
14101da177e4SLinus Torvalds 	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
14111da177e4SLinus Torvalds 	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
14121da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
14131da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
14141da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
14151da177e4SLinus Torvalds };
14161da177e4SLinus Torvalds 
1417f8c3c671SMarkos Chandras static const unsigned char negative_cmptab[8] = {
1418f8c3c671SMarkos Chandras 	0, /* Reserved */
1419f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
1420f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
1421f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT,
1422f8c3c671SMarkos Chandras 	/* Reserved */
1423f8c3c671SMarkos Chandras };
1424f8c3c671SMarkos Chandras 
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds /*
14271da177e4SLinus Torvalds  * Additional MIPS4 instructions
14281da177e4SLinus Torvalds  */
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3)					\
143147fa0c02SRalf Baechle static union ieee754##p fpemu_##p##_##name(union ieee754##p r,		\
143247fa0c02SRalf Baechle 	union ieee754##p s, union ieee754##p t)				\
14331da177e4SLinus Torvalds {									\
1434cd21dfcfSRalf Baechle 	struct _ieee754_csr ieee754_csr_save;				\
14351da177e4SLinus Torvalds 	s = f1(s, t);							\
14361da177e4SLinus Torvalds 	ieee754_csr_save = ieee754_csr;					\
14371da177e4SLinus Torvalds 	s = f2(s, r);							\
14381da177e4SLinus Torvalds 	ieee754_csr_save.cx |= ieee754_csr.cx;				\
14391da177e4SLinus Torvalds 	ieee754_csr_save.sx |= ieee754_csr.sx;				\
14401da177e4SLinus Torvalds 	s = f3(s);							\
14411da177e4SLinus Torvalds 	ieee754_csr.cx |= ieee754_csr_save.cx;				\
14421da177e4SLinus Torvalds 	ieee754_csr.sx |= ieee754_csr_save.sx;				\
14431da177e4SLinus Torvalds 	return s;							\
14441da177e4SLinus Torvalds }
14451da177e4SLinus Torvalds 
14462209bcb1SRalf Baechle static union ieee754dp fpemu_dp_recip(union ieee754dp d)
14471da177e4SLinus Torvalds {
14481da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), d);
14491da177e4SLinus Torvalds }
14501da177e4SLinus Torvalds 
14512209bcb1SRalf Baechle static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
14521da177e4SLinus Torvalds {
14531da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
14541da177e4SLinus Torvalds }
14551da177e4SLinus Torvalds 
14562209bcb1SRalf Baechle static union ieee754sp fpemu_sp_recip(union ieee754sp s)
14571da177e4SLinus Torvalds {
14581da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), s);
14591da177e4SLinus Torvalds }
14601da177e4SLinus Torvalds 
14612209bcb1SRalf Baechle static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
14621da177e4SLinus Torvalds {
14631da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
14641da177e4SLinus Torvalds }
14651da177e4SLinus Torvalds 
14661da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
14671da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
14681da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
14691da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
14701da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
14711da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
14721da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
14731da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
14741da177e4SLinus Torvalds 
1475eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1476445a58ceSPaul Burton 	mips_instruction ir, void __user **fault_addr)
14771da177e4SLinus Torvalds {
1478a58f85b5SAleksandar Markovic 	unsigned int rcsr = 0;	/* resulting csr */
14791da177e4SLinus Torvalds 
1480b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1xops);
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 	switch (MIPSInst_FMA_FFMT(ir)) {
14831da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
14841da177e4SLinus Torvalds 
14852209bcb1SRalf Baechle 		union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
14862209bcb1SRalf Baechle 		union ieee754sp fd, fr, fs, ft;
14873fccc015SRalf Baechle 		u32 __user *va;
14881da177e4SLinus Torvalds 		u32 val;
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
14911da177e4SLinus Torvalds 		case lwxc1_op:
14923fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14931da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14941da177e4SLinus Torvalds 
1495b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1496515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1497b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1498515b029dSDavid Daney 				*fault_addr = va;
14991da177e4SLinus Torvalds 				return SIGBUS;
15001da177e4SLinus Torvalds 			}
1501515b029dSDavid Daney 			if (__get_user(val, va)) {
1502515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1503515b029dSDavid Daney 				*fault_addr = va;
1504515b029dSDavid Daney 				return SIGSEGV;
1505515b029dSDavid Daney 			}
15061da177e4SLinus Torvalds 			SITOREG(val, MIPSInst_FD(ir));
15071da177e4SLinus Torvalds 			break;
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 		case swxc1_op:
15103fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15111da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15121da177e4SLinus Torvalds 
1513b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 			SIFROMREG(val, MIPSInst_FS(ir));
1516515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1517515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1518515b029dSDavid Daney 				*fault_addr = va;
1519515b029dSDavid Daney 				return SIGBUS;
1520515b029dSDavid Daney 			}
15211da177e4SLinus Torvalds 			if (put_user(val, va)) {
1522b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1523515b029dSDavid Daney 				*fault_addr = va;
1524515b029dSDavid Daney 				return SIGSEGV;
15251da177e4SLinus Torvalds 			}
15261da177e4SLinus Torvalds 			break;
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 		case madd_s_op:
15291da177e4SLinus Torvalds 			handler = fpemu_sp_madd;
15301da177e4SLinus Torvalds 			goto scoptop;
15311da177e4SLinus Torvalds 		case msub_s_op:
15321da177e4SLinus Torvalds 			handler = fpemu_sp_msub;
15331da177e4SLinus Torvalds 			goto scoptop;
15341da177e4SLinus Torvalds 		case nmadd_s_op:
15351da177e4SLinus Torvalds 			handler = fpemu_sp_nmadd;
15361da177e4SLinus Torvalds 			goto scoptop;
15371da177e4SLinus Torvalds 		case nmsub_s_op:
15381da177e4SLinus Torvalds 			handler = fpemu_sp_nmsub;
15391da177e4SLinus Torvalds 			goto scoptop;
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 		      scoptop:
15421da177e4SLinus Torvalds 			SPFROMREG(fr, MIPSInst_FR(ir));
15431da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
15441da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
15451da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
15461da177e4SLinus Torvalds 			SPTOREG(fd, MIPSInst_FD(ir));
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 		      copcsr:
1549c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1550c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
15511da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1552c4103526SDeng-Cheng Zhu 			}
1553c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1554c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
15551da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1556c4103526SDeng-Cheng Zhu 			}
1557c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1558c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
15591da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1560c4103526SDeng-Cheng Zhu 			}
1561c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1562c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
15631da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1564c4103526SDeng-Cheng Zhu 			}
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
15671da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
15683f7cac41SRalf Baechle 				/*printk ("SIGFPE: FPU csr = %08x\n",
15691da177e4SLinus Torvalds 				   ctx->fcr31); */
15701da177e4SLinus Torvalds 				return SIGFPE;
15711da177e4SLinus Torvalds 			}
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 			break;
15741da177e4SLinus Torvalds 
15751da177e4SLinus Torvalds 		default:
15761da177e4SLinus Torvalds 			return SIGILL;
15771da177e4SLinus Torvalds 		}
15781da177e4SLinus Torvalds 		break;
15791da177e4SLinus Torvalds 	}
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds 	case d_fmt:{		/* 1 */
15822209bcb1SRalf Baechle 		union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
15832209bcb1SRalf Baechle 		union ieee754dp fd, fr, fs, ft;
15843fccc015SRalf Baechle 		u64 __user *va;
15851da177e4SLinus Torvalds 		u64 val;
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
15881da177e4SLinus Torvalds 		case ldxc1_op:
15893fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15901da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15911da177e4SLinus Torvalds 
1592b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1593515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1594b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1595515b029dSDavid Daney 				*fault_addr = va;
15961da177e4SLinus Torvalds 				return SIGBUS;
15971da177e4SLinus Torvalds 			}
1598515b029dSDavid Daney 			if (__get_user(val, va)) {
1599515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1600515b029dSDavid Daney 				*fault_addr = va;
1601515b029dSDavid Daney 				return SIGSEGV;
1602515b029dSDavid Daney 			}
16031da177e4SLinus Torvalds 			DITOREG(val, MIPSInst_FD(ir));
16041da177e4SLinus Torvalds 			break;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 		case sdxc1_op:
16073fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
16081da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
16091da177e4SLinus Torvalds 
1610b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
16111da177e4SLinus Torvalds 			DIFROMREG(val, MIPSInst_FS(ir));
1612515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1613b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1614515b029dSDavid Daney 				*fault_addr = va;
16151da177e4SLinus Torvalds 				return SIGBUS;
16161da177e4SLinus Torvalds 			}
1617515b029dSDavid Daney 			if (__put_user(val, va)) {
1618515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1619515b029dSDavid Daney 				*fault_addr = va;
1620515b029dSDavid Daney 				return SIGSEGV;
1621515b029dSDavid Daney 			}
16221da177e4SLinus Torvalds 			break;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 		case madd_d_op:
16251da177e4SLinus Torvalds 			handler = fpemu_dp_madd;
16261da177e4SLinus Torvalds 			goto dcoptop;
16271da177e4SLinus Torvalds 		case msub_d_op:
16281da177e4SLinus Torvalds 			handler = fpemu_dp_msub;
16291da177e4SLinus Torvalds 			goto dcoptop;
16301da177e4SLinus Torvalds 		case nmadd_d_op:
16311da177e4SLinus Torvalds 			handler = fpemu_dp_nmadd;
16321da177e4SLinus Torvalds 			goto dcoptop;
16331da177e4SLinus Torvalds 		case nmsub_d_op:
16341da177e4SLinus Torvalds 			handler = fpemu_dp_nmsub;
16351da177e4SLinus Torvalds 			goto dcoptop;
16361da177e4SLinus Torvalds 
16371da177e4SLinus Torvalds 		      dcoptop:
16381da177e4SLinus Torvalds 			DPFROMREG(fr, MIPSInst_FR(ir));
16391da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
16401da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
16411da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
16421da177e4SLinus Torvalds 			DPTOREG(fd, MIPSInst_FD(ir));
16431da177e4SLinus Torvalds 			goto copcsr;
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 		default:
16461da177e4SLinus Torvalds 			return SIGILL;
16471da177e4SLinus Torvalds 		}
16481da177e4SLinus Torvalds 		break;
16491da177e4SLinus Torvalds 	}
16501da177e4SLinus Torvalds 
165151061b88SDeng-Cheng Zhu 	case 0x3:
165251061b88SDeng-Cheng Zhu 		if (MIPSInst_FUNC(ir) != pfetch_op)
16531da177e4SLinus Torvalds 			return SIGILL;
165451061b88SDeng-Cheng Zhu 
16551da177e4SLinus Torvalds 		/* ignore prefx operation */
16561da177e4SLinus Torvalds 		break;
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 	default:
16591da177e4SLinus Torvalds 		return SIGILL;
16601da177e4SLinus Torvalds 	}
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds 	return 0;
16631da177e4SLinus Torvalds }
16641da177e4SLinus Torvalds 
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds /*
16681da177e4SLinus Torvalds  * Emulate a single COP1 arithmetic instruction.
16691da177e4SLinus Torvalds  */
1670eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
16711da177e4SLinus Torvalds 	mips_instruction ir)
16721da177e4SLinus Torvalds {
16731da177e4SLinus Torvalds 	int rfmt;		/* resulting format */
1674a58f85b5SAleksandar Markovic 	unsigned int rcsr = 0;	/* resulting csr */
16753f7cac41SRalf Baechle 	unsigned int oldrm;
16763f7cac41SRalf Baechle 	unsigned int cbit;
1677a58f85b5SAleksandar Markovic 	unsigned int cond;
16781da177e4SLinus Torvalds 	union {
16792209bcb1SRalf Baechle 		union ieee754dp d;
16802209bcb1SRalf Baechle 		union ieee754sp s;
16811da177e4SLinus Torvalds 		int w;
16821da177e4SLinus Torvalds 		s64 l;
16831da177e4SLinus Torvalds 	} rv;			/* resulting value */
16843f7cac41SRalf Baechle 	u64 bits;
16851da177e4SLinus Torvalds 
1686b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1ops);
16871da177e4SLinus Torvalds 	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
16881da177e4SLinus Torvalds 	case s_fmt: {		/* 0 */
16891da177e4SLinus Torvalds 		union {
16902209bcb1SRalf Baechle 			union ieee754sp(*b) (union ieee754sp, union ieee754sp);
16912209bcb1SRalf Baechle 			union ieee754sp(*u) (union ieee754sp);
16921da177e4SLinus Torvalds 		} handler;
16934b820d95SPaul Burton 		union ieee754sp fd, fs, ft;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
16961da177e4SLinus Torvalds 			/* binary ops */
16971da177e4SLinus Torvalds 		case fadd_op:
1698454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(add_s);
16991da177e4SLinus Torvalds 			handler.b = ieee754sp_add;
17001da177e4SLinus Torvalds 			goto scopbop;
17011da177e4SLinus Torvalds 		case fsub_op:
1702454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sub_s);
17031da177e4SLinus Torvalds 			handler.b = ieee754sp_sub;
17041da177e4SLinus Torvalds 			goto scopbop;
17051da177e4SLinus Torvalds 		case fmul_op:
1706454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mul_s);
17071da177e4SLinus Torvalds 			handler.b = ieee754sp_mul;
17081da177e4SLinus Torvalds 			goto scopbop;
17091da177e4SLinus Torvalds 		case fdiv_op:
1710454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(div_s);
17111da177e4SLinus Torvalds 			handler.b = ieee754sp_div;
17121da177e4SLinus Torvalds 			goto scopbop;
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 			/* unary  ops */
17151da177e4SLinus Torvalds 		case fsqrt_op:
17162d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
171708a07904SRalf Baechle 				return SIGILL;
171808a07904SRalf Baechle 
1719454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sqrt_s);
17201da177e4SLinus Torvalds 			handler.u = ieee754sp_sqrt;
17211da177e4SLinus Torvalds 			goto scopuop;
17223f7cac41SRalf Baechle 
172308a07904SRalf Baechle 		/*
172408a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
172508a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
172608a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
172708a07904SRalf Baechle 		 */
17281da177e4SLinus Torvalds 		case frsqrt_op:
17292d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
173008a07904SRalf Baechle 				return SIGILL;
173108a07904SRalf Baechle 
1732454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rsqrt_s);
17331da177e4SLinus Torvalds 			handler.u = fpemu_sp_rsqrt;
17341da177e4SLinus Torvalds 			goto scopuop;
17353f7cac41SRalf Baechle 
17361da177e4SLinus Torvalds 		case frecip_op:
17372d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
173808a07904SRalf Baechle 				return SIGILL;
173908a07904SRalf Baechle 
1740454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(recip_s);
17411da177e4SLinus Torvalds 			handler.u = fpemu_sp_recip;
17421da177e4SLinus Torvalds 			goto scopuop;
174308a07904SRalf Baechle 
17441da177e4SLinus Torvalds 		case fmovc_op:
174508a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
174608a07904SRalf Baechle 				return SIGILL;
174708a07904SRalf Baechle 
17481da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
17491da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
17501da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
17511da177e4SLinus Torvalds 				return 0;
17521da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17531da177e4SLinus Torvalds 			break;
17543f7cac41SRalf Baechle 
17551da177e4SLinus Torvalds 		case fmovz_op:
175608a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
175708a07904SRalf Baechle 				return SIGILL;
175808a07904SRalf Baechle 
17591da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
17601da177e4SLinus Torvalds 				return 0;
17611da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17621da177e4SLinus Torvalds 			break;
17633f7cac41SRalf Baechle 
17641da177e4SLinus Torvalds 		case fmovn_op:
176508a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
176608a07904SRalf Baechle 				return SIGILL;
176708a07904SRalf Baechle 
17681da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
17691da177e4SLinus Torvalds 				return 0;
17701da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17711da177e4SLinus Torvalds 			break;
17723f7cac41SRalf Baechle 
177367613f02SMarkos Chandras 		case fseleqz_op:
177467613f02SMarkos Chandras 			if (!cpu_has_mips_r6)
177567613f02SMarkos Chandras 				return SIGILL;
177667613f02SMarkos Chandras 
1777454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(seleqz_s);
177867613f02SMarkos Chandras 			SPFROMREG(rv.s, MIPSInst_FT(ir));
177967613f02SMarkos Chandras 			if (rv.w & 0x1)
178067613f02SMarkos Chandras 				rv.w = 0;
178167613f02SMarkos Chandras 			else
178267613f02SMarkos Chandras 				SPFROMREG(rv.s, MIPSInst_FS(ir));
178367613f02SMarkos Chandras 			break;
178467613f02SMarkos Chandras 
1785130fe357SMarkos Chandras 		case fselnez_op:
1786130fe357SMarkos Chandras 			if (!cpu_has_mips_r6)
1787130fe357SMarkos Chandras 				return SIGILL;
1788130fe357SMarkos Chandras 
1789454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(selnez_s);
1790130fe357SMarkos Chandras 			SPFROMREG(rv.s, MIPSInst_FT(ir));
1791130fe357SMarkos Chandras 			if (rv.w & 0x1)
1792130fe357SMarkos Chandras 				SPFROMREG(rv.s, MIPSInst_FS(ir));
1793130fe357SMarkos Chandras 			else
1794130fe357SMarkos Chandras 				rv.w = 0;
1795130fe357SMarkos Chandras 			break;
1796130fe357SMarkos Chandras 
1797e24c3becSMarkos Chandras 		case fmaddf_op: {
1798e24c3becSMarkos Chandras 			union ieee754sp ft, fs, fd;
1799e24c3becSMarkos Chandras 
1800e24c3becSMarkos Chandras 			if (!cpu_has_mips_r6)
1801e24c3becSMarkos Chandras 				return SIGILL;
1802e24c3becSMarkos Chandras 
1803454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maddf_s);
1804e24c3becSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1805e24c3becSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1806e24c3becSMarkos Chandras 			SPFROMREG(fd, MIPSInst_FD(ir));
1807e24c3becSMarkos Chandras 			rv.s = ieee754sp_maddf(fd, fs, ft);
1808409fcaceSAleksandar Markovic 			goto copcsr;
1809e24c3becSMarkos Chandras 		}
1810e24c3becSMarkos Chandras 
181183d43305SMarkos Chandras 		case fmsubf_op: {
181283d43305SMarkos Chandras 			union ieee754sp ft, fs, fd;
181383d43305SMarkos Chandras 
181483d43305SMarkos Chandras 			if (!cpu_has_mips_r6)
181583d43305SMarkos Chandras 				return SIGILL;
181683d43305SMarkos Chandras 
1817454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(msubf_s);
181883d43305SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
181983d43305SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
182083d43305SMarkos Chandras 			SPFROMREG(fd, MIPSInst_FD(ir));
182183d43305SMarkos Chandras 			rv.s = ieee754sp_msubf(fd, fs, ft);
1822409fcaceSAleksandar Markovic 			goto copcsr;
182383d43305SMarkos Chandras 		}
182483d43305SMarkos Chandras 
1825400bd2e4SMarkos Chandras 		case frint_op: {
1826400bd2e4SMarkos Chandras 			union ieee754sp fs;
1827400bd2e4SMarkos Chandras 
1828400bd2e4SMarkos Chandras 			if (!cpu_has_mips_r6)
1829400bd2e4SMarkos Chandras 				return SIGILL;
1830400bd2e4SMarkos Chandras 
1831454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rint_s);
1832400bd2e4SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18333ec404d8SAleksandar Markovic 			rv.s = ieee754sp_rint(fs);
1834400bd2e4SMarkos Chandras 			goto copcsr;
1835400bd2e4SMarkos Chandras 		}
1836400bd2e4SMarkos Chandras 
183738db37baSMarkos Chandras 		case fclass_op: {
183838db37baSMarkos Chandras 			union ieee754sp fs;
183938db37baSMarkos Chandras 
184038db37baSMarkos Chandras 			if (!cpu_has_mips_r6)
184138db37baSMarkos Chandras 				return SIGILL;
184238db37baSMarkos Chandras 
1843454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(class_s);
184438db37baSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
184538db37baSMarkos Chandras 			rv.w = ieee754sp_2008class(fs);
184638db37baSMarkos Chandras 			rfmt = w_fmt;
1847409fcaceSAleksandar Markovic 			goto copcsr;
184838db37baSMarkos Chandras 		}
184938db37baSMarkos Chandras 
18504e9561b2SMarkos Chandras 		case fmin_op: {
18514e9561b2SMarkos Chandras 			union ieee754sp fs, ft;
18524e9561b2SMarkos Chandras 
18534e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
18544e9561b2SMarkos Chandras 				return SIGILL;
18554e9561b2SMarkos Chandras 
1856454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(min_s);
18574e9561b2SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
18584e9561b2SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18594e9561b2SMarkos Chandras 			rv.s = ieee754sp_fmin(fs, ft);
1860409fcaceSAleksandar Markovic 			goto copcsr;
18614e9561b2SMarkos Chandras 		}
18624e9561b2SMarkos Chandras 
18634e9561b2SMarkos Chandras 		case fmina_op: {
18644e9561b2SMarkos Chandras 			union ieee754sp fs, ft;
18654e9561b2SMarkos Chandras 
18664e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
18674e9561b2SMarkos Chandras 				return SIGILL;
18684e9561b2SMarkos Chandras 
1869454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mina_s);
18704e9561b2SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
18714e9561b2SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18724e9561b2SMarkos Chandras 			rv.s = ieee754sp_fmina(fs, ft);
1873409fcaceSAleksandar Markovic 			goto copcsr;
18744e9561b2SMarkos Chandras 		}
18754e9561b2SMarkos Chandras 
1876a79f5f9bSMarkos Chandras 		case fmax_op: {
1877a79f5f9bSMarkos Chandras 			union ieee754sp fs, ft;
1878a79f5f9bSMarkos Chandras 
1879a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
1880a79f5f9bSMarkos Chandras 				return SIGILL;
1881a79f5f9bSMarkos Chandras 
1882454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(max_s);
1883a79f5f9bSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1884a79f5f9bSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1885a79f5f9bSMarkos Chandras 			rv.s = ieee754sp_fmax(fs, ft);
1886409fcaceSAleksandar Markovic 			goto copcsr;
1887a79f5f9bSMarkos Chandras 		}
1888a79f5f9bSMarkos Chandras 
1889a79f5f9bSMarkos Chandras 		case fmaxa_op: {
1890a79f5f9bSMarkos Chandras 			union ieee754sp fs, ft;
1891a79f5f9bSMarkos Chandras 
1892a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
1893a79f5f9bSMarkos Chandras 				return SIGILL;
1894a79f5f9bSMarkos Chandras 
1895454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maxa_s);
1896a79f5f9bSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1897a79f5f9bSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1898a79f5f9bSMarkos Chandras 			rv.s = ieee754sp_fmaxa(fs, ft);
1899409fcaceSAleksandar Markovic 			goto copcsr;
1900a79f5f9bSMarkos Chandras 		}
1901a79f5f9bSMarkos Chandras 
19021da177e4SLinus Torvalds 		case fabs_op:
1903454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(abs_s);
19041da177e4SLinus Torvalds 			handler.u = ieee754sp_abs;
19051da177e4SLinus Torvalds 			goto scopuop;
19063f7cac41SRalf Baechle 
19071da177e4SLinus Torvalds 		case fneg_op:
1908454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(neg_s);
19091da177e4SLinus Torvalds 			handler.u = ieee754sp_neg;
19101da177e4SLinus Torvalds 			goto scopuop;
19113f7cac41SRalf Baechle 
19121da177e4SLinus Torvalds 		case fmov_op:
19131da177e4SLinus Torvalds 			/* an easy one */
1914454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mov_s);
19151da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
19161da177e4SLinus Torvalds 			goto copcsr;
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 			/* binary op on handler */
19191da177e4SLinus Torvalds scopbop:
19201da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19211da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 			rv.s = (*handler.b) (fs, ft);
19241da177e4SLinus Torvalds 			goto copcsr;
19251da177e4SLinus Torvalds scopuop:
19261da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19271da177e4SLinus Torvalds 			rv.s = (*handler.u) (fs);
19281da177e4SLinus Torvalds 			goto copcsr;
19291da177e4SLinus Torvalds copcsr:
1930c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1931c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
19321da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1933c4103526SDeng-Cheng Zhu 			}
1934c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1935c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
19361da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1937c4103526SDeng-Cheng Zhu 			}
1938c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1939c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
19401da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1941c4103526SDeng-Cheng Zhu 			}
1942c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1943c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
19441da177e4SLinus Torvalds 				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1945c4103526SDeng-Cheng Zhu 			}
1946c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1947c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
19481da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1949c4103526SDeng-Cheng Zhu 			}
19501da177e4SLinus Torvalds 			break;
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds 			/* unary conv ops */
19531da177e4SLinus Torvalds 		case fcvts_op:
19541da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
19551da177e4SLinus Torvalds 
19563f7cac41SRalf Baechle 		case fcvtd_op:
1957454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_d_s);
19581da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19591da177e4SLinus Torvalds 			rv.d = ieee754dp_fsp(fs);
19601da177e4SLinus Torvalds 			rfmt = d_fmt;
19611da177e4SLinus Torvalds 			goto copcsr;
19621da177e4SLinus Torvalds 
19633f7cac41SRalf Baechle 		case fcvtw_op:
1964454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_w_s);
19651da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19661da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
19671da177e4SLinus Torvalds 			rfmt = w_fmt;
19681da177e4SLinus Torvalds 			goto copcsr;
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 		case fround_op:
19711da177e4SLinus Torvalds 		case ftrunc_op:
19721da177e4SLinus Torvalds 		case fceil_op:
19733f7cac41SRalf Baechle 		case ffloor_op:
19742d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
197508a07904SRalf Baechle 				return SIGILL;
197608a07904SRalf Baechle 
1977454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceil_op)
1978454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_w_s);
1979454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloor_op)
1980454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_w_s);
1981454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fround_op)
1982454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_w_s);
1983454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftrunc_op)
1984454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_w_s);
1985454854acSAleksandar Markovic 
19863f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
19871da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19882cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
19891da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
19901da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
19911da177e4SLinus Torvalds 			rfmt = w_fmt;
19921da177e4SLinus Torvalds 			goto copcsr;
19931da177e4SLinus Torvalds 
19944b820d95SPaul Burton 		case fsel_op:
19954b820d95SPaul Burton 			if (!cpu_has_mips_r6)
19964b820d95SPaul Burton 				return SIGILL;
19974b820d95SPaul Burton 
1998454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sel_s);
19994b820d95SPaul Burton 			SPFROMREG(fd, MIPSInst_FD(ir));
20004b820d95SPaul Burton 			if (fd.bits & 0x1)
20014b820d95SPaul Burton 				SPFROMREG(rv.s, MIPSInst_FT(ir));
20024b820d95SPaul Burton 			else
20034b820d95SPaul Burton 				SPFROMREG(rv.s, MIPSInst_FS(ir));
20044b820d95SPaul Burton 			break;
20054b820d95SPaul Burton 
20063f7cac41SRalf Baechle 		case fcvtl_op:
20072d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
200808a07904SRalf Baechle 				return SIGILL;
200908a07904SRalf Baechle 
2010454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_l_s);
20111da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
20121da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
20131da177e4SLinus Torvalds 			rfmt = l_fmt;
20141da177e4SLinus Torvalds 			goto copcsr;
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 		case froundl_op:
20171da177e4SLinus Torvalds 		case ftruncl_op:
20181da177e4SLinus Torvalds 		case fceill_op:
20193f7cac41SRalf Baechle 		case ffloorl_op:
20202d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
202108a07904SRalf Baechle 				return SIGILL;
202208a07904SRalf Baechle 
2023454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceill_op)
2024454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_l_s);
2025454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloorl_op)
2026454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_l_s);
2027454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == froundl_op)
2028454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_l_s);
2029454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftruncl_op)
2030454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_l_s);
2031454854acSAleksandar Markovic 
20323f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
20331da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
20342cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
20351da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
20361da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
20371da177e4SLinus Torvalds 			rfmt = l_fmt;
20381da177e4SLinus Torvalds 			goto copcsr;
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 		default:
2041f8c3c671SMarkos Chandras 			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
2042a58f85b5SAleksandar Markovic 				unsigned int cmpop;
20432209bcb1SRalf Baechle 				union ieee754sp fs, ft;
20441da177e4SLinus Torvalds 
2045a58f85b5SAleksandar Markovic 				cmpop = MIPSInst_FUNC(ir) - fcmp_op;
20461da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
20471da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
20481da177e4SLinus Torvalds 				rv.w = ieee754sp_cmp(fs, ft,
20491da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
20501da177e4SLinus Torvalds 				rfmt = -1;
20511da177e4SLinus Torvalds 				if ((cmpop & 0x8) && ieee754_cxtest
20521da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
20531da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
20541da177e4SLinus Torvalds 				else
20551da177e4SLinus Torvalds 					goto copcsr;
20561da177e4SLinus Torvalds 
20573f7cac41SRalf Baechle 			} else
20581da177e4SLinus Torvalds 				return SIGILL;
20591da177e4SLinus Torvalds 			break;
20601da177e4SLinus Torvalds 		}
20611da177e4SLinus Torvalds 		break;
20621da177e4SLinus Torvalds 	}
20631da177e4SLinus Torvalds 
20641da177e4SLinus Torvalds 	case d_fmt: {
20654b820d95SPaul Burton 		union ieee754dp fd, fs, ft;
20661da177e4SLinus Torvalds 		union {
20672209bcb1SRalf Baechle 			union ieee754dp(*b) (union ieee754dp, union ieee754dp);
20682209bcb1SRalf Baechle 			union ieee754dp(*u) (union ieee754dp);
20691da177e4SLinus Torvalds 		} handler;
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
20721da177e4SLinus Torvalds 			/* binary ops */
20731da177e4SLinus Torvalds 		case fadd_op:
2074454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(add_d);
20751da177e4SLinus Torvalds 			handler.b = ieee754dp_add;
20761da177e4SLinus Torvalds 			goto dcopbop;
20771da177e4SLinus Torvalds 		case fsub_op:
2078454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sub_d);
20791da177e4SLinus Torvalds 			handler.b = ieee754dp_sub;
20801da177e4SLinus Torvalds 			goto dcopbop;
20811da177e4SLinus Torvalds 		case fmul_op:
2082454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mul_d);
20831da177e4SLinus Torvalds 			handler.b = ieee754dp_mul;
20841da177e4SLinus Torvalds 			goto dcopbop;
20851da177e4SLinus Torvalds 		case fdiv_op:
2086454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(div_d);
20871da177e4SLinus Torvalds 			handler.b = ieee754dp_div;
20881da177e4SLinus Torvalds 			goto dcopbop;
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds 			/* unary  ops */
20911da177e4SLinus Torvalds 		case fsqrt_op:
209208a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
209308a07904SRalf Baechle 				return SIGILL;
209408a07904SRalf Baechle 
2095454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sqrt_d);
20961da177e4SLinus Torvalds 			handler.u = ieee754dp_sqrt;
20971da177e4SLinus Torvalds 			goto dcopuop;
209808a07904SRalf Baechle 		/*
209908a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
210008a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
210108a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
210208a07904SRalf Baechle 		 */
21031da177e4SLinus Torvalds 		case frsqrt_op:
21042d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
210508a07904SRalf Baechle 				return SIGILL;
210608a07904SRalf Baechle 
2107454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rsqrt_d);
21081da177e4SLinus Torvalds 			handler.u = fpemu_dp_rsqrt;
21091da177e4SLinus Torvalds 			goto dcopuop;
21101da177e4SLinus Torvalds 		case frecip_op:
21112d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
211208a07904SRalf Baechle 				return SIGILL;
211308a07904SRalf Baechle 
2114454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(recip_d);
21151da177e4SLinus Torvalds 			handler.u = fpemu_dp_recip;
21161da177e4SLinus Torvalds 			goto dcopuop;
21171da177e4SLinus Torvalds 		case fmovc_op:
211808a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
211908a07904SRalf Baechle 				return SIGILL;
212008a07904SRalf Baechle 
21211da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
21221da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
21231da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
21241da177e4SLinus Torvalds 				return 0;
21251da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
21261da177e4SLinus Torvalds 			break;
21271da177e4SLinus Torvalds 		case fmovz_op:
212808a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
212908a07904SRalf Baechle 				return SIGILL;
213008a07904SRalf Baechle 
21311da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
21321da177e4SLinus Torvalds 				return 0;
21331da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
21341da177e4SLinus Torvalds 			break;
21351da177e4SLinus Torvalds 		case fmovn_op:
213608a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
213708a07904SRalf Baechle 				return SIGILL;
213808a07904SRalf Baechle 
21391da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
21401da177e4SLinus Torvalds 				return 0;
21411da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
21421da177e4SLinus Torvalds 			break;
214367613f02SMarkos Chandras 
214467613f02SMarkos Chandras 		case fseleqz_op:
214567613f02SMarkos Chandras 			if (!cpu_has_mips_r6)
214667613f02SMarkos Chandras 				return SIGILL;
214767613f02SMarkos Chandras 
2148454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(seleqz_d);
214967613f02SMarkos Chandras 			DPFROMREG(rv.d, MIPSInst_FT(ir));
215067613f02SMarkos Chandras 			if (rv.l & 0x1)
215167613f02SMarkos Chandras 				rv.l = 0;
215267613f02SMarkos Chandras 			else
215367613f02SMarkos Chandras 				DPFROMREG(rv.d, MIPSInst_FS(ir));
215467613f02SMarkos Chandras 			break;
215567613f02SMarkos Chandras 
2156130fe357SMarkos Chandras 		case fselnez_op:
2157130fe357SMarkos Chandras 			if (!cpu_has_mips_r6)
2158130fe357SMarkos Chandras 				return SIGILL;
2159130fe357SMarkos Chandras 
2160454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(selnez_d);
2161130fe357SMarkos Chandras 			DPFROMREG(rv.d, MIPSInst_FT(ir));
2162130fe357SMarkos Chandras 			if (rv.l & 0x1)
2163130fe357SMarkos Chandras 				DPFROMREG(rv.d, MIPSInst_FS(ir));
2164130fe357SMarkos Chandras 			else
2165130fe357SMarkos Chandras 				rv.l = 0;
2166130fe357SMarkos Chandras 			break;
2167130fe357SMarkos Chandras 
2168e24c3becSMarkos Chandras 		case fmaddf_op: {
2169e24c3becSMarkos Chandras 			union ieee754dp ft, fs, fd;
2170e24c3becSMarkos Chandras 
2171e24c3becSMarkos Chandras 			if (!cpu_has_mips_r6)
2172e24c3becSMarkos Chandras 				return SIGILL;
2173e24c3becSMarkos Chandras 
2174454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maddf_d);
2175e24c3becSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2176e24c3becSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2177e24c3becSMarkos Chandras 			DPFROMREG(fd, MIPSInst_FD(ir));
2178e24c3becSMarkos Chandras 			rv.d = ieee754dp_maddf(fd, fs, ft);
2179409fcaceSAleksandar Markovic 			goto copcsr;
2180e24c3becSMarkos Chandras 		}
2181e24c3becSMarkos Chandras 
218283d43305SMarkos Chandras 		case fmsubf_op: {
218383d43305SMarkos Chandras 			union ieee754dp ft, fs, fd;
218483d43305SMarkos Chandras 
218583d43305SMarkos Chandras 			if (!cpu_has_mips_r6)
218683d43305SMarkos Chandras 				return SIGILL;
218783d43305SMarkos Chandras 
2188454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(msubf_d);
218983d43305SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
219083d43305SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
219183d43305SMarkos Chandras 			DPFROMREG(fd, MIPSInst_FD(ir));
219283d43305SMarkos Chandras 			rv.d = ieee754dp_msubf(fd, fs, ft);
2193409fcaceSAleksandar Markovic 			goto copcsr;
219483d43305SMarkos Chandras 		}
219583d43305SMarkos Chandras 
2196400bd2e4SMarkos Chandras 		case frint_op: {
2197400bd2e4SMarkos Chandras 			union ieee754dp fs;
2198400bd2e4SMarkos Chandras 
2199400bd2e4SMarkos Chandras 			if (!cpu_has_mips_r6)
2200400bd2e4SMarkos Chandras 				return SIGILL;
2201400bd2e4SMarkos Chandras 
2202454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rint_d);
2203400bd2e4SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
22043ec404d8SAleksandar Markovic 			rv.d = ieee754dp_rint(fs);
2205400bd2e4SMarkos Chandras 			goto copcsr;
2206400bd2e4SMarkos Chandras 		}
2207400bd2e4SMarkos Chandras 
220838db37baSMarkos Chandras 		case fclass_op: {
220938db37baSMarkos Chandras 			union ieee754dp fs;
221038db37baSMarkos Chandras 
221138db37baSMarkos Chandras 			if (!cpu_has_mips_r6)
221238db37baSMarkos Chandras 				return SIGILL;
221338db37baSMarkos Chandras 
2214454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(class_d);
221538db37baSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2216e1231dd6SAleksandar Markovic 			rv.l = ieee754dp_2008class(fs);
2217e1231dd6SAleksandar Markovic 			rfmt = l_fmt;
2218409fcaceSAleksandar Markovic 			goto copcsr;
221938db37baSMarkos Chandras 		}
222038db37baSMarkos Chandras 
22214e9561b2SMarkos Chandras 		case fmin_op: {
22224e9561b2SMarkos Chandras 			union ieee754dp fs, ft;
22234e9561b2SMarkos Chandras 
22244e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
22254e9561b2SMarkos Chandras 				return SIGILL;
22264e9561b2SMarkos Chandras 
2227454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(min_d);
22284e9561b2SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
22294e9561b2SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
22304e9561b2SMarkos Chandras 			rv.d = ieee754dp_fmin(fs, ft);
2231409fcaceSAleksandar Markovic 			goto copcsr;
22324e9561b2SMarkos Chandras 		}
22334e9561b2SMarkos Chandras 
22344e9561b2SMarkos Chandras 		case fmina_op: {
22354e9561b2SMarkos Chandras 			union ieee754dp fs, ft;
22364e9561b2SMarkos Chandras 
22374e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
22384e9561b2SMarkos Chandras 				return SIGILL;
22394e9561b2SMarkos Chandras 
2240454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mina_d);
22414e9561b2SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
22424e9561b2SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
22434e9561b2SMarkos Chandras 			rv.d = ieee754dp_fmina(fs, ft);
2244409fcaceSAleksandar Markovic 			goto copcsr;
22454e9561b2SMarkos Chandras 		}
22464e9561b2SMarkos Chandras 
2247a79f5f9bSMarkos Chandras 		case fmax_op: {
2248a79f5f9bSMarkos Chandras 			union ieee754dp fs, ft;
2249a79f5f9bSMarkos Chandras 
2250a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
2251a79f5f9bSMarkos Chandras 				return SIGILL;
2252a79f5f9bSMarkos Chandras 
2253454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(max_d);
2254a79f5f9bSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2255a79f5f9bSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2256a79f5f9bSMarkos Chandras 			rv.d = ieee754dp_fmax(fs, ft);
2257409fcaceSAleksandar Markovic 			goto copcsr;
2258a79f5f9bSMarkos Chandras 		}
2259a79f5f9bSMarkos Chandras 
2260a79f5f9bSMarkos Chandras 		case fmaxa_op: {
2261a79f5f9bSMarkos Chandras 			union ieee754dp fs, ft;
2262a79f5f9bSMarkos Chandras 
2263a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
2264a79f5f9bSMarkos Chandras 				return SIGILL;
2265a79f5f9bSMarkos Chandras 
2266454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maxa_d);
2267a79f5f9bSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2268a79f5f9bSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2269a79f5f9bSMarkos Chandras 			rv.d = ieee754dp_fmaxa(fs, ft);
2270409fcaceSAleksandar Markovic 			goto copcsr;
2271a79f5f9bSMarkos Chandras 		}
2272a79f5f9bSMarkos Chandras 
22731da177e4SLinus Torvalds 		case fabs_op:
2274454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(abs_d);
22751da177e4SLinus Torvalds 			handler.u = ieee754dp_abs;
22761da177e4SLinus Torvalds 			goto dcopuop;
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds 		case fneg_op:
2279454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(neg_d);
22801da177e4SLinus Torvalds 			handler.u = ieee754dp_neg;
22811da177e4SLinus Torvalds 			goto dcopuop;
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds 		case fmov_op:
22841da177e4SLinus Torvalds 			/* an easy one */
2285454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mov_d);
22861da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
22871da177e4SLinus Torvalds 			goto copcsr;
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds 			/* binary op on handler */
22903f7cac41SRalf Baechle dcopbop:
22911da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22921da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds 			rv.d = (*handler.b) (fs, ft);
22951da177e4SLinus Torvalds 			goto copcsr;
22963f7cac41SRalf Baechle dcopuop:
22971da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22981da177e4SLinus Torvalds 			rv.d = (*handler.u) (fs);
22991da177e4SLinus Torvalds 			goto copcsr;
23001da177e4SLinus Torvalds 
23013f7cac41SRalf Baechle 		/*
23023f7cac41SRalf Baechle 		 * unary conv ops
23033f7cac41SRalf Baechle 		 */
23043f7cac41SRalf Baechle 		case fcvts_op:
2305454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_s_d);
23061da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23071da177e4SLinus Torvalds 			rv.s = ieee754sp_fdp(fs);
23081da177e4SLinus Torvalds 			rfmt = s_fmt;
23091da177e4SLinus Torvalds 			goto copcsr;
23103f7cac41SRalf Baechle 
23111da177e4SLinus Torvalds 		case fcvtd_op:
23121da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
23131da177e4SLinus Torvalds 
23143f7cac41SRalf Baechle 		case fcvtw_op:
2315454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_w_d);
23161da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23171da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);	/* wrong */
23181da177e4SLinus Torvalds 			rfmt = w_fmt;
23191da177e4SLinus Torvalds 			goto copcsr;
23201da177e4SLinus Torvalds 
23211da177e4SLinus Torvalds 		case fround_op:
23221da177e4SLinus Torvalds 		case ftrunc_op:
23231da177e4SLinus Torvalds 		case fceil_op:
23243f7cac41SRalf Baechle 		case ffloor_op:
232508a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
232608a07904SRalf Baechle 				return SIGILL;
232708a07904SRalf Baechle 
2328454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceil_op)
2329454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_w_d);
2330454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloor_op)
2331454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_w_d);
2332454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fround_op)
2333454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_w_d);
2334454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftrunc_op)
2335454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_w_d);
2336454854acSAleksandar Markovic 
23373f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
23381da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23392cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
23401da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);
23411da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
23421da177e4SLinus Torvalds 			rfmt = w_fmt;
23431da177e4SLinus Torvalds 			goto copcsr;
23441da177e4SLinus Torvalds 
23454b820d95SPaul Burton 		case fsel_op:
23464b820d95SPaul Burton 			if (!cpu_has_mips_r6)
23474b820d95SPaul Burton 				return SIGILL;
23484b820d95SPaul Burton 
2349454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sel_d);
23504b820d95SPaul Burton 			DPFROMREG(fd, MIPSInst_FD(ir));
23514b820d95SPaul Burton 			if (fd.bits & 0x1)
23524b820d95SPaul Burton 				DPFROMREG(rv.d, MIPSInst_FT(ir));
23534b820d95SPaul Burton 			else
23544b820d95SPaul Burton 				DPFROMREG(rv.d, MIPSInst_FS(ir));
23554b820d95SPaul Burton 			break;
23564b820d95SPaul Burton 
23573f7cac41SRalf Baechle 		case fcvtl_op:
23582d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
235908a07904SRalf Baechle 				return SIGILL;
236008a07904SRalf Baechle 
2361454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_l_d);
23621da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23631da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
23641da177e4SLinus Torvalds 			rfmt = l_fmt;
23651da177e4SLinus Torvalds 			goto copcsr;
23661da177e4SLinus Torvalds 
23671da177e4SLinus Torvalds 		case froundl_op:
23681da177e4SLinus Torvalds 		case ftruncl_op:
23691da177e4SLinus Torvalds 		case fceill_op:
23703f7cac41SRalf Baechle 		case ffloorl_op:
23712d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
237208a07904SRalf Baechle 				return SIGILL;
237308a07904SRalf Baechle 
2374454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceill_op)
2375454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_l_d);
2376454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloorl_op)
2377454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_l_d);
2378454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == froundl_op)
2379454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_l_d);
2380454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftruncl_op)
2381454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_l_d);
2382454854acSAleksandar Markovic 
23833f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
23841da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23852cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
23861da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
23871da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
23881da177e4SLinus Torvalds 			rfmt = l_fmt;
23891da177e4SLinus Torvalds 			goto copcsr;
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 		default:
2392f8c3c671SMarkos Chandras 			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
2393a58f85b5SAleksandar Markovic 				unsigned int cmpop;
23942209bcb1SRalf Baechle 				union ieee754dp fs, ft;
23951da177e4SLinus Torvalds 
2396a58f85b5SAleksandar Markovic 				cmpop = MIPSInst_FUNC(ir) - fcmp_op;
23971da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
23981da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
23991da177e4SLinus Torvalds 				rv.w = ieee754dp_cmp(fs, ft,
24001da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
24011da177e4SLinus Torvalds 				rfmt = -1;
24021da177e4SLinus Torvalds 				if ((cmpop & 0x8)
24031da177e4SLinus Torvalds 					&&
24041da177e4SLinus Torvalds 					ieee754_cxtest
24051da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
24061da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
24071da177e4SLinus Torvalds 				else
24081da177e4SLinus Torvalds 					goto copcsr;
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds 			}
24111da177e4SLinus Torvalds 			else {
24121da177e4SLinus Torvalds 				return SIGILL;
24131da177e4SLinus Torvalds 			}
24141da177e4SLinus Torvalds 			break;
24151da177e4SLinus Torvalds 		}
24161da177e4SLinus Torvalds 		break;
2417bbdd8147SMarkos Chandras 	}
24181da177e4SLinus Torvalds 
2419bbdd8147SMarkos Chandras 	case w_fmt: {
2420bbdd8147SMarkos Chandras 		union ieee754dp fs;
2421bbdd8147SMarkos Chandras 
24221da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
24231da177e4SLinus Torvalds 		case fcvts_op:
24241da177e4SLinus Torvalds 			/* convert word to single precision real */
2425454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_s_w);
24261da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
24271da177e4SLinus Torvalds 			rv.s = ieee754sp_fint(fs.bits);
24281da177e4SLinus Torvalds 			rfmt = s_fmt;
24291da177e4SLinus Torvalds 			goto copcsr;
24301da177e4SLinus Torvalds 		case fcvtd_op:
24311da177e4SLinus Torvalds 			/* convert word to double precision real */
2432454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_d_w);
24331da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
24341da177e4SLinus Torvalds 			rv.d = ieee754dp_fint(fs.bits);
24351da177e4SLinus Torvalds 			rfmt = d_fmt;
24361da177e4SLinus Torvalds 			goto copcsr;
2437f8c3c671SMarkos Chandras 		default: {
2438f8c3c671SMarkos Chandras 			/* Emulating the new CMP.condn.fmt R6 instruction */
2439f8c3c671SMarkos Chandras #define CMPOP_MASK	0x7
2440f8c3c671SMarkos Chandras #define SIGN_BIT	(0x1 << 3)
2441f8c3c671SMarkos Chandras #define PREDICATE_BIT	(0x1 << 4)
2442f8c3c671SMarkos Chandras 
2443f8c3c671SMarkos Chandras 			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2444f8c3c671SMarkos Chandras 			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2445f8c3c671SMarkos Chandras 			union ieee754sp fs, ft;
2446f8c3c671SMarkos Chandras 
2447f8c3c671SMarkos Chandras 			/* This is an R6 only instruction */
2448f8c3c671SMarkos Chandras 			if (!cpu_has_mips_r6 ||
2449f8c3c671SMarkos Chandras 			    (MIPSInst_FUNC(ir) & 0x20))
2450f8c3c671SMarkos Chandras 				return SIGILL;
2451f8c3c671SMarkos Chandras 
2452454854acSAleksandar Markovic 			if (!sig) {
2453454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2454454854acSAleksandar Markovic 					switch (cmpop) {
2455454854acSAleksandar Markovic 					case 0:
2456454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_af_s);
2457454854acSAleksandar Markovic 					break;
2458454854acSAleksandar Markovic 					case 1:
2459454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_un_s);
2460454854acSAleksandar Markovic 					break;
2461454854acSAleksandar Markovic 					case 2:
2462454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_eq_s);
2463454854acSAleksandar Markovic 					break;
2464454854acSAleksandar Markovic 					case 3:
2465454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ueq_s);
2466454854acSAleksandar Markovic 					break;
2467454854acSAleksandar Markovic 					case 4:
2468454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_lt_s);
2469454854acSAleksandar Markovic 					break;
2470454854acSAleksandar Markovic 					case 5:
2471454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ult_s);
2472454854acSAleksandar Markovic 					break;
2473454854acSAleksandar Markovic 					case 6:
2474454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_le_s);
2475454854acSAleksandar Markovic 					break;
2476454854acSAleksandar Markovic 					case 7:
2477454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ule_s);
2478454854acSAleksandar Markovic 					break;
2479454854acSAleksandar Markovic 					}
2480454854acSAleksandar Markovic 				} else {
2481454854acSAleksandar Markovic 					switch (cmpop) {
2482454854acSAleksandar Markovic 					case 1:
2483454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_or_s);
2484454854acSAleksandar Markovic 					break;
2485454854acSAleksandar Markovic 					case 2:
2486454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_une_s);
2487454854acSAleksandar Markovic 					break;
2488454854acSAleksandar Markovic 					case 3:
2489454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ne_s);
2490454854acSAleksandar Markovic 					break;
2491454854acSAleksandar Markovic 					}
2492454854acSAleksandar Markovic 				}
2493454854acSAleksandar Markovic 			} else {
2494454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2495454854acSAleksandar Markovic 					switch (cmpop) {
2496454854acSAleksandar Markovic 					case 0:
2497454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_saf_s);
2498454854acSAleksandar Markovic 					break;
2499454854acSAleksandar Markovic 					case 1:
2500454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sun_s);
2501454854acSAleksandar Markovic 					break;
2502454854acSAleksandar Markovic 					case 2:
2503454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_seq_s);
2504454854acSAleksandar Markovic 					break;
2505454854acSAleksandar Markovic 					case 3:
2506454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sueq_s);
2507454854acSAleksandar Markovic 					break;
2508454854acSAleksandar Markovic 					case 4:
2509454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_slt_s);
2510454854acSAleksandar Markovic 					break;
2511454854acSAleksandar Markovic 					case 5:
2512454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sult_s);
2513454854acSAleksandar Markovic 					break;
2514454854acSAleksandar Markovic 					case 6:
2515454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sle_s);
2516454854acSAleksandar Markovic 					break;
2517454854acSAleksandar Markovic 					case 7:
2518454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sule_s);
2519454854acSAleksandar Markovic 					break;
2520454854acSAleksandar Markovic 					}
2521454854acSAleksandar Markovic 				} else {
2522454854acSAleksandar Markovic 					switch (cmpop) {
2523454854acSAleksandar Markovic 					case 1:
2524454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sor_s);
2525454854acSAleksandar Markovic 					break;
2526454854acSAleksandar Markovic 					case 2:
2527454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sune_s);
2528454854acSAleksandar Markovic 					break;
2529454854acSAleksandar Markovic 					case 3:
2530454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sne_s);
2531454854acSAleksandar Markovic 					break;
2532454854acSAleksandar Markovic 					}
2533454854acSAleksandar Markovic 				}
2534454854acSAleksandar Markovic 			}
2535454854acSAleksandar Markovic 
2536f8c3c671SMarkos Chandras 			/* fmt is w_fmt for single precision so fix it */
2537f8c3c671SMarkos Chandras 			rfmt = s_fmt;
2538f8c3c671SMarkos Chandras 			/* default to false */
2539f8c3c671SMarkos Chandras 			rv.w = 0;
2540f8c3c671SMarkos Chandras 
2541f8c3c671SMarkos Chandras 			/* CMP.condn.S */
2542f8c3c671SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
2543f8c3c671SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
2544f8c3c671SMarkos Chandras 
2545f8c3c671SMarkos Chandras 			/* positive predicates */
2546f8c3c671SMarkos Chandras 			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2547f8c3c671SMarkos Chandras 				if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
2548f8c3c671SMarkos Chandras 						  sig))
2549f8c3c671SMarkos Chandras 				    rv.w = -1; /* true, all 1s */
2550f8c3c671SMarkos Chandras 				if ((sig) &&
2551f8c3c671SMarkos Chandras 				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2552f8c3c671SMarkos Chandras 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2553f8c3c671SMarkos Chandras 				else
2554f8c3c671SMarkos Chandras 					goto copcsr;
2555f8c3c671SMarkos Chandras 			} else {
2556f8c3c671SMarkos Chandras 				/* negative predicates */
2557f8c3c671SMarkos Chandras 				switch (cmpop) {
2558f8c3c671SMarkos Chandras 				case 1:
2559f8c3c671SMarkos Chandras 				case 2:
2560f8c3c671SMarkos Chandras 				case 3:
2561f8c3c671SMarkos Chandras 					if (ieee754sp_cmp(fs, ft,
2562f8c3c671SMarkos Chandras 							  negative_cmptab[cmpop],
2563f8c3c671SMarkos Chandras 							  sig))
2564f8c3c671SMarkos Chandras 						rv.w = -1; /* true, all 1s */
2565f8c3c671SMarkos Chandras 					if (sig &&
2566f8c3c671SMarkos Chandras 					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2567f8c3c671SMarkos Chandras 						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2568f8c3c671SMarkos Chandras 					else
2569f8c3c671SMarkos Chandras 						goto copcsr;
2570f8c3c671SMarkos Chandras 					break;
25711da177e4SLinus Torvalds 				default:
2572f8c3c671SMarkos Chandras 					/* Reserved R6 ops */
25731da177e4SLinus Torvalds 					return SIGILL;
25741da177e4SLinus Torvalds 				}
2575f8c3c671SMarkos Chandras 			}
25761da177e4SLinus Torvalds 			break;
25771da177e4SLinus Torvalds 			}
2578f8c3c671SMarkos Chandras 		}
25791ff8560aSAleksandar Markovic 		break;
2580f8c3c671SMarkos Chandras 	}
25811da177e4SLinus Torvalds 
25823f7cac41SRalf Baechle 	case l_fmt:
258308a07904SRalf Baechle 
25842d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
258508a07904SRalf Baechle 			return SIGILL;
258608a07904SRalf Baechle 
2587bbd426f5SPaul Burton 		DIFROMREG(bits, MIPSInst_FS(ir));
2588bbd426f5SPaul Burton 
25891da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
25901da177e4SLinus Torvalds 		case fcvts_op:
25911da177e4SLinus Torvalds 			/* convert long to single precision real */
2592454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_s_l);
2593bbd426f5SPaul Burton 			rv.s = ieee754sp_flong(bits);
25941da177e4SLinus Torvalds 			rfmt = s_fmt;
25951da177e4SLinus Torvalds 			goto copcsr;
25961da177e4SLinus Torvalds 		case fcvtd_op:
25971da177e4SLinus Torvalds 			/* convert long to double precision real */
2598454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_d_l);
2599bbd426f5SPaul Burton 			rv.d = ieee754dp_flong(bits);
26001da177e4SLinus Torvalds 			rfmt = d_fmt;
26011da177e4SLinus Torvalds 			goto copcsr;
2602f8c3c671SMarkos Chandras 		default: {
2603f8c3c671SMarkos Chandras 			/* Emulating the new CMP.condn.fmt R6 instruction */
2604f8c3c671SMarkos Chandras 			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2605f8c3c671SMarkos Chandras 			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2606f8c3c671SMarkos Chandras 			union ieee754dp fs, ft;
2607f8c3c671SMarkos Chandras 
2608f8c3c671SMarkos Chandras 			if (!cpu_has_mips_r6 ||
2609f8c3c671SMarkos Chandras 			    (MIPSInst_FUNC(ir) & 0x20))
2610f8c3c671SMarkos Chandras 				return SIGILL;
2611f8c3c671SMarkos Chandras 
2612454854acSAleksandar Markovic 			if (!sig) {
2613454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2614454854acSAleksandar Markovic 					switch (cmpop) {
2615454854acSAleksandar Markovic 					case 0:
2616454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_af_d);
2617454854acSAleksandar Markovic 					break;
2618454854acSAleksandar Markovic 					case 1:
2619454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_un_d);
2620454854acSAleksandar Markovic 					break;
2621454854acSAleksandar Markovic 					case 2:
2622454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_eq_d);
2623454854acSAleksandar Markovic 					break;
2624454854acSAleksandar Markovic 					case 3:
2625454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ueq_d);
2626454854acSAleksandar Markovic 					break;
2627454854acSAleksandar Markovic 					case 4:
2628454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_lt_d);
2629454854acSAleksandar Markovic 					break;
2630454854acSAleksandar Markovic 					case 5:
2631454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ult_d);
2632454854acSAleksandar Markovic 					break;
2633454854acSAleksandar Markovic 					case 6:
2634454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_le_d);
2635454854acSAleksandar Markovic 					break;
2636454854acSAleksandar Markovic 					case 7:
2637454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ule_d);
2638454854acSAleksandar Markovic 					break;
2639454854acSAleksandar Markovic 					}
2640454854acSAleksandar Markovic 				} else {
2641454854acSAleksandar Markovic 					switch (cmpop) {
2642454854acSAleksandar Markovic 					case 1:
2643454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_or_d);
2644454854acSAleksandar Markovic 					break;
2645454854acSAleksandar Markovic 					case 2:
2646454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_une_d);
2647454854acSAleksandar Markovic 					break;
2648454854acSAleksandar Markovic 					case 3:
2649454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ne_d);
2650454854acSAleksandar Markovic 					break;
2651454854acSAleksandar Markovic 					}
2652454854acSAleksandar Markovic 				}
2653454854acSAleksandar Markovic 			} else {
2654454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2655454854acSAleksandar Markovic 					switch (cmpop) {
2656454854acSAleksandar Markovic 					case 0:
2657454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_saf_d);
2658454854acSAleksandar Markovic 					break;
2659454854acSAleksandar Markovic 					case 1:
2660454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sun_d);
2661454854acSAleksandar Markovic 					break;
2662454854acSAleksandar Markovic 					case 2:
2663454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_seq_d);
2664454854acSAleksandar Markovic 					break;
2665454854acSAleksandar Markovic 					case 3:
2666454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sueq_d);
2667454854acSAleksandar Markovic 					break;
2668454854acSAleksandar Markovic 					case 4:
2669454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_slt_d);
2670454854acSAleksandar Markovic 					break;
2671454854acSAleksandar Markovic 					case 5:
2672454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sult_d);
2673454854acSAleksandar Markovic 					break;
2674454854acSAleksandar Markovic 					case 6:
2675454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sle_d);
2676454854acSAleksandar Markovic 					break;
2677454854acSAleksandar Markovic 					case 7:
2678454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sule_d);
2679454854acSAleksandar Markovic 					break;
2680454854acSAleksandar Markovic 					}
2681454854acSAleksandar Markovic 				} else {
2682454854acSAleksandar Markovic 					switch (cmpop) {
2683454854acSAleksandar Markovic 					case 1:
2684454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sor_d);
2685454854acSAleksandar Markovic 					break;
2686454854acSAleksandar Markovic 					case 2:
2687454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sune_d);
2688454854acSAleksandar Markovic 					break;
2689454854acSAleksandar Markovic 					case 3:
2690454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sne_d);
2691454854acSAleksandar Markovic 					break;
2692454854acSAleksandar Markovic 					}
2693454854acSAleksandar Markovic 				}
2694454854acSAleksandar Markovic 			}
2695454854acSAleksandar Markovic 
2696f8c3c671SMarkos Chandras 			/* fmt is l_fmt for double precision so fix it */
2697f8c3c671SMarkos Chandras 			rfmt = d_fmt;
2698f8c3c671SMarkos Chandras 			/* default to false */
2699f8c3c671SMarkos Chandras 			rv.l = 0;
2700f8c3c671SMarkos Chandras 
2701f8c3c671SMarkos Chandras 			/* CMP.condn.D */
2702f8c3c671SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2703f8c3c671SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2704f8c3c671SMarkos Chandras 
2705f8c3c671SMarkos Chandras 			/* positive predicates */
2706f8c3c671SMarkos Chandras 			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2707f8c3c671SMarkos Chandras 				if (ieee754dp_cmp(fs, ft,
2708f8c3c671SMarkos Chandras 						  cmptab[cmpop], sig))
2709f8c3c671SMarkos Chandras 				    rv.l = -1LL; /* true, all 1s */
2710f8c3c671SMarkos Chandras 				if (sig &&
2711f8c3c671SMarkos Chandras 				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2712f8c3c671SMarkos Chandras 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2713f8c3c671SMarkos Chandras 				else
2714f8c3c671SMarkos Chandras 					goto copcsr;
2715f8c3c671SMarkos Chandras 			} else {
2716f8c3c671SMarkos Chandras 				/* negative predicates */
2717f8c3c671SMarkos Chandras 				switch (cmpop) {
2718f8c3c671SMarkos Chandras 				case 1:
2719f8c3c671SMarkos Chandras 				case 2:
2720f8c3c671SMarkos Chandras 				case 3:
2721f8c3c671SMarkos Chandras 					if (ieee754dp_cmp(fs, ft,
2722f8c3c671SMarkos Chandras 							  negative_cmptab[cmpop],
2723f8c3c671SMarkos Chandras 							  sig))
2724f8c3c671SMarkos Chandras 						rv.l = -1LL; /* true, all 1s */
2725f8c3c671SMarkos Chandras 					if (sig &&
2726f8c3c671SMarkos Chandras 					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2727f8c3c671SMarkos Chandras 						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2728f8c3c671SMarkos Chandras 					else
2729f8c3c671SMarkos Chandras 						goto copcsr;
2730f8c3c671SMarkos Chandras 					break;
27311da177e4SLinus Torvalds 				default:
2732f8c3c671SMarkos Chandras 					/* Reserved R6 ops */
27331da177e4SLinus Torvalds 					return SIGILL;
27341da177e4SLinus Torvalds 				}
2735f8c3c671SMarkos Chandras 			}
27361da177e4SLinus Torvalds 			break;
2737f8c3c671SMarkos Chandras 			}
2738f8c3c671SMarkos Chandras 		}
27391ff8560aSAleksandar Markovic 		break;
27401ff8560aSAleksandar Markovic 
27411da177e4SLinus Torvalds 	default:
27421da177e4SLinus Torvalds 		return SIGILL;
27431da177e4SLinus Torvalds 	}
27441da177e4SLinus Torvalds 
27451da177e4SLinus Torvalds 	/*
27461da177e4SLinus Torvalds 	 * Update the fpu CSR register for this operation.
27471da177e4SLinus Torvalds 	 * If an exception is required, generate a tidy SIGFPE exception,
27481da177e4SLinus Torvalds 	 * without updating the result register.
27491da177e4SLinus Torvalds 	 * Note: cause exception bits do not accumulate, they are rewritten
27501da177e4SLinus Torvalds 	 * for each op; only the flag/sticky bits accumulate.
27511da177e4SLinus Torvalds 	 */
27521da177e4SLinus Torvalds 	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
27531da177e4SLinus Torvalds 	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
27543f7cac41SRalf Baechle 		/*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
27551da177e4SLinus Torvalds 		return SIGFPE;
27561da177e4SLinus Torvalds 	}
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds 	/*
27591da177e4SLinus Torvalds 	 * Now we can safely write the result back to the register file.
27601da177e4SLinus Torvalds 	 */
27611da177e4SLinus Torvalds 	switch (rfmt) {
276208a07904SRalf Baechle 	case -1:
276308a07904SRalf Baechle 
276408a07904SRalf Baechle 		if (cpu_has_mips_4_5_r)
2765c3b9b945SRob Kendrick 			cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
27661da177e4SLinus Torvalds 		else
276708a07904SRalf Baechle 			cbit = FPU_CSR_COND;
276808a07904SRalf Baechle 		if (rv.w)
276908a07904SRalf Baechle 			ctx->fcr31 |= cbit;
277008a07904SRalf Baechle 		else
277108a07904SRalf Baechle 			ctx->fcr31 &= ~cbit;
27721da177e4SLinus Torvalds 		break;
277308a07904SRalf Baechle 
27741da177e4SLinus Torvalds 	case d_fmt:
27751da177e4SLinus Torvalds 		DPTOREG(rv.d, MIPSInst_FD(ir));
27761da177e4SLinus Torvalds 		break;
27771da177e4SLinus Torvalds 	case s_fmt:
27781da177e4SLinus Torvalds 		SPTOREG(rv.s, MIPSInst_FD(ir));
27791da177e4SLinus Torvalds 		break;
27801da177e4SLinus Torvalds 	case w_fmt:
27811da177e4SLinus Torvalds 		SITOREG(rv.w, MIPSInst_FD(ir));
27821da177e4SLinus Torvalds 		break;
27831da177e4SLinus Torvalds 	case l_fmt:
27842d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
278508a07904SRalf Baechle 			return SIGILL;
278608a07904SRalf Baechle 
27871da177e4SLinus Torvalds 		DITOREG(rv.l, MIPSInst_FD(ir));
27881da177e4SLinus Torvalds 		break;
27891da177e4SLinus Torvalds 	default:
27901da177e4SLinus Torvalds 		return SIGILL;
27911da177e4SLinus Torvalds 	}
27921da177e4SLinus Torvalds 
27931da177e4SLinus Torvalds 	return 0;
27941da177e4SLinus Torvalds }
27951da177e4SLinus Torvalds 
279613769ebaSMaciej W. Rozycki /*
279713769ebaSMaciej W. Rozycki  * Emulate FPU instructions.
279813769ebaSMaciej W. Rozycki  *
279913769ebaSMaciej W. Rozycki  * If we use FPU hardware, then we have been typically called to handle
280013769ebaSMaciej W. Rozycki  * an unimplemented operation, such as where an operand is a NaN or
280113769ebaSMaciej W. Rozycki  * denormalized.  In that case exit the emulation loop after a single
280213769ebaSMaciej W. Rozycki  * iteration so as to let hardware execute any subsequent instructions.
280313769ebaSMaciej W. Rozycki  *
280413769ebaSMaciej W. Rozycki  * If we have no FPU hardware or it has been disabled, then continue
280513769ebaSMaciej W. Rozycki  * emulating floating-point instructions until one of these conditions
280613769ebaSMaciej W. Rozycki  * has occurred:
280713769ebaSMaciej W. Rozycki  *
280813769ebaSMaciej W. Rozycki  * - a non-FPU instruction has been encountered,
280913769ebaSMaciej W. Rozycki  *
281013769ebaSMaciej W. Rozycki  * - an attempt to emulate has ended with a signal,
281113769ebaSMaciej W. Rozycki  *
281213769ebaSMaciej W. Rozycki  * - the ISA mode has been switched.
281313769ebaSMaciej W. Rozycki  *
281413769ebaSMaciej W. Rozycki  * We need to terminate the emulation loop if we got switched to the
281513769ebaSMaciej W. Rozycki  * MIPS16 mode, whether supported or not, so that we do not attempt
281613769ebaSMaciej W. Rozycki  * to emulate a MIPS16 instruction as a regular MIPS FPU instruction.
281713769ebaSMaciej W. Rozycki  * Similarly if we got switched to the microMIPS mode and only the
281813769ebaSMaciej W. Rozycki  * regular MIPS mode is supported, so that we do not attempt to emulate
281913769ebaSMaciej W. Rozycki  * a microMIPS instruction as a regular MIPS FPU instruction.  Or if
282013769ebaSMaciej W. Rozycki  * we got switched to the regular MIPS mode and only the microMIPS mode
282113769ebaSMaciej W. Rozycki  * is supported, so that we do not attempt to emulate a regular MIPS
282213769ebaSMaciej W. Rozycki  * instruction that should cause an Address Error exception instead.
282313769ebaSMaciej W. Rozycki  * For simplicity we always terminate upon an ISA mode switch.
282413769ebaSMaciej W. Rozycki  */
2825e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2826445a58ceSPaul Burton 	int has_fpu, void __user **fault_addr)
28271da177e4SLinus Torvalds {
2828333d1f67SRalf Baechle 	unsigned long oldepc, prevepc;
2829102cedc3SLeonid Yegoshin 	struct mm_decoded_insn dec_insn;
2830102cedc3SLeonid Yegoshin 	u16 instr[4];
2831102cedc3SLeonid Yegoshin 	u16 *instr_ptr;
28321da177e4SLinus Torvalds 	int sig = 0;
28331da177e4SLinus Torvalds 
2834*1975ed43SPaul Burton 	/*
2835*1975ed43SPaul Burton 	 * Initialize context if it hasn't been used already, otherwise ensure
2836*1975ed43SPaul Burton 	 * it has been saved to struct thread_struct.
2837*1975ed43SPaul Burton 	 */
2838*1975ed43SPaul Burton 	if (!init_fp_ctx(current))
2839*1975ed43SPaul Burton 		lose_fpu(1);
2840*1975ed43SPaul Burton 
28411da177e4SLinus Torvalds 	oldepc = xcp->cp0_epc;
28421da177e4SLinus Torvalds 	do {
28431da177e4SLinus Torvalds 		prevepc = xcp->cp0_epc;
28441da177e4SLinus Torvalds 
2845102cedc3SLeonid Yegoshin 		if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2846102cedc3SLeonid Yegoshin 			/*
2847102cedc3SLeonid Yegoshin 			 * Get next 2 microMIPS instructions and convert them
2848102cedc3SLeonid Yegoshin 			 * into 32-bit instructions.
2849102cedc3SLeonid Yegoshin 			 */
2850102cedc3SLeonid Yegoshin 			if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2851102cedc3SLeonid Yegoshin 			    (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2852102cedc3SLeonid Yegoshin 			    (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2853102cedc3SLeonid Yegoshin 			    (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2854b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
28551da177e4SLinus Torvalds 				return SIGBUS;
28561da177e4SLinus Torvalds 			}
2857102cedc3SLeonid Yegoshin 			instr_ptr = instr;
2858102cedc3SLeonid Yegoshin 
2859102cedc3SLeonid Yegoshin 			/* Get first instruction. */
2860102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2861102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2862102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2863102cedc3SLeonid Yegoshin 					(*instr_ptr);
2864102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2865102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 2;
2866102cedc3SLeonid Yegoshin 				instr_ptr += 1;
2867102cedc3SLeonid Yegoshin 			} else {
2868102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2869102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2870102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2871102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 4;
2872102cedc3SLeonid Yegoshin 				instr_ptr += 2;
2873515b029dSDavid Daney 			}
2874102cedc3SLeonid Yegoshin 			/* Get second instruction. */
2875102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2876102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2877102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2878102cedc3SLeonid Yegoshin 					(*instr_ptr);
2879102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2880102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 2;
2881102cedc3SLeonid Yegoshin 			} else {
2882102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2883102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2884102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2885102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 4;
2886102cedc3SLeonid Yegoshin 			}
2887102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 1;
2888102cedc3SLeonid Yegoshin 		} else {
2889102cedc3SLeonid Yegoshin 			if ((get_user(dec_insn.insn,
2890102cedc3SLeonid Yegoshin 			    (mips_instruction __user *) xcp->cp0_epc)) ||
2891102cedc3SLeonid Yegoshin 			    (get_user(dec_insn.next_insn,
2892102cedc3SLeonid Yegoshin 			    (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2893102cedc3SLeonid Yegoshin 				MIPS_FPU_EMU_INC_STATS(errors);
2894102cedc3SLeonid Yegoshin 				return SIGBUS;
2895102cedc3SLeonid Yegoshin 			}
2896102cedc3SLeonid Yegoshin 			dec_insn.pc_inc = 4;
2897102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc = 4;
2898102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 0;
2899102cedc3SLeonid Yegoshin 		}
2900102cedc3SLeonid Yegoshin 
2901102cedc3SLeonid Yegoshin 		if ((dec_insn.insn == 0) ||
2902102cedc3SLeonid Yegoshin 		   ((dec_insn.pc_inc == 2) &&
2903102cedc3SLeonid Yegoshin 		   ((dec_insn.insn & 0xffff) == MM_NOP16)))
2904102cedc3SLeonid Yegoshin 			xcp->cp0_epc += dec_insn.pc_inc;	/* Skip NOPs */
29051da177e4SLinus Torvalds 		else {
2906cd21dfcfSRalf Baechle 			/*
29072cfcf8a8SMaciej W. Rozycki 			 * The 'ieee754_csr' is an alias of ctx->fcr31.
29082cfcf8a8SMaciej W. Rozycki 			 * No need to copy ctx->fcr31 to ieee754_csr.
2909cd21dfcfSRalf Baechle 			 */
2910102cedc3SLeonid Yegoshin 			sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
29111da177e4SLinus Torvalds 		}
29121da177e4SLinus Torvalds 
2913e04582b7SAtsushi Nemoto 		if (has_fpu)
29141da177e4SLinus Torvalds 			break;
29151da177e4SLinus Torvalds 		if (sig)
29161da177e4SLinus Torvalds 			break;
291713769ebaSMaciej W. Rozycki 		/*
291813769ebaSMaciej W. Rozycki 		 * We have to check for the ISA bit explicitly here,
291913769ebaSMaciej W. Rozycki 		 * because `get_isa16_mode' may return 0 if support
292013769ebaSMaciej W. Rozycki 		 * for code compression has been globally disabled,
292113769ebaSMaciej W. Rozycki 		 * or otherwise we may produce the wrong signal or
292213769ebaSMaciej W. Rozycki 		 * even proceed successfully where we must not.
292313769ebaSMaciej W. Rozycki 		 */
292413769ebaSMaciej W. Rozycki 		if ((xcp->cp0_epc ^ prevepc) & 0x1)
292513769ebaSMaciej W. Rozycki 			break;
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds 		cond_resched();
29281da177e4SLinus Torvalds 	} while (xcp->cp0_epc > prevepc);
29291da177e4SLinus Torvalds 
29301da177e4SLinus Torvalds 	/* SIGILL indicates a non-fpu instruction */
29311da177e4SLinus Torvalds 	if (sig == SIGILL && xcp->cp0_epc != oldepc)
29323f7cac41SRalf Baechle 		/* but if EPC has advanced, then ignore it */
29331da177e4SLinus Torvalds 		sig = 0;
29341da177e4SLinus Torvalds 
29351da177e4SLinus Torvalds 	return sig;
29361da177e4SLinus Torvalds }
2937