xref: /linux/arch/mips/math-emu/cp1emu.c (revision 93583e178ebfdd2fadf950eef1547f305cac12ca)
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:
448102cedc3SLeonid Yegoshin 			regs->regs[insn.r_format.rd] =
449102cedc3SLeonid Yegoshin 				regs->cp0_epc + dec_insn.pc_inc +
450102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
451102cedc3SLeonid Yegoshin 			/* Fall through */
4521da177e4SLinus Torvalds 		case jr_op:
4535f9f41c4SMarkos Chandras 			/* For R6, JR already emulated in jalr_op */
454143fefc8SMarkos Chandras 			if (NO_R6EMU && insn.r_format.func == jr_op)
4555f9f41c4SMarkos Chandras 				break;
456102cedc3SLeonid Yegoshin 			*contpc = regs->regs[insn.r_format.rs];
4571da177e4SLinus Torvalds 			return 1;
4581da177e4SLinus Torvalds 		}
4591da177e4SLinus Torvalds 		break;
4601da177e4SLinus Torvalds 	case bcond_op:
461102cedc3SLeonid Yegoshin 		switch (insn.i_format.rt) {
4621da177e4SLinus Torvalds 		case bltzal_op:
4631da177e4SLinus Torvalds 		case bltzall_op:
464319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
465319824eaSMarkos Chandras 			    insn.i_format.rt == bltzall_op))
466319824eaSMarkos Chandras 				break;
467319824eaSMarkos Chandras 
468102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
469102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
470102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
471102cedc3SLeonid Yegoshin 			/* Fall through */
472102cedc3SLeonid Yegoshin 		case bltzl_op:
473319824eaSMarkos Chandras 			if (NO_R6EMU)
474319824eaSMarkos Chandras 				break;
475319824eaSMarkos Chandras 		case bltz_op:
476102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] < 0)
477102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
478102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
479102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
480102cedc3SLeonid Yegoshin 			else
481102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
482102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
483102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
4841da177e4SLinus Torvalds 			return 1;
485102cedc3SLeonid Yegoshin 		case bgezal_op:
486102cedc3SLeonid Yegoshin 		case bgezall_op:
487319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
488319824eaSMarkos Chandras 			    insn.i_format.rt == bgezall_op))
489319824eaSMarkos Chandras 				break;
490319824eaSMarkos Chandras 
491102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
492102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
493102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
494102cedc3SLeonid Yegoshin 			/* Fall through */
495102cedc3SLeonid Yegoshin 		case bgezl_op:
496319824eaSMarkos Chandras 			if (NO_R6EMU)
497319824eaSMarkos Chandras 				break;
498319824eaSMarkos Chandras 		case bgez_op:
499102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] >= 0)
500102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
501102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
502102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
503102cedc3SLeonid Yegoshin 			else
504102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
505102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
506102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
507102cedc3SLeonid Yegoshin 			return 1;
5081da177e4SLinus Torvalds 		}
5091da177e4SLinus Torvalds 		break;
5101da177e4SLinus Torvalds 	case jalx_op:
511102cedc3SLeonid Yegoshin 		set_isa16_mode(bit);
512102cedc3SLeonid Yegoshin 	case jal_op:
513102cedc3SLeonid Yegoshin 		regs->regs[31] = regs->cp0_epc +
514102cedc3SLeonid Yegoshin 			dec_insn.pc_inc +
515102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc;
516102cedc3SLeonid Yegoshin 		/* Fall through */
517102cedc3SLeonid Yegoshin 	case j_op:
518102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
519102cedc3SLeonid Yegoshin 		*contpc >>= 28;
520102cedc3SLeonid Yegoshin 		*contpc <<= 28;
521102cedc3SLeonid Yegoshin 		*contpc |= (insn.j_format.target << 2);
522102cedc3SLeonid Yegoshin 		/* Set microMIPS mode bit: XOR for jalx. */
523102cedc3SLeonid Yegoshin 		*contpc ^= bit;
5241da177e4SLinus Torvalds 		return 1;
525102cedc3SLeonid Yegoshin 	case beql_op:
526319824eaSMarkos Chandras 		if (NO_R6EMU)
527319824eaSMarkos Chandras 			break;
528319824eaSMarkos Chandras 	case beq_op:
529102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] ==
530102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
531102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
532102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
533102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
534102cedc3SLeonid Yegoshin 		else
535102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
536102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
537102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
538102cedc3SLeonid Yegoshin 		return 1;
539102cedc3SLeonid Yegoshin 	case bnel_op:
540319824eaSMarkos Chandras 		if (NO_R6EMU)
541319824eaSMarkos Chandras 			break;
542319824eaSMarkos Chandras 	case bne_op:
543102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] !=
544102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
545102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
546102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
547102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
548102cedc3SLeonid Yegoshin 		else
549102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
550102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
551102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
552102cedc3SLeonid Yegoshin 		return 1;
553102cedc3SLeonid Yegoshin 	case blezl_op:
554e9d92d22SMarkos Chandras 		if (!insn.i_format.rt && NO_R6EMU)
555319824eaSMarkos Chandras 			break;
556319824eaSMarkos Chandras 	case blez_op:
557a8ff66f5SMarkos Chandras 
558a8ff66f5SMarkos Chandras 		/*
559a8ff66f5SMarkos Chandras 		 * Compact branches for R6 for the
560a8ff66f5SMarkos Chandras 		 * blez and blezl opcodes.
561a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = 0 | rt != 0  == BLEZALC
562a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = rt != 0      == BGEZALC
563a8ff66f5SMarkos Chandras 		 * BLEZ  | rs != 0 | rt != 0 == BGEUC
564a8ff66f5SMarkos Chandras 		 * BLEZL | rs = 0 | rt != 0  == BLEZC
565a8ff66f5SMarkos Chandras 		 * BLEZL | rs = rt != 0      == BGEZC
566a8ff66f5SMarkos Chandras 		 * BLEZL | rs != 0 | rt != 0 == BGEC
567a8ff66f5SMarkos Chandras 		 *
568a8ff66f5SMarkos Chandras 		 * For real BLEZ{,L}, rt is always 0.
569a8ff66f5SMarkos Chandras 		 */
570a8ff66f5SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
571a8ff66f5SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
572a8ff66f5SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
573a8ff66f5SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
574a8ff66f5SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
575a8ff66f5SMarkos Chandras 					dec_insn.pc_inc;
576a8ff66f5SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
577a8ff66f5SMarkos Chandras 				dec_insn.next_pc_inc;
578a8ff66f5SMarkos Chandras 
579a8ff66f5SMarkos Chandras 			return 1;
580a8ff66f5SMarkos Chandras 		}
581102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] <= 0)
582102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
583102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
584102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
585102cedc3SLeonid Yegoshin 		else
586102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
587102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
588102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
589102cedc3SLeonid Yegoshin 		return 1;
590102cedc3SLeonid Yegoshin 	case bgtzl_op:
591e9d92d22SMarkos Chandras 		if (!insn.i_format.rt && NO_R6EMU)
592319824eaSMarkos Chandras 			break;
593319824eaSMarkos Chandras 	case bgtz_op:
594f1b44067SMarkos Chandras 		/*
595f1b44067SMarkos Chandras 		 * Compact branches for R6 for the
596f1b44067SMarkos Chandras 		 * bgtz and bgtzl opcodes.
597f1b44067SMarkos Chandras 		 * BGTZ  | rs = 0 | rt != 0  == BGTZALC
598f1b44067SMarkos Chandras 		 * BGTZ  | rs = rt != 0      == BLTZALC
599f1b44067SMarkos Chandras 		 * BGTZ  | rs != 0 | rt != 0 == BLTUC
600f1b44067SMarkos Chandras 		 * BGTZL | rs = 0 | rt != 0  == BGTZC
601f1b44067SMarkos Chandras 		 * BGTZL | rs = rt != 0      == BLTZC
602f1b44067SMarkos Chandras 		 * BGTZL | rs != 0 | rt != 0 == BLTC
603f1b44067SMarkos Chandras 		 *
604f1b44067SMarkos Chandras 		 * *ZALC varint for BGTZ &&& rt != 0
605f1b44067SMarkos Chandras 		 * For real GTZ{,L}, rt is always 0.
606f1b44067SMarkos Chandras 		 */
607f1b44067SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
608f1b44067SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
609f1b44067SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
610f1b44067SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
611f1b44067SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
612f1b44067SMarkos Chandras 					dec_insn.pc_inc;
613f1b44067SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
614f1b44067SMarkos Chandras 				dec_insn.next_pc_inc;
615f1b44067SMarkos Chandras 
616f1b44067SMarkos Chandras 			return 1;
617f1b44067SMarkos Chandras 		}
618f1b44067SMarkos Chandras 
619102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] > 0)
620102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
621102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
622102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
623102cedc3SLeonid Yegoshin 		else
624102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
625102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
626102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
627102cedc3SLeonid Yegoshin 		return 1;
628c893ce38SMarkos Chandras 	case cbcond0_op:
62910d962d5SMarkos Chandras 	case cbcond1_op:
630c893ce38SMarkos Chandras 		if (!cpu_has_mips_r6)
631c893ce38SMarkos Chandras 			break;
632c893ce38SMarkos Chandras 		if (insn.i_format.rt && !insn.i_format.rs)
633c893ce38SMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
634c893ce38SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
635c893ce38SMarkos Chandras 			dec_insn.next_pc_inc;
636c893ce38SMarkos Chandras 
637c893ce38SMarkos Chandras 		return 1;
638c26d4219SDavid Daney #ifdef CONFIG_CPU_CAVIUM_OCTEON
639c26d4219SDavid Daney 	case lwc2_op: /* This is bbit0 on Octeon */
640c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
641c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
642c26d4219SDavid Daney 		else
643c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
644c26d4219SDavid Daney 		return 1;
645c26d4219SDavid Daney 	case ldc2_op: /* This is bbit032 on Octeon */
646c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
647c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
648c26d4219SDavid Daney 		else
649c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
650c26d4219SDavid Daney 		return 1;
651c26d4219SDavid Daney 	case swc2_op: /* This is bbit1 on Octeon */
652c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
653c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
654c26d4219SDavid Daney 		else
655c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
656c26d4219SDavid Daney 		return 1;
657c26d4219SDavid Daney 	case sdc2_op: /* This is bbit132 on Octeon */
658c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
659c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
660c26d4219SDavid Daney 		else
661c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
662c26d4219SDavid Daney 		return 1;
6638467ca01SMarkos Chandras #else
6648467ca01SMarkos Chandras 	case bc6_op:
6658467ca01SMarkos Chandras 		/*
6668467ca01SMarkos Chandras 		 * Only valid for MIPS R6 but we can still end up
6678467ca01SMarkos Chandras 		 * here from a broken userland so just tell emulator
6688467ca01SMarkos Chandras 		 * this is not a branch and let it break later on.
6698467ca01SMarkos Chandras 		 */
6708467ca01SMarkos Chandras 		if  (!cpu_has_mips_r6)
6718467ca01SMarkos Chandras 			break;
6728467ca01SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
6738467ca01SMarkos Chandras 			dec_insn.next_pc_inc;
6748467ca01SMarkos Chandras 
6758467ca01SMarkos Chandras 		return 1;
67684fef630SMarkos Chandras 	case balc6_op:
67784fef630SMarkos Chandras 		if (!cpu_has_mips_r6)
67884fef630SMarkos Chandras 			break;
67984fef630SMarkos Chandras 		regs->regs[31] = regs->cp0_epc + 4;
68084fef630SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
68184fef630SMarkos Chandras 			dec_insn.next_pc_inc;
68284fef630SMarkos Chandras 
68384fef630SMarkos Chandras 		return 1;
68469b9a2fdSMarkos Chandras 	case beqzcjic_op:
68569b9a2fdSMarkos Chandras 		if (!cpu_has_mips_r6)
68669b9a2fdSMarkos Chandras 			break;
68769b9a2fdSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
68869b9a2fdSMarkos Chandras 			dec_insn.next_pc_inc;
68969b9a2fdSMarkos Chandras 
69069b9a2fdSMarkos Chandras 		return 1;
69128d6f93dSMarkos Chandras 	case bnezcjialc_op:
69228d6f93dSMarkos Chandras 		if (!cpu_has_mips_r6)
69328d6f93dSMarkos Chandras 			break;
69428d6f93dSMarkos Chandras 		if (!insn.i_format.rs)
69528d6f93dSMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
69628d6f93dSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
69728d6f93dSMarkos Chandras 			dec_insn.next_pc_inc;
69828d6f93dSMarkos Chandras 
69928d6f93dSMarkos Chandras 		return 1;
700c26d4219SDavid Daney #endif
7011da177e4SLinus Torvalds 	case cop0_op:
7021da177e4SLinus Torvalds 	case cop1_op:
703c8a34581SMarkos Chandras 		/* Need to check for R6 bc1nez and bc1eqz branches */
704c8a34581SMarkos Chandras 		if (cpu_has_mips_r6 &&
705c8a34581SMarkos Chandras 		    ((insn.i_format.rs == bc1eqz_op) ||
706c8a34581SMarkos Chandras 		     (insn.i_format.rs == bc1nez_op))) {
707c8a34581SMarkos Chandras 			bit = 0;
708c8a34581SMarkos Chandras 			switch (insn.i_format.rs) {
709c8a34581SMarkos Chandras 			case bc1eqz_op:
710c8a34581SMarkos Chandras 				if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
711c8a34581SMarkos Chandras 				    bit = 1;
712c8a34581SMarkos Chandras 				break;
713c8a34581SMarkos Chandras 			case bc1nez_op:
714c8a34581SMarkos Chandras 				if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
715c8a34581SMarkos Chandras 				    bit = 1;
716c8a34581SMarkos Chandras 				break;
717c8a34581SMarkos Chandras 			}
718c8a34581SMarkos Chandras 			if (bit)
719c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
720c8a34581SMarkos Chandras 					dec_insn.pc_inc +
721c8a34581SMarkos Chandras 					(insn.i_format.simmediate << 2);
722c8a34581SMarkos Chandras 			else
723c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
724c8a34581SMarkos Chandras 					dec_insn.pc_inc +
725c8a34581SMarkos Chandras 					dec_insn.next_pc_inc;
726c8a34581SMarkos Chandras 
727c8a34581SMarkos Chandras 			return 1;
728c8a34581SMarkos Chandras 		}
729c8a34581SMarkos Chandras 		/* R2/R6 compatible cop1 instruction. Fall through */
7301da177e4SLinus Torvalds 	case cop2_op:
7311da177e4SLinus Torvalds 	case cop1x_op:
732102cedc3SLeonid Yegoshin 		if (insn.i_format.rs == bc_op) {
733102cedc3SLeonid Yegoshin 			preempt_disable();
734102cedc3SLeonid Yegoshin 			if (is_fpu_owner())
735842dfc11SManuel Lauss 			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
736102cedc3SLeonid Yegoshin 			else
737102cedc3SLeonid Yegoshin 				fcr31 = current->thread.fpu.fcr31;
738102cedc3SLeonid Yegoshin 			preempt_enable();
739102cedc3SLeonid Yegoshin 
740102cedc3SLeonid Yegoshin 			bit = (insn.i_format.rt >> 2);
741102cedc3SLeonid Yegoshin 			bit += (bit != 0);
742102cedc3SLeonid Yegoshin 			bit += 23;
743102cedc3SLeonid Yegoshin 			switch (insn.i_format.rt & 3) {
744102cedc3SLeonid Yegoshin 			case 0:	/* bc1f */
745102cedc3SLeonid Yegoshin 			case 2:	/* bc1fl */
746102cedc3SLeonid Yegoshin 				if (~fcr31 & (1 << bit))
747102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
748102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
749102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
750102cedc3SLeonid Yegoshin 				else
751102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
752102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
753102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
754102cedc3SLeonid Yegoshin 				return 1;
755102cedc3SLeonid Yegoshin 			case 1:	/* bc1t */
756102cedc3SLeonid Yegoshin 			case 3:	/* bc1tl */
757102cedc3SLeonid Yegoshin 				if (fcr31 & (1 << bit))
758102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
759102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
760102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
761102cedc3SLeonid Yegoshin 				else
762102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
763102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
764102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
7651da177e4SLinus Torvalds 				return 1;
7661da177e4SLinus Torvalds 			}
767102cedc3SLeonid Yegoshin 		}
768102cedc3SLeonid Yegoshin 		break;
769102cedc3SLeonid Yegoshin 	}
7701da177e4SLinus Torvalds 	return 0;
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds /*
7741da177e4SLinus Torvalds  * In the Linux kernel, we support selection of FPR format on the
775da0bac33SDavid Daney  * basis of the Status.FR bit.	If an FPU is not present, the FR bit
776da0bac33SDavid Daney  * is hardwired to zero, which would imply a 32-bit FPU even for
777597ce172SPaul Burton  * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
77851d943f0SRalf Baechle  * FPU emu is slow and bulky and optimizing this function offers fairly
77951d943f0SRalf Baechle  * sizeable benefits so we try to be clever and make this function return
78051d943f0SRalf Baechle  * a constant whenever possible, that is on 64-bit kernels without O32
781597ce172SPaul Burton  * compatibility enabled and on 32-bit without 64-bit FPU support.
7821da177e4SLinus Torvalds  */
783da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp)
784da0bac33SDavid Daney {
78508a07904SRalf Baechle 	if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
78651d943f0SRalf Baechle 		return 1;
78708a07904SRalf Baechle 	else if (config_enabled(CONFIG_32BIT) &&
78808a07904SRalf Baechle 		 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
789da0bac33SDavid Daney 		return 0;
79008a07904SRalf Baechle 
791597ce172SPaul Burton 	return !test_thread_flag(TIF_32BIT_FPREGS);
792da0bac33SDavid Daney }
7931da177e4SLinus Torvalds 
7944227a2d4SPaul Burton static inline bool hybrid_fprs(void)
7954227a2d4SPaul Burton {
7964227a2d4SPaul Burton 	return test_thread_flag(TIF_HYBRID_FPREGS);
7974227a2d4SPaul Burton }
7984227a2d4SPaul Burton 
79947fa0c02SRalf Baechle #define SIFROMREG(si, x)						\
80047fa0c02SRalf Baechle do {									\
8014227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs())				\
802c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[x], 0);			\
803bbd426f5SPaul Burton 	else								\
804c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);	\
805bbd426f5SPaul Burton } while (0)
806da0bac33SDavid Daney 
80747fa0c02SRalf Baechle #define SITOREG(si, x)							\
80847fa0c02SRalf Baechle do {									\
8094227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs()) {			\
810ef1c47afSPaul Burton 		unsigned i;						\
811bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[x], 0, si);				\
812ef1c47afSPaul Burton 		for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)	\
813ef1c47afSPaul Burton 			set_fpr32(&ctx->fpr[x], i, 0);			\
814ef1c47afSPaul Burton 	} else {							\
815bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);		\
816ef1c47afSPaul Burton 	}								\
817bbd426f5SPaul Burton } while (0)
8181da177e4SLinus Torvalds 
819c8c0da6bSPaul Burton #define SIFROMHREG(si, x)	((si) = (int)get_fpr32(&ctx->fpr[x], 1))
820ef1c47afSPaul Burton 
82147fa0c02SRalf Baechle #define SITOHREG(si, x)							\
82247fa0c02SRalf Baechle do {									\
823ef1c47afSPaul Burton 	unsigned i;							\
824ef1c47afSPaul Burton 	set_fpr32(&ctx->fpr[x], 1, si);					\
825ef1c47afSPaul Burton 	for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)		\
826ef1c47afSPaul Burton 		set_fpr32(&ctx->fpr[x], i, 0);				\
827ef1c47afSPaul Burton } while (0)
8281ac94400SLeonid Yegoshin 
829bbd426f5SPaul Burton #define DIFROMREG(di, x)						\
830bbd426f5SPaul Burton 	((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
831bbd426f5SPaul Burton 
83247fa0c02SRalf Baechle #define DITOREG(di, x)							\
83347fa0c02SRalf Baechle do {									\
834ef1c47afSPaul Burton 	unsigned fpr, i;						\
835ef1c47afSPaul Burton 	fpr = (x) & ~(cop1_64bit(xcp) == 0);				\
836ef1c47afSPaul Burton 	set_fpr64(&ctx->fpr[fpr], 0, di);				\
837ef1c47afSPaul Burton 	for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++)		\
838ef1c47afSPaul Burton 		set_fpr64(&ctx->fpr[fpr], i, 0);			\
839ef1c47afSPaul Burton } while (0)
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
8421da177e4SLinus Torvalds #define SPTOREG(sp, x)	SITOREG((sp).bits, x)
8431da177e4SLinus Torvalds #define DPFROMREG(dp, x)	DIFROMREG((dp).bits, x)
8441da177e4SLinus Torvalds #define DPTOREG(dp, x)	DITOREG((dp).bits, x)
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds /*
847d4f5b088SMaciej W. Rozycki  * Emulate a CFC1 instruction.
848d4f5b088SMaciej W. Rozycki  */
849d4f5b088SMaciej W. Rozycki static inline void cop1_cfc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
850d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
851d4f5b088SMaciej W. Rozycki {
852c491cfa2SMaciej W. Rozycki 	u32 fcr31 = ctx->fcr31;
853c491cfa2SMaciej W. Rozycki 	u32 value = 0;
854d4f5b088SMaciej W. Rozycki 
855c491cfa2SMaciej W. Rozycki 	switch (MIPSInst_RD(ir)) {
856c491cfa2SMaciej W. Rozycki 	case FPCREG_CSR:
857c491cfa2SMaciej W. Rozycki 		value = fcr31;
858d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-csr=%08x\n",
859c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
860c491cfa2SMaciej W. Rozycki 		break;
861c491cfa2SMaciej W. Rozycki 
862c491cfa2SMaciej W. Rozycki 	case FPCREG_FENR:
863c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
864c491cfa2SMaciej W. Rozycki 			break;
865c491cfa2SMaciej W. Rozycki 		value = (fcr31 >> (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
866c491cfa2SMaciej W. Rozycki 			MIPS_FENR_FS;
867c491cfa2SMaciej W. Rozycki 		value |= fcr31 & (FPU_CSR_ALL_E | FPU_CSR_RM);
868c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-enr=%08x\n",
869c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
870c491cfa2SMaciej W. Rozycki 		break;
871c491cfa2SMaciej W. Rozycki 
872c491cfa2SMaciej W. Rozycki 	case FPCREG_FEXR:
873c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
874c491cfa2SMaciej W. Rozycki 			break;
875c491cfa2SMaciej W. Rozycki 		value = fcr31 & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
876c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-exr=%08x\n",
877c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
878c491cfa2SMaciej W. Rozycki 		break;
879c491cfa2SMaciej W. Rozycki 
880c491cfa2SMaciej W. Rozycki 	case FPCREG_FCCR:
881c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
882c491cfa2SMaciej W. Rozycki 			break;
883c491cfa2SMaciej W. Rozycki 		value = (fcr31 >> (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
884c491cfa2SMaciej W. Rozycki 			MIPS_FCCR_COND0;
885c491cfa2SMaciej W. Rozycki 		value |= (fcr31 >> (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
886c491cfa2SMaciej W. Rozycki 			 (MIPS_FCCR_CONDX & ~MIPS_FCCR_COND0);
887c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]<-ccr=%08x\n",
888c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
889c491cfa2SMaciej W. Rozycki 		break;
890c491cfa2SMaciej W. Rozycki 
891c491cfa2SMaciej W. Rozycki 	case FPCREG_RID:
89203dce595SMaciej W. Rozycki 		value = boot_cpu_data.fpu_id;
893c491cfa2SMaciej W. Rozycki 		break;
894c491cfa2SMaciej W. Rozycki 
895c491cfa2SMaciej W. Rozycki 	default:
896c491cfa2SMaciej W. Rozycki 		break;
897c491cfa2SMaciej W. Rozycki 	}
898c491cfa2SMaciej W. Rozycki 
899d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir))
900d4f5b088SMaciej W. Rozycki 		xcp->regs[MIPSInst_RT(ir)] = value;
901d4f5b088SMaciej W. Rozycki }
902d4f5b088SMaciej W. Rozycki 
903d4f5b088SMaciej W. Rozycki /*
904d4f5b088SMaciej W. Rozycki  * Emulate a CTC1 instruction.
905d4f5b088SMaciej W. Rozycki  */
906d4f5b088SMaciej W. Rozycki static inline void cop1_ctc(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
907d4f5b088SMaciej W. Rozycki 			    mips_instruction ir)
908d4f5b088SMaciej W. Rozycki {
909c491cfa2SMaciej W. Rozycki 	u32 fcr31 = ctx->fcr31;
910d4f5b088SMaciej W. Rozycki 	u32 value;
9119b26616cSMaciej W. Rozycki 	u32 mask;
912d4f5b088SMaciej W. Rozycki 
913d4f5b088SMaciej W. Rozycki 	if (MIPSInst_RT(ir) == 0)
914d4f5b088SMaciej W. Rozycki 		value = 0;
915d4f5b088SMaciej W. Rozycki 	else
916d4f5b088SMaciej W. Rozycki 		value = xcp->regs[MIPSInst_RT(ir)];
917d4f5b088SMaciej W. Rozycki 
918c491cfa2SMaciej W. Rozycki 	switch (MIPSInst_RD(ir)) {
919c491cfa2SMaciej W. Rozycki 	case FPCREG_CSR:
920d4f5b088SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->csr=%08x\n",
921c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
922d4f5b088SMaciej W. Rozycki 
9239b26616cSMaciej W. Rozycki 		/* Preserve read-only bits.  */
92403dce595SMaciej W. Rozycki 		mask = boot_cpu_data.fpu_msk31;
9259b26616cSMaciej W. Rozycki 		fcr31 = (value & ~mask) | (fcr31 & mask);
926c491cfa2SMaciej W. Rozycki 		break;
927c491cfa2SMaciej W. Rozycki 
928c491cfa2SMaciej W. Rozycki 	case FPCREG_FENR:
929c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
930c491cfa2SMaciej W. Rozycki 			break;
931c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->enr=%08x\n",
932c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
933c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_FS | FPU_CSR_ALL_E | FPU_CSR_RM);
934c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_FS_S - MIPS_FENR_FS_S)) &
935c491cfa2SMaciej W. Rozycki 			 FPU_CSR_FS;
936c491cfa2SMaciej W. Rozycki 		fcr31 |= value & (FPU_CSR_ALL_E | FPU_CSR_RM);
937c491cfa2SMaciej W. Rozycki 		break;
938c491cfa2SMaciej W. Rozycki 
939c491cfa2SMaciej W. Rozycki 	case FPCREG_FEXR:
940c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
941c491cfa2SMaciej W. Rozycki 			break;
942c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->exr=%08x\n",
943c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
944c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_ALL_X | FPU_CSR_ALL_S);
945c491cfa2SMaciej W. Rozycki 		fcr31 |= value & (FPU_CSR_ALL_X | FPU_CSR_ALL_S);
946c491cfa2SMaciej W. Rozycki 		break;
947c491cfa2SMaciej W. Rozycki 
948c491cfa2SMaciej W. Rozycki 	case FPCREG_FCCR:
949c491cfa2SMaciej W. Rozycki 		if (!cpu_has_mips_r)
950c491cfa2SMaciej W. Rozycki 			break;
951c491cfa2SMaciej W. Rozycki 		pr_debug("%p gpr[%d]->ccr=%08x\n",
952c491cfa2SMaciej W. Rozycki 			 (void *)xcp->cp0_epc, MIPSInst_RT(ir), value);
953c491cfa2SMaciej W. Rozycki 		fcr31 &= ~(FPU_CSR_CONDX | FPU_CSR_COND);
954c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_COND_S - MIPS_FCCR_COND0_S)) &
955c491cfa2SMaciej W. Rozycki 			 FPU_CSR_COND;
956c491cfa2SMaciej W. Rozycki 		fcr31 |= (value << (FPU_CSR_COND1_S - MIPS_FCCR_COND1_S)) &
957c491cfa2SMaciej W. Rozycki 			 FPU_CSR_CONDX;
958c491cfa2SMaciej W. Rozycki 		break;
959c491cfa2SMaciej W. Rozycki 
960c491cfa2SMaciej W. Rozycki 	default:
961c491cfa2SMaciej W. Rozycki 		break;
962d4f5b088SMaciej W. Rozycki 	}
963c491cfa2SMaciej W. Rozycki 
964c491cfa2SMaciej W. Rozycki 	ctx->fcr31 = fcr31;
965d4f5b088SMaciej W. Rozycki }
966d4f5b088SMaciej W. Rozycki 
967d4f5b088SMaciej W. Rozycki /*
9681da177e4SLinus Torvalds  * Emulate the single floating point instruction pointed at by EPC.
9691da177e4SLinus Torvalds  * Two instructions if the instruction is in a branch delay slot.
9701da177e4SLinus Torvalds  */
9711da177e4SLinus Torvalds 
972515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
973102cedc3SLeonid Yegoshin 		struct mm_decoded_insn dec_insn, void *__user *fault_addr)
9741da177e4SLinus Torvalds {
975102cedc3SLeonid Yegoshin 	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
976*93583e17SPaul Burton 	unsigned int cond, cbit, bit0;
9773f7cac41SRalf Baechle 	mips_instruction ir;
9783f7cac41SRalf Baechle 	int likely, pc_inc;
979*93583e17SPaul Burton 	union fpureg *fpr;
9803f7cac41SRalf Baechle 	u32 __user *wva;
9813f7cac41SRalf Baechle 	u64 __user *dva;
9823f7cac41SRalf Baechle 	u32 wval;
9833f7cac41SRalf Baechle 	u64 dval;
9843f7cac41SRalf Baechle 	int sig;
9851da177e4SLinus Torvalds 
98670e4c234SRalf Baechle 	/*
98770e4c234SRalf Baechle 	 * These are giving gcc a gentle hint about what to expect in
98870e4c234SRalf Baechle 	 * dec_inst in order to do better optimization.
98970e4c234SRalf Baechle 	 */
99070e4c234SRalf Baechle 	if (!cpu_has_mmips && dec_insn.micro_mips_mode)
99170e4c234SRalf Baechle 		unreachable();
99270e4c234SRalf Baechle 
9931da177e4SLinus Torvalds 	/* XXX NEC Vr54xx bug workaround */
994e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
995102cedc3SLeonid Yegoshin 		if (dec_insn.micro_mips_mode) {
996102cedc3SLeonid Yegoshin 			if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
997e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
998102cedc3SLeonid Yegoshin 		} else {
999102cedc3SLeonid Yegoshin 			if (!isBranchInstr(xcp, dec_insn, &contpc))
1000e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
1001102cedc3SLeonid Yegoshin 		}
1002102cedc3SLeonid Yegoshin 	}
10031da177e4SLinus Torvalds 
1004e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
10051da177e4SLinus Torvalds 		/*
10061da177e4SLinus Torvalds 		 * The instruction to be emulated is in a branch delay slot
10071da177e4SLinus Torvalds 		 * which means that we have to	emulate the branch instruction
10081da177e4SLinus Torvalds 		 * BEFORE we do the cop1 instruction.
10091da177e4SLinus Torvalds 		 *
10101da177e4SLinus Torvalds 		 * This branch could be a COP1 branch, but in that case we
10111da177e4SLinus Torvalds 		 * would have had a trap for that instruction, and would not
10121da177e4SLinus Torvalds 		 * come through this route.
10131da177e4SLinus Torvalds 		 *
10141da177e4SLinus Torvalds 		 * Linux MIPS branch emulator operates on context, updating the
10151da177e4SLinus Torvalds 		 * cp0_epc.
10161da177e4SLinus Torvalds 		 */
1017102cedc3SLeonid Yegoshin 		ir = dec_insn.next_insn;  /* process delay slot instr */
1018102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.next_pc_inc;
1019333d1f67SRalf Baechle 	} else {
1020102cedc3SLeonid Yegoshin 		ir = dec_insn.insn;       /* process current instr */
1021102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.pc_inc;
1022102cedc3SLeonid Yegoshin 	}
1023102cedc3SLeonid Yegoshin 
1024102cedc3SLeonid Yegoshin 	/*
1025102cedc3SLeonid Yegoshin 	 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
1026102cedc3SLeonid Yegoshin 	 * instructions, we want to convert microMIPS FPU instructions
1027102cedc3SLeonid Yegoshin 	 * into MIPS32 instructions so that we could reuse all of the
1028102cedc3SLeonid Yegoshin 	 * FPU emulation code.
1029102cedc3SLeonid Yegoshin 	 *
1030102cedc3SLeonid Yegoshin 	 * NOTE: We cannot do this for branch instructions since they
1031102cedc3SLeonid Yegoshin 	 *       are not a subset. Example: Cannot emulate a 16-bit
1032102cedc3SLeonid Yegoshin 	 *       aligned target address with a MIPS32 instruction.
1033102cedc3SLeonid Yegoshin 	 */
1034102cedc3SLeonid Yegoshin 	if (dec_insn.micro_mips_mode) {
1035102cedc3SLeonid Yegoshin 		/*
1036102cedc3SLeonid Yegoshin 		 * If next instruction is a 16-bit instruction, then it
1037102cedc3SLeonid Yegoshin 		 * it cannot be a FPU instruction. This could happen
1038102cedc3SLeonid Yegoshin 		 * since we can be called for non-FPU instructions.
1039102cedc3SLeonid Yegoshin 		 */
1040102cedc3SLeonid Yegoshin 		if ((pc_inc == 2) ||
1041102cedc3SLeonid Yegoshin 			(microMIPS32_to_MIPS32((union mips_instruction *)&ir)
1042102cedc3SLeonid Yegoshin 			 == SIGILL))
1043102cedc3SLeonid Yegoshin 			return SIGILL;
10441da177e4SLinus Torvalds 	}
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds emul:
1047a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
1048b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(emulated);
10491da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(ir)) {
10503f7cac41SRalf Baechle 	case ldc1_op:
10513f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10521da177e4SLinus Torvalds 				     MIPSInst_SIMM(ir));
1053b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
1054515b029dSDavid Daney 
10553f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
1056b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10573f7cac41SRalf Baechle 			*fault_addr = dva;
10581da177e4SLinus Torvalds 			return SIGBUS;
10591da177e4SLinus Torvalds 		}
10603f7cac41SRalf Baechle 		if (__get_user(dval, dva)) {
1061515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10623f7cac41SRalf Baechle 			*fault_addr = dva;
1063515b029dSDavid Daney 			return SIGSEGV;
1064515b029dSDavid Daney 		}
10653f7cac41SRalf Baechle 		DITOREG(dval, MIPSInst_RT(ir));
10661da177e4SLinus Torvalds 		break;
10671da177e4SLinus Torvalds 
10683f7cac41SRalf Baechle 	case sdc1_op:
10693f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10701da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1071b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
10723f7cac41SRalf Baechle 		DIFROMREG(dval, MIPSInst_RT(ir));
10733f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
1074b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10753f7cac41SRalf Baechle 			*fault_addr = dva;
10761da177e4SLinus Torvalds 			return SIGBUS;
10771da177e4SLinus Torvalds 		}
10783f7cac41SRalf Baechle 		if (__put_user(dval, dva)) {
1079515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10803f7cac41SRalf Baechle 			*fault_addr = dva;
1081515b029dSDavid Daney 			return SIGSEGV;
1082515b029dSDavid Daney 		}
10831da177e4SLinus Torvalds 		break;
10841da177e4SLinus Torvalds 
10853f7cac41SRalf Baechle 	case lwc1_op:
10863f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10871da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1088b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
10893f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
1090b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10913f7cac41SRalf Baechle 			*fault_addr = wva;
10921da177e4SLinus Torvalds 			return SIGBUS;
10931da177e4SLinus Torvalds 		}
10943f7cac41SRalf Baechle 		if (__get_user(wval, wva)) {
1095515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
10963f7cac41SRalf Baechle 			*fault_addr = wva;
1097515b029dSDavid Daney 			return SIGSEGV;
1098515b029dSDavid Daney 		}
10993f7cac41SRalf Baechle 		SITOREG(wval, MIPSInst_RT(ir));
11001da177e4SLinus Torvalds 		break;
11011da177e4SLinus Torvalds 
11023f7cac41SRalf Baechle 	case swc1_op:
11033f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
11041da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
1105b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
11063f7cac41SRalf Baechle 		SIFROMREG(wval, MIPSInst_RT(ir));
11073f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
1108b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11093f7cac41SRalf Baechle 			*fault_addr = wva;
11101da177e4SLinus Torvalds 			return SIGBUS;
11111da177e4SLinus Torvalds 		}
11123f7cac41SRalf Baechle 		if (__put_user(wval, wva)) {
1113515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
11143f7cac41SRalf Baechle 			*fault_addr = wva;
1115515b029dSDavid Daney 			return SIGSEGV;
1116515b029dSDavid Daney 		}
11171da177e4SLinus Torvalds 		break;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	case cop1_op:
11201da177e4SLinus Torvalds 		switch (MIPSInst_RS(ir)) {
11211da177e4SLinus Torvalds 		case dmfc_op:
112208a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
112308a07904SRalf Baechle 				return SIGILL;
112408a07904SRalf Baechle 
11251da177e4SLinus Torvalds 			/* copregister fs -> gpr[rt] */
11261da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
11271da177e4SLinus Torvalds 				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
11281da177e4SLinus Torvalds 					MIPSInst_RD(ir));
11291da177e4SLinus Torvalds 			}
11301da177e4SLinus Torvalds 			break;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 		case dmtc_op:
113308a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
113408a07904SRalf Baechle 				return SIGILL;
113508a07904SRalf Baechle 
11361da177e4SLinus Torvalds 			/* copregister fs <- rt */
11371da177e4SLinus Torvalds 			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11381da177e4SLinus Torvalds 			break;
11391da177e4SLinus Torvalds 
11401ac94400SLeonid Yegoshin 		case mfhc_op:
1141e8f80cc1SMarkos Chandras 			if (!cpu_has_mips_r2_r6)
11421ac94400SLeonid Yegoshin 				goto sigill;
11431ac94400SLeonid Yegoshin 
11441ac94400SLeonid Yegoshin 			/* copregister rd -> gpr[rt] */
11451ac94400SLeonid Yegoshin 			if (MIPSInst_RT(ir) != 0) {
11461ac94400SLeonid Yegoshin 				SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
11471ac94400SLeonid Yegoshin 					MIPSInst_RD(ir));
11481ac94400SLeonid Yegoshin 			}
11491ac94400SLeonid Yegoshin 			break;
11501ac94400SLeonid Yegoshin 
11511ac94400SLeonid Yegoshin 		case mthc_op:
1152e8f80cc1SMarkos Chandras 			if (!cpu_has_mips_r2_r6)
11531ac94400SLeonid Yegoshin 				goto sigill;
11541ac94400SLeonid Yegoshin 
11551ac94400SLeonid Yegoshin 			/* copregister rd <- gpr[rt] */
11561ac94400SLeonid Yegoshin 			SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11571ac94400SLeonid Yegoshin 			break;
11581ac94400SLeonid Yegoshin 
11591da177e4SLinus Torvalds 		case mfc_op:
11601da177e4SLinus Torvalds 			/* copregister rd -> gpr[rt] */
11611da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
11621da177e4SLinus Torvalds 				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
11631da177e4SLinus Torvalds 					MIPSInst_RD(ir));
11641da177e4SLinus Torvalds 			}
11651da177e4SLinus Torvalds 			break;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 		case mtc_op:
11681da177e4SLinus Torvalds 			/* copregister rd <- rt */
11691da177e4SLinus Torvalds 			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11701da177e4SLinus Torvalds 			break;
11711da177e4SLinus Torvalds 
11723f7cac41SRalf Baechle 		case cfc_op:
11731da177e4SLinus Torvalds 			/* cop control register rd -> gpr[rt] */
1174d4f5b088SMaciej W. Rozycki 			cop1_cfc(xcp, ctx, ir);
11751da177e4SLinus Torvalds 			break;
11761da177e4SLinus Torvalds 
11773f7cac41SRalf Baechle 		case ctc_op:
11781da177e4SLinus Torvalds 			/* copregister rd <- rt */
1179d4f5b088SMaciej W. Rozycki 			cop1_ctc(xcp, ctx, ir);
11801da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
11811da177e4SLinus Torvalds 				return SIGFPE;
11821da177e4SLinus Torvalds 			}
11831da177e4SLinus Torvalds 			break;
11841da177e4SLinus Torvalds 
1185c909ca71SMarkos Chandras 		case bc1eqz_op:
1186c909ca71SMarkos Chandras 		case bc1nez_op:
1187c909ca71SMarkos Chandras 			if (!cpu_has_mips_r6 || delay_slot(xcp))
1188c909ca71SMarkos Chandras 				return SIGILL;
1189c909ca71SMarkos Chandras 
1190c909ca71SMarkos Chandras 			cond = likely = 0;
1191*93583e17SPaul Burton 			fpr = &current->thread.fpu.fpr[MIPSInst_RT(ir)];
1192*93583e17SPaul Burton 			bit0 = get_fpr32(fpr, 0) & 0x1;
1193c909ca71SMarkos Chandras 			switch (MIPSInst_RS(ir)) {
1194c909ca71SMarkos Chandras 			case bc1eqz_op:
1195*93583e17SPaul Burton 				cond = bit0 == 0;
1196c909ca71SMarkos Chandras 				break;
1197c909ca71SMarkos Chandras 			case bc1nez_op:
1198*93583e17SPaul Burton 				cond = bit0 != 0;
1199c909ca71SMarkos Chandras 				break;
1200c909ca71SMarkos Chandras 			}
1201c909ca71SMarkos Chandras 			goto branch_common;
1202c909ca71SMarkos Chandras 
12033f7cac41SRalf Baechle 		case bc_op:
1204e7e9cae5SRalf Baechle 			if (delay_slot(xcp))
12051da177e4SLinus Torvalds 				return SIGILL;
12061da177e4SLinus Torvalds 
120708a07904SRalf Baechle 			if (cpu_has_mips_4_5_r)
120808a07904SRalf Baechle 				cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
120908a07904SRalf Baechle 			else
121008a07904SRalf Baechle 				cbit = FPU_CSR_COND;
121108a07904SRalf Baechle 			cond = ctx->fcr31 & cbit;
121208a07904SRalf Baechle 
12133f7cac41SRalf Baechle 			likely = 0;
12141da177e4SLinus Torvalds 			switch (MIPSInst_RT(ir) & 3) {
12151da177e4SLinus Torvalds 			case bcfl_op:
12162d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
12171da177e4SLinus Torvalds 					likely = 1;
12182d83fea7SMaciej W. Rozycki 				/* Fall through */
12191da177e4SLinus Torvalds 			case bcf_op:
12201da177e4SLinus Torvalds 				cond = !cond;
12211da177e4SLinus Torvalds 				break;
12221da177e4SLinus Torvalds 			case bctl_op:
12232d83fea7SMaciej W. Rozycki 				if (cpu_has_mips_2_3_4_5_r)
12241da177e4SLinus Torvalds 					likely = 1;
12252d83fea7SMaciej W. Rozycki 				/* Fall through */
12261da177e4SLinus Torvalds 			case bct_op:
12271da177e4SLinus Torvalds 				break;
12281da177e4SLinus Torvalds 			}
1229c909ca71SMarkos Chandras branch_common:
1230e7e9cae5SRalf Baechle 			set_delay_slot(xcp);
12311da177e4SLinus Torvalds 			if (cond) {
12323f7cac41SRalf Baechle 				/*
12333f7cac41SRalf Baechle 				 * Branch taken: emulate dslot instruction
12341da177e4SLinus Torvalds 				 */
12359ab4471cSMaciej W. Rozycki 				unsigned long bcpc;
12369ab4471cSMaciej W. Rozycki 
12379ab4471cSMaciej W. Rozycki 				/*
12389ab4471cSMaciej W. Rozycki 				 * Remember EPC at the branch to point back
12399ab4471cSMaciej W. Rozycki 				 * at so that any delay-slot instruction
12409ab4471cSMaciej W. Rozycki 				 * signal is not silently ignored.
12419ab4471cSMaciej W. Rozycki 				 */
12429ab4471cSMaciej W. Rozycki 				bcpc = xcp->cp0_epc;
1243102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
12441da177e4SLinus Torvalds 
1245102cedc3SLeonid Yegoshin 				contpc = MIPSInst_SIMM(ir);
1246102cedc3SLeonid Yegoshin 				ir = dec_insn.next_insn;
1247102cedc3SLeonid Yegoshin 				if (dec_insn.micro_mips_mode) {
1248102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 1));
1249102cedc3SLeonid Yegoshin 
1250102cedc3SLeonid Yegoshin 					/* If 16-bit instruction, not FPU. */
1251102cedc3SLeonid Yegoshin 					if ((dec_insn.next_pc_inc == 2) ||
1252102cedc3SLeonid Yegoshin 						(microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1253102cedc3SLeonid Yegoshin 
1254102cedc3SLeonid Yegoshin 						/*
1255102cedc3SLeonid Yegoshin 						 * Since this instruction will
1256102cedc3SLeonid Yegoshin 						 * be put on the stack with
1257102cedc3SLeonid Yegoshin 						 * 32-bit words, get around
1258102cedc3SLeonid Yegoshin 						 * this problem by putting a
1259102cedc3SLeonid Yegoshin 						 * NOP16 as the second one.
1260102cedc3SLeonid Yegoshin 						 */
1261102cedc3SLeonid Yegoshin 						if (dec_insn.next_pc_inc == 2)
1262102cedc3SLeonid Yegoshin 							ir = (ir & (~0xffff)) | MM_NOP16;
1263102cedc3SLeonid Yegoshin 
1264102cedc3SLeonid Yegoshin 						/*
1265102cedc3SLeonid Yegoshin 						 * Single step the non-CP1
1266102cedc3SLeonid Yegoshin 						 * instruction in the dslot.
1267102cedc3SLeonid Yegoshin 						 */
12689ab4471cSMaciej W. Rozycki 						sig = mips_dsemul(xcp, ir,
12699ab4471cSMaciej W. Rozycki 								  contpc);
1270e4553573SMaciej W. Rozycki 						if (sig < 0)
1271e4553573SMaciej W. Rozycki 							break;
12729ab4471cSMaciej W. Rozycki 						if (sig)
12739ab4471cSMaciej W. Rozycki 							xcp->cp0_epc = bcpc;
12749ab4471cSMaciej W. Rozycki 						/*
12759ab4471cSMaciej W. Rozycki 						 * SIGILL forces out of
12769ab4471cSMaciej W. Rozycki 						 * the emulation loop.
12779ab4471cSMaciej W. Rozycki 						 */
12789ab4471cSMaciej W. Rozycki 						return sig ? sig : SIGILL;
1279515b029dSDavid Daney 					}
1280102cedc3SLeonid Yegoshin 				} else
1281102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 2));
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 				switch (MIPSInst_OPCODE(ir)) {
12841da177e4SLinus Torvalds 				case lwc1_op:
12851da177e4SLinus Torvalds 				case swc1_op:
128608a07904SRalf Baechle 					goto emul;
12873f7cac41SRalf Baechle 
12881da177e4SLinus Torvalds 				case ldc1_op:
12891da177e4SLinus Torvalds 				case sdc1_op:
12902d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_2_3_4_5_r)
129108a07904SRalf Baechle 						goto emul;
129208a07904SRalf Baechle 
12939ab4471cSMaciej W. Rozycki 					goto bc_sigill;
12943f7cac41SRalf Baechle 
12951da177e4SLinus Torvalds 				case cop1_op:
129608a07904SRalf Baechle 					goto emul;
12973f7cac41SRalf Baechle 
12981da177e4SLinus Torvalds 				case cop1x_op:
12992d83fea7SMaciej W. Rozycki 					if (cpu_has_mips_4_5_64_r2_r6)
13001da177e4SLinus Torvalds 						/* its one of ours */
13011da177e4SLinus Torvalds 						goto emul;
130208a07904SRalf Baechle 
13039ab4471cSMaciej W. Rozycki 					goto bc_sigill;
13043f7cac41SRalf Baechle 
13051da177e4SLinus Torvalds 				case spec_op:
13062d83fea7SMaciej W. Rozycki 					switch (MIPSInst_FUNC(ir)) {
13072d83fea7SMaciej W. Rozycki 					case movc_op:
13082d83fea7SMaciej W. Rozycki 						if (cpu_has_mips_4_5_r)
13091da177e4SLinus Torvalds 							goto emul;
13102d83fea7SMaciej W. Rozycki 
13119ab4471cSMaciej W. Rozycki 						goto bc_sigill;
13122d83fea7SMaciej W. Rozycki 					}
13131da177e4SLinus Torvalds 					break;
13149ab4471cSMaciej W. Rozycki 
13159ab4471cSMaciej W. Rozycki 				bc_sigill:
13169ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
13179ab4471cSMaciej W. Rozycki 					return SIGILL;
13181da177e4SLinus Torvalds 				}
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 				/*
13211da177e4SLinus Torvalds 				 * Single step the non-cp1
13221da177e4SLinus Torvalds 				 * instruction in the dslot
13231da177e4SLinus Torvalds 				 */
13249ab4471cSMaciej W. Rozycki 				sig = mips_dsemul(xcp, ir, contpc);
1325e4553573SMaciej W. Rozycki 				if (sig < 0)
1326e4553573SMaciej W. Rozycki 					break;
13279ab4471cSMaciej W. Rozycki 				if (sig)
13289ab4471cSMaciej W. Rozycki 					xcp->cp0_epc = bcpc;
13299ab4471cSMaciej W. Rozycki 				/* SIGILL forces out of the emulation loop.  */
13309ab4471cSMaciej W. Rozycki 				return sig ? sig : SIGILL;
13313f7cac41SRalf Baechle 			} else if (likely) {	/* branch not taken */
13321da177e4SLinus Torvalds 				/*
13331da177e4SLinus Torvalds 				 * branch likely nullifies
13341da177e4SLinus Torvalds 				 * dslot if not taken
13351da177e4SLinus Torvalds 				 */
1336102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
1337102cedc3SLeonid Yegoshin 				contpc += dec_insn.pc_inc;
13381da177e4SLinus Torvalds 				/*
13391da177e4SLinus Torvalds 				 * else continue & execute
13401da177e4SLinus Torvalds 				 * dslot as normal insn
13411da177e4SLinus Torvalds 				 */
13421da177e4SLinus Torvalds 			}
13431da177e4SLinus Torvalds 			break;
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 		default:
13461da177e4SLinus Torvalds 			if (!(MIPSInst_RS(ir) & 0x10))
13471da177e4SLinus Torvalds 				return SIGILL;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 			/* a real fpu computation instruction */
13501da177e4SLinus Torvalds 			if ((sig = fpu_emu(xcp, ctx, ir)))
13511da177e4SLinus Torvalds 				return sig;
13521da177e4SLinus Torvalds 		}
13531da177e4SLinus Torvalds 		break;
13541da177e4SLinus Torvalds 
13553f7cac41SRalf Baechle 	case cop1x_op:
13562d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_4_5_64_r2_r6)
135708a07904SRalf Baechle 			return SIGILL;
135808a07904SRalf Baechle 
135908a07904SRalf Baechle 		sig = fpux_emu(xcp, ctx, ir, fault_addr);
1360515b029dSDavid Daney 		if (sig)
13611da177e4SLinus Torvalds 			return sig;
13621da177e4SLinus Torvalds 		break;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	case spec_op:
136508a07904SRalf Baechle 		if (!cpu_has_mips_4_5_r)
136608a07904SRalf Baechle 			return SIGILL;
136708a07904SRalf Baechle 
13681da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != movc_op)
13691da177e4SLinus Torvalds 			return SIGILL;
13701da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
13711da177e4SLinus Torvalds 		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
13721da177e4SLinus Torvalds 			xcp->regs[MIPSInst_RD(ir)] =
13731da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RS(ir)];
13741da177e4SLinus Torvalds 		break;
13751da177e4SLinus Torvalds 	default:
13761ac94400SLeonid Yegoshin sigill:
13771da177e4SLinus Torvalds 		return SIGILL;
13781da177e4SLinus Torvalds 	}
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	/* we did it !! */
1381e70dfc10SAtsushi Nemoto 	xcp->cp0_epc = contpc;
1382e7e9cae5SRalf Baechle 	clear_delay_slot(xcp);
1383333d1f67SRalf Baechle 
13841da177e4SLinus Torvalds 	return 0;
13851da177e4SLinus Torvalds }
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds /*
13881da177e4SLinus Torvalds  * Conversion table from MIPS compare ops 48-63
13891da177e4SLinus Torvalds  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
13901da177e4SLinus Torvalds  */
13911da177e4SLinus Torvalds static const unsigned char cmptab[8] = {
13921da177e4SLinus Torvalds 	0,			/* cmp_0 (sig) cmp_sf */
13931da177e4SLinus Torvalds 	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
13941da177e4SLinus Torvalds 	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
13951da177e4SLinus Torvalds 	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
13961da177e4SLinus Torvalds 	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
13971da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
13981da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
13991da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
14001da177e4SLinus Torvalds };
14011da177e4SLinus Torvalds 
1402f8c3c671SMarkos Chandras static const unsigned char negative_cmptab[8] = {
1403f8c3c671SMarkos Chandras 	0, /* Reserved */
1404f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT | IEEE754_CEQ,
1405f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT | IEEE754_CUN,
1406f8c3c671SMarkos Chandras 	IEEE754_CLT | IEEE754_CGT,
1407f8c3c671SMarkos Chandras 	/* Reserved */
1408f8c3c671SMarkos Chandras };
1409f8c3c671SMarkos Chandras 
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds /*
14121da177e4SLinus Torvalds  * Additional MIPS4 instructions
14131da177e4SLinus Torvalds  */
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3)					\
141647fa0c02SRalf Baechle static union ieee754##p fpemu_##p##_##name(union ieee754##p r,		\
141747fa0c02SRalf Baechle 	union ieee754##p s, union ieee754##p t)				\
14181da177e4SLinus Torvalds {									\
1419cd21dfcfSRalf Baechle 	struct _ieee754_csr ieee754_csr_save;				\
14201da177e4SLinus Torvalds 	s = f1(s, t);							\
14211da177e4SLinus Torvalds 	ieee754_csr_save = ieee754_csr;					\
14221da177e4SLinus Torvalds 	s = f2(s, r);							\
14231da177e4SLinus Torvalds 	ieee754_csr_save.cx |= ieee754_csr.cx;				\
14241da177e4SLinus Torvalds 	ieee754_csr_save.sx |= ieee754_csr.sx;				\
14251da177e4SLinus Torvalds 	s = f3(s);							\
14261da177e4SLinus Torvalds 	ieee754_csr.cx |= ieee754_csr_save.cx;				\
14271da177e4SLinus Torvalds 	ieee754_csr.sx |= ieee754_csr_save.sx;				\
14281da177e4SLinus Torvalds 	return s;							\
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
14312209bcb1SRalf Baechle static union ieee754dp fpemu_dp_recip(union ieee754dp d)
14321da177e4SLinus Torvalds {
14331da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), d);
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
14362209bcb1SRalf Baechle static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
14371da177e4SLinus Torvalds {
14381da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
14391da177e4SLinus Torvalds }
14401da177e4SLinus Torvalds 
14412209bcb1SRalf Baechle static union ieee754sp fpemu_sp_recip(union ieee754sp s)
14421da177e4SLinus Torvalds {
14431da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), s);
14441da177e4SLinus Torvalds }
14451da177e4SLinus Torvalds 
14462209bcb1SRalf Baechle static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
14471da177e4SLinus Torvalds {
14481da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
14491da177e4SLinus Torvalds }
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
14521da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
14531da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
14541da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
14551da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
14561da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
14571da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
14581da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
14591da177e4SLinus Torvalds 
1460eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1461515b029dSDavid Daney 	mips_instruction ir, void *__user *fault_addr)
14621da177e4SLinus Torvalds {
14631da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
14641da177e4SLinus Torvalds 
1465b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1xops);
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	switch (MIPSInst_FMA_FFMT(ir)) {
14681da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
14691da177e4SLinus Torvalds 
14702209bcb1SRalf Baechle 		union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
14712209bcb1SRalf Baechle 		union ieee754sp fd, fr, fs, ft;
14723fccc015SRalf Baechle 		u32 __user *va;
14731da177e4SLinus Torvalds 		u32 val;
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
14761da177e4SLinus Torvalds 		case lwxc1_op:
14773fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14781da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14791da177e4SLinus Torvalds 
1480b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1481515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1482b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1483515b029dSDavid Daney 				*fault_addr = va;
14841da177e4SLinus Torvalds 				return SIGBUS;
14851da177e4SLinus Torvalds 			}
1486515b029dSDavid Daney 			if (__get_user(val, va)) {
1487515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1488515b029dSDavid Daney 				*fault_addr = va;
1489515b029dSDavid Daney 				return SIGSEGV;
1490515b029dSDavid Daney 			}
14911da177e4SLinus Torvalds 			SITOREG(val, MIPSInst_FD(ir));
14921da177e4SLinus Torvalds 			break;
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 		case swxc1_op:
14953fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14961da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14971da177e4SLinus Torvalds 
1498b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
14991da177e4SLinus Torvalds 
15001da177e4SLinus Torvalds 			SIFROMREG(val, MIPSInst_FS(ir));
1501515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1502515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1503515b029dSDavid Daney 				*fault_addr = va;
1504515b029dSDavid Daney 				return SIGBUS;
1505515b029dSDavid Daney 			}
15061da177e4SLinus Torvalds 			if (put_user(val, va)) {
1507b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1508515b029dSDavid Daney 				*fault_addr = va;
1509515b029dSDavid Daney 				return SIGSEGV;
15101da177e4SLinus Torvalds 			}
15111da177e4SLinus Torvalds 			break;
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 		case madd_s_op:
15141da177e4SLinus Torvalds 			handler = fpemu_sp_madd;
15151da177e4SLinus Torvalds 			goto scoptop;
15161da177e4SLinus Torvalds 		case msub_s_op:
15171da177e4SLinus Torvalds 			handler = fpemu_sp_msub;
15181da177e4SLinus Torvalds 			goto scoptop;
15191da177e4SLinus Torvalds 		case nmadd_s_op:
15201da177e4SLinus Torvalds 			handler = fpemu_sp_nmadd;
15211da177e4SLinus Torvalds 			goto scoptop;
15221da177e4SLinus Torvalds 		case nmsub_s_op:
15231da177e4SLinus Torvalds 			handler = fpemu_sp_nmsub;
15241da177e4SLinus Torvalds 			goto scoptop;
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 		      scoptop:
15271da177e4SLinus Torvalds 			SPFROMREG(fr, MIPSInst_FR(ir));
15281da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
15291da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
15301da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
15311da177e4SLinus Torvalds 			SPTOREG(fd, MIPSInst_FD(ir));
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds 		      copcsr:
1534c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1535c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
15361da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1537c4103526SDeng-Cheng Zhu 			}
1538c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1539c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
15401da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1541c4103526SDeng-Cheng Zhu 			}
1542c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1543c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
15441da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1545c4103526SDeng-Cheng Zhu 			}
1546c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1547c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
15481da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1549c4103526SDeng-Cheng Zhu 			}
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds 			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
15521da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
15533f7cac41SRalf Baechle 				/*printk ("SIGFPE: FPU csr = %08x\n",
15541da177e4SLinus Torvalds 				   ctx->fcr31); */
15551da177e4SLinus Torvalds 				return SIGFPE;
15561da177e4SLinus Torvalds 			}
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 			break;
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 		default:
15611da177e4SLinus Torvalds 			return SIGILL;
15621da177e4SLinus Torvalds 		}
15631da177e4SLinus Torvalds 		break;
15641da177e4SLinus Torvalds 	}
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	case d_fmt:{		/* 1 */
15672209bcb1SRalf Baechle 		union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
15682209bcb1SRalf Baechle 		union ieee754dp fd, fr, fs, ft;
15693fccc015SRalf Baechle 		u64 __user *va;
15701da177e4SLinus Torvalds 		u64 val;
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
15731da177e4SLinus Torvalds 		case ldxc1_op:
15743fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15751da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15761da177e4SLinus Torvalds 
1577b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1578515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1579b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1580515b029dSDavid Daney 				*fault_addr = va;
15811da177e4SLinus Torvalds 				return SIGBUS;
15821da177e4SLinus Torvalds 			}
1583515b029dSDavid Daney 			if (__get_user(val, va)) {
1584515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1585515b029dSDavid Daney 				*fault_addr = va;
1586515b029dSDavid Daney 				return SIGSEGV;
1587515b029dSDavid Daney 			}
15881da177e4SLinus Torvalds 			DITOREG(val, MIPSInst_FD(ir));
15891da177e4SLinus Torvalds 			break;
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 		case sdxc1_op:
15923fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15931da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15941da177e4SLinus Torvalds 
1595b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
15961da177e4SLinus Torvalds 			DIFROMREG(val, MIPSInst_FS(ir));
1597515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1598b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1599515b029dSDavid Daney 				*fault_addr = va;
16001da177e4SLinus Torvalds 				return SIGBUS;
16011da177e4SLinus Torvalds 			}
1602515b029dSDavid Daney 			if (__put_user(val, va)) {
1603515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1604515b029dSDavid Daney 				*fault_addr = va;
1605515b029dSDavid Daney 				return SIGSEGV;
1606515b029dSDavid Daney 			}
16071da177e4SLinus Torvalds 			break;
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 		case madd_d_op:
16101da177e4SLinus Torvalds 			handler = fpemu_dp_madd;
16111da177e4SLinus Torvalds 			goto dcoptop;
16121da177e4SLinus Torvalds 		case msub_d_op:
16131da177e4SLinus Torvalds 			handler = fpemu_dp_msub;
16141da177e4SLinus Torvalds 			goto dcoptop;
16151da177e4SLinus Torvalds 		case nmadd_d_op:
16161da177e4SLinus Torvalds 			handler = fpemu_dp_nmadd;
16171da177e4SLinus Torvalds 			goto dcoptop;
16181da177e4SLinus Torvalds 		case nmsub_d_op:
16191da177e4SLinus Torvalds 			handler = fpemu_dp_nmsub;
16201da177e4SLinus Torvalds 			goto dcoptop;
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 		      dcoptop:
16231da177e4SLinus Torvalds 			DPFROMREG(fr, MIPSInst_FR(ir));
16241da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
16251da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
16261da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
16271da177e4SLinus Torvalds 			DPTOREG(fd, MIPSInst_FD(ir));
16281da177e4SLinus Torvalds 			goto copcsr;
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 		default:
16311da177e4SLinus Torvalds 			return SIGILL;
16321da177e4SLinus Torvalds 		}
16331da177e4SLinus Torvalds 		break;
16341da177e4SLinus Torvalds 	}
16351da177e4SLinus Torvalds 
163651061b88SDeng-Cheng Zhu 	case 0x3:
163751061b88SDeng-Cheng Zhu 		if (MIPSInst_FUNC(ir) != pfetch_op)
16381da177e4SLinus Torvalds 			return SIGILL;
163951061b88SDeng-Cheng Zhu 
16401da177e4SLinus Torvalds 		/* ignore prefx operation */
16411da177e4SLinus Torvalds 		break;
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	default:
16441da177e4SLinus Torvalds 		return SIGILL;
16451da177e4SLinus Torvalds 	}
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	return 0;
16481da177e4SLinus Torvalds }
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds /*
16531da177e4SLinus Torvalds  * Emulate a single COP1 arithmetic instruction.
16541da177e4SLinus Torvalds  */
1655eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
16561da177e4SLinus Torvalds 	mips_instruction ir)
16571da177e4SLinus Torvalds {
16581da177e4SLinus Torvalds 	int rfmt;		/* resulting format */
16591da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
16603f7cac41SRalf Baechle 	unsigned int oldrm;
16613f7cac41SRalf Baechle 	unsigned int cbit;
16621da177e4SLinus Torvalds 	unsigned cond;
16631da177e4SLinus Torvalds 	union {
16642209bcb1SRalf Baechle 		union ieee754dp d;
16652209bcb1SRalf Baechle 		union ieee754sp s;
16661da177e4SLinus Torvalds 		int w;
16671da177e4SLinus Torvalds 		s64 l;
16681da177e4SLinus Torvalds 	} rv;			/* resulting value */
16693f7cac41SRalf Baechle 	u64 bits;
16701da177e4SLinus Torvalds 
1671b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1ops);
16721da177e4SLinus Torvalds 	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
16731da177e4SLinus Torvalds 	case s_fmt: {		/* 0 */
16741da177e4SLinus Torvalds 		union {
16752209bcb1SRalf Baechle 			union ieee754sp(*b) (union ieee754sp, union ieee754sp);
16762209bcb1SRalf Baechle 			union ieee754sp(*u) (union ieee754sp);
16771da177e4SLinus Torvalds 		} handler;
16783f7cac41SRalf Baechle 		union ieee754sp fs, ft;
16791da177e4SLinus Torvalds 
16801da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
16811da177e4SLinus Torvalds 			/* binary ops */
16821da177e4SLinus Torvalds 		case fadd_op:
16831da177e4SLinus Torvalds 			handler.b = ieee754sp_add;
16841da177e4SLinus Torvalds 			goto scopbop;
16851da177e4SLinus Torvalds 		case fsub_op:
16861da177e4SLinus Torvalds 			handler.b = ieee754sp_sub;
16871da177e4SLinus Torvalds 			goto scopbop;
16881da177e4SLinus Torvalds 		case fmul_op:
16891da177e4SLinus Torvalds 			handler.b = ieee754sp_mul;
16901da177e4SLinus Torvalds 			goto scopbop;
16911da177e4SLinus Torvalds 		case fdiv_op:
16921da177e4SLinus Torvalds 			handler.b = ieee754sp_div;
16931da177e4SLinus Torvalds 			goto scopbop;
16941da177e4SLinus Torvalds 
16951da177e4SLinus Torvalds 			/* unary  ops */
16961da177e4SLinus Torvalds 		case fsqrt_op:
16972d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
169808a07904SRalf Baechle 				return SIGILL;
169908a07904SRalf Baechle 
17001da177e4SLinus Torvalds 			handler.u = ieee754sp_sqrt;
17011da177e4SLinus Torvalds 			goto scopuop;
17023f7cac41SRalf Baechle 
170308a07904SRalf Baechle 		/*
170408a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
170508a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
170608a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
170708a07904SRalf Baechle 		 */
17081da177e4SLinus Torvalds 		case frsqrt_op:
17092d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
171008a07904SRalf Baechle 				return SIGILL;
171108a07904SRalf Baechle 
17121da177e4SLinus Torvalds 			handler.u = fpemu_sp_rsqrt;
17131da177e4SLinus Torvalds 			goto scopuop;
17143f7cac41SRalf Baechle 
17151da177e4SLinus Torvalds 		case frecip_op:
17162d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
171708a07904SRalf Baechle 				return SIGILL;
171808a07904SRalf Baechle 
17191da177e4SLinus Torvalds 			handler.u = fpemu_sp_recip;
17201da177e4SLinus Torvalds 			goto scopuop;
172108a07904SRalf Baechle 
17221da177e4SLinus Torvalds 		case fmovc_op:
172308a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
172408a07904SRalf Baechle 				return SIGILL;
172508a07904SRalf Baechle 
17261da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
17271da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
17281da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
17291da177e4SLinus Torvalds 				return 0;
17301da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17311da177e4SLinus Torvalds 			break;
17323f7cac41SRalf Baechle 
17331da177e4SLinus Torvalds 		case fmovz_op:
173408a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
173508a07904SRalf Baechle 				return SIGILL;
173608a07904SRalf Baechle 
17371da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
17381da177e4SLinus Torvalds 				return 0;
17391da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17401da177e4SLinus Torvalds 			break;
17413f7cac41SRalf Baechle 
17421da177e4SLinus Torvalds 		case fmovn_op:
174308a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
174408a07904SRalf Baechle 				return SIGILL;
174508a07904SRalf Baechle 
17461da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
17471da177e4SLinus Torvalds 				return 0;
17481da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
17491da177e4SLinus Torvalds 			break;
17503f7cac41SRalf Baechle 
175167613f02SMarkos Chandras 		case fseleqz_op:
175267613f02SMarkos Chandras 			if (!cpu_has_mips_r6)
175367613f02SMarkos Chandras 				return SIGILL;
175467613f02SMarkos Chandras 
175567613f02SMarkos Chandras 			SPFROMREG(rv.s, MIPSInst_FT(ir));
175667613f02SMarkos Chandras 			if (rv.w & 0x1)
175767613f02SMarkos Chandras 				rv.w = 0;
175867613f02SMarkos Chandras 			else
175967613f02SMarkos Chandras 				SPFROMREG(rv.s, MIPSInst_FS(ir));
176067613f02SMarkos Chandras 			break;
176167613f02SMarkos Chandras 
1762130fe357SMarkos Chandras 		case fselnez_op:
1763130fe357SMarkos Chandras 			if (!cpu_has_mips_r6)
1764130fe357SMarkos Chandras 				return SIGILL;
1765130fe357SMarkos Chandras 
1766130fe357SMarkos Chandras 			SPFROMREG(rv.s, MIPSInst_FT(ir));
1767130fe357SMarkos Chandras 			if (rv.w & 0x1)
1768130fe357SMarkos Chandras 				SPFROMREG(rv.s, MIPSInst_FS(ir));
1769130fe357SMarkos Chandras 			else
1770130fe357SMarkos Chandras 				rv.w = 0;
1771130fe357SMarkos Chandras 			break;
1772130fe357SMarkos Chandras 
1773e24c3becSMarkos Chandras 		case fmaddf_op: {
1774e24c3becSMarkos Chandras 			union ieee754sp ft, fs, fd;
1775e24c3becSMarkos Chandras 
1776e24c3becSMarkos Chandras 			if (!cpu_has_mips_r6)
1777e24c3becSMarkos Chandras 				return SIGILL;
1778e24c3becSMarkos Chandras 
1779e24c3becSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1780e24c3becSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1781e24c3becSMarkos Chandras 			SPFROMREG(fd, MIPSInst_FD(ir));
1782e24c3becSMarkos Chandras 			rv.s = ieee754sp_maddf(fd, fs, ft);
1783e24c3becSMarkos Chandras 			break;
1784e24c3becSMarkos Chandras 		}
1785e24c3becSMarkos Chandras 
178683d43305SMarkos Chandras 		case fmsubf_op: {
178783d43305SMarkos Chandras 			union ieee754sp ft, fs, fd;
178883d43305SMarkos Chandras 
178983d43305SMarkos Chandras 			if (!cpu_has_mips_r6)
179083d43305SMarkos Chandras 				return SIGILL;
179183d43305SMarkos Chandras 
179283d43305SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
179383d43305SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
179483d43305SMarkos Chandras 			SPFROMREG(fd, MIPSInst_FD(ir));
179583d43305SMarkos Chandras 			rv.s = ieee754sp_msubf(fd, fs, ft);
179683d43305SMarkos Chandras 			break;
179783d43305SMarkos Chandras 		}
179883d43305SMarkos Chandras 
1799400bd2e4SMarkos Chandras 		case frint_op: {
1800400bd2e4SMarkos Chandras 			union ieee754sp fs;
1801400bd2e4SMarkos Chandras 
1802400bd2e4SMarkos Chandras 			if (!cpu_has_mips_r6)
1803400bd2e4SMarkos Chandras 				return SIGILL;
1804400bd2e4SMarkos Chandras 
1805400bd2e4SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1806400bd2e4SMarkos Chandras 			rv.l = ieee754sp_tlong(fs);
1807400bd2e4SMarkos Chandras 			rv.s = ieee754sp_flong(rv.l);
1808400bd2e4SMarkos Chandras 			goto copcsr;
1809400bd2e4SMarkos Chandras 		}
1810400bd2e4SMarkos Chandras 
181138db37baSMarkos Chandras 		case fclass_op: {
181238db37baSMarkos Chandras 			union ieee754sp fs;
181338db37baSMarkos Chandras 
181438db37baSMarkos Chandras 			if (!cpu_has_mips_r6)
181538db37baSMarkos Chandras 				return SIGILL;
181638db37baSMarkos Chandras 
181738db37baSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
181838db37baSMarkos Chandras 			rv.w = ieee754sp_2008class(fs);
181938db37baSMarkos Chandras 			rfmt = w_fmt;
182038db37baSMarkos Chandras 			break;
182138db37baSMarkos Chandras 		}
182238db37baSMarkos Chandras 
18234e9561b2SMarkos Chandras 		case fmin_op: {
18244e9561b2SMarkos Chandras 			union ieee754sp fs, ft;
18254e9561b2SMarkos Chandras 
18264e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
18274e9561b2SMarkos Chandras 				return SIGILL;
18284e9561b2SMarkos Chandras 
18294e9561b2SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
18304e9561b2SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18314e9561b2SMarkos Chandras 			rv.s = ieee754sp_fmin(fs, ft);
18324e9561b2SMarkos Chandras 			break;
18334e9561b2SMarkos Chandras 		}
18344e9561b2SMarkos Chandras 
18354e9561b2SMarkos Chandras 		case fmina_op: {
18364e9561b2SMarkos Chandras 			union ieee754sp fs, ft;
18374e9561b2SMarkos Chandras 
18384e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
18394e9561b2SMarkos Chandras 				return SIGILL;
18404e9561b2SMarkos Chandras 
18414e9561b2SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
18424e9561b2SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
18434e9561b2SMarkos Chandras 			rv.s = ieee754sp_fmina(fs, ft);
18444e9561b2SMarkos Chandras 			break;
18454e9561b2SMarkos Chandras 		}
18464e9561b2SMarkos Chandras 
1847a79f5f9bSMarkos Chandras 		case fmax_op: {
1848a79f5f9bSMarkos Chandras 			union ieee754sp fs, ft;
1849a79f5f9bSMarkos Chandras 
1850a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
1851a79f5f9bSMarkos Chandras 				return SIGILL;
1852a79f5f9bSMarkos Chandras 
1853a79f5f9bSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1854a79f5f9bSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1855a79f5f9bSMarkos Chandras 			rv.s = ieee754sp_fmax(fs, ft);
1856a79f5f9bSMarkos Chandras 			break;
1857a79f5f9bSMarkos Chandras 		}
1858a79f5f9bSMarkos Chandras 
1859a79f5f9bSMarkos Chandras 		case fmaxa_op: {
1860a79f5f9bSMarkos Chandras 			union ieee754sp fs, ft;
1861a79f5f9bSMarkos Chandras 
1862a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
1863a79f5f9bSMarkos Chandras 				return SIGILL;
1864a79f5f9bSMarkos Chandras 
1865a79f5f9bSMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
1866a79f5f9bSMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
1867a79f5f9bSMarkos Chandras 			rv.s = ieee754sp_fmaxa(fs, ft);
1868a79f5f9bSMarkos Chandras 			break;
1869a79f5f9bSMarkos Chandras 		}
1870a79f5f9bSMarkos Chandras 
18711da177e4SLinus Torvalds 		case fabs_op:
18721da177e4SLinus Torvalds 			handler.u = ieee754sp_abs;
18731da177e4SLinus Torvalds 			goto scopuop;
18743f7cac41SRalf Baechle 
18751da177e4SLinus Torvalds 		case fneg_op:
18761da177e4SLinus Torvalds 			handler.u = ieee754sp_neg;
18771da177e4SLinus Torvalds 			goto scopuop;
18783f7cac41SRalf Baechle 
18791da177e4SLinus Torvalds 		case fmov_op:
18801da177e4SLinus Torvalds 			/* an easy one */
18811da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
18821da177e4SLinus Torvalds 			goto copcsr;
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 			/* binary op on handler */
18851da177e4SLinus Torvalds scopbop:
18861da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
18871da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 			rv.s = (*handler.b) (fs, ft);
18901da177e4SLinus Torvalds 			goto copcsr;
18911da177e4SLinus Torvalds scopuop:
18921da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
18931da177e4SLinus Torvalds 			rv.s = (*handler.u) (fs);
18941da177e4SLinus Torvalds 			goto copcsr;
18951da177e4SLinus Torvalds copcsr:
1896c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1897c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
18981da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1899c4103526SDeng-Cheng Zhu 			}
1900c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1901c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
19021da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1903c4103526SDeng-Cheng Zhu 			}
1904c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1905c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
19061da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1907c4103526SDeng-Cheng Zhu 			}
1908c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1909c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
19101da177e4SLinus Torvalds 				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1911c4103526SDeng-Cheng Zhu 			}
1912c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1913c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
19141da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1915c4103526SDeng-Cheng Zhu 			}
19161da177e4SLinus Torvalds 			break;
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds 			/* unary conv ops */
19191da177e4SLinus Torvalds 		case fcvts_op:
19201da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
19211da177e4SLinus Torvalds 
19223f7cac41SRalf Baechle 		case fcvtd_op:
19231da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19241da177e4SLinus Torvalds 			rv.d = ieee754dp_fsp(fs);
19251da177e4SLinus Torvalds 			rfmt = d_fmt;
19261da177e4SLinus Torvalds 			goto copcsr;
19271da177e4SLinus Torvalds 
19283f7cac41SRalf Baechle 		case fcvtw_op:
19291da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19301da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
19311da177e4SLinus Torvalds 			rfmt = w_fmt;
19321da177e4SLinus Torvalds 			goto copcsr;
19331da177e4SLinus Torvalds 
19341da177e4SLinus Torvalds 		case fround_op:
19351da177e4SLinus Torvalds 		case ftrunc_op:
19361da177e4SLinus Torvalds 		case fceil_op:
19373f7cac41SRalf Baechle 		case ffloor_op:
19382d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_2_3_4_5_r)
193908a07904SRalf Baechle 				return SIGILL;
194008a07904SRalf Baechle 
19413f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
19421da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19432cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
19441da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
19451da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
19461da177e4SLinus Torvalds 			rfmt = w_fmt;
19471da177e4SLinus Torvalds 			goto copcsr;
19481da177e4SLinus Torvalds 
19493f7cac41SRalf Baechle 		case fcvtl_op:
19502d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
195108a07904SRalf Baechle 				return SIGILL;
195208a07904SRalf Baechle 
19531da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19541da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
19551da177e4SLinus Torvalds 			rfmt = l_fmt;
19561da177e4SLinus Torvalds 			goto copcsr;
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 		case froundl_op:
19591da177e4SLinus Torvalds 		case ftruncl_op:
19601da177e4SLinus Torvalds 		case fceill_op:
19613f7cac41SRalf Baechle 		case ffloorl_op:
19622d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
196308a07904SRalf Baechle 				return SIGILL;
196408a07904SRalf Baechle 
19653f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
19661da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19672cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
19681da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
19691da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
19701da177e4SLinus Torvalds 			rfmt = l_fmt;
19711da177e4SLinus Torvalds 			goto copcsr;
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 		default:
1974f8c3c671SMarkos Chandras 			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
19751da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
19762209bcb1SRalf Baechle 				union ieee754sp fs, ft;
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
19791da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
19801da177e4SLinus Torvalds 				rv.w = ieee754sp_cmp(fs, ft,
19811da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
19821da177e4SLinus Torvalds 				rfmt = -1;
19831da177e4SLinus Torvalds 				if ((cmpop & 0x8) && ieee754_cxtest
19841da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
19851da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
19861da177e4SLinus Torvalds 				else
19871da177e4SLinus Torvalds 					goto copcsr;
19881da177e4SLinus Torvalds 
19893f7cac41SRalf Baechle 			} else
19901da177e4SLinus Torvalds 				return SIGILL;
19911da177e4SLinus Torvalds 			break;
19921da177e4SLinus Torvalds 		}
19931da177e4SLinus Torvalds 		break;
19941da177e4SLinus Torvalds 	}
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	case d_fmt: {
19973f7cac41SRalf Baechle 		union ieee754dp fs, ft;
19981da177e4SLinus Torvalds 		union {
19992209bcb1SRalf Baechle 			union ieee754dp(*b) (union ieee754dp, union ieee754dp);
20002209bcb1SRalf Baechle 			union ieee754dp(*u) (union ieee754dp);
20011da177e4SLinus Torvalds 		} handler;
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
20041da177e4SLinus Torvalds 			/* binary ops */
20051da177e4SLinus Torvalds 		case fadd_op:
20061da177e4SLinus Torvalds 			handler.b = ieee754dp_add;
20071da177e4SLinus Torvalds 			goto dcopbop;
20081da177e4SLinus Torvalds 		case fsub_op:
20091da177e4SLinus Torvalds 			handler.b = ieee754dp_sub;
20101da177e4SLinus Torvalds 			goto dcopbop;
20111da177e4SLinus Torvalds 		case fmul_op:
20121da177e4SLinus Torvalds 			handler.b = ieee754dp_mul;
20131da177e4SLinus Torvalds 			goto dcopbop;
20141da177e4SLinus Torvalds 		case fdiv_op:
20151da177e4SLinus Torvalds 			handler.b = ieee754dp_div;
20161da177e4SLinus Torvalds 			goto dcopbop;
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 			/* unary  ops */
20191da177e4SLinus Torvalds 		case fsqrt_op:
202008a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
202108a07904SRalf Baechle 				return SIGILL;
202208a07904SRalf Baechle 
20231da177e4SLinus Torvalds 			handler.u = ieee754dp_sqrt;
20241da177e4SLinus Torvalds 			goto dcopuop;
202508a07904SRalf Baechle 		/*
202608a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
202708a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
202808a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
202908a07904SRalf Baechle 		 */
20301da177e4SLinus Torvalds 		case frsqrt_op:
20312d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
203208a07904SRalf Baechle 				return SIGILL;
203308a07904SRalf Baechle 
20341da177e4SLinus Torvalds 			handler.u = fpemu_dp_rsqrt;
20351da177e4SLinus Torvalds 			goto dcopuop;
20361da177e4SLinus Torvalds 		case frecip_op:
20372d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_4_5_64_r2_r6)
203808a07904SRalf Baechle 				return SIGILL;
203908a07904SRalf Baechle 
20401da177e4SLinus Torvalds 			handler.u = fpemu_dp_recip;
20411da177e4SLinus Torvalds 			goto dcopuop;
20421da177e4SLinus Torvalds 		case fmovc_op:
204308a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
204408a07904SRalf Baechle 				return SIGILL;
204508a07904SRalf Baechle 
20461da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
20471da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
20481da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
20491da177e4SLinus Torvalds 				return 0;
20501da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
20511da177e4SLinus Torvalds 			break;
20521da177e4SLinus Torvalds 		case fmovz_op:
205308a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
205408a07904SRalf Baechle 				return SIGILL;
205508a07904SRalf Baechle 
20561da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
20571da177e4SLinus Torvalds 				return 0;
20581da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
20591da177e4SLinus Torvalds 			break;
20601da177e4SLinus Torvalds 		case fmovn_op:
206108a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
206208a07904SRalf Baechle 				return SIGILL;
206308a07904SRalf Baechle 
20641da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
20651da177e4SLinus Torvalds 				return 0;
20661da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
20671da177e4SLinus Torvalds 			break;
206867613f02SMarkos Chandras 
206967613f02SMarkos Chandras 		case fseleqz_op:
207067613f02SMarkos Chandras 			if (!cpu_has_mips_r6)
207167613f02SMarkos Chandras 				return SIGILL;
207267613f02SMarkos Chandras 
207367613f02SMarkos Chandras 			DPFROMREG(rv.d, MIPSInst_FT(ir));
207467613f02SMarkos Chandras 			if (rv.l & 0x1)
207567613f02SMarkos Chandras 				rv.l = 0;
207667613f02SMarkos Chandras 			else
207767613f02SMarkos Chandras 				DPFROMREG(rv.d, MIPSInst_FS(ir));
207867613f02SMarkos Chandras 			break;
207967613f02SMarkos Chandras 
2080130fe357SMarkos Chandras 		case fselnez_op:
2081130fe357SMarkos Chandras 			if (!cpu_has_mips_r6)
2082130fe357SMarkos Chandras 				return SIGILL;
2083130fe357SMarkos Chandras 
2084130fe357SMarkos Chandras 			DPFROMREG(rv.d, MIPSInst_FT(ir));
2085130fe357SMarkos Chandras 			if (rv.l & 0x1)
2086130fe357SMarkos Chandras 				DPFROMREG(rv.d, MIPSInst_FS(ir));
2087130fe357SMarkos Chandras 			else
2088130fe357SMarkos Chandras 				rv.l = 0;
2089130fe357SMarkos Chandras 			break;
2090130fe357SMarkos Chandras 
2091e24c3becSMarkos Chandras 		case fmaddf_op: {
2092e24c3becSMarkos Chandras 			union ieee754dp ft, fs, fd;
2093e24c3becSMarkos Chandras 
2094e24c3becSMarkos Chandras 			if (!cpu_has_mips_r6)
2095e24c3becSMarkos Chandras 				return SIGILL;
2096e24c3becSMarkos Chandras 
2097e24c3becSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2098e24c3becSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2099e24c3becSMarkos Chandras 			DPFROMREG(fd, MIPSInst_FD(ir));
2100e24c3becSMarkos Chandras 			rv.d = ieee754dp_maddf(fd, fs, ft);
2101e24c3becSMarkos Chandras 			break;
2102e24c3becSMarkos Chandras 		}
2103e24c3becSMarkos Chandras 
210483d43305SMarkos Chandras 		case fmsubf_op: {
210583d43305SMarkos Chandras 			union ieee754dp ft, fs, fd;
210683d43305SMarkos Chandras 
210783d43305SMarkos Chandras 			if (!cpu_has_mips_r6)
210883d43305SMarkos Chandras 				return SIGILL;
210983d43305SMarkos Chandras 
211083d43305SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
211183d43305SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
211283d43305SMarkos Chandras 			DPFROMREG(fd, MIPSInst_FD(ir));
211383d43305SMarkos Chandras 			rv.d = ieee754dp_msubf(fd, fs, ft);
211483d43305SMarkos Chandras 			break;
211583d43305SMarkos Chandras 		}
211683d43305SMarkos Chandras 
2117400bd2e4SMarkos Chandras 		case frint_op: {
2118400bd2e4SMarkos Chandras 			union ieee754dp fs;
2119400bd2e4SMarkos Chandras 
2120400bd2e4SMarkos Chandras 			if (!cpu_has_mips_r6)
2121400bd2e4SMarkos Chandras 				return SIGILL;
2122400bd2e4SMarkos Chandras 
2123400bd2e4SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2124400bd2e4SMarkos Chandras 			rv.l = ieee754dp_tlong(fs);
2125400bd2e4SMarkos Chandras 			rv.d = ieee754dp_flong(rv.l);
2126400bd2e4SMarkos Chandras 			goto copcsr;
2127400bd2e4SMarkos Chandras 		}
2128400bd2e4SMarkos Chandras 
212938db37baSMarkos Chandras 		case fclass_op: {
213038db37baSMarkos Chandras 			union ieee754dp fs;
213138db37baSMarkos Chandras 
213238db37baSMarkos Chandras 			if (!cpu_has_mips_r6)
213338db37baSMarkos Chandras 				return SIGILL;
213438db37baSMarkos Chandras 
213538db37baSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
213638db37baSMarkos Chandras 			rv.w = ieee754dp_2008class(fs);
213738db37baSMarkos Chandras 			rfmt = w_fmt;
213838db37baSMarkos Chandras 			break;
213938db37baSMarkos Chandras 		}
214038db37baSMarkos Chandras 
21414e9561b2SMarkos Chandras 		case fmin_op: {
21424e9561b2SMarkos Chandras 			union ieee754dp fs, ft;
21434e9561b2SMarkos Chandras 
21444e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
21454e9561b2SMarkos Chandras 				return SIGILL;
21464e9561b2SMarkos Chandras 
21474e9561b2SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
21484e9561b2SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
21494e9561b2SMarkos Chandras 			rv.d = ieee754dp_fmin(fs, ft);
21504e9561b2SMarkos Chandras 			break;
21514e9561b2SMarkos Chandras 		}
21524e9561b2SMarkos Chandras 
21534e9561b2SMarkos Chandras 		case fmina_op: {
21544e9561b2SMarkos Chandras 			union ieee754dp fs, ft;
21554e9561b2SMarkos Chandras 
21564e9561b2SMarkos Chandras 			if (!cpu_has_mips_r6)
21574e9561b2SMarkos Chandras 				return SIGILL;
21584e9561b2SMarkos Chandras 
21594e9561b2SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
21604e9561b2SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
21614e9561b2SMarkos Chandras 			rv.d = ieee754dp_fmina(fs, ft);
21624e9561b2SMarkos Chandras 			break;
21634e9561b2SMarkos Chandras 		}
21644e9561b2SMarkos Chandras 
2165a79f5f9bSMarkos Chandras 		case fmax_op: {
2166a79f5f9bSMarkos Chandras 			union ieee754dp fs, ft;
2167a79f5f9bSMarkos Chandras 
2168a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
2169a79f5f9bSMarkos Chandras 				return SIGILL;
2170a79f5f9bSMarkos Chandras 
2171a79f5f9bSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2172a79f5f9bSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2173a79f5f9bSMarkos Chandras 			rv.d = ieee754dp_fmax(fs, ft);
2174a79f5f9bSMarkos Chandras 			break;
2175a79f5f9bSMarkos Chandras 		}
2176a79f5f9bSMarkos Chandras 
2177a79f5f9bSMarkos Chandras 		case fmaxa_op: {
2178a79f5f9bSMarkos Chandras 			union ieee754dp fs, ft;
2179a79f5f9bSMarkos Chandras 
2180a79f5f9bSMarkos Chandras 			if (!cpu_has_mips_r6)
2181a79f5f9bSMarkos Chandras 				return SIGILL;
2182a79f5f9bSMarkos Chandras 
2183a79f5f9bSMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2184a79f5f9bSMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2185a79f5f9bSMarkos Chandras 			rv.d = ieee754dp_fmaxa(fs, ft);
2186a79f5f9bSMarkos Chandras 			break;
2187a79f5f9bSMarkos Chandras 		}
2188a79f5f9bSMarkos Chandras 
21891da177e4SLinus Torvalds 		case fabs_op:
21901da177e4SLinus Torvalds 			handler.u = ieee754dp_abs;
21911da177e4SLinus Torvalds 			goto dcopuop;
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 		case fneg_op:
21941da177e4SLinus Torvalds 			handler.u = ieee754dp_neg;
21951da177e4SLinus Torvalds 			goto dcopuop;
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 		case fmov_op:
21981da177e4SLinus Torvalds 			/* an easy one */
21991da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
22001da177e4SLinus Torvalds 			goto copcsr;
22011da177e4SLinus Torvalds 
22021da177e4SLinus Torvalds 			/* binary op on handler */
22033f7cac41SRalf Baechle dcopbop:
22041da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22051da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds 			rv.d = (*handler.b) (fs, ft);
22081da177e4SLinus Torvalds 			goto copcsr;
22093f7cac41SRalf Baechle dcopuop:
22101da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22111da177e4SLinus Torvalds 			rv.d = (*handler.u) (fs);
22121da177e4SLinus Torvalds 			goto copcsr;
22131da177e4SLinus Torvalds 
22143f7cac41SRalf Baechle 		/*
22153f7cac41SRalf Baechle 		 * unary conv ops
22163f7cac41SRalf Baechle 		 */
22173f7cac41SRalf Baechle 		case fcvts_op:
22181da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22191da177e4SLinus Torvalds 			rv.s = ieee754sp_fdp(fs);
22201da177e4SLinus Torvalds 			rfmt = s_fmt;
22211da177e4SLinus Torvalds 			goto copcsr;
22223f7cac41SRalf Baechle 
22231da177e4SLinus Torvalds 		case fcvtd_op:
22241da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
22251da177e4SLinus Torvalds 
22263f7cac41SRalf Baechle 		case fcvtw_op:
22271da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22281da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);	/* wrong */
22291da177e4SLinus Torvalds 			rfmt = w_fmt;
22301da177e4SLinus Torvalds 			goto copcsr;
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 		case fround_op:
22331da177e4SLinus Torvalds 		case ftrunc_op:
22341da177e4SLinus Torvalds 		case fceil_op:
22353f7cac41SRalf Baechle 		case ffloor_op:
223608a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
223708a07904SRalf Baechle 				return SIGILL;
223808a07904SRalf Baechle 
22393f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
22401da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22412cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
22421da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);
22431da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
22441da177e4SLinus Torvalds 			rfmt = w_fmt;
22451da177e4SLinus Torvalds 			goto copcsr;
22461da177e4SLinus Torvalds 
22473f7cac41SRalf Baechle 		case fcvtl_op:
22482d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
224908a07904SRalf Baechle 				return SIGILL;
225008a07904SRalf Baechle 
22511da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22521da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
22531da177e4SLinus Torvalds 			rfmt = l_fmt;
22541da177e4SLinus Torvalds 			goto copcsr;
22551da177e4SLinus Torvalds 
22561da177e4SLinus Torvalds 		case froundl_op:
22571da177e4SLinus Torvalds 		case ftruncl_op:
22581da177e4SLinus Torvalds 		case fceill_op:
22593f7cac41SRalf Baechle 		case ffloorl_op:
22602d83fea7SMaciej W. Rozycki 			if (!cpu_has_mips_3_4_5_64_r2_r6)
226108a07904SRalf Baechle 				return SIGILL;
226208a07904SRalf Baechle 
22633f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
22641da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
22652cfcf8a8SMaciej W. Rozycki 			ieee754_csr.rm = MIPSInst_FUNC(ir);
22661da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
22671da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
22681da177e4SLinus Torvalds 			rfmt = l_fmt;
22691da177e4SLinus Torvalds 			goto copcsr;
22701da177e4SLinus Torvalds 
22711da177e4SLinus Torvalds 		default:
2272f8c3c671SMarkos Chandras 			if (!NO_R6EMU && MIPSInst_FUNC(ir) >= fcmp_op) {
22731da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
22742209bcb1SRalf Baechle 				union ieee754dp fs, ft;
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
22771da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
22781da177e4SLinus Torvalds 				rv.w = ieee754dp_cmp(fs, ft,
22791da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
22801da177e4SLinus Torvalds 				rfmt = -1;
22811da177e4SLinus Torvalds 				if ((cmpop & 0x8)
22821da177e4SLinus Torvalds 					&&
22831da177e4SLinus Torvalds 					ieee754_cxtest
22841da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
22851da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
22861da177e4SLinus Torvalds 				else
22871da177e4SLinus Torvalds 					goto copcsr;
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds 			}
22901da177e4SLinus Torvalds 			else {
22911da177e4SLinus Torvalds 				return SIGILL;
22921da177e4SLinus Torvalds 			}
22931da177e4SLinus Torvalds 			break;
22941da177e4SLinus Torvalds 		}
22951da177e4SLinus Torvalds 		break;
2296bbdd8147SMarkos Chandras 	}
22971da177e4SLinus Torvalds 
2298bbdd8147SMarkos Chandras 	case w_fmt: {
2299bbdd8147SMarkos Chandras 		union ieee754dp fs;
2300bbdd8147SMarkos Chandras 
23011da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
23021da177e4SLinus Torvalds 		case fcvts_op:
23031da177e4SLinus Torvalds 			/* convert word to single precision real */
23041da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
23051da177e4SLinus Torvalds 			rv.s = ieee754sp_fint(fs.bits);
23061da177e4SLinus Torvalds 			rfmt = s_fmt;
23071da177e4SLinus Torvalds 			goto copcsr;
23081da177e4SLinus Torvalds 		case fcvtd_op:
23091da177e4SLinus Torvalds 			/* convert word to double precision real */
23101da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
23111da177e4SLinus Torvalds 			rv.d = ieee754dp_fint(fs.bits);
23121da177e4SLinus Torvalds 			rfmt = d_fmt;
23131da177e4SLinus Torvalds 			goto copcsr;
2314f8c3c671SMarkos Chandras 		default: {
2315f8c3c671SMarkos Chandras 			/* Emulating the new CMP.condn.fmt R6 instruction */
2316f8c3c671SMarkos Chandras #define CMPOP_MASK	0x7
2317f8c3c671SMarkos Chandras #define SIGN_BIT	(0x1 << 3)
2318f8c3c671SMarkos Chandras #define PREDICATE_BIT	(0x1 << 4)
2319f8c3c671SMarkos Chandras 
2320f8c3c671SMarkos Chandras 			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2321f8c3c671SMarkos Chandras 			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2322f8c3c671SMarkos Chandras 			union ieee754sp fs, ft;
2323f8c3c671SMarkos Chandras 
2324f8c3c671SMarkos Chandras 			/* This is an R6 only instruction */
2325f8c3c671SMarkos Chandras 			if (!cpu_has_mips_r6 ||
2326f8c3c671SMarkos Chandras 			    (MIPSInst_FUNC(ir) & 0x20))
2327f8c3c671SMarkos Chandras 				return SIGILL;
2328f8c3c671SMarkos Chandras 
2329f8c3c671SMarkos Chandras 			/* fmt is w_fmt for single precision so fix it */
2330f8c3c671SMarkos Chandras 			rfmt = s_fmt;
2331f8c3c671SMarkos Chandras 			/* default to false */
2332f8c3c671SMarkos Chandras 			rv.w = 0;
2333f8c3c671SMarkos Chandras 
2334f8c3c671SMarkos Chandras 			/* CMP.condn.S */
2335f8c3c671SMarkos Chandras 			SPFROMREG(fs, MIPSInst_FS(ir));
2336f8c3c671SMarkos Chandras 			SPFROMREG(ft, MIPSInst_FT(ir));
2337f8c3c671SMarkos Chandras 
2338f8c3c671SMarkos Chandras 			/* positive predicates */
2339f8c3c671SMarkos Chandras 			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2340f8c3c671SMarkos Chandras 				if (ieee754sp_cmp(fs, ft, cmptab[cmpop],
2341f8c3c671SMarkos Chandras 						  sig))
2342f8c3c671SMarkos Chandras 				    rv.w = -1; /* true, all 1s */
2343f8c3c671SMarkos Chandras 				if ((sig) &&
2344f8c3c671SMarkos Chandras 				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2345f8c3c671SMarkos Chandras 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2346f8c3c671SMarkos Chandras 				else
2347f8c3c671SMarkos Chandras 					goto copcsr;
2348f8c3c671SMarkos Chandras 			} else {
2349f8c3c671SMarkos Chandras 				/* negative predicates */
2350f8c3c671SMarkos Chandras 				switch (cmpop) {
2351f8c3c671SMarkos Chandras 				case 1:
2352f8c3c671SMarkos Chandras 				case 2:
2353f8c3c671SMarkos Chandras 				case 3:
2354f8c3c671SMarkos Chandras 					if (ieee754sp_cmp(fs, ft,
2355f8c3c671SMarkos Chandras 							  negative_cmptab[cmpop],
2356f8c3c671SMarkos Chandras 							  sig))
2357f8c3c671SMarkos Chandras 						rv.w = -1; /* true, all 1s */
2358f8c3c671SMarkos Chandras 					if (sig &&
2359f8c3c671SMarkos Chandras 					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2360f8c3c671SMarkos Chandras 						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2361f8c3c671SMarkos Chandras 					else
2362f8c3c671SMarkos Chandras 						goto copcsr;
2363f8c3c671SMarkos Chandras 					break;
23641da177e4SLinus Torvalds 				default:
2365f8c3c671SMarkos Chandras 					/* Reserved R6 ops */
2366f8c3c671SMarkos Chandras 					pr_err("Reserved MIPS R6 CMP.condn.S operation\n");
23671da177e4SLinus Torvalds 					return SIGILL;
23681da177e4SLinus Torvalds 				}
2369f8c3c671SMarkos Chandras 			}
23701da177e4SLinus Torvalds 			break;
23711da177e4SLinus Torvalds 			}
2372f8c3c671SMarkos Chandras 		}
2373f8c3c671SMarkos Chandras 	}
23741da177e4SLinus Torvalds 
23753f7cac41SRalf Baechle 	case l_fmt:
237608a07904SRalf Baechle 
23772d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
237808a07904SRalf Baechle 			return SIGILL;
237908a07904SRalf Baechle 
2380bbd426f5SPaul Burton 		DIFROMREG(bits, MIPSInst_FS(ir));
2381bbd426f5SPaul Burton 
23821da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
23831da177e4SLinus Torvalds 		case fcvts_op:
23841da177e4SLinus Torvalds 			/* convert long to single precision real */
2385bbd426f5SPaul Burton 			rv.s = ieee754sp_flong(bits);
23861da177e4SLinus Torvalds 			rfmt = s_fmt;
23871da177e4SLinus Torvalds 			goto copcsr;
23881da177e4SLinus Torvalds 		case fcvtd_op:
23891da177e4SLinus Torvalds 			/* convert long to double precision real */
2390bbd426f5SPaul Burton 			rv.d = ieee754dp_flong(bits);
23911da177e4SLinus Torvalds 			rfmt = d_fmt;
23921da177e4SLinus Torvalds 			goto copcsr;
2393f8c3c671SMarkos Chandras 		default: {
2394f8c3c671SMarkos Chandras 			/* Emulating the new CMP.condn.fmt R6 instruction */
2395f8c3c671SMarkos Chandras 			int cmpop = MIPSInst_FUNC(ir) & CMPOP_MASK;
2396f8c3c671SMarkos Chandras 			int sig = MIPSInst_FUNC(ir) & SIGN_BIT;
2397f8c3c671SMarkos Chandras 			union ieee754dp fs, ft;
2398f8c3c671SMarkos Chandras 
2399f8c3c671SMarkos Chandras 			if (!cpu_has_mips_r6 ||
2400f8c3c671SMarkos Chandras 			    (MIPSInst_FUNC(ir) & 0x20))
2401f8c3c671SMarkos Chandras 				return SIGILL;
2402f8c3c671SMarkos Chandras 
2403f8c3c671SMarkos Chandras 			/* fmt is l_fmt for double precision so fix it */
2404f8c3c671SMarkos Chandras 			rfmt = d_fmt;
2405f8c3c671SMarkos Chandras 			/* default to false */
2406f8c3c671SMarkos Chandras 			rv.l = 0;
2407f8c3c671SMarkos Chandras 
2408f8c3c671SMarkos Chandras 			/* CMP.condn.D */
2409f8c3c671SMarkos Chandras 			DPFROMREG(fs, MIPSInst_FS(ir));
2410f8c3c671SMarkos Chandras 			DPFROMREG(ft, MIPSInst_FT(ir));
2411f8c3c671SMarkos Chandras 
2412f8c3c671SMarkos Chandras 			/* positive predicates */
2413f8c3c671SMarkos Chandras 			if (!(MIPSInst_FUNC(ir) & PREDICATE_BIT)) {
2414f8c3c671SMarkos Chandras 				if (ieee754dp_cmp(fs, ft,
2415f8c3c671SMarkos Chandras 						  cmptab[cmpop], sig))
2416f8c3c671SMarkos Chandras 				    rv.l = -1LL; /* true, all 1s */
2417f8c3c671SMarkos Chandras 				if (sig &&
2418f8c3c671SMarkos Chandras 				    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2419f8c3c671SMarkos Chandras 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2420f8c3c671SMarkos Chandras 				else
2421f8c3c671SMarkos Chandras 					goto copcsr;
2422f8c3c671SMarkos Chandras 			} else {
2423f8c3c671SMarkos Chandras 				/* negative predicates */
2424f8c3c671SMarkos Chandras 				switch (cmpop) {
2425f8c3c671SMarkos Chandras 				case 1:
2426f8c3c671SMarkos Chandras 				case 2:
2427f8c3c671SMarkos Chandras 				case 3:
2428f8c3c671SMarkos Chandras 					if (ieee754dp_cmp(fs, ft,
2429f8c3c671SMarkos Chandras 							  negative_cmptab[cmpop],
2430f8c3c671SMarkos Chandras 							  sig))
2431f8c3c671SMarkos Chandras 						rv.l = -1LL; /* true, all 1s */
2432f8c3c671SMarkos Chandras 					if (sig &&
2433f8c3c671SMarkos Chandras 					    ieee754_cxtest(IEEE754_INVALID_OPERATION))
2434f8c3c671SMarkos Chandras 						rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
2435f8c3c671SMarkos Chandras 					else
2436f8c3c671SMarkos Chandras 						goto copcsr;
2437f8c3c671SMarkos Chandras 					break;
24381da177e4SLinus Torvalds 				default:
2439f8c3c671SMarkos Chandras 					/* Reserved R6 ops */
2440f8c3c671SMarkos Chandras 					pr_err("Reserved MIPS R6 CMP.condn.D operation\n");
24411da177e4SLinus Torvalds 					return SIGILL;
24421da177e4SLinus Torvalds 				}
2443f8c3c671SMarkos Chandras 			}
24441da177e4SLinus Torvalds 			break;
2445f8c3c671SMarkos Chandras 			}
2446f8c3c671SMarkos Chandras 		}
24471da177e4SLinus Torvalds 	default:
24481da177e4SLinus Torvalds 		return SIGILL;
24491da177e4SLinus Torvalds 	}
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	/*
24521da177e4SLinus Torvalds 	 * Update the fpu CSR register for this operation.
24531da177e4SLinus Torvalds 	 * If an exception is required, generate a tidy SIGFPE exception,
24541da177e4SLinus Torvalds 	 * without updating the result register.
24551da177e4SLinus Torvalds 	 * Note: cause exception bits do not accumulate, they are rewritten
24561da177e4SLinus Torvalds 	 * for each op; only the flag/sticky bits accumulate.
24571da177e4SLinus Torvalds 	 */
24581da177e4SLinus Torvalds 	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
24591da177e4SLinus Torvalds 	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
24603f7cac41SRalf Baechle 		/*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
24611da177e4SLinus Torvalds 		return SIGFPE;
24621da177e4SLinus Torvalds 	}
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds 	/*
24651da177e4SLinus Torvalds 	 * Now we can safely write the result back to the register file.
24661da177e4SLinus Torvalds 	 */
24671da177e4SLinus Torvalds 	switch (rfmt) {
246808a07904SRalf Baechle 	case -1:
246908a07904SRalf Baechle 
247008a07904SRalf Baechle 		if (cpu_has_mips_4_5_r)
2471c3b9b945SRob Kendrick 			cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
24721da177e4SLinus Torvalds 		else
247308a07904SRalf Baechle 			cbit = FPU_CSR_COND;
247408a07904SRalf Baechle 		if (rv.w)
247508a07904SRalf Baechle 			ctx->fcr31 |= cbit;
247608a07904SRalf Baechle 		else
247708a07904SRalf Baechle 			ctx->fcr31 &= ~cbit;
24781da177e4SLinus Torvalds 		break;
247908a07904SRalf Baechle 
24801da177e4SLinus Torvalds 	case d_fmt:
24811da177e4SLinus Torvalds 		DPTOREG(rv.d, MIPSInst_FD(ir));
24821da177e4SLinus Torvalds 		break;
24831da177e4SLinus Torvalds 	case s_fmt:
24841da177e4SLinus Torvalds 		SPTOREG(rv.s, MIPSInst_FD(ir));
24851da177e4SLinus Torvalds 		break;
24861da177e4SLinus Torvalds 	case w_fmt:
24871da177e4SLinus Torvalds 		SITOREG(rv.w, MIPSInst_FD(ir));
24881da177e4SLinus Torvalds 		break;
24891da177e4SLinus Torvalds 	case l_fmt:
24902d83fea7SMaciej W. Rozycki 		if (!cpu_has_mips_3_4_5_64_r2_r6)
249108a07904SRalf Baechle 			return SIGILL;
249208a07904SRalf Baechle 
24931da177e4SLinus Torvalds 		DITOREG(rv.l, MIPSInst_FD(ir));
24941da177e4SLinus Torvalds 		break;
24951da177e4SLinus Torvalds 	default:
24961da177e4SLinus Torvalds 		return SIGILL;
24971da177e4SLinus Torvalds 	}
24981da177e4SLinus Torvalds 
24991da177e4SLinus Torvalds 	return 0;
25001da177e4SLinus Torvalds }
25011da177e4SLinus Torvalds 
2502e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2503515b029dSDavid Daney 	int has_fpu, void *__user *fault_addr)
25041da177e4SLinus Torvalds {
2505333d1f67SRalf Baechle 	unsigned long oldepc, prevepc;
2506102cedc3SLeonid Yegoshin 	struct mm_decoded_insn dec_insn;
2507102cedc3SLeonid Yegoshin 	u16 instr[4];
2508102cedc3SLeonid Yegoshin 	u16 *instr_ptr;
25091da177e4SLinus Torvalds 	int sig = 0;
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds 	oldepc = xcp->cp0_epc;
25121da177e4SLinus Torvalds 	do {
25131da177e4SLinus Torvalds 		prevepc = xcp->cp0_epc;
25141da177e4SLinus Torvalds 
2515102cedc3SLeonid Yegoshin 		if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2516102cedc3SLeonid Yegoshin 			/*
2517102cedc3SLeonid Yegoshin 			 * Get next 2 microMIPS instructions and convert them
2518102cedc3SLeonid Yegoshin 			 * into 32-bit instructions.
2519102cedc3SLeonid Yegoshin 			 */
2520102cedc3SLeonid Yegoshin 			if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2521102cedc3SLeonid Yegoshin 			    (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2522102cedc3SLeonid Yegoshin 			    (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2523102cedc3SLeonid Yegoshin 			    (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2524b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
25251da177e4SLinus Torvalds 				return SIGBUS;
25261da177e4SLinus Torvalds 			}
2527102cedc3SLeonid Yegoshin 			instr_ptr = instr;
2528102cedc3SLeonid Yegoshin 
2529102cedc3SLeonid Yegoshin 			/* Get first instruction. */
2530102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2531102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2532102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2533102cedc3SLeonid Yegoshin 					(*instr_ptr);
2534102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2535102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 2;
2536102cedc3SLeonid Yegoshin 				instr_ptr += 1;
2537102cedc3SLeonid Yegoshin 			} else {
2538102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2539102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2540102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2541102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 4;
2542102cedc3SLeonid Yegoshin 				instr_ptr += 2;
2543515b029dSDavid Daney 			}
2544102cedc3SLeonid Yegoshin 			/* Get second instruction. */
2545102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2546102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2547102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2548102cedc3SLeonid Yegoshin 					(*instr_ptr);
2549102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2550102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 2;
2551102cedc3SLeonid Yegoshin 			} else {
2552102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2553102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2554102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2555102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 4;
2556102cedc3SLeonid Yegoshin 			}
2557102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 1;
2558102cedc3SLeonid Yegoshin 		} else {
2559102cedc3SLeonid Yegoshin 			if ((get_user(dec_insn.insn,
2560102cedc3SLeonid Yegoshin 			    (mips_instruction __user *) xcp->cp0_epc)) ||
2561102cedc3SLeonid Yegoshin 			    (get_user(dec_insn.next_insn,
2562102cedc3SLeonid Yegoshin 			    (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2563102cedc3SLeonid Yegoshin 				MIPS_FPU_EMU_INC_STATS(errors);
2564102cedc3SLeonid Yegoshin 				return SIGBUS;
2565102cedc3SLeonid Yegoshin 			}
2566102cedc3SLeonid Yegoshin 			dec_insn.pc_inc = 4;
2567102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc = 4;
2568102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 0;
2569102cedc3SLeonid Yegoshin 		}
2570102cedc3SLeonid Yegoshin 
2571102cedc3SLeonid Yegoshin 		if ((dec_insn.insn == 0) ||
2572102cedc3SLeonid Yegoshin 		   ((dec_insn.pc_inc == 2) &&
2573102cedc3SLeonid Yegoshin 		   ((dec_insn.insn & 0xffff) == MM_NOP16)))
2574102cedc3SLeonid Yegoshin 			xcp->cp0_epc += dec_insn.pc_inc;	/* Skip NOPs */
25751da177e4SLinus Torvalds 		else {
2576cd21dfcfSRalf Baechle 			/*
25772cfcf8a8SMaciej W. Rozycki 			 * The 'ieee754_csr' is an alias of ctx->fcr31.
25782cfcf8a8SMaciej W. Rozycki 			 * No need to copy ctx->fcr31 to ieee754_csr.
2579cd21dfcfSRalf Baechle 			 */
2580102cedc3SLeonid Yegoshin 			sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
25811da177e4SLinus Torvalds 		}
25821da177e4SLinus Torvalds 
2583e04582b7SAtsushi Nemoto 		if (has_fpu)
25841da177e4SLinus Torvalds 			break;
25851da177e4SLinus Torvalds 		if (sig)
25861da177e4SLinus Torvalds 			break;
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds 		cond_resched();
25891da177e4SLinus Torvalds 	} while (xcp->cp0_epc > prevepc);
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 	/* SIGILL indicates a non-fpu instruction */
25921da177e4SLinus Torvalds 	if (sig == SIGILL && xcp->cp0_epc != oldepc)
25933f7cac41SRalf Baechle 		/* but if EPC has advanced, then ignore it */
25941da177e4SLinus Torvalds 		sig = 0;
25951da177e4SLinus Torvalds 
25961da177e4SLinus Torvalds 	return sig;
25971da177e4SLinus Torvalds }
2598