xref: /linux/arch/mips/math-emu/cp1emu.c (revision ab4a92e66741b35ca12f8497896bafbe579c28a1)
11da177e4SLinus Torvalds /*
23f7cac41SRalf Baechle  * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * MIPS floating point support
51da177e4SLinus Torvalds  * Copyright (C) 1994-2000 Algorithmics Ltd.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
81da177e4SLinus Torvalds  * Copyright (C) 2000  MIPS Technologies, Inc.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  This program is free software; you can distribute it and/or modify it
111da177e4SLinus Torvalds  *  under the terms of the GNU General Public License (Version 2) as
121da177e4SLinus Torvalds  *  published by the Free Software Foundation.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  This program is distributed in the hope it will be useful, but WITHOUT
151da177e4SLinus Torvalds  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
161da177e4SLinus Torvalds  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
171da177e4SLinus Torvalds  *  for more details.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *  You should have received a copy of the GNU General Public License along
201da177e4SLinus Torvalds  *  with this program; if not, write to the Free Software Foundation, Inc.,
213f7cac41SRalf Baechle  *  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * A complete emulator for MIPS coprocessor 1 instructions.  This is
241da177e4SLinus Torvalds  * required for #float(switch) or #float(trap), where it catches all
251da177e4SLinus Torvalds  * COP1 instructions via the "CoProcessor Unusable" exception.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  * More surprisingly it is also required for #float(ieee), to help out
283f7cac41SRalf Baechle  * the hardware FPU at the boundaries of the IEEE-754 representation
291da177e4SLinus Torvalds  * (denormalised values, infinities, underflow, etc).  It is made
301da177e4SLinus Torvalds  * quite nasty because emulation of some non-COP1 instructions is
311da177e4SLinus Torvalds  * required, e.g. in branch delay slots.
321da177e4SLinus Torvalds  *
333f7cac41SRalf Baechle  * Note if you know that you won't have an FPU, then you'll get much
341da177e4SLinus Torvalds  * better performance by compiling with -msoft-float!
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds #include <linux/sched.h>
3783fd38caSAtsushi Nemoto #include <linux/debugfs.h>
3808a07904SRalf Baechle #include <linux/kconfig.h>
3985c51c51SRalf Baechle #include <linux/percpu-defs.h>
407f788d2dSDeng-Cheng Zhu #include <linux/perf_event.h>
411da177e4SLinus Torvalds 
42cd8ee345SRalf Baechle #include <asm/branch.h>
431da177e4SLinus Torvalds #include <asm/inst.h>
441da177e4SLinus Torvalds #include <asm/ptrace.h>
451da177e4SLinus Torvalds #include <asm/signal.h>
46cd8ee345SRalf Baechle #include <asm/uaccess.h>
47cd8ee345SRalf Baechle 
48f6843626SMaciej W. Rozycki #include <asm/cpu-info.h>
49cd8ee345SRalf Baechle #include <asm/processor.h>
501da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
51102cedc3SLeonid Yegoshin #include <asm/fpu.h>
52b0a668fbSLeonid Yegoshin #include <asm/mips-r2-to-r6-emul.h>
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #include "ieee754.h"
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */
571da177e4SLinus Torvalds 
58eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
591da177e4SLinus Torvalds 	mips_instruction);
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *,
62515b029dSDavid Daney 	struct mips_fpu_struct *, mips_instruction, void *__user *);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* Control registers */
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #define FPCREG_RID	0	/* $0  = revision id */
67c491cfa2SMaciej W. Rozycki #define FPCREG_FCCR	25	/* $25 = fccr */
68c491cfa2SMaciej W. Rozycki #define FPCREG_FEXR	26	/* $26 = fexr */
69c491cfa2SMaciej W. Rozycki #define FPCREG_FENR	28	/* $28 = fenr */
701da177e4SLinus Torvalds #define FPCREG_CSR	31	/* $31 = csr */
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /* convert condition code register number to csr bit */
73b0a668fbSLeonid Yegoshin const unsigned int fpucondbit[8] = {
74c491cfa2SMaciej W. Rozycki 	FPU_CSR_COND,
751da177e4SLinus Torvalds 	FPU_CSR_COND1,
761da177e4SLinus Torvalds 	FPU_CSR_COND2,
771da177e4SLinus Torvalds 	FPU_CSR_COND3,
781da177e4SLinus Torvalds 	FPU_CSR_COND4,
791da177e4SLinus Torvalds 	FPU_CSR_COND5,
801da177e4SLinus Torvalds 	FPU_CSR_COND6,
811da177e4SLinus Torvalds 	FPU_CSR_COND7
821da177e4SLinus Torvalds };
831da177e4SLinus Torvalds 
84102cedc3SLeonid Yegoshin /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
85102cedc3SLeonid Yegoshin static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
86102cedc3SLeonid Yegoshin static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
87102cedc3SLeonid Yegoshin static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
88102cedc3SLeonid Yegoshin static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
89102cedc3SLeonid Yegoshin 
90102cedc3SLeonid Yegoshin /*
91102cedc3SLeonid Yegoshin  * This functions translates a 32-bit microMIPS instruction
92102cedc3SLeonid Yegoshin  * into a 32-bit MIPS32 instruction. Returns 0 on success
93102cedc3SLeonid Yegoshin  * and SIGILL otherwise.
94102cedc3SLeonid Yegoshin  */
95102cedc3SLeonid Yegoshin static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
96102cedc3SLeonid Yegoshin {
97102cedc3SLeonid Yegoshin 	union mips_instruction insn = *insn_ptr;
98102cedc3SLeonid Yegoshin 	union mips_instruction mips32_insn = insn;
99102cedc3SLeonid Yegoshin 	int func, fmt, op;
100102cedc3SLeonid Yegoshin 
101102cedc3SLeonid Yegoshin 	switch (insn.mm_i_format.opcode) {
102102cedc3SLeonid Yegoshin 	case mm_ldc132_op:
103102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = ldc1_op;
104102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
105102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
106102cedc3SLeonid Yegoshin 		break;
107102cedc3SLeonid Yegoshin 	case mm_lwc132_op:
108102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = lwc1_op;
109102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
110102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
111102cedc3SLeonid Yegoshin 		break;
112102cedc3SLeonid Yegoshin 	case mm_sdc132_op:
113102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = sdc1_op;
114102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
115102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
116102cedc3SLeonid Yegoshin 		break;
117102cedc3SLeonid Yegoshin 	case mm_swc132_op:
118102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = swc1_op;
119102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
120102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
121102cedc3SLeonid Yegoshin 		break;
122102cedc3SLeonid Yegoshin 	case mm_pool32i_op:
123102cedc3SLeonid Yegoshin 		/* NOTE: offset is << by 1 if in microMIPS mode. */
124102cedc3SLeonid Yegoshin 		if ((insn.mm_i_format.rt == mm_bc1f_op) ||
125102cedc3SLeonid Yegoshin 		    (insn.mm_i_format.rt == mm_bc1t_op)) {
126102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.opcode = cop1_op;
127102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.bc = bc_op;
128102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.flag =
129102cedc3SLeonid Yegoshin 				(insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
130102cedc3SLeonid Yegoshin 		} else
131102cedc3SLeonid Yegoshin 			return SIGILL;
132102cedc3SLeonid Yegoshin 		break;
133102cedc3SLeonid Yegoshin 	case mm_pool32f_op:
134102cedc3SLeonid Yegoshin 		switch (insn.mm_fp0_format.func) {
135102cedc3SLeonid Yegoshin 		case mm_32f_01_op:
136102cedc3SLeonid Yegoshin 		case mm_32f_11_op:
137102cedc3SLeonid Yegoshin 		case mm_32f_02_op:
138102cedc3SLeonid Yegoshin 		case mm_32f_12_op:
139102cedc3SLeonid Yegoshin 		case mm_32f_41_op:
140102cedc3SLeonid Yegoshin 		case mm_32f_51_op:
141102cedc3SLeonid Yegoshin 		case mm_32f_42_op:
142102cedc3SLeonid Yegoshin 		case mm_32f_52_op:
143102cedc3SLeonid Yegoshin 			op = insn.mm_fp0_format.func;
144102cedc3SLeonid Yegoshin 			if (op == mm_32f_01_op)
145102cedc3SLeonid Yegoshin 				func = madd_s_op;
146102cedc3SLeonid Yegoshin 			else if (op == mm_32f_11_op)
147102cedc3SLeonid Yegoshin 				func = madd_d_op;
148102cedc3SLeonid Yegoshin 			else if (op == mm_32f_02_op)
149102cedc3SLeonid Yegoshin 				func = nmadd_s_op;
150102cedc3SLeonid Yegoshin 			else if (op == mm_32f_12_op)
151102cedc3SLeonid Yegoshin 				func = nmadd_d_op;
152102cedc3SLeonid Yegoshin 			else if (op == mm_32f_41_op)
153102cedc3SLeonid Yegoshin 				func = msub_s_op;
154102cedc3SLeonid Yegoshin 			else if (op == mm_32f_51_op)
155102cedc3SLeonid Yegoshin 				func = msub_d_op;
156102cedc3SLeonid Yegoshin 			else if (op == mm_32f_42_op)
157102cedc3SLeonid Yegoshin 				func = nmsub_s_op;
158102cedc3SLeonid Yegoshin 			else
159102cedc3SLeonid Yegoshin 				func = nmsub_d_op;
160102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.opcode = cop1x_op;
161102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
162102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
163102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
164102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
165102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.func = func;
166102cedc3SLeonid Yegoshin 			break;
167102cedc3SLeonid Yegoshin 		case mm_32f_10_op:
168102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
169102cedc3SLeonid Yegoshin 			op = insn.mm_fp5_format.op & 0x7;
170102cedc3SLeonid Yegoshin 			if (op == mm_ldxc1_op)
171102cedc3SLeonid Yegoshin 				func = ldxc1_op;
172102cedc3SLeonid Yegoshin 			else if (op == mm_sdxc1_op)
173102cedc3SLeonid Yegoshin 				func = sdxc1_op;
174102cedc3SLeonid Yegoshin 			else if (op == mm_lwxc1_op)
175102cedc3SLeonid Yegoshin 				func = lwxc1_op;
176102cedc3SLeonid Yegoshin 			else if (op == mm_swxc1_op)
177102cedc3SLeonid Yegoshin 				func = swxc1_op;
178102cedc3SLeonid Yegoshin 
179102cedc3SLeonid Yegoshin 			if (func != -1) {
180102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = cop1x_op;
181102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs =
182102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.base;
183102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
184102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.index;
185102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = 0;
186102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = insn.mm_fp5_format.fd;
187102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = func;
188102cedc3SLeonid Yegoshin 			} else
189102cedc3SLeonid Yegoshin 				return SIGILL;
190102cedc3SLeonid Yegoshin 			break;
191102cedc3SLeonid Yegoshin 		case mm_32f_40_op:
192102cedc3SLeonid Yegoshin 			op = -1;	/* Invalid */
193102cedc3SLeonid Yegoshin 			if (insn.mm_fp2_format.op == mm_fmovt_op)
194102cedc3SLeonid Yegoshin 				op = 1;
195102cedc3SLeonid Yegoshin 			else if (insn.mm_fp2_format.op == mm_fmovf_op)
196102cedc3SLeonid Yegoshin 				op = 0;
197102cedc3SLeonid Yegoshin 			if (op != -1) {
198102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
199102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
200102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp2_format.fmt];
201102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
202102cedc3SLeonid Yegoshin 					(insn.mm_fp2_format.cc<<2) + op;
203102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
204102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fs;
205102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
206102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fd;
207102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = fmovc_op;
208102cedc3SLeonid Yegoshin 			} else
209102cedc3SLeonid Yegoshin 				return SIGILL;
210102cedc3SLeonid Yegoshin 			break;
211102cedc3SLeonid Yegoshin 		case mm_32f_60_op:
212102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
213102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fadd_op)
214102cedc3SLeonid Yegoshin 				func = fadd_op;
215102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fsub_op)
216102cedc3SLeonid Yegoshin 				func = fsub_op;
217102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmul_op)
218102cedc3SLeonid Yegoshin 				func = fmul_op;
219102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fdiv_op)
220102cedc3SLeonid Yegoshin 				func = fdiv_op;
221102cedc3SLeonid Yegoshin 			if (func != -1) {
222102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
223102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
224102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
225102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
226102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
227102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
228102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
229102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
230102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
231102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
232102cedc3SLeonid Yegoshin 			} else
233102cedc3SLeonid Yegoshin 				return SIGILL;
234102cedc3SLeonid Yegoshin 			break;
235102cedc3SLeonid Yegoshin 		case mm_32f_70_op:
236102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
237102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fmovn_op)
238102cedc3SLeonid Yegoshin 				func = fmovn_op;
239102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmovz_op)
240102cedc3SLeonid Yegoshin 				func = fmovz_op;
241102cedc3SLeonid Yegoshin 			if (func != -1) {
242102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
243102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
244102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
245102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
246102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
247102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
248102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
249102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
250102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
251102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
252102cedc3SLeonid Yegoshin 			} else
253102cedc3SLeonid Yegoshin 				return SIGILL;
254102cedc3SLeonid Yegoshin 			break;
255102cedc3SLeonid Yegoshin 		case mm_32f_73_op:    /* POOL32FXF */
256102cedc3SLeonid Yegoshin 			switch (insn.mm_fp1_format.op) {
257102cedc3SLeonid Yegoshin 			case mm_movf0_op:
258102cedc3SLeonid Yegoshin 			case mm_movf1_op:
259102cedc3SLeonid Yegoshin 			case mm_movt0_op:
260102cedc3SLeonid Yegoshin 			case mm_movt1_op:
261102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
262102cedc3SLeonid Yegoshin 				    mm_movf0_op)
263102cedc3SLeonid Yegoshin 					op = 0;
264102cedc3SLeonid Yegoshin 				else
265102cedc3SLeonid Yegoshin 					op = 1;
266102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = spec_op;
267102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
268102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
269102cedc3SLeonid Yegoshin 					(insn.mm_fp4_format.cc << 2) + op;
270102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
271102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = 0;
272102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = movc_op;
273102cedc3SLeonid Yegoshin 				break;
274102cedc3SLeonid Yegoshin 			case mm_fcvtd0_op:
275102cedc3SLeonid Yegoshin 			case mm_fcvtd1_op:
276102cedc3SLeonid Yegoshin 			case mm_fcvts0_op:
277102cedc3SLeonid Yegoshin 			case mm_fcvts1_op:
278102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
279102cedc3SLeonid Yegoshin 				    mm_fcvtd0_op) {
280102cedc3SLeonid Yegoshin 					func = fcvtd_op;
281102cedc3SLeonid Yegoshin 					fmt = swl_format[insn.mm_fp3_format.fmt];
282102cedc3SLeonid Yegoshin 				} else {
283102cedc3SLeonid Yegoshin 					func = fcvts_op;
284102cedc3SLeonid Yegoshin 					fmt = dwl_format[insn.mm_fp3_format.fmt];
285102cedc3SLeonid Yegoshin 				}
286102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
287102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt = fmt;
288102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
289102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
290102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
291102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
292102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
293102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
294102cedc3SLeonid Yegoshin 				break;
295102cedc3SLeonid Yegoshin 			case mm_fmov0_op:
296102cedc3SLeonid Yegoshin 			case mm_fmov1_op:
297102cedc3SLeonid Yegoshin 			case mm_fabs0_op:
298102cedc3SLeonid Yegoshin 			case mm_fabs1_op:
299102cedc3SLeonid Yegoshin 			case mm_fneg0_op:
300102cedc3SLeonid Yegoshin 			case mm_fneg1_op:
301102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
302102cedc3SLeonid Yegoshin 				    mm_fmov0_op)
303102cedc3SLeonid Yegoshin 					func = fmov_op;
304102cedc3SLeonid Yegoshin 				else if ((insn.mm_fp1_format.op & 0x7f) ==
305102cedc3SLeonid Yegoshin 					 mm_fabs0_op)
306102cedc3SLeonid Yegoshin 					func = fabs_op;
307102cedc3SLeonid Yegoshin 				else
308102cedc3SLeonid Yegoshin 					func = fneg_op;
309102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
310102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
311102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp3_format.fmt];
312102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
313102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
314102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
315102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
316102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
317102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
318102cedc3SLeonid Yegoshin 				break;
319102cedc3SLeonid Yegoshin 			case mm_ffloorl_op:
320102cedc3SLeonid Yegoshin 			case mm_ffloorw_op:
321102cedc3SLeonid Yegoshin 			case mm_fceill_op:
322102cedc3SLeonid Yegoshin 			case mm_fceilw_op:
323102cedc3SLeonid Yegoshin 			case mm_ftruncl_op:
324102cedc3SLeonid Yegoshin 			case mm_ftruncw_op:
325102cedc3SLeonid Yegoshin 			case mm_froundl_op:
326102cedc3SLeonid Yegoshin 			case mm_froundw_op:
327102cedc3SLeonid Yegoshin 			case mm_fcvtl_op:
328102cedc3SLeonid Yegoshin 			case mm_fcvtw_op:
329102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_ffloorl_op)
330102cedc3SLeonid Yegoshin 					func = ffloorl_op;
331102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ffloorw_op)
332102cedc3SLeonid Yegoshin 					func = ffloor_op;
333102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceill_op)
334102cedc3SLeonid Yegoshin 					func = fceill_op;
335102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceilw_op)
336102cedc3SLeonid Yegoshin 					func = fceil_op;
337102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncl_op)
338102cedc3SLeonid Yegoshin 					func = ftruncl_op;
339102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncw_op)
340102cedc3SLeonid Yegoshin 					func = ftrunc_op;
341102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundl_op)
342102cedc3SLeonid Yegoshin 					func = froundl_op;
343102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundw_op)
344102cedc3SLeonid Yegoshin 					func = fround_op;
345102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fcvtl_op)
346102cedc3SLeonid Yegoshin 					func = fcvtl_op;
347102cedc3SLeonid Yegoshin 				else
348102cedc3SLeonid Yegoshin 					func = fcvtw_op;
349102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
350102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
351102cedc3SLeonid Yegoshin 					sd_format[insn.mm_fp1_format.fmt];
352102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
353102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
354102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
355102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
356102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
357102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
358102cedc3SLeonid Yegoshin 				break;
359102cedc3SLeonid Yegoshin 			case mm_frsqrt_op:
360102cedc3SLeonid Yegoshin 			case mm_fsqrt_op:
361102cedc3SLeonid Yegoshin 			case mm_frecip_op:
362102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_frsqrt_op)
363102cedc3SLeonid Yegoshin 					func = frsqrt_op;
364102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fsqrt_op)
365102cedc3SLeonid Yegoshin 					func = fsqrt_op;
366102cedc3SLeonid Yegoshin 				else
367102cedc3SLeonid Yegoshin 					func = frecip_op;
368102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
369102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
370102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp1_format.fmt];
371102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
372102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
373102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
374102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
375102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
376102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
377102cedc3SLeonid Yegoshin 				break;
378102cedc3SLeonid Yegoshin 			case mm_mfc1_op:
379102cedc3SLeonid Yegoshin 			case mm_mtc1_op:
380102cedc3SLeonid Yegoshin 			case mm_cfc1_op:
381102cedc3SLeonid Yegoshin 			case mm_ctc1_op:
3829355e59cSSteven J. Hill 			case mm_mfhc1_op:
3839355e59cSSteven J. Hill 			case mm_mthc1_op:
384102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_mfc1_op)
385102cedc3SLeonid Yegoshin 					op = mfc_op;
386102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_mtc1_op)
387102cedc3SLeonid Yegoshin 					op = mtc_op;
388102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_cfc1_op)
389102cedc3SLeonid Yegoshin 					op = cfc_op;
3909355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_ctc1_op)
391102cedc3SLeonid Yegoshin 					op = ctc_op;
3929355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_mfhc1_op)
3939355e59cSSteven J. Hill 					op = mfhc_op;
3949355e59cSSteven J. Hill 				else
3959355e59cSSteven J. Hill 					op = mthc_op;
396102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.opcode = cop1_op;
397102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.op = op;
398102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.rt =
399102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
400102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fs =
401102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
402102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fd = 0;
403102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.func = 0;
404102cedc3SLeonid Yegoshin 				break;
405102cedc3SLeonid Yegoshin 			default:
406102cedc3SLeonid Yegoshin 				return SIGILL;
407102cedc3SLeonid Yegoshin 			}
408102cedc3SLeonid Yegoshin 			break;
409102cedc3SLeonid Yegoshin 		case mm_32f_74_op:	/* c.cond.fmt */
410102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.opcode = cop1_op;
411102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fmt =
412102cedc3SLeonid Yegoshin 				sdps_format[insn.mm_fp4_format.fmt];
413102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
414102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
415102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
416102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.func =
417102cedc3SLeonid Yegoshin 				insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
418102cedc3SLeonid Yegoshin 			break;
419102cedc3SLeonid Yegoshin 		default:
420102cedc3SLeonid Yegoshin 			return SIGILL;
421102cedc3SLeonid Yegoshin 		}
422102cedc3SLeonid Yegoshin 		break;
423102cedc3SLeonid Yegoshin 	default:
424102cedc3SLeonid Yegoshin 		return SIGILL;
425102cedc3SLeonid Yegoshin 	}
426102cedc3SLeonid Yegoshin 
427102cedc3SLeonid Yegoshin 	*insn_ptr = mips32_insn;
428102cedc3SLeonid Yegoshin 	return 0;
429102cedc3SLeonid Yegoshin }
430102cedc3SLeonid Yegoshin 
4311da177e4SLinus Torvalds /*
4321da177e4SLinus Torvalds  * Redundant with logic already in kernel/branch.c,
4331da177e4SLinus Torvalds  * embedded in compute_return_epc.  At some point,
4341da177e4SLinus Torvalds  * a single subroutine should be used across both
4351da177e4SLinus Torvalds  * modules.
4361da177e4SLinus Torvalds  */
437102cedc3SLeonid Yegoshin static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
438102cedc3SLeonid Yegoshin 			 unsigned long *contpc)
4391da177e4SLinus Torvalds {
440102cedc3SLeonid Yegoshin 	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
441102cedc3SLeonid Yegoshin 	unsigned int fcr31;
442102cedc3SLeonid Yegoshin 	unsigned int bit = 0;
443102cedc3SLeonid Yegoshin 
444102cedc3SLeonid Yegoshin 	switch (insn.i_format.opcode) {
4451da177e4SLinus Torvalds 	case spec_op:
446102cedc3SLeonid Yegoshin 		switch (insn.r_format.func) {
4471da177e4SLinus Torvalds 		case jalr_op:
448*ab4a92e6SPaul Burton 			if (insn.r_format.rd != 0) {
449102cedc3SLeonid Yegoshin 				regs->regs[insn.r_format.rd] =
450102cedc3SLeonid Yegoshin 					regs->cp0_epc + dec_insn.pc_inc +
451102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
452*ab4a92e6SPaul Burton 			}
453102cedc3SLeonid Yegoshin 			/* Fall through */
4541da177e4SLinus Torvalds 		case jr_op:
4555f9f41c4SMarkos Chandras 			/* For R6, JR already emulated in jalr_op */
456143fefc8SMarkos Chandras 			if (NO_R6EMU && insn.r_format.func == jr_op)
4575f9f41c4SMarkos Chandras 				break;
458102cedc3SLeonid Yegoshin 			*contpc = regs->regs[insn.r_format.rs];
4591da177e4SLinus Torvalds 			return 1;
4601da177e4SLinus Torvalds 		}
4611da177e4SLinus Torvalds 		break;
4621da177e4SLinus Torvalds 	case bcond_op:
463102cedc3SLeonid Yegoshin 		switch (insn.i_format.rt) {
4641da177e4SLinus Torvalds 		case bltzal_op:
4651da177e4SLinus Torvalds 		case bltzall_op:
466319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
467319824eaSMarkos Chandras 			    insn.i_format.rt == bltzall_op))
468319824eaSMarkos Chandras 				break;
469319824eaSMarkos Chandras 
470102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
471102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
472102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
473102cedc3SLeonid Yegoshin 			/* Fall through */
474102cedc3SLeonid Yegoshin 		case bltzl_op:
475319824eaSMarkos Chandras 			if (NO_R6EMU)
476319824eaSMarkos Chandras 				break;
477319824eaSMarkos Chandras 		case bltz_op:
478102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] < 0)
479102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
480102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
481102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
482102cedc3SLeonid Yegoshin 			else
483102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
484102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
485102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
4861da177e4SLinus Torvalds 			return 1;
487102cedc3SLeonid Yegoshin 		case bgezal_op:
488102cedc3SLeonid Yegoshin 		case bgezall_op:
489319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
490319824eaSMarkos Chandras 			    insn.i_format.rt == bgezall_op))
491319824eaSMarkos Chandras 				break;
492319824eaSMarkos Chandras 
493102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
494102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
495102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
496102cedc3SLeonid Yegoshin 			/* Fall through */
497102cedc3SLeonid Yegoshin 		case bgezl_op:
498319824eaSMarkos Chandras 			if (NO_R6EMU)
499319824eaSMarkos Chandras 				break;
500319824eaSMarkos Chandras 		case bgez_op:
501102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] >= 0)
502102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
503102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
504102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
505102cedc3SLeonid Yegoshin 			else
506102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
507102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
508102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
509102cedc3SLeonid Yegoshin 			return 1;
5101da177e4SLinus Torvalds 		}
5111da177e4SLinus Torvalds 		break;
5121da177e4SLinus Torvalds 	case jalx_op:
513102cedc3SLeonid Yegoshin 		set_isa16_mode(bit);
514102cedc3SLeonid Yegoshin 	case jal_op:
515102cedc3SLeonid Yegoshin 		regs->regs[31] = regs->cp0_epc +
516102cedc3SLeonid Yegoshin 			dec_insn.pc_inc +
517102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc;
518102cedc3SLeonid Yegoshin 		/* Fall through */
519102cedc3SLeonid Yegoshin 	case j_op:
520102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
521102cedc3SLeonid Yegoshin 		*contpc >>= 28;
522102cedc3SLeonid Yegoshin 		*contpc <<= 28;
523102cedc3SLeonid Yegoshin 		*contpc |= (insn.j_format.target << 2);
524102cedc3SLeonid Yegoshin 		/* Set microMIPS mode bit: XOR for jalx. */
525102cedc3SLeonid Yegoshin 		*contpc ^= bit;
5261da177e4SLinus Torvalds 		return 1;
527102cedc3SLeonid Yegoshin 	case beql_op:
528319824eaSMarkos Chandras 		if (NO_R6EMU)
529319824eaSMarkos Chandras 			break;
530319824eaSMarkos Chandras 	case beq_op:
531102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] ==
532102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
533102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
534102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
535102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
536102cedc3SLeonid Yegoshin 		else
537102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
538102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
539102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
540102cedc3SLeonid Yegoshin 		return 1;
541102cedc3SLeonid Yegoshin 	case bnel_op:
542319824eaSMarkos Chandras 		if (NO_R6EMU)
543319824eaSMarkos Chandras 			break;
544319824eaSMarkos Chandras 	case bne_op:
545102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] !=
546102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
547102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
548102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
549102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
550102cedc3SLeonid Yegoshin 		else
551102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
552102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
553102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
554102cedc3SLeonid Yegoshin 		return 1;
555102cedc3SLeonid Yegoshin 	case blezl_op:
556e9d92d22SMarkos Chandras 		if (!insn.i_format.rt && NO_R6EMU)
557319824eaSMarkos Chandras 			break;
558319824eaSMarkos Chandras 	case blez_op:
559a8ff66f5SMarkos Chandras 
560a8ff66f5SMarkos Chandras 		/*
561a8ff66f5SMarkos Chandras 		 * Compact branches for R6 for the
562a8ff66f5SMarkos Chandras 		 * blez and blezl opcodes.
563a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = 0 | rt != 0  == BLEZALC
564a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = rt != 0      == BGEZALC
565a8ff66f5SMarkos Chandras 		 * BLEZ  | rs != 0 | rt != 0 == BGEUC
566a8ff66f5SMarkos Chandras 		 * BLEZL | rs = 0 | rt != 0  == BLEZC
567a8ff66f5SMarkos Chandras 		 * BLEZL | rs = rt != 0      == BGEZC
568a8ff66f5SMarkos Chandras 		 * BLEZL | rs != 0 | rt != 0 == BGEC
569a8ff66f5SMarkos Chandras 		 *
570a8ff66f5SMarkos Chandras 		 * For real BLEZ{,L}, rt is always 0.
571a8ff66f5SMarkos Chandras 		 */
572a8ff66f5SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
573a8ff66f5SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
574a8ff66f5SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
575a8ff66f5SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
576a8ff66f5SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
577a8ff66f5SMarkos Chandras 					dec_insn.pc_inc;
578a8ff66f5SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
579a8ff66f5SMarkos Chandras 				dec_insn.next_pc_inc;
580a8ff66f5SMarkos Chandras 
581a8ff66f5SMarkos Chandras 			return 1;
582a8ff66f5SMarkos Chandras 		}
583102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] <= 0)
584102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
585102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
586102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
587102cedc3SLeonid Yegoshin 		else
588102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
589102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
590102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
591102cedc3SLeonid Yegoshin 		return 1;
592102cedc3SLeonid Yegoshin 	case bgtzl_op:
593e9d92d22SMarkos Chandras 		if (!insn.i_format.rt && NO_R6EMU)
594319824eaSMarkos Chandras 			break;
595319824eaSMarkos Chandras 	case bgtz_op:
596f1b44067SMarkos Chandras 		/*
597f1b44067SMarkos Chandras 		 * Compact branches for R6 for the
598f1b44067SMarkos Chandras 		 * bgtz and bgtzl opcodes.
599f1b44067SMarkos Chandras 		 * BGTZ  | rs = 0 | rt != 0  == BGTZALC
600f1b44067SMarkos Chandras 		 * BGTZ  | rs = rt != 0      == BLTZALC
601f1b44067SMarkos Chandras 		 * BGTZ  | rs != 0 | rt != 0 == BLTUC
602f1b44067SMarkos Chandras 		 * BGTZL | rs = 0 | rt != 0  == BGTZC
603f1b44067SMarkos Chandras 		 * BGTZL | rs = rt != 0      == BLTZC
604f1b44067SMarkos Chandras 		 * BGTZL | rs != 0 | rt != 0 == BLTC
605f1b44067SMarkos Chandras 		 *
606f1b44067SMarkos Chandras 		 * *ZALC varint for BGTZ &&& rt != 0
607f1b44067SMarkos Chandras 		 * For real GTZ{,L}, rt is always 0.
608f1b44067SMarkos Chandras 		 */
609f1b44067SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
610f1b44067SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
611f1b44067SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
612f1b44067SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
613f1b44067SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
614f1b44067SMarkos Chandras 					dec_insn.pc_inc;
615f1b44067SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
616f1b44067SMarkos Chandras 				dec_insn.next_pc_inc;
617f1b44067SMarkos Chandras 
618f1b44067SMarkos Chandras 			return 1;
619f1b44067SMarkos Chandras 		}
620f1b44067SMarkos Chandras 
621102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] > 0)
622102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
623102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
624102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
625102cedc3SLeonid Yegoshin 		else
626102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
627102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
628102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
629102cedc3SLeonid Yegoshin 		return 1;
630c893ce38SMarkos Chandras 	case cbcond0_op:
63110d962d5SMarkos Chandras 	case cbcond1_op:
632c893ce38SMarkos Chandras 		if (!cpu_has_mips_r6)
633c893ce38SMarkos Chandras 			break;
634c893ce38SMarkos Chandras 		if (insn.i_format.rt && !insn.i_format.rs)
635c893ce38SMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
636c893ce38SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
637c893ce38SMarkos Chandras 			dec_insn.next_pc_inc;
638c893ce38SMarkos Chandras 
639c893ce38SMarkos Chandras 		return 1;
640c26d4219SDavid Daney #ifdef CONFIG_CPU_CAVIUM_OCTEON
641c26d4219SDavid Daney 	case lwc2_op: /* This is bbit0 on Octeon */
642c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
643c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
644c26d4219SDavid Daney 		else
645c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
646c26d4219SDavid Daney 		return 1;
647c26d4219SDavid Daney 	case ldc2_op: /* This is bbit032 on Octeon */
648c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
649c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
650c26d4219SDavid Daney 		else
651c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
652c26d4219SDavid Daney 		return 1;
653c26d4219SDavid Daney 	case swc2_op: /* This is bbit1 on Octeon */
654c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
655c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
656c26d4219SDavid Daney 		else
657c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
658c26d4219SDavid Daney 		return 1;
659c26d4219SDavid Daney 	case sdc2_op: /* This is bbit132 on Octeon */
660c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
661c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
662c26d4219SDavid Daney 		else
663c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
664c26d4219SDavid Daney 		return 1;
6658467ca01SMarkos Chandras #else
6668467ca01SMarkos Chandras 	case bc6_op:
6678467ca01SMarkos Chandras 		/*
6688467ca01SMarkos Chandras 		 * Only valid for MIPS R6 but we can still end up
6698467ca01SMarkos Chandras 		 * here from a broken userland so just tell emulator
6708467ca01SMarkos Chandras 		 * this is not a branch and let it break later on.
6718467ca01SMarkos Chandras 		 */
6728467ca01SMarkos Chandras 		if  (!cpu_has_mips_r6)
6738467ca01SMarkos Chandras 			break;
6748467ca01SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
6758467ca01SMarkos Chandras 			dec_insn.next_pc_inc;
6768467ca01SMarkos Chandras 
6778467ca01SMarkos Chandras 		return 1;
67884fef630SMarkos Chandras 	case balc6_op:
67984fef630SMarkos Chandras 		if (!cpu_has_mips_r6)
68084fef630SMarkos Chandras 			break;
68184fef630SMarkos Chandras 		regs->regs[31] = regs->cp0_epc + 4;
68284fef630SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
68384fef630SMarkos Chandras 			dec_insn.next_pc_inc;
68484fef630SMarkos Chandras 
68584fef630SMarkos Chandras 		return 1;
68669b9a2fdSMarkos Chandras 	case beqzcjic_op:
68769b9a2fdSMarkos Chandras 		if (!cpu_has_mips_r6)
68869b9a2fdSMarkos Chandras 			break;
68969b9a2fdSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
69069b9a2fdSMarkos Chandras 			dec_insn.next_pc_inc;
69169b9a2fdSMarkos Chandras 
69269b9a2fdSMarkos Chandras 		return 1;
69328d6f93dSMarkos Chandras 	case bnezcjialc_op:
69428d6f93dSMarkos Chandras 		if (!cpu_has_mips_r6)
69528d6f93dSMarkos Chandras 			break;
69628d6f93dSMarkos Chandras 		if (!insn.i_format.rs)
69728d6f93dSMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
69828d6f93dSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
69928d6f93dSMarkos Chandras 			dec_insn.next_pc_inc;
70028d6f93dSMarkos Chandras 
70128d6f93dSMarkos Chandras 		return 1;
702c26d4219SDavid Daney #endif
7031da177e4SLinus Torvalds 	case cop0_op:
7041da177e4SLinus Torvalds 	case cop1_op:
705c8a34581SMarkos Chandras 		/* Need to check for R6 bc1nez and bc1eqz branches */
706c8a34581SMarkos Chandras 		if (cpu_has_mips_r6 &&
707c8a34581SMarkos Chandras 		    ((insn.i_format.rs == bc1eqz_op) ||
708c8a34581SMarkos Chandras 		     (insn.i_format.rs == bc1nez_op))) {
709c8a34581SMarkos Chandras 			bit = 0;
710c8a34581SMarkos Chandras 			switch (insn.i_format.rs) {
711c8a34581SMarkos Chandras 			case bc1eqz_op:
712c8a34581SMarkos Chandras 				if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
713c8a34581SMarkos Chandras 				    bit = 1;
714c8a34581SMarkos Chandras 				break;
715c8a34581SMarkos Chandras 			case bc1nez_op:
716c8a34581SMarkos Chandras 				if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
717c8a34581SMarkos Chandras 				    bit = 1;
718c8a34581SMarkos Chandras 				break;
719c8a34581SMarkos Chandras 			}
720c8a34581SMarkos Chandras 			if (bit)
721c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
722c8a34581SMarkos Chandras 					dec_insn.pc_inc +
723c8a34581SMarkos Chandras 					(insn.i_format.simmediate << 2);
724c8a34581SMarkos Chandras 			else
725c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
726c8a34581SMarkos Chandras 					dec_insn.pc_inc +
727c8a34581SMarkos Chandras 					dec_insn.next_pc_inc;
728c8a34581SMarkos Chandras 
729c8a34581SMarkos Chandras 			return 1;
730c8a34581SMarkos Chandras 		}
731c8a34581SMarkos Chandras 		/* R2/R6 compatible cop1 instruction. Fall through */
7321da177e4SLinus Torvalds 	case cop2_op:
7331da177e4SLinus Torvalds 	case cop1x_op:
734102cedc3SLeonid Yegoshin 		if (insn.i_format.rs == bc_op) {
735102cedc3SLeonid Yegoshin 			preempt_disable();
736102cedc3SLeonid Yegoshin 			if (is_fpu_owner())
737842dfc11SManuel Lauss 			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
738102cedc3SLeonid Yegoshin 			else
739102cedc3SLeonid Yegoshin 				fcr31 = current->thread.fpu.fcr31;
740102cedc3SLeonid Yegoshin 			preempt_enable();
741102cedc3SLeonid Yegoshin 
742102cedc3SLeonid Yegoshin 			bit = (insn.i_format.rt >> 2);
743102cedc3SLeonid Yegoshin 			bit += (bit != 0);
744102cedc3SLeonid Yegoshin 			bit += 23;
745102cedc3SLeonid Yegoshin 			switch (insn.i_format.rt & 3) {
746102cedc3SLeonid Yegoshin 			case 0:	/* bc1f */
747102cedc3SLeonid Yegoshin 			case 2:	/* bc1fl */
748102cedc3SLeonid Yegoshin 				if (~fcr31 & (1 << bit))
749102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
750102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
751102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
752102cedc3SLeonid Yegoshin 				else
753102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
754102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
755102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
756102cedc3SLeonid Yegoshin 				return 1;
757102cedc3SLeonid Yegoshin 			case 1:	/* bc1t */
758102cedc3SLeonid Yegoshin 			case 3:	/* bc1tl */
759102cedc3SLeonid Yegoshin 				if (fcr31 & (1 << bit))
760102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
761102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
762102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
763102cedc3SLeonid Yegoshin 				else
764102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
765102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
766102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
7671da177e4SLinus Torvalds 				return 1;
7681da177e4SLinus Torvalds 			}
769102cedc3SLeonid Yegoshin 		}
770102cedc3SLeonid Yegoshin 		break;
771102cedc3SLeonid Yegoshin 	}
7721da177e4SLinus Torvalds 	return 0;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds /*
7761da177e4SLinus Torvalds  * In the Linux kernel, we support selection of FPR format on the
777da0bac33SDavid Daney  * basis of the Status.FR bit.	If an FPU is not present, the FR bit
778da0bac33SDavid Daney  * is hardwired to zero, which would imply a 32-bit FPU even for
779597ce172SPaul Burton  * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
78051d943f0SRalf Baechle  * FPU emu is slow and bulky and optimizing this function offers fairly
78151d943f0SRalf Baechle  * sizeable benefits so we try to be clever and make this function return
78251d943f0SRalf Baechle  * a constant whenever possible, that is on 64-bit kernels without O32
783597ce172SPaul Burton  * compatibility enabled and on 32-bit without 64-bit FPU support.
7841da177e4SLinus Torvalds  */
785da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp)
786da0bac33SDavid Daney {
78708a07904SRalf Baechle 	if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
78851d943f0SRalf Baechle 		return 1;
78908a07904SRalf Baechle 	else if (config_enabled(CONFIG_32BIT) &&
79008a07904SRalf Baechle 		 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
791da0bac33SDavid Daney 		return 0;
79208a07904SRalf Baechle 
793597ce172SPaul Burton 	return !test_thread_flag(TIF_32BIT_FPREGS);
794da0bac33SDavid Daney }
7951da177e4SLinus Torvalds 
7964227a2d4SPaul Burton static inline bool hybrid_fprs(void)
7974227a2d4SPaul Burton {
7984227a2d4SPaul Burton 	return test_thread_flag(TIF_HYBRID_FPREGS);
7994227a2d4SPaul Burton }
8004227a2d4SPaul Burton 
80147fa0c02SRalf Baechle #define SIFROMREG(si, x)						\
80247fa0c02SRalf Baechle do {									\
8034227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs())				\
804c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[x], 0);			\
805bbd426f5SPaul Burton 	else								\
806c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);	\
807bbd426f5SPaul Burton } while (0)
808da0bac33SDavid Daney 
80947fa0c02SRalf Baechle #define SITOREG(si, x)							\
81047fa0c02SRalf Baechle do {									\
8114227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs()) {			\
812ef1c47afSPaul Burton 		unsigned i;						\
813bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[x], 0, si);				\
814ef1c47afSPaul Burton 		for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)	\
815ef1c47afSPaul Burton 			set_fpr32(&ctx->fpr[x], i, 0);			\
816ef1c47afSPaul Burton 	} else {							\
817bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);		\
818ef1c47afSPaul Burton 	}								\
819bbd426f5SPaul Burton } while (0)
8201da177e4SLinus Torvalds 
821c8c0da6bSPaul Burton #define SIFROMHREG(si, x)	((si) = (int)get_fpr32(&ctx->fpr[x], 1))
822ef1c47afSPaul Burton 
82347fa0c02SRalf Baechle #define SITOHREG(si, x)							\
82447fa0c02SRalf Baechle do {									\
825ef1c47afSPaul Burton 	unsigned i;							\
826ef1c47afSPaul Burton 	set_fpr32(&ctx->fpr[x], 1, si);					\
827ef1c47afSPaul Burton 	for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)		\
828ef1c47afSPaul Burton 		set_fpr32(&ctx->fpr[x], i, 0);				\
829ef1c47afSPaul Burton } while (0)
8301ac94400SLeonid Yegoshin 
831bbd426f5SPaul Burton #define DIFROMREG(di, x)						\
832bbd426f5SPaul Burton 	((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
833bbd426f5SPaul Burton 
83447fa0c02SRalf Baechle #define DITOREG(di, x)							\
83547fa0c02SRalf Baechle do {									\
836ef1c47afSPaul Burton 	unsigned fpr, i;						\
837ef1c47afSPaul Burton 	fpr = (x) & ~(cop1_64bit(xcp) == 0);				\
838ef1c47afSPaul Burton 	set_fpr64(&ctx->fpr[fpr], 0, di);				\
839ef1c47afSPaul Burton 	for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++)		\
840ef1c47afSPaul Burton 		set_fpr64(&ctx->fpr[fpr], i, 0);			\
841ef1c47afSPaul Burton } while (0)
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
8441da177e4SLinus Torvalds #define SPTOREG(sp, x)	SITOREG((sp).bits, x)
8451da177e4SLinus Torvalds #define DPFROMREG(dp, x)	DIFROMREG((dp).bits, x)
8461da177e4SLinus Torvalds #define DPTOREG(dp, x)	DITOREG((dp).bits, x)
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds /*
849d4f5b088SMaciej W. Rozycki  * Emulate a CFC1 instruction.
850d4f5b088SMaciej W. Rozycki  */
851d4f5b088SMaciej W. Rozycki static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
852d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
853d4f5b088SMaciej W. Rozycki {
854c491cfa2SMaciej W. Rozycki 	u32 fcr31 = ctx->fcr31;
855c491cfa2SMaciej W. Rozycki 	u32 value = 0;
856d4f5b088SMaciej W. Rozycki 
857c491cfa2SMaciej W. Rozycki 	switch (MIPSInst_RD(ir)) {
858c491cfa2SMaciej W. Rozycki 	case FPCREG_CSR:
859c491cfa2SMaciej W. Rozycki 		value = fcr31;
860d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-csr=%08x\n",
861c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
862c491cfa2SMaciej W. Rozycki 		break;
863c491cfa2SMaciej W. Rozycki 
864c491cfa2SMaciej W. Rozycki 	case FPCREG_FENR:
865c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
866c491cfa2SMaciej W. Rozycki 			break;
867c491cfa2SMaciej W. Rozycki 		value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
868c491cfa2SMaciej W. Rozycki 			MIPS_FENR_FS;
869c491cfa2SMaciej W. Rozycki 		value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
870c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-enr=%08x\n",
871c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
872c491cfa2SMaciej W. Rozycki 		break;
873c491cfa2SMaciej W. Rozycki 
874c491cfa2SMaciej W. Rozycki 	case FPCREG_FEXR:
875c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
876c491cfa2SMaciej W. Rozycki 			break;
877c491cfa2SMaciej W. Rozycki 		value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
878c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-exr=%08x\n",
879c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
880c491cfa2SMaciej W. Rozycki 		break;
881c491cfa2SMaciej W. Rozycki 
882c491cfa2SMaciej W. Rozycki 	case FPCREG_FCCR:
883c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
884c491cfa2SMaciej W. Rozycki 			break;
885c491cfa2SMaciej W. Rozycki 		value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
886c491cfa2SMaciej W. Rozycki 			MIPS_FCCR_COND0;
887c491cfa2SMaciej W. Rozycki 		value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
888c491cfa2SMaciej W. Rozycki 			 (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
889c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-ccr=%08x\n",
890c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
891c491cfa2SMaciej W. Rozycki 		break;
892c491cfa2SMaciej W. Rozycki 
893c491cfa2SMaciej W. Rozycki 	case FPCREG_RID:
89403dce595SMaciej W. Rozycki 		value = boot_cpu_data.fpu_id;
895c491cfa2SMaciej W. Rozycki 		break;
896c491cfa2SMaciej W. Rozycki 
897c491cfa2SMaciej W. Rozycki 	default:
898c491cfa2SMaciej W. Rozycki 		break;
899c491cfa2SMaciej W. Rozycki 	}
900c491cfa2SMaciej W. Rozycki 
901d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir))
902d4f5b088SMaciej W. Rozycki 		xcp->regs[MIPSInst_RT(ir)] = value;
903d4f5b088SMaciej W. Rozycki }
904d4f5b088SMaciej W. Rozycki 
905d4f5b088SMaciej W. Rozycki /*
906d4f5b088SMaciej W. Rozycki  * Emulate a CTC1 instruction.
907d4f5b088SMaciej W. Rozycki  */
908d4f5b088SMaciej W. Rozycki static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
909d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
910d4f5b088SMaciej W. Rozycki {
911c491cfa2SMaciej W. Rozycki 	u32 fcr31 = ctx->fcr31;
912d4f5b088SMaciej W. Rozycki 	u32 value;
9139b26616cSMaciej W. Rozycki 	u32 mask;
914d4f5b088SMaciej W. Rozycki 
915d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir) == 0)
916d4f5b088SMaciej W. Rozycki 		value = 0;
917d4f5b088SMaciej W. Rozycki 	else
918d4f5b088SMaciej W. Rozycki 		value = xcp->regs[MIPSInst_RT(ir)];
919d4f5b088SMaciej W. Rozycki 
920c491cfa2SMaciej W. Rozycki 	switch (MIPSInst_RD(ir)) {
921c491cfa2SMaciej W. Rozycki 	case FPCREG_CSR:
922d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->csr=%08x\n",
923c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
924d4f5b088SMaciej W. Rozycki 
9259b26616cSMaciej W. Rozycki 		/* Preserve read-only bits.  */
92603dce595SMaciej W. Rozycki 		mask = boot_cpu_data.fpu_msk31;
9279b26616cSMaciej W. Rozycki 		fcr31 = (value & ~mask) | (fcr31 & mask);
928c491cfa2SMaciej W. Rozycki 		break;
929c491cfa2SMaciej W. Rozycki 
930c491cfa2SMaciej W. Rozycki 	case FPCREG_FENR:
931c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
932c491cfa2SMaciej W. Rozycki 			break;
933c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->enr=%08x\n",
934c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
935c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
936c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
937c491cfa2SMaciej W. Rozycki 			 FPU_CSR_FS;
938c491cfa2SMaciej W. Rozycki 		fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
939c491cfa2SMaciej W. Rozycki 		break;
940c491cfa2SMaciej W. Rozycki 
941c491cfa2SMaciej W. Rozycki 	case FPCREG_FEXR:
942c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
943c491cfa2SMaciej W. Rozycki 			break;
944c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->exr=%08x\n",
945c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
946c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
947c491cfa2SMaciej W. Rozycki 		fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
948c491cfa2SMaciej W. Rozycki 		break;
949c491cfa2SMaciej W. Rozycki 
950c491cfa2SMaciej W. Rozycki 	case FPCREG_FCCR:
951c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
952c491cfa2SMaciej W. Rozycki 			break;
953c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->ccr=%08x\n",
954c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
955c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
956c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
957c491cfa2SMaciej W. Rozycki 			 FPU_CSR_COND;
958c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
959c491cfa2SMaciej W. Rozycki 			 FPU_CSR_CONDX;
960c491cfa2SMaciej W. Rozycki 		break;
961c491cfa2SMaciej W. Rozycki 
962c491cfa2SMaciej W. Rozycki 	default:
963c491cfa2SMaciej W. Rozycki 		break;
964d4f5b088SMaciej W. Rozycki 	}
965c491cfa2SMaciej W. Rozycki 
966c491cfa2SMaciej W. Rozycki 	ctx->fcr31 = fcr31;
967d4f5b088SMaciej W. Rozycki }
968d4f5b088SMaciej W. Rozycki 
969d4f5b088SMaciej W. Rozycki /*
9701da177e4SLinus Torvalds  * Emulate the single floating point instruction pointed at by EPC.
9711da177e4SLinus Torvalds  * Two instructions if the instruction is in a branch delay slot.
9721da177e4SLinus Torvalds  */
9731da177e4SLinus Torvalds 
974515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
975102cedc3SLeonid Yegoshin 		struct mm_decoded_insn dec_insn, void *__user *fault_addr)
9761da177e4SLinus Torvalds {
977102cedc3SLeonid Yegoshin 	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
97893583e17SPaul Burton 	unsigned int cond, cbit, bit0;
9793f7cac41SRalf Baechle 	mips_instruction ir;
9803f7cac41SRalf Baechle 	int likely, pc_inc;
98193583e17SPaul Burton 	union fpureg *fpr;
9823f7cac41SRalf Baechle 	u32 __user *wva;
9833f7cac41SRalf Baechle 	u64 __user *dva;
9843f7cac41SRalf Baechle 	u32 wval;
9853f7cac41SRalf Baechle 	u64 dval;
9863f7cac41SRalf Baechle 	int sig;
9871da177e4SLinus Torvalds 
98870e4c234SRalf Baechle 	/*
98970e4c234SRalf Baechle 	 * These are giving gcc a gentle hint about what to expect in
99070e4c234SRalf Baechle 	 * dec_inst in order to do better optimization.
99170e4c234SRalf Baechle 	 */
99270e4c234SRalf Baechle 	if (!cpu_has_mmips && dec_insn.micro_mips_mode)
99370e4c234SRalf Baechle 		unreachable();
99470e4c234SRalf Baechle 
9951da177e4SLinus Torvalds 	/* XXX NEC Vr54xx bug workaround */
996e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
997102cedc3SLeonid Yegoshin 		if (dec_insn.micro_mips_mode) {
998102cedc3SLeonid Yegoshin 			if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
999e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
1000102cedc3SLeonid Yegoshin 		} else {
1001102cedc3SLeonid Yegoshin 			if (!isBranchInstr(xcp, dec_insn, &contpc))
1002e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
1003102cedc3SLeonid Yegoshin 		}
1004102cedc3SLeonid Yegoshin 	}
10051da177e4SLinus Torvalds 
1006e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
10071da177e4SLinus Torvalds 		/*
10081da177e4SLinus Torvalds 		 * The instruction to be emulated is in a branch delay slot
10091da177e4SLinus Torvalds 		 * which means that we have to	emulate the branch instruction
10101da177e4SLinus Torvalds 		 * BEFORE we do the cop1 instruction.
10111da177e4SLinus Torvalds 		 *
10121da177e4SLinus Torvalds 		 * This branch could be a COP1 branch, but in that case we
10131da177e4SLinus Torvalds 		 * would have had a trap for that instruction, and would not
10141da177e4SLinus Torvalds 		 * come through this route.
10151da177e4SLinus Torvalds 		 *
10161da177e4SLinus Torvalds 		 * Linux MIPS branch emulator operates on context, updating the
10171da177e4SLinus Torvalds 		 * cp0_epc.
10181da177e4SLinus Torvalds 		 */
1019102cedc3SLeonid Yegoshin 		ir = dec_insn.next_insn;  /* process delay slot instr */
1020102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.next_pc_inc;
1021333d1f67SRalf Baechle 	} else {
1022102cedc3SLeonid Yegoshin 		ir = dec_insn.insn;       /* process current instr */
1023102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.pc_inc;
1024102cedc3SLeonid Yegoshin 	}
1025102cedc3SLeonid Yegoshin 
1026102cedc3SLeonid Yegoshin 	/*
1027102cedc3SLeonid Yegoshin 	 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
1028102cedc3SLeonid Yegoshin 	 * instructions, we want to convert microMIPS FPU instructions
1029102cedc3SLeonid Yegoshin 	 * into MIPS32 instructions so that we could reuse all of the
1030102cedc3SLeonid Yegoshin 	 * FPU emulation code.
1031102cedc3SLeonid Yegoshin 	 *
1032102cedc3SLeonid Yegoshin 	 * NOTE: We cannot do this for branch instructions since they
1033102cedc3SLeonid Yegoshin 	 *       are not a subset. Example: Cannot emulate a 16-bit
1034102cedc3SLeonid Yegoshin 	 *       aligned target address with a MIPS32 instruction.
1035102cedc3SLeonid Yegoshin 	 */
1036102cedc3SLeonid Yegoshin 	if (dec_insn.micro_mips_mode) {
1037102cedc3SLeonid Yegoshin 		/*
1038102cedc3SLeonid Yegoshin 		 * If next instruction is a 16-bit instruction, then it
1039102cedc3SLeonid Yegoshin 		 * it cannot be a FPU instruction. This could happen
1040102cedc3SLeonid Yegoshin 		 * since we can be called for non-FPU instructions.
1041102cedc3SLeonid Yegoshin 		 */
1042102cedc3SLeonid Yegoshin 		if ((pc_inc == 2) ||
1043102cedc3SLeonid Yegoshin 			(microMIPS32_to_MIPS32((union mips_instruction *)&ir)
1044102cedc3SLeonid Yegoshin 			 == SIGILL))
1045102cedc3SLeonid Yegoshin 			return SIGILL;
10461da177e4SLinus Torvalds 	}
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds emul:
1049a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
1050b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(emulated);
10511da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(ir)) {
10523f7cac41SRalf Baechle 	case ldc1_op:
10533f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10541da177e4SLinus Torvalds 				     MIPSInst_SIMM(ir));
1055b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
1056515b029dSDavid Daney 
10573f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
1058b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10593f7cac41SRalf Baechle 			*fault_addr = dva;
10601da177e4SLinus Torvalds 			return SIGBUS;
10611da177e4SLinus Torvalds 		}
10623f7cac41SRalf Baechle 		if (__get_user(dval, dva)) {
1063515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10643f7cac41SRalf Baechle 			*fault_addr = dva;
1065515b029dSDavid Daney 			return SIGSEGV;
1066515b029dSDavid Daney 		}
10673f7cac41SRalf Baechle 		DITOREG(dval, MIPSInst_RT(ir));
10681da177e4SLinus Torvalds 		break;
10691da177e4SLinus Torvalds 
10703f7cac41SRalf Baechle 	case sdc1_op:
10713f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10721da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1073b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
10743f7cac41SRalf Baechle 		DIFROMREG(dval, MIPSInst_RT(ir));
10753f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
1076b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10773f7cac41SRalf Baechle 			*fault_addr = dva;
10781da177e4SLinus Torvalds 			return SIGBUS;
10791da177e4SLinus Torvalds 		}
10803f7cac41SRalf Baechle 		if (__put_user(dval, dva)) {
1081515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10823f7cac41SRalf Baechle 			*fault_addr = dva;
1083515b029dSDavid Daney 			return SIGSEGV;
1084515b029dSDavid Daney 		}
10851da177e4SLinus Torvalds 		break;
10861da177e4SLinus Torvalds 
10873f7cac41SRalf Baechle 	case lwc1_op:
10883f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10891da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1090b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
10913f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
1092b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10933f7cac41SRalf Baechle 			*fault_addr = wva;
10941da177e4SLinus Torvalds 			return SIGBUS;
10951da177e4SLinus Torvalds 		}
10963f7cac41SRalf Baechle 		if (__get_user(wval, wva)) {
1097515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10983f7cac41SRalf Baechle 			*fault_addr = wva;
1099515b029dSDavid Daney 			return SIGSEGV;
1100515b029dSDavid Daney 		}
11013f7cac41SRalf Baechle 		SITOREG(wval, MIPSInst_RT(ir));
11021da177e4SLinus Torvalds 		break;
11031da177e4SLinus Torvalds 
11043f7cac41SRalf Baechle 	case swc1_op:
11053f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
11061da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1107b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
11083f7cac41SRalf Baechle 		SIFROMREG(wval, MIPSInst_RT(ir));
11093f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
1110b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11113f7cac41SRalf Baechle 			*fault_addr = wva;
11121da177e4SLinus Torvalds 			return SIGBUS;
11131da177e4SLinus Torvalds 		}
11143f7cac41SRalf Baechle 		if (__put_user(wval, wva)) {
1115515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11163f7cac41SRalf Baechle 			*fault_addr = wva;
1117515b029dSDavid Daney 			return SIGSEGV;
1118515b029dSDavid Daney 		}
11191da177e4SLinus Torvalds 		break;
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	case cop1_op:
11221da177e4SLinus Torvalds 		switch (MIPSInst_RS(ir)) {
11231da177e4SLinus Torvalds 		case dmfc_op:
112408a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
112508a07904SRalf Baechle 				return SIGILL;
112608a07904SRalf Baechle 
11271da177e4SLinus Torvalds 			/* copregister fs -> gpr[rt] */
11281da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
11291da177e4SLinus Torvalds 				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
11301da177e4SLinus Torvalds 					MIPSInst_RD(ir));
11311da177e4SLinus Torvalds 			}
11321da177e4SLinus Torvalds 			break;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 		case dmtc_op:
113508a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
113608a07904SRalf Baechle 				return SIGILL;
113708a07904SRalf Baechle 
11381da177e4SLinus Torvalds 			/* copregister fs <- rt */
11391da177e4SLinus Torvalds 			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11401da177e4SLinus Torvalds 			break;
11411da177e4SLinus Torvalds 
11421ac94400SLeonid Yegoshin 		case mfhc_op:
1143e8f80cc1SMarkos Chandras 			if (!cpu_has_mips_r2_r6)
11441ac94400SLeonid Yegoshin 				goto sigill;
11451ac94400SLeonid Yegoshin 
11461ac94400SLeonid Yegoshin 			/* copregister rd -> gpr[rt] */
11471ac94400SLeonid Yegoshin 			if (MIPSInst_RT(ir) != 0) {
11481ac94400SLeonid Yegoshin 				SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
11491ac94400SLeonid Yegoshin 					MIPSInst_RD(ir));
11501ac94400SLeonid Yegoshin 			}
11511ac94400SLeonid Yegoshin 			break;
11521ac94400SLeonid Yegoshin 
11531ac94400SLeonid Yegoshin 		case mthc_op:
1154e8f80cc1SMarkos Chandras 			if (!cpu_has_mips_r2_r6)
11551ac94400SLeonid Yegoshin 				goto sigill;
11561ac94400SLeonid Yegoshin 
11571ac94400SLeonid Yegoshin 			/* copregister rd <- gpr[rt] */
11581ac94400SLeonid Yegoshin 			SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11591ac94400SLeonid Yegoshin 			break;
11601ac94400SLeonid Yegoshin 
11611da177e4SLinus Torvalds 		case mfc_op:
11621da177e4SLinus Torvalds 			/* copregister rd -> gpr[rt] */
11631da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
11641da177e4SLinus Torvalds 				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
11651da177e4SLinus Torvalds 					MIPSInst_RD(ir));
11661da177e4SLinus Torvalds 			}
11671da177e4SLinus Torvalds 			break;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 		case mtc_op:
11701da177e4SLinus Torvalds 			/* copregister rd <- rt */
11711da177e4SLinus Torvalds 			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11721da177e4SLinus Torvalds 			break;
11731da177e4SLinus Torvalds 
11743f7cac41SRalf Baechle 		case cfc_op:
11751da177e4SLinus Torvalds 			/* cop control register rd -> gpr[rt] */
1176d4f5b088SMaciej W. Rozycki 			cop1_cfc(xcp, ctx, ir);
11771da177e4SLinus Torvalds 			break;
11781da177e4SLinus Torvalds 
11793f7cac41SRalf Baechle 		case ctc_op:
11801da177e4SLinus Torvalds 			/* copregister rd <- rt */
1181d4f5b088SMaciej W. Rozycki 			cop1_ctc(xcp, ctx, ir);
11821da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
11831da177e4SLinus Torvalds 				return SIGFPE;
11841da177e4SLinus Torvalds 			}
11851da177e4SLinus Torvalds 			break;
11861da177e4SLinus Torvalds 
1187c909ca71SMarkos Chandras 		case bc1eqz_op:
1188c909ca71SMarkos Chandras 		case bc1nez_op:
1189c909ca71SMarkos Chandras 			if (!cpu_has_mips_r6 || delay_slot(xcp))
1190c909ca71SMarkos Chandras 				return SIGILL;
1191c909ca71SMarkos Chandras 
1192c909ca71SMarkos Chandras 			cond = likely = 0;
119393583e17SPaul Burton 			fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
119493583e17SPaul Burton 			bit0 = get_fpr32(fpr, 0) & 0x1;
1195c909ca71SMarkos Chandras 			switch (MIPSInst_RS(ir)) {
1196c909ca71SMarkos Chandras 			case bc1eqz_op:
119793583e17SPaul Burton 				cond = bit0 == 0;
1198c909ca71SMarkos Chandras 				break;
1199c909ca71SMarkos Chandras 			case bc1nez_op:
120093583e17SPaul Burton 				cond = bit0 != 0;
1201c909ca71SMarkos Chandras 				break;
1202c909ca71SMarkos Chandras 			}
1203c909ca71SMarkos Chandras 			goto branch_common;
1204c909ca71SMarkos Chandras 
12053f7cac41SRalf Baechle 		case bc_op:
1206e7e9cae5SRalf Baechle 			if (delay_slot(xcp))
12071da177e4SLinus Torvalds 				return SIGILL;
12081da177e4SLinus Torvalds 
120908a07904SRalf Baechle 			if (cpu_has_mips_4_5_r)
121008a07904SRalf Baechle 				cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
121108a07904SRalf Baechle 			else
121208a07904SRalf Baechle 				cbit = FPU_CSR_COND;
121308a07904SRalf Baechle 			cond = ctx->fcr31 & cbit;
121408a07904SRalf Baechle 
12153f7cac41SRalf Baechle 			likely = 0;
12161da177e4SLinus Torvalds 			switch (MIPSInst_RT(ir) & 3) {
12171da177e4SLinus Torvalds 			case bcfl_op:
12182d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
12191da177e4SLinus Torvalds 					likely = 1;
12202d83fea7SMaciej W. Rozycki 				/* Fall through */
12211da177e4SLinus Torvalds 			case bcf_op:
12221da177e4SLinus Torvalds 				cond = !cond;
12231da177e4SLinus Torvalds 				break;
12241da177e4SLinus Torvalds 			case bctl_op:
12252d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
12261da177e4SLinus Torvalds 					likely = 1;
12272d83fea7SMaciej W. Rozycki 				/* Fall through */
12281da177e4SLinus Torvalds 			case bct_op:
12291da177e4SLinus Torvalds 				break;
12301da177e4SLinus Torvalds 			}
1231c909ca71SMarkos Chandras branch_common:
1232e7e9cae5SRalf Baechle 			set_delay_slot(xcp);
12331da177e4SLinus Torvalds 			if (cond) {
12343f7cac41SRalf Baechle 				/*
12353f7cac41SRalf Baechle 				 * Branch taken: emulate dslot instruction
12361da177e4SLinus Torvalds 				 */
12379ab4471cSMaciej W. Rozycki 				unsigned long bcpc;
12389ab4471cSMaciej W. Rozycki 
12399ab4471cSMaciej W. Rozycki 				/*
12409ab4471cSMaciej W. Rozycki 				 * Remember EPC at the branch to point back
12419ab4471cSMaciej W. Rozycki 				 * at so that any delay-slot instruction
12429ab4471cSMaciej W. Rozycki 				 * signal is not silently ignored.
12439ab4471cSMaciej W. Rozycki 				 */
12449ab4471cSMaciej W. Rozycki 				bcpc = xcp->cp0_epc;
1245102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
12461da177e4SLinus Torvalds 
1247102cedc3SLeonid Yegoshin 				contpc = MIPSInst_SIMM(ir);
1248102cedc3SLeonid Yegoshin 				ir = dec_insn.next_insn;
1249102cedc3SLeonid Yegoshin 				if (dec_insn.micro_mips_mode) {
1250102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 1));
1251102cedc3SLeonid Yegoshin 
1252102cedc3SLeonid Yegoshin 					/* If 16-bit instruction, not FPU. */
1253102cedc3SLeonid Yegoshin 					if ((dec_insn.next_pc_inc == 2) ||
1254102cedc3SLeonid Yegoshin 						(microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1255102cedc3SLeonid Yegoshin 
1256102cedc3SLeonid Yegoshin 						/*
1257102cedc3SLeonid Yegoshin 						 * Since this instruction will
1258102cedc3SLeonid Yegoshin 						 * be put on the stack with
1259102cedc3SLeonid Yegoshin 						 * 32-bit words, get around
1260102cedc3SLeonid Yegoshin 						 * this problem by putting a
1261102cedc3SLeonid Yegoshin 						 * NOP16 as the second one.
1262102cedc3SLeonid Yegoshin 						 */
1263102cedc3SLeonid Yegoshin 						if (dec_insn.next_pc_inc == 2)
1264102cedc3SLeonid Yegoshin 							ir = (ir & (~0xffff)) | MM_NOP16;
1265102cedc3SLeonid Yegoshin 
1266102cedc3SLeonid Yegoshin 						/*
1267102cedc3SLeonid Yegoshin 						 * Single step the non-CP1
1268102cedc3SLeonid Yegoshin 						 * instruction in the dslot.
1269102cedc3SLeonid Yegoshin 						 */
12709ab4471cSMaciej W. Rozycki 						sig = mips_dsemul(xcp, ir,
12719ab4471cSMaciej W. Rozycki 								  contpc);
1272e4553573SMaciej W. Rozycki 						if (sig < 0)
1273e4553573SMaciej W. Rozycki 							break;
12749ab4471cSMaciej W. Rozycki 						if (sig)
12759ab4471cSMaciej W. Rozycki 							xcp->cp0_epc = bcpc;
12769ab4471cSMaciej W. Rozycki 						/*
12779ab4471cSMaciej W. Rozycki 						 * SIGILL forces out of
12789ab4471cSMaciej W. Rozycki 						 * the emulation loop.
12799ab4471cSMaciej W. Rozycki 						 */
12809ab4471cSMaciej W. Rozycki 						return sig ? sig : SIGILL;
1281515b029dSDavid Daney 					}
1282102cedc3SLeonid Yegoshin 				} else
1283102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 2));
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 				switch (MIPSInst_OPCODE(ir)) {
12861da177e4SLinus Torvalds 				case lwc1_op:
12871da177e4SLinus Torvalds 				case swc1_op:
128808a07904SRalf Baechle 					goto emul;
12893f7cac41SRalf Baechle 
12901da177e4SLinus Torvalds 				case ldc1_op:
12911da177e4SLinus Torvalds 				case sdc1_op:
12922d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_2_3_4_5_r)
129308a07904SRalf Baechle 						goto emul;
129408a07904SRalf Baechle 
12959ab4471cSMaciej W. Rozycki 					goto bc_sigill;
12963f7cac41SRalf Baechle 
12971da177e4SLinus Torvalds 				case cop1_op:
129808a07904SRalf Baechle 					goto emul;
12993f7cac41SRalf Baechle 
13001da177e4SLinus Torvalds 				case cop1x_op:
13012d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_4_5_64_r2_r6)
13021da177e4SLinus Torvalds 						/* its one of ours */
13031da177e4SLinus Torvalds 						goto emul;
130408a07904SRalf Baechle 
13059ab4471cSMaciej W. Rozycki 					goto bc_sigill;
13063f7cac41SRalf Baechle 
13071da177e4SLinus Torvalds 				case spec_op:
13082d83fea7SMaciej W. Rozycki 					switch (MIPSInst_FUNC(ir)) {
13092d83fea7SMaciej W. Rozycki 					case movc_op:
13102d83fea7SMaciej W. Rozycki 						if (cpu_has_mips_4_5_r)
13111da177e4SLinus Torvalds 							goto emul;
13122d83fea7SMaciej W. Rozycki 
13139ab4471cSMaciej W. Rozycki 						goto bc_sigill;
13142d83fea7SMaciej W. Rozycki 					}
13151da177e4SLinus Torvalds 					break;
13169ab4471cSMaciej W. Rozycki 
13179ab4471cSMaciej W. Rozycki 				bc_sigill:
13189ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
13199ab4471cSMaciej W. Rozycki 					return SIGILL;
13201da177e4SLinus Torvalds 				}
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 				/*
13231da177e4SLinus Torvalds 				 * Single step the non-cp1
13241da177e4SLinus Torvalds 				 * instruction in the dslot
13251da177e4SLinus Torvalds 				 */
13269ab4471cSMaciej W. Rozycki 				sig = mips_dsemul(xcp, ir, contpc);
1327e4553573SMaciej W. Rozycki 				if (sig < 0)
1328e4553573SMaciej W. Rozycki 					break;
13299ab4471cSMaciej W. Rozycki 				if (sig)
13309ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
13319ab4471cSMaciej W. Rozycki 				/* SIGILL forces out of the emulation loop.  */
13329ab4471cSMaciej W. Rozycki 				return sig ? sig : SIGILL;
13333f7cac41SRalf Baechle 			} else if (likely) {	/* branch not taken */
13341da177e4SLinus Torvalds 				/*
13351da177e4SLinus Torvalds 				 * branch likely nullifies
13361da177e4SLinus Torvalds 				 * dslot if not taken
13371da177e4SLinus Torvalds 				 */
1338102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
1339102cedc3SLeonid Yegoshin 				contpc += dec_insn.pc_inc;
13401da177e4SLinus Torvalds 				/*
13411da177e4SLinus Torvalds 				 * else continue & execute
13421da177e4SLinus Torvalds 				 * dslot as normal insn
13431da177e4SLinus Torvalds 				 */
13441da177e4SLinus Torvalds 			}
13451da177e4SLinus Torvalds 			break;
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 		default:
13481da177e4SLinus Torvalds 			if (!(MIPSInst_RS(ir) & 0x10))
13491da177e4SLinus Torvalds 				return SIGILL;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 			/* a real fpu computation instruction */
13521da177e4SLinus Torvalds 			if ((sig = fpu_emu(xcp, ctx, ir)))
13531da177e4SLinus Torvalds 				return sig;
13541da177e4SLinus Torvalds 		}
13551da177e4SLinus Torvalds 		break;
13561da177e4SLinus Torvalds 
13573f7cac41SRalf Baechle 	case cop1x_op:
13582d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_4_5_64_r2_r6)
135908a07904SRalf Baechle 			return SIGILL;
136008a07904SRalf Baechle 
136108a07904SRalf Baechle 		sig = fpux_emu(xcp, ctx, ir, fault_addr);
1362515b029dSDavid Daney 		if (sig)
13631da177e4SLinus Torvalds 			return sig;
13641da177e4SLinus Torvalds 		break;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	case spec_op:
136708a07904SRalf Baechle 		if (!cpu_has_mips_4_5_r)
136808a07904SRalf Baechle 			return SIGILL;
136908a07904SRalf Baechle 
13701da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != movc_op)
13711da177e4SLinus Torvalds 			return SIGILL;
13721da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
13731da177e4SLinus Torvalds 		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
13741da177e4SLinus Torvalds 			xcp->regs[MIPSInst_RD(ir)] =
13751da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RS(ir)];
13761da177e4SLinus Torvalds 		break;
13771da177e4SLinus Torvalds 	default:
13781ac94400SLeonid Yegoshin sigill:
13791da177e4SLinus Torvalds 		return SIGILL;
13801da177e4SLinus Torvalds 	}
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	/* we did it !! */
1383e70dfc10SAtsushi Nemoto 	xcp->cp0_epc = contpc;
1384e7e9cae5SRalf Baechle 	clear_delay_slot(xcp);
1385333d1f67SRalf Baechle 
13861da177e4SLinus Torvalds 	return 0;
13871da177e4SLinus Torvalds }
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds /*
13901da177e4SLinus Torvalds  * Conversion table from MIPS compare ops 48-63
13911da177e4SLinus Torvalds  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
13921da177e4SLinus Torvalds  */
13931da177e4SLinus Torvalds static const unsigned char cmptab[8] = {
13941da177e4SLinus Torvalds 	0,			/* cmp_0 (sig) cmp_sf */
13951da177e4SLinus Torvalds 	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
13961da177e4SLinus Torvalds 	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
13971da177e4SLinus Torvalds 	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
13981da177e4SLinus Torvalds 	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
13991da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
14001da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
14011da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
14021da177e4SLinus Torvalds };
14031da177e4SLinus Torvalds 
1404f8c3c671SMarkos Chandras static const unsigned char negative_cmptab[8] = {
1405f8c3c671SMarkos Chandras 	0, /* Reserved */
1406f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
1407f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
1408f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT,
1409f8c3c671SMarkos Chandras 	/* Reserved */
1410f8c3c671SMarkos Chandras };
1411f8c3c671SMarkos Chandras 
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds /*
14141da177e4SLinus Torvalds  * Additional MIPS4 instructions
14151da177e4SLinus Torvalds  */
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3)					\
141847fa0c02SRalf Baechle static union ieee754##p fpemu_##p##_##name(union ieee754##p r,		\
141947fa0c02SRalf Baechle 	union ieee754##p s, union ieee754##p t)				\
14201da177e4SLinus Torvalds {									\
1421cd21dfcfSRalf Baechle 	struct _ieee754_csr ieee754_csr_save;				\
14221da177e4SLinus Torvalds 	s = f1(s, t);							\
14231da177e4SLinus Torvalds 	ieee754_csr_save = ieee754_csr;					\
14241da177e4SLinus Torvalds 	s = f2(s, r);							\
14251da177e4SLinus Torvalds 	ieee754_csr_save.cx |= ieee754_csr.cx;				\
14261da177e4SLinus Torvalds 	ieee754_csr_save.sx |= ieee754_csr.sx;				\
14271da177e4SLinus Torvalds 	s = f3(s);							\
14281da177e4SLinus Torvalds 	ieee754_csr.cx |= ieee754_csr_save.cx;				\
14291da177e4SLinus Torvalds 	ieee754_csr.sx |= ieee754_csr_save.sx;				\
14301da177e4SLinus Torvalds 	return s;							\
14311da177e4SLinus Torvalds }
14321da177e4SLinus Torvalds 
14332209bcb1SRalf Baechle static union ieee754dp fpemu_dp_recip(union ieee754dp d)
14341da177e4SLinus Torvalds {
14351da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), d);
14361da177e4SLinus Torvalds }
14371da177e4SLinus Torvalds 
14382209bcb1SRalf Baechle static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
14391da177e4SLinus Torvalds {
14401da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
14411da177e4SLinus Torvalds }
14421da177e4SLinus Torvalds 
14432209bcb1SRalf Baechle static union ieee754sp fpemu_sp_recip(union ieee754sp s)
14441da177e4SLinus Torvalds {
14451da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), s);
14461da177e4SLinus Torvalds }
14471da177e4SLinus Torvalds 
14482209bcb1SRalf Baechle static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
14491da177e4SLinus Torvalds {
14501da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
14541da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
14551da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
14561da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
14571da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
14581da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
14591da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
14601da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
14611da177e4SLinus Torvalds 
1462eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1463515b029dSDavid Daney 	mips_instruction ir, void *__user *fault_addr)
14641da177e4SLinus Torvalds {
14651da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
14661da177e4SLinus Torvalds 
1467b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1xops);
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	switch (MIPSInst_FMA_FFMT(ir)) {
14701da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
14711da177e4SLinus Torvalds 
14722209bcb1SRalf Baechle 		union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
14732209bcb1SRalf Baechle 		union ieee754sp fd, fr, fs, ft;
14743fccc015SRalf Baechle 		u32 __user *va;
14751da177e4SLinus Torvalds 		u32 val;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
14781da177e4SLinus Torvalds 		case lwxc1_op:
14793fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14801da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14811da177e4SLinus Torvalds 
1482b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1483515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1484b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1485515b029dSDavid Daney 				*fault_addr = va;
14861da177e4SLinus Torvalds 				return SIGBUS;
14871da177e4SLinus Torvalds 			}
1488515b029dSDavid Daney 			if (__get_user(val, va)) {
1489515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1490515b029dSDavid Daney 				*fault_addr = va;
1491515b029dSDavid Daney 				return SIGSEGV;
1492515b029dSDavid Daney 			}
14931da177e4SLinus Torvalds 			SITOREG(val, MIPSInst_FD(ir));
14941da177e4SLinus Torvalds 			break;
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds 		case swxc1_op:
14973fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14981da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14991da177e4SLinus Torvalds 
1500b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 			SIFROMREG(val, MIPSInst_FS(ir));
1503515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1504515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1505515b029dSDavid Daney 				*fault_addr = va;
1506515b029dSDavid Daney 				return SIGBUS;
1507515b029dSDavid Daney 			}
15081da177e4SLinus Torvalds 			if (put_user(val, va)) {
1509b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1510515b029dSDavid Daney 				*fault_addr = va;
1511515b029dSDavid Daney 				return SIGSEGV;
15121da177e4SLinus Torvalds 			}
15131da177e4SLinus Torvalds 			break;
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 		case madd_s_op:
15161da177e4SLinus Torvalds 			handler = fpemu_sp_madd;
15171da177e4SLinus Torvalds 			goto scoptop;
15181da177e4SLinus Torvalds 		case msub_s_op:
15191da177e4SLinus Torvalds 			handler = fpemu_sp_msub;
15201da177e4SLinus Torvalds 			goto scoptop;
15211da177e4SLinus Torvalds 		case nmadd_s_op:
15221da177e4SLinus Torvalds 			handler = fpemu_sp_nmadd;
15231da177e4SLinus Torvalds 			goto scoptop;
15241da177e4SLinus Torvalds 		case nmsub_s_op:
15251da177e4SLinus Torvalds 			handler = fpemu_sp_nmsub;
15261da177e4SLinus Torvalds 			goto scoptop;
15271da177e4SLinus Torvalds 
15281da177e4SLinus Torvalds 		      scoptop:
15291da177e4SLinus Torvalds 			SPFROMREG(fr, MIPSInst_FR(ir));
15301da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
15311da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
15321da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
15331da177e4SLinus Torvalds 			SPTOREG(fd, MIPSInst_FD(ir));
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 		      copcsr:
1536c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1537c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
15381da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1539c4103526SDeng-Cheng Zhu 			}
1540c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1541c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
15421da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1543c4103526SDeng-Cheng Zhu 			}
1544c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1545c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
15461da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1547c4103526SDeng-Cheng Zhu 			}
1548c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1549c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
15501da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1551c4103526SDeng-Cheng Zhu 			}
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
15541da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
15553f7cac41SRalf Baechle 				/*printk ("SIGFPE: FPU csr = %08x\n",
15561da177e4SLinus Torvalds 				   ctx->fcr31); */
15571da177e4SLinus Torvalds 				return SIGFPE;
15581da177e4SLinus Torvalds 			}
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 			break;
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 		default:
15631da177e4SLinus Torvalds 			return SIGILL;
15641da177e4SLinus Torvalds 		}
15651da177e4SLinus Torvalds 		break;
15661da177e4SLinus Torvalds 	}
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds 	case d_fmt:{		/* 1 */
15692209bcb1SRalf Baechle 		union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
15702209bcb1SRalf Baechle 		union ieee754dp fd, fr, fs, ft;
15713fccc015SRalf Baechle 		u64 __user *va;
15721da177e4SLinus Torvalds 		u64 val;
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
15751da177e4SLinus Torvalds 		case ldxc1_op:
15763fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15771da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15781da177e4SLinus Torvalds 
1579b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1580515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1581b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1582515b029dSDavid Daney 				*fault_addr = va;
15831da177e4SLinus Torvalds 				return SIGBUS;
15841da177e4SLinus Torvalds 			}
1585515b029dSDavid Daney 			if (__get_user(val, va)) {
1586515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1587515b029dSDavid Daney 				*fault_addr = va;
1588515b029dSDavid Daney 				return SIGSEGV;
1589515b029dSDavid Daney 			}
15901da177e4SLinus Torvalds 			DITOREG(val, MIPSInst_FD(ir));
15911da177e4SLinus Torvalds 			break;
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 		case sdxc1_op:
15943fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15951da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15961da177e4SLinus Torvalds 
1597b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
15981da177e4SLinus Torvalds 			DIFROMREG(val, MIPSInst_FS(ir));
1599515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1600b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1601515b029dSDavid Daney 				*fault_addr = va;
16021da177e4SLinus Torvalds 				return SIGBUS;
16031da177e4SLinus Torvalds 			}
1604515b029dSDavid Daney 			if (__put_user(val, va)) {
1605515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1606515b029dSDavid Daney 				*fault_addr = va;
1607515b029dSDavid Daney 				return SIGSEGV;
1608515b029dSDavid Daney 			}
16091da177e4SLinus Torvalds 			break;
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds 		case madd_d_op:
16121da177e4SLinus Torvalds 			handler = fpemu_dp_madd;
16131da177e4SLinus Torvalds 			goto dcoptop;
16141da177e4SLinus Torvalds 		case msub_d_op:
16151da177e4SLinus Torvalds 			handler = fpemu_dp_msub;
16161da177e4SLinus Torvalds 			goto dcoptop;
16171da177e4SLinus Torvalds 		case nmadd_d_op:
16181da177e4SLinus Torvalds 			handler = fpemu_dp_nmadd;
16191da177e4SLinus Torvalds 			goto dcoptop;
16201da177e4SLinus Torvalds 		case nmsub_d_op:
16211da177e4SLinus Torvalds 			handler = fpemu_dp_nmsub;
16221da177e4SLinus Torvalds 			goto dcoptop;
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 		      dcoptop:
16251da177e4SLinus Torvalds 			DPFROMREG(fr, MIPSInst_FR(ir));
16261da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
16271da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
16281da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
16291da177e4SLinus Torvalds 			DPTOREG(fd, MIPSInst_FD(ir));
16301da177e4SLinus Torvalds 			goto copcsr;
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 		default:
16331da177e4SLinus Torvalds 			return SIGILL;
16341da177e4SLinus Torvalds 		}
16351da177e4SLinus Torvalds 		break;
16361da177e4SLinus Torvalds 	}
16371da177e4SLinus Torvalds 
163851061b88SDeng-Cheng Zhu 	case 0x3:
163951061b88SDeng-Cheng Zhu 		if (MIPSInst_FUNC(ir) != pfetch_op)
16401da177e4SLinus Torvalds 			return SIGILL;
164151061b88SDeng-Cheng Zhu 
16421da177e4SLinus Torvalds 		/* ignore prefx operation */
16431da177e4SLinus Torvalds 		break;
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	default:
16461da177e4SLinus Torvalds 		return SIGILL;
16471da177e4SLinus Torvalds 	}
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds 	return 0;
16501da177e4SLinus Torvalds }
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds /*
16551da177e4SLinus Torvalds  * Emulate a single COP1 arithmetic instruction.
16561da177e4SLinus Torvalds  */
1657eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
16581da177e4SLinus Torvalds 	mips_instruction ir)
16591da177e4SLinus Torvalds {
16601da177e4SLinus Torvalds 	int rfmt;		/* resulting format */
16611da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
16623f7cac41SRalf Baechle 	unsigned int oldrm;
16633f7cac41SRalf Baechle 	unsigned int cbit;
16641da177e4SLinus Torvalds 	unsigned cond;
16651da177e4SLinus Torvalds 	union {
16662209bcb1SRalf Baechle 		union ieee754dp d;
16672209bcb1SRalf Baechle 		union ieee754sp s;
16681da177e4SLinus Torvalds 		int w;
16691da177e4SLinus Torvalds 		s64 l;
16701da177e4SLinus Torvalds 	} rv;			/* resulting value */
16713f7cac41SRalf Baechle 	u64 bits;
16721da177e4SLinus Torvalds 
1673b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1ops);
16741da177e4SLinus Torvalds 	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
16751da177e4SLinus Torvalds 	case s_fmt: {		/* 0 */
16761da177e4SLinus Torvalds 		union {
16772209bcb1SRalf Baechle 			union ieee754sp(*b) (union ieee754sp, union ieee754sp);
16782209bcb1SRalf Baechle 			union ieee754sp(*u) (union ieee754sp);
16791da177e4SLinus Torvalds 		} handler;
16804b820d95SPaul Burton 		union ieee754sp fd, fs, ft;
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
16831da177e4SLinus Torvalds 			/* binary ops */
16841da177e4SLinus Torvalds 		case fadd_op:
16851da177e4SLinus Torvalds 			handler.b = ieee754sp_add;
16861da177e4SLinus Torvalds 			goto scopbop;
16871da177e4SLinus Torvalds 		case fsub_op:
16881da177e4SLinus Torvalds 			handler.b = ieee754sp_sub;
16891da177e4SLinus Torvalds 			goto scopbop;
16901da177e4SLinus Torvalds 		case fmul_op:
16911da177e4SLinus Torvalds 			handler.b = ieee754sp_mul;
16921da177e4SLinus Torvalds 			goto scopbop;
16931da177e4SLinus Torvalds 		case fdiv_op:
16941da177e4SLinus Torvalds 			handler.b = ieee754sp_div;
16951da177e4SLinus Torvalds 			goto scopbop;
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 			/* unary  ops */
16981da177e4SLinus Torvalds 		case fsqrt_op:
16992d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
170008a07904SRalf Baechle 				return SIGILL;
170108a07904SRalf Baechle 
17021da177e4SLinus Torvalds 			handler.u = ieee754sp_sqrt;
17031da177e4SLinus Torvalds 			goto scopuop;
17043f7cac41SRalf Baechle 
170508a07904SRalf Baechle 		/*
170608a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
170708a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
170808a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
170908a07904SRalf Baechle 		 */
17101da177e4SLinus Torvalds 		case frsqrt_op:
17112d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
171208a07904SRalf Baechle 				return SIGILL;
171308a07904SRalf Baechle 
17141da177e4SLinus Torvalds 			handler.u = fpemu_sp_rsqrt;
17151da177e4SLinus Torvalds 			goto scopuop;
17163f7cac41SRalf Baechle 
17171da177e4SLinus Torvalds 		case frecip_op:
17182d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
171908a07904SRalf Baechle 				return SIGILL;
172008a07904SRalf Baechle 
17211da177e4SLinus Torvalds 			handler.u = fpemu_sp_recip;
17221da177e4SLinus Torvalds 			goto scopuop;
172308a07904SRalf Baechle 
17241da177e4SLinus Torvalds 		case fmovc_op:
172508a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
172608a07904SRalf Baechle 				return SIGILL;
172708a07904SRalf Baechle 
17281da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
17291da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
17301da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
17311da177e4SLinus Torvalds 				return 0;
17321da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17331da177e4SLinus Torvalds 			break;
17343f7cac41SRalf Baechle 
17351da177e4SLinus Torvalds 		case fmovz_op:
173608a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
173708a07904SRalf Baechle 				return SIGILL;
173808a07904SRalf Baechle 
17391da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
17401da177e4SLinus Torvalds 				return 0;
17411da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17421da177e4SLinus Torvalds 			break;
17433f7cac41SRalf Baechle 
17441da177e4SLinus Torvalds 		case fmovn_op:
174508a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
174608a07904SRalf Baechle 				return SIGILL;
174708a07904SRalf Baechle 
17481da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
17491da177e4SLinus Torvalds 				return 0;
17501da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17511da177e4SLinus Torvalds 			break;
17523f7cac41SRalf Baechle 
175367613f02SMarkos Chandras 		case fseleqz_op:
175467613f02SMarkos Chandras 			if (!cpu_has_mips_r6)
175567613f02SMarkos Chandras 				return SIGILL;
175667613f02SMarkos Chandras 
175767613f02SMarkos Chandras 			SPFROMREG(rv.s, MIPSInst_FT(ir));
175867613f02SMarkos Chandras 			if (rv.w & 0x1)
175967613f02SMarkos Chandras 				rv.w = 0;
176067613f02SMarkos Chandras 			else
176167613f02SMarkos Chandras 				SPFROMREG(rv.s, MIPSInst_FS(ir));
176267613f02SMarkos Chandras 			break;
176367613f02SMarkos Chandras 
1764130fe357SMarkos Chandras 		case fselnez_op:
1765130fe357SMarkos Chandras 			if (!cpu_has_mips_r6)
1766130fe357SMarkos Chandras 				return SIGILL;
1767130fe357SMarkos Chandras 
1768130fe357SMarkos Chandras 			SPFROMREG(rv.s, MIPSInst_FT(ir));
1769130fe357SMarkos Chandras 			if (rv.w & 0x1)
1770130fe357SMarkos Chandras 				SPFROMREG(rv.s, MIPSInst_FS(ir));
1771130fe357SMarkos Chandras 			else
1772130fe357SMarkos Chandras 				rv.w = 0;
1773130fe357SMarkos Chandras 			break;
1774130fe357SMarkos Chandras 
1775e24c3becSMarkos Chandras 		case fmaddf_op: {
1776e24c3becSMarkos Chandras 			union ieee754sp ft, fs, fd;
1777e24c3becSMarkos Chandras 
1778e24c3becSMarkos Chandras 			if (!cpu_has_mips_r6)
1779e24c3becSMarkos Chandras 				return SIGILL;
1780e24c3becSMarkos Chandras 
1781e24c3becSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1782e24c3becSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1783e24c3becSMarkos Chandras 			SPFROMREG(fd, MIPSInst_FD(ir));
1784e24c3becSMarkos Chandras 			rv.s = ieee754sp_maddf(fd, fs, ft);
1785e24c3becSMarkos Chandras 			break;
1786e24c3becSMarkos Chandras 		}
1787e24c3becSMarkos Chandras 
178883d43305SMarkos Chandras 		case fmsubf_op: {
178983d43305SMarkos Chandras 			union ieee754sp ft, fs, fd;
179083d43305SMarkos Chandras 
179183d43305SMarkos Chandras 			if (!cpu_has_mips_r6)
179283d43305SMarkos Chandras 				return SIGILL;
179383d43305SMarkos Chandras 
179483d43305SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
179583d43305SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
179683d43305SMarkos Chandras 			SPFROMREG(fd, MIPSInst_FD(ir));
179783d43305SMarkos Chandras 			rv.s = ieee754sp_msubf(fd, fs, ft);
179883d43305SMarkos Chandras 			break;
179983d43305SMarkos Chandras 		}
180083d43305SMarkos Chandras 
1801400bd2e4SMarkos Chandras 		case frint_op: {
1802400bd2e4SMarkos Chandras 			union ieee754sp fs;
1803400bd2e4SMarkos Chandras 
1804400bd2e4SMarkos Chandras 			if (!cpu_has_mips_r6)
1805400bd2e4SMarkos Chandras 				return SIGILL;
1806400bd2e4SMarkos Chandras 
1807400bd2e4SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1808400bd2e4SMarkos Chandras 			rv.l = ieee754sp_tlong(fs);
1809400bd2e4SMarkos Chandras 			rv.s = ieee754sp_flong(rv.l);
1810400bd2e4SMarkos Chandras 			goto copcsr;
1811400bd2e4SMarkos Chandras 		}
1812400bd2e4SMarkos Chandras 
181338db37baSMarkos Chandras 		case fclass_op: {
181438db37baSMarkos Chandras 			union ieee754sp fs;
181538db37baSMarkos Chandras 
181638db37baSMarkos Chandras 			if (!cpu_has_mips_r6)
181738db37baSMarkos Chandras 				return SIGILL;
181838db37baSMarkos Chandras 
181938db37baSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
182038db37baSMarkos Chandras 			rv.w = ieee754sp_2008class(fs);
182138db37baSMarkos Chandras 			rfmt = w_fmt;
182238db37baSMarkos Chandras 			break;
182338db37baSMarkos Chandras 		}
182438db37baSMarkos Chandras 
18254e9561b2SMarkos Chandras 		case fmin_op: {
18264e9561b2SMarkos Chandras 			union ieee754sp fs, ft;
18274e9561b2SMarkos Chandras 
18284e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
18294e9561b2SMarkos Chandras 				return SIGILL;
18304e9561b2SMarkos Chandras 
18314e9561b2SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
18324e9561b2SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18334e9561b2SMarkos Chandras 			rv.s = ieee754sp_fmin(fs, ft);
18344e9561b2SMarkos Chandras 			break;
18354e9561b2SMarkos Chandras 		}
18364e9561b2SMarkos Chandras 
18374e9561b2SMarkos Chandras 		case fmina_op: {
18384e9561b2SMarkos Chandras 			union ieee754sp fs, ft;
18394e9561b2SMarkos Chandras 
18404e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
18414e9561b2SMarkos Chandras 				return SIGILL;
18424e9561b2SMarkos Chandras 
18434e9561b2SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
18444e9561b2SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18454e9561b2SMarkos Chandras 			rv.s = ieee754sp_fmina(fs, ft);
18464e9561b2SMarkos Chandras 			break;
18474e9561b2SMarkos Chandras 		}
18484e9561b2SMarkos Chandras 
1849a79f5f9bSMarkos Chandras 		case fmax_op: {
1850a79f5f9bSMarkos Chandras 			union ieee754sp fs, ft;
1851a79f5f9bSMarkos Chandras 
1852a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
1853a79f5f9bSMarkos Chandras 				return SIGILL;
1854a79f5f9bSMarkos Chandras 
1855a79f5f9bSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1856a79f5f9bSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1857a79f5f9bSMarkos Chandras 			rv.s = ieee754sp_fmax(fs, ft);
1858a79f5f9bSMarkos Chandras 			break;
1859a79f5f9bSMarkos Chandras 		}
1860a79f5f9bSMarkos Chandras 
1861a79f5f9bSMarkos Chandras 		case fmaxa_op: {
1862a79f5f9bSMarkos Chandras 			union ieee754sp fs, ft;
1863a79f5f9bSMarkos Chandras 
1864a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
1865a79f5f9bSMarkos Chandras 				return SIGILL;
1866a79f5f9bSMarkos Chandras 
1867a79f5f9bSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1868a79f5f9bSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1869a79f5f9bSMarkos Chandras 			rv.s = ieee754sp_fmaxa(fs, ft);
1870a79f5f9bSMarkos Chandras 			break;
1871a79f5f9bSMarkos Chandras 		}
1872a79f5f9bSMarkos Chandras 
18731da177e4SLinus Torvalds 		case fabs_op:
18741da177e4SLinus Torvalds 			handler.u = ieee754sp_abs;
18751da177e4SLinus Torvalds 			goto scopuop;
18763f7cac41SRalf Baechle 
18771da177e4SLinus Torvalds 		case fneg_op:
18781da177e4SLinus Torvalds 			handler.u = ieee754sp_neg;
18791da177e4SLinus Torvalds 			goto scopuop;
18803f7cac41SRalf Baechle 
18811da177e4SLinus Torvalds 		case fmov_op:
18821da177e4SLinus Torvalds 			/* an easy one */
18831da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
18841da177e4SLinus Torvalds 			goto copcsr;
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds 			/* binary op on handler */
18871da177e4SLinus Torvalds scopbop:
18881da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
18891da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
18901da177e4SLinus Torvalds 
18911da177e4SLinus Torvalds 			rv.s = (*handler.b) (fs, ft);
18921da177e4SLinus Torvalds 			goto copcsr;
18931da177e4SLinus Torvalds scopuop:
18941da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
18951da177e4SLinus Torvalds 			rv.s = (*handler.u) (fs);
18961da177e4SLinus Torvalds 			goto copcsr;
18971da177e4SLinus Torvalds copcsr:
1898c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1899c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
19001da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1901c4103526SDeng-Cheng Zhu 			}
1902c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1903c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
19041da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1905c4103526SDeng-Cheng Zhu 			}
1906c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1907c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
19081da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1909c4103526SDeng-Cheng Zhu 			}
1910c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1911c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
19121da177e4SLinus Torvalds 				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1913c4103526SDeng-Cheng Zhu 			}
1914c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1915c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
19161da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1917c4103526SDeng-Cheng Zhu 			}
19181da177e4SLinus Torvalds 			break;
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 			/* unary conv ops */
19211da177e4SLinus Torvalds 		case fcvts_op:
19221da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
19231da177e4SLinus Torvalds 
19243f7cac41SRalf Baechle 		case fcvtd_op:
19251da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19261da177e4SLinus Torvalds 			rv.d = ieee754dp_fsp(fs);
19271da177e4SLinus Torvalds 			rfmt = d_fmt;
19281da177e4SLinus Torvalds 			goto copcsr;
19291da177e4SLinus Torvalds 
19303f7cac41SRalf Baechle 		case fcvtw_op:
19311da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19321da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
19331da177e4SLinus Torvalds 			rfmt = w_fmt;
19341da177e4SLinus Torvalds 			goto copcsr;
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds 		case fround_op:
19371da177e4SLinus Torvalds 		case ftrunc_op:
19381da177e4SLinus Torvalds 		case fceil_op:
19393f7cac41SRalf Baechle 		case ffloor_op:
19402d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
194108a07904SRalf Baechle 				return SIGILL;
194208a07904SRalf Baechle 
19433f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
19441da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19452cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
19461da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
19471da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
19481da177e4SLinus Torvalds 			rfmt = w_fmt;
19491da177e4SLinus Torvalds 			goto copcsr;
19501da177e4SLinus Torvalds 
19514b820d95SPaul Burton 		case fsel_op:
19524b820d95SPaul Burton 			if (!cpu_has_mips_r6)
19534b820d95SPaul Burton 				return SIGILL;
19544b820d95SPaul Burton 
19554b820d95SPaul Burton 			SPFROMREG(fd, MIPSInst_FD(ir));
19564b820d95SPaul Burton 			if (fd.bits & 0x1)
19574b820d95SPaul Burton 				SPFROMREG(rv.s, MIPSInst_FT(ir));
19584b820d95SPaul Burton 			else
19594b820d95SPaul Burton 				SPFROMREG(rv.s, MIPSInst_FS(ir));
19604b820d95SPaul Burton 			break;
19614b820d95SPaul Burton 
19623f7cac41SRalf Baechle 		case fcvtl_op:
19632d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
196408a07904SRalf Baechle 				return SIGILL;
196508a07904SRalf Baechle 
19661da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19671da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
19681da177e4SLinus Torvalds 			rfmt = l_fmt;
19691da177e4SLinus Torvalds 			goto copcsr;
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 		case froundl_op:
19721da177e4SLinus Torvalds 		case ftruncl_op:
19731da177e4SLinus Torvalds 		case fceill_op:
19743f7cac41SRalf Baechle 		case ffloorl_op:
19752d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
197608a07904SRalf Baechle 				return SIGILL;
197708a07904SRalf Baechle 
19783f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
19791da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19802cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
19811da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
19821da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
19831da177e4SLinus Torvalds 			rfmt = l_fmt;
19841da177e4SLinus Torvalds 			goto copcsr;
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds 		default:
1987f8c3c671SMarkos Chandras 			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
19881da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
19892209bcb1SRalf Baechle 				union ieee754sp fs, ft;
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
19921da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
19931da177e4SLinus Torvalds 				rv.w = ieee754sp_cmp(fs, ft,
19941da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
19951da177e4SLinus Torvalds 				rfmt = -1;
19961da177e4SLinus Torvalds 				if ((cmpop & 0x8) && ieee754_cxtest
19971da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
19981da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
19991da177e4SLinus Torvalds 				else
20001da177e4SLinus Torvalds 					goto copcsr;
20011da177e4SLinus Torvalds 
20023f7cac41SRalf Baechle 			} else
20031da177e4SLinus Torvalds 				return SIGILL;
20041da177e4SLinus Torvalds 			break;
20051da177e4SLinus Torvalds 		}
20061da177e4SLinus Torvalds 		break;
20071da177e4SLinus Torvalds 	}
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds 	case d_fmt: {
20104b820d95SPaul Burton 		union ieee754dp fd, fs, ft;
20111da177e4SLinus Torvalds 		union {
20122209bcb1SRalf Baechle 			union ieee754dp(*b) (union ieee754dp, union ieee754dp);
20132209bcb1SRalf Baechle 			union ieee754dp(*u) (union ieee754dp);
20141da177e4SLinus Torvalds 		} handler;
20151da177e4SLinus Torvalds 
20161da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
20171da177e4SLinus Torvalds 			/* binary ops */
20181da177e4SLinus Torvalds 		case fadd_op:
20191da177e4SLinus Torvalds 			handler.b = ieee754dp_add;
20201da177e4SLinus Torvalds 			goto dcopbop;
20211da177e4SLinus Torvalds 		case fsub_op:
20221da177e4SLinus Torvalds 			handler.b = ieee754dp_sub;
20231da177e4SLinus Torvalds 			goto dcopbop;
20241da177e4SLinus Torvalds 		case fmul_op:
20251da177e4SLinus Torvalds 			handler.b = ieee754dp_mul;
20261da177e4SLinus Torvalds 			goto dcopbop;
20271da177e4SLinus Torvalds 		case fdiv_op:
20281da177e4SLinus Torvalds 			handler.b = ieee754dp_div;
20291da177e4SLinus Torvalds 			goto dcopbop;
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds 			/* unary  ops */
20321da177e4SLinus Torvalds 		case fsqrt_op:
203308a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
203408a07904SRalf Baechle 				return SIGILL;
203508a07904SRalf Baechle 
20361da177e4SLinus Torvalds 			handler.u = ieee754dp_sqrt;
20371da177e4SLinus Torvalds 			goto dcopuop;
203808a07904SRalf Baechle 		/*
203908a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
204008a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
204108a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
204208a07904SRalf Baechle 		 */
20431da177e4SLinus Torvalds 		case frsqrt_op:
20442d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
204508a07904SRalf Baechle 				return SIGILL;
204608a07904SRalf Baechle 
20471da177e4SLinus Torvalds 			handler.u = fpemu_dp_rsqrt;
20481da177e4SLinus Torvalds 			goto dcopuop;
20491da177e4SLinus Torvalds 		case frecip_op:
20502d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
205108a07904SRalf Baechle 				return SIGILL;
205208a07904SRalf Baechle 
20531da177e4SLinus Torvalds 			handler.u = fpemu_dp_recip;
20541da177e4SLinus Torvalds 			goto dcopuop;
20551da177e4SLinus Torvalds 		case fmovc_op:
205608a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
205708a07904SRalf Baechle 				return SIGILL;
205808a07904SRalf Baechle 
20591da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
20601da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
20611da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
20621da177e4SLinus Torvalds 				return 0;
20631da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
20641da177e4SLinus Torvalds 			break;
20651da177e4SLinus Torvalds 		case fmovz_op:
206608a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
206708a07904SRalf Baechle 				return SIGILL;
206808a07904SRalf Baechle 
20691da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
20701da177e4SLinus Torvalds 				return 0;
20711da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
20721da177e4SLinus Torvalds 			break;
20731da177e4SLinus Torvalds 		case fmovn_op:
207408a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
207508a07904SRalf Baechle 				return SIGILL;
207608a07904SRalf Baechle 
20771da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
20781da177e4SLinus Torvalds 				return 0;
20791da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
20801da177e4SLinus Torvalds 			break;
208167613f02SMarkos Chandras 
208267613f02SMarkos Chandras 		case fseleqz_op:
208367613f02SMarkos Chandras 			if (!cpu_has_mips_r6)
208467613f02SMarkos Chandras 				return SIGILL;
208567613f02SMarkos Chandras 
208667613f02SMarkos Chandras 			DPFROMREG(rv.d, MIPSInst_FT(ir));
208767613f02SMarkos Chandras 			if (rv.l & 0x1)
208867613f02SMarkos Chandras 				rv.l = 0;
208967613f02SMarkos Chandras 			else
209067613f02SMarkos Chandras 				DPFROMREG(rv.d, MIPSInst_FS(ir));
209167613f02SMarkos Chandras 			break;
209267613f02SMarkos Chandras 
2093130fe357SMarkos Chandras 		case fselnez_op:
2094130fe357SMarkos Chandras 			if (!cpu_has_mips_r6)
2095130fe357SMarkos Chandras 				return SIGILL;
2096130fe357SMarkos Chandras 
2097130fe357SMarkos Chandras 			DPFROMREG(rv.d, MIPSInst_FT(ir));
2098130fe357SMarkos Chandras 			if (rv.l & 0x1)
2099130fe357SMarkos Chandras 				DPFROMREG(rv.d, MIPSInst_FS(ir));
2100130fe357SMarkos Chandras 			else
2101130fe357SMarkos Chandras 				rv.l = 0;
2102130fe357SMarkos Chandras 			break;
2103130fe357SMarkos Chandras 
2104e24c3becSMarkos Chandras 		case fmaddf_op: {
2105e24c3becSMarkos Chandras 			union ieee754dp ft, fs, fd;
2106e24c3becSMarkos Chandras 
2107e24c3becSMarkos Chandras 			if (!cpu_has_mips_r6)
2108e24c3becSMarkos Chandras 				return SIGILL;
2109e24c3becSMarkos Chandras 
2110e24c3becSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2111e24c3becSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2112e24c3becSMarkos Chandras 			DPFROMREG(fd, MIPSInst_FD(ir));
2113e24c3becSMarkos Chandras 			rv.d = ieee754dp_maddf(fd, fs, ft);
2114e24c3becSMarkos Chandras 			break;
2115e24c3becSMarkos Chandras 		}
2116e24c3becSMarkos Chandras 
211783d43305SMarkos Chandras 		case fmsubf_op: {
211883d43305SMarkos Chandras 			union ieee754dp ft, fs, fd;
211983d43305SMarkos Chandras 
212083d43305SMarkos Chandras 			if (!cpu_has_mips_r6)
212183d43305SMarkos Chandras 				return SIGILL;
212283d43305SMarkos Chandras 
212383d43305SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
212483d43305SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
212583d43305SMarkos Chandras 			DPFROMREG(fd, MIPSInst_FD(ir));
212683d43305SMarkos Chandras 			rv.d = ieee754dp_msubf(fd, fs, ft);
212783d43305SMarkos Chandras 			break;
212883d43305SMarkos Chandras 		}
212983d43305SMarkos Chandras 
2130400bd2e4SMarkos Chandras 		case frint_op: {
2131400bd2e4SMarkos Chandras 			union ieee754dp fs;
2132400bd2e4SMarkos Chandras 
2133400bd2e4SMarkos Chandras 			if (!cpu_has_mips_r6)
2134400bd2e4SMarkos Chandras 				return SIGILL;
2135400bd2e4SMarkos Chandras 
2136400bd2e4SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2137400bd2e4SMarkos Chandras 			rv.l = ieee754dp_tlong(fs);
2138400bd2e4SMarkos Chandras 			rv.d = ieee754dp_flong(rv.l);
2139400bd2e4SMarkos Chandras 			goto copcsr;
2140400bd2e4SMarkos Chandras 		}
2141400bd2e4SMarkos Chandras 
214238db37baSMarkos Chandras 		case fclass_op: {
214338db37baSMarkos Chandras 			union ieee754dp fs;
214438db37baSMarkos Chandras 
214538db37baSMarkos Chandras 			if (!cpu_has_mips_r6)
214638db37baSMarkos Chandras 				return SIGILL;
214738db37baSMarkos Chandras 
214838db37baSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
214938db37baSMarkos Chandras 			rv.w = ieee754dp_2008class(fs);
215038db37baSMarkos Chandras 			rfmt = w_fmt;
215138db37baSMarkos Chandras 			break;
215238db37baSMarkos Chandras 		}
215338db37baSMarkos Chandras 
21544e9561b2SMarkos Chandras 		case fmin_op: {
21554e9561b2SMarkos Chandras 			union ieee754dp fs, ft;
21564e9561b2SMarkos Chandras 
21574e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
21584e9561b2SMarkos Chandras 				return SIGILL;
21594e9561b2SMarkos Chandras 
21604e9561b2SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
21614e9561b2SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
21624e9561b2SMarkos Chandras 			rv.d = ieee754dp_fmin(fs, ft);
21634e9561b2SMarkos Chandras 			break;
21644e9561b2SMarkos Chandras 		}
21654e9561b2SMarkos Chandras 
21664e9561b2SMarkos Chandras 		case fmina_op: {
21674e9561b2SMarkos Chandras 			union ieee754dp fs, ft;
21684e9561b2SMarkos Chandras 
21694e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
21704e9561b2SMarkos Chandras 				return SIGILL;
21714e9561b2SMarkos Chandras 
21724e9561b2SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
21734e9561b2SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
21744e9561b2SMarkos Chandras 			rv.d = ieee754dp_fmina(fs, ft);
21754e9561b2SMarkos Chandras 			break;
21764e9561b2SMarkos Chandras 		}
21774e9561b2SMarkos Chandras 
2178a79f5f9bSMarkos Chandras 		case fmax_op: {
2179a79f5f9bSMarkos Chandras 			union ieee754dp fs, ft;
2180a79f5f9bSMarkos Chandras 
2181a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
2182a79f5f9bSMarkos Chandras 				return SIGILL;
2183a79f5f9bSMarkos Chandras 
2184a79f5f9bSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2185a79f5f9bSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2186a79f5f9bSMarkos Chandras 			rv.d = ieee754dp_fmax(fs, ft);
2187a79f5f9bSMarkos Chandras 			break;
2188a79f5f9bSMarkos Chandras 		}
2189a79f5f9bSMarkos Chandras 
2190a79f5f9bSMarkos Chandras 		case fmaxa_op: {
2191a79f5f9bSMarkos Chandras 			union ieee754dp fs, ft;
2192a79f5f9bSMarkos Chandras 
2193a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
2194a79f5f9bSMarkos Chandras 				return SIGILL;
2195a79f5f9bSMarkos Chandras 
2196a79f5f9bSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2197a79f5f9bSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2198a79f5f9bSMarkos Chandras 			rv.d = ieee754dp_fmaxa(fs, ft);
2199a79f5f9bSMarkos Chandras 			break;
2200a79f5f9bSMarkos Chandras 		}
2201a79f5f9bSMarkos Chandras 
22021da177e4SLinus Torvalds 		case fabs_op:
22031da177e4SLinus Torvalds 			handler.u = ieee754dp_abs;
22041da177e4SLinus Torvalds 			goto dcopuop;
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 		case fneg_op:
22071da177e4SLinus Torvalds 			handler.u = ieee754dp_neg;
22081da177e4SLinus Torvalds 			goto dcopuop;
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 		case fmov_op:
22111da177e4SLinus Torvalds 			/* an easy one */
22121da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
22131da177e4SLinus Torvalds 			goto copcsr;
22141da177e4SLinus Torvalds 
22151da177e4SLinus Torvalds 			/* binary op on handler */
22163f7cac41SRalf Baechle dcopbop:
22171da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22181da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds 			rv.d = (*handler.b) (fs, ft);
22211da177e4SLinus Torvalds 			goto copcsr;
22223f7cac41SRalf Baechle dcopuop:
22231da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22241da177e4SLinus Torvalds 			rv.d = (*handler.u) (fs);
22251da177e4SLinus Torvalds 			goto copcsr;
22261da177e4SLinus Torvalds 
22273f7cac41SRalf Baechle 		/*
22283f7cac41SRalf Baechle 		 * unary conv ops
22293f7cac41SRalf Baechle 		 */
22303f7cac41SRalf Baechle 		case fcvts_op:
22311da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22321da177e4SLinus Torvalds 			rv.s = ieee754sp_fdp(fs);
22331da177e4SLinus Torvalds 			rfmt = s_fmt;
22341da177e4SLinus Torvalds 			goto copcsr;
22353f7cac41SRalf Baechle 
22361da177e4SLinus Torvalds 		case fcvtd_op:
22371da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
22381da177e4SLinus Torvalds 
22393f7cac41SRalf Baechle 		case fcvtw_op:
22401da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22411da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);	/* wrong */
22421da177e4SLinus Torvalds 			rfmt = w_fmt;
22431da177e4SLinus Torvalds 			goto copcsr;
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds 		case fround_op:
22461da177e4SLinus Torvalds 		case ftrunc_op:
22471da177e4SLinus Torvalds 		case fceil_op:
22483f7cac41SRalf Baechle 		case ffloor_op:
224908a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
225008a07904SRalf Baechle 				return SIGILL;
225108a07904SRalf Baechle 
22523f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
22531da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22542cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
22551da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);
22561da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
22571da177e4SLinus Torvalds 			rfmt = w_fmt;
22581da177e4SLinus Torvalds 			goto copcsr;
22591da177e4SLinus Torvalds 
22604b820d95SPaul Burton 		case fsel_op:
22614b820d95SPaul Burton 			if (!cpu_has_mips_r6)
22624b820d95SPaul Burton 				return SIGILL;
22634b820d95SPaul Burton 
22644b820d95SPaul Burton 			DPFROMREG(fd, MIPSInst_FD(ir));
22654b820d95SPaul Burton 			if (fd.bits & 0x1)
22664b820d95SPaul Burton 				DPFROMREG(rv.d, MIPSInst_FT(ir));
22674b820d95SPaul Burton 			else
22684b820d95SPaul Burton 				DPFROMREG(rv.d, MIPSInst_FS(ir));
22694b820d95SPaul Burton 			break;
22704b820d95SPaul Burton 
22713f7cac41SRalf Baechle 		case fcvtl_op:
22722d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
227308a07904SRalf Baechle 				return SIGILL;
227408a07904SRalf Baechle 
22751da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22761da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
22771da177e4SLinus Torvalds 			rfmt = l_fmt;
22781da177e4SLinus Torvalds 			goto copcsr;
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds 		case froundl_op:
22811da177e4SLinus Torvalds 		case ftruncl_op:
22821da177e4SLinus Torvalds 		case fceill_op:
22833f7cac41SRalf Baechle 		case ffloorl_op:
22842d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
228508a07904SRalf Baechle 				return SIGILL;
228608a07904SRalf Baechle 
22873f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
22881da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22892cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
22901da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
22911da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
22921da177e4SLinus Torvalds 			rfmt = l_fmt;
22931da177e4SLinus Torvalds 			goto copcsr;
22941da177e4SLinus Torvalds 
22951da177e4SLinus Torvalds 		default:
2296f8c3c671SMarkos Chandras 			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
22971da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
22982209bcb1SRalf Baechle 				union ieee754dp fs, ft;
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
23011da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
23021da177e4SLinus Torvalds 				rv.w = ieee754dp_cmp(fs, ft,
23031da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
23041da177e4SLinus Torvalds 				rfmt = -1;
23051da177e4SLinus Torvalds 				if ((cmpop & 0x8)
23061da177e4SLinus Torvalds 					&&
23071da177e4SLinus Torvalds 					ieee754_cxtest
23081da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
23091da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
23101da177e4SLinus Torvalds 				else
23111da177e4SLinus Torvalds 					goto copcsr;
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds 			}
23141da177e4SLinus Torvalds 			else {
23151da177e4SLinus Torvalds 				return SIGILL;
23161da177e4SLinus Torvalds 			}
23171da177e4SLinus Torvalds 			break;
23181da177e4SLinus Torvalds 		}
23191da177e4SLinus Torvalds 		break;
2320bbdd8147SMarkos Chandras 	}
23211da177e4SLinus Torvalds 
2322bbdd8147SMarkos Chandras 	case w_fmt: {
2323bbdd8147SMarkos Chandras 		union ieee754dp fs;
2324bbdd8147SMarkos Chandras 
23251da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
23261da177e4SLinus Torvalds 		case fcvts_op:
23271da177e4SLinus Torvalds 			/* convert word to single precision real */
23281da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
23291da177e4SLinus Torvalds 			rv.s = ieee754sp_fint(fs.bits);
23301da177e4SLinus Torvalds 			rfmt = s_fmt;
23311da177e4SLinus Torvalds 			goto copcsr;
23321da177e4SLinus Torvalds 		case fcvtd_op:
23331da177e4SLinus Torvalds 			/* convert word to double precision real */
23341da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
23351da177e4SLinus Torvalds 			rv.d = ieee754dp_fint(fs.bits);
23361da177e4SLinus Torvalds 			rfmt = d_fmt;
23371da177e4SLinus Torvalds 			goto copcsr;
2338f8c3c671SMarkos Chandras 		default: {
2339f8c3c671SMarkos Chandras 			/* Emulating the new CMP.condn.fmt R6 instruction */
2340f8c3c671SMarkos Chandras #define CMPOP_MASK	0x7
2341f8c3c671SMarkos Chandras #define SIGN_BIT	(0x1 << 3)
2342f8c3c671SMarkos Chandras #define PREDICATE_BIT	(0x1 << 4)
2343f8c3c671SMarkos Chandras 
2344f8c3c671SMarkos Chandras 			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2345f8c3c671SMarkos Chandras 			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2346f8c3c671SMarkos Chandras 			union ieee754sp fs, ft;
2347f8c3c671SMarkos Chandras 
2348f8c3c671SMarkos Chandras 			/* This is an R6 only instruction */
2349f8c3c671SMarkos Chandras 			if (!cpu_has_mips_r6 ||
2350f8c3c671SMarkos Chandras 			    (MIPSInst_FUNC(ir) & 0x20))
2351f8c3c671SMarkos Chandras 				return SIGILL;
2352f8c3c671SMarkos Chandras 
2353f8c3c671SMarkos Chandras 			/* fmt is w_fmt for single precision so fix it */
2354f8c3c671SMarkos Chandras 			rfmt = s_fmt;
2355f8c3c671SMarkos Chandras 			/* default to false */
2356f8c3c671SMarkos Chandras 			rv.w = 0;
2357f8c3c671SMarkos Chandras 
2358f8c3c671SMarkos Chandras 			/* CMP.condn.S */
2359f8c3c671SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
2360f8c3c671SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
2361f8c3c671SMarkos Chandras 
2362f8c3c671SMarkos Chandras 			/* positive predicates */
2363f8c3c671SMarkos Chandras 			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2364f8c3c671SMarkos Chandras 				if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
2365f8c3c671SMarkos Chandras 						  sig))
2366f8c3c671SMarkos Chandras 				    rv.w = -1; /* true, all 1s */
2367f8c3c671SMarkos Chandras 				if ((sig) &&
2368f8c3c671SMarkos Chandras 				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2369f8c3c671SMarkos Chandras 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2370f8c3c671SMarkos Chandras 				else
2371f8c3c671SMarkos Chandras 					goto copcsr;
2372f8c3c671SMarkos Chandras 			} else {
2373f8c3c671SMarkos Chandras 				/* negative predicates */
2374f8c3c671SMarkos Chandras 				switch (cmpop) {
2375f8c3c671SMarkos Chandras 				case 1:
2376f8c3c671SMarkos Chandras 				case 2:
2377f8c3c671SMarkos Chandras 				case 3:
2378f8c3c671SMarkos Chandras 					if (ieee754sp_cmp(fs, ft,
2379f8c3c671SMarkos Chandras 							  negative_cmptab[cmpop],
2380f8c3c671SMarkos Chandras 							  sig))
2381f8c3c671SMarkos Chandras 						rv.w = -1; /* true, all 1s */
2382f8c3c671SMarkos Chandras 					if (sig &&
2383f8c3c671SMarkos Chandras 					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2384f8c3c671SMarkos Chandras 						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2385f8c3c671SMarkos Chandras 					else
2386f8c3c671SMarkos Chandras 						goto copcsr;
2387f8c3c671SMarkos Chandras 					break;
23881da177e4SLinus Torvalds 				default:
2389f8c3c671SMarkos Chandras 					/* Reserved R6 ops */
2390f8c3c671SMarkos Chandras 					pr_err("Reserved MIPS R6 CMP.condn.S operation\n");
23911da177e4SLinus Torvalds 					return SIGILL;
23921da177e4SLinus Torvalds 				}
2393f8c3c671SMarkos Chandras 			}
23941da177e4SLinus Torvalds 			break;
23951da177e4SLinus Torvalds 			}
2396f8c3c671SMarkos Chandras 		}
2397f8c3c671SMarkos Chandras 	}
23981da177e4SLinus Torvalds 
23993f7cac41SRalf Baechle 	case l_fmt:
240008a07904SRalf Baechle 
24012d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
240208a07904SRalf Baechle 			return SIGILL;
240308a07904SRalf Baechle 
2404bbd426f5SPaul Burton 		DIFROMREG(bits, MIPSInst_FS(ir));
2405bbd426f5SPaul Burton 
24061da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
24071da177e4SLinus Torvalds 		case fcvts_op:
24081da177e4SLinus Torvalds 			/* convert long to single precision real */
2409bbd426f5SPaul Burton 			rv.s = ieee754sp_flong(bits);
24101da177e4SLinus Torvalds 			rfmt = s_fmt;
24111da177e4SLinus Torvalds 			goto copcsr;
24121da177e4SLinus Torvalds 		case fcvtd_op:
24131da177e4SLinus Torvalds 			/* convert long to double precision real */
2414bbd426f5SPaul Burton 			rv.d = ieee754dp_flong(bits);
24151da177e4SLinus Torvalds 			rfmt = d_fmt;
24161da177e4SLinus Torvalds 			goto copcsr;
2417f8c3c671SMarkos Chandras 		default: {
2418f8c3c671SMarkos Chandras 			/* Emulating the new CMP.condn.fmt R6 instruction */
2419f8c3c671SMarkos Chandras 			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2420f8c3c671SMarkos Chandras 			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2421f8c3c671SMarkos Chandras 			union ieee754dp fs, ft;
2422f8c3c671SMarkos Chandras 
2423f8c3c671SMarkos Chandras 			if (!cpu_has_mips_r6 ||
2424f8c3c671SMarkos Chandras 			    (MIPSInst_FUNC(ir) & 0x20))
2425f8c3c671SMarkos Chandras 				return SIGILL;
2426f8c3c671SMarkos Chandras 
2427f8c3c671SMarkos Chandras 			/* fmt is l_fmt for double precision so fix it */
2428f8c3c671SMarkos Chandras 			rfmt = d_fmt;
2429f8c3c671SMarkos Chandras 			/* default to false */
2430f8c3c671SMarkos Chandras 			rv.l = 0;
2431f8c3c671SMarkos Chandras 
2432f8c3c671SMarkos Chandras 			/* CMP.condn.D */
2433f8c3c671SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2434f8c3c671SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2435f8c3c671SMarkos Chandras 
2436f8c3c671SMarkos Chandras 			/* positive predicates */
2437f8c3c671SMarkos Chandras 			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2438f8c3c671SMarkos Chandras 				if (ieee754dp_cmp(fs, ft,
2439f8c3c671SMarkos Chandras 						  cmptab[cmpop], sig))
2440f8c3c671SMarkos Chandras 				    rv.l = -1LL; /* true, all 1s */
2441f8c3c671SMarkos Chandras 				if (sig &&
2442f8c3c671SMarkos Chandras 				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2443f8c3c671SMarkos Chandras 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2444f8c3c671SMarkos Chandras 				else
2445f8c3c671SMarkos Chandras 					goto copcsr;
2446f8c3c671SMarkos Chandras 			} else {
2447f8c3c671SMarkos Chandras 				/* negative predicates */
2448f8c3c671SMarkos Chandras 				switch (cmpop) {
2449f8c3c671SMarkos Chandras 				case 1:
2450f8c3c671SMarkos Chandras 				case 2:
2451f8c3c671SMarkos Chandras 				case 3:
2452f8c3c671SMarkos Chandras 					if (ieee754dp_cmp(fs, ft,
2453f8c3c671SMarkos Chandras 							  negative_cmptab[cmpop],
2454f8c3c671SMarkos Chandras 							  sig))
2455f8c3c671SMarkos Chandras 						rv.l = -1LL; /* true, all 1s */
2456f8c3c671SMarkos Chandras 					if (sig &&
2457f8c3c671SMarkos Chandras 					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2458f8c3c671SMarkos Chandras 						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2459f8c3c671SMarkos Chandras 					else
2460f8c3c671SMarkos Chandras 						goto copcsr;
2461f8c3c671SMarkos Chandras 					break;
24621da177e4SLinus Torvalds 				default:
2463f8c3c671SMarkos Chandras 					/* Reserved R6 ops */
2464f8c3c671SMarkos Chandras 					pr_err("Reserved MIPS R6 CMP.condn.D operation\n");
24651da177e4SLinus Torvalds 					return SIGILL;
24661da177e4SLinus Torvalds 				}
2467f8c3c671SMarkos Chandras 			}
24681da177e4SLinus Torvalds 			break;
2469f8c3c671SMarkos Chandras 			}
2470f8c3c671SMarkos Chandras 		}
24711da177e4SLinus Torvalds 	default:
24721da177e4SLinus Torvalds 		return SIGILL;
24731da177e4SLinus Torvalds 	}
24741da177e4SLinus Torvalds 
24751da177e4SLinus Torvalds 	/*
24761da177e4SLinus Torvalds 	 * Update the fpu CSR register for this operation.
24771da177e4SLinus Torvalds 	 * If an exception is required, generate a tidy SIGFPE exception,
24781da177e4SLinus Torvalds 	 * without updating the result register.
24791da177e4SLinus Torvalds 	 * Note: cause exception bits do not accumulate, they are rewritten
24801da177e4SLinus Torvalds 	 * for each op; only the flag/sticky bits accumulate.
24811da177e4SLinus Torvalds 	 */
24821da177e4SLinus Torvalds 	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
24831da177e4SLinus Torvalds 	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
24843f7cac41SRalf Baechle 		/*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
24851da177e4SLinus Torvalds 		return SIGFPE;
24861da177e4SLinus Torvalds 	}
24871da177e4SLinus Torvalds 
24881da177e4SLinus Torvalds 	/*
24891da177e4SLinus Torvalds 	 * Now we can safely write the result back to the register file.
24901da177e4SLinus Torvalds 	 */
24911da177e4SLinus Torvalds 	switch (rfmt) {
249208a07904SRalf Baechle 	case -1:
249308a07904SRalf Baechle 
249408a07904SRalf Baechle 		if (cpu_has_mips_4_5_r)
2495c3b9b945SRob Kendrick 			cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
24961da177e4SLinus Torvalds 		else
249708a07904SRalf Baechle 			cbit = FPU_CSR_COND;
249808a07904SRalf Baechle 		if (rv.w)
249908a07904SRalf Baechle 			ctx->fcr31 |= cbit;
250008a07904SRalf Baechle 		else
250108a07904SRalf Baechle 			ctx->fcr31 &= ~cbit;
25021da177e4SLinus Torvalds 		break;
250308a07904SRalf Baechle 
25041da177e4SLinus Torvalds 	case d_fmt:
25051da177e4SLinus Torvalds 		DPTOREG(rv.d, MIPSInst_FD(ir));
25061da177e4SLinus Torvalds 		break;
25071da177e4SLinus Torvalds 	case s_fmt:
25081da177e4SLinus Torvalds 		SPTOREG(rv.s, MIPSInst_FD(ir));
25091da177e4SLinus Torvalds 		break;
25101da177e4SLinus Torvalds 	case w_fmt:
25111da177e4SLinus Torvalds 		SITOREG(rv.w, MIPSInst_FD(ir));
25121da177e4SLinus Torvalds 		break;
25131da177e4SLinus Torvalds 	case l_fmt:
25142d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
251508a07904SRalf Baechle 			return SIGILL;
251608a07904SRalf Baechle 
25171da177e4SLinus Torvalds 		DITOREG(rv.l, MIPSInst_FD(ir));
25181da177e4SLinus Torvalds 		break;
25191da177e4SLinus Torvalds 	default:
25201da177e4SLinus Torvalds 		return SIGILL;
25211da177e4SLinus Torvalds 	}
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 	return 0;
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds 
2526e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2527515b029dSDavid Daney 	int has_fpu, void *__user *fault_addr)
25281da177e4SLinus Torvalds {
2529333d1f67SRalf Baechle 	unsigned long oldepc, prevepc;
2530102cedc3SLeonid Yegoshin 	struct mm_decoded_insn dec_insn;
2531102cedc3SLeonid Yegoshin 	u16 instr[4];
2532102cedc3SLeonid Yegoshin 	u16 *instr_ptr;
25331da177e4SLinus Torvalds 	int sig = 0;
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 	oldepc = xcp->cp0_epc;
25361da177e4SLinus Torvalds 	do {
25371da177e4SLinus Torvalds 		prevepc = xcp->cp0_epc;
25381da177e4SLinus Torvalds 
2539102cedc3SLeonid Yegoshin 		if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2540102cedc3SLeonid Yegoshin 			/*
2541102cedc3SLeonid Yegoshin 			 * Get next 2 microMIPS instructions and convert them
2542102cedc3SLeonid Yegoshin 			 * into 32-bit instructions.
2543102cedc3SLeonid Yegoshin 			 */
2544102cedc3SLeonid Yegoshin 			if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2545102cedc3SLeonid Yegoshin 			    (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2546102cedc3SLeonid Yegoshin 			    (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2547102cedc3SLeonid Yegoshin 			    (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2548b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
25491da177e4SLinus Torvalds 				return SIGBUS;
25501da177e4SLinus Torvalds 			}
2551102cedc3SLeonid Yegoshin 			instr_ptr = instr;
2552102cedc3SLeonid Yegoshin 
2553102cedc3SLeonid Yegoshin 			/* Get first instruction. */
2554102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2555102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2556102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2557102cedc3SLeonid Yegoshin 					(*instr_ptr);
2558102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2559102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 2;
2560102cedc3SLeonid Yegoshin 				instr_ptr += 1;
2561102cedc3SLeonid Yegoshin 			} else {
2562102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2563102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2564102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2565102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 4;
2566102cedc3SLeonid Yegoshin 				instr_ptr += 2;
2567515b029dSDavid Daney 			}
2568102cedc3SLeonid Yegoshin 			/* Get second instruction. */
2569102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2570102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2571102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2572102cedc3SLeonid Yegoshin 					(*instr_ptr);
2573102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2574102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 2;
2575102cedc3SLeonid Yegoshin 			} else {
2576102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2577102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2578102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2579102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 4;
2580102cedc3SLeonid Yegoshin 			}
2581102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 1;
2582102cedc3SLeonid Yegoshin 		} else {
2583102cedc3SLeonid Yegoshin 			if ((get_user(dec_insn.insn,
2584102cedc3SLeonid Yegoshin 			    (mips_instruction __user *) xcp->cp0_epc)) ||
2585102cedc3SLeonid Yegoshin 			    (get_user(dec_insn.next_insn,
2586102cedc3SLeonid Yegoshin 			    (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2587102cedc3SLeonid Yegoshin 				MIPS_FPU_EMU_INC_STATS(errors);
2588102cedc3SLeonid Yegoshin 				return SIGBUS;
2589102cedc3SLeonid Yegoshin 			}
2590102cedc3SLeonid Yegoshin 			dec_insn.pc_inc = 4;
2591102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc = 4;
2592102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 0;
2593102cedc3SLeonid Yegoshin 		}
2594102cedc3SLeonid Yegoshin 
2595102cedc3SLeonid Yegoshin 		if ((dec_insn.insn == 0) ||
2596102cedc3SLeonid Yegoshin 		   ((dec_insn.pc_inc == 2) &&
2597102cedc3SLeonid Yegoshin 		   ((dec_insn.insn & 0xffff) == MM_NOP16)))
2598102cedc3SLeonid Yegoshin 			xcp->cp0_epc += dec_insn.pc_inc;	/* Skip NOPs */
25991da177e4SLinus Torvalds 		else {
2600cd21dfcfSRalf Baechle 			/*
26012cfcf8a8SMaciej W. Rozycki 			 * The 'ieee754_csr' is an alias of ctx->fcr31.
26022cfcf8a8SMaciej W. Rozycki 			 * No need to copy ctx->fcr31 to ieee754_csr.
2603cd21dfcfSRalf Baechle 			 */
2604102cedc3SLeonid Yegoshin 			sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
26051da177e4SLinus Torvalds 		}
26061da177e4SLinus Torvalds 
2607e04582b7SAtsushi Nemoto 		if (has_fpu)
26081da177e4SLinus Torvalds 			break;
26091da177e4SLinus Torvalds 		if (sig)
26101da177e4SLinus Torvalds 			break;
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds 		cond_resched();
26131da177e4SLinus Torvalds 	} while (xcp->cp0_epc > prevepc);
26141da177e4SLinus Torvalds 
26151da177e4SLinus Torvalds 	/* SIGILL indicates a non-fpu instruction */
26161da177e4SLinus Torvalds 	if (sig == SIGILL && xcp->cp0_epc != oldepc)
26173f7cac41SRalf Baechle 		/* but if EPC has advanced, then ignore it */
26181da177e4SLinus Torvalds 		sig = 0;
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds 	return sig;
26211da177e4SLinus Torvalds }
2622