xref: /linux/arch/mips/math-emu/cp1emu.c (revision c8a34581ec09a5ee11dd833d6c5cf41fdbef706f)
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>
3808a07904SRalf Baechle #include <linux/kconfig.h>
3985c51c51SRalf Baechle #include <linux/percpu-defs.h>
407f788d2dSDeng-Cheng Zhu #include <linux/perf_event.h>
411da177e4SLinus Torvalds 
42cd8ee345SRalf Baechle #include <asm/branch.h>
431da177e4SLinus Torvalds #include <asm/inst.h>
441da177e4SLinus Torvalds #include <asm/ptrace.h>
451da177e4SLinus Torvalds #include <asm/signal.h>
46cd8ee345SRalf Baechle #include <asm/uaccess.h>
47cd8ee345SRalf Baechle 
48cd8ee345SRalf Baechle #include <asm/processor.h>
491da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
50102cedc3SLeonid Yegoshin #include <asm/fpu.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include "ieee754.h"
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */
551da177e4SLinus Torvalds 
56eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
571da177e4SLinus Torvalds 	mips_instruction);
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *,
60515b029dSDavid Daney 	struct mips_fpu_struct *, mips_instruction, void *__user *);
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /* Control registers */
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #define FPCREG_RID	0	/* $0  = revision id */
651da177e4SLinus Torvalds #define FPCREG_CSR	31	/* $31 = csr */
661da177e4SLinus Torvalds 
6795e8f634SShane McDonald /* Determine rounding mode from the RM bits of the FCSR */
6895e8f634SShane McDonald #define modeindex(v) ((v) & FPU_CSR_RM)
6995e8f634SShane McDonald 
701da177e4SLinus Torvalds /* convert condition code register number to csr bit */
711da177e4SLinus Torvalds static const unsigned int fpucondbit[8] = {
721da177e4SLinus Torvalds 	FPU_CSR_COND0,
731da177e4SLinus Torvalds 	FPU_CSR_COND1,
741da177e4SLinus Torvalds 	FPU_CSR_COND2,
751da177e4SLinus Torvalds 	FPU_CSR_COND3,
761da177e4SLinus Torvalds 	FPU_CSR_COND4,
771da177e4SLinus Torvalds 	FPU_CSR_COND5,
781da177e4SLinus Torvalds 	FPU_CSR_COND6,
791da177e4SLinus Torvalds 	FPU_CSR_COND7
801da177e4SLinus Torvalds };
811da177e4SLinus Torvalds 
82102cedc3SLeonid Yegoshin /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
83102cedc3SLeonid Yegoshin static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
84102cedc3SLeonid Yegoshin static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
85102cedc3SLeonid Yegoshin static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
86102cedc3SLeonid Yegoshin static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
87102cedc3SLeonid Yegoshin 
88102cedc3SLeonid Yegoshin /*
89102cedc3SLeonid Yegoshin  * This functions translates a 32-bit microMIPS instruction
90102cedc3SLeonid Yegoshin  * into a 32-bit MIPS32 instruction. Returns 0 on success
91102cedc3SLeonid Yegoshin  * and SIGILL otherwise.
92102cedc3SLeonid Yegoshin  */
93102cedc3SLeonid Yegoshin static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
94102cedc3SLeonid Yegoshin {
95102cedc3SLeonid Yegoshin 	union mips_instruction insn = *insn_ptr;
96102cedc3SLeonid Yegoshin 	union mips_instruction mips32_insn = insn;
97102cedc3SLeonid Yegoshin 	int func, fmt, op;
98102cedc3SLeonid Yegoshin 
99102cedc3SLeonid Yegoshin 	switch (insn.mm_i_format.opcode) {
100102cedc3SLeonid Yegoshin 	case mm_ldc132_op:
101102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = ldc1_op;
102102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
103102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
104102cedc3SLeonid Yegoshin 		break;
105102cedc3SLeonid Yegoshin 	case mm_lwc132_op:
106102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = lwc1_op;
107102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
108102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
109102cedc3SLeonid Yegoshin 		break;
110102cedc3SLeonid Yegoshin 	case mm_sdc132_op:
111102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = sdc1_op;
112102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
113102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
114102cedc3SLeonid Yegoshin 		break;
115102cedc3SLeonid Yegoshin 	case mm_swc132_op:
116102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = swc1_op;
117102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
118102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
119102cedc3SLeonid Yegoshin 		break;
120102cedc3SLeonid Yegoshin 	case mm_pool32i_op:
121102cedc3SLeonid Yegoshin 		/* NOTE: offset is << by 1 if in microMIPS mode. */
122102cedc3SLeonid Yegoshin 		if ((insn.mm_i_format.rt == mm_bc1f_op) ||
123102cedc3SLeonid Yegoshin 		    (insn.mm_i_format.rt == mm_bc1t_op)) {
124102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.opcode = cop1_op;
125102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.bc = bc_op;
126102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.flag =
127102cedc3SLeonid Yegoshin 				(insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
128102cedc3SLeonid Yegoshin 		} else
129102cedc3SLeonid Yegoshin 			return SIGILL;
130102cedc3SLeonid Yegoshin 		break;
131102cedc3SLeonid Yegoshin 	case mm_pool32f_op:
132102cedc3SLeonid Yegoshin 		switch (insn.mm_fp0_format.func) {
133102cedc3SLeonid Yegoshin 		case mm_32f_01_op:
134102cedc3SLeonid Yegoshin 		case mm_32f_11_op:
135102cedc3SLeonid Yegoshin 		case mm_32f_02_op:
136102cedc3SLeonid Yegoshin 		case mm_32f_12_op:
137102cedc3SLeonid Yegoshin 		case mm_32f_41_op:
138102cedc3SLeonid Yegoshin 		case mm_32f_51_op:
139102cedc3SLeonid Yegoshin 		case mm_32f_42_op:
140102cedc3SLeonid Yegoshin 		case mm_32f_52_op:
141102cedc3SLeonid Yegoshin 			op = insn.mm_fp0_format.func;
142102cedc3SLeonid Yegoshin 			if (op == mm_32f_01_op)
143102cedc3SLeonid Yegoshin 				func = madd_s_op;
144102cedc3SLeonid Yegoshin 			else if (op == mm_32f_11_op)
145102cedc3SLeonid Yegoshin 				func = madd_d_op;
146102cedc3SLeonid Yegoshin 			else if (op == mm_32f_02_op)
147102cedc3SLeonid Yegoshin 				func = nmadd_s_op;
148102cedc3SLeonid Yegoshin 			else if (op == mm_32f_12_op)
149102cedc3SLeonid Yegoshin 				func = nmadd_d_op;
150102cedc3SLeonid Yegoshin 			else if (op == mm_32f_41_op)
151102cedc3SLeonid Yegoshin 				func = msub_s_op;
152102cedc3SLeonid Yegoshin 			else if (op == mm_32f_51_op)
153102cedc3SLeonid Yegoshin 				func = msub_d_op;
154102cedc3SLeonid Yegoshin 			else if (op == mm_32f_42_op)
155102cedc3SLeonid Yegoshin 				func = nmsub_s_op;
156102cedc3SLeonid Yegoshin 			else
157102cedc3SLeonid Yegoshin 				func = nmsub_d_op;
158102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.opcode = cop1x_op;
159102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
160102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
161102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
162102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
163102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.func = func;
164102cedc3SLeonid Yegoshin 			break;
165102cedc3SLeonid Yegoshin 		case mm_32f_10_op:
166102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
167102cedc3SLeonid Yegoshin 			op = insn.mm_fp5_format.op & 0x7;
168102cedc3SLeonid Yegoshin 			if (op == mm_ldxc1_op)
169102cedc3SLeonid Yegoshin 				func = ldxc1_op;
170102cedc3SLeonid Yegoshin 			else if (op == mm_sdxc1_op)
171102cedc3SLeonid Yegoshin 				func = sdxc1_op;
172102cedc3SLeonid Yegoshin 			else if (op == mm_lwxc1_op)
173102cedc3SLeonid Yegoshin 				func = lwxc1_op;
174102cedc3SLeonid Yegoshin 			else if (op == mm_swxc1_op)
175102cedc3SLeonid Yegoshin 				func = swxc1_op;
176102cedc3SLeonid Yegoshin 
177102cedc3SLeonid Yegoshin 			if (func != -1) {
178102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = cop1x_op;
179102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs =
180102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.base;
181102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
182102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.index;
183102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = 0;
184102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = insn.mm_fp5_format.fd;
185102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = func;
186102cedc3SLeonid Yegoshin 			} else
187102cedc3SLeonid Yegoshin 				return SIGILL;
188102cedc3SLeonid Yegoshin 			break;
189102cedc3SLeonid Yegoshin 		case mm_32f_40_op:
190102cedc3SLeonid Yegoshin 			op = -1;	/* Invalid */
191102cedc3SLeonid Yegoshin 			if (insn.mm_fp2_format.op == mm_fmovt_op)
192102cedc3SLeonid Yegoshin 				op = 1;
193102cedc3SLeonid Yegoshin 			else if (insn.mm_fp2_format.op == mm_fmovf_op)
194102cedc3SLeonid Yegoshin 				op = 0;
195102cedc3SLeonid Yegoshin 			if (op != -1) {
196102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
197102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
198102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp2_format.fmt];
199102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
200102cedc3SLeonid Yegoshin 					(insn.mm_fp2_format.cc<<2) + op;
201102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
202102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fs;
203102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
204102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fd;
205102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = fmovc_op;
206102cedc3SLeonid Yegoshin 			} else
207102cedc3SLeonid Yegoshin 				return SIGILL;
208102cedc3SLeonid Yegoshin 			break;
209102cedc3SLeonid Yegoshin 		case mm_32f_60_op:
210102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
211102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fadd_op)
212102cedc3SLeonid Yegoshin 				func = fadd_op;
213102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fsub_op)
214102cedc3SLeonid Yegoshin 				func = fsub_op;
215102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmul_op)
216102cedc3SLeonid Yegoshin 				func = fmul_op;
217102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fdiv_op)
218102cedc3SLeonid Yegoshin 				func = fdiv_op;
219102cedc3SLeonid Yegoshin 			if (func != -1) {
220102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
221102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
222102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
223102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
224102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
225102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
226102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
227102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
228102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
229102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
230102cedc3SLeonid Yegoshin 			} else
231102cedc3SLeonid Yegoshin 				return SIGILL;
232102cedc3SLeonid Yegoshin 			break;
233102cedc3SLeonid Yegoshin 		case mm_32f_70_op:
234102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
235102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fmovn_op)
236102cedc3SLeonid Yegoshin 				func = fmovn_op;
237102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmovz_op)
238102cedc3SLeonid Yegoshin 				func = fmovz_op;
239102cedc3SLeonid Yegoshin 			if (func != -1) {
240102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
241102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
242102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
243102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
244102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
245102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
246102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
247102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
248102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
249102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
250102cedc3SLeonid Yegoshin 			} else
251102cedc3SLeonid Yegoshin 				return SIGILL;
252102cedc3SLeonid Yegoshin 			break;
253102cedc3SLeonid Yegoshin 		case mm_32f_73_op:    /* POOL32FXF */
254102cedc3SLeonid Yegoshin 			switch (insn.mm_fp1_format.op) {
255102cedc3SLeonid Yegoshin 			case mm_movf0_op:
256102cedc3SLeonid Yegoshin 			case mm_movf1_op:
257102cedc3SLeonid Yegoshin 			case mm_movt0_op:
258102cedc3SLeonid Yegoshin 			case mm_movt1_op:
259102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
260102cedc3SLeonid Yegoshin 				    mm_movf0_op)
261102cedc3SLeonid Yegoshin 					op = 0;
262102cedc3SLeonid Yegoshin 				else
263102cedc3SLeonid Yegoshin 					op = 1;
264102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = spec_op;
265102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
266102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
267102cedc3SLeonid Yegoshin 					(insn.mm_fp4_format.cc << 2) + op;
268102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
269102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = 0;
270102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = movc_op;
271102cedc3SLeonid Yegoshin 				break;
272102cedc3SLeonid Yegoshin 			case mm_fcvtd0_op:
273102cedc3SLeonid Yegoshin 			case mm_fcvtd1_op:
274102cedc3SLeonid Yegoshin 			case mm_fcvts0_op:
275102cedc3SLeonid Yegoshin 			case mm_fcvts1_op:
276102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
277102cedc3SLeonid Yegoshin 				    mm_fcvtd0_op) {
278102cedc3SLeonid Yegoshin 					func = fcvtd_op;
279102cedc3SLeonid Yegoshin 					fmt = swl_format[insn.mm_fp3_format.fmt];
280102cedc3SLeonid Yegoshin 				} else {
281102cedc3SLeonid Yegoshin 					func = fcvts_op;
282102cedc3SLeonid Yegoshin 					fmt = dwl_format[insn.mm_fp3_format.fmt];
283102cedc3SLeonid Yegoshin 				}
284102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
285102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt = fmt;
286102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
287102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
288102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
289102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
290102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
291102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
292102cedc3SLeonid Yegoshin 				break;
293102cedc3SLeonid Yegoshin 			case mm_fmov0_op:
294102cedc3SLeonid Yegoshin 			case mm_fmov1_op:
295102cedc3SLeonid Yegoshin 			case mm_fabs0_op:
296102cedc3SLeonid Yegoshin 			case mm_fabs1_op:
297102cedc3SLeonid Yegoshin 			case mm_fneg0_op:
298102cedc3SLeonid Yegoshin 			case mm_fneg1_op:
299102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
300102cedc3SLeonid Yegoshin 				    mm_fmov0_op)
301102cedc3SLeonid Yegoshin 					func = fmov_op;
302102cedc3SLeonid Yegoshin 				else if ((insn.mm_fp1_format.op & 0x7f) ==
303102cedc3SLeonid Yegoshin 					 mm_fabs0_op)
304102cedc3SLeonid Yegoshin 					func = fabs_op;
305102cedc3SLeonid Yegoshin 				else
306102cedc3SLeonid Yegoshin 					func = fneg_op;
307102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
308102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
309102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp3_format.fmt];
310102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
311102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
312102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
313102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
314102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
315102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
316102cedc3SLeonid Yegoshin 				break;
317102cedc3SLeonid Yegoshin 			case mm_ffloorl_op:
318102cedc3SLeonid Yegoshin 			case mm_ffloorw_op:
319102cedc3SLeonid Yegoshin 			case mm_fceill_op:
320102cedc3SLeonid Yegoshin 			case mm_fceilw_op:
321102cedc3SLeonid Yegoshin 			case mm_ftruncl_op:
322102cedc3SLeonid Yegoshin 			case mm_ftruncw_op:
323102cedc3SLeonid Yegoshin 			case mm_froundl_op:
324102cedc3SLeonid Yegoshin 			case mm_froundw_op:
325102cedc3SLeonid Yegoshin 			case mm_fcvtl_op:
326102cedc3SLeonid Yegoshin 			case mm_fcvtw_op:
327102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_ffloorl_op)
328102cedc3SLeonid Yegoshin 					func = ffloorl_op;
329102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ffloorw_op)
330102cedc3SLeonid Yegoshin 					func = ffloor_op;
331102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceill_op)
332102cedc3SLeonid Yegoshin 					func = fceill_op;
333102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceilw_op)
334102cedc3SLeonid Yegoshin 					func = fceil_op;
335102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncl_op)
336102cedc3SLeonid Yegoshin 					func = ftruncl_op;
337102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncw_op)
338102cedc3SLeonid Yegoshin 					func = ftrunc_op;
339102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundl_op)
340102cedc3SLeonid Yegoshin 					func = froundl_op;
341102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundw_op)
342102cedc3SLeonid Yegoshin 					func = fround_op;
343102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fcvtl_op)
344102cedc3SLeonid Yegoshin 					func = fcvtl_op;
345102cedc3SLeonid Yegoshin 				else
346102cedc3SLeonid Yegoshin 					func = fcvtw_op;
347102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
348102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
349102cedc3SLeonid Yegoshin 					sd_format[insn.mm_fp1_format.fmt];
350102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
351102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
352102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
353102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
354102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
355102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
356102cedc3SLeonid Yegoshin 				break;
357102cedc3SLeonid Yegoshin 			case mm_frsqrt_op:
358102cedc3SLeonid Yegoshin 			case mm_fsqrt_op:
359102cedc3SLeonid Yegoshin 			case mm_frecip_op:
360102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_frsqrt_op)
361102cedc3SLeonid Yegoshin 					func = frsqrt_op;
362102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fsqrt_op)
363102cedc3SLeonid Yegoshin 					func = fsqrt_op;
364102cedc3SLeonid Yegoshin 				else
365102cedc3SLeonid Yegoshin 					func = frecip_op;
366102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
367102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
368102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp1_format.fmt];
369102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
370102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
371102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
372102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
373102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
374102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
375102cedc3SLeonid Yegoshin 				break;
376102cedc3SLeonid Yegoshin 			case mm_mfc1_op:
377102cedc3SLeonid Yegoshin 			case mm_mtc1_op:
378102cedc3SLeonid Yegoshin 			case mm_cfc1_op:
379102cedc3SLeonid Yegoshin 			case mm_ctc1_op:
3809355e59cSSteven J. Hill 			case mm_mfhc1_op:
3819355e59cSSteven J. Hill 			case mm_mthc1_op:
382102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_mfc1_op)
383102cedc3SLeonid Yegoshin 					op = mfc_op;
384102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_mtc1_op)
385102cedc3SLeonid Yegoshin 					op = mtc_op;
386102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_cfc1_op)
387102cedc3SLeonid Yegoshin 					op = cfc_op;
3889355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_ctc1_op)
389102cedc3SLeonid Yegoshin 					op = ctc_op;
3909355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_mfhc1_op)
3919355e59cSSteven J. Hill 					op = mfhc_op;
3929355e59cSSteven J. Hill 				else
3939355e59cSSteven J. Hill 					op = mthc_op;
394102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.opcode = cop1_op;
395102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.op = op;
396102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.rt =
397102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
398102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fs =
399102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
400102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fd = 0;
401102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.func = 0;
402102cedc3SLeonid Yegoshin 				break;
403102cedc3SLeonid Yegoshin 			default:
404102cedc3SLeonid Yegoshin 				return SIGILL;
405102cedc3SLeonid Yegoshin 			}
406102cedc3SLeonid Yegoshin 			break;
407102cedc3SLeonid Yegoshin 		case mm_32f_74_op:	/* c.cond.fmt */
408102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.opcode = cop1_op;
409102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fmt =
410102cedc3SLeonid Yegoshin 				sdps_format[insn.mm_fp4_format.fmt];
411102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
412102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
413102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
414102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.func =
415102cedc3SLeonid Yegoshin 				insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
416102cedc3SLeonid Yegoshin 			break;
417102cedc3SLeonid Yegoshin 		default:
418102cedc3SLeonid Yegoshin 			return SIGILL;
419102cedc3SLeonid Yegoshin 		}
420102cedc3SLeonid Yegoshin 		break;
421102cedc3SLeonid Yegoshin 	default:
422102cedc3SLeonid Yegoshin 		return SIGILL;
423102cedc3SLeonid Yegoshin 	}
424102cedc3SLeonid Yegoshin 
425102cedc3SLeonid Yegoshin 	*insn_ptr = mips32_insn;
426102cedc3SLeonid Yegoshin 	return 0;
427102cedc3SLeonid Yegoshin }
428102cedc3SLeonid Yegoshin 
4291da177e4SLinus Torvalds /*
4301da177e4SLinus Torvalds  * Redundant with logic already in kernel/branch.c,
4311da177e4SLinus Torvalds  * embedded in compute_return_epc.  At some point,
4321da177e4SLinus Torvalds  * a single subroutine should be used across both
4331da177e4SLinus Torvalds  * modules.
4341da177e4SLinus Torvalds  */
435102cedc3SLeonid Yegoshin static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
436102cedc3SLeonid Yegoshin 			 unsigned long *contpc)
4371da177e4SLinus Torvalds {
438102cedc3SLeonid Yegoshin 	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
439102cedc3SLeonid Yegoshin 	unsigned int fcr31;
440102cedc3SLeonid Yegoshin 	unsigned int bit = 0;
441102cedc3SLeonid Yegoshin 
442102cedc3SLeonid Yegoshin 	switch (insn.i_format.opcode) {
4431da177e4SLinus Torvalds 	case spec_op:
444102cedc3SLeonid Yegoshin 		switch (insn.r_format.func) {
4451da177e4SLinus Torvalds 		case jalr_op:
446102cedc3SLeonid Yegoshin 			regs->regs[insn.r_format.rd] =
447102cedc3SLeonid Yegoshin 				regs->cp0_epc + dec_insn.pc_inc +
448102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
449102cedc3SLeonid Yegoshin 			/* Fall through */
4501da177e4SLinus Torvalds 		case jr_op:
4515f9f41c4SMarkos Chandras 			/* For R6, JR already emulated in jalr_op */
4525f9f41c4SMarkos Chandras 			if (NO_R6EMU && insn.r_format.opcode == jr_op)
4535f9f41c4SMarkos Chandras 				break;
454102cedc3SLeonid Yegoshin 			*contpc = regs->regs[insn.r_format.rs];
4551da177e4SLinus Torvalds 			return 1;
4561da177e4SLinus Torvalds 		}
4571da177e4SLinus Torvalds 		break;
4581da177e4SLinus Torvalds 	case bcond_op:
459102cedc3SLeonid Yegoshin 		switch (insn.i_format.rt) {
4601da177e4SLinus Torvalds 		case bltzal_op:
4611da177e4SLinus Torvalds 		case bltzall_op:
462319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
463319824eaSMarkos Chandras 			    insn.i_format.rt == bltzall_op))
464319824eaSMarkos Chandras 				break;
465319824eaSMarkos Chandras 
466102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
467102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
468102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
469102cedc3SLeonid Yegoshin 			/* Fall through */
470102cedc3SLeonid Yegoshin 		case bltzl_op:
471319824eaSMarkos Chandras 			if (NO_R6EMU)
472319824eaSMarkos Chandras 				break;
473319824eaSMarkos Chandras 		case bltz_op:
474102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] < 0)
475102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
476102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
477102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
478102cedc3SLeonid Yegoshin 			else
479102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
480102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
481102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
4821da177e4SLinus Torvalds 			return 1;
483102cedc3SLeonid Yegoshin 		case bgezal_op:
484102cedc3SLeonid Yegoshin 		case bgezall_op:
485319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
486319824eaSMarkos Chandras 			    insn.i_format.rt == bgezall_op))
487319824eaSMarkos Chandras 				break;
488319824eaSMarkos Chandras 
489102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
490102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
491102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
492102cedc3SLeonid Yegoshin 			/* Fall through */
493102cedc3SLeonid Yegoshin 		case bgezl_op:
494319824eaSMarkos Chandras 			if (NO_R6EMU)
495319824eaSMarkos Chandras 				break;
496319824eaSMarkos Chandras 		case bgez_op:
497102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] >= 0)
498102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
499102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
500102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
501102cedc3SLeonid Yegoshin 			else
502102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
503102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
504102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
505102cedc3SLeonid Yegoshin 			return 1;
5061da177e4SLinus Torvalds 		}
5071da177e4SLinus Torvalds 		break;
5081da177e4SLinus Torvalds 	case jalx_op:
509102cedc3SLeonid Yegoshin 		set_isa16_mode(bit);
510102cedc3SLeonid Yegoshin 	case jal_op:
511102cedc3SLeonid Yegoshin 		regs->regs[31] = regs->cp0_epc +
512102cedc3SLeonid Yegoshin 			dec_insn.pc_inc +
513102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc;
514102cedc3SLeonid Yegoshin 		/* Fall through */
515102cedc3SLeonid Yegoshin 	case j_op:
516102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
517102cedc3SLeonid Yegoshin 		*contpc >>= 28;
518102cedc3SLeonid Yegoshin 		*contpc <<= 28;
519102cedc3SLeonid Yegoshin 		*contpc |= (insn.j_format.target << 2);
520102cedc3SLeonid Yegoshin 		/* Set microMIPS mode bit: XOR for jalx. */
521102cedc3SLeonid Yegoshin 		*contpc ^= bit;
5221da177e4SLinus Torvalds 		return 1;
523102cedc3SLeonid Yegoshin 	case beql_op:
524319824eaSMarkos Chandras 		if (NO_R6EMU)
525319824eaSMarkos Chandras 			break;
526319824eaSMarkos Chandras 	case beq_op:
527102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] ==
528102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
529102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
530102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
531102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
532102cedc3SLeonid Yegoshin 		else
533102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
534102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
535102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
536102cedc3SLeonid Yegoshin 		return 1;
537102cedc3SLeonid Yegoshin 	case bnel_op:
538319824eaSMarkos Chandras 		if (NO_R6EMU)
539319824eaSMarkos Chandras 			break;
540319824eaSMarkos Chandras 	case bne_op:
541102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] !=
542102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
543102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
544102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
545102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
546102cedc3SLeonid Yegoshin 		else
547102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
548102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
549102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
550102cedc3SLeonid Yegoshin 		return 1;
551102cedc3SLeonid Yegoshin 	case blezl_op:
552319824eaSMarkos Chandras 		if (NO_R6EMU)
553319824eaSMarkos Chandras 			break;
554319824eaSMarkos Chandras 	case blez_op:
555102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] <= 0)
556102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
557102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
558102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
559102cedc3SLeonid Yegoshin 		else
560102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
561102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
562102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
563102cedc3SLeonid Yegoshin 		return 1;
564102cedc3SLeonid Yegoshin 	case bgtzl_op:
565319824eaSMarkos Chandras 		if (NO_R6EMU)
566319824eaSMarkos Chandras 			break;
567319824eaSMarkos Chandras 	case bgtz_op:
568102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] > 0)
569102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
570102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
571102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
572102cedc3SLeonid Yegoshin 		else
573102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
574102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
575102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
576102cedc3SLeonid Yegoshin 		return 1;
577c26d4219SDavid Daney #ifdef CONFIG_CPU_CAVIUM_OCTEON
578c26d4219SDavid Daney 	case lwc2_op: /* This is bbit0 on Octeon */
579c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
580c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
581c26d4219SDavid Daney 		else
582c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
583c26d4219SDavid Daney 		return 1;
584c26d4219SDavid Daney 	case ldc2_op: /* This is bbit032 on Octeon */
585c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
586c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
587c26d4219SDavid Daney 		else
588c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
589c26d4219SDavid Daney 		return 1;
590c26d4219SDavid Daney 	case swc2_op: /* This is bbit1 on Octeon */
591c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
592c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
593c26d4219SDavid Daney 		else
594c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
595c26d4219SDavid Daney 		return 1;
596c26d4219SDavid Daney 	case sdc2_op: /* This is bbit132 on Octeon */
597c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
598c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
599c26d4219SDavid Daney 		else
600c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
601c26d4219SDavid Daney 		return 1;
602c26d4219SDavid Daney #endif
6031da177e4SLinus Torvalds 	case cop0_op:
6041da177e4SLinus Torvalds 	case cop1_op:
605*c8a34581SMarkos Chandras 		/* Need to check for R6 bc1nez and bc1eqz branches */
606*c8a34581SMarkos Chandras 		if (cpu_has_mips_r6 &&
607*c8a34581SMarkos Chandras 		    ((insn.i_format.rs == bc1eqz_op) ||
608*c8a34581SMarkos Chandras 		     (insn.i_format.rs == bc1nez_op))) {
609*c8a34581SMarkos Chandras 			bit = 0;
610*c8a34581SMarkos Chandras 			switch (insn.i_format.rs) {
611*c8a34581SMarkos Chandras 			case bc1eqz_op:
612*c8a34581SMarkos Chandras 				if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
613*c8a34581SMarkos Chandras 				    bit = 1;
614*c8a34581SMarkos Chandras 				break;
615*c8a34581SMarkos Chandras 			case bc1nez_op:
616*c8a34581SMarkos Chandras 				if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
617*c8a34581SMarkos Chandras 				    bit = 1;
618*c8a34581SMarkos Chandras 				break;
619*c8a34581SMarkos Chandras 			}
620*c8a34581SMarkos Chandras 			if (bit)
621*c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
622*c8a34581SMarkos Chandras 					dec_insn.pc_inc +
623*c8a34581SMarkos Chandras 					(insn.i_format.simmediate << 2);
624*c8a34581SMarkos Chandras 			else
625*c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
626*c8a34581SMarkos Chandras 					dec_insn.pc_inc +
627*c8a34581SMarkos Chandras 					dec_insn.next_pc_inc;
628*c8a34581SMarkos Chandras 
629*c8a34581SMarkos Chandras 			return 1;
630*c8a34581SMarkos Chandras 		}
631*c8a34581SMarkos Chandras 		/* R2/R6 compatible cop1 instruction. Fall through */
6321da177e4SLinus Torvalds 	case cop2_op:
6331da177e4SLinus Torvalds 	case cop1x_op:
634102cedc3SLeonid Yegoshin 		if (insn.i_format.rs == bc_op) {
635102cedc3SLeonid Yegoshin 			preempt_disable();
636102cedc3SLeonid Yegoshin 			if (is_fpu_owner())
637842dfc11SManuel Lauss 			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
638102cedc3SLeonid Yegoshin 			else
639102cedc3SLeonid Yegoshin 				fcr31 = current->thread.fpu.fcr31;
640102cedc3SLeonid Yegoshin 			preempt_enable();
641102cedc3SLeonid Yegoshin 
642102cedc3SLeonid Yegoshin 			bit = (insn.i_format.rt >> 2);
643102cedc3SLeonid Yegoshin 			bit += (bit != 0);
644102cedc3SLeonid Yegoshin 			bit += 23;
645102cedc3SLeonid Yegoshin 			switch (insn.i_format.rt & 3) {
646102cedc3SLeonid Yegoshin 			case 0:	/* bc1f */
647102cedc3SLeonid Yegoshin 			case 2:	/* bc1fl */
648102cedc3SLeonid Yegoshin 				if (~fcr31 & (1 << bit))
649102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
650102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
651102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
652102cedc3SLeonid Yegoshin 				else
653102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
654102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
655102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
656102cedc3SLeonid Yegoshin 				return 1;
657102cedc3SLeonid Yegoshin 			case 1:	/* bc1t */
658102cedc3SLeonid Yegoshin 			case 3:	/* bc1tl */
659102cedc3SLeonid Yegoshin 				if (fcr31 & (1 << bit))
660102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
661102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
662102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
663102cedc3SLeonid Yegoshin 				else
664102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
665102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
666102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
6671da177e4SLinus Torvalds 				return 1;
6681da177e4SLinus Torvalds 			}
669102cedc3SLeonid Yegoshin 		}
670102cedc3SLeonid Yegoshin 		break;
671102cedc3SLeonid Yegoshin 	}
6721da177e4SLinus Torvalds 	return 0;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds /*
6761da177e4SLinus Torvalds  * In the Linux kernel, we support selection of FPR format on the
677da0bac33SDavid Daney  * basis of the Status.FR bit.	If an FPU is not present, the FR bit
678da0bac33SDavid Daney  * is hardwired to zero, which would imply a 32-bit FPU even for
679597ce172SPaul Burton  * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
68051d943f0SRalf Baechle  * FPU emu is slow and bulky and optimizing this function offers fairly
68151d943f0SRalf Baechle  * sizeable benefits so we try to be clever and make this function return
68251d943f0SRalf Baechle  * a constant whenever possible, that is on 64-bit kernels without O32
683597ce172SPaul Burton  * compatibility enabled and on 32-bit without 64-bit FPU support.
6841da177e4SLinus Torvalds  */
685da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp)
686da0bac33SDavid Daney {
68708a07904SRalf Baechle 	if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
68851d943f0SRalf Baechle 		return 1;
68908a07904SRalf Baechle 	else if (config_enabled(CONFIG_32BIT) &&
69008a07904SRalf Baechle 		 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
691da0bac33SDavid Daney 		return 0;
69208a07904SRalf Baechle 
693597ce172SPaul Burton 	return !test_thread_flag(TIF_32BIT_FPREGS);
694da0bac33SDavid Daney }
6951da177e4SLinus Torvalds 
6964227a2d4SPaul Burton static inline bool hybrid_fprs(void)
6974227a2d4SPaul Burton {
6984227a2d4SPaul Burton 	return test_thread_flag(TIF_HYBRID_FPREGS);
6994227a2d4SPaul Burton }
7004227a2d4SPaul Burton 
70147fa0c02SRalf Baechle #define SIFROMREG(si, x)						\
70247fa0c02SRalf Baechle do {									\
7034227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs())				\
704c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[x], 0);			\
705bbd426f5SPaul Burton 	else								\
706c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);	\
707bbd426f5SPaul Burton } while (0)
708da0bac33SDavid Daney 
70947fa0c02SRalf Baechle #define SITOREG(si, x)							\
71047fa0c02SRalf Baechle do {									\
7114227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs()) {			\
712ef1c47afSPaul Burton 		unsigned i;						\
713bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[x], 0, si);				\
714ef1c47afSPaul Burton 		for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)	\
715ef1c47afSPaul Burton 			set_fpr32(&ctx->fpr[x], i, 0);			\
716ef1c47afSPaul Burton 	} else {							\
717bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);		\
718ef1c47afSPaul Burton 	}								\
719bbd426f5SPaul Burton } while (0)
7201da177e4SLinus Torvalds 
721c8c0da6bSPaul Burton #define SIFROMHREG(si, x)	((si) = (int)get_fpr32(&ctx->fpr[x], 1))
722ef1c47afSPaul Burton 
72347fa0c02SRalf Baechle #define SITOHREG(si, x)							\
72447fa0c02SRalf Baechle do {									\
725ef1c47afSPaul Burton 	unsigned i;							\
726ef1c47afSPaul Burton 	set_fpr32(&ctx->fpr[x], 1, si);					\
727ef1c47afSPaul Burton 	for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)		\
728ef1c47afSPaul Burton 		set_fpr32(&ctx->fpr[x], i, 0);				\
729ef1c47afSPaul Burton } while (0)
7301ac94400SLeonid Yegoshin 
731bbd426f5SPaul Burton #define DIFROMREG(di, x)						\
732bbd426f5SPaul Burton 	((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
733bbd426f5SPaul Burton 
73447fa0c02SRalf Baechle #define DITOREG(di, x)							\
73547fa0c02SRalf Baechle do {									\
736ef1c47afSPaul Burton 	unsigned fpr, i;						\
737ef1c47afSPaul Burton 	fpr = (x) & ~(cop1_64bit(xcp) == 0);				\
738ef1c47afSPaul Burton 	set_fpr64(&ctx->fpr[fpr], 0, di);				\
739ef1c47afSPaul Burton 	for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++)		\
740ef1c47afSPaul Burton 		set_fpr64(&ctx->fpr[fpr], i, 0);			\
741ef1c47afSPaul Burton } while (0)
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
7441da177e4SLinus Torvalds #define SPTOREG(sp, x)	SITOREG((sp).bits, x)
7451da177e4SLinus Torvalds #define DPFROMREG(dp, x)	DIFROMREG((dp).bits, x)
7461da177e4SLinus Torvalds #define DPTOREG(dp, x)	DITOREG((dp).bits, x)
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds /*
7491da177e4SLinus Torvalds  * Emulate the single floating point instruction pointed at by EPC.
7501da177e4SLinus Torvalds  * Two instructions if the instruction is in a branch delay slot.
7511da177e4SLinus Torvalds  */
7521da177e4SLinus Torvalds 
753515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
754102cedc3SLeonid Yegoshin 		struct mm_decoded_insn dec_insn, void *__user *fault_addr)
7551da177e4SLinus Torvalds {
756102cedc3SLeonid Yegoshin 	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
7573f7cac41SRalf Baechle 	unsigned int cond, cbit;
7583f7cac41SRalf Baechle 	mips_instruction ir;
7593f7cac41SRalf Baechle 	int likely, pc_inc;
7603f7cac41SRalf Baechle 	u32 __user *wva;
7613f7cac41SRalf Baechle 	u64 __user *dva;
7623f7cac41SRalf Baechle 	u32 value;
7633f7cac41SRalf Baechle 	u32 wval;
7643f7cac41SRalf Baechle 	u64 dval;
7653f7cac41SRalf Baechle 	int sig;
7661da177e4SLinus Torvalds 
76770e4c234SRalf Baechle 	/*
76870e4c234SRalf Baechle 	 * These are giving gcc a gentle hint about what to expect in
76970e4c234SRalf Baechle 	 * dec_inst in order to do better optimization.
77070e4c234SRalf Baechle 	 */
77170e4c234SRalf Baechle 	if (!cpu_has_mmips && dec_insn.micro_mips_mode)
77270e4c234SRalf Baechle 		unreachable();
77370e4c234SRalf Baechle 
7741da177e4SLinus Torvalds 	/* XXX NEC Vr54xx bug workaround */
775e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
776102cedc3SLeonid Yegoshin 		if (dec_insn.micro_mips_mode) {
777102cedc3SLeonid Yegoshin 			if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
778e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
779102cedc3SLeonid Yegoshin 		} else {
780102cedc3SLeonid Yegoshin 			if (!isBranchInstr(xcp, dec_insn, &contpc))
781e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
782102cedc3SLeonid Yegoshin 		}
783102cedc3SLeonid Yegoshin 	}
7841da177e4SLinus Torvalds 
785e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
7861da177e4SLinus Torvalds 		/*
7871da177e4SLinus Torvalds 		 * The instruction to be emulated is in a branch delay slot
7881da177e4SLinus Torvalds 		 * which means that we have to	emulate the branch instruction
7891da177e4SLinus Torvalds 		 * BEFORE we do the cop1 instruction.
7901da177e4SLinus Torvalds 		 *
7911da177e4SLinus Torvalds 		 * This branch could be a COP1 branch, but in that case we
7921da177e4SLinus Torvalds 		 * would have had a trap for that instruction, and would not
7931da177e4SLinus Torvalds 		 * come through this route.
7941da177e4SLinus Torvalds 		 *
7951da177e4SLinus Torvalds 		 * Linux MIPS branch emulator operates on context, updating the
7961da177e4SLinus Torvalds 		 * cp0_epc.
7971da177e4SLinus Torvalds 		 */
798102cedc3SLeonid Yegoshin 		ir = dec_insn.next_insn;  /* process delay slot instr */
799102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.next_pc_inc;
800333d1f67SRalf Baechle 	} else {
801102cedc3SLeonid Yegoshin 		ir = dec_insn.insn;       /* process current instr */
802102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.pc_inc;
803102cedc3SLeonid Yegoshin 	}
804102cedc3SLeonid Yegoshin 
805102cedc3SLeonid Yegoshin 	/*
806102cedc3SLeonid Yegoshin 	 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
807102cedc3SLeonid Yegoshin 	 * instructions, we want to convert microMIPS FPU instructions
808102cedc3SLeonid Yegoshin 	 * into MIPS32 instructions so that we could reuse all of the
809102cedc3SLeonid Yegoshin 	 * FPU emulation code.
810102cedc3SLeonid Yegoshin 	 *
811102cedc3SLeonid Yegoshin 	 * NOTE: We cannot do this for branch instructions since they
812102cedc3SLeonid Yegoshin 	 *       are not a subset. Example: Cannot emulate a 16-bit
813102cedc3SLeonid Yegoshin 	 *       aligned target address with a MIPS32 instruction.
814102cedc3SLeonid Yegoshin 	 */
815102cedc3SLeonid Yegoshin 	if (dec_insn.micro_mips_mode) {
816102cedc3SLeonid Yegoshin 		/*
817102cedc3SLeonid Yegoshin 		 * If next instruction is a 16-bit instruction, then it
818102cedc3SLeonid Yegoshin 		 * it cannot be a FPU instruction. This could happen
819102cedc3SLeonid Yegoshin 		 * since we can be called for non-FPU instructions.
820102cedc3SLeonid Yegoshin 		 */
821102cedc3SLeonid Yegoshin 		if ((pc_inc == 2) ||
822102cedc3SLeonid Yegoshin 			(microMIPS32_to_MIPS32((union mips_instruction *)&ir)
823102cedc3SLeonid Yegoshin 			 == SIGILL))
824102cedc3SLeonid Yegoshin 			return SIGILL;
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds emul:
828a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
829b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(emulated);
8301da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(ir)) {
8313f7cac41SRalf Baechle 	case ldc1_op:
8323f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
8331da177e4SLinus Torvalds 				     MIPSInst_SIMM(ir));
834b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
835515b029dSDavid Daney 
8363f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
837b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
8383f7cac41SRalf Baechle 			*fault_addr = dva;
8391da177e4SLinus Torvalds 			return SIGBUS;
8401da177e4SLinus Torvalds 		}
8413f7cac41SRalf Baechle 		if (__get_user(dval, dva)) {
842515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
8433f7cac41SRalf Baechle 			*fault_addr = dva;
844515b029dSDavid Daney 			return SIGSEGV;
845515b029dSDavid Daney 		}
8463f7cac41SRalf Baechle 		DITOREG(dval, MIPSInst_RT(ir));
8471da177e4SLinus Torvalds 		break;
8481da177e4SLinus Torvalds 
8493f7cac41SRalf Baechle 	case sdc1_op:
8503f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
8511da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
852b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
8533f7cac41SRalf Baechle 		DIFROMREG(dval, MIPSInst_RT(ir));
8543f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
855b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
8563f7cac41SRalf Baechle 			*fault_addr = dva;
8571da177e4SLinus Torvalds 			return SIGBUS;
8581da177e4SLinus Torvalds 		}
8593f7cac41SRalf Baechle 		if (__put_user(dval, dva)) {
860515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
8613f7cac41SRalf Baechle 			*fault_addr = dva;
862515b029dSDavid Daney 			return SIGSEGV;
863515b029dSDavid Daney 		}
8641da177e4SLinus Torvalds 		break;
8651da177e4SLinus Torvalds 
8663f7cac41SRalf Baechle 	case lwc1_op:
8673f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
8681da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
869b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
8703f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
871b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
8723f7cac41SRalf Baechle 			*fault_addr = wva;
8731da177e4SLinus Torvalds 			return SIGBUS;
8741da177e4SLinus Torvalds 		}
8753f7cac41SRalf Baechle 		if (__get_user(wval, wva)) {
876515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
8773f7cac41SRalf Baechle 			*fault_addr = wva;
878515b029dSDavid Daney 			return SIGSEGV;
879515b029dSDavid Daney 		}
8803f7cac41SRalf Baechle 		SITOREG(wval, MIPSInst_RT(ir));
8811da177e4SLinus Torvalds 		break;
8821da177e4SLinus Torvalds 
8833f7cac41SRalf Baechle 	case swc1_op:
8843f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
8851da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
886b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
8873f7cac41SRalf Baechle 		SIFROMREG(wval, MIPSInst_RT(ir));
8883f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
889b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
8903f7cac41SRalf Baechle 			*fault_addr = wva;
8911da177e4SLinus Torvalds 			return SIGBUS;
8921da177e4SLinus Torvalds 		}
8933f7cac41SRalf Baechle 		if (__put_user(wval, wva)) {
894515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
8953f7cac41SRalf Baechle 			*fault_addr = wva;
896515b029dSDavid Daney 			return SIGSEGV;
897515b029dSDavid Daney 		}
8981da177e4SLinus Torvalds 		break;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 	case cop1_op:
9011da177e4SLinus Torvalds 		switch (MIPSInst_RS(ir)) {
9021da177e4SLinus Torvalds 		case dmfc_op:
90308a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
90408a07904SRalf Baechle 				return SIGILL;
90508a07904SRalf Baechle 
9061da177e4SLinus Torvalds 			/* copregister fs -> gpr[rt] */
9071da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
9081da177e4SLinus Torvalds 				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
9091da177e4SLinus Torvalds 					MIPSInst_RD(ir));
9101da177e4SLinus Torvalds 			}
9111da177e4SLinus Torvalds 			break;
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 		case dmtc_op:
91408a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
91508a07904SRalf Baechle 				return SIGILL;
91608a07904SRalf Baechle 
9171da177e4SLinus Torvalds 			/* copregister fs <- rt */
9181da177e4SLinus Torvalds 			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
9191da177e4SLinus Torvalds 			break;
9201da177e4SLinus Torvalds 
9211ac94400SLeonid Yegoshin 		case mfhc_op:
9221ac94400SLeonid Yegoshin 			if (!cpu_has_mips_r2)
9231ac94400SLeonid Yegoshin 				goto sigill;
9241ac94400SLeonid Yegoshin 
9251ac94400SLeonid Yegoshin 			/* copregister rd -> gpr[rt] */
9261ac94400SLeonid Yegoshin 			if (MIPSInst_RT(ir) != 0) {
9271ac94400SLeonid Yegoshin 				SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
9281ac94400SLeonid Yegoshin 					MIPSInst_RD(ir));
9291ac94400SLeonid Yegoshin 			}
9301ac94400SLeonid Yegoshin 			break;
9311ac94400SLeonid Yegoshin 
9321ac94400SLeonid Yegoshin 		case mthc_op:
9331ac94400SLeonid Yegoshin 			if (!cpu_has_mips_r2)
9341ac94400SLeonid Yegoshin 				goto sigill;
9351ac94400SLeonid Yegoshin 
9361ac94400SLeonid Yegoshin 			/* copregister rd <- gpr[rt] */
9371ac94400SLeonid Yegoshin 			SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
9381ac94400SLeonid Yegoshin 			break;
9391ac94400SLeonid Yegoshin 
9401da177e4SLinus Torvalds 		case mfc_op:
9411da177e4SLinus Torvalds 			/* copregister rd -> gpr[rt] */
9421da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
9431da177e4SLinus Torvalds 				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
9441da177e4SLinus Torvalds 					MIPSInst_RD(ir));
9451da177e4SLinus Torvalds 			}
9461da177e4SLinus Torvalds 			break;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 		case mtc_op:
9491da177e4SLinus Torvalds 			/* copregister rd <- rt */
9501da177e4SLinus Torvalds 			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
9511da177e4SLinus Torvalds 			break;
9521da177e4SLinus Torvalds 
9533f7cac41SRalf Baechle 		case cfc_op:
9541da177e4SLinus Torvalds 			/* cop control register rd -> gpr[rt] */
9551da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) == FPCREG_CSR) {
9561da177e4SLinus Torvalds 				value = ctx->fcr31;
95756a64733SRalf Baechle 				value = (value & ~FPU_CSR_RM) | modeindex(value);
95892df0f8bSRalf Baechle 				pr_debug("%p gpr[%d]<-csr=%08x\n",
959333d1f67SRalf Baechle 					 (void *) (xcp->cp0_epc),
9601da177e4SLinus Torvalds 					 MIPSInst_RT(ir), value);
9611da177e4SLinus Torvalds 			}
9621da177e4SLinus Torvalds 			else if (MIPSInst_RD(ir) == FPCREG_RID)
9631da177e4SLinus Torvalds 				value = 0;
9641da177e4SLinus Torvalds 			else
9651da177e4SLinus Torvalds 				value = 0;
9661da177e4SLinus Torvalds 			if (MIPSInst_RT(ir))
9671da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RT(ir)] = value;
9681da177e4SLinus Torvalds 			break;
9691da177e4SLinus Torvalds 
9703f7cac41SRalf Baechle 		case ctc_op:
9711da177e4SLinus Torvalds 			/* copregister rd <- rt */
9721da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) == 0)
9731da177e4SLinus Torvalds 				value = 0;
9741da177e4SLinus Torvalds 			else
9751da177e4SLinus Torvalds 				value = xcp->regs[MIPSInst_RT(ir)];
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 			/* we only have one writable control reg
9781da177e4SLinus Torvalds 			 */
9791da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) == FPCREG_CSR) {
98092df0f8bSRalf Baechle 				pr_debug("%p gpr[%d]->csr=%08x\n",
981333d1f67SRalf Baechle 					 (void *) (xcp->cp0_epc),
9821da177e4SLinus Torvalds 					 MIPSInst_RT(ir), value);
98395e8f634SShane McDonald 
98495e8f634SShane McDonald 				/*
98595e8f634SShane McDonald 				 * Don't write reserved bits,
98695e8f634SShane McDonald 				 * and convert to ieee library modes
98795e8f634SShane McDonald 				 */
98856a64733SRalf Baechle 				ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
98956a64733SRalf Baechle 					     modeindex(value);
9901da177e4SLinus Torvalds 			}
9911da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
9921da177e4SLinus Torvalds 				return SIGFPE;
9931da177e4SLinus Torvalds 			}
9941da177e4SLinus Torvalds 			break;
9951da177e4SLinus Torvalds 
9963f7cac41SRalf Baechle 		case bc_op:
997e7e9cae5SRalf Baechle 			if (delay_slot(xcp))
9981da177e4SLinus Torvalds 				return SIGILL;
9991da177e4SLinus Torvalds 
100008a07904SRalf Baechle 			if (cpu_has_mips_4_5_r)
100108a07904SRalf Baechle 				cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
100208a07904SRalf Baechle 			else
100308a07904SRalf Baechle 				cbit = FPU_CSR_COND;
100408a07904SRalf Baechle 			cond = ctx->fcr31 & cbit;
100508a07904SRalf Baechle 
10063f7cac41SRalf Baechle 			likely = 0;
10071da177e4SLinus Torvalds 			switch (MIPSInst_RT(ir) & 3) {
10081da177e4SLinus Torvalds 			case bcfl_op:
10091da177e4SLinus Torvalds 				likely = 1;
10101da177e4SLinus Torvalds 			case bcf_op:
10111da177e4SLinus Torvalds 				cond = !cond;
10121da177e4SLinus Torvalds 				break;
10131da177e4SLinus Torvalds 			case bctl_op:
10141da177e4SLinus Torvalds 				likely = 1;
10151da177e4SLinus Torvalds 			case bct_op:
10161da177e4SLinus Torvalds 				break;
10171da177e4SLinus Torvalds 			default:
10181da177e4SLinus Torvalds 				/* thats an illegal instruction */
10191da177e4SLinus Torvalds 				return SIGILL;
10201da177e4SLinus Torvalds 			}
10211da177e4SLinus Torvalds 
1022e7e9cae5SRalf Baechle 			set_delay_slot(xcp);
10231da177e4SLinus Torvalds 			if (cond) {
10243f7cac41SRalf Baechle 				/*
10253f7cac41SRalf Baechle 				 * Branch taken: emulate dslot instruction
10261da177e4SLinus Torvalds 				 */
1027102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
10281da177e4SLinus Torvalds 
1029102cedc3SLeonid Yegoshin 				contpc = MIPSInst_SIMM(ir);
1030102cedc3SLeonid Yegoshin 				ir = dec_insn.next_insn;
1031102cedc3SLeonid Yegoshin 				if (dec_insn.micro_mips_mode) {
1032102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 1));
1033102cedc3SLeonid Yegoshin 
1034102cedc3SLeonid Yegoshin 					/* If 16-bit instruction, not FPU. */
1035102cedc3SLeonid Yegoshin 					if ((dec_insn.next_pc_inc == 2) ||
1036102cedc3SLeonid Yegoshin 						(microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1037102cedc3SLeonid Yegoshin 
1038102cedc3SLeonid Yegoshin 						/*
1039102cedc3SLeonid Yegoshin 						 * Since this instruction will
1040102cedc3SLeonid Yegoshin 						 * be put on the stack with
1041102cedc3SLeonid Yegoshin 						 * 32-bit words, get around
1042102cedc3SLeonid Yegoshin 						 * this problem by putting a
1043102cedc3SLeonid Yegoshin 						 * NOP16 as the second one.
1044102cedc3SLeonid Yegoshin 						 */
1045102cedc3SLeonid Yegoshin 						if (dec_insn.next_pc_inc == 2)
1046102cedc3SLeonid Yegoshin 							ir = (ir & (~0xffff)) | MM_NOP16;
1047102cedc3SLeonid Yegoshin 
1048102cedc3SLeonid Yegoshin 						/*
1049102cedc3SLeonid Yegoshin 						 * Single step the non-CP1
1050102cedc3SLeonid Yegoshin 						 * instruction in the dslot.
1051102cedc3SLeonid Yegoshin 						 */
1052102cedc3SLeonid Yegoshin 						return mips_dsemul(xcp, ir, contpc);
1053515b029dSDavid Daney 					}
1054102cedc3SLeonid Yegoshin 				} else
1055102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 2));
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 				switch (MIPSInst_OPCODE(ir)) {
10581da177e4SLinus Torvalds 				case lwc1_op:
105908a07904SRalf Baechle 					goto emul;
10603f7cac41SRalf Baechle 
10611da177e4SLinus Torvalds 				case swc1_op:
106208a07904SRalf Baechle 					goto emul;
10633f7cac41SRalf Baechle 
10641da177e4SLinus Torvalds 				case ldc1_op:
10651da177e4SLinus Torvalds 				case sdc1_op:
106608a07904SRalf Baechle 					if (cpu_has_mips_2_3_4_5 ||
106708a07904SRalf Baechle 					    cpu_has_mips64)
106808a07904SRalf Baechle 						goto emul;
106908a07904SRalf Baechle 
107008a07904SRalf Baechle 					return SIGILL;
107108a07904SRalf Baechle 					goto emul;
10723f7cac41SRalf Baechle 
10731da177e4SLinus Torvalds 				case cop1_op:
107408a07904SRalf Baechle 					goto emul;
10753f7cac41SRalf Baechle 
10761da177e4SLinus Torvalds 				case cop1x_op:
1077a5466d7bSMarkos Chandras 					if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
10781da177e4SLinus Torvalds 						/* its one of ours */
10791da177e4SLinus Torvalds 						goto emul;
108008a07904SRalf Baechle 
108108a07904SRalf Baechle 					return SIGILL;
10823f7cac41SRalf Baechle 
10831da177e4SLinus Torvalds 				case spec_op:
108408a07904SRalf Baechle 					if (!cpu_has_mips_4_5_r)
108508a07904SRalf Baechle 						return SIGILL;
108608a07904SRalf Baechle 
10871da177e4SLinus Torvalds 					if (MIPSInst_FUNC(ir) == movc_op)
10881da177e4SLinus Torvalds 						goto emul;
10891da177e4SLinus Torvalds 					break;
10901da177e4SLinus Torvalds 				}
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 				/*
10931da177e4SLinus Torvalds 				 * Single step the non-cp1
10941da177e4SLinus Torvalds 				 * instruction in the dslot
10951da177e4SLinus Torvalds 				 */
1096e70dfc10SAtsushi Nemoto 				return mips_dsemul(xcp, ir, contpc);
10973f7cac41SRalf Baechle 			} else if (likely) {	/* branch not taken */
10981da177e4SLinus Torvalds 					/*
10991da177e4SLinus Torvalds 					 * branch likely nullifies
11001da177e4SLinus Torvalds 					 * dslot if not taken
11011da177e4SLinus Torvalds 					 */
1102102cedc3SLeonid Yegoshin 					xcp->cp0_epc += dec_insn.pc_inc;
1103102cedc3SLeonid Yegoshin 					contpc += dec_insn.pc_inc;
11041da177e4SLinus Torvalds 					/*
11051da177e4SLinus Torvalds 					 * else continue & execute
11061da177e4SLinus Torvalds 					 * dslot as normal insn
11071da177e4SLinus Torvalds 					 */
11081da177e4SLinus Torvalds 				}
11091da177e4SLinus Torvalds 			break;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 		default:
11121da177e4SLinus Torvalds 			if (!(MIPSInst_RS(ir) & 0x10))
11131da177e4SLinus Torvalds 				return SIGILL;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 			/* a real fpu computation instruction */
11161da177e4SLinus Torvalds 			if ((sig = fpu_emu(xcp, ctx, ir)))
11171da177e4SLinus Torvalds 				return sig;
11181da177e4SLinus Torvalds 		}
11191da177e4SLinus Torvalds 		break;
11201da177e4SLinus Torvalds 
11213f7cac41SRalf Baechle 	case cop1x_op:
1122a5466d7bSMarkos Chandras 		if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
112308a07904SRalf Baechle 			return SIGILL;
112408a07904SRalf Baechle 
112508a07904SRalf Baechle 		sig = fpux_emu(xcp, ctx, ir, fault_addr);
1126515b029dSDavid Daney 		if (sig)
11271da177e4SLinus Torvalds 			return sig;
11281da177e4SLinus Torvalds 		break;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	case spec_op:
113108a07904SRalf Baechle 		if (!cpu_has_mips_4_5_r)
113208a07904SRalf Baechle 			return SIGILL;
113308a07904SRalf Baechle 
11341da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != movc_op)
11351da177e4SLinus Torvalds 			return SIGILL;
11361da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
11371da177e4SLinus Torvalds 		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
11381da177e4SLinus Torvalds 			xcp->regs[MIPSInst_RD(ir)] =
11391da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RS(ir)];
11401da177e4SLinus Torvalds 		break;
11411da177e4SLinus Torvalds 	default:
11421ac94400SLeonid Yegoshin sigill:
11431da177e4SLinus Torvalds 		return SIGILL;
11441da177e4SLinus Torvalds 	}
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	/* we did it !! */
1147e70dfc10SAtsushi Nemoto 	xcp->cp0_epc = contpc;
1148e7e9cae5SRalf Baechle 	clear_delay_slot(xcp);
1149333d1f67SRalf Baechle 
11501da177e4SLinus Torvalds 	return 0;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds /*
11541da177e4SLinus Torvalds  * Conversion table from MIPS compare ops 48-63
11551da177e4SLinus Torvalds  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
11561da177e4SLinus Torvalds  */
11571da177e4SLinus Torvalds static const unsigned char cmptab[8] = {
11581da177e4SLinus Torvalds 	0,			/* cmp_0 (sig) cmp_sf */
11591da177e4SLinus Torvalds 	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
11601da177e4SLinus Torvalds 	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
11611da177e4SLinus Torvalds 	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
11621da177e4SLinus Torvalds 	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
11631da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
11641da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
11651da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
11661da177e4SLinus Torvalds };
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds /*
11701da177e4SLinus Torvalds  * Additional MIPS4 instructions
11711da177e4SLinus Torvalds  */
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3)					\
117447fa0c02SRalf Baechle static union ieee754##p fpemu_##p##_##name(union ieee754##p r,		\
117547fa0c02SRalf Baechle 	union ieee754##p s, union ieee754##p t)				\
11761da177e4SLinus Torvalds {									\
1177cd21dfcfSRalf Baechle 	struct _ieee754_csr ieee754_csr_save;				\
11781da177e4SLinus Torvalds 	s = f1(s, t);							\
11791da177e4SLinus Torvalds 	ieee754_csr_save = ieee754_csr;					\
11801da177e4SLinus Torvalds 	s = f2(s, r);							\
11811da177e4SLinus Torvalds 	ieee754_csr_save.cx |= ieee754_csr.cx;				\
11821da177e4SLinus Torvalds 	ieee754_csr_save.sx |= ieee754_csr.sx;				\
11831da177e4SLinus Torvalds 	s = f3(s);							\
11841da177e4SLinus Torvalds 	ieee754_csr.cx |= ieee754_csr_save.cx;				\
11851da177e4SLinus Torvalds 	ieee754_csr.sx |= ieee754_csr_save.sx;				\
11861da177e4SLinus Torvalds 	return s;							\
11871da177e4SLinus Torvalds }
11881da177e4SLinus Torvalds 
11892209bcb1SRalf Baechle static union ieee754dp fpemu_dp_recip(union ieee754dp d)
11901da177e4SLinus Torvalds {
11911da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), d);
11921da177e4SLinus Torvalds }
11931da177e4SLinus Torvalds 
11942209bcb1SRalf Baechle static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
11951da177e4SLinus Torvalds {
11961da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
11971da177e4SLinus Torvalds }
11981da177e4SLinus Torvalds 
11992209bcb1SRalf Baechle static union ieee754sp fpemu_sp_recip(union ieee754sp s)
12001da177e4SLinus Torvalds {
12011da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), s);
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
12042209bcb1SRalf Baechle static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
12101da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
12111da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
12121da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
12131da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
12141da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
12151da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
12161da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
12171da177e4SLinus Torvalds 
1218eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1219515b029dSDavid Daney 	mips_instruction ir, void *__user *fault_addr)
12201da177e4SLinus Torvalds {
12211da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
12221da177e4SLinus Torvalds 
1223b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1xops);
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	switch (MIPSInst_FMA_FFMT(ir)) {
12261da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
12271da177e4SLinus Torvalds 
12282209bcb1SRalf Baechle 		union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
12292209bcb1SRalf Baechle 		union ieee754sp fd, fr, fs, ft;
12303fccc015SRalf Baechle 		u32 __user *va;
12311da177e4SLinus Torvalds 		u32 val;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
12341da177e4SLinus Torvalds 		case lwxc1_op:
12353fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
12361da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
12371da177e4SLinus Torvalds 
1238b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1239515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1240b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1241515b029dSDavid Daney 				*fault_addr = va;
12421da177e4SLinus Torvalds 				return SIGBUS;
12431da177e4SLinus Torvalds 			}
1244515b029dSDavid Daney 			if (__get_user(val, va)) {
1245515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1246515b029dSDavid Daney 				*fault_addr = va;
1247515b029dSDavid Daney 				return SIGSEGV;
1248515b029dSDavid Daney 			}
12491da177e4SLinus Torvalds 			SITOREG(val, MIPSInst_FD(ir));
12501da177e4SLinus Torvalds 			break;
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 		case swxc1_op:
12533fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
12541da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
12551da177e4SLinus Torvalds 
1256b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 			SIFROMREG(val, MIPSInst_FS(ir));
1259515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1260515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1261515b029dSDavid Daney 				*fault_addr = va;
1262515b029dSDavid Daney 				return SIGBUS;
1263515b029dSDavid Daney 			}
12641da177e4SLinus Torvalds 			if (put_user(val, va)) {
1265b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1266515b029dSDavid Daney 				*fault_addr = va;
1267515b029dSDavid Daney 				return SIGSEGV;
12681da177e4SLinus Torvalds 			}
12691da177e4SLinus Torvalds 			break;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 		case madd_s_op:
12721da177e4SLinus Torvalds 			handler = fpemu_sp_madd;
12731da177e4SLinus Torvalds 			goto scoptop;
12741da177e4SLinus Torvalds 		case msub_s_op:
12751da177e4SLinus Torvalds 			handler = fpemu_sp_msub;
12761da177e4SLinus Torvalds 			goto scoptop;
12771da177e4SLinus Torvalds 		case nmadd_s_op:
12781da177e4SLinus Torvalds 			handler = fpemu_sp_nmadd;
12791da177e4SLinus Torvalds 			goto scoptop;
12801da177e4SLinus Torvalds 		case nmsub_s_op:
12811da177e4SLinus Torvalds 			handler = fpemu_sp_nmsub;
12821da177e4SLinus Torvalds 			goto scoptop;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 		      scoptop:
12851da177e4SLinus Torvalds 			SPFROMREG(fr, MIPSInst_FR(ir));
12861da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
12871da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
12881da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
12891da177e4SLinus Torvalds 			SPTOREG(fd, MIPSInst_FD(ir));
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 		      copcsr:
1292c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1293c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
12941da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1295c4103526SDeng-Cheng Zhu 			}
1296c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1297c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
12981da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1299c4103526SDeng-Cheng Zhu 			}
1300c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1301c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
13021da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1303c4103526SDeng-Cheng Zhu 			}
1304c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1305c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
13061da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1307c4103526SDeng-Cheng Zhu 			}
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
13101da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
13113f7cac41SRalf Baechle 				/*printk ("SIGFPE: FPU csr = %08x\n",
13121da177e4SLinus Torvalds 				   ctx->fcr31); */
13131da177e4SLinus Torvalds 				return SIGFPE;
13141da177e4SLinus Torvalds 			}
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds 			break;
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 		default:
13191da177e4SLinus Torvalds 			return SIGILL;
13201da177e4SLinus Torvalds 		}
13211da177e4SLinus Torvalds 		break;
13221da177e4SLinus Torvalds 	}
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	case d_fmt:{		/* 1 */
13252209bcb1SRalf Baechle 		union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
13262209bcb1SRalf Baechle 		union ieee754dp fd, fr, fs, ft;
13273fccc015SRalf Baechle 		u64 __user *va;
13281da177e4SLinus Torvalds 		u64 val;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
13311da177e4SLinus Torvalds 		case ldxc1_op:
13323fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
13331da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
13341da177e4SLinus Torvalds 
1335b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1336515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1337b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1338515b029dSDavid Daney 				*fault_addr = va;
13391da177e4SLinus Torvalds 				return SIGBUS;
13401da177e4SLinus Torvalds 			}
1341515b029dSDavid Daney 			if (__get_user(val, va)) {
1342515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1343515b029dSDavid Daney 				*fault_addr = va;
1344515b029dSDavid Daney 				return SIGSEGV;
1345515b029dSDavid Daney 			}
13461da177e4SLinus Torvalds 			DITOREG(val, MIPSInst_FD(ir));
13471da177e4SLinus Torvalds 			break;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 		case sdxc1_op:
13503fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
13511da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
13521da177e4SLinus Torvalds 
1353b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
13541da177e4SLinus Torvalds 			DIFROMREG(val, MIPSInst_FS(ir));
1355515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1356b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1357515b029dSDavid Daney 				*fault_addr = va;
13581da177e4SLinus Torvalds 				return SIGBUS;
13591da177e4SLinus Torvalds 			}
1360515b029dSDavid Daney 			if (__put_user(val, va)) {
1361515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1362515b029dSDavid Daney 				*fault_addr = va;
1363515b029dSDavid Daney 				return SIGSEGV;
1364515b029dSDavid Daney 			}
13651da177e4SLinus Torvalds 			break;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 		case madd_d_op:
13681da177e4SLinus Torvalds 			handler = fpemu_dp_madd;
13691da177e4SLinus Torvalds 			goto dcoptop;
13701da177e4SLinus Torvalds 		case msub_d_op:
13711da177e4SLinus Torvalds 			handler = fpemu_dp_msub;
13721da177e4SLinus Torvalds 			goto dcoptop;
13731da177e4SLinus Torvalds 		case nmadd_d_op:
13741da177e4SLinus Torvalds 			handler = fpemu_dp_nmadd;
13751da177e4SLinus Torvalds 			goto dcoptop;
13761da177e4SLinus Torvalds 		case nmsub_d_op:
13771da177e4SLinus Torvalds 			handler = fpemu_dp_nmsub;
13781da177e4SLinus Torvalds 			goto dcoptop;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 		      dcoptop:
13811da177e4SLinus Torvalds 			DPFROMREG(fr, MIPSInst_FR(ir));
13821da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
13831da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
13841da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
13851da177e4SLinus Torvalds 			DPTOREG(fd, MIPSInst_FD(ir));
13861da177e4SLinus Torvalds 			goto copcsr;
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 		default:
13891da177e4SLinus Torvalds 			return SIGILL;
13901da177e4SLinus Torvalds 		}
13911da177e4SLinus Torvalds 		break;
13921da177e4SLinus Torvalds 	}
13931da177e4SLinus Torvalds 
139451061b88SDeng-Cheng Zhu 	case 0x3:
139551061b88SDeng-Cheng Zhu 		if (MIPSInst_FUNC(ir) != pfetch_op)
13961da177e4SLinus Torvalds 			return SIGILL;
139751061b88SDeng-Cheng Zhu 
13981da177e4SLinus Torvalds 		/* ignore prefx operation */
13991da177e4SLinus Torvalds 		break;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	default:
14021da177e4SLinus Torvalds 		return SIGILL;
14031da177e4SLinus Torvalds 	}
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	return 0;
14061da177e4SLinus Torvalds }
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds /*
14111da177e4SLinus Torvalds  * Emulate a single COP1 arithmetic instruction.
14121da177e4SLinus Torvalds  */
1413eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
14141da177e4SLinus Torvalds 	mips_instruction ir)
14151da177e4SLinus Torvalds {
14161da177e4SLinus Torvalds 	int rfmt;		/* resulting format */
14171da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
14183f7cac41SRalf Baechle 	unsigned int oldrm;
14193f7cac41SRalf Baechle 	unsigned int cbit;
14201da177e4SLinus Torvalds 	unsigned cond;
14211da177e4SLinus Torvalds 	union {
14222209bcb1SRalf Baechle 		union ieee754dp d;
14232209bcb1SRalf Baechle 		union ieee754sp s;
14241da177e4SLinus Torvalds 		int w;
14251da177e4SLinus Torvalds 		s64 l;
14261da177e4SLinus Torvalds 	} rv;			/* resulting value */
14273f7cac41SRalf Baechle 	u64 bits;
14281da177e4SLinus Torvalds 
1429b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1ops);
14301da177e4SLinus Torvalds 	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
14311da177e4SLinus Torvalds 	case s_fmt: {		/* 0 */
14321da177e4SLinus Torvalds 		union {
14332209bcb1SRalf Baechle 			union ieee754sp(*b) (union ieee754sp, union ieee754sp);
14342209bcb1SRalf Baechle 			union ieee754sp(*u) (union ieee754sp);
14351da177e4SLinus Torvalds 		} handler;
14363f7cac41SRalf Baechle 		union ieee754sp fs, ft;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
14391da177e4SLinus Torvalds 			/* binary ops */
14401da177e4SLinus Torvalds 		case fadd_op:
14411da177e4SLinus Torvalds 			handler.b = ieee754sp_add;
14421da177e4SLinus Torvalds 			goto scopbop;
14431da177e4SLinus Torvalds 		case fsub_op:
14441da177e4SLinus Torvalds 			handler.b = ieee754sp_sub;
14451da177e4SLinus Torvalds 			goto scopbop;
14461da177e4SLinus Torvalds 		case fmul_op:
14471da177e4SLinus Torvalds 			handler.b = ieee754sp_mul;
14481da177e4SLinus Torvalds 			goto scopbop;
14491da177e4SLinus Torvalds 		case fdiv_op:
14501da177e4SLinus Torvalds 			handler.b = ieee754sp_div;
14511da177e4SLinus Torvalds 			goto scopbop;
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 			/* unary  ops */
14541da177e4SLinus Torvalds 		case fsqrt_op:
145508a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
145608a07904SRalf Baechle 				return SIGILL;
145708a07904SRalf Baechle 
14581da177e4SLinus Torvalds 			handler.u = ieee754sp_sqrt;
14591da177e4SLinus Torvalds 			goto scopuop;
14603f7cac41SRalf Baechle 
146108a07904SRalf Baechle 		/*
146208a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
146308a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
146408a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
146508a07904SRalf Baechle 		 */
14661da177e4SLinus Torvalds 		case frsqrt_op:
146708a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r2)
146808a07904SRalf Baechle 				return SIGILL;
146908a07904SRalf Baechle 
14701da177e4SLinus Torvalds 			handler.u = fpemu_sp_rsqrt;
14711da177e4SLinus Torvalds 			goto scopuop;
14723f7cac41SRalf Baechle 
14731da177e4SLinus Torvalds 		case frecip_op:
147408a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r2)
147508a07904SRalf Baechle 				return SIGILL;
147608a07904SRalf Baechle 
14771da177e4SLinus Torvalds 			handler.u = fpemu_sp_recip;
14781da177e4SLinus Torvalds 			goto scopuop;
147908a07904SRalf Baechle 
14801da177e4SLinus Torvalds 		case fmovc_op:
148108a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
148208a07904SRalf Baechle 				return SIGILL;
148308a07904SRalf Baechle 
14841da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
14851da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
14861da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
14871da177e4SLinus Torvalds 				return 0;
14881da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
14891da177e4SLinus Torvalds 			break;
14903f7cac41SRalf Baechle 
14911da177e4SLinus Torvalds 		case fmovz_op:
149208a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
149308a07904SRalf Baechle 				return SIGILL;
149408a07904SRalf Baechle 
14951da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
14961da177e4SLinus Torvalds 				return 0;
14971da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
14981da177e4SLinus Torvalds 			break;
14993f7cac41SRalf Baechle 
15001da177e4SLinus Torvalds 		case fmovn_op:
150108a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
150208a07904SRalf Baechle 				return SIGILL;
150308a07904SRalf Baechle 
15041da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
15051da177e4SLinus Torvalds 				return 0;
15061da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
15071da177e4SLinus Torvalds 			break;
15083f7cac41SRalf Baechle 
15091da177e4SLinus Torvalds 		case fabs_op:
15101da177e4SLinus Torvalds 			handler.u = ieee754sp_abs;
15111da177e4SLinus Torvalds 			goto scopuop;
15123f7cac41SRalf Baechle 
15131da177e4SLinus Torvalds 		case fneg_op:
15141da177e4SLinus Torvalds 			handler.u = ieee754sp_neg;
15151da177e4SLinus Torvalds 			goto scopuop;
15163f7cac41SRalf Baechle 
15171da177e4SLinus Torvalds 		case fmov_op:
15181da177e4SLinus Torvalds 			/* an easy one */
15191da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
15201da177e4SLinus Torvalds 			goto copcsr;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 			/* binary op on handler */
15231da177e4SLinus Torvalds scopbop:
15241da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
15251da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 			rv.s = (*handler.b) (fs, ft);
15281da177e4SLinus Torvalds 			goto copcsr;
15291da177e4SLinus Torvalds scopuop:
15301da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
15311da177e4SLinus Torvalds 			rv.s = (*handler.u) (fs);
15321da177e4SLinus Torvalds 			goto copcsr;
15331da177e4SLinus Torvalds copcsr:
1534c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1535c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
15361da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1537c4103526SDeng-Cheng Zhu 			}
1538c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1539c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
15401da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1541c4103526SDeng-Cheng Zhu 			}
1542c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1543c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
15441da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1545c4103526SDeng-Cheng Zhu 			}
1546c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1547c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
15481da177e4SLinus Torvalds 				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1549c4103526SDeng-Cheng Zhu 			}
1550c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1551c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
15521da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1553c4103526SDeng-Cheng Zhu 			}
15541da177e4SLinus Torvalds 			break;
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 			/* unary conv ops */
15571da177e4SLinus Torvalds 		case fcvts_op:
15581da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
15591da177e4SLinus Torvalds 
15603f7cac41SRalf Baechle 		case fcvtd_op:
15611da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
15621da177e4SLinus Torvalds 			rv.d = ieee754dp_fsp(fs);
15631da177e4SLinus Torvalds 			rfmt = d_fmt;
15641da177e4SLinus Torvalds 			goto copcsr;
15651da177e4SLinus Torvalds 
15663f7cac41SRalf Baechle 		case fcvtw_op:
15671da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
15681da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
15691da177e4SLinus Torvalds 			rfmt = w_fmt;
15701da177e4SLinus Torvalds 			goto copcsr;
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 		case fround_op:
15731da177e4SLinus Torvalds 		case ftrunc_op:
15741da177e4SLinus Torvalds 		case fceil_op:
15753f7cac41SRalf Baechle 		case ffloor_op:
157608a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
157708a07904SRalf Baechle 				return SIGILL;
157808a07904SRalf Baechle 
15793f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
15801da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
158156a64733SRalf Baechle 			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
15821da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
15831da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
15841da177e4SLinus Torvalds 			rfmt = w_fmt;
15851da177e4SLinus Torvalds 			goto copcsr;
15861da177e4SLinus Torvalds 
15873f7cac41SRalf Baechle 		case fcvtl_op:
158808a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
158908a07904SRalf Baechle 				return SIGILL;
159008a07904SRalf Baechle 
15911da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
15921da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
15931da177e4SLinus Torvalds 			rfmt = l_fmt;
15941da177e4SLinus Torvalds 			goto copcsr;
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 		case froundl_op:
15971da177e4SLinus Torvalds 		case ftruncl_op:
15981da177e4SLinus Torvalds 		case fceill_op:
15993f7cac41SRalf Baechle 		case ffloorl_op:
160008a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
160108a07904SRalf Baechle 				return SIGILL;
160208a07904SRalf Baechle 
16033f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
16041da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
160556a64733SRalf Baechle 			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
16061da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
16071da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
16081da177e4SLinus Torvalds 			rfmt = l_fmt;
16091da177e4SLinus Torvalds 			goto copcsr;
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 		default:
16121da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
16131da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
16142209bcb1SRalf Baechle 				union ieee754sp fs, ft;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
16171da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
16181da177e4SLinus Torvalds 				rv.w = ieee754sp_cmp(fs, ft,
16191da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
16201da177e4SLinus Torvalds 				rfmt = -1;
16211da177e4SLinus Torvalds 				if ((cmpop & 0x8) && ieee754_cxtest
16221da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
16231da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
16241da177e4SLinus Torvalds 				else
16251da177e4SLinus Torvalds 					goto copcsr;
16261da177e4SLinus Torvalds 
16273f7cac41SRalf Baechle 			} else
16281da177e4SLinus Torvalds 				return SIGILL;
16291da177e4SLinus Torvalds 			break;
16301da177e4SLinus Torvalds 		}
16311da177e4SLinus Torvalds 		break;
16321da177e4SLinus Torvalds 	}
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 	case d_fmt: {
16353f7cac41SRalf Baechle 		union ieee754dp fs, ft;
16361da177e4SLinus Torvalds 		union {
16372209bcb1SRalf Baechle 			union ieee754dp(*b) (union ieee754dp, union ieee754dp);
16382209bcb1SRalf Baechle 			union ieee754dp(*u) (union ieee754dp);
16391da177e4SLinus Torvalds 		} handler;
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
16421da177e4SLinus Torvalds 			/* binary ops */
16431da177e4SLinus Torvalds 		case fadd_op:
16441da177e4SLinus Torvalds 			handler.b = ieee754dp_add;
16451da177e4SLinus Torvalds 			goto dcopbop;
16461da177e4SLinus Torvalds 		case fsub_op:
16471da177e4SLinus Torvalds 			handler.b = ieee754dp_sub;
16481da177e4SLinus Torvalds 			goto dcopbop;
16491da177e4SLinus Torvalds 		case fmul_op:
16501da177e4SLinus Torvalds 			handler.b = ieee754dp_mul;
16511da177e4SLinus Torvalds 			goto dcopbop;
16521da177e4SLinus Torvalds 		case fdiv_op:
16531da177e4SLinus Torvalds 			handler.b = ieee754dp_div;
16541da177e4SLinus Torvalds 			goto dcopbop;
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 			/* unary  ops */
16571da177e4SLinus Torvalds 		case fsqrt_op:
165808a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
165908a07904SRalf Baechle 				return SIGILL;
166008a07904SRalf Baechle 
16611da177e4SLinus Torvalds 			handler.u = ieee754dp_sqrt;
16621da177e4SLinus Torvalds 			goto dcopuop;
166308a07904SRalf Baechle 		/*
166408a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
166508a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
166608a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
166708a07904SRalf Baechle 		 */
16681da177e4SLinus Torvalds 		case frsqrt_op:
166908a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r2)
167008a07904SRalf Baechle 				return SIGILL;
167108a07904SRalf Baechle 
16721da177e4SLinus Torvalds 			handler.u = fpemu_dp_rsqrt;
16731da177e4SLinus Torvalds 			goto dcopuop;
16741da177e4SLinus Torvalds 		case frecip_op:
167508a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r2)
167608a07904SRalf Baechle 				return SIGILL;
167708a07904SRalf Baechle 
16781da177e4SLinus Torvalds 			handler.u = fpemu_dp_recip;
16791da177e4SLinus Torvalds 			goto dcopuop;
16801da177e4SLinus Torvalds 		case fmovc_op:
168108a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
168208a07904SRalf Baechle 				return SIGILL;
168308a07904SRalf Baechle 
16841da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
16851da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
16861da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
16871da177e4SLinus Torvalds 				return 0;
16881da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
16891da177e4SLinus Torvalds 			break;
16901da177e4SLinus Torvalds 		case fmovz_op:
169108a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
169208a07904SRalf Baechle 				return SIGILL;
169308a07904SRalf Baechle 
16941da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
16951da177e4SLinus Torvalds 				return 0;
16961da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
16971da177e4SLinus Torvalds 			break;
16981da177e4SLinus Torvalds 		case fmovn_op:
169908a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
170008a07904SRalf Baechle 				return SIGILL;
170108a07904SRalf Baechle 
17021da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
17031da177e4SLinus Torvalds 				return 0;
17041da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
17051da177e4SLinus Torvalds 			break;
17061da177e4SLinus Torvalds 		case fabs_op:
17071da177e4SLinus Torvalds 			handler.u = ieee754dp_abs;
17081da177e4SLinus Torvalds 			goto dcopuop;
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds 		case fneg_op:
17111da177e4SLinus Torvalds 			handler.u = ieee754dp_neg;
17121da177e4SLinus Torvalds 			goto dcopuop;
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 		case fmov_op:
17151da177e4SLinus Torvalds 			/* an easy one */
17161da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
17171da177e4SLinus Torvalds 			goto copcsr;
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds 			/* binary op on handler */
17203f7cac41SRalf Baechle dcopbop:
17211da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
17221da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 			rv.d = (*handler.b) (fs, ft);
17251da177e4SLinus Torvalds 			goto copcsr;
17263f7cac41SRalf Baechle dcopuop:
17271da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
17281da177e4SLinus Torvalds 			rv.d = (*handler.u) (fs);
17291da177e4SLinus Torvalds 			goto copcsr;
17301da177e4SLinus Torvalds 
17313f7cac41SRalf Baechle 		/*
17323f7cac41SRalf Baechle 		 * unary conv ops
17333f7cac41SRalf Baechle 		 */
17343f7cac41SRalf Baechle 		case fcvts_op:
17351da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
17361da177e4SLinus Torvalds 			rv.s = ieee754sp_fdp(fs);
17371da177e4SLinus Torvalds 			rfmt = s_fmt;
17381da177e4SLinus Torvalds 			goto copcsr;
17393f7cac41SRalf Baechle 
17401da177e4SLinus Torvalds 		case fcvtd_op:
17411da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
17421da177e4SLinus Torvalds 
17433f7cac41SRalf Baechle 		case fcvtw_op:
17441da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
17451da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);	/* wrong */
17461da177e4SLinus Torvalds 			rfmt = w_fmt;
17471da177e4SLinus Torvalds 			goto copcsr;
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 		case fround_op:
17501da177e4SLinus Torvalds 		case ftrunc_op:
17511da177e4SLinus Torvalds 		case fceil_op:
17523f7cac41SRalf Baechle 		case ffloor_op:
175308a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
175408a07904SRalf Baechle 				return SIGILL;
175508a07904SRalf Baechle 
17563f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
17571da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
175856a64733SRalf Baechle 			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
17591da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);
17601da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
17611da177e4SLinus Torvalds 			rfmt = w_fmt;
17621da177e4SLinus Torvalds 			goto copcsr;
17631da177e4SLinus Torvalds 
17643f7cac41SRalf Baechle 		case fcvtl_op:
176508a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
176608a07904SRalf Baechle 				return SIGILL;
176708a07904SRalf Baechle 
17681da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
17691da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
17701da177e4SLinus Torvalds 			rfmt = l_fmt;
17711da177e4SLinus Torvalds 			goto copcsr;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 		case froundl_op:
17741da177e4SLinus Torvalds 		case ftruncl_op:
17751da177e4SLinus Torvalds 		case fceill_op:
17763f7cac41SRalf Baechle 		case ffloorl_op:
177708a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
177808a07904SRalf Baechle 				return SIGILL;
177908a07904SRalf Baechle 
17803f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
17811da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
178256a64733SRalf Baechle 			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
17831da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
17841da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
17851da177e4SLinus Torvalds 			rfmt = l_fmt;
17861da177e4SLinus Torvalds 			goto copcsr;
17871da177e4SLinus Torvalds 
17881da177e4SLinus Torvalds 		default:
17891da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
17901da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
17912209bcb1SRalf Baechle 				union ieee754dp fs, ft;
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
17941da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
17951da177e4SLinus Torvalds 				rv.w = ieee754dp_cmp(fs, ft,
17961da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
17971da177e4SLinus Torvalds 				rfmt = -1;
17981da177e4SLinus Torvalds 				if ((cmpop & 0x8)
17991da177e4SLinus Torvalds 					&&
18001da177e4SLinus Torvalds 					ieee754_cxtest
18011da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
18021da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
18031da177e4SLinus Torvalds 				else
18041da177e4SLinus Torvalds 					goto copcsr;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds 			}
18071da177e4SLinus Torvalds 			else {
18081da177e4SLinus Torvalds 				return SIGILL;
18091da177e4SLinus Torvalds 			}
18101da177e4SLinus Torvalds 			break;
18111da177e4SLinus Torvalds 		}
18121da177e4SLinus Torvalds 		break;
18131da177e4SLinus Torvalds 
18143f7cac41SRalf Baechle 	case w_fmt:
18151da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
18161da177e4SLinus Torvalds 		case fcvts_op:
18171da177e4SLinus Torvalds 			/* convert word to single precision real */
18181da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
18191da177e4SLinus Torvalds 			rv.s = ieee754sp_fint(fs.bits);
18201da177e4SLinus Torvalds 			rfmt = s_fmt;
18211da177e4SLinus Torvalds 			goto copcsr;
18221da177e4SLinus Torvalds 		case fcvtd_op:
18231da177e4SLinus Torvalds 			/* convert word to double precision real */
18241da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
18251da177e4SLinus Torvalds 			rv.d = ieee754dp_fint(fs.bits);
18261da177e4SLinus Torvalds 			rfmt = d_fmt;
18271da177e4SLinus Torvalds 			goto copcsr;
18281da177e4SLinus Torvalds 		default:
18291da177e4SLinus Torvalds 			return SIGILL;
18301da177e4SLinus Torvalds 		}
18311da177e4SLinus Torvalds 		break;
18321da177e4SLinus Torvalds 	}
18331da177e4SLinus Torvalds 
18343f7cac41SRalf Baechle 	case l_fmt:
183508a07904SRalf Baechle 
183608a07904SRalf Baechle 		if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
183708a07904SRalf Baechle 			return SIGILL;
183808a07904SRalf Baechle 
1839bbd426f5SPaul Burton 		DIFROMREG(bits, MIPSInst_FS(ir));
1840bbd426f5SPaul Burton 
18411da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
18421da177e4SLinus Torvalds 		case fcvts_op:
18431da177e4SLinus Torvalds 			/* convert long to single precision real */
1844bbd426f5SPaul Burton 			rv.s = ieee754sp_flong(bits);
18451da177e4SLinus Torvalds 			rfmt = s_fmt;
18461da177e4SLinus Torvalds 			goto copcsr;
18471da177e4SLinus Torvalds 		case fcvtd_op:
18481da177e4SLinus Torvalds 			/* convert long to double precision real */
1849bbd426f5SPaul Burton 			rv.d = ieee754dp_flong(bits);
18501da177e4SLinus Torvalds 			rfmt = d_fmt;
18511da177e4SLinus Torvalds 			goto copcsr;
18521da177e4SLinus Torvalds 		default:
18531da177e4SLinus Torvalds 			return SIGILL;
18541da177e4SLinus Torvalds 		}
18551da177e4SLinus Torvalds 		break;
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 	default:
18581da177e4SLinus Torvalds 		return SIGILL;
18591da177e4SLinus Torvalds 	}
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	/*
18621da177e4SLinus Torvalds 	 * Update the fpu CSR register for this operation.
18631da177e4SLinus Torvalds 	 * If an exception is required, generate a tidy SIGFPE exception,
18641da177e4SLinus Torvalds 	 * without updating the result register.
18651da177e4SLinus Torvalds 	 * Note: cause exception bits do not accumulate, they are rewritten
18661da177e4SLinus Torvalds 	 * for each op; only the flag/sticky bits accumulate.
18671da177e4SLinus Torvalds 	 */
18681da177e4SLinus Torvalds 	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
18691da177e4SLinus Torvalds 	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
18703f7cac41SRalf Baechle 		/*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
18711da177e4SLinus Torvalds 		return SIGFPE;
18721da177e4SLinus Torvalds 	}
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	/*
18751da177e4SLinus Torvalds 	 * Now we can safely write the result back to the register file.
18761da177e4SLinus Torvalds 	 */
18771da177e4SLinus Torvalds 	switch (rfmt) {
187808a07904SRalf Baechle 	case -1:
187908a07904SRalf Baechle 
188008a07904SRalf Baechle 		if (cpu_has_mips_4_5_r)
1881c3b9b945SRob Kendrick 			cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
18821da177e4SLinus Torvalds 		else
188308a07904SRalf Baechle 			cbit = FPU_CSR_COND;
188408a07904SRalf Baechle 		if (rv.w)
188508a07904SRalf Baechle 			ctx->fcr31 |= cbit;
188608a07904SRalf Baechle 		else
188708a07904SRalf Baechle 			ctx->fcr31 &= ~cbit;
18881da177e4SLinus Torvalds 		break;
188908a07904SRalf Baechle 
18901da177e4SLinus Torvalds 	case d_fmt:
18911da177e4SLinus Torvalds 		DPTOREG(rv.d, MIPSInst_FD(ir));
18921da177e4SLinus Torvalds 		break;
18931da177e4SLinus Torvalds 	case s_fmt:
18941da177e4SLinus Torvalds 		SPTOREG(rv.s, MIPSInst_FD(ir));
18951da177e4SLinus Torvalds 		break;
18961da177e4SLinus Torvalds 	case w_fmt:
18971da177e4SLinus Torvalds 		SITOREG(rv.w, MIPSInst_FD(ir));
18981da177e4SLinus Torvalds 		break;
18991da177e4SLinus Torvalds 	case l_fmt:
190008a07904SRalf Baechle 		if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
190108a07904SRalf Baechle 			return SIGILL;
190208a07904SRalf Baechle 
19031da177e4SLinus Torvalds 		DITOREG(rv.l, MIPSInst_FD(ir));
19041da177e4SLinus Torvalds 		break;
19051da177e4SLinus Torvalds 	default:
19061da177e4SLinus Torvalds 		return SIGILL;
19071da177e4SLinus Torvalds 	}
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 	return 0;
19101da177e4SLinus Torvalds }
19111da177e4SLinus Torvalds 
1912e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1913515b029dSDavid Daney 	int has_fpu, void *__user *fault_addr)
19141da177e4SLinus Torvalds {
1915333d1f67SRalf Baechle 	unsigned long oldepc, prevepc;
1916102cedc3SLeonid Yegoshin 	struct mm_decoded_insn dec_insn;
1917102cedc3SLeonid Yegoshin 	u16 instr[4];
1918102cedc3SLeonid Yegoshin 	u16 *instr_ptr;
19191da177e4SLinus Torvalds 	int sig = 0;
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 	oldepc = xcp->cp0_epc;
19221da177e4SLinus Torvalds 	do {
19231da177e4SLinus Torvalds 		prevepc = xcp->cp0_epc;
19241da177e4SLinus Torvalds 
1925102cedc3SLeonid Yegoshin 		if (get_isa16_mode(prevepc) && cpu_has_mmips) {
1926102cedc3SLeonid Yegoshin 			/*
1927102cedc3SLeonid Yegoshin 			 * Get next 2 microMIPS instructions and convert them
1928102cedc3SLeonid Yegoshin 			 * into 32-bit instructions.
1929102cedc3SLeonid Yegoshin 			 */
1930102cedc3SLeonid Yegoshin 			if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
1931102cedc3SLeonid Yegoshin 			    (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
1932102cedc3SLeonid Yegoshin 			    (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
1933102cedc3SLeonid Yegoshin 			    (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
1934b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
19351da177e4SLinus Torvalds 				return SIGBUS;
19361da177e4SLinus Torvalds 			}
1937102cedc3SLeonid Yegoshin 			instr_ptr = instr;
1938102cedc3SLeonid Yegoshin 
1939102cedc3SLeonid Yegoshin 			/* Get first instruction. */
1940102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
1941102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
1942102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
1943102cedc3SLeonid Yegoshin 					(*instr_ptr);
1944102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
1945102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 2;
1946102cedc3SLeonid Yegoshin 				instr_ptr += 1;
1947102cedc3SLeonid Yegoshin 			} else {
1948102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
1949102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
1950102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
1951102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 4;
1952102cedc3SLeonid Yegoshin 				instr_ptr += 2;
1953515b029dSDavid Daney 			}
1954102cedc3SLeonid Yegoshin 			/* Get second instruction. */
1955102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
1956102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
1957102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
1958102cedc3SLeonid Yegoshin 					(*instr_ptr);
1959102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
1960102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 2;
1961102cedc3SLeonid Yegoshin 			} else {
1962102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
1963102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
1964102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
1965102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 4;
1966102cedc3SLeonid Yegoshin 			}
1967102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 1;
1968102cedc3SLeonid Yegoshin 		} else {
1969102cedc3SLeonid Yegoshin 			if ((get_user(dec_insn.insn,
1970102cedc3SLeonid Yegoshin 			    (mips_instruction __user *) xcp->cp0_epc)) ||
1971102cedc3SLeonid Yegoshin 			    (get_user(dec_insn.next_insn,
1972102cedc3SLeonid Yegoshin 			    (mips_instruction __user *)(xcp->cp0_epc+4)))) {
1973102cedc3SLeonid Yegoshin 				MIPS_FPU_EMU_INC_STATS(errors);
1974102cedc3SLeonid Yegoshin 				return SIGBUS;
1975102cedc3SLeonid Yegoshin 			}
1976102cedc3SLeonid Yegoshin 			dec_insn.pc_inc = 4;
1977102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc = 4;
1978102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 0;
1979102cedc3SLeonid Yegoshin 		}
1980102cedc3SLeonid Yegoshin 
1981102cedc3SLeonid Yegoshin 		if ((dec_insn.insn == 0) ||
1982102cedc3SLeonid Yegoshin 		   ((dec_insn.pc_inc == 2) &&
1983102cedc3SLeonid Yegoshin 		   ((dec_insn.insn & 0xffff) == MM_NOP16)))
1984102cedc3SLeonid Yegoshin 			xcp->cp0_epc += dec_insn.pc_inc;	/* Skip NOPs */
19851da177e4SLinus Torvalds 		else {
1986cd21dfcfSRalf Baechle 			/*
1987cd21dfcfSRalf Baechle 			 * The 'ieee754_csr' is an alias of
1988cd21dfcfSRalf Baechle 			 * ctx->fcr31.	No need to copy ctx->fcr31 to
1989cd21dfcfSRalf Baechle 			 * ieee754_csr.	 But ieee754_csr.rm is ieee
1990cd21dfcfSRalf Baechle 			 * library modes. (not mips rounding mode)
1991cd21dfcfSRalf Baechle 			 */
1992102cedc3SLeonid Yegoshin 			sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
19931da177e4SLinus Torvalds 		}
19941da177e4SLinus Torvalds 
1995e04582b7SAtsushi Nemoto 		if (has_fpu)
19961da177e4SLinus Torvalds 			break;
19971da177e4SLinus Torvalds 		if (sig)
19981da177e4SLinus Torvalds 			break;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 		cond_resched();
20011da177e4SLinus Torvalds 	} while (xcp->cp0_epc > prevepc);
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 	/* SIGILL indicates a non-fpu instruction */
20041da177e4SLinus Torvalds 	if (sig == SIGILL && xcp->cp0_epc != oldepc)
20053f7cac41SRalf Baechle 		/* but if EPC has advanced, then ignore it */
20061da177e4SLinus Torvalds 		sig = 0;
20071da177e4SLinus Torvalds 
20081da177e4SLinus Torvalds 	return sig;
20091da177e4SLinus Torvalds }
2010