xref: /openbmc/linux/arch/mips/math-emu/cp1emu.c (revision 409fcace)
11da177e4SLinus Torvalds /*
23f7cac41SRalf Baechle  * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * MIPS floating point support
51da177e4SLinus Torvalds  * Copyright (C) 1994-2000 Algorithmics Ltd.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
81da177e4SLinus Torvalds  * Copyright (C) 2000  MIPS Technologies, Inc.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  This program is free software; you can distribute it and/or modify it
111da177e4SLinus Torvalds  *  under the terms of the GNU General Public License (Version 2) as
121da177e4SLinus Torvalds  *  published by the Free Software Foundation.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  This program is distributed in the hope it will be useful, but WITHOUT
151da177e4SLinus Torvalds  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
161da177e4SLinus Torvalds  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
171da177e4SLinus Torvalds  *  for more details.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *  You should have received a copy of the GNU General Public License along
201da177e4SLinus Torvalds  *  with this program; if not, write to the Free Software Foundation, Inc.,
213f7cac41SRalf Baechle  *  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * A complete emulator for MIPS coprocessor 1 instructions.  This is
241da177e4SLinus Torvalds  * required for #float(switch) or #float(trap), where it catches all
251da177e4SLinus Torvalds  * COP1 instructions via the "CoProcessor Unusable" exception.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  * More surprisingly it is also required for #float(ieee), to help out
283f7cac41SRalf Baechle  * the hardware FPU at the boundaries of the IEEE-754 representation
291da177e4SLinus Torvalds  * (denormalised values, infinities, underflow, etc).  It is made
301da177e4SLinus Torvalds  * quite nasty because emulation of some non-COP1 instructions is
311da177e4SLinus Torvalds  * required, e.g. in branch delay slots.
321da177e4SLinus Torvalds  *
333f7cac41SRalf Baechle  * Note if you know that you won't have an FPU, then you'll get much
341da177e4SLinus Torvalds  * better performance by compiling with -msoft-float!
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds #include <linux/sched.h>
3783fd38caSAtsushi Nemoto #include <linux/debugfs.h>
3885c51c51SRalf Baechle #include <linux/percpu-defs.h>
397f788d2dSDeng-Cheng Zhu #include <linux/perf_event.h>
401da177e4SLinus Torvalds 
41cd8ee345SRalf Baechle #include <asm/branch.h>
421da177e4SLinus Torvalds #include <asm/inst.h>
431da177e4SLinus Torvalds #include <asm/ptrace.h>
441da177e4SLinus Torvalds #include <asm/signal.h>
457c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
46cd8ee345SRalf Baechle 
47f6843626SMaciej W. Rozycki #include <asm/cpu-info.h>
48cd8ee345SRalf Baechle #include <asm/processor.h>
491da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
50102cedc3SLeonid Yegoshin #include <asm/fpu.h>
51b0a668fbSLeonid Yegoshin #include <asm/mips-r2-to-r6-emul.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include "ieee754.h"
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */
561da177e4SLinus Torvalds 
57eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
581da177e4SLinus Torvalds 	mips_instruction);
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *,
61445a58ceSPaul Burton 	struct mips_fpu_struct *, mips_instruction, void __user **);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds /* Control registers */
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #define FPCREG_RID	0	/* $0  = revision id */
66c491cfa2SMaciej W. Rozycki #define FPCREG_FCCR	25	/* $25 = fccr */
67c491cfa2SMaciej W. Rozycki #define FPCREG_FEXR	26	/* $26 = fexr */
68c491cfa2SMaciej W. Rozycki #define FPCREG_FENR	28	/* $28 = fenr */
691da177e4SLinus Torvalds #define FPCREG_CSR	31	/* $31 = csr */
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /* convert condition code register number to csr bit */
72b0a668fbSLeonid Yegoshin const unsigned int fpucondbit[8] = {
73c491cfa2SMaciej W. Rozycki 	FPU_CSR_COND,
741da177e4SLinus Torvalds 	FPU_CSR_COND1,
751da177e4SLinus Torvalds 	FPU_CSR_COND2,
761da177e4SLinus Torvalds 	FPU_CSR_COND3,
771da177e4SLinus Torvalds 	FPU_CSR_COND4,
781da177e4SLinus Torvalds 	FPU_CSR_COND5,
791da177e4SLinus Torvalds 	FPU_CSR_COND6,
801da177e4SLinus Torvalds 	FPU_CSR_COND7
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds 
83102cedc3SLeonid Yegoshin /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
84102cedc3SLeonid Yegoshin static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
85102cedc3SLeonid Yegoshin static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
86102cedc3SLeonid Yegoshin static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
87102cedc3SLeonid Yegoshin static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
88102cedc3SLeonid Yegoshin 
89102cedc3SLeonid Yegoshin /*
90102cedc3SLeonid Yegoshin  * This functions translates a 32-bit microMIPS instruction
91102cedc3SLeonid Yegoshin  * into a 32-bit MIPS32 instruction. Returns 0 on success
92102cedc3SLeonid Yegoshin  * and SIGILL otherwise.
93102cedc3SLeonid Yegoshin  */
94102cedc3SLeonid Yegoshin static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
95102cedc3SLeonid Yegoshin {
96102cedc3SLeonid Yegoshin 	union mips_instruction insn = *insn_ptr;
97102cedc3SLeonid Yegoshin 	union mips_instruction mips32_insn = insn;
98102cedc3SLeonid Yegoshin 	int func, fmt, op;
99102cedc3SLeonid Yegoshin 
100102cedc3SLeonid Yegoshin 	switch (insn.mm_i_format.opcode) {
101102cedc3SLeonid Yegoshin 	case mm_ldc132_op:
102102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = ldc1_op;
103102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
104102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
105102cedc3SLeonid Yegoshin 		break;
106102cedc3SLeonid Yegoshin 	case mm_lwc132_op:
107102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = lwc1_op;
108102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
109102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
110102cedc3SLeonid Yegoshin 		break;
111102cedc3SLeonid Yegoshin 	case mm_sdc132_op:
112102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = sdc1_op;
113102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
114102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
115102cedc3SLeonid Yegoshin 		break;
116102cedc3SLeonid Yegoshin 	case mm_swc132_op:
117102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = swc1_op;
118102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
119102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
120102cedc3SLeonid Yegoshin 		break;
121102cedc3SLeonid Yegoshin 	case mm_pool32i_op:
122102cedc3SLeonid Yegoshin 		/* NOTE: offset is << by 1 if in microMIPS mode. */
123102cedc3SLeonid Yegoshin 		if ((insn.mm_i_format.rt == mm_bc1f_op) ||
124102cedc3SLeonid Yegoshin 		    (insn.mm_i_format.rt == mm_bc1t_op)) {
125102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.opcode = cop1_op;
126102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.bc = bc_op;
127102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.flag =
128102cedc3SLeonid Yegoshin 				(insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
129102cedc3SLeonid Yegoshin 		} else
130102cedc3SLeonid Yegoshin 			return SIGILL;
131102cedc3SLeonid Yegoshin 		break;
132102cedc3SLeonid Yegoshin 	case mm_pool32f_op:
133102cedc3SLeonid Yegoshin 		switch (insn.mm_fp0_format.func) {
134102cedc3SLeonid Yegoshin 		case mm_32f_01_op:
135102cedc3SLeonid Yegoshin 		case mm_32f_11_op:
136102cedc3SLeonid Yegoshin 		case mm_32f_02_op:
137102cedc3SLeonid Yegoshin 		case mm_32f_12_op:
138102cedc3SLeonid Yegoshin 		case mm_32f_41_op:
139102cedc3SLeonid Yegoshin 		case mm_32f_51_op:
140102cedc3SLeonid Yegoshin 		case mm_32f_42_op:
141102cedc3SLeonid Yegoshin 		case mm_32f_52_op:
142102cedc3SLeonid Yegoshin 			op = insn.mm_fp0_format.func;
143102cedc3SLeonid Yegoshin 			if (op == mm_32f_01_op)
144102cedc3SLeonid Yegoshin 				func = madd_s_op;
145102cedc3SLeonid Yegoshin 			else if (op == mm_32f_11_op)
146102cedc3SLeonid Yegoshin 				func = madd_d_op;
147102cedc3SLeonid Yegoshin 			else if (op == mm_32f_02_op)
148102cedc3SLeonid Yegoshin 				func = nmadd_s_op;
149102cedc3SLeonid Yegoshin 			else if (op == mm_32f_12_op)
150102cedc3SLeonid Yegoshin 				func = nmadd_d_op;
151102cedc3SLeonid Yegoshin 			else if (op == mm_32f_41_op)
152102cedc3SLeonid Yegoshin 				func = msub_s_op;
153102cedc3SLeonid Yegoshin 			else if (op == mm_32f_51_op)
154102cedc3SLeonid Yegoshin 				func = msub_d_op;
155102cedc3SLeonid Yegoshin 			else if (op == mm_32f_42_op)
156102cedc3SLeonid Yegoshin 				func = nmsub_s_op;
157102cedc3SLeonid Yegoshin 			else
158102cedc3SLeonid Yegoshin 				func = nmsub_d_op;
159102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.opcode = cop1x_op;
160102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
161102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
162102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
163102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
164102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.func = func;
165102cedc3SLeonid Yegoshin 			break;
166102cedc3SLeonid Yegoshin 		case mm_32f_10_op:
167102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
168102cedc3SLeonid Yegoshin 			op = insn.mm_fp5_format.op & 0x7;
169102cedc3SLeonid Yegoshin 			if (op == mm_ldxc1_op)
170102cedc3SLeonid Yegoshin 				func = ldxc1_op;
171102cedc3SLeonid Yegoshin 			else if (op == mm_sdxc1_op)
172102cedc3SLeonid Yegoshin 				func = sdxc1_op;
173102cedc3SLeonid Yegoshin 			else if (op == mm_lwxc1_op)
174102cedc3SLeonid Yegoshin 				func = lwxc1_op;
175102cedc3SLeonid Yegoshin 			else if (op == mm_swxc1_op)
176102cedc3SLeonid Yegoshin 				func = swxc1_op;
177102cedc3SLeonid Yegoshin 
178102cedc3SLeonid Yegoshin 			if (func != -1) {
179102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = cop1x_op;
180102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs =
181102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.base;
182102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
183102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.index;
184102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = 0;
185102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = insn.mm_fp5_format.fd;
186102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = func;
187102cedc3SLeonid Yegoshin 			} else
188102cedc3SLeonid Yegoshin 				return SIGILL;
189102cedc3SLeonid Yegoshin 			break;
190102cedc3SLeonid Yegoshin 		case mm_32f_40_op:
191102cedc3SLeonid Yegoshin 			op = -1;	/* Invalid */
192102cedc3SLeonid Yegoshin 			if (insn.mm_fp2_format.op == mm_fmovt_op)
193102cedc3SLeonid Yegoshin 				op = 1;
194102cedc3SLeonid Yegoshin 			else if (insn.mm_fp2_format.op == mm_fmovf_op)
195102cedc3SLeonid Yegoshin 				op = 0;
196102cedc3SLeonid Yegoshin 			if (op != -1) {
197102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
198102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
199102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp2_format.fmt];
200102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
201102cedc3SLeonid Yegoshin 					(insn.mm_fp2_format.cc<<2) + op;
202102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
203102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fs;
204102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
205102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fd;
206102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = fmovc_op;
207102cedc3SLeonid Yegoshin 			} else
208102cedc3SLeonid Yegoshin 				return SIGILL;
209102cedc3SLeonid Yegoshin 			break;
210102cedc3SLeonid Yegoshin 		case mm_32f_60_op:
211102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
212102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fadd_op)
213102cedc3SLeonid Yegoshin 				func = fadd_op;
214102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fsub_op)
215102cedc3SLeonid Yegoshin 				func = fsub_op;
216102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmul_op)
217102cedc3SLeonid Yegoshin 				func = fmul_op;
218102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fdiv_op)
219102cedc3SLeonid Yegoshin 				func = fdiv_op;
220102cedc3SLeonid Yegoshin 			if (func != -1) {
221102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
222102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
223102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
224102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
225102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
226102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
227102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
228102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
229102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
230102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
231102cedc3SLeonid Yegoshin 			} else
232102cedc3SLeonid Yegoshin 				return SIGILL;
233102cedc3SLeonid Yegoshin 			break;
234102cedc3SLeonid Yegoshin 		case mm_32f_70_op:
235102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
236102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fmovn_op)
237102cedc3SLeonid Yegoshin 				func = fmovn_op;
238102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmovz_op)
239102cedc3SLeonid Yegoshin 				func = fmovz_op;
240102cedc3SLeonid Yegoshin 			if (func != -1) {
241102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
242102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
243102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
244102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
245102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
246102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
247102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
248102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
249102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
250102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
251102cedc3SLeonid Yegoshin 			} else
252102cedc3SLeonid Yegoshin 				return SIGILL;
253102cedc3SLeonid Yegoshin 			break;
254102cedc3SLeonid Yegoshin 		case mm_32f_73_op:    /* POOL32FXF */
255102cedc3SLeonid Yegoshin 			switch (insn.mm_fp1_format.op) {
256102cedc3SLeonid Yegoshin 			case mm_movf0_op:
257102cedc3SLeonid Yegoshin 			case mm_movf1_op:
258102cedc3SLeonid Yegoshin 			case mm_movt0_op:
259102cedc3SLeonid Yegoshin 			case mm_movt1_op:
260102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
261102cedc3SLeonid Yegoshin 				    mm_movf0_op)
262102cedc3SLeonid Yegoshin 					op = 0;
263102cedc3SLeonid Yegoshin 				else
264102cedc3SLeonid Yegoshin 					op = 1;
265102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = spec_op;
266102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
267102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
268102cedc3SLeonid Yegoshin 					(insn.mm_fp4_format.cc << 2) + op;
269102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
270102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = 0;
271102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = movc_op;
272102cedc3SLeonid Yegoshin 				break;
273102cedc3SLeonid Yegoshin 			case mm_fcvtd0_op:
274102cedc3SLeonid Yegoshin 			case mm_fcvtd1_op:
275102cedc3SLeonid Yegoshin 			case mm_fcvts0_op:
276102cedc3SLeonid Yegoshin 			case mm_fcvts1_op:
277102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
278102cedc3SLeonid Yegoshin 				    mm_fcvtd0_op) {
279102cedc3SLeonid Yegoshin 					func = fcvtd_op;
280102cedc3SLeonid Yegoshin 					fmt = swl_format[insn.mm_fp3_format.fmt];
281102cedc3SLeonid Yegoshin 				} else {
282102cedc3SLeonid Yegoshin 					func = fcvts_op;
283102cedc3SLeonid Yegoshin 					fmt = dwl_format[insn.mm_fp3_format.fmt];
284102cedc3SLeonid Yegoshin 				}
285102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
286102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt = fmt;
287102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
288102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
289102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
290102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
291102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
292102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
293102cedc3SLeonid Yegoshin 				break;
294102cedc3SLeonid Yegoshin 			case mm_fmov0_op:
295102cedc3SLeonid Yegoshin 			case mm_fmov1_op:
296102cedc3SLeonid Yegoshin 			case mm_fabs0_op:
297102cedc3SLeonid Yegoshin 			case mm_fabs1_op:
298102cedc3SLeonid Yegoshin 			case mm_fneg0_op:
299102cedc3SLeonid Yegoshin 			case mm_fneg1_op:
300102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
301102cedc3SLeonid Yegoshin 				    mm_fmov0_op)
302102cedc3SLeonid Yegoshin 					func = fmov_op;
303102cedc3SLeonid Yegoshin 				else if ((insn.mm_fp1_format.op & 0x7f) ==
304102cedc3SLeonid Yegoshin 					 mm_fabs0_op)
305102cedc3SLeonid Yegoshin 					func = fabs_op;
306102cedc3SLeonid Yegoshin 				else
307102cedc3SLeonid Yegoshin 					func = fneg_op;
308102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
309102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
310102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp3_format.fmt];
311102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
312102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
313102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
314102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
315102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
316102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
317102cedc3SLeonid Yegoshin 				break;
318102cedc3SLeonid Yegoshin 			case mm_ffloorl_op:
319102cedc3SLeonid Yegoshin 			case mm_ffloorw_op:
320102cedc3SLeonid Yegoshin 			case mm_fceill_op:
321102cedc3SLeonid Yegoshin 			case mm_fceilw_op:
322102cedc3SLeonid Yegoshin 			case mm_ftruncl_op:
323102cedc3SLeonid Yegoshin 			case mm_ftruncw_op:
324102cedc3SLeonid Yegoshin 			case mm_froundl_op:
325102cedc3SLeonid Yegoshin 			case mm_froundw_op:
326102cedc3SLeonid Yegoshin 			case mm_fcvtl_op:
327102cedc3SLeonid Yegoshin 			case mm_fcvtw_op:
328102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_ffloorl_op)
329102cedc3SLeonid Yegoshin 					func = ffloorl_op;
330102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ffloorw_op)
331102cedc3SLeonid Yegoshin 					func = ffloor_op;
332102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceill_op)
333102cedc3SLeonid Yegoshin 					func = fceill_op;
334102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceilw_op)
335102cedc3SLeonid Yegoshin 					func = fceil_op;
336102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncl_op)
337102cedc3SLeonid Yegoshin 					func = ftruncl_op;
338102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncw_op)
339102cedc3SLeonid Yegoshin 					func = ftrunc_op;
340102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundl_op)
341102cedc3SLeonid Yegoshin 					func = froundl_op;
342102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundw_op)
343102cedc3SLeonid Yegoshin 					func = fround_op;
344102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fcvtl_op)
345102cedc3SLeonid Yegoshin 					func = fcvtl_op;
346102cedc3SLeonid Yegoshin 				else
347102cedc3SLeonid Yegoshin 					func = fcvtw_op;
348102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
349102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
350102cedc3SLeonid Yegoshin 					sd_format[insn.mm_fp1_format.fmt];
351102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
352102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
353102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
354102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
355102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
356102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
357102cedc3SLeonid Yegoshin 				break;
358102cedc3SLeonid Yegoshin 			case mm_frsqrt_op:
359102cedc3SLeonid Yegoshin 			case mm_fsqrt_op:
360102cedc3SLeonid Yegoshin 			case mm_frecip_op:
361102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_frsqrt_op)
362102cedc3SLeonid Yegoshin 					func = frsqrt_op;
363102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fsqrt_op)
364102cedc3SLeonid Yegoshin 					func = fsqrt_op;
365102cedc3SLeonid Yegoshin 				else
366102cedc3SLeonid Yegoshin 					func = frecip_op;
367102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
368102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
369102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp1_format.fmt];
370102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
371102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
372102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
373102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
374102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
375102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
376102cedc3SLeonid Yegoshin 				break;
377102cedc3SLeonid Yegoshin 			case mm_mfc1_op:
378102cedc3SLeonid Yegoshin 			case mm_mtc1_op:
379102cedc3SLeonid Yegoshin 			case mm_cfc1_op:
380102cedc3SLeonid Yegoshin 			case mm_ctc1_op:
3819355e59cSSteven J. Hill 			case mm_mfhc1_op:
3829355e59cSSteven J. Hill 			case mm_mthc1_op:
383102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_mfc1_op)
384102cedc3SLeonid Yegoshin 					op = mfc_op;
385102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_mtc1_op)
386102cedc3SLeonid Yegoshin 					op = mtc_op;
387102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_cfc1_op)
388102cedc3SLeonid Yegoshin 					op = cfc_op;
3899355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_ctc1_op)
390102cedc3SLeonid Yegoshin 					op = ctc_op;
3919355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_mfhc1_op)
3929355e59cSSteven J. Hill 					op = mfhc_op;
3939355e59cSSteven J. Hill 				else
3949355e59cSSteven J. Hill 					op = mthc_op;
395102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.opcode = cop1_op;
396102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.op = op;
397102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.rt =
398102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
399102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fs =
400102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
401102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fd = 0;
402102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.func = 0;
403102cedc3SLeonid Yegoshin 				break;
404102cedc3SLeonid Yegoshin 			default:
405102cedc3SLeonid Yegoshin 				return SIGILL;
406102cedc3SLeonid Yegoshin 			}
407102cedc3SLeonid Yegoshin 			break;
408102cedc3SLeonid Yegoshin 		case mm_32f_74_op:	/* c.cond.fmt */
409102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.opcode = cop1_op;
410102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fmt =
411102cedc3SLeonid Yegoshin 				sdps_format[insn.mm_fp4_format.fmt];
412102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
413102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
414102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
415102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.func =
416102cedc3SLeonid Yegoshin 				insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
417102cedc3SLeonid Yegoshin 			break;
418102cedc3SLeonid Yegoshin 		default:
419102cedc3SLeonid Yegoshin 			return SIGILL;
420102cedc3SLeonid Yegoshin 		}
421102cedc3SLeonid Yegoshin 		break;
422102cedc3SLeonid Yegoshin 	default:
423102cedc3SLeonid Yegoshin 		return SIGILL;
424102cedc3SLeonid Yegoshin 	}
425102cedc3SLeonid Yegoshin 
426102cedc3SLeonid Yegoshin 	*insn_ptr = mips32_insn;
427102cedc3SLeonid Yegoshin 	return 0;
428102cedc3SLeonid Yegoshin }
429102cedc3SLeonid Yegoshin 
4301da177e4SLinus Torvalds /*
4311da177e4SLinus Torvalds  * Redundant with logic already in kernel/branch.c,
4321da177e4SLinus Torvalds  * embedded in compute_return_epc.  At some point,
4331da177e4SLinus Torvalds  * a single subroutine should be used across both
4341da177e4SLinus Torvalds  * modules.
4351da177e4SLinus Torvalds  */
436432c6bacSPaul Burton int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
437102cedc3SLeonid Yegoshin 		  unsigned long *contpc)
4381da177e4SLinus Torvalds {
439102cedc3SLeonid Yegoshin 	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
440102cedc3SLeonid Yegoshin 	unsigned int fcr31;
441102cedc3SLeonid Yegoshin 	unsigned int bit = 0;
4428bcd84a4SDouglas Leung 	unsigned int bit0;
4438bcd84a4SDouglas Leung 	union fpureg *fpr;
444102cedc3SLeonid Yegoshin 
445102cedc3SLeonid Yegoshin 	switch (insn.i_format.opcode) {
4461da177e4SLinus Torvalds 	case spec_op:
447102cedc3SLeonid Yegoshin 		switch (insn.r_format.func) {
4481da177e4SLinus Torvalds 		case jalr_op:
449ab4a92e6SPaul Burton 			if (insn.r_format.rd != 0) {
450102cedc3SLeonid Yegoshin 				regs->regs[insn.r_format.rd] =
451102cedc3SLeonid Yegoshin 					regs->cp0_epc + dec_insn.pc_inc +
452102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
453ab4a92e6SPaul Burton 			}
454102cedc3SLeonid Yegoshin 			/* Fall through */
4551da177e4SLinus Torvalds 		case jr_op:
4565f9f41c4SMarkos Chandras 			/* For R6, JR already emulated in jalr_op */
457143fefc8SMarkos Chandras 			if (NO_R6EMU && insn.r_format.func == jr_op)
4585f9f41c4SMarkos Chandras 				break;
459102cedc3SLeonid Yegoshin 			*contpc = regs->regs[insn.r_format.rs];
4601da177e4SLinus Torvalds 			return 1;
4611da177e4SLinus Torvalds 		}
4621da177e4SLinus Torvalds 		break;
4631da177e4SLinus Torvalds 	case bcond_op:
464102cedc3SLeonid Yegoshin 		switch (insn.i_format.rt) {
4651da177e4SLinus Torvalds 		case bltzal_op:
4661da177e4SLinus Torvalds 		case bltzall_op:
467319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
468319824eaSMarkos Chandras 			    insn.i_format.rt == bltzall_op))
469319824eaSMarkos Chandras 				break;
470319824eaSMarkos Chandras 
471102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
472102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
473102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
474102cedc3SLeonid Yegoshin 			/* Fall through */
475102cedc3SLeonid Yegoshin 		case bltzl_op:
476319824eaSMarkos Chandras 			if (NO_R6EMU)
477319824eaSMarkos Chandras 				break;
478319824eaSMarkos Chandras 		case bltz_op:
479102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] < 0)
480102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
481102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
482102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
483102cedc3SLeonid Yegoshin 			else
484102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
485102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
486102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
4871da177e4SLinus Torvalds 			return 1;
488102cedc3SLeonid Yegoshin 		case bgezal_op:
489102cedc3SLeonid Yegoshin 		case bgezall_op:
490319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
491319824eaSMarkos Chandras 			    insn.i_format.rt == bgezall_op))
492319824eaSMarkos Chandras 				break;
493319824eaSMarkos Chandras 
494102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
495102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
496102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
497102cedc3SLeonid Yegoshin 			/* Fall through */
498102cedc3SLeonid Yegoshin 		case bgezl_op:
499319824eaSMarkos Chandras 			if (NO_R6EMU)
500319824eaSMarkos Chandras 				break;
501319824eaSMarkos Chandras 		case bgez_op:
502102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] >= 0)
503102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
504102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
505102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
506102cedc3SLeonid Yegoshin 			else
507102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
508102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
509102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
510102cedc3SLeonid Yegoshin 			return 1;
5111da177e4SLinus Torvalds 		}
5121da177e4SLinus Torvalds 		break;
5131da177e4SLinus Torvalds 	case jalx_op:
514102cedc3SLeonid Yegoshin 		set_isa16_mode(bit);
515102cedc3SLeonid Yegoshin 	case jal_op:
516102cedc3SLeonid Yegoshin 		regs->regs[31] = regs->cp0_epc +
517102cedc3SLeonid Yegoshin 			dec_insn.pc_inc +
518102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc;
519102cedc3SLeonid Yegoshin 		/* Fall through */
520102cedc3SLeonid Yegoshin 	case j_op:
521102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
522102cedc3SLeonid Yegoshin 		*contpc >>= 28;
523102cedc3SLeonid Yegoshin 		*contpc <<= 28;
524102cedc3SLeonid Yegoshin 		*contpc |= (insn.j_format.target << 2);
525102cedc3SLeonid Yegoshin 		/* Set microMIPS mode bit: XOR for jalx. */
526102cedc3SLeonid Yegoshin 		*contpc ^= bit;
5271da177e4SLinus Torvalds 		return 1;
528102cedc3SLeonid Yegoshin 	case beql_op:
529319824eaSMarkos Chandras 		if (NO_R6EMU)
530319824eaSMarkos Chandras 			break;
531319824eaSMarkos Chandras 	case beq_op:
532102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] ==
533102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
534102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
535102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
536102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
537102cedc3SLeonid Yegoshin 		else
538102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
539102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
540102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
541102cedc3SLeonid Yegoshin 		return 1;
542102cedc3SLeonid Yegoshin 	case bnel_op:
543319824eaSMarkos Chandras 		if (NO_R6EMU)
544319824eaSMarkos Chandras 			break;
545319824eaSMarkos Chandras 	case bne_op:
546102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] !=
547102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
548102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
549102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
550102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
551102cedc3SLeonid Yegoshin 		else
552102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
553102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
554102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
555102cedc3SLeonid Yegoshin 		return 1;
556102cedc3SLeonid Yegoshin 	case blezl_op:
557e9d92d22SMarkos Chandras 		if (!insn.i_format.rt && NO_R6EMU)
558319824eaSMarkos Chandras 			break;
559319824eaSMarkos Chandras 	case blez_op:
560a8ff66f5SMarkos Chandras 
561a8ff66f5SMarkos Chandras 		/*
562a8ff66f5SMarkos Chandras 		 * Compact branches for R6 for the
563a8ff66f5SMarkos Chandras 		 * blez and blezl opcodes.
564a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = 0 | rt != 0  == BLEZALC
565a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = rt != 0      == BGEZALC
566a8ff66f5SMarkos Chandras 		 * BLEZ  | rs != 0 | rt != 0 == BGEUC
567a8ff66f5SMarkos Chandras 		 * BLEZL | rs = 0 | rt != 0  == BLEZC
568a8ff66f5SMarkos Chandras 		 * BLEZL | rs = rt != 0      == BGEZC
569a8ff66f5SMarkos Chandras 		 * BLEZL | rs != 0 | rt != 0 == BGEC
570a8ff66f5SMarkos Chandras 		 *
571a8ff66f5SMarkos Chandras 		 * For real BLEZ{,L}, rt is always 0.
572a8ff66f5SMarkos Chandras 		 */
573a8ff66f5SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
574a8ff66f5SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
575a8ff66f5SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
576a8ff66f5SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
577a8ff66f5SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
578a8ff66f5SMarkos Chandras 					dec_insn.pc_inc;
579a8ff66f5SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
580a8ff66f5SMarkos Chandras 				dec_insn.next_pc_inc;
581a8ff66f5SMarkos Chandras 
582a8ff66f5SMarkos Chandras 			return 1;
583a8ff66f5SMarkos Chandras 		}
584102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] <= 0)
585102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
586102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
587102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
588102cedc3SLeonid Yegoshin 		else
589102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
590102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
591102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
592102cedc3SLeonid Yegoshin 		return 1;
593102cedc3SLeonid Yegoshin 	case bgtzl_op:
594e9d92d22SMarkos Chandras 		if (!insn.i_format.rt && NO_R6EMU)
595319824eaSMarkos Chandras 			break;
596319824eaSMarkos Chandras 	case bgtz_op:
597f1b44067SMarkos Chandras 		/*
598f1b44067SMarkos Chandras 		 * Compact branches for R6 for the
599f1b44067SMarkos Chandras 		 * bgtz and bgtzl opcodes.
600f1b44067SMarkos Chandras 		 * BGTZ  | rs = 0 | rt != 0  == BGTZALC
601f1b44067SMarkos Chandras 		 * BGTZ  | rs = rt != 0      == BLTZALC
602f1b44067SMarkos Chandras 		 * BGTZ  | rs != 0 | rt != 0 == BLTUC
603f1b44067SMarkos Chandras 		 * BGTZL | rs = 0 | rt != 0  == BGTZC
604f1b44067SMarkos Chandras 		 * BGTZL | rs = rt != 0      == BLTZC
605f1b44067SMarkos Chandras 		 * BGTZL | rs != 0 | rt != 0 == BLTC
606f1b44067SMarkos Chandras 		 *
607f1b44067SMarkos Chandras 		 * *ZALC varint for BGTZ &&& rt != 0
608f1b44067SMarkos Chandras 		 * For real GTZ{,L}, rt is always 0.
609f1b44067SMarkos Chandras 		 */
610f1b44067SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
611f1b44067SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
612f1b44067SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
613f1b44067SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
614f1b44067SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
615f1b44067SMarkos Chandras 					dec_insn.pc_inc;
616f1b44067SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
617f1b44067SMarkos Chandras 				dec_insn.next_pc_inc;
618f1b44067SMarkos Chandras 
619f1b44067SMarkos Chandras 			return 1;
620f1b44067SMarkos Chandras 		}
621f1b44067SMarkos Chandras 
622102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] > 0)
623102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
624102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
625102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
626102cedc3SLeonid Yegoshin 		else
627102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
628102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
629102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
630102cedc3SLeonid Yegoshin 		return 1;
6311b492600SPaul Burton 	case pop10_op:
6321b492600SPaul Burton 	case pop30_op:
633c893ce38SMarkos Chandras 		if (!cpu_has_mips_r6)
634c893ce38SMarkos Chandras 			break;
635c893ce38SMarkos Chandras 		if (insn.i_format.rt && !insn.i_format.rs)
636c893ce38SMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
637c893ce38SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
638c893ce38SMarkos Chandras 			dec_insn.next_pc_inc;
639c893ce38SMarkos Chandras 
640c893ce38SMarkos Chandras 		return 1;
641c26d4219SDavid Daney #ifdef CONFIG_CPU_CAVIUM_OCTEON
642c26d4219SDavid Daney 	case lwc2_op: /* This is bbit0 on Octeon */
643c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 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 ldc2_op: /* This is bbit032 on Octeon */
649c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
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 swc2_op: /* This is bbit1 on Octeon */
655c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
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;
660c26d4219SDavid Daney 	case sdc2_op: /* This is bbit132 on Octeon */
661c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
662c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
663c26d4219SDavid Daney 		else
664c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
665c26d4219SDavid Daney 		return 1;
6668467ca01SMarkos Chandras #else
6678467ca01SMarkos Chandras 	case bc6_op:
6688467ca01SMarkos Chandras 		/*
6698467ca01SMarkos Chandras 		 * Only valid for MIPS R6 but we can still end up
6708467ca01SMarkos Chandras 		 * here from a broken userland so just tell emulator
6718467ca01SMarkos Chandras 		 * this is not a branch and let it break later on.
6728467ca01SMarkos Chandras 		 */
6738467ca01SMarkos Chandras 		if  (!cpu_has_mips_r6)
6748467ca01SMarkos Chandras 			break;
6758467ca01SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
6768467ca01SMarkos Chandras 			dec_insn.next_pc_inc;
6778467ca01SMarkos Chandras 
6788467ca01SMarkos Chandras 		return 1;
67984fef630SMarkos Chandras 	case balc6_op:
68084fef630SMarkos Chandras 		if (!cpu_has_mips_r6)
68184fef630SMarkos Chandras 			break;
68284fef630SMarkos Chandras 		regs->regs[31] = regs->cp0_epc + 4;
68384fef630SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
68484fef630SMarkos Chandras 			dec_insn.next_pc_inc;
68584fef630SMarkos Chandras 
68684fef630SMarkos Chandras 		return 1;
6871c66b79bSPaul Burton 	case pop66_op:
68869b9a2fdSMarkos Chandras 		if (!cpu_has_mips_r6)
68969b9a2fdSMarkos Chandras 			break;
69069b9a2fdSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
69169b9a2fdSMarkos Chandras 			dec_insn.next_pc_inc;
69269b9a2fdSMarkos Chandras 
69369b9a2fdSMarkos Chandras 		return 1;
6941c66b79bSPaul Burton 	case pop76_op:
69528d6f93dSMarkos Chandras 		if (!cpu_has_mips_r6)
69628d6f93dSMarkos Chandras 			break;
69728d6f93dSMarkos Chandras 		if (!insn.i_format.rs)
69828d6f93dSMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
69928d6f93dSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
70028d6f93dSMarkos Chandras 			dec_insn.next_pc_inc;
70128d6f93dSMarkos Chandras 
70228d6f93dSMarkos Chandras 		return 1;
703c26d4219SDavid Daney #endif
7041da177e4SLinus Torvalds 	case cop0_op:
7051da177e4SLinus Torvalds 	case cop1_op:
706c8a34581SMarkos Chandras 		/* Need to check for R6 bc1nez and bc1eqz branches */
707c8a34581SMarkos Chandras 		if (cpu_has_mips_r6 &&
708c8a34581SMarkos Chandras 		    ((insn.i_format.rs == bc1eqz_op) ||
709c8a34581SMarkos Chandras 		     (insn.i_format.rs == bc1nez_op))) {
710c8a34581SMarkos Chandras 			bit = 0;
7118bcd84a4SDouglas Leung 			fpr = &current->thread.fpu.fpr[insn.i_format.rt];
7128bcd84a4SDouglas Leung 			bit0 = get_fpr32(fpr, 0) & 0x1;
713c8a34581SMarkos Chandras 			switch (insn.i_format.rs) {
714c8a34581SMarkos Chandras 			case bc1eqz_op:
7158bcd84a4SDouglas Leung 				bit = bit0 == 0;
716c8a34581SMarkos Chandras 				break;
717c8a34581SMarkos Chandras 			case bc1nez_op:
7188bcd84a4SDouglas Leung 				bit = bit0 != 0;
719c8a34581SMarkos Chandras 				break;
720c8a34581SMarkos Chandras 			}
721c8a34581SMarkos Chandras 			if (bit)
722c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
723c8a34581SMarkos Chandras 					dec_insn.pc_inc +
724c8a34581SMarkos Chandras 					(insn.i_format.simmediate << 2);
725c8a34581SMarkos Chandras 			else
726c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
727c8a34581SMarkos Chandras 					dec_insn.pc_inc +
728c8a34581SMarkos Chandras 					dec_insn.next_pc_inc;
729c8a34581SMarkos Chandras 
730c8a34581SMarkos Chandras 			return 1;
731c8a34581SMarkos Chandras 		}
732c8a34581SMarkos Chandras 		/* R2/R6 compatible cop1 instruction. Fall through */
7331da177e4SLinus Torvalds 	case cop2_op:
7341da177e4SLinus Torvalds 	case cop1x_op:
735102cedc3SLeonid Yegoshin 		if (insn.i_format.rs == bc_op) {
736102cedc3SLeonid Yegoshin 			preempt_disable();
737102cedc3SLeonid Yegoshin 			if (is_fpu_owner())
738842dfc11SManuel Lauss 			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
739102cedc3SLeonid Yegoshin 			else
740102cedc3SLeonid Yegoshin 				fcr31 = current->thread.fpu.fcr31;
741102cedc3SLeonid Yegoshin 			preempt_enable();
742102cedc3SLeonid Yegoshin 
743102cedc3SLeonid Yegoshin 			bit = (insn.i_format.rt >> 2);
744102cedc3SLeonid Yegoshin 			bit += (bit != 0);
745102cedc3SLeonid Yegoshin 			bit += 23;
746102cedc3SLeonid Yegoshin 			switch (insn.i_format.rt & 3) {
747102cedc3SLeonid Yegoshin 			case 0:	/* bc1f */
748102cedc3SLeonid Yegoshin 			case 2:	/* bc1fl */
749102cedc3SLeonid Yegoshin 				if (~fcr31 & (1 << bit))
750102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
751102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
752102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
753102cedc3SLeonid Yegoshin 				else
754102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
755102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
756102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
757102cedc3SLeonid Yegoshin 				return 1;
758102cedc3SLeonid Yegoshin 			case 1:	/* bc1t */
759102cedc3SLeonid Yegoshin 			case 3:	/* bc1tl */
760102cedc3SLeonid Yegoshin 				if (fcr31 & (1 << bit))
761102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
762102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
763102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
764102cedc3SLeonid Yegoshin 				else
765102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
766102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
767102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
7681da177e4SLinus Torvalds 				return 1;
7691da177e4SLinus Torvalds 			}
770102cedc3SLeonid Yegoshin 		}
771102cedc3SLeonid Yegoshin 		break;
772102cedc3SLeonid Yegoshin 	}
7731da177e4SLinus Torvalds 	return 0;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds /*
7771da177e4SLinus Torvalds  * In the Linux kernel, we support selection of FPR format on the
778da0bac33SDavid Daney  * basis of the Status.FR bit.	If an FPU is not present, the FR bit
779da0bac33SDavid Daney  * is hardwired to zero, which would imply a 32-bit FPU even for
780597ce172SPaul Burton  * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
78151d943f0SRalf Baechle  * FPU emu is slow and bulky and optimizing this function offers fairly
78251d943f0SRalf Baechle  * sizeable benefits so we try to be clever and make this function return
78351d943f0SRalf Baechle  * a constant whenever possible, that is on 64-bit kernels without O32
784597ce172SPaul Burton  * compatibility enabled and on 32-bit without 64-bit FPU support.
7851da177e4SLinus Torvalds  */
786da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp)
787da0bac33SDavid Daney {
78897f2645fSMasahiro Yamada 	if (IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_MIPS32_O32))
78951d943f0SRalf Baechle 		return 1;
79097f2645fSMasahiro Yamada 	else if (IS_ENABLED(CONFIG_32BIT) &&
79197f2645fSMasahiro Yamada 		 !IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
792da0bac33SDavid Daney 		return 0;
79308a07904SRalf Baechle 
794597ce172SPaul Burton 	return !test_thread_flag(TIF_32BIT_FPREGS);
795da0bac33SDavid Daney }
7961da177e4SLinus Torvalds 
7974227a2d4SPaul Burton static inline bool hybrid_fprs(void)
7984227a2d4SPaul Burton {
7994227a2d4SPaul Burton 	return test_thread_flag(TIF_HYBRID_FPREGS);
8004227a2d4SPaul Burton }
8014227a2d4SPaul Burton 
80247fa0c02SRalf Baechle #define SIFROMREG(si, x)						\
80347fa0c02SRalf Baechle do {									\
8044227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs())				\
805c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[x], 0);			\
806bbd426f5SPaul Burton 	else								\
807c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);	\
808bbd426f5SPaul Burton } while (0)
809da0bac33SDavid Daney 
81047fa0c02SRalf Baechle #define SITOREG(si, x)							\
81147fa0c02SRalf Baechle do {									\
8124227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs()) {			\
813ef1c47afSPaul Burton 		unsigned i;						\
814bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[x], 0, si);				\
815ef1c47afSPaul Burton 		for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)	\
816ef1c47afSPaul Burton 			set_fpr32(&ctx->fpr[x], i, 0);			\
817ef1c47afSPaul Burton 	} else {							\
818bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);		\
819ef1c47afSPaul Burton 	}								\
820bbd426f5SPaul Burton } while (0)
8211da177e4SLinus Torvalds 
822c8c0da6bSPaul Burton #define SIFROMHREG(si, x)	((si) = (int)get_fpr32(&ctx->fpr[x], 1))
823ef1c47afSPaul Burton 
82447fa0c02SRalf Baechle #define SITOHREG(si, x)							\
82547fa0c02SRalf Baechle do {									\
826ef1c47afSPaul Burton 	unsigned i;							\
827ef1c47afSPaul Burton 	set_fpr32(&ctx->fpr[x], 1, si);					\
828ef1c47afSPaul Burton 	for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)		\
829ef1c47afSPaul Burton 		set_fpr32(&ctx->fpr[x], i, 0);				\
830ef1c47afSPaul Burton } while (0)
8311ac94400SLeonid Yegoshin 
832bbd426f5SPaul Burton #define DIFROMREG(di, x)						\
8338535f2baSManuel Lauss 	((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) ^ 1)], 0))
834bbd426f5SPaul Burton 
83547fa0c02SRalf Baechle #define DITOREG(di, x)							\
83647fa0c02SRalf Baechle do {									\
837ef1c47afSPaul Burton 	unsigned fpr, i;						\
8388535f2baSManuel Lauss 	fpr = (x) & ~(cop1_64bit(xcp) ^ 1);				\
839ef1c47afSPaul Burton 	set_fpr64(&ctx->fpr[fpr], 0, di);				\
840ef1c47afSPaul Burton 	for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++)		\
841ef1c47afSPaul Burton 		set_fpr64(&ctx->fpr[fpr], i, 0);			\
842ef1c47afSPaul Burton } while (0)
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
8451da177e4SLinus Torvalds #define SPTOREG(sp, x)	SITOREG((sp).bits, x)
8461da177e4SLinus Torvalds #define DPFROMREG(dp, x)	DIFROMREG((dp).bits, x)
8471da177e4SLinus Torvalds #define DPTOREG(dp, x)	DITOREG((dp).bits, x)
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds /*
850d4f5b088SMaciej W. Rozycki  * Emulate a CFC1 instruction.
851d4f5b088SMaciej W. Rozycki  */
852d4f5b088SMaciej W. Rozycki static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
853d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
854d4f5b088SMaciej W. Rozycki {
855c491cfa2SMaciej W. Rozycki 	u32 fcr31 = ctx->fcr31;
856c491cfa2SMaciej W. Rozycki 	u32 value = 0;
857d4f5b088SMaciej W. Rozycki 
858c491cfa2SMaciej W. Rozycki 	switch (MIPSInst_RD(ir)) {
859c491cfa2SMaciej W. Rozycki 	case FPCREG_CSR:
860c491cfa2SMaciej W. Rozycki 		value = fcr31;
861d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-csr=%08x\n",
862c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
863c491cfa2SMaciej W. Rozycki 		break;
864c491cfa2SMaciej W. Rozycki 
865c491cfa2SMaciej W. Rozycki 	case FPCREG_FENR:
866c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
867c491cfa2SMaciej W. Rozycki 			break;
868c491cfa2SMaciej W. Rozycki 		value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
869c491cfa2SMaciej W. Rozycki 			MIPS_FENR_FS;
870c491cfa2SMaciej W. Rozycki 		value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
871c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-enr=%08x\n",
872c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
873c491cfa2SMaciej W. Rozycki 		break;
874c491cfa2SMaciej W. Rozycki 
875c491cfa2SMaciej W. Rozycki 	case FPCREG_FEXR:
876c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
877c491cfa2SMaciej W. Rozycki 			break;
878c491cfa2SMaciej W. Rozycki 		value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
879c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-exr=%08x\n",
880c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
881c491cfa2SMaciej W. Rozycki 		break;
882c491cfa2SMaciej W. Rozycki 
883c491cfa2SMaciej W. Rozycki 	case FPCREG_FCCR:
884c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
885c491cfa2SMaciej W. Rozycki 			break;
886c491cfa2SMaciej W. Rozycki 		value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
887c491cfa2SMaciej W. Rozycki 			MIPS_FCCR_COND0;
888c491cfa2SMaciej W. Rozycki 		value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
889c491cfa2SMaciej W. Rozycki 			 (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
890c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-ccr=%08x\n",
891c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
892c491cfa2SMaciej W. Rozycki 		break;
893c491cfa2SMaciej W. Rozycki 
894c491cfa2SMaciej W. Rozycki 	case FPCREG_RID:
89503dce595SMaciej W. Rozycki 		value = boot_cpu_data.fpu_id;
896c491cfa2SMaciej W. Rozycki 		break;
897c491cfa2SMaciej W. Rozycki 
898c491cfa2SMaciej W. Rozycki 	default:
899c491cfa2SMaciej W. Rozycki 		break;
900c491cfa2SMaciej W. Rozycki 	}
901c491cfa2SMaciej W. Rozycki 
902d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir))
903d4f5b088SMaciej W. Rozycki 		xcp->regs[MIPSInst_RT(ir)] = value;
904d4f5b088SMaciej W. Rozycki }
905d4f5b088SMaciej W. Rozycki 
906d4f5b088SMaciej W. Rozycki /*
907d4f5b088SMaciej W. Rozycki  * Emulate a CTC1 instruction.
908d4f5b088SMaciej W. Rozycki  */
909d4f5b088SMaciej W. Rozycki static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
910d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
911d4f5b088SMaciej W. Rozycki {
912c491cfa2SMaciej W. Rozycki 	u32 fcr31 = ctx->fcr31;
913d4f5b088SMaciej W. Rozycki 	u32 value;
9149b26616cSMaciej W. Rozycki 	u32 mask;
915d4f5b088SMaciej W. Rozycki 
916d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir) == 0)
917d4f5b088SMaciej W. Rozycki 		value = 0;
918d4f5b088SMaciej W. Rozycki 	else
919d4f5b088SMaciej W. Rozycki 		value = xcp->regs[MIPSInst_RT(ir)];
920d4f5b088SMaciej W. Rozycki 
921c491cfa2SMaciej W. Rozycki 	switch (MIPSInst_RD(ir)) {
922c491cfa2SMaciej W. Rozycki 	case FPCREG_CSR:
923d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->csr=%08x\n",
924c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
925d4f5b088SMaciej W. Rozycki 
9269b26616cSMaciej W. Rozycki 		/* Preserve read-only bits.  */
92703dce595SMaciej W. Rozycki 		mask = boot_cpu_data.fpu_msk31;
9289b26616cSMaciej W. Rozycki 		fcr31 = (value & ~mask) | (fcr31 & mask);
929c491cfa2SMaciej W. Rozycki 		break;
930c491cfa2SMaciej W. Rozycki 
931c491cfa2SMaciej W. Rozycki 	case FPCREG_FENR:
932c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
933c491cfa2SMaciej W. Rozycki 			break;
934c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->enr=%08x\n",
935c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
936c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
937c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
938c491cfa2SMaciej W. Rozycki 			 FPU_CSR_FS;
939c491cfa2SMaciej W. Rozycki 		fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
940c491cfa2SMaciej W. Rozycki 		break;
941c491cfa2SMaciej W. Rozycki 
942c491cfa2SMaciej W. Rozycki 	case FPCREG_FEXR:
943c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
944c491cfa2SMaciej W. Rozycki 			break;
945c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->exr=%08x\n",
946c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
947c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
948c491cfa2SMaciej W. Rozycki 		fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
949c491cfa2SMaciej W. Rozycki 		break;
950c491cfa2SMaciej W. Rozycki 
951c491cfa2SMaciej W. Rozycki 	case FPCREG_FCCR:
952c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
953c491cfa2SMaciej W. Rozycki 			break;
954c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->ccr=%08x\n",
955c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
956c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
957c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
958c491cfa2SMaciej W. Rozycki 			 FPU_CSR_COND;
959c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
960c491cfa2SMaciej W. Rozycki 			 FPU_CSR_CONDX;
961c491cfa2SMaciej W. Rozycki 		break;
962c491cfa2SMaciej W. Rozycki 
963c491cfa2SMaciej W. Rozycki 	default:
964c491cfa2SMaciej W. Rozycki 		break;
965d4f5b088SMaciej W. Rozycki 	}
966c491cfa2SMaciej W. Rozycki 
967c491cfa2SMaciej W. Rozycki 	ctx->fcr31 = fcr31;
968d4f5b088SMaciej W. Rozycki }
969d4f5b088SMaciej W. Rozycki 
970d4f5b088SMaciej W. Rozycki /*
9711da177e4SLinus Torvalds  * Emulate the single floating point instruction pointed at by EPC.
9721da177e4SLinus Torvalds  * Two instructions if the instruction is in a branch delay slot.
9731da177e4SLinus Torvalds  */
9741da177e4SLinus Torvalds 
975515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
976445a58ceSPaul Burton 		struct mm_decoded_insn dec_insn, void __user **fault_addr)
9771da177e4SLinus Torvalds {
978102cedc3SLeonid Yegoshin 	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
97993583e17SPaul Burton 	unsigned int cond, cbit, bit0;
9803f7cac41SRalf Baechle 	mips_instruction ir;
9813f7cac41SRalf Baechle 	int likely, pc_inc;
98293583e17SPaul Burton 	union fpureg *fpr;
9833f7cac41SRalf Baechle 	u32 __user *wva;
9843f7cac41SRalf Baechle 	u64 __user *dva;
9853f7cac41SRalf Baechle 	u32 wval;
9863f7cac41SRalf Baechle 	u64 dval;
9873f7cac41SRalf Baechle 	int sig;
9881da177e4SLinus Torvalds 
98970e4c234SRalf Baechle 	/*
99070e4c234SRalf Baechle 	 * These are giving gcc a gentle hint about what to expect in
99170e4c234SRalf Baechle 	 * dec_inst in order to do better optimization.
99270e4c234SRalf Baechle 	 */
99370e4c234SRalf Baechle 	if (!cpu_has_mmips && dec_insn.micro_mips_mode)
99470e4c234SRalf Baechle 		unreachable();
99570e4c234SRalf Baechle 
9961da177e4SLinus Torvalds 	/* XXX NEC Vr54xx bug workaround */
997e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
998102cedc3SLeonid Yegoshin 		if (dec_insn.micro_mips_mode) {
999102cedc3SLeonid Yegoshin 			if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
1000e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
1001102cedc3SLeonid Yegoshin 		} else {
1002102cedc3SLeonid Yegoshin 			if (!isBranchInstr(xcp, dec_insn, &contpc))
1003e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
1004102cedc3SLeonid Yegoshin 		}
1005102cedc3SLeonid Yegoshin 	}
10061da177e4SLinus Torvalds 
1007e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
10081da177e4SLinus Torvalds 		/*
10091da177e4SLinus Torvalds 		 * The instruction to be emulated is in a branch delay slot
10101da177e4SLinus Torvalds 		 * which means that we have to	emulate the branch instruction
10111da177e4SLinus Torvalds 		 * BEFORE we do the cop1 instruction.
10121da177e4SLinus Torvalds 		 *
10131da177e4SLinus Torvalds 		 * This branch could be a COP1 branch, but in that case we
10141da177e4SLinus Torvalds 		 * would have had a trap for that instruction, and would not
10151da177e4SLinus Torvalds 		 * come through this route.
10161da177e4SLinus Torvalds 		 *
10171da177e4SLinus Torvalds 		 * Linux MIPS branch emulator operates on context, updating the
10181da177e4SLinus Torvalds 		 * cp0_epc.
10191da177e4SLinus Torvalds 		 */
1020102cedc3SLeonid Yegoshin 		ir = dec_insn.next_insn;  /* process delay slot instr */
1021102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.next_pc_inc;
1022333d1f67SRalf Baechle 	} else {
1023102cedc3SLeonid Yegoshin 		ir = dec_insn.insn;       /* process current instr */
1024102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.pc_inc;
1025102cedc3SLeonid Yegoshin 	}
1026102cedc3SLeonid Yegoshin 
1027102cedc3SLeonid Yegoshin 	/*
1028102cedc3SLeonid Yegoshin 	 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
1029102cedc3SLeonid Yegoshin 	 * instructions, we want to convert microMIPS FPU instructions
1030102cedc3SLeonid Yegoshin 	 * into MIPS32 instructions so that we could reuse all of the
1031102cedc3SLeonid Yegoshin 	 * FPU emulation code.
1032102cedc3SLeonid Yegoshin 	 *
1033102cedc3SLeonid Yegoshin 	 * NOTE: We cannot do this for branch instructions since they
1034102cedc3SLeonid Yegoshin 	 *       are not a subset. Example: Cannot emulate a 16-bit
1035102cedc3SLeonid Yegoshin 	 *       aligned target address with a MIPS32 instruction.
1036102cedc3SLeonid Yegoshin 	 */
1037102cedc3SLeonid Yegoshin 	if (dec_insn.micro_mips_mode) {
1038102cedc3SLeonid Yegoshin 		/*
1039102cedc3SLeonid Yegoshin 		 * If next instruction is a 16-bit instruction, then it
1040102cedc3SLeonid Yegoshin 		 * it cannot be a FPU instruction. This could happen
1041102cedc3SLeonid Yegoshin 		 * since we can be called for non-FPU instructions.
1042102cedc3SLeonid Yegoshin 		 */
1043102cedc3SLeonid Yegoshin 		if ((pc_inc == 2) ||
1044102cedc3SLeonid Yegoshin 			(microMIPS32_to_MIPS32((union mips_instruction *)&ir)
1045102cedc3SLeonid Yegoshin 			 == SIGILL))
1046102cedc3SLeonid Yegoshin 			return SIGILL;
10471da177e4SLinus Torvalds 	}
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds emul:
1050a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
1051b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(emulated);
10521da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(ir)) {
10533f7cac41SRalf Baechle 	case ldc1_op:
10543f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10551da177e4SLinus Torvalds 				     MIPSInst_SIMM(ir));
1056b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
1057515b029dSDavid Daney 
10583f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
1059b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10603f7cac41SRalf Baechle 			*fault_addr = dva;
10611da177e4SLinus Torvalds 			return SIGBUS;
10621da177e4SLinus Torvalds 		}
10633f7cac41SRalf Baechle 		if (__get_user(dval, dva)) {
1064515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10653f7cac41SRalf Baechle 			*fault_addr = dva;
1066515b029dSDavid Daney 			return SIGSEGV;
1067515b029dSDavid Daney 		}
10683f7cac41SRalf Baechle 		DITOREG(dval, MIPSInst_RT(ir));
10691da177e4SLinus Torvalds 		break;
10701da177e4SLinus Torvalds 
10713f7cac41SRalf Baechle 	case sdc1_op:
10723f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10731da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1074b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
10753f7cac41SRalf Baechle 		DIFROMREG(dval, MIPSInst_RT(ir));
10763f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
1077b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10783f7cac41SRalf Baechle 			*fault_addr = dva;
10791da177e4SLinus Torvalds 			return SIGBUS;
10801da177e4SLinus Torvalds 		}
10813f7cac41SRalf Baechle 		if (__put_user(dval, dva)) {
1082515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10833f7cac41SRalf Baechle 			*fault_addr = dva;
1084515b029dSDavid Daney 			return SIGSEGV;
1085515b029dSDavid Daney 		}
10861da177e4SLinus Torvalds 		break;
10871da177e4SLinus Torvalds 
10883f7cac41SRalf Baechle 	case lwc1_op:
10893f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10901da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1091b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
10923f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
1093b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10943f7cac41SRalf Baechle 			*fault_addr = wva;
10951da177e4SLinus Torvalds 			return SIGBUS;
10961da177e4SLinus Torvalds 		}
10973f7cac41SRalf Baechle 		if (__get_user(wval, wva)) {
1098515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10993f7cac41SRalf Baechle 			*fault_addr = wva;
1100515b029dSDavid Daney 			return SIGSEGV;
1101515b029dSDavid Daney 		}
11023f7cac41SRalf Baechle 		SITOREG(wval, MIPSInst_RT(ir));
11031da177e4SLinus Torvalds 		break;
11041da177e4SLinus Torvalds 
11053f7cac41SRalf Baechle 	case swc1_op:
11063f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
11071da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1108b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
11093f7cac41SRalf Baechle 		SIFROMREG(wval, MIPSInst_RT(ir));
11103f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
1111b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11123f7cac41SRalf Baechle 			*fault_addr = wva;
11131da177e4SLinus Torvalds 			return SIGBUS;
11141da177e4SLinus Torvalds 		}
11153f7cac41SRalf Baechle 		if (__put_user(wval, wva)) {
1116515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11173f7cac41SRalf Baechle 			*fault_addr = wva;
1118515b029dSDavid Daney 			return SIGSEGV;
1119515b029dSDavid Daney 		}
11201da177e4SLinus Torvalds 		break;
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	case cop1_op:
11231da177e4SLinus Torvalds 		switch (MIPSInst_RS(ir)) {
11241da177e4SLinus Torvalds 		case dmfc_op:
112508a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
112608a07904SRalf Baechle 				return SIGILL;
112708a07904SRalf Baechle 
11281da177e4SLinus Torvalds 			/* copregister fs -> gpr[rt] */
11291da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
11301da177e4SLinus Torvalds 				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
11311da177e4SLinus Torvalds 					MIPSInst_RD(ir));
11321da177e4SLinus Torvalds 			}
11331da177e4SLinus Torvalds 			break;
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 		case dmtc_op:
113608a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
113708a07904SRalf Baechle 				return SIGILL;
113808a07904SRalf Baechle 
11391da177e4SLinus Torvalds 			/* copregister fs <- rt */
11401da177e4SLinus Torvalds 			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11411da177e4SLinus Torvalds 			break;
11421da177e4SLinus Torvalds 
11431ac94400SLeonid Yegoshin 		case mfhc_op:
1144e8f80cc1SMarkos Chandras 			if (!cpu_has_mips_r2_r6)
114570f743d1SMaciej W. Rozycki 				return SIGILL;
11461ac94400SLeonid Yegoshin 
11471ac94400SLeonid Yegoshin 			/* copregister rd -> gpr[rt] */
11481ac94400SLeonid Yegoshin 			if (MIPSInst_RT(ir) != 0) {
11491ac94400SLeonid Yegoshin 				SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
11501ac94400SLeonid Yegoshin 					MIPSInst_RD(ir));
11511ac94400SLeonid Yegoshin 			}
11521ac94400SLeonid Yegoshin 			break;
11531ac94400SLeonid Yegoshin 
11541ac94400SLeonid Yegoshin 		case mthc_op:
1155e8f80cc1SMarkos Chandras 			if (!cpu_has_mips_r2_r6)
115670f743d1SMaciej W. Rozycki 				return SIGILL;
11571ac94400SLeonid Yegoshin 
11581ac94400SLeonid Yegoshin 			/* copregister rd <- gpr[rt] */
11591ac94400SLeonid Yegoshin 			SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11601ac94400SLeonid Yegoshin 			break;
11611ac94400SLeonid Yegoshin 
11621da177e4SLinus Torvalds 		case mfc_op:
11631da177e4SLinus Torvalds 			/* copregister rd -> gpr[rt] */
11641da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
11651da177e4SLinus Torvalds 				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
11661da177e4SLinus Torvalds 					MIPSInst_RD(ir));
11671da177e4SLinus Torvalds 			}
11681da177e4SLinus Torvalds 			break;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 		case mtc_op:
11711da177e4SLinus Torvalds 			/* copregister rd <- rt */
11721da177e4SLinus Torvalds 			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11731da177e4SLinus Torvalds 			break;
11741da177e4SLinus Torvalds 
11753f7cac41SRalf Baechle 		case cfc_op:
11761da177e4SLinus Torvalds 			/* cop control register rd -> gpr[rt] */
1177d4f5b088SMaciej W. Rozycki 			cop1_cfc(xcp, ctx, ir);
11781da177e4SLinus Torvalds 			break;
11791da177e4SLinus Torvalds 
11803f7cac41SRalf Baechle 		case ctc_op:
11811da177e4SLinus Torvalds 			/* copregister rd <- rt */
1182d4f5b088SMaciej W. Rozycki 			cop1_ctc(xcp, ctx, ir);
11831da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
11841da177e4SLinus Torvalds 				return SIGFPE;
11851da177e4SLinus Torvalds 			}
11861da177e4SLinus Torvalds 			break;
11871da177e4SLinus Torvalds 
1188c909ca71SMarkos Chandras 		case bc1eqz_op:
1189c909ca71SMarkos Chandras 		case bc1nez_op:
1190c909ca71SMarkos Chandras 			if (!cpu_has_mips_r6 || delay_slot(xcp))
1191c909ca71SMarkos Chandras 				return SIGILL;
1192c909ca71SMarkos Chandras 
1193c909ca71SMarkos Chandras 			cond = likely = 0;
119493583e17SPaul Burton 			fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
119593583e17SPaul Burton 			bit0 = get_fpr32(fpr, 0) & 0x1;
1196c909ca71SMarkos Chandras 			switch (MIPSInst_RS(ir)) {
1197c909ca71SMarkos Chandras 			case bc1eqz_op:
1198454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(bc1eqz);
119993583e17SPaul Burton 				cond = bit0 == 0;
1200c909ca71SMarkos Chandras 				break;
1201c909ca71SMarkos Chandras 			case bc1nez_op:
1202454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(bc1nez);
120393583e17SPaul Burton 				cond = bit0 != 0;
1204c909ca71SMarkos Chandras 				break;
1205c909ca71SMarkos Chandras 			}
1206c909ca71SMarkos Chandras 			goto branch_common;
1207c909ca71SMarkos Chandras 
12083f7cac41SRalf Baechle 		case bc_op:
1209e7e9cae5SRalf Baechle 			if (delay_slot(xcp))
12101da177e4SLinus Torvalds 				return SIGILL;
12111da177e4SLinus Torvalds 
121208a07904SRalf Baechle 			if (cpu_has_mips_4_5_r)
121308a07904SRalf Baechle 				cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
121408a07904SRalf Baechle 			else
121508a07904SRalf Baechle 				cbit = FPU_CSR_COND;
121608a07904SRalf Baechle 			cond = ctx->fcr31 & cbit;
121708a07904SRalf Baechle 
12183f7cac41SRalf Baechle 			likely = 0;
12191da177e4SLinus Torvalds 			switch (MIPSInst_RT(ir) & 3) {
12201da177e4SLinus Torvalds 			case bcfl_op:
12212d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
12221da177e4SLinus Torvalds 					likely = 1;
12232d83fea7SMaciej W. Rozycki 				/* Fall through */
12241da177e4SLinus Torvalds 			case bcf_op:
12251da177e4SLinus Torvalds 				cond = !cond;
12261da177e4SLinus Torvalds 				break;
12271da177e4SLinus Torvalds 			case bctl_op:
12282d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
12291da177e4SLinus Torvalds 					likely = 1;
12302d83fea7SMaciej W. Rozycki 				/* Fall through */
12311da177e4SLinus Torvalds 			case bct_op:
12321da177e4SLinus Torvalds 				break;
12331da177e4SLinus Torvalds 			}
1234c909ca71SMarkos Chandras branch_common:
1235ae5f3f5bSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(branches);
1236e7e9cae5SRalf Baechle 			set_delay_slot(xcp);
12371da177e4SLinus Torvalds 			if (cond) {
12383f7cac41SRalf Baechle 				/*
12393f7cac41SRalf Baechle 				 * Branch taken: emulate dslot instruction
12401da177e4SLinus Torvalds 				 */
12419ab4471cSMaciej W. Rozycki 				unsigned long bcpc;
12429ab4471cSMaciej W. Rozycki 
12439ab4471cSMaciej W. Rozycki 				/*
12449ab4471cSMaciej W. Rozycki 				 * Remember EPC at the branch to point back
12459ab4471cSMaciej W. Rozycki 				 * at so that any delay-slot instruction
12469ab4471cSMaciej W. Rozycki 				 * signal is not silently ignored.
12479ab4471cSMaciej W. Rozycki 				 */
12489ab4471cSMaciej W. Rozycki 				bcpc = xcp->cp0_epc;
1249102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
12501da177e4SLinus Torvalds 
1251102cedc3SLeonid Yegoshin 				contpc = MIPSInst_SIMM(ir);
1252102cedc3SLeonid Yegoshin 				ir = dec_insn.next_insn;
1253102cedc3SLeonid Yegoshin 				if (dec_insn.micro_mips_mode) {
1254102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 1));
1255102cedc3SLeonid Yegoshin 
1256102cedc3SLeonid Yegoshin 					/* If 16-bit instruction, not FPU. */
1257102cedc3SLeonid Yegoshin 					if ((dec_insn.next_pc_inc == 2) ||
1258102cedc3SLeonid Yegoshin 						(microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1259102cedc3SLeonid Yegoshin 
1260102cedc3SLeonid Yegoshin 						/*
1261102cedc3SLeonid Yegoshin 						 * Since this instruction will
1262102cedc3SLeonid Yegoshin 						 * be put on the stack with
1263102cedc3SLeonid Yegoshin 						 * 32-bit words, get around
1264102cedc3SLeonid Yegoshin 						 * this problem by putting a
1265102cedc3SLeonid Yegoshin 						 * NOP16 as the second one.
1266102cedc3SLeonid Yegoshin 						 */
1267102cedc3SLeonid Yegoshin 						if (dec_insn.next_pc_inc == 2)
1268102cedc3SLeonid Yegoshin 							ir = (ir & (~0xffff)) | MM_NOP16;
1269102cedc3SLeonid Yegoshin 
1270102cedc3SLeonid Yegoshin 						/*
1271102cedc3SLeonid Yegoshin 						 * Single step the non-CP1
1272102cedc3SLeonid Yegoshin 						 * instruction in the dslot.
1273102cedc3SLeonid Yegoshin 						 */
12749ab4471cSMaciej W. Rozycki 						sig = mips_dsemul(xcp, ir,
1275432c6bacSPaul Burton 								  bcpc, contpc);
1276e4553573SMaciej W. Rozycki 						if (sig < 0)
1277e4553573SMaciej W. Rozycki 							break;
12789ab4471cSMaciej W. Rozycki 						if (sig)
12799ab4471cSMaciej W. Rozycki 							xcp->cp0_epc = bcpc;
12809ab4471cSMaciej W. Rozycki 						/*
12819ab4471cSMaciej W. Rozycki 						 * SIGILL forces out of
12829ab4471cSMaciej W. Rozycki 						 * the emulation loop.
12839ab4471cSMaciej W. Rozycki 						 */
12849ab4471cSMaciej W. Rozycki 						return sig ? sig : SIGILL;
1285515b029dSDavid Daney 					}
1286102cedc3SLeonid Yegoshin 				} else
1287102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 2));
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 				switch (MIPSInst_OPCODE(ir)) {
12901da177e4SLinus Torvalds 				case lwc1_op:
12911da177e4SLinus Torvalds 				case swc1_op:
129208a07904SRalf Baechle 					goto emul;
12933f7cac41SRalf Baechle 
12941da177e4SLinus Torvalds 				case ldc1_op:
12951da177e4SLinus Torvalds 				case sdc1_op:
12962d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_2_3_4_5_r)
129708a07904SRalf Baechle 						goto emul;
129808a07904SRalf Baechle 
12999ab4471cSMaciej W. Rozycki 					goto bc_sigill;
13003f7cac41SRalf Baechle 
13011da177e4SLinus Torvalds 				case cop1_op:
130208a07904SRalf Baechle 					goto emul;
13033f7cac41SRalf Baechle 
13041da177e4SLinus Torvalds 				case cop1x_op:
13052d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_4_5_64_r2_r6)
13061da177e4SLinus Torvalds 						/* its one of ours */
13071da177e4SLinus Torvalds 						goto emul;
130808a07904SRalf Baechle 
13099ab4471cSMaciej W. Rozycki 					goto bc_sigill;
13103f7cac41SRalf Baechle 
13111da177e4SLinus Torvalds 				case spec_op:
13122d83fea7SMaciej W. Rozycki 					switch (MIPSInst_FUNC(ir)) {
13132d83fea7SMaciej W. Rozycki 					case movc_op:
13142d83fea7SMaciej W. Rozycki 						if (cpu_has_mips_4_5_r)
13151da177e4SLinus Torvalds 							goto emul;
13162d83fea7SMaciej W. Rozycki 
13179ab4471cSMaciej W. Rozycki 						goto bc_sigill;
13182d83fea7SMaciej W. Rozycki 					}
13191da177e4SLinus Torvalds 					break;
13209ab4471cSMaciej W. Rozycki 
13219ab4471cSMaciej W. Rozycki 				bc_sigill:
13229ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
13239ab4471cSMaciej W. Rozycki 					return SIGILL;
13241da177e4SLinus Torvalds 				}
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 				/*
13271da177e4SLinus Torvalds 				 * Single step the non-cp1
13281da177e4SLinus Torvalds 				 * instruction in the dslot
13291da177e4SLinus Torvalds 				 */
1330432c6bacSPaul Burton 				sig = mips_dsemul(xcp, ir, bcpc, contpc);
1331e4553573SMaciej W. Rozycki 				if (sig < 0)
1332e4553573SMaciej W. Rozycki 					break;
13339ab4471cSMaciej W. Rozycki 				if (sig)
13349ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
13359ab4471cSMaciej W. Rozycki 				/* SIGILL forces out of the emulation loop.  */
13369ab4471cSMaciej W. Rozycki 				return sig ? sig : SIGILL;
13373f7cac41SRalf Baechle 			} else if (likely) {	/* branch not taken */
13381da177e4SLinus Torvalds 				/*
13391da177e4SLinus Torvalds 				 * branch likely nullifies
13401da177e4SLinus Torvalds 				 * dslot if not taken
13411da177e4SLinus Torvalds 				 */
1342102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
1343102cedc3SLeonid Yegoshin 				contpc += dec_insn.pc_inc;
13441da177e4SLinus Torvalds 				/*
13451da177e4SLinus Torvalds 				 * else continue & execute
13461da177e4SLinus Torvalds 				 * dslot as normal insn
13471da177e4SLinus Torvalds 				 */
13481da177e4SLinus Torvalds 			}
13491da177e4SLinus Torvalds 			break;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 		default:
13521da177e4SLinus Torvalds 			if (!(MIPSInst_RS(ir) & 0x10))
13531da177e4SLinus Torvalds 				return SIGILL;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 			/* a real fpu computation instruction */
13561da177e4SLinus Torvalds 			if ((sig = fpu_emu(xcp, ctx, ir)))
13571da177e4SLinus Torvalds 				return sig;
13581da177e4SLinus Torvalds 		}
13591da177e4SLinus Torvalds 		break;
13601da177e4SLinus Torvalds 
13613f7cac41SRalf Baechle 	case cop1x_op:
13622d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_4_5_64_r2_r6)
136308a07904SRalf Baechle 			return SIGILL;
136408a07904SRalf Baechle 
136508a07904SRalf Baechle 		sig = fpux_emu(xcp, ctx, ir, fault_addr);
1366515b029dSDavid Daney 		if (sig)
13671da177e4SLinus Torvalds 			return sig;
13681da177e4SLinus Torvalds 		break;
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	case spec_op:
137108a07904SRalf Baechle 		if (!cpu_has_mips_4_5_r)
137208a07904SRalf Baechle 			return SIGILL;
137308a07904SRalf Baechle 
13741da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != movc_op)
13751da177e4SLinus Torvalds 			return SIGILL;
13761da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
13771da177e4SLinus Torvalds 		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
13781da177e4SLinus Torvalds 			xcp->regs[MIPSInst_RD(ir)] =
13791da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RS(ir)];
13801da177e4SLinus Torvalds 		break;
13811da177e4SLinus Torvalds 	default:
13821da177e4SLinus Torvalds 		return SIGILL;
13831da177e4SLinus Torvalds 	}
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	/* we did it !! */
1386e70dfc10SAtsushi Nemoto 	xcp->cp0_epc = contpc;
1387e7e9cae5SRalf Baechle 	clear_delay_slot(xcp);
1388333d1f67SRalf Baechle 
13891da177e4SLinus Torvalds 	return 0;
13901da177e4SLinus Torvalds }
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds /*
13931da177e4SLinus Torvalds  * Conversion table from MIPS compare ops 48-63
13941da177e4SLinus Torvalds  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
13951da177e4SLinus Torvalds  */
13961da177e4SLinus Torvalds static const unsigned char cmptab[8] = {
13971da177e4SLinus Torvalds 	0,			/* cmp_0 (sig) cmp_sf */
13981da177e4SLinus Torvalds 	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
13991da177e4SLinus Torvalds 	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
14001da177e4SLinus Torvalds 	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
14011da177e4SLinus Torvalds 	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
14021da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
14031da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
14041da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
14051da177e4SLinus Torvalds };
14061da177e4SLinus Torvalds 
1407f8c3c671SMarkos Chandras static const unsigned char negative_cmptab[8] = {
1408f8c3c671SMarkos Chandras 	0, /* Reserved */
1409f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
1410f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
1411f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT,
1412f8c3c671SMarkos Chandras 	/* Reserved */
1413f8c3c671SMarkos Chandras };
1414f8c3c671SMarkos Chandras 
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds /*
14171da177e4SLinus Torvalds  * Additional MIPS4 instructions
14181da177e4SLinus Torvalds  */
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3)					\
142147fa0c02SRalf Baechle static union ieee754##p fpemu_##p##_##name(union ieee754##p r,		\
142247fa0c02SRalf Baechle 	union ieee754##p s, union ieee754##p t)				\
14231da177e4SLinus Torvalds {									\
1424cd21dfcfSRalf Baechle 	struct _ieee754_csr ieee754_csr_save;				\
14251da177e4SLinus Torvalds 	s = f1(s, t);							\
14261da177e4SLinus Torvalds 	ieee754_csr_save = ieee754_csr;					\
14271da177e4SLinus Torvalds 	s = f2(s, r);							\
14281da177e4SLinus Torvalds 	ieee754_csr_save.cx |= ieee754_csr.cx;				\
14291da177e4SLinus Torvalds 	ieee754_csr_save.sx |= ieee754_csr.sx;				\
14301da177e4SLinus Torvalds 	s = f3(s);							\
14311da177e4SLinus Torvalds 	ieee754_csr.cx |= ieee754_csr_save.cx;				\
14321da177e4SLinus Torvalds 	ieee754_csr.sx |= ieee754_csr_save.sx;				\
14331da177e4SLinus Torvalds 	return s;							\
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
14362209bcb1SRalf Baechle static union ieee754dp fpemu_dp_recip(union ieee754dp d)
14371da177e4SLinus Torvalds {
14381da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), d);
14391da177e4SLinus Torvalds }
14401da177e4SLinus Torvalds 
14412209bcb1SRalf Baechle static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
14421da177e4SLinus Torvalds {
14431da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
14441da177e4SLinus Torvalds }
14451da177e4SLinus Torvalds 
14462209bcb1SRalf Baechle static union ieee754sp fpemu_sp_recip(union ieee754sp s)
14471da177e4SLinus Torvalds {
14481da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), s);
14491da177e4SLinus Torvalds }
14501da177e4SLinus Torvalds 
14512209bcb1SRalf Baechle static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
14521da177e4SLinus Torvalds {
14531da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
14541da177e4SLinus Torvalds }
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
14571da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
14581da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
14591da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
14601da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
14611da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
14621da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
14631da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
14641da177e4SLinus Torvalds 
1465eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1466445a58ceSPaul Burton 	mips_instruction ir, void __user **fault_addr)
14671da177e4SLinus Torvalds {
14681da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
14691da177e4SLinus Torvalds 
1470b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1xops);
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 	switch (MIPSInst_FMA_FFMT(ir)) {
14731da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
14741da177e4SLinus Torvalds 
14752209bcb1SRalf Baechle 		union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
14762209bcb1SRalf Baechle 		union ieee754sp fd, fr, fs, ft;
14773fccc015SRalf Baechle 		u32 __user *va;
14781da177e4SLinus Torvalds 		u32 val;
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
14811da177e4SLinus Torvalds 		case lwxc1_op:
14823fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14831da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14841da177e4SLinus Torvalds 
1485b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1486515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1487b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1488515b029dSDavid Daney 				*fault_addr = va;
14891da177e4SLinus Torvalds 				return SIGBUS;
14901da177e4SLinus Torvalds 			}
1491515b029dSDavid Daney 			if (__get_user(val, va)) {
1492515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1493515b029dSDavid Daney 				*fault_addr = va;
1494515b029dSDavid Daney 				return SIGSEGV;
1495515b029dSDavid Daney 			}
14961da177e4SLinus Torvalds 			SITOREG(val, MIPSInst_FD(ir));
14971da177e4SLinus Torvalds 			break;
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 		case swxc1_op:
15003fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15011da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15021da177e4SLinus Torvalds 
1503b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 			SIFROMREG(val, MIPSInst_FS(ir));
1506515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1507515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1508515b029dSDavid Daney 				*fault_addr = va;
1509515b029dSDavid Daney 				return SIGBUS;
1510515b029dSDavid Daney 			}
15111da177e4SLinus Torvalds 			if (put_user(val, va)) {
1512b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1513515b029dSDavid Daney 				*fault_addr = va;
1514515b029dSDavid Daney 				return SIGSEGV;
15151da177e4SLinus Torvalds 			}
15161da177e4SLinus Torvalds 			break;
15171da177e4SLinus Torvalds 
15181da177e4SLinus Torvalds 		case madd_s_op:
15191da177e4SLinus Torvalds 			handler = fpemu_sp_madd;
15201da177e4SLinus Torvalds 			goto scoptop;
15211da177e4SLinus Torvalds 		case msub_s_op:
15221da177e4SLinus Torvalds 			handler = fpemu_sp_msub;
15231da177e4SLinus Torvalds 			goto scoptop;
15241da177e4SLinus Torvalds 		case nmadd_s_op:
15251da177e4SLinus Torvalds 			handler = fpemu_sp_nmadd;
15261da177e4SLinus Torvalds 			goto scoptop;
15271da177e4SLinus Torvalds 		case nmsub_s_op:
15281da177e4SLinus Torvalds 			handler = fpemu_sp_nmsub;
15291da177e4SLinus Torvalds 			goto scoptop;
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 		      scoptop:
15321da177e4SLinus Torvalds 			SPFROMREG(fr, MIPSInst_FR(ir));
15331da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
15341da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
15351da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
15361da177e4SLinus Torvalds 			SPTOREG(fd, MIPSInst_FD(ir));
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 		      copcsr:
1539c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1540c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
15411da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1542c4103526SDeng-Cheng Zhu 			}
1543c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1544c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
15451da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1546c4103526SDeng-Cheng Zhu 			}
1547c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1548c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
15491da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1550c4103526SDeng-Cheng Zhu 			}
1551c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1552c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
15531da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1554c4103526SDeng-Cheng Zhu 			}
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
15571da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
15583f7cac41SRalf Baechle 				/*printk ("SIGFPE: FPU csr = %08x\n",
15591da177e4SLinus Torvalds 				   ctx->fcr31); */
15601da177e4SLinus Torvalds 				return SIGFPE;
15611da177e4SLinus Torvalds 			}
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 			break;
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 		default:
15661da177e4SLinus Torvalds 			return SIGILL;
15671da177e4SLinus Torvalds 		}
15681da177e4SLinus Torvalds 		break;
15691da177e4SLinus Torvalds 	}
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	case d_fmt:{		/* 1 */
15722209bcb1SRalf Baechle 		union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
15732209bcb1SRalf Baechle 		union ieee754dp fd, fr, fs, ft;
15743fccc015SRalf Baechle 		u64 __user *va;
15751da177e4SLinus Torvalds 		u64 val;
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
15781da177e4SLinus Torvalds 		case ldxc1_op:
15793fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15801da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15811da177e4SLinus Torvalds 
1582b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1583515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1584b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1585515b029dSDavid Daney 				*fault_addr = va;
15861da177e4SLinus Torvalds 				return SIGBUS;
15871da177e4SLinus Torvalds 			}
1588515b029dSDavid Daney 			if (__get_user(val, va)) {
1589515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1590515b029dSDavid Daney 				*fault_addr = va;
1591515b029dSDavid Daney 				return SIGSEGV;
1592515b029dSDavid Daney 			}
15931da177e4SLinus Torvalds 			DITOREG(val, MIPSInst_FD(ir));
15941da177e4SLinus Torvalds 			break;
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 		case sdxc1_op:
15973fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15981da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15991da177e4SLinus Torvalds 
1600b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
16011da177e4SLinus Torvalds 			DIFROMREG(val, MIPSInst_FS(ir));
1602515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1603b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1604515b029dSDavid Daney 				*fault_addr = va;
16051da177e4SLinus Torvalds 				return SIGBUS;
16061da177e4SLinus Torvalds 			}
1607515b029dSDavid Daney 			if (__put_user(val, va)) {
1608515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1609515b029dSDavid Daney 				*fault_addr = va;
1610515b029dSDavid Daney 				return SIGSEGV;
1611515b029dSDavid Daney 			}
16121da177e4SLinus Torvalds 			break;
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 		case madd_d_op:
16151da177e4SLinus Torvalds 			handler = fpemu_dp_madd;
16161da177e4SLinus Torvalds 			goto dcoptop;
16171da177e4SLinus Torvalds 		case msub_d_op:
16181da177e4SLinus Torvalds 			handler = fpemu_dp_msub;
16191da177e4SLinus Torvalds 			goto dcoptop;
16201da177e4SLinus Torvalds 		case nmadd_d_op:
16211da177e4SLinus Torvalds 			handler = fpemu_dp_nmadd;
16221da177e4SLinus Torvalds 			goto dcoptop;
16231da177e4SLinus Torvalds 		case nmsub_d_op:
16241da177e4SLinus Torvalds 			handler = fpemu_dp_nmsub;
16251da177e4SLinus Torvalds 			goto dcoptop;
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 		      dcoptop:
16281da177e4SLinus Torvalds 			DPFROMREG(fr, MIPSInst_FR(ir));
16291da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
16301da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
16311da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
16321da177e4SLinus Torvalds 			DPTOREG(fd, MIPSInst_FD(ir));
16331da177e4SLinus Torvalds 			goto copcsr;
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 		default:
16361da177e4SLinus Torvalds 			return SIGILL;
16371da177e4SLinus Torvalds 		}
16381da177e4SLinus Torvalds 		break;
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds 
164151061b88SDeng-Cheng Zhu 	case 0x3:
164251061b88SDeng-Cheng Zhu 		if (MIPSInst_FUNC(ir) != pfetch_op)
16431da177e4SLinus Torvalds 			return SIGILL;
164451061b88SDeng-Cheng Zhu 
16451da177e4SLinus Torvalds 		/* ignore prefx operation */
16461da177e4SLinus Torvalds 		break;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	default:
16491da177e4SLinus Torvalds 		return SIGILL;
16501da177e4SLinus Torvalds 	}
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	return 0;
16531da177e4SLinus Torvalds }
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds /*
16581da177e4SLinus Torvalds  * Emulate a single COP1 arithmetic instruction.
16591da177e4SLinus Torvalds  */
1660eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
16611da177e4SLinus Torvalds 	mips_instruction ir)
16621da177e4SLinus Torvalds {
16631da177e4SLinus Torvalds 	int rfmt;		/* resulting format */
16641da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
16653f7cac41SRalf Baechle 	unsigned int oldrm;
16663f7cac41SRalf Baechle 	unsigned int cbit;
16671da177e4SLinus Torvalds 	unsigned cond;
16681da177e4SLinus Torvalds 	union {
16692209bcb1SRalf Baechle 		union ieee754dp d;
16702209bcb1SRalf Baechle 		union ieee754sp s;
16711da177e4SLinus Torvalds 		int w;
16721da177e4SLinus Torvalds 		s64 l;
16731da177e4SLinus Torvalds 	} rv;			/* resulting value */
16743f7cac41SRalf Baechle 	u64 bits;
16751da177e4SLinus Torvalds 
1676b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1ops);
16771da177e4SLinus Torvalds 	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
16781da177e4SLinus Torvalds 	case s_fmt: {		/* 0 */
16791da177e4SLinus Torvalds 		union {
16802209bcb1SRalf Baechle 			union ieee754sp(*b) (union ieee754sp, union ieee754sp);
16812209bcb1SRalf Baechle 			union ieee754sp(*u) (union ieee754sp);
16821da177e4SLinus Torvalds 		} handler;
16834b820d95SPaul Burton 		union ieee754sp fd, fs, ft;
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
16861da177e4SLinus Torvalds 			/* binary ops */
16871da177e4SLinus Torvalds 		case fadd_op:
1688454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(add_s);
16891da177e4SLinus Torvalds 			handler.b = ieee754sp_add;
16901da177e4SLinus Torvalds 			goto scopbop;
16911da177e4SLinus Torvalds 		case fsub_op:
1692454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sub_s);
16931da177e4SLinus Torvalds 			handler.b = ieee754sp_sub;
16941da177e4SLinus Torvalds 			goto scopbop;
16951da177e4SLinus Torvalds 		case fmul_op:
1696454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mul_s);
16971da177e4SLinus Torvalds 			handler.b = ieee754sp_mul;
16981da177e4SLinus Torvalds 			goto scopbop;
16991da177e4SLinus Torvalds 		case fdiv_op:
1700454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(div_s);
17011da177e4SLinus Torvalds 			handler.b = ieee754sp_div;
17021da177e4SLinus Torvalds 			goto scopbop;
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds 			/* unary  ops */
17051da177e4SLinus Torvalds 		case fsqrt_op:
17062d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
170708a07904SRalf Baechle 				return SIGILL;
170808a07904SRalf Baechle 
1709454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sqrt_s);
17101da177e4SLinus Torvalds 			handler.u = ieee754sp_sqrt;
17111da177e4SLinus Torvalds 			goto scopuop;
17123f7cac41SRalf Baechle 
171308a07904SRalf Baechle 		/*
171408a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
171508a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
171608a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
171708a07904SRalf Baechle 		 */
17181da177e4SLinus Torvalds 		case frsqrt_op:
17192d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
172008a07904SRalf Baechle 				return SIGILL;
172108a07904SRalf Baechle 
1722454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rsqrt_s);
17231da177e4SLinus Torvalds 			handler.u = fpemu_sp_rsqrt;
17241da177e4SLinus Torvalds 			goto scopuop;
17253f7cac41SRalf Baechle 
17261da177e4SLinus Torvalds 		case frecip_op:
17272d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
172808a07904SRalf Baechle 				return SIGILL;
172908a07904SRalf Baechle 
1730454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(recip_s);
17311da177e4SLinus Torvalds 			handler.u = fpemu_sp_recip;
17321da177e4SLinus Torvalds 			goto scopuop;
173308a07904SRalf Baechle 
17341da177e4SLinus Torvalds 		case fmovc_op:
173508a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
173608a07904SRalf Baechle 				return SIGILL;
173708a07904SRalf Baechle 
17381da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
17391da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
17401da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
17411da177e4SLinus Torvalds 				return 0;
17421da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17431da177e4SLinus Torvalds 			break;
17443f7cac41SRalf Baechle 
17451da177e4SLinus Torvalds 		case fmovz_op:
174608a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
174708a07904SRalf Baechle 				return SIGILL;
174808a07904SRalf Baechle 
17491da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
17501da177e4SLinus Torvalds 				return 0;
17511da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17521da177e4SLinus Torvalds 			break;
17533f7cac41SRalf Baechle 
17541da177e4SLinus Torvalds 		case fmovn_op:
175508a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
175608a07904SRalf Baechle 				return SIGILL;
175708a07904SRalf Baechle 
17581da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
17591da177e4SLinus Torvalds 				return 0;
17601da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17611da177e4SLinus Torvalds 			break;
17623f7cac41SRalf Baechle 
176367613f02SMarkos Chandras 		case fseleqz_op:
176467613f02SMarkos Chandras 			if (!cpu_has_mips_r6)
176567613f02SMarkos Chandras 				return SIGILL;
176667613f02SMarkos Chandras 
1767454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(seleqz_s);
176867613f02SMarkos Chandras 			SPFROMREG(rv.s, MIPSInst_FT(ir));
176967613f02SMarkos Chandras 			if (rv.w & 0x1)
177067613f02SMarkos Chandras 				rv.w = 0;
177167613f02SMarkos Chandras 			else
177267613f02SMarkos Chandras 				SPFROMREG(rv.s, MIPSInst_FS(ir));
177367613f02SMarkos Chandras 			break;
177467613f02SMarkos Chandras 
1775130fe357SMarkos Chandras 		case fselnez_op:
1776130fe357SMarkos Chandras 			if (!cpu_has_mips_r6)
1777130fe357SMarkos Chandras 				return SIGILL;
1778130fe357SMarkos Chandras 
1779454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(selnez_s);
1780130fe357SMarkos Chandras 			SPFROMREG(rv.s, MIPSInst_FT(ir));
1781130fe357SMarkos Chandras 			if (rv.w & 0x1)
1782130fe357SMarkos Chandras 				SPFROMREG(rv.s, MIPSInst_FS(ir));
1783130fe357SMarkos Chandras 			else
1784130fe357SMarkos Chandras 				rv.w = 0;
1785130fe357SMarkos Chandras 			break;
1786130fe357SMarkos Chandras 
1787e24c3becSMarkos Chandras 		case fmaddf_op: {
1788e24c3becSMarkos Chandras 			union ieee754sp ft, fs, fd;
1789e24c3becSMarkos Chandras 
1790e24c3becSMarkos Chandras 			if (!cpu_has_mips_r6)
1791e24c3becSMarkos Chandras 				return SIGILL;
1792e24c3becSMarkos Chandras 
1793454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maddf_s);
1794e24c3becSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1795e24c3becSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1796e24c3becSMarkos Chandras 			SPFROMREG(fd, MIPSInst_FD(ir));
1797e24c3becSMarkos Chandras 			rv.s = ieee754sp_maddf(fd, fs, ft);
1798409fcaceSAleksandar Markovic 			goto copcsr;
1799e24c3becSMarkos Chandras 		}
1800e24c3becSMarkos Chandras 
180183d43305SMarkos Chandras 		case fmsubf_op: {
180283d43305SMarkos Chandras 			union ieee754sp ft, fs, fd;
180383d43305SMarkos Chandras 
180483d43305SMarkos Chandras 			if (!cpu_has_mips_r6)
180583d43305SMarkos Chandras 				return SIGILL;
180683d43305SMarkos Chandras 
1807454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(msubf_s);
180883d43305SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
180983d43305SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
181083d43305SMarkos Chandras 			SPFROMREG(fd, MIPSInst_FD(ir));
181183d43305SMarkos Chandras 			rv.s = ieee754sp_msubf(fd, fs, ft);
1812409fcaceSAleksandar Markovic 			goto copcsr;
181383d43305SMarkos Chandras 		}
181483d43305SMarkos Chandras 
1815400bd2e4SMarkos Chandras 		case frint_op: {
1816400bd2e4SMarkos Chandras 			union ieee754sp fs;
1817400bd2e4SMarkos Chandras 
1818400bd2e4SMarkos Chandras 			if (!cpu_has_mips_r6)
1819400bd2e4SMarkos Chandras 				return SIGILL;
1820400bd2e4SMarkos Chandras 
1821454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rint_s);
1822400bd2e4SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18233ec404d8SAleksandar Markovic 			rv.s = ieee754sp_rint(fs);
1824400bd2e4SMarkos Chandras 			goto copcsr;
1825400bd2e4SMarkos Chandras 		}
1826400bd2e4SMarkos Chandras 
182738db37baSMarkos Chandras 		case fclass_op: {
182838db37baSMarkos Chandras 			union ieee754sp fs;
182938db37baSMarkos Chandras 
183038db37baSMarkos Chandras 			if (!cpu_has_mips_r6)
183138db37baSMarkos Chandras 				return SIGILL;
183238db37baSMarkos Chandras 
1833454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(class_s);
183438db37baSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
183538db37baSMarkos Chandras 			rv.w = ieee754sp_2008class(fs);
183638db37baSMarkos Chandras 			rfmt = w_fmt;
1837409fcaceSAleksandar Markovic 			goto copcsr;
183838db37baSMarkos Chandras 		}
183938db37baSMarkos Chandras 
18404e9561b2SMarkos Chandras 		case fmin_op: {
18414e9561b2SMarkos Chandras 			union ieee754sp fs, ft;
18424e9561b2SMarkos Chandras 
18434e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
18444e9561b2SMarkos Chandras 				return SIGILL;
18454e9561b2SMarkos Chandras 
1846454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(min_s);
18474e9561b2SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
18484e9561b2SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18494e9561b2SMarkos Chandras 			rv.s = ieee754sp_fmin(fs, ft);
1850409fcaceSAleksandar Markovic 			goto copcsr;
18514e9561b2SMarkos Chandras 		}
18524e9561b2SMarkos Chandras 
18534e9561b2SMarkos Chandras 		case fmina_op: {
18544e9561b2SMarkos Chandras 			union ieee754sp fs, ft;
18554e9561b2SMarkos Chandras 
18564e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
18574e9561b2SMarkos Chandras 				return SIGILL;
18584e9561b2SMarkos Chandras 
1859454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mina_s);
18604e9561b2SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
18614e9561b2SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18624e9561b2SMarkos Chandras 			rv.s = ieee754sp_fmina(fs, ft);
1863409fcaceSAleksandar Markovic 			goto copcsr;
18644e9561b2SMarkos Chandras 		}
18654e9561b2SMarkos Chandras 
1866a79f5f9bSMarkos Chandras 		case fmax_op: {
1867a79f5f9bSMarkos Chandras 			union ieee754sp fs, ft;
1868a79f5f9bSMarkos Chandras 
1869a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
1870a79f5f9bSMarkos Chandras 				return SIGILL;
1871a79f5f9bSMarkos Chandras 
1872454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(max_s);
1873a79f5f9bSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1874a79f5f9bSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1875a79f5f9bSMarkos Chandras 			rv.s = ieee754sp_fmax(fs, ft);
1876409fcaceSAleksandar Markovic 			goto copcsr;
1877a79f5f9bSMarkos Chandras 		}
1878a79f5f9bSMarkos Chandras 
1879a79f5f9bSMarkos Chandras 		case fmaxa_op: {
1880a79f5f9bSMarkos Chandras 			union ieee754sp fs, ft;
1881a79f5f9bSMarkos Chandras 
1882a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
1883a79f5f9bSMarkos Chandras 				return SIGILL;
1884a79f5f9bSMarkos Chandras 
1885454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maxa_s);
1886a79f5f9bSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1887a79f5f9bSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1888a79f5f9bSMarkos Chandras 			rv.s = ieee754sp_fmaxa(fs, ft);
1889409fcaceSAleksandar Markovic 			goto copcsr;
1890a79f5f9bSMarkos Chandras 		}
1891a79f5f9bSMarkos Chandras 
18921da177e4SLinus Torvalds 		case fabs_op:
1893454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(abs_s);
18941da177e4SLinus Torvalds 			handler.u = ieee754sp_abs;
18951da177e4SLinus Torvalds 			goto scopuop;
18963f7cac41SRalf Baechle 
18971da177e4SLinus Torvalds 		case fneg_op:
1898454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(neg_s);
18991da177e4SLinus Torvalds 			handler.u = ieee754sp_neg;
19001da177e4SLinus Torvalds 			goto scopuop;
19013f7cac41SRalf Baechle 
19021da177e4SLinus Torvalds 		case fmov_op:
19031da177e4SLinus Torvalds 			/* an easy one */
1904454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mov_s);
19051da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
19061da177e4SLinus Torvalds 			goto copcsr;
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds 			/* binary op on handler */
19091da177e4SLinus Torvalds scopbop:
19101da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19111da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
19121da177e4SLinus Torvalds 
19131da177e4SLinus Torvalds 			rv.s = (*handler.b) (fs, ft);
19141da177e4SLinus Torvalds 			goto copcsr;
19151da177e4SLinus Torvalds scopuop:
19161da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19171da177e4SLinus Torvalds 			rv.s = (*handler.u) (fs);
19181da177e4SLinus Torvalds 			goto copcsr;
19191da177e4SLinus Torvalds copcsr:
1920c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1921c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
19221da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1923c4103526SDeng-Cheng Zhu 			}
1924c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1925c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
19261da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1927c4103526SDeng-Cheng Zhu 			}
1928c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1929c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
19301da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1931c4103526SDeng-Cheng Zhu 			}
1932c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1933c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
19341da177e4SLinus Torvalds 				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1935c4103526SDeng-Cheng Zhu 			}
1936c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1937c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
19381da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1939c4103526SDeng-Cheng Zhu 			}
19401da177e4SLinus Torvalds 			break;
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds 			/* unary conv ops */
19431da177e4SLinus Torvalds 		case fcvts_op:
19441da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
19451da177e4SLinus Torvalds 
19463f7cac41SRalf Baechle 		case fcvtd_op:
1947454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_d_s);
19481da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19491da177e4SLinus Torvalds 			rv.d = ieee754dp_fsp(fs);
19501da177e4SLinus Torvalds 			rfmt = d_fmt;
19511da177e4SLinus Torvalds 			goto copcsr;
19521da177e4SLinus Torvalds 
19533f7cac41SRalf Baechle 		case fcvtw_op:
1954454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_w_s);
19551da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19561da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
19571da177e4SLinus Torvalds 			rfmt = w_fmt;
19581da177e4SLinus Torvalds 			goto copcsr;
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds 		case fround_op:
19611da177e4SLinus Torvalds 		case ftrunc_op:
19621da177e4SLinus Torvalds 		case fceil_op:
19633f7cac41SRalf Baechle 		case ffloor_op:
19642d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
196508a07904SRalf Baechle 				return SIGILL;
196608a07904SRalf Baechle 
1967454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceil_op)
1968454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_w_s);
1969454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloor_op)
1970454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_w_s);
1971454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fround_op)
1972454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_w_s);
1973454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftrunc_op)
1974454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_w_s);
1975454854acSAleksandar Markovic 
19763f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
19771da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19782cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
19791da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
19801da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
19811da177e4SLinus Torvalds 			rfmt = w_fmt;
19821da177e4SLinus Torvalds 			goto copcsr;
19831da177e4SLinus Torvalds 
19844b820d95SPaul Burton 		case fsel_op:
19854b820d95SPaul Burton 			if (!cpu_has_mips_r6)
19864b820d95SPaul Burton 				return SIGILL;
19874b820d95SPaul Burton 
1988454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sel_s);
19894b820d95SPaul Burton 			SPFROMREG(fd, MIPSInst_FD(ir));
19904b820d95SPaul Burton 			if (fd.bits & 0x1)
19914b820d95SPaul Burton 				SPFROMREG(rv.s, MIPSInst_FT(ir));
19924b820d95SPaul Burton 			else
19934b820d95SPaul Burton 				SPFROMREG(rv.s, MIPSInst_FS(ir));
19944b820d95SPaul Burton 			break;
19954b820d95SPaul Burton 
19963f7cac41SRalf Baechle 		case fcvtl_op:
19972d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
199808a07904SRalf Baechle 				return SIGILL;
199908a07904SRalf Baechle 
2000454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_l_s);
20011da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
20021da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
20031da177e4SLinus Torvalds 			rfmt = l_fmt;
20041da177e4SLinus Torvalds 			goto copcsr;
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 		case froundl_op:
20071da177e4SLinus Torvalds 		case ftruncl_op:
20081da177e4SLinus Torvalds 		case fceill_op:
20093f7cac41SRalf Baechle 		case ffloorl_op:
20102d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
201108a07904SRalf Baechle 				return SIGILL;
201208a07904SRalf Baechle 
2013454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceill_op)
2014454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_l_s);
2015454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloorl_op)
2016454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_l_s);
2017454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == froundl_op)
2018454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_l_s);
2019454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftruncl_op)
2020454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_l_s);
2021454854acSAleksandar Markovic 
20223f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
20231da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
20242cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
20251da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
20261da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
20271da177e4SLinus Torvalds 			rfmt = l_fmt;
20281da177e4SLinus Torvalds 			goto copcsr;
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds 		default:
2031f8c3c671SMarkos Chandras 			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
20321da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
20332209bcb1SRalf Baechle 				union ieee754sp fs, ft;
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
20361da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
20371da177e4SLinus Torvalds 				rv.w = ieee754sp_cmp(fs, ft,
20381da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
20391da177e4SLinus Torvalds 				rfmt = -1;
20401da177e4SLinus Torvalds 				if ((cmpop & 0x8) && ieee754_cxtest
20411da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
20421da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
20431da177e4SLinus Torvalds 				else
20441da177e4SLinus Torvalds 					goto copcsr;
20451da177e4SLinus Torvalds 
20463f7cac41SRalf Baechle 			} else
20471da177e4SLinus Torvalds 				return SIGILL;
20481da177e4SLinus Torvalds 			break;
20491da177e4SLinus Torvalds 		}
20501da177e4SLinus Torvalds 		break;
20511da177e4SLinus Torvalds 	}
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	case d_fmt: {
20544b820d95SPaul Burton 		union ieee754dp fd, fs, ft;
20551da177e4SLinus Torvalds 		union {
20562209bcb1SRalf Baechle 			union ieee754dp(*b) (union ieee754dp, union ieee754dp);
20572209bcb1SRalf Baechle 			union ieee754dp(*u) (union ieee754dp);
20581da177e4SLinus Torvalds 		} handler;
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
20611da177e4SLinus Torvalds 			/* binary ops */
20621da177e4SLinus Torvalds 		case fadd_op:
2063454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(add_d);
20641da177e4SLinus Torvalds 			handler.b = ieee754dp_add;
20651da177e4SLinus Torvalds 			goto dcopbop;
20661da177e4SLinus Torvalds 		case fsub_op:
2067454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sub_d);
20681da177e4SLinus Torvalds 			handler.b = ieee754dp_sub;
20691da177e4SLinus Torvalds 			goto dcopbop;
20701da177e4SLinus Torvalds 		case fmul_op:
2071454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mul_d);
20721da177e4SLinus Torvalds 			handler.b = ieee754dp_mul;
20731da177e4SLinus Torvalds 			goto dcopbop;
20741da177e4SLinus Torvalds 		case fdiv_op:
2075454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(div_d);
20761da177e4SLinus Torvalds 			handler.b = ieee754dp_div;
20771da177e4SLinus Torvalds 			goto dcopbop;
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 			/* unary  ops */
20801da177e4SLinus Torvalds 		case fsqrt_op:
208108a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
208208a07904SRalf Baechle 				return SIGILL;
208308a07904SRalf Baechle 
2084454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sqrt_d);
20851da177e4SLinus Torvalds 			handler.u = ieee754dp_sqrt;
20861da177e4SLinus Torvalds 			goto dcopuop;
208708a07904SRalf Baechle 		/*
208808a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
208908a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
209008a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
209108a07904SRalf Baechle 		 */
20921da177e4SLinus Torvalds 		case frsqrt_op:
20932d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
209408a07904SRalf Baechle 				return SIGILL;
209508a07904SRalf Baechle 
2096454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rsqrt_d);
20971da177e4SLinus Torvalds 			handler.u = fpemu_dp_rsqrt;
20981da177e4SLinus Torvalds 			goto dcopuop;
20991da177e4SLinus Torvalds 		case frecip_op:
21002d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
210108a07904SRalf Baechle 				return SIGILL;
210208a07904SRalf Baechle 
2103454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(recip_d);
21041da177e4SLinus Torvalds 			handler.u = fpemu_dp_recip;
21051da177e4SLinus Torvalds 			goto dcopuop;
21061da177e4SLinus Torvalds 		case fmovc_op:
210708a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
210808a07904SRalf Baechle 				return SIGILL;
210908a07904SRalf Baechle 
21101da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
21111da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
21121da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
21131da177e4SLinus Torvalds 				return 0;
21141da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
21151da177e4SLinus Torvalds 			break;
21161da177e4SLinus Torvalds 		case fmovz_op:
211708a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
211808a07904SRalf Baechle 				return SIGILL;
211908a07904SRalf Baechle 
21201da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
21211da177e4SLinus Torvalds 				return 0;
21221da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
21231da177e4SLinus Torvalds 			break;
21241da177e4SLinus Torvalds 		case fmovn_op:
212508a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
212608a07904SRalf Baechle 				return SIGILL;
212708a07904SRalf Baechle 
21281da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
21291da177e4SLinus Torvalds 				return 0;
21301da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
21311da177e4SLinus Torvalds 			break;
213267613f02SMarkos Chandras 
213367613f02SMarkos Chandras 		case fseleqz_op:
213467613f02SMarkos Chandras 			if (!cpu_has_mips_r6)
213567613f02SMarkos Chandras 				return SIGILL;
213667613f02SMarkos Chandras 
2137454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(seleqz_d);
213867613f02SMarkos Chandras 			DPFROMREG(rv.d, MIPSInst_FT(ir));
213967613f02SMarkos Chandras 			if (rv.l & 0x1)
214067613f02SMarkos Chandras 				rv.l = 0;
214167613f02SMarkos Chandras 			else
214267613f02SMarkos Chandras 				DPFROMREG(rv.d, MIPSInst_FS(ir));
214367613f02SMarkos Chandras 			break;
214467613f02SMarkos Chandras 
2145130fe357SMarkos Chandras 		case fselnez_op:
2146130fe357SMarkos Chandras 			if (!cpu_has_mips_r6)
2147130fe357SMarkos Chandras 				return SIGILL;
2148130fe357SMarkos Chandras 
2149454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(selnez_d);
2150130fe357SMarkos Chandras 			DPFROMREG(rv.d, MIPSInst_FT(ir));
2151130fe357SMarkos Chandras 			if (rv.l & 0x1)
2152130fe357SMarkos Chandras 				DPFROMREG(rv.d, MIPSInst_FS(ir));
2153130fe357SMarkos Chandras 			else
2154130fe357SMarkos Chandras 				rv.l = 0;
2155130fe357SMarkos Chandras 			break;
2156130fe357SMarkos Chandras 
2157e24c3becSMarkos Chandras 		case fmaddf_op: {
2158e24c3becSMarkos Chandras 			union ieee754dp ft, fs, fd;
2159e24c3becSMarkos Chandras 
2160e24c3becSMarkos Chandras 			if (!cpu_has_mips_r6)
2161e24c3becSMarkos Chandras 				return SIGILL;
2162e24c3becSMarkos Chandras 
2163454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maddf_d);
2164e24c3becSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2165e24c3becSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2166e24c3becSMarkos Chandras 			DPFROMREG(fd, MIPSInst_FD(ir));
2167e24c3becSMarkos Chandras 			rv.d = ieee754dp_maddf(fd, fs, ft);
2168409fcaceSAleksandar Markovic 			goto copcsr;
2169e24c3becSMarkos Chandras 		}
2170e24c3becSMarkos Chandras 
217183d43305SMarkos Chandras 		case fmsubf_op: {
217283d43305SMarkos Chandras 			union ieee754dp ft, fs, fd;
217383d43305SMarkos Chandras 
217483d43305SMarkos Chandras 			if (!cpu_has_mips_r6)
217583d43305SMarkos Chandras 				return SIGILL;
217683d43305SMarkos Chandras 
2177454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(msubf_d);
217883d43305SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
217983d43305SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
218083d43305SMarkos Chandras 			DPFROMREG(fd, MIPSInst_FD(ir));
218183d43305SMarkos Chandras 			rv.d = ieee754dp_msubf(fd, fs, ft);
2182409fcaceSAleksandar Markovic 			goto copcsr;
218383d43305SMarkos Chandras 		}
218483d43305SMarkos Chandras 
2185400bd2e4SMarkos Chandras 		case frint_op: {
2186400bd2e4SMarkos Chandras 			union ieee754dp fs;
2187400bd2e4SMarkos Chandras 
2188400bd2e4SMarkos Chandras 			if (!cpu_has_mips_r6)
2189400bd2e4SMarkos Chandras 				return SIGILL;
2190400bd2e4SMarkos Chandras 
2191454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(rint_d);
2192400bd2e4SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
21933ec404d8SAleksandar Markovic 			rv.d = ieee754dp_rint(fs);
2194400bd2e4SMarkos Chandras 			goto copcsr;
2195400bd2e4SMarkos Chandras 		}
2196400bd2e4SMarkos Chandras 
219738db37baSMarkos Chandras 		case fclass_op: {
219838db37baSMarkos Chandras 			union ieee754dp fs;
219938db37baSMarkos Chandras 
220038db37baSMarkos Chandras 			if (!cpu_has_mips_r6)
220138db37baSMarkos Chandras 				return SIGILL;
220238db37baSMarkos Chandras 
2203454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(class_d);
220438db37baSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2205e1231dd6SAleksandar Markovic 			rv.l = ieee754dp_2008class(fs);
2206e1231dd6SAleksandar Markovic 			rfmt = l_fmt;
2207409fcaceSAleksandar Markovic 			goto copcsr;
220838db37baSMarkos Chandras 		}
220938db37baSMarkos Chandras 
22104e9561b2SMarkos Chandras 		case fmin_op: {
22114e9561b2SMarkos Chandras 			union ieee754dp fs, ft;
22124e9561b2SMarkos Chandras 
22134e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
22144e9561b2SMarkos Chandras 				return SIGILL;
22154e9561b2SMarkos Chandras 
2216454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(min_d);
22174e9561b2SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
22184e9561b2SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
22194e9561b2SMarkos Chandras 			rv.d = ieee754dp_fmin(fs, ft);
2220409fcaceSAleksandar Markovic 			goto copcsr;
22214e9561b2SMarkos Chandras 		}
22224e9561b2SMarkos Chandras 
22234e9561b2SMarkos Chandras 		case fmina_op: {
22244e9561b2SMarkos Chandras 			union ieee754dp fs, ft;
22254e9561b2SMarkos Chandras 
22264e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
22274e9561b2SMarkos Chandras 				return SIGILL;
22284e9561b2SMarkos Chandras 
2229454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mina_d);
22304e9561b2SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
22314e9561b2SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
22324e9561b2SMarkos Chandras 			rv.d = ieee754dp_fmina(fs, ft);
2233409fcaceSAleksandar Markovic 			goto copcsr;
22344e9561b2SMarkos Chandras 		}
22354e9561b2SMarkos Chandras 
2236a79f5f9bSMarkos Chandras 		case fmax_op: {
2237a79f5f9bSMarkos Chandras 			union ieee754dp fs, ft;
2238a79f5f9bSMarkos Chandras 
2239a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
2240a79f5f9bSMarkos Chandras 				return SIGILL;
2241a79f5f9bSMarkos Chandras 
2242454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(max_d);
2243a79f5f9bSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2244a79f5f9bSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2245a79f5f9bSMarkos Chandras 			rv.d = ieee754dp_fmax(fs, ft);
2246409fcaceSAleksandar Markovic 			goto copcsr;
2247a79f5f9bSMarkos Chandras 		}
2248a79f5f9bSMarkos Chandras 
2249a79f5f9bSMarkos Chandras 		case fmaxa_op: {
2250a79f5f9bSMarkos Chandras 			union ieee754dp fs, ft;
2251a79f5f9bSMarkos Chandras 
2252a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
2253a79f5f9bSMarkos Chandras 				return SIGILL;
2254a79f5f9bSMarkos Chandras 
2255454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(maxa_d);
2256a79f5f9bSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2257a79f5f9bSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2258a79f5f9bSMarkos Chandras 			rv.d = ieee754dp_fmaxa(fs, ft);
2259409fcaceSAleksandar Markovic 			goto copcsr;
2260a79f5f9bSMarkos Chandras 		}
2261a79f5f9bSMarkos Chandras 
22621da177e4SLinus Torvalds 		case fabs_op:
2263454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(abs_d);
22641da177e4SLinus Torvalds 			handler.u = ieee754dp_abs;
22651da177e4SLinus Torvalds 			goto dcopuop;
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds 		case fneg_op:
2268454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(neg_d);
22691da177e4SLinus Torvalds 			handler.u = ieee754dp_neg;
22701da177e4SLinus Torvalds 			goto dcopuop;
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 		case fmov_op:
22731da177e4SLinus Torvalds 			/* an easy one */
2274454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(mov_d);
22751da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
22761da177e4SLinus Torvalds 			goto copcsr;
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds 			/* binary op on handler */
22793f7cac41SRalf Baechle dcopbop:
22801da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22811da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds 			rv.d = (*handler.b) (fs, ft);
22841da177e4SLinus Torvalds 			goto copcsr;
22853f7cac41SRalf Baechle dcopuop:
22861da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22871da177e4SLinus Torvalds 			rv.d = (*handler.u) (fs);
22881da177e4SLinus Torvalds 			goto copcsr;
22891da177e4SLinus Torvalds 
22903f7cac41SRalf Baechle 		/*
22913f7cac41SRalf Baechle 		 * unary conv ops
22923f7cac41SRalf Baechle 		 */
22933f7cac41SRalf Baechle 		case fcvts_op:
2294454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_s_d);
22951da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22961da177e4SLinus Torvalds 			rv.s = ieee754sp_fdp(fs);
22971da177e4SLinus Torvalds 			rfmt = s_fmt;
22981da177e4SLinus Torvalds 			goto copcsr;
22993f7cac41SRalf Baechle 
23001da177e4SLinus Torvalds 		case fcvtd_op:
23011da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
23021da177e4SLinus Torvalds 
23033f7cac41SRalf Baechle 		case fcvtw_op:
2304454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_w_d);
23051da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23061da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);	/* wrong */
23071da177e4SLinus Torvalds 			rfmt = w_fmt;
23081da177e4SLinus Torvalds 			goto copcsr;
23091da177e4SLinus Torvalds 
23101da177e4SLinus Torvalds 		case fround_op:
23111da177e4SLinus Torvalds 		case ftrunc_op:
23121da177e4SLinus Torvalds 		case fceil_op:
23133f7cac41SRalf Baechle 		case ffloor_op:
231408a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
231508a07904SRalf Baechle 				return SIGILL;
231608a07904SRalf Baechle 
2317454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceil_op)
2318454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_w_d);
2319454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloor_op)
2320454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_w_d);
2321454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fround_op)
2322454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_w_d);
2323454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftrunc_op)
2324454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_w_d);
2325454854acSAleksandar Markovic 
23263f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
23271da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23282cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
23291da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);
23301da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
23311da177e4SLinus Torvalds 			rfmt = w_fmt;
23321da177e4SLinus Torvalds 			goto copcsr;
23331da177e4SLinus Torvalds 
23344b820d95SPaul Burton 		case fsel_op:
23354b820d95SPaul Burton 			if (!cpu_has_mips_r6)
23364b820d95SPaul Burton 				return SIGILL;
23374b820d95SPaul Burton 
2338454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(sel_d);
23394b820d95SPaul Burton 			DPFROMREG(fd, MIPSInst_FD(ir));
23404b820d95SPaul Burton 			if (fd.bits & 0x1)
23414b820d95SPaul Burton 				DPFROMREG(rv.d, MIPSInst_FT(ir));
23424b820d95SPaul Burton 			else
23434b820d95SPaul Burton 				DPFROMREG(rv.d, MIPSInst_FS(ir));
23444b820d95SPaul Burton 			break;
23454b820d95SPaul Burton 
23463f7cac41SRalf Baechle 		case fcvtl_op:
23472d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
234808a07904SRalf Baechle 				return SIGILL;
234908a07904SRalf Baechle 
2350454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_l_d);
23511da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23521da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
23531da177e4SLinus Torvalds 			rfmt = l_fmt;
23541da177e4SLinus Torvalds 			goto copcsr;
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds 		case froundl_op:
23571da177e4SLinus Torvalds 		case ftruncl_op:
23581da177e4SLinus Torvalds 		case fceill_op:
23593f7cac41SRalf Baechle 		case ffloorl_op:
23602d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
236108a07904SRalf Baechle 				return SIGILL;
236208a07904SRalf Baechle 
2363454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == fceill_op)
2364454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(ceil_l_d);
2365454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ffloorl_op)
2366454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(floor_l_d);
2367454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == froundl_op)
2368454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(round_l_d);
2369454854acSAleksandar Markovic 			if (MIPSInst_FUNC(ir) == ftruncl_op)
2370454854acSAleksandar Markovic 				MIPS_FPU_EMU_INC_STATS(trunc_l_d);
2371454854acSAleksandar Markovic 
23723f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
23731da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
23742cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
23751da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
23761da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
23771da177e4SLinus Torvalds 			rfmt = l_fmt;
23781da177e4SLinus Torvalds 			goto copcsr;
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds 		default:
2381f8c3c671SMarkos Chandras 			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
23821da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
23832209bcb1SRalf Baechle 				union ieee754dp fs, ft;
23841da177e4SLinus Torvalds 
23851da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
23861da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
23871da177e4SLinus Torvalds 				rv.w = ieee754dp_cmp(fs, ft,
23881da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
23891da177e4SLinus Torvalds 				rfmt = -1;
23901da177e4SLinus Torvalds 				if ((cmpop & 0x8)
23911da177e4SLinus Torvalds 					&&
23921da177e4SLinus Torvalds 					ieee754_cxtest
23931da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
23941da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
23951da177e4SLinus Torvalds 				else
23961da177e4SLinus Torvalds 					goto copcsr;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 			}
23991da177e4SLinus Torvalds 			else {
24001da177e4SLinus Torvalds 				return SIGILL;
24011da177e4SLinus Torvalds 			}
24021da177e4SLinus Torvalds 			break;
24031da177e4SLinus Torvalds 		}
24041da177e4SLinus Torvalds 		break;
2405bbdd8147SMarkos Chandras 	}
24061da177e4SLinus Torvalds 
2407bbdd8147SMarkos Chandras 	case w_fmt: {
2408bbdd8147SMarkos Chandras 		union ieee754dp fs;
2409bbdd8147SMarkos Chandras 
24101da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
24111da177e4SLinus Torvalds 		case fcvts_op:
24121da177e4SLinus Torvalds 			/* convert word to single precision real */
2413454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_s_w);
24141da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
24151da177e4SLinus Torvalds 			rv.s = ieee754sp_fint(fs.bits);
24161da177e4SLinus Torvalds 			rfmt = s_fmt;
24171da177e4SLinus Torvalds 			goto copcsr;
24181da177e4SLinus Torvalds 		case fcvtd_op:
24191da177e4SLinus Torvalds 			/* convert word to double precision real */
2420454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_d_w);
24211da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
24221da177e4SLinus Torvalds 			rv.d = ieee754dp_fint(fs.bits);
24231da177e4SLinus Torvalds 			rfmt = d_fmt;
24241da177e4SLinus Torvalds 			goto copcsr;
2425f8c3c671SMarkos Chandras 		default: {
2426f8c3c671SMarkos Chandras 			/* Emulating the new CMP.condn.fmt R6 instruction */
2427f8c3c671SMarkos Chandras #define CMPOP_MASK	0x7
2428f8c3c671SMarkos Chandras #define SIGN_BIT	(0x1 << 3)
2429f8c3c671SMarkos Chandras #define PREDICATE_BIT	(0x1 << 4)
2430f8c3c671SMarkos Chandras 
2431f8c3c671SMarkos Chandras 			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2432f8c3c671SMarkos Chandras 			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2433f8c3c671SMarkos Chandras 			union ieee754sp fs, ft;
2434f8c3c671SMarkos Chandras 
2435f8c3c671SMarkos Chandras 			/* This is an R6 only instruction */
2436f8c3c671SMarkos Chandras 			if (!cpu_has_mips_r6 ||
2437f8c3c671SMarkos Chandras 			    (MIPSInst_FUNC(ir) & 0x20))
2438f8c3c671SMarkos Chandras 				return SIGILL;
2439f8c3c671SMarkos Chandras 
2440454854acSAleksandar Markovic 			if (!sig) {
2441454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2442454854acSAleksandar Markovic 					switch (cmpop) {
2443454854acSAleksandar Markovic 					case 0:
2444454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_af_s);
2445454854acSAleksandar Markovic 					break;
2446454854acSAleksandar Markovic 					case 1:
2447454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_un_s);
2448454854acSAleksandar Markovic 					break;
2449454854acSAleksandar Markovic 					case 2:
2450454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_eq_s);
2451454854acSAleksandar Markovic 					break;
2452454854acSAleksandar Markovic 					case 3:
2453454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ueq_s);
2454454854acSAleksandar Markovic 					break;
2455454854acSAleksandar Markovic 					case 4:
2456454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_lt_s);
2457454854acSAleksandar Markovic 					break;
2458454854acSAleksandar Markovic 					case 5:
2459454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ult_s);
2460454854acSAleksandar Markovic 					break;
2461454854acSAleksandar Markovic 					case 6:
2462454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_le_s);
2463454854acSAleksandar Markovic 					break;
2464454854acSAleksandar Markovic 					case 7:
2465454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ule_s);
2466454854acSAleksandar Markovic 					break;
2467454854acSAleksandar Markovic 					}
2468454854acSAleksandar Markovic 				} else {
2469454854acSAleksandar Markovic 					switch (cmpop) {
2470454854acSAleksandar Markovic 					case 1:
2471454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_or_s);
2472454854acSAleksandar Markovic 					break;
2473454854acSAleksandar Markovic 					case 2:
2474454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_une_s);
2475454854acSAleksandar Markovic 					break;
2476454854acSAleksandar Markovic 					case 3:
2477454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ne_s);
2478454854acSAleksandar Markovic 					break;
2479454854acSAleksandar Markovic 					}
2480454854acSAleksandar Markovic 				}
2481454854acSAleksandar Markovic 			} else {
2482454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2483454854acSAleksandar Markovic 					switch (cmpop) {
2484454854acSAleksandar Markovic 					case 0:
2485454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_saf_s);
2486454854acSAleksandar Markovic 					break;
2487454854acSAleksandar Markovic 					case 1:
2488454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sun_s);
2489454854acSAleksandar Markovic 					break;
2490454854acSAleksandar Markovic 					case 2:
2491454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_seq_s);
2492454854acSAleksandar Markovic 					break;
2493454854acSAleksandar Markovic 					case 3:
2494454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sueq_s);
2495454854acSAleksandar Markovic 					break;
2496454854acSAleksandar Markovic 					case 4:
2497454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_slt_s);
2498454854acSAleksandar Markovic 					break;
2499454854acSAleksandar Markovic 					case 5:
2500454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sult_s);
2501454854acSAleksandar Markovic 					break;
2502454854acSAleksandar Markovic 					case 6:
2503454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sle_s);
2504454854acSAleksandar Markovic 					break;
2505454854acSAleksandar Markovic 					case 7:
2506454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sule_s);
2507454854acSAleksandar Markovic 					break;
2508454854acSAleksandar Markovic 					}
2509454854acSAleksandar Markovic 				} else {
2510454854acSAleksandar Markovic 					switch (cmpop) {
2511454854acSAleksandar Markovic 					case 1:
2512454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sor_s);
2513454854acSAleksandar Markovic 					break;
2514454854acSAleksandar Markovic 					case 2:
2515454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sune_s);
2516454854acSAleksandar Markovic 					break;
2517454854acSAleksandar Markovic 					case 3:
2518454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sne_s);
2519454854acSAleksandar Markovic 					break;
2520454854acSAleksandar Markovic 					}
2521454854acSAleksandar Markovic 				}
2522454854acSAleksandar Markovic 			}
2523454854acSAleksandar Markovic 
2524f8c3c671SMarkos Chandras 			/* fmt is w_fmt for single precision so fix it */
2525f8c3c671SMarkos Chandras 			rfmt = s_fmt;
2526f8c3c671SMarkos Chandras 			/* default to false */
2527f8c3c671SMarkos Chandras 			rv.w = 0;
2528f8c3c671SMarkos Chandras 
2529f8c3c671SMarkos Chandras 			/* CMP.condn.S */
2530f8c3c671SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
2531f8c3c671SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
2532f8c3c671SMarkos Chandras 
2533f8c3c671SMarkos Chandras 			/* positive predicates */
2534f8c3c671SMarkos Chandras 			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2535f8c3c671SMarkos Chandras 				if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
2536f8c3c671SMarkos Chandras 						  sig))
2537f8c3c671SMarkos Chandras 				    rv.w = -1; /* true, all 1s */
2538f8c3c671SMarkos Chandras 				if ((sig) &&
2539f8c3c671SMarkos Chandras 				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2540f8c3c671SMarkos Chandras 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2541f8c3c671SMarkos Chandras 				else
2542f8c3c671SMarkos Chandras 					goto copcsr;
2543f8c3c671SMarkos Chandras 			} else {
2544f8c3c671SMarkos Chandras 				/* negative predicates */
2545f8c3c671SMarkos Chandras 				switch (cmpop) {
2546f8c3c671SMarkos Chandras 				case 1:
2547f8c3c671SMarkos Chandras 				case 2:
2548f8c3c671SMarkos Chandras 				case 3:
2549f8c3c671SMarkos Chandras 					if (ieee754sp_cmp(fs, ft,
2550f8c3c671SMarkos Chandras 							  negative_cmptab[cmpop],
2551f8c3c671SMarkos Chandras 							  sig))
2552f8c3c671SMarkos Chandras 						rv.w = -1; /* true, all 1s */
2553f8c3c671SMarkos Chandras 					if (sig &&
2554f8c3c671SMarkos Chandras 					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2555f8c3c671SMarkos Chandras 						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2556f8c3c671SMarkos Chandras 					else
2557f8c3c671SMarkos Chandras 						goto copcsr;
2558f8c3c671SMarkos Chandras 					break;
25591da177e4SLinus Torvalds 				default:
2560f8c3c671SMarkos Chandras 					/* Reserved R6 ops */
2561f8c3c671SMarkos Chandras 					pr_err("Reserved MIPS R6 CMP.condn.S operation\n");
25621da177e4SLinus Torvalds 					return SIGILL;
25631da177e4SLinus Torvalds 				}
2564f8c3c671SMarkos Chandras 			}
25651da177e4SLinus Torvalds 			break;
25661da177e4SLinus Torvalds 			}
2567f8c3c671SMarkos Chandras 		}
25681ff8560aSAleksandar Markovic 		break;
2569f8c3c671SMarkos Chandras 	}
25701da177e4SLinus Torvalds 
25713f7cac41SRalf Baechle 	case l_fmt:
257208a07904SRalf Baechle 
25732d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
257408a07904SRalf Baechle 			return SIGILL;
257508a07904SRalf Baechle 
2576bbd426f5SPaul Burton 		DIFROMREG(bits, MIPSInst_FS(ir));
2577bbd426f5SPaul Burton 
25781da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
25791da177e4SLinus Torvalds 		case fcvts_op:
25801da177e4SLinus Torvalds 			/* convert long to single precision real */
2581454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_s_l);
2582bbd426f5SPaul Burton 			rv.s = ieee754sp_flong(bits);
25831da177e4SLinus Torvalds 			rfmt = s_fmt;
25841da177e4SLinus Torvalds 			goto copcsr;
25851da177e4SLinus Torvalds 		case fcvtd_op:
25861da177e4SLinus Torvalds 			/* convert long to double precision real */
2587454854acSAleksandar Markovic 			MIPS_FPU_EMU_INC_STATS(cvt_d_l);
2588bbd426f5SPaul Burton 			rv.d = ieee754dp_flong(bits);
25891da177e4SLinus Torvalds 			rfmt = d_fmt;
25901da177e4SLinus Torvalds 			goto copcsr;
2591f8c3c671SMarkos Chandras 		default: {
2592f8c3c671SMarkos Chandras 			/* Emulating the new CMP.condn.fmt R6 instruction */
2593f8c3c671SMarkos Chandras 			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2594f8c3c671SMarkos Chandras 			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2595f8c3c671SMarkos Chandras 			union ieee754dp fs, ft;
2596f8c3c671SMarkos Chandras 
2597f8c3c671SMarkos Chandras 			if (!cpu_has_mips_r6 ||
2598f8c3c671SMarkos Chandras 			    (MIPSInst_FUNC(ir) & 0x20))
2599f8c3c671SMarkos Chandras 				return SIGILL;
2600f8c3c671SMarkos Chandras 
2601454854acSAleksandar Markovic 			if (!sig) {
2602454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2603454854acSAleksandar Markovic 					switch (cmpop) {
2604454854acSAleksandar Markovic 					case 0:
2605454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_af_d);
2606454854acSAleksandar Markovic 					break;
2607454854acSAleksandar Markovic 					case 1:
2608454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_un_d);
2609454854acSAleksandar Markovic 					break;
2610454854acSAleksandar Markovic 					case 2:
2611454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_eq_d);
2612454854acSAleksandar Markovic 					break;
2613454854acSAleksandar Markovic 					case 3:
2614454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ueq_d);
2615454854acSAleksandar Markovic 					break;
2616454854acSAleksandar Markovic 					case 4:
2617454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_lt_d);
2618454854acSAleksandar Markovic 					break;
2619454854acSAleksandar Markovic 					case 5:
2620454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ult_d);
2621454854acSAleksandar Markovic 					break;
2622454854acSAleksandar Markovic 					case 6:
2623454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_le_d);
2624454854acSAleksandar Markovic 					break;
2625454854acSAleksandar Markovic 					case 7:
2626454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ule_d);
2627454854acSAleksandar Markovic 					break;
2628454854acSAleksandar Markovic 					}
2629454854acSAleksandar Markovic 				} else {
2630454854acSAleksandar Markovic 					switch (cmpop) {
2631454854acSAleksandar Markovic 					case 1:
2632454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_or_d);
2633454854acSAleksandar Markovic 					break;
2634454854acSAleksandar Markovic 					case 2:
2635454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_une_d);
2636454854acSAleksandar Markovic 					break;
2637454854acSAleksandar Markovic 					case 3:
2638454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_ne_d);
2639454854acSAleksandar Markovic 					break;
2640454854acSAleksandar Markovic 					}
2641454854acSAleksandar Markovic 				}
2642454854acSAleksandar Markovic 			} else {
2643454854acSAleksandar Markovic 				if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2644454854acSAleksandar Markovic 					switch (cmpop) {
2645454854acSAleksandar Markovic 					case 0:
2646454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_saf_d);
2647454854acSAleksandar Markovic 					break;
2648454854acSAleksandar Markovic 					case 1:
2649454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sun_d);
2650454854acSAleksandar Markovic 					break;
2651454854acSAleksandar Markovic 					case 2:
2652454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_seq_d);
2653454854acSAleksandar Markovic 					break;
2654454854acSAleksandar Markovic 					case 3:
2655454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sueq_d);
2656454854acSAleksandar Markovic 					break;
2657454854acSAleksandar Markovic 					case 4:
2658454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_slt_d);
2659454854acSAleksandar Markovic 					break;
2660454854acSAleksandar Markovic 					case 5:
2661454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sult_d);
2662454854acSAleksandar Markovic 					break;
2663454854acSAleksandar Markovic 					case 6:
2664454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sle_d);
2665454854acSAleksandar Markovic 					break;
2666454854acSAleksandar Markovic 					case 7:
2667454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sule_d);
2668454854acSAleksandar Markovic 					break;
2669454854acSAleksandar Markovic 					}
2670454854acSAleksandar Markovic 				} else {
2671454854acSAleksandar Markovic 					switch (cmpop) {
2672454854acSAleksandar Markovic 					case 1:
2673454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sor_d);
2674454854acSAleksandar Markovic 					break;
2675454854acSAleksandar Markovic 					case 2:
2676454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sune_d);
2677454854acSAleksandar Markovic 					break;
2678454854acSAleksandar Markovic 					case 3:
2679454854acSAleksandar Markovic 					MIPS_FPU_EMU_INC_STATS(cmp_sne_d);
2680454854acSAleksandar Markovic 					break;
2681454854acSAleksandar Markovic 					}
2682454854acSAleksandar Markovic 				}
2683454854acSAleksandar Markovic 			}
2684454854acSAleksandar Markovic 
2685f8c3c671SMarkos Chandras 			/* fmt is l_fmt for double precision so fix it */
2686f8c3c671SMarkos Chandras 			rfmt = d_fmt;
2687f8c3c671SMarkos Chandras 			/* default to false */
2688f8c3c671SMarkos Chandras 			rv.l = 0;
2689f8c3c671SMarkos Chandras 
2690f8c3c671SMarkos Chandras 			/* CMP.condn.D */
2691f8c3c671SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2692f8c3c671SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2693f8c3c671SMarkos Chandras 
2694f8c3c671SMarkos Chandras 			/* positive predicates */
2695f8c3c671SMarkos Chandras 			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2696f8c3c671SMarkos Chandras 				if (ieee754dp_cmp(fs, ft,
2697f8c3c671SMarkos Chandras 						  cmptab[cmpop], sig))
2698f8c3c671SMarkos Chandras 				    rv.l = -1LL; /* true, all 1s */
2699f8c3c671SMarkos Chandras 				if (sig &&
2700f8c3c671SMarkos Chandras 				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2701f8c3c671SMarkos Chandras 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2702f8c3c671SMarkos Chandras 				else
2703f8c3c671SMarkos Chandras 					goto copcsr;
2704f8c3c671SMarkos Chandras 			} else {
2705f8c3c671SMarkos Chandras 				/* negative predicates */
2706f8c3c671SMarkos Chandras 				switch (cmpop) {
2707f8c3c671SMarkos Chandras 				case 1:
2708f8c3c671SMarkos Chandras 				case 2:
2709f8c3c671SMarkos Chandras 				case 3:
2710f8c3c671SMarkos Chandras 					if (ieee754dp_cmp(fs, ft,
2711f8c3c671SMarkos Chandras 							  negative_cmptab[cmpop],
2712f8c3c671SMarkos Chandras 							  sig))
2713f8c3c671SMarkos Chandras 						rv.l = -1LL; /* true, all 1s */
2714f8c3c671SMarkos Chandras 					if (sig &&
2715f8c3c671SMarkos Chandras 					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2716f8c3c671SMarkos Chandras 						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2717f8c3c671SMarkos Chandras 					else
2718f8c3c671SMarkos Chandras 						goto copcsr;
2719f8c3c671SMarkos Chandras 					break;
27201da177e4SLinus Torvalds 				default:
2721f8c3c671SMarkos Chandras 					/* Reserved R6 ops */
2722f8c3c671SMarkos Chandras 					pr_err("Reserved MIPS R6 CMP.condn.D operation\n");
27231da177e4SLinus Torvalds 					return SIGILL;
27241da177e4SLinus Torvalds 				}
2725f8c3c671SMarkos Chandras 			}
27261da177e4SLinus Torvalds 			break;
2727f8c3c671SMarkos Chandras 			}
2728f8c3c671SMarkos Chandras 		}
27291ff8560aSAleksandar Markovic 		break;
27301ff8560aSAleksandar Markovic 
27311da177e4SLinus Torvalds 	default:
27321da177e4SLinus Torvalds 		return SIGILL;
27331da177e4SLinus Torvalds 	}
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds 	/*
27361da177e4SLinus Torvalds 	 * Update the fpu CSR register for this operation.
27371da177e4SLinus Torvalds 	 * If an exception is required, generate a tidy SIGFPE exception,
27381da177e4SLinus Torvalds 	 * without updating the result register.
27391da177e4SLinus Torvalds 	 * Note: cause exception bits do not accumulate, they are rewritten
27401da177e4SLinus Torvalds 	 * for each op; only the flag/sticky bits accumulate.
27411da177e4SLinus Torvalds 	 */
27421da177e4SLinus Torvalds 	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
27431da177e4SLinus Torvalds 	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
27443f7cac41SRalf Baechle 		/*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
27451da177e4SLinus Torvalds 		return SIGFPE;
27461da177e4SLinus Torvalds 	}
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds 	/*
27491da177e4SLinus Torvalds 	 * Now we can safely write the result back to the register file.
27501da177e4SLinus Torvalds 	 */
27511da177e4SLinus Torvalds 	switch (rfmt) {
275208a07904SRalf Baechle 	case -1:
275308a07904SRalf Baechle 
275408a07904SRalf Baechle 		if (cpu_has_mips_4_5_r)
2755c3b9b945SRob Kendrick 			cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
27561da177e4SLinus Torvalds 		else
275708a07904SRalf Baechle 			cbit = FPU_CSR_COND;
275808a07904SRalf Baechle 		if (rv.w)
275908a07904SRalf Baechle 			ctx->fcr31 |= cbit;
276008a07904SRalf Baechle 		else
276108a07904SRalf Baechle 			ctx->fcr31 &= ~cbit;
27621da177e4SLinus Torvalds 		break;
276308a07904SRalf Baechle 
27641da177e4SLinus Torvalds 	case d_fmt:
27651da177e4SLinus Torvalds 		DPTOREG(rv.d, MIPSInst_FD(ir));
27661da177e4SLinus Torvalds 		break;
27671da177e4SLinus Torvalds 	case s_fmt:
27681da177e4SLinus Torvalds 		SPTOREG(rv.s, MIPSInst_FD(ir));
27691da177e4SLinus Torvalds 		break;
27701da177e4SLinus Torvalds 	case w_fmt:
27711da177e4SLinus Torvalds 		SITOREG(rv.w, MIPSInst_FD(ir));
27721da177e4SLinus Torvalds 		break;
27731da177e4SLinus Torvalds 	case l_fmt:
27742d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
277508a07904SRalf Baechle 			return SIGILL;
277608a07904SRalf Baechle 
27771da177e4SLinus Torvalds 		DITOREG(rv.l, MIPSInst_FD(ir));
27781da177e4SLinus Torvalds 		break;
27791da177e4SLinus Torvalds 	default:
27801da177e4SLinus Torvalds 		return SIGILL;
27811da177e4SLinus Torvalds 	}
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds 	return 0;
27841da177e4SLinus Torvalds }
27851da177e4SLinus Torvalds 
278613769ebaSMaciej W. Rozycki /*
278713769ebaSMaciej W. Rozycki  * Emulate FPU instructions.
278813769ebaSMaciej W. Rozycki  *
278913769ebaSMaciej W. Rozycki  * If we use FPU hardware, then we have been typically called to handle
279013769ebaSMaciej W. Rozycki  * an unimplemented operation, such as where an operand is a NaN or
279113769ebaSMaciej W. Rozycki  * denormalized.  In that case exit the emulation loop after a single
279213769ebaSMaciej W. Rozycki  * iteration so as to let hardware execute any subsequent instructions.
279313769ebaSMaciej W. Rozycki  *
279413769ebaSMaciej W. Rozycki  * If we have no FPU hardware or it has been disabled, then continue
279513769ebaSMaciej W. Rozycki  * emulating floating-point instructions until one of these conditions
279613769ebaSMaciej W. Rozycki  * has occurred:
279713769ebaSMaciej W. Rozycki  *
279813769ebaSMaciej W. Rozycki  * - a non-FPU instruction has been encountered,
279913769ebaSMaciej W. Rozycki  *
280013769ebaSMaciej W. Rozycki  * - an attempt to emulate has ended with a signal,
280113769ebaSMaciej W. Rozycki  *
280213769ebaSMaciej W. Rozycki  * - the ISA mode has been switched.
280313769ebaSMaciej W. Rozycki  *
280413769ebaSMaciej W. Rozycki  * We need to terminate the emulation loop if we got switched to the
280513769ebaSMaciej W. Rozycki  * MIPS16 mode, whether supported or not, so that we do not attempt
280613769ebaSMaciej W. Rozycki  * to emulate a MIPS16 instruction as a regular MIPS FPU instruction.
280713769ebaSMaciej W. Rozycki  * Similarly if we got switched to the microMIPS mode and only the
280813769ebaSMaciej W. Rozycki  * regular MIPS mode is supported, so that we do not attempt to emulate
280913769ebaSMaciej W. Rozycki  * a microMIPS instruction as a regular MIPS FPU instruction.  Or if
281013769ebaSMaciej W. Rozycki  * we got switched to the regular MIPS mode and only the microMIPS mode
281113769ebaSMaciej W. Rozycki  * is supported, so that we do not attempt to emulate a regular MIPS
281213769ebaSMaciej W. Rozycki  * instruction that should cause an Address Error exception instead.
281313769ebaSMaciej W. Rozycki  * For simplicity we always terminate upon an ISA mode switch.
281413769ebaSMaciej W. Rozycki  */
2815e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2816445a58ceSPaul Burton 	int has_fpu, void __user **fault_addr)
28171da177e4SLinus Torvalds {
2818333d1f67SRalf Baechle 	unsigned long oldepc, prevepc;
2819102cedc3SLeonid Yegoshin 	struct mm_decoded_insn dec_insn;
2820102cedc3SLeonid Yegoshin 	u16 instr[4];
2821102cedc3SLeonid Yegoshin 	u16 *instr_ptr;
28221da177e4SLinus Torvalds 	int sig = 0;
28231da177e4SLinus Torvalds 
28241da177e4SLinus Torvalds 	oldepc = xcp->cp0_epc;
28251da177e4SLinus Torvalds 	do {
28261da177e4SLinus Torvalds 		prevepc = xcp->cp0_epc;
28271da177e4SLinus Torvalds 
2828102cedc3SLeonid Yegoshin 		if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2829102cedc3SLeonid Yegoshin 			/*
2830102cedc3SLeonid Yegoshin 			 * Get next 2 microMIPS instructions and convert them
2831102cedc3SLeonid Yegoshin 			 * into 32-bit instructions.
2832102cedc3SLeonid Yegoshin 			 */
2833102cedc3SLeonid Yegoshin 			if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2834102cedc3SLeonid Yegoshin 			    (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2835102cedc3SLeonid Yegoshin 			    (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2836102cedc3SLeonid Yegoshin 			    (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2837b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
28381da177e4SLinus Torvalds 				return SIGBUS;
28391da177e4SLinus Torvalds 			}
2840102cedc3SLeonid Yegoshin 			instr_ptr = instr;
2841102cedc3SLeonid Yegoshin 
2842102cedc3SLeonid Yegoshin 			/* Get first instruction. */
2843102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2844102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2845102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2846102cedc3SLeonid Yegoshin 					(*instr_ptr);
2847102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2848102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 2;
2849102cedc3SLeonid Yegoshin 				instr_ptr += 1;
2850102cedc3SLeonid Yegoshin 			} else {
2851102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2852102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2853102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2854102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 4;
2855102cedc3SLeonid Yegoshin 				instr_ptr += 2;
2856515b029dSDavid Daney 			}
2857102cedc3SLeonid Yegoshin 			/* Get second instruction. */
2858102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2859102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2860102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2861102cedc3SLeonid Yegoshin 					(*instr_ptr);
2862102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2863102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 2;
2864102cedc3SLeonid Yegoshin 			} else {
2865102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2866102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2867102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2868102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 4;
2869102cedc3SLeonid Yegoshin 			}
2870102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 1;
2871102cedc3SLeonid Yegoshin 		} else {
2872102cedc3SLeonid Yegoshin 			if ((get_user(dec_insn.insn,
2873102cedc3SLeonid Yegoshin 			    (mips_instruction __user *) xcp->cp0_epc)) ||
2874102cedc3SLeonid Yegoshin 			    (get_user(dec_insn.next_insn,
2875102cedc3SLeonid Yegoshin 			    (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2876102cedc3SLeonid Yegoshin 				MIPS_FPU_EMU_INC_STATS(errors);
2877102cedc3SLeonid Yegoshin 				return SIGBUS;
2878102cedc3SLeonid Yegoshin 			}
2879102cedc3SLeonid Yegoshin 			dec_insn.pc_inc = 4;
2880102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc = 4;
2881102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 0;
2882102cedc3SLeonid Yegoshin 		}
2883102cedc3SLeonid Yegoshin 
2884102cedc3SLeonid Yegoshin 		if ((dec_insn.insn == 0) ||
2885102cedc3SLeonid Yegoshin 		   ((dec_insn.pc_inc == 2) &&
2886102cedc3SLeonid Yegoshin 		   ((dec_insn.insn & 0xffff) == MM_NOP16)))
2887102cedc3SLeonid Yegoshin 			xcp->cp0_epc += dec_insn.pc_inc;	/* Skip NOPs */
28881da177e4SLinus Torvalds 		else {
2889cd21dfcfSRalf Baechle 			/*
28902cfcf8a8SMaciej W. Rozycki 			 * The 'ieee754_csr' is an alias of ctx->fcr31.
28912cfcf8a8SMaciej W. Rozycki 			 * No need to copy ctx->fcr31 to ieee754_csr.
2892cd21dfcfSRalf Baechle 			 */
2893102cedc3SLeonid Yegoshin 			sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
28941da177e4SLinus Torvalds 		}
28951da177e4SLinus Torvalds 
2896e04582b7SAtsushi Nemoto 		if (has_fpu)
28971da177e4SLinus Torvalds 			break;
28981da177e4SLinus Torvalds 		if (sig)
28991da177e4SLinus Torvalds 			break;
290013769ebaSMaciej W. Rozycki 		/*
290113769ebaSMaciej W. Rozycki 		 * We have to check for the ISA bit explicitly here,
290213769ebaSMaciej W. Rozycki 		 * because `get_isa16_mode' may return 0 if support
290313769ebaSMaciej W. Rozycki 		 * for code compression has been globally disabled,
290413769ebaSMaciej W. Rozycki 		 * or otherwise we may produce the wrong signal or
290513769ebaSMaciej W. Rozycki 		 * even proceed successfully where we must not.
290613769ebaSMaciej W. Rozycki 		 */
290713769ebaSMaciej W. Rozycki 		if ((xcp->cp0_epc ^ prevepc) & 0x1)
290813769ebaSMaciej W. Rozycki 			break;
29091da177e4SLinus Torvalds 
29101da177e4SLinus Torvalds 		cond_resched();
29111da177e4SLinus Torvalds 	} while (xcp->cp0_epc > prevepc);
29121da177e4SLinus Torvalds 
29131da177e4SLinus Torvalds 	/* SIGILL indicates a non-fpu instruction */
29141da177e4SLinus Torvalds 	if (sig == SIGILL && xcp->cp0_epc != oldepc)
29153f7cac41SRalf Baechle 		/* but if EPC has advanced, then ignore it */
29161da177e4SLinus Torvalds 		sig = 0;
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds 	return sig;
29191da177e4SLinus Torvalds }
2920