xref: /linux/arch/mips/math-emu/cp1emu.c (revision f684362689ddc4a4e055be438d6416cc280a1372)
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 
48*f6843626SMaciej W. Rozycki #include <asm/cpu-info.h>
49cd8ee345SRalf Baechle #include <asm/processor.h>
501da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
51102cedc3SLeonid Yegoshin #include <asm/fpu.h>
52b0a668fbSLeonid Yegoshin #include <asm/mips-r2-to-r6-emul.h>
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #include "ieee754.h"
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */
571da177e4SLinus Torvalds 
58eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
591da177e4SLinus Torvalds 	mips_instruction);
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *,
62515b029dSDavid Daney 	struct mips_fpu_struct *, mips_instruction, void *__user *);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* Control registers */
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #define FPCREG_RID	0	/* $0  = revision id */
671da177e4SLinus Torvalds #define FPCREG_CSR	31	/* $31 = csr */
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* convert condition code register number to csr bit */
70b0a668fbSLeonid Yegoshin const unsigned int fpucondbit[8] = {
711da177e4SLinus Torvalds 	FPU_CSR_COND0,
721da177e4SLinus Torvalds 	FPU_CSR_COND1,
731da177e4SLinus Torvalds 	FPU_CSR_COND2,
741da177e4SLinus Torvalds 	FPU_CSR_COND3,
751da177e4SLinus Torvalds 	FPU_CSR_COND4,
761da177e4SLinus Torvalds 	FPU_CSR_COND5,
771da177e4SLinus Torvalds 	FPU_CSR_COND6,
781da177e4SLinus Torvalds 	FPU_CSR_COND7
791da177e4SLinus Torvalds };
801da177e4SLinus Torvalds 
81102cedc3SLeonid Yegoshin /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
82102cedc3SLeonid Yegoshin static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
83102cedc3SLeonid Yegoshin static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
84102cedc3SLeonid Yegoshin static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
85102cedc3SLeonid Yegoshin static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
86102cedc3SLeonid Yegoshin 
87102cedc3SLeonid Yegoshin /*
88102cedc3SLeonid Yegoshin  * This functions translates a 32-bit microMIPS instruction
89102cedc3SLeonid Yegoshin  * into a 32-bit MIPS32 instruction. Returns 0 on success
90102cedc3SLeonid Yegoshin  * and SIGILL otherwise.
91102cedc3SLeonid Yegoshin  */
92102cedc3SLeonid Yegoshin static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
93102cedc3SLeonid Yegoshin {
94102cedc3SLeonid Yegoshin 	union mips_instruction insn = *insn_ptr;
95102cedc3SLeonid Yegoshin 	union mips_instruction mips32_insn = insn;
96102cedc3SLeonid Yegoshin 	int func, fmt, op;
97102cedc3SLeonid Yegoshin 
98102cedc3SLeonid Yegoshin 	switch (insn.mm_i_format.opcode) {
99102cedc3SLeonid Yegoshin 	case mm_ldc132_op:
100102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = ldc1_op;
101102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
102102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
103102cedc3SLeonid Yegoshin 		break;
104102cedc3SLeonid Yegoshin 	case mm_lwc132_op:
105102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = lwc1_op;
106102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
107102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
108102cedc3SLeonid Yegoshin 		break;
109102cedc3SLeonid Yegoshin 	case mm_sdc132_op:
110102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = sdc1_op;
111102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
112102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
113102cedc3SLeonid Yegoshin 		break;
114102cedc3SLeonid Yegoshin 	case mm_swc132_op:
115102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = swc1_op;
116102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
117102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
118102cedc3SLeonid Yegoshin 		break;
119102cedc3SLeonid Yegoshin 	case mm_pool32i_op:
120102cedc3SLeonid Yegoshin 		/* NOTE: offset is << by 1 if in microMIPS mode. */
121102cedc3SLeonid Yegoshin 		if ((insn.mm_i_format.rt == mm_bc1f_op) ||
122102cedc3SLeonid Yegoshin 		    (insn.mm_i_format.rt == mm_bc1t_op)) {
123102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.opcode = cop1_op;
124102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.bc = bc_op;
125102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.flag =
126102cedc3SLeonid Yegoshin 				(insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
127102cedc3SLeonid Yegoshin 		} else
128102cedc3SLeonid Yegoshin 			return SIGILL;
129102cedc3SLeonid Yegoshin 		break;
130102cedc3SLeonid Yegoshin 	case mm_pool32f_op:
131102cedc3SLeonid Yegoshin 		switch (insn.mm_fp0_format.func) {
132102cedc3SLeonid Yegoshin 		case mm_32f_01_op:
133102cedc3SLeonid Yegoshin 		case mm_32f_11_op:
134102cedc3SLeonid Yegoshin 		case mm_32f_02_op:
135102cedc3SLeonid Yegoshin 		case mm_32f_12_op:
136102cedc3SLeonid Yegoshin 		case mm_32f_41_op:
137102cedc3SLeonid Yegoshin 		case mm_32f_51_op:
138102cedc3SLeonid Yegoshin 		case mm_32f_42_op:
139102cedc3SLeonid Yegoshin 		case mm_32f_52_op:
140102cedc3SLeonid Yegoshin 			op = insn.mm_fp0_format.func;
141102cedc3SLeonid Yegoshin 			if (op == mm_32f_01_op)
142102cedc3SLeonid Yegoshin 				func = madd_s_op;
143102cedc3SLeonid Yegoshin 			else if (op == mm_32f_11_op)
144102cedc3SLeonid Yegoshin 				func = madd_d_op;
145102cedc3SLeonid Yegoshin 			else if (op == mm_32f_02_op)
146102cedc3SLeonid Yegoshin 				func = nmadd_s_op;
147102cedc3SLeonid Yegoshin 			else if (op == mm_32f_12_op)
148102cedc3SLeonid Yegoshin 				func = nmadd_d_op;
149102cedc3SLeonid Yegoshin 			else if (op == mm_32f_41_op)
150102cedc3SLeonid Yegoshin 				func = msub_s_op;
151102cedc3SLeonid Yegoshin 			else if (op == mm_32f_51_op)
152102cedc3SLeonid Yegoshin 				func = msub_d_op;
153102cedc3SLeonid Yegoshin 			else if (op == mm_32f_42_op)
154102cedc3SLeonid Yegoshin 				func = nmsub_s_op;
155102cedc3SLeonid Yegoshin 			else
156102cedc3SLeonid Yegoshin 				func = nmsub_d_op;
157102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.opcode = cop1x_op;
158102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
159102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
160102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
161102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
162102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.func = func;
163102cedc3SLeonid Yegoshin 			break;
164102cedc3SLeonid Yegoshin 		case mm_32f_10_op:
165102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
166102cedc3SLeonid Yegoshin 			op = insn.mm_fp5_format.op & 0x7;
167102cedc3SLeonid Yegoshin 			if (op == mm_ldxc1_op)
168102cedc3SLeonid Yegoshin 				func = ldxc1_op;
169102cedc3SLeonid Yegoshin 			else if (op == mm_sdxc1_op)
170102cedc3SLeonid Yegoshin 				func = sdxc1_op;
171102cedc3SLeonid Yegoshin 			else if (op == mm_lwxc1_op)
172102cedc3SLeonid Yegoshin 				func = lwxc1_op;
173102cedc3SLeonid Yegoshin 			else if (op == mm_swxc1_op)
174102cedc3SLeonid Yegoshin 				func = swxc1_op;
175102cedc3SLeonid Yegoshin 
176102cedc3SLeonid Yegoshin 			if (func != -1) {
177102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = cop1x_op;
178102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs =
179102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.base;
180102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
181102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.index;
182102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = 0;
183102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = insn.mm_fp5_format.fd;
184102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = func;
185102cedc3SLeonid Yegoshin 			} else
186102cedc3SLeonid Yegoshin 				return SIGILL;
187102cedc3SLeonid Yegoshin 			break;
188102cedc3SLeonid Yegoshin 		case mm_32f_40_op:
189102cedc3SLeonid Yegoshin 			op = -1;	/* Invalid */
190102cedc3SLeonid Yegoshin 			if (insn.mm_fp2_format.op == mm_fmovt_op)
191102cedc3SLeonid Yegoshin 				op = 1;
192102cedc3SLeonid Yegoshin 			else if (insn.mm_fp2_format.op == mm_fmovf_op)
193102cedc3SLeonid Yegoshin 				op = 0;
194102cedc3SLeonid Yegoshin 			if (op != -1) {
195102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
196102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
197102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp2_format.fmt];
198102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
199102cedc3SLeonid Yegoshin 					(insn.mm_fp2_format.cc<<2) + op;
200102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
201102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fs;
202102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
203102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fd;
204102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = fmovc_op;
205102cedc3SLeonid Yegoshin 			} else
206102cedc3SLeonid Yegoshin 				return SIGILL;
207102cedc3SLeonid Yegoshin 			break;
208102cedc3SLeonid Yegoshin 		case mm_32f_60_op:
209102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
210102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fadd_op)
211102cedc3SLeonid Yegoshin 				func = fadd_op;
212102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fsub_op)
213102cedc3SLeonid Yegoshin 				func = fsub_op;
214102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmul_op)
215102cedc3SLeonid Yegoshin 				func = fmul_op;
216102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fdiv_op)
217102cedc3SLeonid Yegoshin 				func = fdiv_op;
218102cedc3SLeonid Yegoshin 			if (func != -1) {
219102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
220102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
221102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
222102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
223102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
224102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
225102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
226102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
227102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
228102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
229102cedc3SLeonid Yegoshin 			} else
230102cedc3SLeonid Yegoshin 				return SIGILL;
231102cedc3SLeonid Yegoshin 			break;
232102cedc3SLeonid Yegoshin 		case mm_32f_70_op:
233102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
234102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fmovn_op)
235102cedc3SLeonid Yegoshin 				func = fmovn_op;
236102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmovz_op)
237102cedc3SLeonid Yegoshin 				func = fmovz_op;
238102cedc3SLeonid Yegoshin 			if (func != -1) {
239102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
240102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
241102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
242102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
243102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
244102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
245102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
246102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
247102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
248102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
249102cedc3SLeonid Yegoshin 			} else
250102cedc3SLeonid Yegoshin 				return SIGILL;
251102cedc3SLeonid Yegoshin 			break;
252102cedc3SLeonid Yegoshin 		case mm_32f_73_op:    /* POOL32FXF */
253102cedc3SLeonid Yegoshin 			switch (insn.mm_fp1_format.op) {
254102cedc3SLeonid Yegoshin 			case mm_movf0_op:
255102cedc3SLeonid Yegoshin 			case mm_movf1_op:
256102cedc3SLeonid Yegoshin 			case mm_movt0_op:
257102cedc3SLeonid Yegoshin 			case mm_movt1_op:
258102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
259102cedc3SLeonid Yegoshin 				    mm_movf0_op)
260102cedc3SLeonid Yegoshin 					op = 0;
261102cedc3SLeonid Yegoshin 				else
262102cedc3SLeonid Yegoshin 					op = 1;
263102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = spec_op;
264102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
265102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
266102cedc3SLeonid Yegoshin 					(insn.mm_fp4_format.cc << 2) + op;
267102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
268102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = 0;
269102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = movc_op;
270102cedc3SLeonid Yegoshin 				break;
271102cedc3SLeonid Yegoshin 			case mm_fcvtd0_op:
272102cedc3SLeonid Yegoshin 			case mm_fcvtd1_op:
273102cedc3SLeonid Yegoshin 			case mm_fcvts0_op:
274102cedc3SLeonid Yegoshin 			case mm_fcvts1_op:
275102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
276102cedc3SLeonid Yegoshin 				    mm_fcvtd0_op) {
277102cedc3SLeonid Yegoshin 					func = fcvtd_op;
278102cedc3SLeonid Yegoshin 					fmt = swl_format[insn.mm_fp3_format.fmt];
279102cedc3SLeonid Yegoshin 				} else {
280102cedc3SLeonid Yegoshin 					func = fcvts_op;
281102cedc3SLeonid Yegoshin 					fmt = dwl_format[insn.mm_fp3_format.fmt];
282102cedc3SLeonid Yegoshin 				}
283102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
284102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt = fmt;
285102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
286102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
287102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
288102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
289102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
290102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
291102cedc3SLeonid Yegoshin 				break;
292102cedc3SLeonid Yegoshin 			case mm_fmov0_op:
293102cedc3SLeonid Yegoshin 			case mm_fmov1_op:
294102cedc3SLeonid Yegoshin 			case mm_fabs0_op:
295102cedc3SLeonid Yegoshin 			case mm_fabs1_op:
296102cedc3SLeonid Yegoshin 			case mm_fneg0_op:
297102cedc3SLeonid Yegoshin 			case mm_fneg1_op:
298102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
299102cedc3SLeonid Yegoshin 				    mm_fmov0_op)
300102cedc3SLeonid Yegoshin 					func = fmov_op;
301102cedc3SLeonid Yegoshin 				else if ((insn.mm_fp1_format.op & 0x7f) ==
302102cedc3SLeonid Yegoshin 					 mm_fabs0_op)
303102cedc3SLeonid Yegoshin 					func = fabs_op;
304102cedc3SLeonid Yegoshin 				else
305102cedc3SLeonid Yegoshin 					func = fneg_op;
306102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
307102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
308102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp3_format.fmt];
309102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
310102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
311102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
312102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
313102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
314102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
315102cedc3SLeonid Yegoshin 				break;
316102cedc3SLeonid Yegoshin 			case mm_ffloorl_op:
317102cedc3SLeonid Yegoshin 			case mm_ffloorw_op:
318102cedc3SLeonid Yegoshin 			case mm_fceill_op:
319102cedc3SLeonid Yegoshin 			case mm_fceilw_op:
320102cedc3SLeonid Yegoshin 			case mm_ftruncl_op:
321102cedc3SLeonid Yegoshin 			case mm_ftruncw_op:
322102cedc3SLeonid Yegoshin 			case mm_froundl_op:
323102cedc3SLeonid Yegoshin 			case mm_froundw_op:
324102cedc3SLeonid Yegoshin 			case mm_fcvtl_op:
325102cedc3SLeonid Yegoshin 			case mm_fcvtw_op:
326102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_ffloorl_op)
327102cedc3SLeonid Yegoshin 					func = ffloorl_op;
328102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ffloorw_op)
329102cedc3SLeonid Yegoshin 					func = ffloor_op;
330102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceill_op)
331102cedc3SLeonid Yegoshin 					func = fceill_op;
332102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceilw_op)
333102cedc3SLeonid Yegoshin 					func = fceil_op;
334102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncl_op)
335102cedc3SLeonid Yegoshin 					func = ftruncl_op;
336102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncw_op)
337102cedc3SLeonid Yegoshin 					func = ftrunc_op;
338102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundl_op)
339102cedc3SLeonid Yegoshin 					func = froundl_op;
340102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundw_op)
341102cedc3SLeonid Yegoshin 					func = fround_op;
342102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fcvtl_op)
343102cedc3SLeonid Yegoshin 					func = fcvtl_op;
344102cedc3SLeonid Yegoshin 				else
345102cedc3SLeonid Yegoshin 					func = fcvtw_op;
346102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
347102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
348102cedc3SLeonid Yegoshin 					sd_format[insn.mm_fp1_format.fmt];
349102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
350102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
351102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
352102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
353102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
354102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
355102cedc3SLeonid Yegoshin 				break;
356102cedc3SLeonid Yegoshin 			case mm_frsqrt_op:
357102cedc3SLeonid Yegoshin 			case mm_fsqrt_op:
358102cedc3SLeonid Yegoshin 			case mm_frecip_op:
359102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_frsqrt_op)
360102cedc3SLeonid Yegoshin 					func = frsqrt_op;
361102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fsqrt_op)
362102cedc3SLeonid Yegoshin 					func = fsqrt_op;
363102cedc3SLeonid Yegoshin 				else
364102cedc3SLeonid Yegoshin 					func = frecip_op;
365102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
366102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
367102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp1_format.fmt];
368102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
369102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
370102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
371102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
372102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
373102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
374102cedc3SLeonid Yegoshin 				break;
375102cedc3SLeonid Yegoshin 			case mm_mfc1_op:
376102cedc3SLeonid Yegoshin 			case mm_mtc1_op:
377102cedc3SLeonid Yegoshin 			case mm_cfc1_op:
378102cedc3SLeonid Yegoshin 			case mm_ctc1_op:
3799355e59cSSteven J. Hill 			case mm_mfhc1_op:
3809355e59cSSteven J. Hill 			case mm_mthc1_op:
381102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_mfc1_op)
382102cedc3SLeonid Yegoshin 					op = mfc_op;
383102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_mtc1_op)
384102cedc3SLeonid Yegoshin 					op = mtc_op;
385102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_cfc1_op)
386102cedc3SLeonid Yegoshin 					op = cfc_op;
3879355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_ctc1_op)
388102cedc3SLeonid Yegoshin 					op = ctc_op;
3899355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_mfhc1_op)
3909355e59cSSteven J. Hill 					op = mfhc_op;
3919355e59cSSteven J. Hill 				else
3929355e59cSSteven J. Hill 					op = mthc_op;
393102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.opcode = cop1_op;
394102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.op = op;
395102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.rt =
396102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
397102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fs =
398102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
399102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fd = 0;
400102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.func = 0;
401102cedc3SLeonid Yegoshin 				break;
402102cedc3SLeonid Yegoshin 			default:
403102cedc3SLeonid Yegoshin 				return SIGILL;
404102cedc3SLeonid Yegoshin 			}
405102cedc3SLeonid Yegoshin 			break;
406102cedc3SLeonid Yegoshin 		case mm_32f_74_op:	/* c.cond.fmt */
407102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.opcode = cop1_op;
408102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fmt =
409102cedc3SLeonid Yegoshin 				sdps_format[insn.mm_fp4_format.fmt];
410102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
411102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
412102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
413102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.func =
414102cedc3SLeonid Yegoshin 				insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
415102cedc3SLeonid Yegoshin 			break;
416102cedc3SLeonid Yegoshin 		default:
417102cedc3SLeonid Yegoshin 			return SIGILL;
418102cedc3SLeonid Yegoshin 		}
419102cedc3SLeonid Yegoshin 		break;
420102cedc3SLeonid Yegoshin 	default:
421102cedc3SLeonid Yegoshin 		return SIGILL;
422102cedc3SLeonid Yegoshin 	}
423102cedc3SLeonid Yegoshin 
424102cedc3SLeonid Yegoshin 	*insn_ptr = mips32_insn;
425102cedc3SLeonid Yegoshin 	return 0;
426102cedc3SLeonid Yegoshin }
427102cedc3SLeonid Yegoshin 
4281da177e4SLinus Torvalds /*
4291da177e4SLinus Torvalds  * Redundant with logic already in kernel/branch.c,
4301da177e4SLinus Torvalds  * embedded in compute_return_epc.  At some point,
4311da177e4SLinus Torvalds  * a single subroutine should be used across both
4321da177e4SLinus Torvalds  * modules.
4331da177e4SLinus Torvalds  */
434102cedc3SLeonid Yegoshin static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
435102cedc3SLeonid Yegoshin 			 unsigned long *contpc)
4361da177e4SLinus Torvalds {
437102cedc3SLeonid Yegoshin 	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
438102cedc3SLeonid Yegoshin 	unsigned int fcr31;
439102cedc3SLeonid Yegoshin 	unsigned int bit = 0;
440102cedc3SLeonid Yegoshin 
441102cedc3SLeonid Yegoshin 	switch (insn.i_format.opcode) {
4421da177e4SLinus Torvalds 	case spec_op:
443102cedc3SLeonid Yegoshin 		switch (insn.r_format.func) {
4441da177e4SLinus Torvalds 		case jalr_op:
445102cedc3SLeonid Yegoshin 			regs->regs[insn.r_format.rd] =
446102cedc3SLeonid Yegoshin 				regs->cp0_epc + dec_insn.pc_inc +
447102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
448102cedc3SLeonid Yegoshin 			/* Fall through */
4491da177e4SLinus Torvalds 		case jr_op:
4505f9f41c4SMarkos Chandras 			/* For R6, JR already emulated in jalr_op */
4515f9f41c4SMarkos Chandras 			if (NO_R6EMU && insn.r_format.opcode == jr_op)
4525f9f41c4SMarkos Chandras 				break;
453102cedc3SLeonid Yegoshin 			*contpc = regs->regs[insn.r_format.rs];
4541da177e4SLinus Torvalds 			return 1;
4551da177e4SLinus Torvalds 		}
4561da177e4SLinus Torvalds 		break;
4571da177e4SLinus Torvalds 	case bcond_op:
458102cedc3SLeonid Yegoshin 		switch (insn.i_format.rt) {
4591da177e4SLinus Torvalds 		case bltzal_op:
4601da177e4SLinus Torvalds 		case bltzall_op:
461319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
462319824eaSMarkos Chandras 			    insn.i_format.rt == bltzall_op))
463319824eaSMarkos Chandras 				break;
464319824eaSMarkos Chandras 
465102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
466102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
467102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
468102cedc3SLeonid Yegoshin 			/* Fall through */
469102cedc3SLeonid Yegoshin 		case bltzl_op:
470319824eaSMarkos Chandras 			if (NO_R6EMU)
471319824eaSMarkos Chandras 				break;
472319824eaSMarkos Chandras 		case bltz_op:
473102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] < 0)
474102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
475102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
476102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
477102cedc3SLeonid Yegoshin 			else
478102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
479102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
480102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
4811da177e4SLinus Torvalds 			return 1;
482102cedc3SLeonid Yegoshin 		case bgezal_op:
483102cedc3SLeonid Yegoshin 		case bgezall_op:
484319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
485319824eaSMarkos Chandras 			    insn.i_format.rt == bgezall_op))
486319824eaSMarkos Chandras 				break;
487319824eaSMarkos Chandras 
488102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
489102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
490102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
491102cedc3SLeonid Yegoshin 			/* Fall through */
492102cedc3SLeonid Yegoshin 		case bgezl_op:
493319824eaSMarkos Chandras 			if (NO_R6EMU)
494319824eaSMarkos Chandras 				break;
495319824eaSMarkos Chandras 		case bgez_op:
496102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] >= 0)
497102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
498102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
499102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
500102cedc3SLeonid Yegoshin 			else
501102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
502102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
503102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
504102cedc3SLeonid Yegoshin 			return 1;
5051da177e4SLinus Torvalds 		}
5061da177e4SLinus Torvalds 		break;
5071da177e4SLinus Torvalds 	case jalx_op:
508102cedc3SLeonid Yegoshin 		set_isa16_mode(bit);
509102cedc3SLeonid Yegoshin 	case jal_op:
510102cedc3SLeonid Yegoshin 		regs->regs[31] = regs->cp0_epc +
511102cedc3SLeonid Yegoshin 			dec_insn.pc_inc +
512102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc;
513102cedc3SLeonid Yegoshin 		/* Fall through */
514102cedc3SLeonid Yegoshin 	case j_op:
515102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
516102cedc3SLeonid Yegoshin 		*contpc >>= 28;
517102cedc3SLeonid Yegoshin 		*contpc <<= 28;
518102cedc3SLeonid Yegoshin 		*contpc |= (insn.j_format.target << 2);
519102cedc3SLeonid Yegoshin 		/* Set microMIPS mode bit: XOR for jalx. */
520102cedc3SLeonid Yegoshin 		*contpc ^= bit;
5211da177e4SLinus Torvalds 		return 1;
522102cedc3SLeonid Yegoshin 	case beql_op:
523319824eaSMarkos Chandras 		if (NO_R6EMU)
524319824eaSMarkos Chandras 			break;
525319824eaSMarkos Chandras 	case beq_op:
526102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] ==
527102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
528102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
529102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
530102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
531102cedc3SLeonid Yegoshin 		else
532102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
533102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
534102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
535102cedc3SLeonid Yegoshin 		return 1;
536102cedc3SLeonid Yegoshin 	case bnel_op:
537319824eaSMarkos Chandras 		if (NO_R6EMU)
538319824eaSMarkos Chandras 			break;
539319824eaSMarkos Chandras 	case bne_op:
540102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] !=
541102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
542102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
543102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
544102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
545102cedc3SLeonid Yegoshin 		else
546102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
547102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
548102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
549102cedc3SLeonid Yegoshin 		return 1;
550102cedc3SLeonid Yegoshin 	case blezl_op:
551319824eaSMarkos Chandras 		if (NO_R6EMU)
552319824eaSMarkos Chandras 			break;
553319824eaSMarkos Chandras 	case blez_op:
554a8ff66f5SMarkos Chandras 
555a8ff66f5SMarkos Chandras 		/*
556a8ff66f5SMarkos Chandras 		 * Compact branches for R6 for the
557a8ff66f5SMarkos Chandras 		 * blez and blezl opcodes.
558a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = 0 | rt != 0  == BLEZALC
559a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = rt != 0      == BGEZALC
560a8ff66f5SMarkos Chandras 		 * BLEZ  | rs != 0 | rt != 0 == BGEUC
561a8ff66f5SMarkos Chandras 		 * BLEZL | rs = 0 | rt != 0  == BLEZC
562a8ff66f5SMarkos Chandras 		 * BLEZL | rs = rt != 0      == BGEZC
563a8ff66f5SMarkos Chandras 		 * BLEZL | rs != 0 | rt != 0 == BGEC
564a8ff66f5SMarkos Chandras 		 *
565a8ff66f5SMarkos Chandras 		 * For real BLEZ{,L}, rt is always 0.
566a8ff66f5SMarkos Chandras 		 */
567a8ff66f5SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
568a8ff66f5SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
569a8ff66f5SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
570a8ff66f5SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
571a8ff66f5SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
572a8ff66f5SMarkos Chandras 					dec_insn.pc_inc;
573a8ff66f5SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
574a8ff66f5SMarkos Chandras 				dec_insn.next_pc_inc;
575a8ff66f5SMarkos Chandras 
576a8ff66f5SMarkos Chandras 			return 1;
577a8ff66f5SMarkos Chandras 		}
578102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] <= 0)
579102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
580102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
581102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
582102cedc3SLeonid Yegoshin 		else
583102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
584102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
585102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
586102cedc3SLeonid Yegoshin 		return 1;
587102cedc3SLeonid Yegoshin 	case bgtzl_op:
588319824eaSMarkos Chandras 		if (NO_R6EMU)
589319824eaSMarkos Chandras 			break;
590319824eaSMarkos Chandras 	case bgtz_op:
591f1b44067SMarkos Chandras 		/*
592f1b44067SMarkos Chandras 		 * Compact branches for R6 for the
593f1b44067SMarkos Chandras 		 * bgtz and bgtzl opcodes.
594f1b44067SMarkos Chandras 		 * BGTZ  | rs = 0 | rt != 0  == BGTZALC
595f1b44067SMarkos Chandras 		 * BGTZ  | rs = rt != 0      == BLTZALC
596f1b44067SMarkos Chandras 		 * BGTZ  | rs != 0 | rt != 0 == BLTUC
597f1b44067SMarkos Chandras 		 * BGTZL | rs = 0 | rt != 0  == BGTZC
598f1b44067SMarkos Chandras 		 * BGTZL | rs = rt != 0      == BLTZC
599f1b44067SMarkos Chandras 		 * BGTZL | rs != 0 | rt != 0 == BLTC
600f1b44067SMarkos Chandras 		 *
601f1b44067SMarkos Chandras 		 * *ZALC varint for BGTZ &&& rt != 0
602f1b44067SMarkos Chandras 		 * For real GTZ{,L}, rt is always 0.
603f1b44067SMarkos Chandras 		 */
604f1b44067SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
605f1b44067SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
606f1b44067SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
607f1b44067SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
608f1b44067SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
609f1b44067SMarkos Chandras 					dec_insn.pc_inc;
610f1b44067SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
611f1b44067SMarkos Chandras 				dec_insn.next_pc_inc;
612f1b44067SMarkos Chandras 
613f1b44067SMarkos Chandras 			return 1;
614f1b44067SMarkos Chandras 		}
615f1b44067SMarkos Chandras 
616102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] > 0)
617102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
618102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
619102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
620102cedc3SLeonid Yegoshin 		else
621102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
622102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
623102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
624102cedc3SLeonid Yegoshin 		return 1;
625c893ce38SMarkos Chandras 	case cbcond0_op:
62610d962d5SMarkos Chandras 	case cbcond1_op:
627c893ce38SMarkos Chandras 		if (!cpu_has_mips_r6)
628c893ce38SMarkos Chandras 			break;
629c893ce38SMarkos Chandras 		if (insn.i_format.rt && !insn.i_format.rs)
630c893ce38SMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
631c893ce38SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
632c893ce38SMarkos Chandras 			dec_insn.next_pc_inc;
633c893ce38SMarkos Chandras 
634c893ce38SMarkos Chandras 		return 1;
635c26d4219SDavid Daney #ifdef CONFIG_CPU_CAVIUM_OCTEON
636c26d4219SDavid Daney 	case lwc2_op: /* This is bbit0 on Octeon */
637c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
638c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
639c26d4219SDavid Daney 		else
640c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
641c26d4219SDavid Daney 		return 1;
642c26d4219SDavid Daney 	case ldc2_op: /* This is bbit032 on Octeon */
643c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
644c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
645c26d4219SDavid Daney 		else
646c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
647c26d4219SDavid Daney 		return 1;
648c26d4219SDavid Daney 	case swc2_op: /* This is bbit1 on Octeon */
649c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
650c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
651c26d4219SDavid Daney 		else
652c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
653c26d4219SDavid Daney 		return 1;
654c26d4219SDavid Daney 	case sdc2_op: /* This is bbit132 on Octeon */
655c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
656c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
657c26d4219SDavid Daney 		else
658c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
659c26d4219SDavid Daney 		return 1;
6608467ca01SMarkos Chandras #else
6618467ca01SMarkos Chandras 	case bc6_op:
6628467ca01SMarkos Chandras 		/*
6638467ca01SMarkos Chandras 		 * Only valid for MIPS R6 but we can still end up
6648467ca01SMarkos Chandras 		 * here from a broken userland so just tell emulator
6658467ca01SMarkos Chandras 		 * this is not a branch and let it break later on.
6668467ca01SMarkos Chandras 		 */
6678467ca01SMarkos Chandras 		if  (!cpu_has_mips_r6)
6688467ca01SMarkos Chandras 			break;
6698467ca01SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
6708467ca01SMarkos Chandras 			dec_insn.next_pc_inc;
6718467ca01SMarkos Chandras 
6728467ca01SMarkos Chandras 		return 1;
67384fef630SMarkos Chandras 	case balc6_op:
67484fef630SMarkos Chandras 		if (!cpu_has_mips_r6)
67584fef630SMarkos Chandras 			break;
67684fef630SMarkos Chandras 		regs->regs[31] = regs->cp0_epc + 4;
67784fef630SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
67884fef630SMarkos Chandras 			dec_insn.next_pc_inc;
67984fef630SMarkos Chandras 
68084fef630SMarkos Chandras 		return 1;
68169b9a2fdSMarkos Chandras 	case beqzcjic_op:
68269b9a2fdSMarkos Chandras 		if (!cpu_has_mips_r6)
68369b9a2fdSMarkos Chandras 			break;
68469b9a2fdSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
68569b9a2fdSMarkos Chandras 			dec_insn.next_pc_inc;
68669b9a2fdSMarkos Chandras 
68769b9a2fdSMarkos Chandras 		return 1;
68828d6f93dSMarkos Chandras 	case bnezcjialc_op:
68928d6f93dSMarkos Chandras 		if (!cpu_has_mips_r6)
69028d6f93dSMarkos Chandras 			break;
69128d6f93dSMarkos Chandras 		if (!insn.i_format.rs)
69228d6f93dSMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
69328d6f93dSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
69428d6f93dSMarkos Chandras 			dec_insn.next_pc_inc;
69528d6f93dSMarkos Chandras 
69628d6f93dSMarkos Chandras 		return 1;
697c26d4219SDavid Daney #endif
6981da177e4SLinus Torvalds 	case cop0_op:
6991da177e4SLinus Torvalds 	case cop1_op:
700c8a34581SMarkos Chandras 		/* Need to check for R6 bc1nez and bc1eqz branches */
701c8a34581SMarkos Chandras 		if (cpu_has_mips_r6 &&
702c8a34581SMarkos Chandras 		    ((insn.i_format.rs == bc1eqz_op) ||
703c8a34581SMarkos Chandras 		     (insn.i_format.rs == bc1nez_op))) {
704c8a34581SMarkos Chandras 			bit = 0;
705c8a34581SMarkos Chandras 			switch (insn.i_format.rs) {
706c8a34581SMarkos Chandras 			case bc1eqz_op:
707c8a34581SMarkos Chandras 				if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
708c8a34581SMarkos Chandras 				    bit = 1;
709c8a34581SMarkos Chandras 				break;
710c8a34581SMarkos Chandras 			case bc1nez_op:
711c8a34581SMarkos Chandras 				if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
712c8a34581SMarkos Chandras 				    bit = 1;
713c8a34581SMarkos Chandras 				break;
714c8a34581SMarkos Chandras 			}
715c8a34581SMarkos Chandras 			if (bit)
716c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
717c8a34581SMarkos Chandras 					dec_insn.pc_inc +
718c8a34581SMarkos Chandras 					(insn.i_format.simmediate << 2);
719c8a34581SMarkos Chandras 			else
720c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
721c8a34581SMarkos Chandras 					dec_insn.pc_inc +
722c8a34581SMarkos Chandras 					dec_insn.next_pc_inc;
723c8a34581SMarkos Chandras 
724c8a34581SMarkos Chandras 			return 1;
725c8a34581SMarkos Chandras 		}
726c8a34581SMarkos Chandras 		/* R2/R6 compatible cop1 instruction. Fall through */
7271da177e4SLinus Torvalds 	case cop2_op:
7281da177e4SLinus Torvalds 	case cop1x_op:
729102cedc3SLeonid Yegoshin 		if (insn.i_format.rs == bc_op) {
730102cedc3SLeonid Yegoshin 			preempt_disable();
731102cedc3SLeonid Yegoshin 			if (is_fpu_owner())
732842dfc11SManuel Lauss 			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
733102cedc3SLeonid Yegoshin 			else
734102cedc3SLeonid Yegoshin 				fcr31 = current->thread.fpu.fcr31;
735102cedc3SLeonid Yegoshin 			preempt_enable();
736102cedc3SLeonid Yegoshin 
737102cedc3SLeonid Yegoshin 			bit = (insn.i_format.rt >> 2);
738102cedc3SLeonid Yegoshin 			bit += (bit != 0);
739102cedc3SLeonid Yegoshin 			bit += 23;
740102cedc3SLeonid Yegoshin 			switch (insn.i_format.rt & 3) {
741102cedc3SLeonid Yegoshin 			case 0:	/* bc1f */
742102cedc3SLeonid Yegoshin 			case 2:	/* bc1fl */
743102cedc3SLeonid Yegoshin 				if (~fcr31 & (1 << bit))
744102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
745102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
746102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
747102cedc3SLeonid Yegoshin 				else
748102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
749102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
750102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
751102cedc3SLeonid Yegoshin 				return 1;
752102cedc3SLeonid Yegoshin 			case 1:	/* bc1t */
753102cedc3SLeonid Yegoshin 			case 3:	/* bc1tl */
754102cedc3SLeonid Yegoshin 				if (fcr31 & (1 << bit))
755102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
756102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
757102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
758102cedc3SLeonid Yegoshin 				else
759102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
760102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
761102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
7621da177e4SLinus Torvalds 				return 1;
7631da177e4SLinus Torvalds 			}
764102cedc3SLeonid Yegoshin 		}
765102cedc3SLeonid Yegoshin 		break;
766102cedc3SLeonid Yegoshin 	}
7671da177e4SLinus Torvalds 	return 0;
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds /*
7711da177e4SLinus Torvalds  * In the Linux kernel, we support selection of FPR format on the
772da0bac33SDavid Daney  * basis of the Status.FR bit.	If an FPU is not present, the FR bit
773da0bac33SDavid Daney  * is hardwired to zero, which would imply a 32-bit FPU even for
774597ce172SPaul Burton  * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
77551d943f0SRalf Baechle  * FPU emu is slow and bulky and optimizing this function offers fairly
77651d943f0SRalf Baechle  * sizeable benefits so we try to be clever and make this function return
77751d943f0SRalf Baechle  * a constant whenever possible, that is on 64-bit kernels without O32
778597ce172SPaul Burton  * compatibility enabled and on 32-bit without 64-bit FPU support.
7791da177e4SLinus Torvalds  */
780da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp)
781da0bac33SDavid Daney {
78208a07904SRalf Baechle 	if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
78351d943f0SRalf Baechle 		return 1;
78408a07904SRalf Baechle 	else if (config_enabled(CONFIG_32BIT) &&
78508a07904SRalf Baechle 		 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
786da0bac33SDavid Daney 		return 0;
78708a07904SRalf Baechle 
788597ce172SPaul Burton 	return !test_thread_flag(TIF_32BIT_FPREGS);
789da0bac33SDavid Daney }
7901da177e4SLinus Torvalds 
7914227a2d4SPaul Burton static inline bool hybrid_fprs(void)
7924227a2d4SPaul Burton {
7934227a2d4SPaul Burton 	return test_thread_flag(TIF_HYBRID_FPREGS);
7944227a2d4SPaul Burton }
7954227a2d4SPaul Burton 
79647fa0c02SRalf Baechle #define SIFROMREG(si, x)						\
79747fa0c02SRalf Baechle do {									\
7984227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs())				\
799c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[x], 0);			\
800bbd426f5SPaul Burton 	else								\
801c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);	\
802bbd426f5SPaul Burton } while (0)
803da0bac33SDavid Daney 
80447fa0c02SRalf Baechle #define SITOREG(si, x)							\
80547fa0c02SRalf Baechle do {									\
8064227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs()) {			\
807ef1c47afSPaul Burton 		unsigned i;						\
808bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[x], 0, si);				\
809ef1c47afSPaul Burton 		for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)	\
810ef1c47afSPaul Burton 			set_fpr32(&ctx->fpr[x], i, 0);			\
811ef1c47afSPaul Burton 	} else {							\
812bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);		\
813ef1c47afSPaul Burton 	}								\
814bbd426f5SPaul Burton } while (0)
8151da177e4SLinus Torvalds 
816c8c0da6bSPaul Burton #define SIFROMHREG(si, x)	((si) = (int)get_fpr32(&ctx->fpr[x], 1))
817ef1c47afSPaul Burton 
81847fa0c02SRalf Baechle #define SITOHREG(si, x)							\
81947fa0c02SRalf Baechle do {									\
820ef1c47afSPaul Burton 	unsigned i;							\
821ef1c47afSPaul Burton 	set_fpr32(&ctx->fpr[x], 1, si);					\
822ef1c47afSPaul Burton 	for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)		\
823ef1c47afSPaul Burton 		set_fpr32(&ctx->fpr[x], i, 0);				\
824ef1c47afSPaul Burton } while (0)
8251ac94400SLeonid Yegoshin 
826bbd426f5SPaul Burton #define DIFROMREG(di, x)						\
827bbd426f5SPaul Burton 	((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
828bbd426f5SPaul Burton 
82947fa0c02SRalf Baechle #define DITOREG(di, x)							\
83047fa0c02SRalf Baechle do {									\
831ef1c47afSPaul Burton 	unsigned fpr, i;						\
832ef1c47afSPaul Burton 	fpr = (x) & ~(cop1_64bit(xcp) == 0);				\
833ef1c47afSPaul Burton 	set_fpr64(&ctx->fpr[fpr], 0, di);				\
834ef1c47afSPaul Burton 	for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++)		\
835ef1c47afSPaul Burton 		set_fpr64(&ctx->fpr[fpr], i, 0);			\
836ef1c47afSPaul Burton } while (0)
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
8391da177e4SLinus Torvalds #define SPTOREG(sp, x)	SITOREG((sp).bits, x)
8401da177e4SLinus Torvalds #define DPFROMREG(dp, x)	DIFROMREG((dp).bits, x)
8411da177e4SLinus Torvalds #define DPTOREG(dp, x)	DITOREG((dp).bits, x)
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds /*
844d4f5b088SMaciej W. Rozycki  * Emulate a CFC1 instruction.
845d4f5b088SMaciej W. Rozycki  */
846d4f5b088SMaciej W. Rozycki static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
847d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
848d4f5b088SMaciej W. Rozycki {
849d4f5b088SMaciej W. Rozycki 	u32 value;
850d4f5b088SMaciej W. Rozycki 
851d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RD(ir) == FPCREG_CSR) {
852d4f5b088SMaciej W. Rozycki 		value = ctx->fcr31;
853d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-csr=%08x\n",
854d4f5b088SMaciej W. Rozycki 			 (void *)xcp->cp0_epc,
855d4f5b088SMaciej W. Rozycki 			 MIPSInst_RT(ir), value);
856d4f5b088SMaciej W. Rozycki 	} else if (MIPSInst_RD(ir) == FPCREG_RID)
857*f6843626SMaciej W. Rozycki 		value = current_cpu_data.fpu_id;
858d4f5b088SMaciej W. Rozycki 	else
859d4f5b088SMaciej W. Rozycki 		value = 0;
860d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir))
861d4f5b088SMaciej W. Rozycki 		xcp->regs[MIPSInst_RT(ir)] = value;
862d4f5b088SMaciej W. Rozycki }
863d4f5b088SMaciej W. Rozycki 
864d4f5b088SMaciej W. Rozycki /*
865d4f5b088SMaciej W. Rozycki  * Emulate a CTC1 instruction.
866d4f5b088SMaciej W. Rozycki  */
867d4f5b088SMaciej W. Rozycki static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
868d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
869d4f5b088SMaciej W. Rozycki {
870d4f5b088SMaciej W. Rozycki 	u32 value;
871d4f5b088SMaciej W. Rozycki 
872d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir) == 0)
873d4f5b088SMaciej W. Rozycki 		value = 0;
874d4f5b088SMaciej W. Rozycki 	else
875d4f5b088SMaciej W. Rozycki 		value = xcp->regs[MIPSInst_RT(ir)];
876d4f5b088SMaciej W. Rozycki 
877d4f5b088SMaciej W. Rozycki 	/* we only have one writable control reg
878d4f5b088SMaciej W. Rozycki 	 */
879d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RD(ir) == FPCREG_CSR) {
880d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->csr=%08x\n",
881d4f5b088SMaciej W. Rozycki 			 (void *)xcp->cp0_epc,
882d4f5b088SMaciej W. Rozycki 			 MIPSInst_RT(ir), value);
883d4f5b088SMaciej W. Rozycki 
884d4f5b088SMaciej W. Rozycki 		/* Don't write reserved bits.  */
885d4f5b088SMaciej W. Rozycki 		ctx->fcr31 = value & ~FPU_CSR_RSVD;
886d4f5b088SMaciej W. Rozycki 	}
887d4f5b088SMaciej W. Rozycki }
888d4f5b088SMaciej W. Rozycki 
889d4f5b088SMaciej W. Rozycki /*
8901da177e4SLinus Torvalds  * Emulate the single floating point instruction pointed at by EPC.
8911da177e4SLinus Torvalds  * Two instructions if the instruction is in a branch delay slot.
8921da177e4SLinus Torvalds  */
8931da177e4SLinus Torvalds 
894515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
895102cedc3SLeonid Yegoshin 		struct mm_decoded_insn dec_insn, void *__user *fault_addr)
8961da177e4SLinus Torvalds {
897102cedc3SLeonid Yegoshin 	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
8983f7cac41SRalf Baechle 	unsigned int cond, cbit;
8993f7cac41SRalf Baechle 	mips_instruction ir;
9003f7cac41SRalf Baechle 	int likely, pc_inc;
9013f7cac41SRalf Baechle 	u32 __user *wva;
9023f7cac41SRalf Baechle 	u64 __user *dva;
9033f7cac41SRalf Baechle 	u32 wval;
9043f7cac41SRalf Baechle 	u64 dval;
9053f7cac41SRalf Baechle 	int sig;
9061da177e4SLinus Torvalds 
90770e4c234SRalf Baechle 	/*
90870e4c234SRalf Baechle 	 * These are giving gcc a gentle hint about what to expect in
90970e4c234SRalf Baechle 	 * dec_inst in order to do better optimization.
91070e4c234SRalf Baechle 	 */
91170e4c234SRalf Baechle 	if (!cpu_has_mmips && dec_insn.micro_mips_mode)
91270e4c234SRalf Baechle 		unreachable();
91370e4c234SRalf Baechle 
9141da177e4SLinus Torvalds 	/* XXX NEC Vr54xx bug workaround */
915e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
916102cedc3SLeonid Yegoshin 		if (dec_insn.micro_mips_mode) {
917102cedc3SLeonid Yegoshin 			if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
918e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
919102cedc3SLeonid Yegoshin 		} else {
920102cedc3SLeonid Yegoshin 			if (!isBranchInstr(xcp, dec_insn, &contpc))
921e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
922102cedc3SLeonid Yegoshin 		}
923102cedc3SLeonid Yegoshin 	}
9241da177e4SLinus Torvalds 
925e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
9261da177e4SLinus Torvalds 		/*
9271da177e4SLinus Torvalds 		 * The instruction to be emulated is in a branch delay slot
9281da177e4SLinus Torvalds 		 * which means that we have to	emulate the branch instruction
9291da177e4SLinus Torvalds 		 * BEFORE we do the cop1 instruction.
9301da177e4SLinus Torvalds 		 *
9311da177e4SLinus Torvalds 		 * This branch could be a COP1 branch, but in that case we
9321da177e4SLinus Torvalds 		 * would have had a trap for that instruction, and would not
9331da177e4SLinus Torvalds 		 * come through this route.
9341da177e4SLinus Torvalds 		 *
9351da177e4SLinus Torvalds 		 * Linux MIPS branch emulator operates on context, updating the
9361da177e4SLinus Torvalds 		 * cp0_epc.
9371da177e4SLinus Torvalds 		 */
938102cedc3SLeonid Yegoshin 		ir = dec_insn.next_insn;  /* process delay slot instr */
939102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.next_pc_inc;
940333d1f67SRalf Baechle 	} else {
941102cedc3SLeonid Yegoshin 		ir = dec_insn.insn;       /* process current instr */
942102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.pc_inc;
943102cedc3SLeonid Yegoshin 	}
944102cedc3SLeonid Yegoshin 
945102cedc3SLeonid Yegoshin 	/*
946102cedc3SLeonid Yegoshin 	 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
947102cedc3SLeonid Yegoshin 	 * instructions, we want to convert microMIPS FPU instructions
948102cedc3SLeonid Yegoshin 	 * into MIPS32 instructions so that we could reuse all of the
949102cedc3SLeonid Yegoshin 	 * FPU emulation code.
950102cedc3SLeonid Yegoshin 	 *
951102cedc3SLeonid Yegoshin 	 * NOTE: We cannot do this for branch instructions since they
952102cedc3SLeonid Yegoshin 	 *       are not a subset. Example: Cannot emulate a 16-bit
953102cedc3SLeonid Yegoshin 	 *       aligned target address with a MIPS32 instruction.
954102cedc3SLeonid Yegoshin 	 */
955102cedc3SLeonid Yegoshin 	if (dec_insn.micro_mips_mode) {
956102cedc3SLeonid Yegoshin 		/*
957102cedc3SLeonid Yegoshin 		 * If next instruction is a 16-bit instruction, then it
958102cedc3SLeonid Yegoshin 		 * it cannot be a FPU instruction. This could happen
959102cedc3SLeonid Yegoshin 		 * since we can be called for non-FPU instructions.
960102cedc3SLeonid Yegoshin 		 */
961102cedc3SLeonid Yegoshin 		if ((pc_inc == 2) ||
962102cedc3SLeonid Yegoshin 			(microMIPS32_to_MIPS32((union mips_instruction *)&ir)
963102cedc3SLeonid Yegoshin 			 == SIGILL))
964102cedc3SLeonid Yegoshin 			return SIGILL;
9651da177e4SLinus Torvalds 	}
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds emul:
968a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
969b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(emulated);
9701da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(ir)) {
9713f7cac41SRalf Baechle 	case ldc1_op:
9723f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
9731da177e4SLinus Torvalds 				     MIPSInst_SIMM(ir));
974b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
975515b029dSDavid Daney 
9763f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
977b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
9783f7cac41SRalf Baechle 			*fault_addr = dva;
9791da177e4SLinus Torvalds 			return SIGBUS;
9801da177e4SLinus Torvalds 		}
9813f7cac41SRalf Baechle 		if (__get_user(dval, dva)) {
982515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
9833f7cac41SRalf Baechle 			*fault_addr = dva;
984515b029dSDavid Daney 			return SIGSEGV;
985515b029dSDavid Daney 		}
9863f7cac41SRalf Baechle 		DITOREG(dval, MIPSInst_RT(ir));
9871da177e4SLinus Torvalds 		break;
9881da177e4SLinus Torvalds 
9893f7cac41SRalf Baechle 	case sdc1_op:
9903f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
9911da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
992b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
9933f7cac41SRalf Baechle 		DIFROMREG(dval, MIPSInst_RT(ir));
9943f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
995b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
9963f7cac41SRalf Baechle 			*fault_addr = dva;
9971da177e4SLinus Torvalds 			return SIGBUS;
9981da177e4SLinus Torvalds 		}
9993f7cac41SRalf Baechle 		if (__put_user(dval, dva)) {
1000515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10013f7cac41SRalf Baechle 			*fault_addr = dva;
1002515b029dSDavid Daney 			return SIGSEGV;
1003515b029dSDavid Daney 		}
10041da177e4SLinus Torvalds 		break;
10051da177e4SLinus Torvalds 
10063f7cac41SRalf Baechle 	case lwc1_op:
10073f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10081da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1009b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
10103f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
1011b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10123f7cac41SRalf Baechle 			*fault_addr = wva;
10131da177e4SLinus Torvalds 			return SIGBUS;
10141da177e4SLinus Torvalds 		}
10153f7cac41SRalf Baechle 		if (__get_user(wval, wva)) {
1016515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10173f7cac41SRalf Baechle 			*fault_addr = wva;
1018515b029dSDavid Daney 			return SIGSEGV;
1019515b029dSDavid Daney 		}
10203f7cac41SRalf Baechle 		SITOREG(wval, MIPSInst_RT(ir));
10211da177e4SLinus Torvalds 		break;
10221da177e4SLinus Torvalds 
10233f7cac41SRalf Baechle 	case swc1_op:
10243f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10251da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1026b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
10273f7cac41SRalf Baechle 		SIFROMREG(wval, MIPSInst_RT(ir));
10283f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
1029b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10303f7cac41SRalf Baechle 			*fault_addr = wva;
10311da177e4SLinus Torvalds 			return SIGBUS;
10321da177e4SLinus Torvalds 		}
10333f7cac41SRalf Baechle 		if (__put_user(wval, wva)) {
1034515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10353f7cac41SRalf Baechle 			*fault_addr = wva;
1036515b029dSDavid Daney 			return SIGSEGV;
1037515b029dSDavid Daney 		}
10381da177e4SLinus Torvalds 		break;
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	case cop1_op:
10411da177e4SLinus Torvalds 		switch (MIPSInst_RS(ir)) {
10421da177e4SLinus Torvalds 		case dmfc_op:
104308a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
104408a07904SRalf Baechle 				return SIGILL;
104508a07904SRalf Baechle 
10461da177e4SLinus Torvalds 			/* copregister fs -> gpr[rt] */
10471da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
10481da177e4SLinus Torvalds 				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
10491da177e4SLinus Torvalds 					MIPSInst_RD(ir));
10501da177e4SLinus Torvalds 			}
10511da177e4SLinus Torvalds 			break;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 		case dmtc_op:
105408a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
105508a07904SRalf Baechle 				return SIGILL;
105608a07904SRalf Baechle 
10571da177e4SLinus Torvalds 			/* copregister fs <- rt */
10581da177e4SLinus Torvalds 			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
10591da177e4SLinus Torvalds 			break;
10601da177e4SLinus Torvalds 
10611ac94400SLeonid Yegoshin 		case mfhc_op:
10621ac94400SLeonid Yegoshin 			if (!cpu_has_mips_r2)
10631ac94400SLeonid Yegoshin 				goto sigill;
10641ac94400SLeonid Yegoshin 
10651ac94400SLeonid Yegoshin 			/* copregister rd -> gpr[rt] */
10661ac94400SLeonid Yegoshin 			if (MIPSInst_RT(ir) != 0) {
10671ac94400SLeonid Yegoshin 				SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
10681ac94400SLeonid Yegoshin 					MIPSInst_RD(ir));
10691ac94400SLeonid Yegoshin 			}
10701ac94400SLeonid Yegoshin 			break;
10711ac94400SLeonid Yegoshin 
10721ac94400SLeonid Yegoshin 		case mthc_op:
10731ac94400SLeonid Yegoshin 			if (!cpu_has_mips_r2)
10741ac94400SLeonid Yegoshin 				goto sigill;
10751ac94400SLeonid Yegoshin 
10761ac94400SLeonid Yegoshin 			/* copregister rd <- gpr[rt] */
10771ac94400SLeonid Yegoshin 			SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
10781ac94400SLeonid Yegoshin 			break;
10791ac94400SLeonid Yegoshin 
10801da177e4SLinus Torvalds 		case mfc_op:
10811da177e4SLinus Torvalds 			/* copregister rd -> gpr[rt] */
10821da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
10831da177e4SLinus Torvalds 				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
10841da177e4SLinus Torvalds 					MIPSInst_RD(ir));
10851da177e4SLinus Torvalds 			}
10861da177e4SLinus Torvalds 			break;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 		case mtc_op:
10891da177e4SLinus Torvalds 			/* copregister rd <- rt */
10901da177e4SLinus Torvalds 			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
10911da177e4SLinus Torvalds 			break;
10921da177e4SLinus Torvalds 
10933f7cac41SRalf Baechle 		case cfc_op:
10941da177e4SLinus Torvalds 			/* cop control register rd -> gpr[rt] */
1095d4f5b088SMaciej W. Rozycki 			cop1_cfc(xcp, ctx, ir);
10961da177e4SLinus Torvalds 			break;
10971da177e4SLinus Torvalds 
10983f7cac41SRalf Baechle 		case ctc_op:
10991da177e4SLinus Torvalds 			/* copregister rd <- rt */
1100d4f5b088SMaciej W. Rozycki 			cop1_ctc(xcp, ctx, ir);
11011da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
11021da177e4SLinus Torvalds 				return SIGFPE;
11031da177e4SLinus Torvalds 			}
11041da177e4SLinus Torvalds 			break;
11051da177e4SLinus Torvalds 
11063f7cac41SRalf Baechle 		case bc_op:
1107e7e9cae5SRalf Baechle 			if (delay_slot(xcp))
11081da177e4SLinus Torvalds 				return SIGILL;
11091da177e4SLinus Torvalds 
111008a07904SRalf Baechle 			if (cpu_has_mips_4_5_r)
111108a07904SRalf Baechle 				cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
111208a07904SRalf Baechle 			else
111308a07904SRalf Baechle 				cbit = FPU_CSR_COND;
111408a07904SRalf Baechle 			cond = ctx->fcr31 & cbit;
111508a07904SRalf Baechle 
11163f7cac41SRalf Baechle 			likely = 0;
11171da177e4SLinus Torvalds 			switch (MIPSInst_RT(ir) & 3) {
11181da177e4SLinus Torvalds 			case bcfl_op:
11192d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
11201da177e4SLinus Torvalds 					likely = 1;
11212d83fea7SMaciej W. Rozycki 				/* Fall through */
11221da177e4SLinus Torvalds 			case bcf_op:
11231da177e4SLinus Torvalds 				cond = !cond;
11241da177e4SLinus Torvalds 				break;
11251da177e4SLinus Torvalds 			case bctl_op:
11262d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
11271da177e4SLinus Torvalds 					likely = 1;
11282d83fea7SMaciej W. Rozycki 				/* Fall through */
11291da177e4SLinus Torvalds 			case bct_op:
11301da177e4SLinus Torvalds 				break;
11311da177e4SLinus Torvalds 			}
11321da177e4SLinus Torvalds 
1133e7e9cae5SRalf Baechle 			set_delay_slot(xcp);
11341da177e4SLinus Torvalds 			if (cond) {
11353f7cac41SRalf Baechle 				/*
11363f7cac41SRalf Baechle 				 * Branch taken: emulate dslot instruction
11371da177e4SLinus Torvalds 				 */
11389ab4471cSMaciej W. Rozycki 				unsigned long bcpc;
11399ab4471cSMaciej W. Rozycki 
11409ab4471cSMaciej W. Rozycki 				/*
11419ab4471cSMaciej W. Rozycki 				 * Remember EPC at the branch to point back
11429ab4471cSMaciej W. Rozycki 				 * at so that any delay-slot instruction
11439ab4471cSMaciej W. Rozycki 				 * signal is not silently ignored.
11449ab4471cSMaciej W. Rozycki 				 */
11459ab4471cSMaciej W. Rozycki 				bcpc = xcp->cp0_epc;
1146102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
11471da177e4SLinus Torvalds 
1148102cedc3SLeonid Yegoshin 				contpc = MIPSInst_SIMM(ir);
1149102cedc3SLeonid Yegoshin 				ir = dec_insn.next_insn;
1150102cedc3SLeonid Yegoshin 				if (dec_insn.micro_mips_mode) {
1151102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 1));
1152102cedc3SLeonid Yegoshin 
1153102cedc3SLeonid Yegoshin 					/* If 16-bit instruction, not FPU. */
1154102cedc3SLeonid Yegoshin 					if ((dec_insn.next_pc_inc == 2) ||
1155102cedc3SLeonid Yegoshin 						(microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1156102cedc3SLeonid Yegoshin 
1157102cedc3SLeonid Yegoshin 						/*
1158102cedc3SLeonid Yegoshin 						 * Since this instruction will
1159102cedc3SLeonid Yegoshin 						 * be put on the stack with
1160102cedc3SLeonid Yegoshin 						 * 32-bit words, get around
1161102cedc3SLeonid Yegoshin 						 * this problem by putting a
1162102cedc3SLeonid Yegoshin 						 * NOP16 as the second one.
1163102cedc3SLeonid Yegoshin 						 */
1164102cedc3SLeonid Yegoshin 						if (dec_insn.next_pc_inc == 2)
1165102cedc3SLeonid Yegoshin 							ir = (ir & (~0xffff)) | MM_NOP16;
1166102cedc3SLeonid Yegoshin 
1167102cedc3SLeonid Yegoshin 						/*
1168102cedc3SLeonid Yegoshin 						 * Single step the non-CP1
1169102cedc3SLeonid Yegoshin 						 * instruction in the dslot.
1170102cedc3SLeonid Yegoshin 						 */
11719ab4471cSMaciej W. Rozycki 						sig = mips_dsemul(xcp, ir,
11729ab4471cSMaciej W. Rozycki 								  contpc);
11739ab4471cSMaciej W. Rozycki 						if (sig)
11749ab4471cSMaciej W. Rozycki 							xcp->cp0_epc = bcpc;
11759ab4471cSMaciej W. Rozycki 						/*
11769ab4471cSMaciej W. Rozycki 						 * SIGILL forces out of
11779ab4471cSMaciej W. Rozycki 						 * the emulation loop.
11789ab4471cSMaciej W. Rozycki 						 */
11799ab4471cSMaciej W. Rozycki 						return sig ? sig : SIGILL;
1180515b029dSDavid Daney 					}
1181102cedc3SLeonid Yegoshin 				} else
1182102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 2));
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 				switch (MIPSInst_OPCODE(ir)) {
11851da177e4SLinus Torvalds 				case lwc1_op:
11861da177e4SLinus Torvalds 				case swc1_op:
118708a07904SRalf Baechle 					goto emul;
11883f7cac41SRalf Baechle 
11891da177e4SLinus Torvalds 				case ldc1_op:
11901da177e4SLinus Torvalds 				case sdc1_op:
11912d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_2_3_4_5_r)
119208a07904SRalf Baechle 						goto emul;
119308a07904SRalf Baechle 
11949ab4471cSMaciej W. Rozycki 					goto bc_sigill;
11953f7cac41SRalf Baechle 
11961da177e4SLinus Torvalds 				case cop1_op:
119708a07904SRalf Baechle 					goto emul;
11983f7cac41SRalf Baechle 
11991da177e4SLinus Torvalds 				case cop1x_op:
12002d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_4_5_64_r2_r6)
12011da177e4SLinus Torvalds 						/* its one of ours */
12021da177e4SLinus Torvalds 						goto emul;
120308a07904SRalf Baechle 
12049ab4471cSMaciej W. Rozycki 					goto bc_sigill;
12053f7cac41SRalf Baechle 
12061da177e4SLinus Torvalds 				case spec_op:
12072d83fea7SMaciej W. Rozycki 					switch (MIPSInst_FUNC(ir)) {
12082d83fea7SMaciej W. Rozycki 					case movc_op:
12092d83fea7SMaciej W. Rozycki 						if (cpu_has_mips_4_5_r)
12101da177e4SLinus Torvalds 							goto emul;
12112d83fea7SMaciej W. Rozycki 
12129ab4471cSMaciej W. Rozycki 						goto bc_sigill;
12132d83fea7SMaciej W. Rozycki 					}
12141da177e4SLinus Torvalds 					break;
12159ab4471cSMaciej W. Rozycki 
12169ab4471cSMaciej W. Rozycki 				bc_sigill:
12179ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
12189ab4471cSMaciej W. Rozycki 					return SIGILL;
12191da177e4SLinus Torvalds 				}
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 				/*
12221da177e4SLinus Torvalds 				 * Single step the non-cp1
12231da177e4SLinus Torvalds 				 * instruction in the dslot
12241da177e4SLinus Torvalds 				 */
12259ab4471cSMaciej W. Rozycki 				sig = mips_dsemul(xcp, ir, contpc);
12269ab4471cSMaciej W. Rozycki 				if (sig)
12279ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
12289ab4471cSMaciej W. Rozycki 				/* SIGILL forces out of the emulation loop.  */
12299ab4471cSMaciej W. Rozycki 				return sig ? sig : SIGILL;
12303f7cac41SRalf Baechle 			} else if (likely) {	/* branch not taken */
12311da177e4SLinus Torvalds 				/*
12321da177e4SLinus Torvalds 				 * branch likely nullifies
12331da177e4SLinus Torvalds 				 * dslot if not taken
12341da177e4SLinus Torvalds 				 */
1235102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
1236102cedc3SLeonid Yegoshin 				contpc += dec_insn.pc_inc;
12371da177e4SLinus Torvalds 				/*
12381da177e4SLinus Torvalds 				 * else continue & execute
12391da177e4SLinus Torvalds 				 * dslot as normal insn
12401da177e4SLinus Torvalds 				 */
12411da177e4SLinus Torvalds 			}
12421da177e4SLinus Torvalds 			break;
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 		default:
12451da177e4SLinus Torvalds 			if (!(MIPSInst_RS(ir) & 0x10))
12461da177e4SLinus Torvalds 				return SIGILL;
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 			/* a real fpu computation instruction */
12491da177e4SLinus Torvalds 			if ((sig = fpu_emu(xcp, ctx, ir)))
12501da177e4SLinus Torvalds 				return sig;
12511da177e4SLinus Torvalds 		}
12521da177e4SLinus Torvalds 		break;
12531da177e4SLinus Torvalds 
12543f7cac41SRalf Baechle 	case cop1x_op:
12552d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_4_5_64_r2_r6)
125608a07904SRalf Baechle 			return SIGILL;
125708a07904SRalf Baechle 
125808a07904SRalf Baechle 		sig = fpux_emu(xcp, ctx, ir, fault_addr);
1259515b029dSDavid Daney 		if (sig)
12601da177e4SLinus Torvalds 			return sig;
12611da177e4SLinus Torvalds 		break;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	case spec_op:
126408a07904SRalf Baechle 		if (!cpu_has_mips_4_5_r)
126508a07904SRalf Baechle 			return SIGILL;
126608a07904SRalf Baechle 
12671da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != movc_op)
12681da177e4SLinus Torvalds 			return SIGILL;
12691da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
12701da177e4SLinus Torvalds 		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
12711da177e4SLinus Torvalds 			xcp->regs[MIPSInst_RD(ir)] =
12721da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RS(ir)];
12731da177e4SLinus Torvalds 		break;
12741da177e4SLinus Torvalds 	default:
12751ac94400SLeonid Yegoshin sigill:
12761da177e4SLinus Torvalds 		return SIGILL;
12771da177e4SLinus Torvalds 	}
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	/* we did it !! */
1280e70dfc10SAtsushi Nemoto 	xcp->cp0_epc = contpc;
1281e7e9cae5SRalf Baechle 	clear_delay_slot(xcp);
1282333d1f67SRalf Baechle 
12831da177e4SLinus Torvalds 	return 0;
12841da177e4SLinus Torvalds }
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds /*
12871da177e4SLinus Torvalds  * Conversion table from MIPS compare ops 48-63
12881da177e4SLinus Torvalds  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
12891da177e4SLinus Torvalds  */
12901da177e4SLinus Torvalds static const unsigned char cmptab[8] = {
12911da177e4SLinus Torvalds 	0,			/* cmp_0 (sig) cmp_sf */
12921da177e4SLinus Torvalds 	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
12931da177e4SLinus Torvalds 	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
12941da177e4SLinus Torvalds 	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
12951da177e4SLinus Torvalds 	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
12961da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
12971da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
12981da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
12991da177e4SLinus Torvalds };
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds /*
13031da177e4SLinus Torvalds  * Additional MIPS4 instructions
13041da177e4SLinus Torvalds  */
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3)					\
130747fa0c02SRalf Baechle static union ieee754##p fpemu_##p##_##name(union ieee754##p r,		\
130847fa0c02SRalf Baechle 	union ieee754##p s, union ieee754##p t)				\
13091da177e4SLinus Torvalds {									\
1310cd21dfcfSRalf Baechle 	struct _ieee754_csr ieee754_csr_save;				\
13111da177e4SLinus Torvalds 	s = f1(s, t);							\
13121da177e4SLinus Torvalds 	ieee754_csr_save = ieee754_csr;					\
13131da177e4SLinus Torvalds 	s = f2(s, r);							\
13141da177e4SLinus Torvalds 	ieee754_csr_save.cx |= ieee754_csr.cx;				\
13151da177e4SLinus Torvalds 	ieee754_csr_save.sx |= ieee754_csr.sx;				\
13161da177e4SLinus Torvalds 	s = f3(s);							\
13171da177e4SLinus Torvalds 	ieee754_csr.cx |= ieee754_csr_save.cx;				\
13181da177e4SLinus Torvalds 	ieee754_csr.sx |= ieee754_csr_save.sx;				\
13191da177e4SLinus Torvalds 	return s;							\
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds 
13222209bcb1SRalf Baechle static union ieee754dp fpemu_dp_recip(union ieee754dp d)
13231da177e4SLinus Torvalds {
13241da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), d);
13251da177e4SLinus Torvalds }
13261da177e4SLinus Torvalds 
13272209bcb1SRalf Baechle static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
13281da177e4SLinus Torvalds {
13291da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
13322209bcb1SRalf Baechle static union ieee754sp fpemu_sp_recip(union ieee754sp s)
13331da177e4SLinus Torvalds {
13341da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), s);
13351da177e4SLinus Torvalds }
13361da177e4SLinus Torvalds 
13372209bcb1SRalf Baechle static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
13381da177e4SLinus Torvalds {
13391da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
13401da177e4SLinus Torvalds }
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
13431da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
13441da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
13451da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
13461da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
13471da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
13481da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
13491da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
13501da177e4SLinus Torvalds 
1351eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1352515b029dSDavid Daney 	mips_instruction ir, void *__user *fault_addr)
13531da177e4SLinus Torvalds {
13541da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
13551da177e4SLinus Torvalds 
1356b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1xops);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	switch (MIPSInst_FMA_FFMT(ir)) {
13591da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
13601da177e4SLinus Torvalds 
13612209bcb1SRalf Baechle 		union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
13622209bcb1SRalf Baechle 		union ieee754sp fd, fr, fs, ft;
13633fccc015SRalf Baechle 		u32 __user *va;
13641da177e4SLinus Torvalds 		u32 val;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
13671da177e4SLinus Torvalds 		case lwxc1_op:
13683fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
13691da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
13701da177e4SLinus Torvalds 
1371b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1372515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1373b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1374515b029dSDavid Daney 				*fault_addr = va;
13751da177e4SLinus Torvalds 				return SIGBUS;
13761da177e4SLinus Torvalds 			}
1377515b029dSDavid Daney 			if (__get_user(val, va)) {
1378515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1379515b029dSDavid Daney 				*fault_addr = va;
1380515b029dSDavid Daney 				return SIGSEGV;
1381515b029dSDavid Daney 			}
13821da177e4SLinus Torvalds 			SITOREG(val, MIPSInst_FD(ir));
13831da177e4SLinus Torvalds 			break;
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 		case swxc1_op:
13863fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
13871da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
13881da177e4SLinus Torvalds 
1389b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 			SIFROMREG(val, MIPSInst_FS(ir));
1392515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1393515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1394515b029dSDavid Daney 				*fault_addr = va;
1395515b029dSDavid Daney 				return SIGBUS;
1396515b029dSDavid Daney 			}
13971da177e4SLinus Torvalds 			if (put_user(val, va)) {
1398b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1399515b029dSDavid Daney 				*fault_addr = va;
1400515b029dSDavid Daney 				return SIGSEGV;
14011da177e4SLinus Torvalds 			}
14021da177e4SLinus Torvalds 			break;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 		case madd_s_op:
14051da177e4SLinus Torvalds 			handler = fpemu_sp_madd;
14061da177e4SLinus Torvalds 			goto scoptop;
14071da177e4SLinus Torvalds 		case msub_s_op:
14081da177e4SLinus Torvalds 			handler = fpemu_sp_msub;
14091da177e4SLinus Torvalds 			goto scoptop;
14101da177e4SLinus Torvalds 		case nmadd_s_op:
14111da177e4SLinus Torvalds 			handler = fpemu_sp_nmadd;
14121da177e4SLinus Torvalds 			goto scoptop;
14131da177e4SLinus Torvalds 		case nmsub_s_op:
14141da177e4SLinus Torvalds 			handler = fpemu_sp_nmsub;
14151da177e4SLinus Torvalds 			goto scoptop;
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 		      scoptop:
14181da177e4SLinus Torvalds 			SPFROMREG(fr, MIPSInst_FR(ir));
14191da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
14201da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
14211da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
14221da177e4SLinus Torvalds 			SPTOREG(fd, MIPSInst_FD(ir));
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 		      copcsr:
1425c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1426c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
14271da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1428c4103526SDeng-Cheng Zhu 			}
1429c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1430c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
14311da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1432c4103526SDeng-Cheng Zhu 			}
1433c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1434c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
14351da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1436c4103526SDeng-Cheng Zhu 			}
1437c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1438c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
14391da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1440c4103526SDeng-Cheng Zhu 			}
14411da177e4SLinus Torvalds 
14421da177e4SLinus Torvalds 			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
14431da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
14443f7cac41SRalf Baechle 				/*printk ("SIGFPE: FPU csr = %08x\n",
14451da177e4SLinus Torvalds 				   ctx->fcr31); */
14461da177e4SLinus Torvalds 				return SIGFPE;
14471da177e4SLinus Torvalds 			}
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 			break;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 		default:
14521da177e4SLinus Torvalds 			return SIGILL;
14531da177e4SLinus Torvalds 		}
14541da177e4SLinus Torvalds 		break;
14551da177e4SLinus Torvalds 	}
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	case d_fmt:{		/* 1 */
14582209bcb1SRalf Baechle 		union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
14592209bcb1SRalf Baechle 		union ieee754dp fd, fr, fs, ft;
14603fccc015SRalf Baechle 		u64 __user *va;
14611da177e4SLinus Torvalds 		u64 val;
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
14641da177e4SLinus Torvalds 		case ldxc1_op:
14653fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14661da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14671da177e4SLinus Torvalds 
1468b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1469515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1470b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1471515b029dSDavid Daney 				*fault_addr = va;
14721da177e4SLinus Torvalds 				return SIGBUS;
14731da177e4SLinus Torvalds 			}
1474515b029dSDavid Daney 			if (__get_user(val, va)) {
1475515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1476515b029dSDavid Daney 				*fault_addr = va;
1477515b029dSDavid Daney 				return SIGSEGV;
1478515b029dSDavid Daney 			}
14791da177e4SLinus Torvalds 			DITOREG(val, MIPSInst_FD(ir));
14801da177e4SLinus Torvalds 			break;
14811da177e4SLinus Torvalds 
14821da177e4SLinus Torvalds 		case sdxc1_op:
14833fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14841da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14851da177e4SLinus Torvalds 
1486b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
14871da177e4SLinus Torvalds 			DIFROMREG(val, MIPSInst_FS(ir));
1488515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1489b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1490515b029dSDavid Daney 				*fault_addr = va;
14911da177e4SLinus Torvalds 				return SIGBUS;
14921da177e4SLinus Torvalds 			}
1493515b029dSDavid Daney 			if (__put_user(val, va)) {
1494515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1495515b029dSDavid Daney 				*fault_addr = va;
1496515b029dSDavid Daney 				return SIGSEGV;
1497515b029dSDavid Daney 			}
14981da177e4SLinus Torvalds 			break;
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 		case madd_d_op:
15011da177e4SLinus Torvalds 			handler = fpemu_dp_madd;
15021da177e4SLinus Torvalds 			goto dcoptop;
15031da177e4SLinus Torvalds 		case msub_d_op:
15041da177e4SLinus Torvalds 			handler = fpemu_dp_msub;
15051da177e4SLinus Torvalds 			goto dcoptop;
15061da177e4SLinus Torvalds 		case nmadd_d_op:
15071da177e4SLinus Torvalds 			handler = fpemu_dp_nmadd;
15081da177e4SLinus Torvalds 			goto dcoptop;
15091da177e4SLinus Torvalds 		case nmsub_d_op:
15101da177e4SLinus Torvalds 			handler = fpemu_dp_nmsub;
15111da177e4SLinus Torvalds 			goto dcoptop;
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 		      dcoptop:
15141da177e4SLinus Torvalds 			DPFROMREG(fr, MIPSInst_FR(ir));
15151da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
15161da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
15171da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
15181da177e4SLinus Torvalds 			DPTOREG(fd, MIPSInst_FD(ir));
15191da177e4SLinus Torvalds 			goto copcsr;
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 		default:
15221da177e4SLinus Torvalds 			return SIGILL;
15231da177e4SLinus Torvalds 		}
15241da177e4SLinus Torvalds 		break;
15251da177e4SLinus Torvalds 	}
15261da177e4SLinus Torvalds 
152751061b88SDeng-Cheng Zhu 	case 0x3:
152851061b88SDeng-Cheng Zhu 		if (MIPSInst_FUNC(ir) != pfetch_op)
15291da177e4SLinus Torvalds 			return SIGILL;
153051061b88SDeng-Cheng Zhu 
15311da177e4SLinus Torvalds 		/* ignore prefx operation */
15321da177e4SLinus Torvalds 		break;
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	default:
15351da177e4SLinus Torvalds 		return SIGILL;
15361da177e4SLinus Torvalds 	}
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	return 0;
15391da177e4SLinus Torvalds }
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds /*
15441da177e4SLinus Torvalds  * Emulate a single COP1 arithmetic instruction.
15451da177e4SLinus Torvalds  */
1546eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
15471da177e4SLinus Torvalds 	mips_instruction ir)
15481da177e4SLinus Torvalds {
15491da177e4SLinus Torvalds 	int rfmt;		/* resulting format */
15501da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
15513f7cac41SRalf Baechle 	unsigned int oldrm;
15523f7cac41SRalf Baechle 	unsigned int cbit;
15531da177e4SLinus Torvalds 	unsigned cond;
15541da177e4SLinus Torvalds 	union {
15552209bcb1SRalf Baechle 		union ieee754dp d;
15562209bcb1SRalf Baechle 		union ieee754sp s;
15571da177e4SLinus Torvalds 		int w;
15581da177e4SLinus Torvalds 		s64 l;
15591da177e4SLinus Torvalds 	} rv;			/* resulting value */
15603f7cac41SRalf Baechle 	u64 bits;
15611da177e4SLinus Torvalds 
1562b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1ops);
15631da177e4SLinus Torvalds 	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
15641da177e4SLinus Torvalds 	case s_fmt: {		/* 0 */
15651da177e4SLinus Torvalds 		union {
15662209bcb1SRalf Baechle 			union ieee754sp(*b) (union ieee754sp, union ieee754sp);
15672209bcb1SRalf Baechle 			union ieee754sp(*u) (union ieee754sp);
15681da177e4SLinus Torvalds 		} handler;
15693f7cac41SRalf Baechle 		union ieee754sp fs, ft;
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
15721da177e4SLinus Torvalds 			/* binary ops */
15731da177e4SLinus Torvalds 		case fadd_op:
15741da177e4SLinus Torvalds 			handler.b = ieee754sp_add;
15751da177e4SLinus Torvalds 			goto scopbop;
15761da177e4SLinus Torvalds 		case fsub_op:
15771da177e4SLinus Torvalds 			handler.b = ieee754sp_sub;
15781da177e4SLinus Torvalds 			goto scopbop;
15791da177e4SLinus Torvalds 		case fmul_op:
15801da177e4SLinus Torvalds 			handler.b = ieee754sp_mul;
15811da177e4SLinus Torvalds 			goto scopbop;
15821da177e4SLinus Torvalds 		case fdiv_op:
15831da177e4SLinus Torvalds 			handler.b = ieee754sp_div;
15841da177e4SLinus Torvalds 			goto scopbop;
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 			/* unary  ops */
15871da177e4SLinus Torvalds 		case fsqrt_op:
15882d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
158908a07904SRalf Baechle 				return SIGILL;
159008a07904SRalf Baechle 
15911da177e4SLinus Torvalds 			handler.u = ieee754sp_sqrt;
15921da177e4SLinus Torvalds 			goto scopuop;
15933f7cac41SRalf Baechle 
159408a07904SRalf Baechle 		/*
159508a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
159608a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
159708a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
159808a07904SRalf Baechle 		 */
15991da177e4SLinus Torvalds 		case frsqrt_op:
16002d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
160108a07904SRalf Baechle 				return SIGILL;
160208a07904SRalf Baechle 
16031da177e4SLinus Torvalds 			handler.u = fpemu_sp_rsqrt;
16041da177e4SLinus Torvalds 			goto scopuop;
16053f7cac41SRalf Baechle 
16061da177e4SLinus Torvalds 		case frecip_op:
16072d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
160808a07904SRalf Baechle 				return SIGILL;
160908a07904SRalf Baechle 
16101da177e4SLinus Torvalds 			handler.u = fpemu_sp_recip;
16111da177e4SLinus Torvalds 			goto scopuop;
161208a07904SRalf Baechle 
16131da177e4SLinus Torvalds 		case fmovc_op:
161408a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
161508a07904SRalf Baechle 				return SIGILL;
161608a07904SRalf Baechle 
16171da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
16181da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
16191da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
16201da177e4SLinus Torvalds 				return 0;
16211da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
16221da177e4SLinus Torvalds 			break;
16233f7cac41SRalf Baechle 
16241da177e4SLinus Torvalds 		case fmovz_op:
162508a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
162608a07904SRalf Baechle 				return SIGILL;
162708a07904SRalf Baechle 
16281da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
16291da177e4SLinus Torvalds 				return 0;
16301da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
16311da177e4SLinus Torvalds 			break;
16323f7cac41SRalf Baechle 
16331da177e4SLinus Torvalds 		case fmovn_op:
163408a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
163508a07904SRalf Baechle 				return SIGILL;
163608a07904SRalf Baechle 
16371da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
16381da177e4SLinus Torvalds 				return 0;
16391da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
16401da177e4SLinus Torvalds 			break;
16413f7cac41SRalf Baechle 
16421da177e4SLinus Torvalds 		case fabs_op:
16431da177e4SLinus Torvalds 			handler.u = ieee754sp_abs;
16441da177e4SLinus Torvalds 			goto scopuop;
16453f7cac41SRalf Baechle 
16461da177e4SLinus Torvalds 		case fneg_op:
16471da177e4SLinus Torvalds 			handler.u = ieee754sp_neg;
16481da177e4SLinus Torvalds 			goto scopuop;
16493f7cac41SRalf Baechle 
16501da177e4SLinus Torvalds 		case fmov_op:
16511da177e4SLinus Torvalds 			/* an easy one */
16521da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
16531da177e4SLinus Torvalds 			goto copcsr;
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 			/* binary op on handler */
16561da177e4SLinus Torvalds scopbop:
16571da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
16581da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 			rv.s = (*handler.b) (fs, ft);
16611da177e4SLinus Torvalds 			goto copcsr;
16621da177e4SLinus Torvalds scopuop:
16631da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
16641da177e4SLinus Torvalds 			rv.s = (*handler.u) (fs);
16651da177e4SLinus Torvalds 			goto copcsr;
16661da177e4SLinus Torvalds copcsr:
1667c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1668c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
16691da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1670c4103526SDeng-Cheng Zhu 			}
1671c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1672c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
16731da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1674c4103526SDeng-Cheng Zhu 			}
1675c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1676c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
16771da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1678c4103526SDeng-Cheng Zhu 			}
1679c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1680c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
16811da177e4SLinus Torvalds 				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1682c4103526SDeng-Cheng Zhu 			}
1683c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1684c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
16851da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1686c4103526SDeng-Cheng Zhu 			}
16871da177e4SLinus Torvalds 			break;
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 			/* unary conv ops */
16901da177e4SLinus Torvalds 		case fcvts_op:
16911da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
16921da177e4SLinus Torvalds 
16933f7cac41SRalf Baechle 		case fcvtd_op:
16941da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
16951da177e4SLinus Torvalds 			rv.d = ieee754dp_fsp(fs);
16961da177e4SLinus Torvalds 			rfmt = d_fmt;
16971da177e4SLinus Torvalds 			goto copcsr;
16981da177e4SLinus Torvalds 
16993f7cac41SRalf Baechle 		case fcvtw_op:
17001da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
17011da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
17021da177e4SLinus Torvalds 			rfmt = w_fmt;
17031da177e4SLinus Torvalds 			goto copcsr;
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 		case fround_op:
17061da177e4SLinus Torvalds 		case ftrunc_op:
17071da177e4SLinus Torvalds 		case fceil_op:
17083f7cac41SRalf Baechle 		case ffloor_op:
17092d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
171008a07904SRalf Baechle 				return SIGILL;
171108a07904SRalf Baechle 
17123f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
17131da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
17142cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
17151da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
17161da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
17171da177e4SLinus Torvalds 			rfmt = w_fmt;
17181da177e4SLinus Torvalds 			goto copcsr;
17191da177e4SLinus Torvalds 
17203f7cac41SRalf Baechle 		case fcvtl_op:
17212d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
172208a07904SRalf Baechle 				return SIGILL;
172308a07904SRalf Baechle 
17241da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
17251da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
17261da177e4SLinus Torvalds 			rfmt = l_fmt;
17271da177e4SLinus Torvalds 			goto copcsr;
17281da177e4SLinus Torvalds 
17291da177e4SLinus Torvalds 		case froundl_op:
17301da177e4SLinus Torvalds 		case ftruncl_op:
17311da177e4SLinus Torvalds 		case fceill_op:
17323f7cac41SRalf Baechle 		case ffloorl_op:
17332d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
173408a07904SRalf Baechle 				return SIGILL;
173508a07904SRalf Baechle 
17363f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
17371da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
17382cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
17391da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
17401da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
17411da177e4SLinus Torvalds 			rfmt = l_fmt;
17421da177e4SLinus Torvalds 			goto copcsr;
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 		default:
17451da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
17461da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
17472209bcb1SRalf Baechle 				union ieee754sp fs, ft;
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
17501da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
17511da177e4SLinus Torvalds 				rv.w = ieee754sp_cmp(fs, ft,
17521da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
17531da177e4SLinus Torvalds 				rfmt = -1;
17541da177e4SLinus Torvalds 				if ((cmpop & 0x8) && ieee754_cxtest
17551da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
17561da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
17571da177e4SLinus Torvalds 				else
17581da177e4SLinus Torvalds 					goto copcsr;
17591da177e4SLinus Torvalds 
17603f7cac41SRalf Baechle 			} else
17611da177e4SLinus Torvalds 				return SIGILL;
17621da177e4SLinus Torvalds 			break;
17631da177e4SLinus Torvalds 		}
17641da177e4SLinus Torvalds 		break;
17651da177e4SLinus Torvalds 	}
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 	case d_fmt: {
17683f7cac41SRalf Baechle 		union ieee754dp fs, ft;
17691da177e4SLinus Torvalds 		union {
17702209bcb1SRalf Baechle 			union ieee754dp(*b) (union ieee754dp, union ieee754dp);
17712209bcb1SRalf Baechle 			union ieee754dp(*u) (union ieee754dp);
17721da177e4SLinus Torvalds 		} handler;
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
17751da177e4SLinus Torvalds 			/* binary ops */
17761da177e4SLinus Torvalds 		case fadd_op:
17771da177e4SLinus Torvalds 			handler.b = ieee754dp_add;
17781da177e4SLinus Torvalds 			goto dcopbop;
17791da177e4SLinus Torvalds 		case fsub_op:
17801da177e4SLinus Torvalds 			handler.b = ieee754dp_sub;
17811da177e4SLinus Torvalds 			goto dcopbop;
17821da177e4SLinus Torvalds 		case fmul_op:
17831da177e4SLinus Torvalds 			handler.b = ieee754dp_mul;
17841da177e4SLinus Torvalds 			goto dcopbop;
17851da177e4SLinus Torvalds 		case fdiv_op:
17861da177e4SLinus Torvalds 			handler.b = ieee754dp_div;
17871da177e4SLinus Torvalds 			goto dcopbop;
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds 			/* unary  ops */
17901da177e4SLinus Torvalds 		case fsqrt_op:
179108a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
179208a07904SRalf Baechle 				return SIGILL;
179308a07904SRalf Baechle 
17941da177e4SLinus Torvalds 			handler.u = ieee754dp_sqrt;
17951da177e4SLinus Torvalds 			goto dcopuop;
179608a07904SRalf Baechle 		/*
179708a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
179808a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
179908a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
180008a07904SRalf Baechle 		 */
18011da177e4SLinus Torvalds 		case frsqrt_op:
18022d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
180308a07904SRalf Baechle 				return SIGILL;
180408a07904SRalf Baechle 
18051da177e4SLinus Torvalds 			handler.u = fpemu_dp_rsqrt;
18061da177e4SLinus Torvalds 			goto dcopuop;
18071da177e4SLinus Torvalds 		case frecip_op:
18082d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
180908a07904SRalf Baechle 				return SIGILL;
181008a07904SRalf Baechle 
18111da177e4SLinus Torvalds 			handler.u = fpemu_dp_recip;
18121da177e4SLinus Torvalds 			goto dcopuop;
18131da177e4SLinus Torvalds 		case fmovc_op:
181408a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
181508a07904SRalf Baechle 				return SIGILL;
181608a07904SRalf Baechle 
18171da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
18181da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
18191da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
18201da177e4SLinus Torvalds 				return 0;
18211da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
18221da177e4SLinus Torvalds 			break;
18231da177e4SLinus Torvalds 		case fmovz_op:
182408a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
182508a07904SRalf Baechle 				return SIGILL;
182608a07904SRalf Baechle 
18271da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
18281da177e4SLinus Torvalds 				return 0;
18291da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
18301da177e4SLinus Torvalds 			break;
18311da177e4SLinus Torvalds 		case fmovn_op:
183208a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
183308a07904SRalf Baechle 				return SIGILL;
183408a07904SRalf Baechle 
18351da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
18361da177e4SLinus Torvalds 				return 0;
18371da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
18381da177e4SLinus Torvalds 			break;
18391da177e4SLinus Torvalds 		case fabs_op:
18401da177e4SLinus Torvalds 			handler.u = ieee754dp_abs;
18411da177e4SLinus Torvalds 			goto dcopuop;
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds 		case fneg_op:
18441da177e4SLinus Torvalds 			handler.u = ieee754dp_neg;
18451da177e4SLinus Torvalds 			goto dcopuop;
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds 		case fmov_op:
18481da177e4SLinus Torvalds 			/* an easy one */
18491da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
18501da177e4SLinus Torvalds 			goto copcsr;
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 			/* binary op on handler */
18533f7cac41SRalf Baechle dcopbop:
18541da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18551da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds 			rv.d = (*handler.b) (fs, ft);
18581da177e4SLinus Torvalds 			goto copcsr;
18593f7cac41SRalf Baechle dcopuop:
18601da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18611da177e4SLinus Torvalds 			rv.d = (*handler.u) (fs);
18621da177e4SLinus Torvalds 			goto copcsr;
18631da177e4SLinus Torvalds 
18643f7cac41SRalf Baechle 		/*
18653f7cac41SRalf Baechle 		 * unary conv ops
18663f7cac41SRalf Baechle 		 */
18673f7cac41SRalf Baechle 		case fcvts_op:
18681da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18691da177e4SLinus Torvalds 			rv.s = ieee754sp_fdp(fs);
18701da177e4SLinus Torvalds 			rfmt = s_fmt;
18711da177e4SLinus Torvalds 			goto copcsr;
18723f7cac41SRalf Baechle 
18731da177e4SLinus Torvalds 		case fcvtd_op:
18741da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
18751da177e4SLinus Torvalds 
18763f7cac41SRalf Baechle 		case fcvtw_op:
18771da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18781da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);	/* wrong */
18791da177e4SLinus Torvalds 			rfmt = w_fmt;
18801da177e4SLinus Torvalds 			goto copcsr;
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 		case fround_op:
18831da177e4SLinus Torvalds 		case ftrunc_op:
18841da177e4SLinus Torvalds 		case fceil_op:
18853f7cac41SRalf Baechle 		case ffloor_op:
188608a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
188708a07904SRalf Baechle 				return SIGILL;
188808a07904SRalf Baechle 
18893f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
18901da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18912cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
18921da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);
18931da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
18941da177e4SLinus Torvalds 			rfmt = w_fmt;
18951da177e4SLinus Torvalds 			goto copcsr;
18961da177e4SLinus Torvalds 
18973f7cac41SRalf Baechle 		case fcvtl_op:
18982d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
189908a07904SRalf Baechle 				return SIGILL;
190008a07904SRalf Baechle 
19011da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
19021da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
19031da177e4SLinus Torvalds 			rfmt = l_fmt;
19041da177e4SLinus Torvalds 			goto copcsr;
19051da177e4SLinus Torvalds 
19061da177e4SLinus Torvalds 		case froundl_op:
19071da177e4SLinus Torvalds 		case ftruncl_op:
19081da177e4SLinus Torvalds 		case fceill_op:
19093f7cac41SRalf Baechle 		case ffloorl_op:
19102d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
191108a07904SRalf Baechle 				return SIGILL;
191208a07904SRalf Baechle 
19133f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
19141da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
19152cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
19161da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
19171da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
19181da177e4SLinus Torvalds 			rfmt = l_fmt;
19191da177e4SLinus Torvalds 			goto copcsr;
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 		default:
19221da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
19231da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
19242209bcb1SRalf Baechle 				union ieee754dp fs, ft;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
19271da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
19281da177e4SLinus Torvalds 				rv.w = ieee754dp_cmp(fs, ft,
19291da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
19301da177e4SLinus Torvalds 				rfmt = -1;
19311da177e4SLinus Torvalds 				if ((cmpop & 0x8)
19321da177e4SLinus Torvalds 					&&
19331da177e4SLinus Torvalds 					ieee754_cxtest
19341da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
19351da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
19361da177e4SLinus Torvalds 				else
19371da177e4SLinus Torvalds 					goto copcsr;
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 			}
19401da177e4SLinus Torvalds 			else {
19411da177e4SLinus Torvalds 				return SIGILL;
19421da177e4SLinus Torvalds 			}
19431da177e4SLinus Torvalds 			break;
19441da177e4SLinus Torvalds 		}
19451da177e4SLinus Torvalds 		break;
19461da177e4SLinus Torvalds 
19473f7cac41SRalf Baechle 	case w_fmt:
19481da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
19491da177e4SLinus Torvalds 		case fcvts_op:
19501da177e4SLinus Torvalds 			/* convert word to single precision real */
19511da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19521da177e4SLinus Torvalds 			rv.s = ieee754sp_fint(fs.bits);
19531da177e4SLinus Torvalds 			rfmt = s_fmt;
19541da177e4SLinus Torvalds 			goto copcsr;
19551da177e4SLinus Torvalds 		case fcvtd_op:
19561da177e4SLinus Torvalds 			/* convert word to double precision real */
19571da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19581da177e4SLinus Torvalds 			rv.d = ieee754dp_fint(fs.bits);
19591da177e4SLinus Torvalds 			rfmt = d_fmt;
19601da177e4SLinus Torvalds 			goto copcsr;
19611da177e4SLinus Torvalds 		default:
19621da177e4SLinus Torvalds 			return SIGILL;
19631da177e4SLinus Torvalds 		}
19641da177e4SLinus Torvalds 		break;
19651da177e4SLinus Torvalds 	}
19661da177e4SLinus Torvalds 
19673f7cac41SRalf Baechle 	case l_fmt:
196808a07904SRalf Baechle 
19692d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
197008a07904SRalf Baechle 			return SIGILL;
197108a07904SRalf Baechle 
1972bbd426f5SPaul Burton 		DIFROMREG(bits, MIPSInst_FS(ir));
1973bbd426f5SPaul Burton 
19741da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
19751da177e4SLinus Torvalds 		case fcvts_op:
19761da177e4SLinus Torvalds 			/* convert long to single precision real */
1977bbd426f5SPaul Burton 			rv.s = ieee754sp_flong(bits);
19781da177e4SLinus Torvalds 			rfmt = s_fmt;
19791da177e4SLinus Torvalds 			goto copcsr;
19801da177e4SLinus Torvalds 		case fcvtd_op:
19811da177e4SLinus Torvalds 			/* convert long to double precision real */
1982bbd426f5SPaul Burton 			rv.d = ieee754dp_flong(bits);
19831da177e4SLinus Torvalds 			rfmt = d_fmt;
19841da177e4SLinus Torvalds 			goto copcsr;
19851da177e4SLinus Torvalds 		default:
19861da177e4SLinus Torvalds 			return SIGILL;
19871da177e4SLinus Torvalds 		}
19881da177e4SLinus Torvalds 		break;
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 	default:
19911da177e4SLinus Torvalds 		return SIGILL;
19921da177e4SLinus Torvalds 	}
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	/*
19951da177e4SLinus Torvalds 	 * Update the fpu CSR register for this operation.
19961da177e4SLinus Torvalds 	 * If an exception is required, generate a tidy SIGFPE exception,
19971da177e4SLinus Torvalds 	 * without updating the result register.
19981da177e4SLinus Torvalds 	 * Note: cause exception bits do not accumulate, they are rewritten
19991da177e4SLinus Torvalds 	 * for each op; only the flag/sticky bits accumulate.
20001da177e4SLinus Torvalds 	 */
20011da177e4SLinus Torvalds 	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
20021da177e4SLinus Torvalds 	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
20033f7cac41SRalf Baechle 		/*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
20041da177e4SLinus Torvalds 		return SIGFPE;
20051da177e4SLinus Torvalds 	}
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds 	/*
20081da177e4SLinus Torvalds 	 * Now we can safely write the result back to the register file.
20091da177e4SLinus Torvalds 	 */
20101da177e4SLinus Torvalds 	switch (rfmt) {
201108a07904SRalf Baechle 	case -1:
201208a07904SRalf Baechle 
201308a07904SRalf Baechle 		if (cpu_has_mips_4_5_r)
2014c3b9b945SRob Kendrick 			cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
20151da177e4SLinus Torvalds 		else
201608a07904SRalf Baechle 			cbit = FPU_CSR_COND;
201708a07904SRalf Baechle 		if (rv.w)
201808a07904SRalf Baechle 			ctx->fcr31 |= cbit;
201908a07904SRalf Baechle 		else
202008a07904SRalf Baechle 			ctx->fcr31 &= ~cbit;
20211da177e4SLinus Torvalds 		break;
202208a07904SRalf Baechle 
20231da177e4SLinus Torvalds 	case d_fmt:
20241da177e4SLinus Torvalds 		DPTOREG(rv.d, MIPSInst_FD(ir));
20251da177e4SLinus Torvalds 		break;
20261da177e4SLinus Torvalds 	case s_fmt:
20271da177e4SLinus Torvalds 		SPTOREG(rv.s, MIPSInst_FD(ir));
20281da177e4SLinus Torvalds 		break;
20291da177e4SLinus Torvalds 	case w_fmt:
20301da177e4SLinus Torvalds 		SITOREG(rv.w, MIPSInst_FD(ir));
20311da177e4SLinus Torvalds 		break;
20321da177e4SLinus Torvalds 	case l_fmt:
20332d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
203408a07904SRalf Baechle 			return SIGILL;
203508a07904SRalf Baechle 
20361da177e4SLinus Torvalds 		DITOREG(rv.l, MIPSInst_FD(ir));
20371da177e4SLinus Torvalds 		break;
20381da177e4SLinus Torvalds 	default:
20391da177e4SLinus Torvalds 		return SIGILL;
20401da177e4SLinus Torvalds 	}
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 	return 0;
20431da177e4SLinus Torvalds }
20441da177e4SLinus Torvalds 
2045e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2046515b029dSDavid Daney 	int has_fpu, void *__user *fault_addr)
20471da177e4SLinus Torvalds {
2048333d1f67SRalf Baechle 	unsigned long oldepc, prevepc;
2049102cedc3SLeonid Yegoshin 	struct mm_decoded_insn dec_insn;
2050102cedc3SLeonid Yegoshin 	u16 instr[4];
2051102cedc3SLeonid Yegoshin 	u16 *instr_ptr;
20521da177e4SLinus Torvalds 	int sig = 0;
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds 	oldepc = xcp->cp0_epc;
20551da177e4SLinus Torvalds 	do {
20561da177e4SLinus Torvalds 		prevepc = xcp->cp0_epc;
20571da177e4SLinus Torvalds 
2058102cedc3SLeonid Yegoshin 		if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2059102cedc3SLeonid Yegoshin 			/*
2060102cedc3SLeonid Yegoshin 			 * Get next 2 microMIPS instructions and convert them
2061102cedc3SLeonid Yegoshin 			 * into 32-bit instructions.
2062102cedc3SLeonid Yegoshin 			 */
2063102cedc3SLeonid Yegoshin 			if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2064102cedc3SLeonid Yegoshin 			    (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2065102cedc3SLeonid Yegoshin 			    (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2066102cedc3SLeonid Yegoshin 			    (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2067b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
20681da177e4SLinus Torvalds 				return SIGBUS;
20691da177e4SLinus Torvalds 			}
2070102cedc3SLeonid Yegoshin 			instr_ptr = instr;
2071102cedc3SLeonid Yegoshin 
2072102cedc3SLeonid Yegoshin 			/* Get first instruction. */
2073102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2074102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2075102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2076102cedc3SLeonid Yegoshin 					(*instr_ptr);
2077102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2078102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 2;
2079102cedc3SLeonid Yegoshin 				instr_ptr += 1;
2080102cedc3SLeonid Yegoshin 			} else {
2081102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2082102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2083102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2084102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 4;
2085102cedc3SLeonid Yegoshin 				instr_ptr += 2;
2086515b029dSDavid Daney 			}
2087102cedc3SLeonid Yegoshin 			/* Get second instruction. */
2088102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2089102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2090102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2091102cedc3SLeonid Yegoshin 					(*instr_ptr);
2092102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2093102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 2;
2094102cedc3SLeonid Yegoshin 			} else {
2095102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2096102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2097102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2098102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 4;
2099102cedc3SLeonid Yegoshin 			}
2100102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 1;
2101102cedc3SLeonid Yegoshin 		} else {
2102102cedc3SLeonid Yegoshin 			if ((get_user(dec_insn.insn,
2103102cedc3SLeonid Yegoshin 			    (mips_instruction __user *) xcp->cp0_epc)) ||
2104102cedc3SLeonid Yegoshin 			    (get_user(dec_insn.next_insn,
2105102cedc3SLeonid Yegoshin 			    (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2106102cedc3SLeonid Yegoshin 				MIPS_FPU_EMU_INC_STATS(errors);
2107102cedc3SLeonid Yegoshin 				return SIGBUS;
2108102cedc3SLeonid Yegoshin 			}
2109102cedc3SLeonid Yegoshin 			dec_insn.pc_inc = 4;
2110102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc = 4;
2111102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 0;
2112102cedc3SLeonid Yegoshin 		}
2113102cedc3SLeonid Yegoshin 
2114102cedc3SLeonid Yegoshin 		if ((dec_insn.insn == 0) ||
2115102cedc3SLeonid Yegoshin 		   ((dec_insn.pc_inc == 2) &&
2116102cedc3SLeonid Yegoshin 		   ((dec_insn.insn & 0xffff) == MM_NOP16)))
2117102cedc3SLeonid Yegoshin 			xcp->cp0_epc += dec_insn.pc_inc;	/* Skip NOPs */
21181da177e4SLinus Torvalds 		else {
2119cd21dfcfSRalf Baechle 			/*
21202cfcf8a8SMaciej W. Rozycki 			 * The 'ieee754_csr' is an alias of ctx->fcr31.
21212cfcf8a8SMaciej W. Rozycki 			 * No need to copy ctx->fcr31 to ieee754_csr.
2122cd21dfcfSRalf Baechle 			 */
2123102cedc3SLeonid Yegoshin 			sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
21241da177e4SLinus Torvalds 		}
21251da177e4SLinus Torvalds 
2126e04582b7SAtsushi Nemoto 		if (has_fpu)
21271da177e4SLinus Torvalds 			break;
21281da177e4SLinus Torvalds 		if (sig)
21291da177e4SLinus Torvalds 			break;
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 		cond_resched();
21321da177e4SLinus Torvalds 	} while (xcp->cp0_epc > prevepc);
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds 	/* SIGILL indicates a non-fpu instruction */
21351da177e4SLinus Torvalds 	if (sig == SIGILL && xcp->cp0_epc != oldepc)
21363f7cac41SRalf Baechle 		/* but if EPC has advanced, then ignore it */
21371da177e4SLinus Torvalds 		sig = 0;
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds 	return sig;
21401da177e4SLinus Torvalds }
2141