xref: /linux/arch/mips/math-emu/cp1emu.c (revision e0d32f33e651a393a23826c06a9301917372f3e2)
11da177e4SLinus Torvalds /*
23f7cac41SRalf Baechle  * cp1emu.c: a MIPS coprocessor 1 (FPU) instruction emulator
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * MIPS floating point support
51da177e4SLinus Torvalds  * Copyright (C) 1994-2000 Algorithmics Ltd.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
81da177e4SLinus Torvalds  * Copyright (C) 2000  MIPS Technologies, Inc.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  This program is free software; you can distribute it and/or modify it
111da177e4SLinus Torvalds  *  under the terms of the GNU General Public License (Version 2) as
121da177e4SLinus Torvalds  *  published by the Free Software Foundation.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  This program is distributed in the hope it will be useful, but WITHOUT
151da177e4SLinus Torvalds  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
161da177e4SLinus Torvalds  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
171da177e4SLinus Torvalds  *  for more details.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *  You should have received a copy of the GNU General Public License along
201da177e4SLinus Torvalds  *  with this program; if not, write to the Free Software Foundation, Inc.,
213f7cac41SRalf Baechle  *  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * A complete emulator for MIPS coprocessor 1 instructions.  This is
241da177e4SLinus Torvalds  * required for #float(switch) or #float(trap), where it catches all
251da177e4SLinus Torvalds  * COP1 instructions via the "CoProcessor Unusable" exception.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  * More surprisingly it is also required for #float(ieee), to help out
283f7cac41SRalf Baechle  * the hardware FPU at the boundaries of the IEEE-754 representation
291da177e4SLinus Torvalds  * (denormalised values, infinities, underflow, etc).  It is made
301da177e4SLinus Torvalds  * quite nasty because emulation of some non-COP1 instructions is
311da177e4SLinus Torvalds  * required, e.g. in branch delay slots.
321da177e4SLinus Torvalds  *
333f7cac41SRalf Baechle  * Note if you know that you won't have an FPU, then you'll get much
341da177e4SLinus Torvalds  * better performance by compiling with -msoft-float!
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds #include <linux/sched.h>
3783fd38caSAtsushi Nemoto #include <linux/debugfs.h>
3808a07904SRalf Baechle #include <linux/kconfig.h>
3985c51c51SRalf Baechle #include <linux/percpu-defs.h>
407f788d2dSDeng-Cheng Zhu #include <linux/perf_event.h>
411da177e4SLinus Torvalds 
42cd8ee345SRalf Baechle #include <asm/branch.h>
431da177e4SLinus Torvalds #include <asm/inst.h>
441da177e4SLinus Torvalds #include <asm/ptrace.h>
451da177e4SLinus Torvalds #include <asm/signal.h>
46cd8ee345SRalf Baechle #include <asm/uaccess.h>
47cd8ee345SRalf Baechle 
48cd8ee345SRalf Baechle #include <asm/processor.h>
491da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
50102cedc3SLeonid Yegoshin #include <asm/fpu.h>
51b0a668fbSLeonid Yegoshin #include <asm/mips-r2-to-r6-emul.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include "ieee754.h"
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */
561da177e4SLinus Torvalds 
57eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
581da177e4SLinus Torvalds 	mips_instruction);
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *,
61515b029dSDavid Daney 	struct mips_fpu_struct *, mips_instruction, void *__user *);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds /* Control registers */
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #define FPCREG_RID	0	/* $0  = revision id */
661da177e4SLinus Torvalds #define FPCREG_CSR	31	/* $31 = csr */
671da177e4SLinus Torvalds 
6895e8f634SShane McDonald /* Determine rounding mode from the RM bits of the FCSR */
6995e8f634SShane McDonald #define modeindex(v) ((v) & FPU_CSR_RM)
7095e8f634SShane McDonald 
711da177e4SLinus Torvalds /* convert condition code register number to csr bit */
72b0a668fbSLeonid Yegoshin const unsigned int fpucondbit[8] = {
731da177e4SLinus Torvalds 	FPU_CSR_COND0,
741da177e4SLinus Torvalds 	FPU_CSR_COND1,
751da177e4SLinus Torvalds 	FPU_CSR_COND2,
761da177e4SLinus Torvalds 	FPU_CSR_COND3,
771da177e4SLinus Torvalds 	FPU_CSR_COND4,
781da177e4SLinus Torvalds 	FPU_CSR_COND5,
791da177e4SLinus Torvalds 	FPU_CSR_COND6,
801da177e4SLinus Torvalds 	FPU_CSR_COND7
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds 
83102cedc3SLeonid Yegoshin /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
84102cedc3SLeonid Yegoshin static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
85102cedc3SLeonid Yegoshin static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
86102cedc3SLeonid Yegoshin static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
87102cedc3SLeonid Yegoshin static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
88102cedc3SLeonid Yegoshin 
89102cedc3SLeonid Yegoshin /*
90102cedc3SLeonid Yegoshin  * This functions translates a 32-bit microMIPS instruction
91102cedc3SLeonid Yegoshin  * into a 32-bit MIPS32 instruction. Returns 0 on success
92102cedc3SLeonid Yegoshin  * and SIGILL otherwise.
93102cedc3SLeonid Yegoshin  */
94102cedc3SLeonid Yegoshin static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
95102cedc3SLeonid Yegoshin {
96102cedc3SLeonid Yegoshin 	union mips_instruction insn = *insn_ptr;
97102cedc3SLeonid Yegoshin 	union mips_instruction mips32_insn = insn;
98102cedc3SLeonid Yegoshin 	int func, fmt, op;
99102cedc3SLeonid Yegoshin 
100102cedc3SLeonid Yegoshin 	switch (insn.mm_i_format.opcode) {
101102cedc3SLeonid Yegoshin 	case mm_ldc132_op:
102102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = ldc1_op;
103102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
104102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
105102cedc3SLeonid Yegoshin 		break;
106102cedc3SLeonid Yegoshin 	case mm_lwc132_op:
107102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = lwc1_op;
108102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
109102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
110102cedc3SLeonid Yegoshin 		break;
111102cedc3SLeonid Yegoshin 	case mm_sdc132_op:
112102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = sdc1_op;
113102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
114102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
115102cedc3SLeonid Yegoshin 		break;
116102cedc3SLeonid Yegoshin 	case mm_swc132_op:
117102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = swc1_op;
118102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
119102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
120102cedc3SLeonid Yegoshin 		break;
121102cedc3SLeonid Yegoshin 	case mm_pool32i_op:
122102cedc3SLeonid Yegoshin 		/* NOTE: offset is << by 1 if in microMIPS mode. */
123102cedc3SLeonid Yegoshin 		if ((insn.mm_i_format.rt == mm_bc1f_op) ||
124102cedc3SLeonid Yegoshin 		    (insn.mm_i_format.rt == mm_bc1t_op)) {
125102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.opcode = cop1_op;
126102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.bc = bc_op;
127102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.flag =
128102cedc3SLeonid Yegoshin 				(insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
129102cedc3SLeonid Yegoshin 		} else
130102cedc3SLeonid Yegoshin 			return SIGILL;
131102cedc3SLeonid Yegoshin 		break;
132102cedc3SLeonid Yegoshin 	case mm_pool32f_op:
133102cedc3SLeonid Yegoshin 		switch (insn.mm_fp0_format.func) {
134102cedc3SLeonid Yegoshin 		case mm_32f_01_op:
135102cedc3SLeonid Yegoshin 		case mm_32f_11_op:
136102cedc3SLeonid Yegoshin 		case mm_32f_02_op:
137102cedc3SLeonid Yegoshin 		case mm_32f_12_op:
138102cedc3SLeonid Yegoshin 		case mm_32f_41_op:
139102cedc3SLeonid Yegoshin 		case mm_32f_51_op:
140102cedc3SLeonid Yegoshin 		case mm_32f_42_op:
141102cedc3SLeonid Yegoshin 		case mm_32f_52_op:
142102cedc3SLeonid Yegoshin 			op = insn.mm_fp0_format.func;
143102cedc3SLeonid Yegoshin 			if (op == mm_32f_01_op)
144102cedc3SLeonid Yegoshin 				func = madd_s_op;
145102cedc3SLeonid Yegoshin 			else if (op == mm_32f_11_op)
146102cedc3SLeonid Yegoshin 				func = madd_d_op;
147102cedc3SLeonid Yegoshin 			else if (op == mm_32f_02_op)
148102cedc3SLeonid Yegoshin 				func = nmadd_s_op;
149102cedc3SLeonid Yegoshin 			else if (op == mm_32f_12_op)
150102cedc3SLeonid Yegoshin 				func = nmadd_d_op;
151102cedc3SLeonid Yegoshin 			else if (op == mm_32f_41_op)
152102cedc3SLeonid Yegoshin 				func = msub_s_op;
153102cedc3SLeonid Yegoshin 			else if (op == mm_32f_51_op)
154102cedc3SLeonid Yegoshin 				func = msub_d_op;
155102cedc3SLeonid Yegoshin 			else if (op == mm_32f_42_op)
156102cedc3SLeonid Yegoshin 				func = nmsub_s_op;
157102cedc3SLeonid Yegoshin 			else
158102cedc3SLeonid Yegoshin 				func = nmsub_d_op;
159102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.opcode = cop1x_op;
160102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
161102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
162102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
163102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
164102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.func = func;
165102cedc3SLeonid Yegoshin 			break;
166102cedc3SLeonid Yegoshin 		case mm_32f_10_op:
167102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
168102cedc3SLeonid Yegoshin 			op = insn.mm_fp5_format.op & 0x7;
169102cedc3SLeonid Yegoshin 			if (op == mm_ldxc1_op)
170102cedc3SLeonid Yegoshin 				func = ldxc1_op;
171102cedc3SLeonid Yegoshin 			else if (op == mm_sdxc1_op)
172102cedc3SLeonid Yegoshin 				func = sdxc1_op;
173102cedc3SLeonid Yegoshin 			else if (op == mm_lwxc1_op)
174102cedc3SLeonid Yegoshin 				func = lwxc1_op;
175102cedc3SLeonid Yegoshin 			else if (op == mm_swxc1_op)
176102cedc3SLeonid Yegoshin 				func = swxc1_op;
177102cedc3SLeonid Yegoshin 
178102cedc3SLeonid Yegoshin 			if (func != -1) {
179102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = cop1x_op;
180102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs =
181102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.base;
182102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
183102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.index;
184102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = 0;
185102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = insn.mm_fp5_format.fd;
186102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = func;
187102cedc3SLeonid Yegoshin 			} else
188102cedc3SLeonid Yegoshin 				return SIGILL;
189102cedc3SLeonid Yegoshin 			break;
190102cedc3SLeonid Yegoshin 		case mm_32f_40_op:
191102cedc3SLeonid Yegoshin 			op = -1;	/* Invalid */
192102cedc3SLeonid Yegoshin 			if (insn.mm_fp2_format.op == mm_fmovt_op)
193102cedc3SLeonid Yegoshin 				op = 1;
194102cedc3SLeonid Yegoshin 			else if (insn.mm_fp2_format.op == mm_fmovf_op)
195102cedc3SLeonid Yegoshin 				op = 0;
196102cedc3SLeonid Yegoshin 			if (op != -1) {
197102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
198102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
199102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp2_format.fmt];
200102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
201102cedc3SLeonid Yegoshin 					(insn.mm_fp2_format.cc<<2) + op;
202102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
203102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fs;
204102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
205102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fd;
206102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = fmovc_op;
207102cedc3SLeonid Yegoshin 			} else
208102cedc3SLeonid Yegoshin 				return SIGILL;
209102cedc3SLeonid Yegoshin 			break;
210102cedc3SLeonid Yegoshin 		case mm_32f_60_op:
211102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
212102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fadd_op)
213102cedc3SLeonid Yegoshin 				func = fadd_op;
214102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fsub_op)
215102cedc3SLeonid Yegoshin 				func = fsub_op;
216102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmul_op)
217102cedc3SLeonid Yegoshin 				func = fmul_op;
218102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fdiv_op)
219102cedc3SLeonid Yegoshin 				func = fdiv_op;
220102cedc3SLeonid Yegoshin 			if (func != -1) {
221102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
222102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
223102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
224102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
225102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
226102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
227102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
228102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
229102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
230102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
231102cedc3SLeonid Yegoshin 			} else
232102cedc3SLeonid Yegoshin 				return SIGILL;
233102cedc3SLeonid Yegoshin 			break;
234102cedc3SLeonid Yegoshin 		case mm_32f_70_op:
235102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
236102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fmovn_op)
237102cedc3SLeonid Yegoshin 				func = fmovn_op;
238102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmovz_op)
239102cedc3SLeonid Yegoshin 				func = fmovz_op;
240102cedc3SLeonid Yegoshin 			if (func != -1) {
241102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
242102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
243102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
244102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
245102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
246102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
247102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
248102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
249102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
250102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
251102cedc3SLeonid Yegoshin 			} else
252102cedc3SLeonid Yegoshin 				return SIGILL;
253102cedc3SLeonid Yegoshin 			break;
254102cedc3SLeonid Yegoshin 		case mm_32f_73_op:    /* POOL32FXF */
255102cedc3SLeonid Yegoshin 			switch (insn.mm_fp1_format.op) {
256102cedc3SLeonid Yegoshin 			case mm_movf0_op:
257102cedc3SLeonid Yegoshin 			case mm_movf1_op:
258102cedc3SLeonid Yegoshin 			case mm_movt0_op:
259102cedc3SLeonid Yegoshin 			case mm_movt1_op:
260102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
261102cedc3SLeonid Yegoshin 				    mm_movf0_op)
262102cedc3SLeonid Yegoshin 					op = 0;
263102cedc3SLeonid Yegoshin 				else
264102cedc3SLeonid Yegoshin 					op = 1;
265102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = spec_op;
266102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
267102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
268102cedc3SLeonid Yegoshin 					(insn.mm_fp4_format.cc << 2) + op;
269102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
270102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = 0;
271102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = movc_op;
272102cedc3SLeonid Yegoshin 				break;
273102cedc3SLeonid Yegoshin 			case mm_fcvtd0_op:
274102cedc3SLeonid Yegoshin 			case mm_fcvtd1_op:
275102cedc3SLeonid Yegoshin 			case mm_fcvts0_op:
276102cedc3SLeonid Yegoshin 			case mm_fcvts1_op:
277102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
278102cedc3SLeonid Yegoshin 				    mm_fcvtd0_op) {
279102cedc3SLeonid Yegoshin 					func = fcvtd_op;
280102cedc3SLeonid Yegoshin 					fmt = swl_format[insn.mm_fp3_format.fmt];
281102cedc3SLeonid Yegoshin 				} else {
282102cedc3SLeonid Yegoshin 					func = fcvts_op;
283102cedc3SLeonid Yegoshin 					fmt = dwl_format[insn.mm_fp3_format.fmt];
284102cedc3SLeonid Yegoshin 				}
285102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
286102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt = fmt;
287102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
288102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
289102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
290102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
291102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
292102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
293102cedc3SLeonid Yegoshin 				break;
294102cedc3SLeonid Yegoshin 			case mm_fmov0_op:
295102cedc3SLeonid Yegoshin 			case mm_fmov1_op:
296102cedc3SLeonid Yegoshin 			case mm_fabs0_op:
297102cedc3SLeonid Yegoshin 			case mm_fabs1_op:
298102cedc3SLeonid Yegoshin 			case mm_fneg0_op:
299102cedc3SLeonid Yegoshin 			case mm_fneg1_op:
300102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
301102cedc3SLeonid Yegoshin 				    mm_fmov0_op)
302102cedc3SLeonid Yegoshin 					func = fmov_op;
303102cedc3SLeonid Yegoshin 				else if ((insn.mm_fp1_format.op & 0x7f) ==
304102cedc3SLeonid Yegoshin 					 mm_fabs0_op)
305102cedc3SLeonid Yegoshin 					func = fabs_op;
306102cedc3SLeonid Yegoshin 				else
307102cedc3SLeonid Yegoshin 					func = fneg_op;
308102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
309102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
310102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp3_format.fmt];
311102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
312102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
313102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
314102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
315102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
316102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
317102cedc3SLeonid Yegoshin 				break;
318102cedc3SLeonid Yegoshin 			case mm_ffloorl_op:
319102cedc3SLeonid Yegoshin 			case mm_ffloorw_op:
320102cedc3SLeonid Yegoshin 			case mm_fceill_op:
321102cedc3SLeonid Yegoshin 			case mm_fceilw_op:
322102cedc3SLeonid Yegoshin 			case mm_ftruncl_op:
323102cedc3SLeonid Yegoshin 			case mm_ftruncw_op:
324102cedc3SLeonid Yegoshin 			case mm_froundl_op:
325102cedc3SLeonid Yegoshin 			case mm_froundw_op:
326102cedc3SLeonid Yegoshin 			case mm_fcvtl_op:
327102cedc3SLeonid Yegoshin 			case mm_fcvtw_op:
328102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_ffloorl_op)
329102cedc3SLeonid Yegoshin 					func = ffloorl_op;
330102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ffloorw_op)
331102cedc3SLeonid Yegoshin 					func = ffloor_op;
332102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceill_op)
333102cedc3SLeonid Yegoshin 					func = fceill_op;
334102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceilw_op)
335102cedc3SLeonid Yegoshin 					func = fceil_op;
336102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncl_op)
337102cedc3SLeonid Yegoshin 					func = ftruncl_op;
338102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncw_op)
339102cedc3SLeonid Yegoshin 					func = ftrunc_op;
340102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundl_op)
341102cedc3SLeonid Yegoshin 					func = froundl_op;
342102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundw_op)
343102cedc3SLeonid Yegoshin 					func = fround_op;
344102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fcvtl_op)
345102cedc3SLeonid Yegoshin 					func = fcvtl_op;
346102cedc3SLeonid Yegoshin 				else
347102cedc3SLeonid Yegoshin 					func = fcvtw_op;
348102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
349102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
350102cedc3SLeonid Yegoshin 					sd_format[insn.mm_fp1_format.fmt];
351102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
352102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
353102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
354102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
355102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
356102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
357102cedc3SLeonid Yegoshin 				break;
358102cedc3SLeonid Yegoshin 			case mm_frsqrt_op:
359102cedc3SLeonid Yegoshin 			case mm_fsqrt_op:
360102cedc3SLeonid Yegoshin 			case mm_frecip_op:
361102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_frsqrt_op)
362102cedc3SLeonid Yegoshin 					func = frsqrt_op;
363102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fsqrt_op)
364102cedc3SLeonid Yegoshin 					func = fsqrt_op;
365102cedc3SLeonid Yegoshin 				else
366102cedc3SLeonid Yegoshin 					func = frecip_op;
367102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
368102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
369102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp1_format.fmt];
370102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
371102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
372102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
373102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
374102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
375102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
376102cedc3SLeonid Yegoshin 				break;
377102cedc3SLeonid Yegoshin 			case mm_mfc1_op:
378102cedc3SLeonid Yegoshin 			case mm_mtc1_op:
379102cedc3SLeonid Yegoshin 			case mm_cfc1_op:
380102cedc3SLeonid Yegoshin 			case mm_ctc1_op:
3819355e59cSSteven J. Hill 			case mm_mfhc1_op:
3829355e59cSSteven J. Hill 			case mm_mthc1_op:
383102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_mfc1_op)
384102cedc3SLeonid Yegoshin 					op = mfc_op;
385102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_mtc1_op)
386102cedc3SLeonid Yegoshin 					op = mtc_op;
387102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_cfc1_op)
388102cedc3SLeonid Yegoshin 					op = cfc_op;
3899355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_ctc1_op)
390102cedc3SLeonid Yegoshin 					op = ctc_op;
3919355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_mfhc1_op)
3929355e59cSSteven J. Hill 					op = mfhc_op;
3939355e59cSSteven J. Hill 				else
3949355e59cSSteven J. Hill 					op = mthc_op;
395102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.opcode = cop1_op;
396102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.op = op;
397102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.rt =
398102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
399102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fs =
400102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
401102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fd = 0;
402102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.func = 0;
403102cedc3SLeonid Yegoshin 				break;
404102cedc3SLeonid Yegoshin 			default:
405102cedc3SLeonid Yegoshin 				return SIGILL;
406102cedc3SLeonid Yegoshin 			}
407102cedc3SLeonid Yegoshin 			break;
408102cedc3SLeonid Yegoshin 		case mm_32f_74_op:	/* c.cond.fmt */
409102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.opcode = cop1_op;
410102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fmt =
411102cedc3SLeonid Yegoshin 				sdps_format[insn.mm_fp4_format.fmt];
412102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
413102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
414102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
415102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.func =
416102cedc3SLeonid Yegoshin 				insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
417102cedc3SLeonid Yegoshin 			break;
418102cedc3SLeonid Yegoshin 		default:
419102cedc3SLeonid Yegoshin 			return SIGILL;
420102cedc3SLeonid Yegoshin 		}
421102cedc3SLeonid Yegoshin 		break;
422102cedc3SLeonid Yegoshin 	default:
423102cedc3SLeonid Yegoshin 		return SIGILL;
424102cedc3SLeonid Yegoshin 	}
425102cedc3SLeonid Yegoshin 
426102cedc3SLeonid Yegoshin 	*insn_ptr = mips32_insn;
427102cedc3SLeonid Yegoshin 	return 0;
428102cedc3SLeonid Yegoshin }
429102cedc3SLeonid Yegoshin 
4301da177e4SLinus Torvalds /*
4311da177e4SLinus Torvalds  * Redundant with logic already in kernel/branch.c,
4321da177e4SLinus Torvalds  * embedded in compute_return_epc.  At some point,
4331da177e4SLinus Torvalds  * a single subroutine should be used across both
4341da177e4SLinus Torvalds  * modules.
4351da177e4SLinus Torvalds  */
436102cedc3SLeonid Yegoshin static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
437102cedc3SLeonid Yegoshin 			 unsigned long *contpc)
4381da177e4SLinus Torvalds {
439102cedc3SLeonid Yegoshin 	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
440102cedc3SLeonid Yegoshin 	unsigned int fcr31;
441102cedc3SLeonid Yegoshin 	unsigned int bit = 0;
442102cedc3SLeonid Yegoshin 
443102cedc3SLeonid Yegoshin 	switch (insn.i_format.opcode) {
4441da177e4SLinus Torvalds 	case spec_op:
445102cedc3SLeonid Yegoshin 		switch (insn.r_format.func) {
4461da177e4SLinus Torvalds 		case jalr_op:
447102cedc3SLeonid Yegoshin 			regs->regs[insn.r_format.rd] =
448102cedc3SLeonid Yegoshin 				regs->cp0_epc + dec_insn.pc_inc +
449102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
450102cedc3SLeonid Yegoshin 			/* Fall through */
4511da177e4SLinus Torvalds 		case jr_op:
4525f9f41c4SMarkos Chandras 			/* For R6, JR already emulated in jalr_op */
4535f9f41c4SMarkos Chandras 			if (NO_R6EMU && insn.r_format.opcode == jr_op)
4545f9f41c4SMarkos Chandras 				break;
455102cedc3SLeonid Yegoshin 			*contpc = regs->regs[insn.r_format.rs];
4561da177e4SLinus Torvalds 			return 1;
4571da177e4SLinus Torvalds 		}
4581da177e4SLinus Torvalds 		break;
4591da177e4SLinus Torvalds 	case bcond_op:
460102cedc3SLeonid Yegoshin 		switch (insn.i_format.rt) {
4611da177e4SLinus Torvalds 		case bltzal_op:
4621da177e4SLinus Torvalds 		case bltzall_op:
463319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
464319824eaSMarkos Chandras 			    insn.i_format.rt == bltzall_op))
465319824eaSMarkos Chandras 				break;
466319824eaSMarkos Chandras 
467102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
468102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
469102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
470102cedc3SLeonid Yegoshin 			/* Fall through */
471102cedc3SLeonid Yegoshin 		case bltzl_op:
472319824eaSMarkos Chandras 			if (NO_R6EMU)
473319824eaSMarkos Chandras 				break;
474319824eaSMarkos Chandras 		case bltz_op:
475102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] < 0)
476102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
477102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
478102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
479102cedc3SLeonid Yegoshin 			else
480102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
481102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
482102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
4831da177e4SLinus Torvalds 			return 1;
484102cedc3SLeonid Yegoshin 		case bgezal_op:
485102cedc3SLeonid Yegoshin 		case bgezall_op:
486319824eaSMarkos Chandras 			if (NO_R6EMU && (insn.i_format.rs ||
487319824eaSMarkos Chandras 			    insn.i_format.rt == bgezall_op))
488319824eaSMarkos Chandras 				break;
489319824eaSMarkos Chandras 
490102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
491102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
492102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
493102cedc3SLeonid Yegoshin 			/* Fall through */
494102cedc3SLeonid Yegoshin 		case bgezl_op:
495319824eaSMarkos Chandras 			if (NO_R6EMU)
496319824eaSMarkos Chandras 				break;
497319824eaSMarkos Chandras 		case bgez_op:
498102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] >= 0)
499102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
500102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
501102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
502102cedc3SLeonid Yegoshin 			else
503102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
504102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
505102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
506102cedc3SLeonid Yegoshin 			return 1;
5071da177e4SLinus Torvalds 		}
5081da177e4SLinus Torvalds 		break;
5091da177e4SLinus Torvalds 	case jalx_op:
510102cedc3SLeonid Yegoshin 		set_isa16_mode(bit);
511102cedc3SLeonid Yegoshin 	case jal_op:
512102cedc3SLeonid Yegoshin 		regs->regs[31] = regs->cp0_epc +
513102cedc3SLeonid Yegoshin 			dec_insn.pc_inc +
514102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc;
515102cedc3SLeonid Yegoshin 		/* Fall through */
516102cedc3SLeonid Yegoshin 	case j_op:
517102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
518102cedc3SLeonid Yegoshin 		*contpc >>= 28;
519102cedc3SLeonid Yegoshin 		*contpc <<= 28;
520102cedc3SLeonid Yegoshin 		*contpc |= (insn.j_format.target << 2);
521102cedc3SLeonid Yegoshin 		/* Set microMIPS mode bit: XOR for jalx. */
522102cedc3SLeonid Yegoshin 		*contpc ^= bit;
5231da177e4SLinus Torvalds 		return 1;
524102cedc3SLeonid Yegoshin 	case beql_op:
525319824eaSMarkos Chandras 		if (NO_R6EMU)
526319824eaSMarkos Chandras 			break;
527319824eaSMarkos Chandras 	case beq_op:
528102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] ==
529102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
530102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
531102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
532102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
533102cedc3SLeonid Yegoshin 		else
534102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
535102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
536102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
537102cedc3SLeonid Yegoshin 		return 1;
538102cedc3SLeonid Yegoshin 	case bnel_op:
539319824eaSMarkos Chandras 		if (NO_R6EMU)
540319824eaSMarkos Chandras 			break;
541319824eaSMarkos Chandras 	case bne_op:
542102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] !=
543102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
544102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
545102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
546102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
547102cedc3SLeonid Yegoshin 		else
548102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
549102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
550102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
551102cedc3SLeonid Yegoshin 		return 1;
552102cedc3SLeonid Yegoshin 	case blezl_op:
553319824eaSMarkos Chandras 		if (NO_R6EMU)
554319824eaSMarkos Chandras 			break;
555319824eaSMarkos Chandras 	case blez_op:
556a8ff66f5SMarkos Chandras 
557a8ff66f5SMarkos Chandras 		/*
558a8ff66f5SMarkos Chandras 		 * Compact branches for R6 for the
559a8ff66f5SMarkos Chandras 		 * blez and blezl opcodes.
560a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = 0 | rt != 0  == BLEZALC
561a8ff66f5SMarkos Chandras 		 * BLEZ  | rs = rt != 0      == BGEZALC
562a8ff66f5SMarkos Chandras 		 * BLEZ  | rs != 0 | rt != 0 == BGEUC
563a8ff66f5SMarkos Chandras 		 * BLEZL | rs = 0 | rt != 0  == BLEZC
564a8ff66f5SMarkos Chandras 		 * BLEZL | rs = rt != 0      == BGEZC
565a8ff66f5SMarkos Chandras 		 * BLEZL | rs != 0 | rt != 0 == BGEC
566a8ff66f5SMarkos Chandras 		 *
567a8ff66f5SMarkos Chandras 		 * For real BLEZ{,L}, rt is always 0.
568a8ff66f5SMarkos Chandras 		 */
569a8ff66f5SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
570a8ff66f5SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
571a8ff66f5SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
572a8ff66f5SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
573a8ff66f5SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
574a8ff66f5SMarkos Chandras 					dec_insn.pc_inc;
575a8ff66f5SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
576a8ff66f5SMarkos Chandras 				dec_insn.next_pc_inc;
577a8ff66f5SMarkos Chandras 
578a8ff66f5SMarkos Chandras 			return 1;
579a8ff66f5SMarkos Chandras 		}
580102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] <= 0)
581102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
582102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
583102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
584102cedc3SLeonid Yegoshin 		else
585102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
586102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
587102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
588102cedc3SLeonid Yegoshin 		return 1;
589102cedc3SLeonid Yegoshin 	case bgtzl_op:
590319824eaSMarkos Chandras 		if (NO_R6EMU)
591319824eaSMarkos Chandras 			break;
592319824eaSMarkos Chandras 	case bgtz_op:
593f1b44067SMarkos Chandras 		/*
594f1b44067SMarkos Chandras 		 * Compact branches for R6 for the
595f1b44067SMarkos Chandras 		 * bgtz and bgtzl opcodes.
596f1b44067SMarkos Chandras 		 * BGTZ  | rs = 0 | rt != 0  == BGTZALC
597f1b44067SMarkos Chandras 		 * BGTZ  | rs = rt != 0      == BLTZALC
598f1b44067SMarkos Chandras 		 * BGTZ  | rs != 0 | rt != 0 == BLTUC
599f1b44067SMarkos Chandras 		 * BGTZL | rs = 0 | rt != 0  == BGTZC
600f1b44067SMarkos Chandras 		 * BGTZL | rs = rt != 0      == BLTZC
601f1b44067SMarkos Chandras 		 * BGTZL | rs != 0 | rt != 0 == BLTC
602f1b44067SMarkos Chandras 		 *
603f1b44067SMarkos Chandras 		 * *ZALC varint for BGTZ &&& rt != 0
604f1b44067SMarkos Chandras 		 * For real GTZ{,L}, rt is always 0.
605f1b44067SMarkos Chandras 		 */
606f1b44067SMarkos Chandras 		if (cpu_has_mips_r6 && insn.i_format.rt) {
607f1b44067SMarkos Chandras 			if ((insn.i_format.opcode == blez_op) &&
608f1b44067SMarkos Chandras 			    ((!insn.i_format.rs && insn.i_format.rt) ||
609f1b44067SMarkos Chandras 			     (insn.i_format.rs == insn.i_format.rt)))
610f1b44067SMarkos Chandras 				regs->regs[31] = regs->cp0_epc +
611f1b44067SMarkos Chandras 					dec_insn.pc_inc;
612f1b44067SMarkos Chandras 			*contpc = regs->cp0_epc + dec_insn.pc_inc +
613f1b44067SMarkos Chandras 				dec_insn.next_pc_inc;
614f1b44067SMarkos Chandras 
615f1b44067SMarkos Chandras 			return 1;
616f1b44067SMarkos Chandras 		}
617f1b44067SMarkos Chandras 
618102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] > 0)
619102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
620102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
621102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
622102cedc3SLeonid Yegoshin 		else
623102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
624102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
625102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
626102cedc3SLeonid Yegoshin 		return 1;
627c893ce38SMarkos Chandras 	case cbcond0_op:
62810d962d5SMarkos Chandras 	case cbcond1_op:
629c893ce38SMarkos Chandras 		if (!cpu_has_mips_r6)
630c893ce38SMarkos Chandras 			break;
631c893ce38SMarkos Chandras 		if (insn.i_format.rt && !insn.i_format.rs)
632c893ce38SMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
633c893ce38SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
634c893ce38SMarkos Chandras 			dec_insn.next_pc_inc;
635c893ce38SMarkos Chandras 
636c893ce38SMarkos Chandras 		return 1;
637c26d4219SDavid Daney #ifdef CONFIG_CPU_CAVIUM_OCTEON
638c26d4219SDavid Daney 	case lwc2_op: /* This is bbit0 on Octeon */
639c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
640c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
641c26d4219SDavid Daney 		else
642c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
643c26d4219SDavid Daney 		return 1;
644c26d4219SDavid Daney 	case ldc2_op: /* This is bbit032 on Octeon */
645c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
646c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
647c26d4219SDavid Daney 		else
648c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
649c26d4219SDavid Daney 		return 1;
650c26d4219SDavid Daney 	case swc2_op: /* This is bbit1 on Octeon */
651c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
652c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
653c26d4219SDavid Daney 		else
654c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
655c26d4219SDavid Daney 		return 1;
656c26d4219SDavid Daney 	case sdc2_op: /* This is bbit132 on Octeon */
657c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
658c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
659c26d4219SDavid Daney 		else
660c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
661c26d4219SDavid Daney 		return 1;
6628467ca01SMarkos Chandras #else
6638467ca01SMarkos Chandras 	case bc6_op:
6648467ca01SMarkos Chandras 		/*
6658467ca01SMarkos Chandras 		 * Only valid for MIPS R6 but we can still end up
6668467ca01SMarkos Chandras 		 * here from a broken userland so just tell emulator
6678467ca01SMarkos Chandras 		 * this is not a branch and let it break later on.
6688467ca01SMarkos Chandras 		 */
6698467ca01SMarkos Chandras 		if  (!cpu_has_mips_r6)
6708467ca01SMarkos Chandras 			break;
6718467ca01SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
6728467ca01SMarkos Chandras 			dec_insn.next_pc_inc;
6738467ca01SMarkos Chandras 
6748467ca01SMarkos Chandras 		return 1;
67584fef630SMarkos Chandras 	case balc6_op:
67684fef630SMarkos Chandras 		if (!cpu_has_mips_r6)
67784fef630SMarkos Chandras 			break;
67884fef630SMarkos Chandras 		regs->regs[31] = regs->cp0_epc + 4;
67984fef630SMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
68084fef630SMarkos Chandras 			dec_insn.next_pc_inc;
68184fef630SMarkos Chandras 
68284fef630SMarkos Chandras 		return 1;
68369b9a2fdSMarkos Chandras 	case beqzcjic_op:
68469b9a2fdSMarkos Chandras 		if (!cpu_has_mips_r6)
68569b9a2fdSMarkos Chandras 			break;
68669b9a2fdSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
68769b9a2fdSMarkos Chandras 			dec_insn.next_pc_inc;
68869b9a2fdSMarkos Chandras 
68969b9a2fdSMarkos Chandras 		return 1;
69028d6f93dSMarkos Chandras 	case bnezcjialc_op:
69128d6f93dSMarkos Chandras 		if (!cpu_has_mips_r6)
69228d6f93dSMarkos Chandras 			break;
69328d6f93dSMarkos Chandras 		if (!insn.i_format.rs)
69428d6f93dSMarkos Chandras 			regs->regs[31] = regs->cp0_epc + 4;
69528d6f93dSMarkos Chandras 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
69628d6f93dSMarkos Chandras 			dec_insn.next_pc_inc;
69728d6f93dSMarkos Chandras 
69828d6f93dSMarkos Chandras 		return 1;
699c26d4219SDavid Daney #endif
7001da177e4SLinus Torvalds 	case cop0_op:
7011da177e4SLinus Torvalds 	case cop1_op:
702c8a34581SMarkos Chandras 		/* Need to check for R6 bc1nez and bc1eqz branches */
703c8a34581SMarkos Chandras 		if (cpu_has_mips_r6 &&
704c8a34581SMarkos Chandras 		    ((insn.i_format.rs == bc1eqz_op) ||
705c8a34581SMarkos Chandras 		     (insn.i_format.rs == bc1nez_op))) {
706c8a34581SMarkos Chandras 			bit = 0;
707c8a34581SMarkos Chandras 			switch (insn.i_format.rs) {
708c8a34581SMarkos Chandras 			case bc1eqz_op:
709c8a34581SMarkos Chandras 				if (get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1)
710c8a34581SMarkos Chandras 				    bit = 1;
711c8a34581SMarkos Chandras 				break;
712c8a34581SMarkos Chandras 			case bc1nez_op:
713c8a34581SMarkos Chandras 				if (!(get_fpr32(&current->thread.fpu.fpr[insn.i_format.rt], 0) & 0x1))
714c8a34581SMarkos Chandras 				    bit = 1;
715c8a34581SMarkos Chandras 				break;
716c8a34581SMarkos Chandras 			}
717c8a34581SMarkos Chandras 			if (bit)
718c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
719c8a34581SMarkos Chandras 					dec_insn.pc_inc +
720c8a34581SMarkos Chandras 					(insn.i_format.simmediate << 2);
721c8a34581SMarkos Chandras 			else
722c8a34581SMarkos Chandras 				*contpc = regs->cp0_epc +
723c8a34581SMarkos Chandras 					dec_insn.pc_inc +
724c8a34581SMarkos Chandras 					dec_insn.next_pc_inc;
725c8a34581SMarkos Chandras 
726c8a34581SMarkos Chandras 			return 1;
727c8a34581SMarkos Chandras 		}
728c8a34581SMarkos Chandras 		/* R2/R6 compatible cop1 instruction. Fall through */
7291da177e4SLinus Torvalds 	case cop2_op:
7301da177e4SLinus Torvalds 	case cop1x_op:
731102cedc3SLeonid Yegoshin 		if (insn.i_format.rs == bc_op) {
732102cedc3SLeonid Yegoshin 			preempt_disable();
733102cedc3SLeonid Yegoshin 			if (is_fpu_owner())
734842dfc11SManuel Lauss 			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
735102cedc3SLeonid Yegoshin 			else
736102cedc3SLeonid Yegoshin 				fcr31 = current->thread.fpu.fcr31;
737102cedc3SLeonid Yegoshin 			preempt_enable();
738102cedc3SLeonid Yegoshin 
739102cedc3SLeonid Yegoshin 			bit = (insn.i_format.rt >> 2);
740102cedc3SLeonid Yegoshin 			bit += (bit != 0);
741102cedc3SLeonid Yegoshin 			bit += 23;
742102cedc3SLeonid Yegoshin 			switch (insn.i_format.rt & 3) {
743102cedc3SLeonid Yegoshin 			case 0:	/* bc1f */
744102cedc3SLeonid Yegoshin 			case 2:	/* bc1fl */
745102cedc3SLeonid Yegoshin 				if (~fcr31 & (1 << bit))
746102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
747102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
748102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
749102cedc3SLeonid Yegoshin 				else
750102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
751102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
752102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
753102cedc3SLeonid Yegoshin 				return 1;
754102cedc3SLeonid Yegoshin 			case 1:	/* bc1t */
755102cedc3SLeonid Yegoshin 			case 3:	/* bc1tl */
756102cedc3SLeonid Yegoshin 				if (fcr31 & (1 << bit))
757102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
758102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
759102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
760102cedc3SLeonid Yegoshin 				else
761102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
762102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
763102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
7641da177e4SLinus Torvalds 				return 1;
7651da177e4SLinus Torvalds 			}
766102cedc3SLeonid Yegoshin 		}
767102cedc3SLeonid Yegoshin 		break;
768102cedc3SLeonid Yegoshin 	}
7691da177e4SLinus Torvalds 	return 0;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds /*
7731da177e4SLinus Torvalds  * In the Linux kernel, we support selection of FPR format on the
774da0bac33SDavid Daney  * basis of the Status.FR bit.	If an FPU is not present, the FR bit
775da0bac33SDavid Daney  * is hardwired to zero, which would imply a 32-bit FPU even for
776597ce172SPaul Burton  * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
77751d943f0SRalf Baechle  * FPU emu is slow and bulky and optimizing this function offers fairly
77851d943f0SRalf Baechle  * sizeable benefits so we try to be clever and make this function return
77951d943f0SRalf Baechle  * a constant whenever possible, that is on 64-bit kernels without O32
780597ce172SPaul Burton  * compatibility enabled and on 32-bit without 64-bit FPU support.
7811da177e4SLinus Torvalds  */
782da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp)
783da0bac33SDavid Daney {
78408a07904SRalf Baechle 	if (config_enabled(CONFIG_64BIT) && !config_enabled(CONFIG_MIPS32_O32))
78551d943f0SRalf Baechle 		return 1;
78608a07904SRalf Baechle 	else if (config_enabled(CONFIG_32BIT) &&
78708a07904SRalf Baechle 		 !config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
788da0bac33SDavid Daney 		return 0;
78908a07904SRalf Baechle 
790597ce172SPaul Burton 	return !test_thread_flag(TIF_32BIT_FPREGS);
791da0bac33SDavid Daney }
7921da177e4SLinus Torvalds 
7934227a2d4SPaul Burton static inline bool hybrid_fprs(void)
7944227a2d4SPaul Burton {
7954227a2d4SPaul Burton 	return test_thread_flag(TIF_HYBRID_FPREGS);
7964227a2d4SPaul Burton }
7974227a2d4SPaul Burton 
79847fa0c02SRalf Baechle #define SIFROMREG(si, x)						\
79947fa0c02SRalf Baechle do {									\
8004227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs())				\
801c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[x], 0);			\
802bbd426f5SPaul Burton 	else								\
803c8c0da6bSPaul Burton 		(si) = (int)get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);	\
804bbd426f5SPaul Burton } while (0)
805da0bac33SDavid Daney 
80647fa0c02SRalf Baechle #define SITOREG(si, x)							\
80747fa0c02SRalf Baechle do {									\
8084227a2d4SPaul Burton 	if (cop1_64bit(xcp) && !hybrid_fprs()) {			\
809ef1c47afSPaul Burton 		unsigned i;						\
810bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[x], 0, si);				\
811ef1c47afSPaul Burton 		for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)	\
812ef1c47afSPaul Burton 			set_fpr32(&ctx->fpr[x], i, 0);			\
813ef1c47afSPaul Burton 	} else {							\
814bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);		\
815ef1c47afSPaul Burton 	}								\
816bbd426f5SPaul Burton } while (0)
8171da177e4SLinus Torvalds 
818c8c0da6bSPaul Burton #define SIFROMHREG(si, x)	((si) = (int)get_fpr32(&ctx->fpr[x], 1))
819ef1c47afSPaul Burton 
82047fa0c02SRalf Baechle #define SITOHREG(si, x)							\
82147fa0c02SRalf Baechle do {									\
822ef1c47afSPaul Burton 	unsigned i;							\
823ef1c47afSPaul Burton 	set_fpr32(&ctx->fpr[x], 1, si);					\
824ef1c47afSPaul Burton 	for (i = 2; i < ARRAY_SIZE(ctx->fpr[x].val32); i++)		\
825ef1c47afSPaul Burton 		set_fpr32(&ctx->fpr[x], i, 0);				\
826ef1c47afSPaul Burton } while (0)
8271ac94400SLeonid Yegoshin 
828bbd426f5SPaul Burton #define DIFROMREG(di, x)						\
829bbd426f5SPaul Burton 	((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
830bbd426f5SPaul Burton 
83147fa0c02SRalf Baechle #define DITOREG(di, x)							\
83247fa0c02SRalf Baechle do {									\
833ef1c47afSPaul Burton 	unsigned fpr, i;						\
834ef1c47afSPaul Burton 	fpr = (x) & ~(cop1_64bit(xcp) == 0);				\
835ef1c47afSPaul Burton 	set_fpr64(&ctx->fpr[fpr], 0, di);				\
836ef1c47afSPaul Burton 	for (i = 1; i < ARRAY_SIZE(ctx->fpr[x].val64); i++)		\
837ef1c47afSPaul Burton 		set_fpr64(&ctx->fpr[fpr], i, 0);			\
838ef1c47afSPaul Burton } while (0)
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
8411da177e4SLinus Torvalds #define SPTOREG(sp, x)	SITOREG((sp).bits, x)
8421da177e4SLinus Torvalds #define DPFROMREG(dp, x)	DIFROMREG((dp).bits, x)
8431da177e4SLinus Torvalds #define DPTOREG(dp, x)	DITOREG((dp).bits, x)
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds /*
8461da177e4SLinus Torvalds  * Emulate the single floating point instruction pointed at by EPC.
8471da177e4SLinus Torvalds  * Two instructions if the instruction is in a branch delay slot.
8481da177e4SLinus Torvalds  */
8491da177e4SLinus Torvalds 
850515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
851102cedc3SLeonid Yegoshin 		struct mm_decoded_insn dec_insn, void *__user *fault_addr)
8521da177e4SLinus Torvalds {
853102cedc3SLeonid Yegoshin 	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
8543f7cac41SRalf Baechle 	unsigned int cond, cbit;
8553f7cac41SRalf Baechle 	mips_instruction ir;
8563f7cac41SRalf Baechle 	int likely, pc_inc;
8573f7cac41SRalf Baechle 	u32 __user *wva;
8583f7cac41SRalf Baechle 	u64 __user *dva;
8593f7cac41SRalf Baechle 	u32 value;
8603f7cac41SRalf Baechle 	u32 wval;
8613f7cac41SRalf Baechle 	u64 dval;
8623f7cac41SRalf Baechle 	int sig;
8631da177e4SLinus Torvalds 
86470e4c234SRalf Baechle 	/*
86570e4c234SRalf Baechle 	 * These are giving gcc a gentle hint about what to expect in
86670e4c234SRalf Baechle 	 * dec_inst in order to do better optimization.
86770e4c234SRalf Baechle 	 */
86870e4c234SRalf Baechle 	if (!cpu_has_mmips && dec_insn.micro_mips_mode)
86970e4c234SRalf Baechle 		unreachable();
87070e4c234SRalf Baechle 
8711da177e4SLinus Torvalds 	/* XXX NEC Vr54xx bug workaround */
872e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
873102cedc3SLeonid Yegoshin 		if (dec_insn.micro_mips_mode) {
874102cedc3SLeonid Yegoshin 			if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
875e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
876102cedc3SLeonid Yegoshin 		} else {
877102cedc3SLeonid Yegoshin 			if (!isBranchInstr(xcp, dec_insn, &contpc))
878e7e9cae5SRalf Baechle 				clear_delay_slot(xcp);
879102cedc3SLeonid Yegoshin 		}
880102cedc3SLeonid Yegoshin 	}
8811da177e4SLinus Torvalds 
882e7e9cae5SRalf Baechle 	if (delay_slot(xcp)) {
8831da177e4SLinus Torvalds 		/*
8841da177e4SLinus Torvalds 		 * The instruction to be emulated is in a branch delay slot
8851da177e4SLinus Torvalds 		 * which means that we have to	emulate the branch instruction
8861da177e4SLinus Torvalds 		 * BEFORE we do the cop1 instruction.
8871da177e4SLinus Torvalds 		 *
8881da177e4SLinus Torvalds 		 * This branch could be a COP1 branch, but in that case we
8891da177e4SLinus Torvalds 		 * would have had a trap for that instruction, and would not
8901da177e4SLinus Torvalds 		 * come through this route.
8911da177e4SLinus Torvalds 		 *
8921da177e4SLinus Torvalds 		 * Linux MIPS branch emulator operates on context, updating the
8931da177e4SLinus Torvalds 		 * cp0_epc.
8941da177e4SLinus Torvalds 		 */
895102cedc3SLeonid Yegoshin 		ir = dec_insn.next_insn;  /* process delay slot instr */
896102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.next_pc_inc;
897333d1f67SRalf Baechle 	} else {
898102cedc3SLeonid Yegoshin 		ir = dec_insn.insn;       /* process current instr */
899102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.pc_inc;
900102cedc3SLeonid Yegoshin 	}
901102cedc3SLeonid Yegoshin 
902102cedc3SLeonid Yegoshin 	/*
903102cedc3SLeonid Yegoshin 	 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
904102cedc3SLeonid Yegoshin 	 * instructions, we want to convert microMIPS FPU instructions
905102cedc3SLeonid Yegoshin 	 * into MIPS32 instructions so that we could reuse all of the
906102cedc3SLeonid Yegoshin 	 * FPU emulation code.
907102cedc3SLeonid Yegoshin 	 *
908102cedc3SLeonid Yegoshin 	 * NOTE: We cannot do this for branch instructions since they
909102cedc3SLeonid Yegoshin 	 *       are not a subset. Example: Cannot emulate a 16-bit
910102cedc3SLeonid Yegoshin 	 *       aligned target address with a MIPS32 instruction.
911102cedc3SLeonid Yegoshin 	 */
912102cedc3SLeonid Yegoshin 	if (dec_insn.micro_mips_mode) {
913102cedc3SLeonid Yegoshin 		/*
914102cedc3SLeonid Yegoshin 		 * If next instruction is a 16-bit instruction, then it
915102cedc3SLeonid Yegoshin 		 * it cannot be a FPU instruction. This could happen
916102cedc3SLeonid Yegoshin 		 * since we can be called for non-FPU instructions.
917102cedc3SLeonid Yegoshin 		 */
918102cedc3SLeonid Yegoshin 		if ((pc_inc == 2) ||
919102cedc3SLeonid Yegoshin 			(microMIPS32_to_MIPS32((union mips_instruction *)&ir)
920102cedc3SLeonid Yegoshin 			 == SIGILL))
921102cedc3SLeonid Yegoshin 			return SIGILL;
9221da177e4SLinus Torvalds 	}
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds emul:
925a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
926b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(emulated);
9271da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(ir)) {
9283f7cac41SRalf Baechle 	case ldc1_op:
9293f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
9301da177e4SLinus Torvalds 				     MIPSInst_SIMM(ir));
931b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
932515b029dSDavid Daney 
9333f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, dva, sizeof(u64))) {
934b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
9353f7cac41SRalf Baechle 			*fault_addr = dva;
9361da177e4SLinus Torvalds 			return SIGBUS;
9371da177e4SLinus Torvalds 		}
9383f7cac41SRalf Baechle 		if (__get_user(dval, dva)) {
939515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
9403f7cac41SRalf Baechle 			*fault_addr = dva;
941515b029dSDavid Daney 			return SIGSEGV;
942515b029dSDavid Daney 		}
9433f7cac41SRalf Baechle 		DITOREG(dval, MIPSInst_RT(ir));
9441da177e4SLinus Torvalds 		break;
9451da177e4SLinus Torvalds 
9463f7cac41SRalf Baechle 	case sdc1_op:
9473f7cac41SRalf Baechle 		dva = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
9481da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
949b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
9503f7cac41SRalf Baechle 		DIFROMREG(dval, MIPSInst_RT(ir));
9513f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, dva, sizeof(u64))) {
952b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
9533f7cac41SRalf Baechle 			*fault_addr = dva;
9541da177e4SLinus Torvalds 			return SIGBUS;
9551da177e4SLinus Torvalds 		}
9563f7cac41SRalf Baechle 		if (__put_user(dval, dva)) {
957515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
9583f7cac41SRalf Baechle 			*fault_addr = dva;
959515b029dSDavid Daney 			return SIGSEGV;
960515b029dSDavid Daney 		}
9611da177e4SLinus Torvalds 		break;
9621da177e4SLinus Torvalds 
9633f7cac41SRalf Baechle 	case lwc1_op:
9643f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
9651da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
966b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
9673f7cac41SRalf Baechle 		if (!access_ok(VERIFY_READ, wva, sizeof(u32))) {
968b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
9693f7cac41SRalf Baechle 			*fault_addr = wva;
9701da177e4SLinus Torvalds 			return SIGBUS;
9711da177e4SLinus Torvalds 		}
9723f7cac41SRalf Baechle 		if (__get_user(wval, wva)) {
973515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
9743f7cac41SRalf Baechle 			*fault_addr = wva;
975515b029dSDavid Daney 			return SIGSEGV;
976515b029dSDavid Daney 		}
9773f7cac41SRalf Baechle 		SITOREG(wval, MIPSInst_RT(ir));
9781da177e4SLinus Torvalds 		break;
9791da177e4SLinus Torvalds 
9803f7cac41SRalf Baechle 	case swc1_op:
9813f7cac41SRalf Baechle 		wva = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
9821da177e4SLinus Torvalds 				      MIPSInst_SIMM(ir));
983b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
9843f7cac41SRalf Baechle 		SIFROMREG(wval, MIPSInst_RT(ir));
9853f7cac41SRalf Baechle 		if (!access_ok(VERIFY_WRITE, wva, sizeof(u32))) {
986b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
9873f7cac41SRalf Baechle 			*fault_addr = wva;
9881da177e4SLinus Torvalds 			return SIGBUS;
9891da177e4SLinus Torvalds 		}
9903f7cac41SRalf Baechle 		if (__put_user(wval, wva)) {
991515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
9923f7cac41SRalf Baechle 			*fault_addr = wva;
993515b029dSDavid Daney 			return SIGSEGV;
994515b029dSDavid Daney 		}
9951da177e4SLinus Torvalds 		break;
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	case cop1_op:
9981da177e4SLinus Torvalds 		switch (MIPSInst_RS(ir)) {
9991da177e4SLinus Torvalds 		case dmfc_op:
100008a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
100108a07904SRalf Baechle 				return SIGILL;
100208a07904SRalf Baechle 
10031da177e4SLinus Torvalds 			/* copregister fs -> gpr[rt] */
10041da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
10051da177e4SLinus Torvalds 				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
10061da177e4SLinus Torvalds 					MIPSInst_RD(ir));
10071da177e4SLinus Torvalds 			}
10081da177e4SLinus Torvalds 			break;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 		case dmtc_op:
101108a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
101208a07904SRalf Baechle 				return SIGILL;
101308a07904SRalf Baechle 
10141da177e4SLinus Torvalds 			/* copregister fs <- rt */
10151da177e4SLinus Torvalds 			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
10161da177e4SLinus Torvalds 			break;
10171da177e4SLinus Torvalds 
10181ac94400SLeonid Yegoshin 		case mfhc_op:
10191ac94400SLeonid Yegoshin 			if (!cpu_has_mips_r2)
10201ac94400SLeonid Yegoshin 				goto sigill;
10211ac94400SLeonid Yegoshin 
10221ac94400SLeonid Yegoshin 			/* copregister rd -> gpr[rt] */
10231ac94400SLeonid Yegoshin 			if (MIPSInst_RT(ir) != 0) {
10241ac94400SLeonid Yegoshin 				SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
10251ac94400SLeonid Yegoshin 					MIPSInst_RD(ir));
10261ac94400SLeonid Yegoshin 			}
10271ac94400SLeonid Yegoshin 			break;
10281ac94400SLeonid Yegoshin 
10291ac94400SLeonid Yegoshin 		case mthc_op:
10301ac94400SLeonid Yegoshin 			if (!cpu_has_mips_r2)
10311ac94400SLeonid Yegoshin 				goto sigill;
10321ac94400SLeonid Yegoshin 
10331ac94400SLeonid Yegoshin 			/* copregister rd <- gpr[rt] */
10341ac94400SLeonid Yegoshin 			SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
10351ac94400SLeonid Yegoshin 			break;
10361ac94400SLeonid Yegoshin 
10371da177e4SLinus Torvalds 		case mfc_op:
10381da177e4SLinus Torvalds 			/* copregister rd -> gpr[rt] */
10391da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
10401da177e4SLinus Torvalds 				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
10411da177e4SLinus Torvalds 					MIPSInst_RD(ir));
10421da177e4SLinus Torvalds 			}
10431da177e4SLinus Torvalds 			break;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 		case mtc_op:
10461da177e4SLinus Torvalds 			/* copregister rd <- rt */
10471da177e4SLinus Torvalds 			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
10481da177e4SLinus Torvalds 			break;
10491da177e4SLinus Torvalds 
10503f7cac41SRalf Baechle 		case cfc_op:
10511da177e4SLinus Torvalds 			/* cop control register rd -> gpr[rt] */
10521da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) == FPCREG_CSR) {
10531da177e4SLinus Torvalds 				value = ctx->fcr31;
105456a64733SRalf Baechle 				value = (value & ~FPU_CSR_RM) | modeindex(value);
105592df0f8bSRalf Baechle 				pr_debug("%p gpr[%d]<-csr=%08x\n",
1056333d1f67SRalf Baechle 					 (void *) (xcp->cp0_epc),
10571da177e4SLinus Torvalds 					 MIPSInst_RT(ir), value);
10581da177e4SLinus Torvalds 			}
10591da177e4SLinus Torvalds 			else if (MIPSInst_RD(ir) == FPCREG_RID)
10601da177e4SLinus Torvalds 				value = 0;
10611da177e4SLinus Torvalds 			else
10621da177e4SLinus Torvalds 				value = 0;
10631da177e4SLinus Torvalds 			if (MIPSInst_RT(ir))
10641da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RT(ir)] = value;
10651da177e4SLinus Torvalds 			break;
10661da177e4SLinus Torvalds 
10673f7cac41SRalf Baechle 		case ctc_op:
10681da177e4SLinus Torvalds 			/* copregister rd <- rt */
10691da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) == 0)
10701da177e4SLinus Torvalds 				value = 0;
10711da177e4SLinus Torvalds 			else
10721da177e4SLinus Torvalds 				value = xcp->regs[MIPSInst_RT(ir)];
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 			/* we only have one writable control reg
10751da177e4SLinus Torvalds 			 */
10761da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) == FPCREG_CSR) {
107792df0f8bSRalf Baechle 				pr_debug("%p gpr[%d]->csr=%08x\n",
1078333d1f67SRalf Baechle 					 (void *) (xcp->cp0_epc),
10791da177e4SLinus Torvalds 					 MIPSInst_RT(ir), value);
108095e8f634SShane McDonald 
108195e8f634SShane McDonald 				/*
108295e8f634SShane McDonald 				 * Don't write reserved bits,
108395e8f634SShane McDonald 				 * and convert to ieee library modes
108495e8f634SShane McDonald 				 */
108556a64733SRalf Baechle 				ctx->fcr31 = (value & ~(FPU_CSR_RSVD | FPU_CSR_RM)) |
108656a64733SRalf Baechle 					     modeindex(value);
10871da177e4SLinus Torvalds 			}
10881da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
10891da177e4SLinus Torvalds 				return SIGFPE;
10901da177e4SLinus Torvalds 			}
10911da177e4SLinus Torvalds 			break;
10921da177e4SLinus Torvalds 
10933f7cac41SRalf Baechle 		case bc_op:
1094e7e9cae5SRalf Baechle 			if (delay_slot(xcp))
10951da177e4SLinus Torvalds 				return SIGILL;
10961da177e4SLinus Torvalds 
109708a07904SRalf Baechle 			if (cpu_has_mips_4_5_r)
109808a07904SRalf Baechle 				cbit = fpucondbit[MIPSInst_RT(ir) >> 2];
109908a07904SRalf Baechle 			else
110008a07904SRalf Baechle 				cbit = FPU_CSR_COND;
110108a07904SRalf Baechle 			cond = ctx->fcr31 & cbit;
110208a07904SRalf Baechle 
11033f7cac41SRalf Baechle 			likely = 0;
11041da177e4SLinus Torvalds 			switch (MIPSInst_RT(ir) & 3) {
11051da177e4SLinus Torvalds 			case bcfl_op:
11061da177e4SLinus Torvalds 				likely = 1;
11071da177e4SLinus Torvalds 			case bcf_op:
11081da177e4SLinus Torvalds 				cond = !cond;
11091da177e4SLinus Torvalds 				break;
11101da177e4SLinus Torvalds 			case bctl_op:
11111da177e4SLinus Torvalds 				likely = 1;
11121da177e4SLinus Torvalds 			case bct_op:
11131da177e4SLinus Torvalds 				break;
11141da177e4SLinus Torvalds 			default:
11151da177e4SLinus Torvalds 				/* thats an illegal instruction */
11161da177e4SLinus Torvalds 				return SIGILL;
11171da177e4SLinus Torvalds 			}
11181da177e4SLinus Torvalds 
1119e7e9cae5SRalf Baechle 			set_delay_slot(xcp);
11201da177e4SLinus Torvalds 			if (cond) {
11213f7cac41SRalf Baechle 				/*
11223f7cac41SRalf Baechle 				 * Branch taken: emulate dslot instruction
11231da177e4SLinus Torvalds 				 */
1124102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
11251da177e4SLinus Torvalds 
1126102cedc3SLeonid Yegoshin 				contpc = MIPSInst_SIMM(ir);
1127102cedc3SLeonid Yegoshin 				ir = dec_insn.next_insn;
1128102cedc3SLeonid Yegoshin 				if (dec_insn.micro_mips_mode) {
1129102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 1));
1130102cedc3SLeonid Yegoshin 
1131102cedc3SLeonid Yegoshin 					/* If 16-bit instruction, not FPU. */
1132102cedc3SLeonid Yegoshin 					if ((dec_insn.next_pc_inc == 2) ||
1133102cedc3SLeonid Yegoshin 						(microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1134102cedc3SLeonid Yegoshin 
1135102cedc3SLeonid Yegoshin 						/*
1136102cedc3SLeonid Yegoshin 						 * Since this instruction will
1137102cedc3SLeonid Yegoshin 						 * be put on the stack with
1138102cedc3SLeonid Yegoshin 						 * 32-bit words, get around
1139102cedc3SLeonid Yegoshin 						 * this problem by putting a
1140102cedc3SLeonid Yegoshin 						 * NOP16 as the second one.
1141102cedc3SLeonid Yegoshin 						 */
1142102cedc3SLeonid Yegoshin 						if (dec_insn.next_pc_inc == 2)
1143102cedc3SLeonid Yegoshin 							ir = (ir & (~0xffff)) | MM_NOP16;
1144102cedc3SLeonid Yegoshin 
1145102cedc3SLeonid Yegoshin 						/*
1146102cedc3SLeonid Yegoshin 						 * Single step the non-CP1
1147102cedc3SLeonid Yegoshin 						 * instruction in the dslot.
1148102cedc3SLeonid Yegoshin 						 */
1149102cedc3SLeonid Yegoshin 						return mips_dsemul(xcp, ir, contpc);
1150515b029dSDavid Daney 					}
1151102cedc3SLeonid Yegoshin 				} else
1152102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 2));
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 				switch (MIPSInst_OPCODE(ir)) {
11551da177e4SLinus Torvalds 				case lwc1_op:
115608a07904SRalf Baechle 					goto emul;
11573f7cac41SRalf Baechle 
11581da177e4SLinus Torvalds 				case swc1_op:
115908a07904SRalf Baechle 					goto emul;
11603f7cac41SRalf Baechle 
11611da177e4SLinus Torvalds 				case ldc1_op:
11621da177e4SLinus Torvalds 				case sdc1_op:
116308a07904SRalf Baechle 					if (cpu_has_mips_2_3_4_5 ||
116408a07904SRalf Baechle 					    cpu_has_mips64)
116508a07904SRalf Baechle 						goto emul;
116608a07904SRalf Baechle 
116708a07904SRalf Baechle 					return SIGILL;
116808a07904SRalf Baechle 					goto emul;
11693f7cac41SRalf Baechle 
11701da177e4SLinus Torvalds 				case cop1_op:
117108a07904SRalf Baechle 					goto emul;
11723f7cac41SRalf Baechle 
11731da177e4SLinus Torvalds 				case cop1x_op:
1174a5466d7bSMarkos Chandras 					if (cpu_has_mips_4_5 || cpu_has_mips64 || cpu_has_mips32r2)
11751da177e4SLinus Torvalds 						/* its one of ours */
11761da177e4SLinus Torvalds 						goto emul;
117708a07904SRalf Baechle 
117808a07904SRalf Baechle 					return SIGILL;
11793f7cac41SRalf Baechle 
11801da177e4SLinus Torvalds 				case spec_op:
118108a07904SRalf Baechle 					if (!cpu_has_mips_4_5_r)
118208a07904SRalf Baechle 						return SIGILL;
118308a07904SRalf Baechle 
11841da177e4SLinus Torvalds 					if (MIPSInst_FUNC(ir) == movc_op)
11851da177e4SLinus Torvalds 						goto emul;
11861da177e4SLinus Torvalds 					break;
11871da177e4SLinus Torvalds 				}
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 				/*
11901da177e4SLinus Torvalds 				 * Single step the non-cp1
11911da177e4SLinus Torvalds 				 * instruction in the dslot
11921da177e4SLinus Torvalds 				 */
1193e70dfc10SAtsushi Nemoto 				return mips_dsemul(xcp, ir, contpc);
11943f7cac41SRalf Baechle 			} else if (likely) {	/* branch not taken */
11951da177e4SLinus Torvalds 					/*
11961da177e4SLinus Torvalds 					 * branch likely nullifies
11971da177e4SLinus Torvalds 					 * dslot if not taken
11981da177e4SLinus Torvalds 					 */
1199102cedc3SLeonid Yegoshin 					xcp->cp0_epc += dec_insn.pc_inc;
1200102cedc3SLeonid Yegoshin 					contpc += dec_insn.pc_inc;
12011da177e4SLinus Torvalds 					/*
12021da177e4SLinus Torvalds 					 * else continue & execute
12031da177e4SLinus Torvalds 					 * dslot as normal insn
12041da177e4SLinus Torvalds 					 */
12051da177e4SLinus Torvalds 				}
12061da177e4SLinus Torvalds 			break;
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 		default:
12091da177e4SLinus Torvalds 			if (!(MIPSInst_RS(ir) & 0x10))
12101da177e4SLinus Torvalds 				return SIGILL;
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 			/* a real fpu computation instruction */
12131da177e4SLinus Torvalds 			if ((sig = fpu_emu(xcp, ctx, ir)))
12141da177e4SLinus Torvalds 				return sig;
12151da177e4SLinus Torvalds 		}
12161da177e4SLinus Torvalds 		break;
12171da177e4SLinus Torvalds 
12183f7cac41SRalf Baechle 	case cop1x_op:
1219a5466d7bSMarkos Chandras 		if (!cpu_has_mips_4_5 && !cpu_has_mips64 && !cpu_has_mips32r2)
122008a07904SRalf Baechle 			return SIGILL;
122108a07904SRalf Baechle 
122208a07904SRalf Baechle 		sig = fpux_emu(xcp, ctx, ir, fault_addr);
1223515b029dSDavid Daney 		if (sig)
12241da177e4SLinus Torvalds 			return sig;
12251da177e4SLinus Torvalds 		break;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	case spec_op:
122808a07904SRalf Baechle 		if (!cpu_has_mips_4_5_r)
122908a07904SRalf Baechle 			return SIGILL;
123008a07904SRalf Baechle 
12311da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != movc_op)
12321da177e4SLinus Torvalds 			return SIGILL;
12331da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
12341da177e4SLinus Torvalds 		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
12351da177e4SLinus Torvalds 			xcp->regs[MIPSInst_RD(ir)] =
12361da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RS(ir)];
12371da177e4SLinus Torvalds 		break;
12381da177e4SLinus Torvalds 	default:
12391ac94400SLeonid Yegoshin sigill:
12401da177e4SLinus Torvalds 		return SIGILL;
12411da177e4SLinus Torvalds 	}
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	/* we did it !! */
1244e70dfc10SAtsushi Nemoto 	xcp->cp0_epc = contpc;
1245e7e9cae5SRalf Baechle 	clear_delay_slot(xcp);
1246333d1f67SRalf Baechle 
12471da177e4SLinus Torvalds 	return 0;
12481da177e4SLinus Torvalds }
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds /*
12511da177e4SLinus Torvalds  * Conversion table from MIPS compare ops 48-63
12521da177e4SLinus Torvalds  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
12531da177e4SLinus Torvalds  */
12541da177e4SLinus Torvalds static const unsigned char cmptab[8] = {
12551da177e4SLinus Torvalds 	0,			/* cmp_0 (sig) cmp_sf */
12561da177e4SLinus Torvalds 	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
12571da177e4SLinus Torvalds 	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
12581da177e4SLinus Torvalds 	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
12591da177e4SLinus Torvalds 	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
12601da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
12611da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
12621da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
12631da177e4SLinus Torvalds };
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds /*
12671da177e4SLinus Torvalds  * Additional MIPS4 instructions
12681da177e4SLinus Torvalds  */
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3)					\
127147fa0c02SRalf Baechle static union ieee754##p fpemu_##p##_##name(union ieee754##p r,		\
127247fa0c02SRalf Baechle 	union ieee754##p s, union ieee754##p t)				\
12731da177e4SLinus Torvalds {									\
1274cd21dfcfSRalf Baechle 	struct _ieee754_csr ieee754_csr_save;				\
12751da177e4SLinus Torvalds 	s = f1(s, t);							\
12761da177e4SLinus Torvalds 	ieee754_csr_save = ieee754_csr;					\
12771da177e4SLinus Torvalds 	s = f2(s, r);							\
12781da177e4SLinus Torvalds 	ieee754_csr_save.cx |= ieee754_csr.cx;				\
12791da177e4SLinus Torvalds 	ieee754_csr_save.sx |= ieee754_csr.sx;				\
12801da177e4SLinus Torvalds 	s = f3(s);							\
12811da177e4SLinus Torvalds 	ieee754_csr.cx |= ieee754_csr_save.cx;				\
12821da177e4SLinus Torvalds 	ieee754_csr.sx |= ieee754_csr_save.sx;				\
12831da177e4SLinus Torvalds 	return s;							\
12841da177e4SLinus Torvalds }
12851da177e4SLinus Torvalds 
12862209bcb1SRalf Baechle static union ieee754dp fpemu_dp_recip(union ieee754dp d)
12871da177e4SLinus Torvalds {
12881da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), d);
12891da177e4SLinus Torvalds }
12901da177e4SLinus Torvalds 
12912209bcb1SRalf Baechle static union ieee754dp fpemu_dp_rsqrt(union ieee754dp d)
12921da177e4SLinus Torvalds {
12931da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
12962209bcb1SRalf Baechle static union ieee754sp fpemu_sp_recip(union ieee754sp s)
12971da177e4SLinus Torvalds {
12981da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), s);
12991da177e4SLinus Torvalds }
13001da177e4SLinus Torvalds 
13012209bcb1SRalf Baechle static union ieee754sp fpemu_sp_rsqrt(union ieee754sp s)
13021da177e4SLinus Torvalds {
13031da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
13071da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
13081da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
13091da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
13101da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
13111da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
13121da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
13131da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
13141da177e4SLinus Torvalds 
1315eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1316515b029dSDavid Daney 	mips_instruction ir, void *__user *fault_addr)
13171da177e4SLinus Torvalds {
13181da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
13191da177e4SLinus Torvalds 
1320b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1xops);
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	switch (MIPSInst_FMA_FFMT(ir)) {
13231da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
13241da177e4SLinus Torvalds 
13252209bcb1SRalf Baechle 		union ieee754sp(*handler) (union ieee754sp, union ieee754sp, union ieee754sp);
13262209bcb1SRalf Baechle 		union ieee754sp fd, fr, fs, ft;
13273fccc015SRalf Baechle 		u32 __user *va;
13281da177e4SLinus Torvalds 		u32 val;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
13311da177e4SLinus Torvalds 		case lwxc1_op:
13323fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
13331da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
13341da177e4SLinus Torvalds 
1335b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1336515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1337b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1338515b029dSDavid Daney 				*fault_addr = va;
13391da177e4SLinus Torvalds 				return SIGBUS;
13401da177e4SLinus Torvalds 			}
1341515b029dSDavid Daney 			if (__get_user(val, va)) {
1342515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1343515b029dSDavid Daney 				*fault_addr = va;
1344515b029dSDavid Daney 				return SIGSEGV;
1345515b029dSDavid Daney 			}
13461da177e4SLinus Torvalds 			SITOREG(val, MIPSInst_FD(ir));
13471da177e4SLinus Torvalds 			break;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 		case swxc1_op:
13503fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
13511da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
13521da177e4SLinus Torvalds 
1353b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 			SIFROMREG(val, MIPSInst_FS(ir));
1356515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1357515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1358515b029dSDavid Daney 				*fault_addr = va;
1359515b029dSDavid Daney 				return SIGBUS;
1360515b029dSDavid Daney 			}
13611da177e4SLinus Torvalds 			if (put_user(val, va)) {
1362b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1363515b029dSDavid Daney 				*fault_addr = va;
1364515b029dSDavid Daney 				return SIGSEGV;
13651da177e4SLinus Torvalds 			}
13661da177e4SLinus Torvalds 			break;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 		case madd_s_op:
13691da177e4SLinus Torvalds 			handler = fpemu_sp_madd;
13701da177e4SLinus Torvalds 			goto scoptop;
13711da177e4SLinus Torvalds 		case msub_s_op:
13721da177e4SLinus Torvalds 			handler = fpemu_sp_msub;
13731da177e4SLinus Torvalds 			goto scoptop;
13741da177e4SLinus Torvalds 		case nmadd_s_op:
13751da177e4SLinus Torvalds 			handler = fpemu_sp_nmadd;
13761da177e4SLinus Torvalds 			goto scoptop;
13771da177e4SLinus Torvalds 		case nmsub_s_op:
13781da177e4SLinus Torvalds 			handler = fpemu_sp_nmsub;
13791da177e4SLinus Torvalds 			goto scoptop;
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 		      scoptop:
13821da177e4SLinus Torvalds 			SPFROMREG(fr, MIPSInst_FR(ir));
13831da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
13841da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
13851da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
13861da177e4SLinus Torvalds 			SPTOREG(fd, MIPSInst_FD(ir));
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 		      copcsr:
1389c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1390c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
13911da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1392c4103526SDeng-Cheng Zhu 			}
1393c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1394c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
13951da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1396c4103526SDeng-Cheng Zhu 			}
1397c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1398c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
13991da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1400c4103526SDeng-Cheng Zhu 			}
1401c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1402c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
14031da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1404c4103526SDeng-Cheng Zhu 			}
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
14071da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
14083f7cac41SRalf Baechle 				/*printk ("SIGFPE: FPU csr = %08x\n",
14091da177e4SLinus Torvalds 				   ctx->fcr31); */
14101da177e4SLinus Torvalds 				return SIGFPE;
14111da177e4SLinus Torvalds 			}
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 			break;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 		default:
14161da177e4SLinus Torvalds 			return SIGILL;
14171da177e4SLinus Torvalds 		}
14181da177e4SLinus Torvalds 		break;
14191da177e4SLinus Torvalds 	}
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	case d_fmt:{		/* 1 */
14222209bcb1SRalf Baechle 		union ieee754dp(*handler) (union ieee754dp, union ieee754dp, union ieee754dp);
14232209bcb1SRalf Baechle 		union ieee754dp fd, fr, fs, ft;
14243fccc015SRalf Baechle 		u64 __user *va;
14251da177e4SLinus Torvalds 		u64 val;
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
14281da177e4SLinus Torvalds 		case ldxc1_op:
14293fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14301da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14311da177e4SLinus Torvalds 
1432b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1433515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1434b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1435515b029dSDavid Daney 				*fault_addr = va;
14361da177e4SLinus Torvalds 				return SIGBUS;
14371da177e4SLinus Torvalds 			}
1438515b029dSDavid Daney 			if (__get_user(val, va)) {
1439515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1440515b029dSDavid Daney 				*fault_addr = va;
1441515b029dSDavid Daney 				return SIGSEGV;
1442515b029dSDavid Daney 			}
14431da177e4SLinus Torvalds 			DITOREG(val, MIPSInst_FD(ir));
14441da177e4SLinus Torvalds 			break;
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 		case sdxc1_op:
14473fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14481da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14491da177e4SLinus Torvalds 
1450b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
14511da177e4SLinus Torvalds 			DIFROMREG(val, MIPSInst_FS(ir));
1452515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1453b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1454515b029dSDavid Daney 				*fault_addr = va;
14551da177e4SLinus Torvalds 				return SIGBUS;
14561da177e4SLinus Torvalds 			}
1457515b029dSDavid Daney 			if (__put_user(val, va)) {
1458515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1459515b029dSDavid Daney 				*fault_addr = va;
1460515b029dSDavid Daney 				return SIGSEGV;
1461515b029dSDavid Daney 			}
14621da177e4SLinus Torvalds 			break;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 		case madd_d_op:
14651da177e4SLinus Torvalds 			handler = fpemu_dp_madd;
14661da177e4SLinus Torvalds 			goto dcoptop;
14671da177e4SLinus Torvalds 		case msub_d_op:
14681da177e4SLinus Torvalds 			handler = fpemu_dp_msub;
14691da177e4SLinus Torvalds 			goto dcoptop;
14701da177e4SLinus Torvalds 		case nmadd_d_op:
14711da177e4SLinus Torvalds 			handler = fpemu_dp_nmadd;
14721da177e4SLinus Torvalds 			goto dcoptop;
14731da177e4SLinus Torvalds 		case nmsub_d_op:
14741da177e4SLinus Torvalds 			handler = fpemu_dp_nmsub;
14751da177e4SLinus Torvalds 			goto dcoptop;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 		      dcoptop:
14781da177e4SLinus Torvalds 			DPFROMREG(fr, MIPSInst_FR(ir));
14791da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
14801da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
14811da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
14821da177e4SLinus Torvalds 			DPTOREG(fd, MIPSInst_FD(ir));
14831da177e4SLinus Torvalds 			goto copcsr;
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 		default:
14861da177e4SLinus Torvalds 			return SIGILL;
14871da177e4SLinus Torvalds 		}
14881da177e4SLinus Torvalds 		break;
14891da177e4SLinus Torvalds 	}
14901da177e4SLinus Torvalds 
149151061b88SDeng-Cheng Zhu 	case 0x3:
149251061b88SDeng-Cheng Zhu 		if (MIPSInst_FUNC(ir) != pfetch_op)
14931da177e4SLinus Torvalds 			return SIGILL;
149451061b88SDeng-Cheng Zhu 
14951da177e4SLinus Torvalds 		/* ignore prefx operation */
14961da177e4SLinus Torvalds 		break;
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	default:
14991da177e4SLinus Torvalds 		return SIGILL;
15001da177e4SLinus Torvalds 	}
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 	return 0;
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 
15071da177e4SLinus Torvalds /*
15081da177e4SLinus Torvalds  * Emulate a single COP1 arithmetic instruction.
15091da177e4SLinus Torvalds  */
1510eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
15111da177e4SLinus Torvalds 	mips_instruction ir)
15121da177e4SLinus Torvalds {
15131da177e4SLinus Torvalds 	int rfmt;		/* resulting format */
15141da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
15153f7cac41SRalf Baechle 	unsigned int oldrm;
15163f7cac41SRalf Baechle 	unsigned int cbit;
15171da177e4SLinus Torvalds 	unsigned cond;
15181da177e4SLinus Torvalds 	union {
15192209bcb1SRalf Baechle 		union ieee754dp d;
15202209bcb1SRalf Baechle 		union ieee754sp s;
15211da177e4SLinus Torvalds 		int w;
15221da177e4SLinus Torvalds 		s64 l;
15231da177e4SLinus Torvalds 	} rv;			/* resulting value */
15243f7cac41SRalf Baechle 	u64 bits;
15251da177e4SLinus Torvalds 
1526b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1ops);
15271da177e4SLinus Torvalds 	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
15281da177e4SLinus Torvalds 	case s_fmt: {		/* 0 */
15291da177e4SLinus Torvalds 		union {
15302209bcb1SRalf Baechle 			union ieee754sp(*b) (union ieee754sp, union ieee754sp);
15312209bcb1SRalf Baechle 			union ieee754sp(*u) (union ieee754sp);
15321da177e4SLinus Torvalds 		} handler;
15333f7cac41SRalf Baechle 		union ieee754sp fs, ft;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
15361da177e4SLinus Torvalds 			/* binary ops */
15371da177e4SLinus Torvalds 		case fadd_op:
15381da177e4SLinus Torvalds 			handler.b = ieee754sp_add;
15391da177e4SLinus Torvalds 			goto scopbop;
15401da177e4SLinus Torvalds 		case fsub_op:
15411da177e4SLinus Torvalds 			handler.b = ieee754sp_sub;
15421da177e4SLinus Torvalds 			goto scopbop;
15431da177e4SLinus Torvalds 		case fmul_op:
15441da177e4SLinus Torvalds 			handler.b = ieee754sp_mul;
15451da177e4SLinus Torvalds 			goto scopbop;
15461da177e4SLinus Torvalds 		case fdiv_op:
15471da177e4SLinus Torvalds 			handler.b = ieee754sp_div;
15481da177e4SLinus Torvalds 			goto scopbop;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 			/* unary  ops */
15511da177e4SLinus Torvalds 		case fsqrt_op:
155208a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
155308a07904SRalf Baechle 				return SIGILL;
155408a07904SRalf Baechle 
15551da177e4SLinus Torvalds 			handler.u = ieee754sp_sqrt;
15561da177e4SLinus Torvalds 			goto scopuop;
15573f7cac41SRalf Baechle 
155808a07904SRalf Baechle 		/*
155908a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
156008a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
156108a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
156208a07904SRalf Baechle 		 */
15631da177e4SLinus Torvalds 		case frsqrt_op:
1564*e0d32f33SMarkos Chandras 			if (!cpu_has_mips_4_5_r2_r6)
156508a07904SRalf Baechle 				return SIGILL;
156608a07904SRalf Baechle 
15671da177e4SLinus Torvalds 			handler.u = fpemu_sp_rsqrt;
15681da177e4SLinus Torvalds 			goto scopuop;
15693f7cac41SRalf Baechle 
15701da177e4SLinus Torvalds 		case frecip_op:
1571*e0d32f33SMarkos Chandras 			if (!cpu_has_mips_4_5_r2_r6)
157208a07904SRalf Baechle 				return SIGILL;
157308a07904SRalf Baechle 
15741da177e4SLinus Torvalds 			handler.u = fpemu_sp_recip;
15751da177e4SLinus Torvalds 			goto scopuop;
157608a07904SRalf Baechle 
15771da177e4SLinus Torvalds 		case fmovc_op:
157808a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
157908a07904SRalf Baechle 				return SIGILL;
158008a07904SRalf Baechle 
15811da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
15821da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
15831da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
15841da177e4SLinus Torvalds 				return 0;
15851da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
15861da177e4SLinus Torvalds 			break;
15873f7cac41SRalf Baechle 
15881da177e4SLinus Torvalds 		case fmovz_op:
158908a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
159008a07904SRalf Baechle 				return SIGILL;
159108a07904SRalf Baechle 
15921da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
15931da177e4SLinus Torvalds 				return 0;
15941da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
15951da177e4SLinus Torvalds 			break;
15963f7cac41SRalf Baechle 
15971da177e4SLinus Torvalds 		case fmovn_op:
159808a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
159908a07904SRalf Baechle 				return SIGILL;
160008a07904SRalf Baechle 
16011da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
16021da177e4SLinus Torvalds 				return 0;
16031da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
16041da177e4SLinus Torvalds 			break;
16053f7cac41SRalf Baechle 
16061da177e4SLinus Torvalds 		case fabs_op:
16071da177e4SLinus Torvalds 			handler.u = ieee754sp_abs;
16081da177e4SLinus Torvalds 			goto scopuop;
16093f7cac41SRalf Baechle 
16101da177e4SLinus Torvalds 		case fneg_op:
16111da177e4SLinus Torvalds 			handler.u = ieee754sp_neg;
16121da177e4SLinus Torvalds 			goto scopuop;
16133f7cac41SRalf Baechle 
16141da177e4SLinus Torvalds 		case fmov_op:
16151da177e4SLinus Torvalds 			/* an easy one */
16161da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
16171da177e4SLinus Torvalds 			goto copcsr;
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds 			/* binary op on handler */
16201da177e4SLinus Torvalds scopbop:
16211da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
16221da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 			rv.s = (*handler.b) (fs, ft);
16251da177e4SLinus Torvalds 			goto copcsr;
16261da177e4SLinus Torvalds scopuop:
16271da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
16281da177e4SLinus Torvalds 			rv.s = (*handler.u) (fs);
16291da177e4SLinus Torvalds 			goto copcsr;
16301da177e4SLinus Torvalds copcsr:
1631c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INEXACT)) {
1632c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_inexact);
16331da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
1634c4103526SDeng-Cheng Zhu 			}
1635c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_UNDERFLOW)) {
1636c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_underflow);
16371da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
1638c4103526SDeng-Cheng Zhu 			}
1639c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_OVERFLOW)) {
1640c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_overflow);
16411da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
1642c4103526SDeng-Cheng Zhu 			}
1643c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE)) {
1644c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_zerodiv);
16451da177e4SLinus Torvalds 				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
1646c4103526SDeng-Cheng Zhu 			}
1647c4103526SDeng-Cheng Zhu 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION)) {
1648c4103526SDeng-Cheng Zhu 				MIPS_FPU_EMU_INC_STATS(ieee754_invalidop);
16491da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
1650c4103526SDeng-Cheng Zhu 			}
16511da177e4SLinus Torvalds 			break;
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 			/* unary conv ops */
16541da177e4SLinus Torvalds 		case fcvts_op:
16551da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
16561da177e4SLinus Torvalds 
16573f7cac41SRalf Baechle 		case fcvtd_op:
16581da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
16591da177e4SLinus Torvalds 			rv.d = ieee754dp_fsp(fs);
16601da177e4SLinus Torvalds 			rfmt = d_fmt;
16611da177e4SLinus Torvalds 			goto copcsr;
16621da177e4SLinus Torvalds 
16633f7cac41SRalf Baechle 		case fcvtw_op:
16641da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
16651da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
16661da177e4SLinus Torvalds 			rfmt = w_fmt;
16671da177e4SLinus Torvalds 			goto copcsr;
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 		case fround_op:
16701da177e4SLinus Torvalds 		case ftrunc_op:
16711da177e4SLinus Torvalds 		case fceil_op:
16723f7cac41SRalf Baechle 		case ffloor_op:
167308a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5 && !cpu_has_mips64)
167408a07904SRalf Baechle 				return SIGILL;
167508a07904SRalf Baechle 
16763f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
16771da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
167856a64733SRalf Baechle 			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
16791da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
16801da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
16811da177e4SLinus Torvalds 			rfmt = w_fmt;
16821da177e4SLinus Torvalds 			goto copcsr;
16831da177e4SLinus Torvalds 
16843f7cac41SRalf Baechle 		case fcvtl_op:
168508a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
168608a07904SRalf Baechle 				return SIGILL;
168708a07904SRalf Baechle 
16881da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
16891da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
16901da177e4SLinus Torvalds 			rfmt = l_fmt;
16911da177e4SLinus Torvalds 			goto copcsr;
16921da177e4SLinus Torvalds 
16931da177e4SLinus Torvalds 		case froundl_op:
16941da177e4SLinus Torvalds 		case ftruncl_op:
16951da177e4SLinus Torvalds 		case fceill_op:
16963f7cac41SRalf Baechle 		case ffloorl_op:
169708a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
169808a07904SRalf Baechle 				return SIGILL;
169908a07904SRalf Baechle 
17003f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
17011da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
170256a64733SRalf Baechle 			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
17031da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
17041da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
17051da177e4SLinus Torvalds 			rfmt = l_fmt;
17061da177e4SLinus Torvalds 			goto copcsr;
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 		default:
17091da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
17101da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
17112209bcb1SRalf Baechle 				union ieee754sp fs, ft;
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
17141da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
17151da177e4SLinus Torvalds 				rv.w = ieee754sp_cmp(fs, ft,
17161da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
17171da177e4SLinus Torvalds 				rfmt = -1;
17181da177e4SLinus Torvalds 				if ((cmpop & 0x8) && ieee754_cxtest
17191da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
17201da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
17211da177e4SLinus Torvalds 				else
17221da177e4SLinus Torvalds 					goto copcsr;
17231da177e4SLinus Torvalds 
17243f7cac41SRalf Baechle 			} else
17251da177e4SLinus Torvalds 				return SIGILL;
17261da177e4SLinus Torvalds 			break;
17271da177e4SLinus Torvalds 		}
17281da177e4SLinus Torvalds 		break;
17291da177e4SLinus Torvalds 	}
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds 	case d_fmt: {
17323f7cac41SRalf Baechle 		union ieee754dp fs, ft;
17331da177e4SLinus Torvalds 		union {
17342209bcb1SRalf Baechle 			union ieee754dp(*b) (union ieee754dp, union ieee754dp);
17352209bcb1SRalf Baechle 			union ieee754dp(*u) (union ieee754dp);
17361da177e4SLinus Torvalds 		} handler;
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
17391da177e4SLinus Torvalds 			/* binary ops */
17401da177e4SLinus Torvalds 		case fadd_op:
17411da177e4SLinus Torvalds 			handler.b = ieee754dp_add;
17421da177e4SLinus Torvalds 			goto dcopbop;
17431da177e4SLinus Torvalds 		case fsub_op:
17441da177e4SLinus Torvalds 			handler.b = ieee754dp_sub;
17451da177e4SLinus Torvalds 			goto dcopbop;
17461da177e4SLinus Torvalds 		case fmul_op:
17471da177e4SLinus Torvalds 			handler.b = ieee754dp_mul;
17481da177e4SLinus Torvalds 			goto dcopbop;
17491da177e4SLinus Torvalds 		case fdiv_op:
17501da177e4SLinus Torvalds 			handler.b = ieee754dp_div;
17511da177e4SLinus Torvalds 			goto dcopbop;
17521da177e4SLinus Torvalds 
17531da177e4SLinus Torvalds 			/* unary  ops */
17541da177e4SLinus Torvalds 		case fsqrt_op:
175508a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
175608a07904SRalf Baechle 				return SIGILL;
175708a07904SRalf Baechle 
17581da177e4SLinus Torvalds 			handler.u = ieee754dp_sqrt;
17591da177e4SLinus Torvalds 			goto dcopuop;
176008a07904SRalf Baechle 		/*
176108a07904SRalf Baechle 		 * Note that on some MIPS IV implementations such as the
176208a07904SRalf Baechle 		 * R5000 and R8000 the FSQRT and FRECIP instructions do not
176308a07904SRalf Baechle 		 * achieve full IEEE-754 accuracy - however this emulator does.
176408a07904SRalf Baechle 		 */
17651da177e4SLinus Torvalds 		case frsqrt_op:
1766*e0d32f33SMarkos Chandras 			if (!cpu_has_mips_4_5_r2_r6)
176708a07904SRalf Baechle 				return SIGILL;
176808a07904SRalf Baechle 
17691da177e4SLinus Torvalds 			handler.u = fpemu_dp_rsqrt;
17701da177e4SLinus Torvalds 			goto dcopuop;
17711da177e4SLinus Torvalds 		case frecip_op:
1772*e0d32f33SMarkos Chandras 			if (!cpu_has_mips_4_5_r2_r6)
177308a07904SRalf Baechle 				return SIGILL;
177408a07904SRalf Baechle 
17751da177e4SLinus Torvalds 			handler.u = fpemu_dp_recip;
17761da177e4SLinus Torvalds 			goto dcopuop;
17771da177e4SLinus Torvalds 		case fmovc_op:
177808a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
177908a07904SRalf Baechle 				return SIGILL;
178008a07904SRalf Baechle 
17811da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
17821da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
17831da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
17841da177e4SLinus Torvalds 				return 0;
17851da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
17861da177e4SLinus Torvalds 			break;
17871da177e4SLinus Torvalds 		case fmovz_op:
178808a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
178908a07904SRalf Baechle 				return SIGILL;
179008a07904SRalf Baechle 
17911da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
17921da177e4SLinus Torvalds 				return 0;
17931da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
17941da177e4SLinus Torvalds 			break;
17951da177e4SLinus Torvalds 		case fmovn_op:
179608a07904SRalf Baechle 			if (!cpu_has_mips_4_5_r)
179708a07904SRalf Baechle 				return SIGILL;
179808a07904SRalf Baechle 
17991da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
18001da177e4SLinus Torvalds 				return 0;
18011da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
18021da177e4SLinus Torvalds 			break;
18031da177e4SLinus Torvalds 		case fabs_op:
18041da177e4SLinus Torvalds 			handler.u = ieee754dp_abs;
18051da177e4SLinus Torvalds 			goto dcopuop;
18061da177e4SLinus Torvalds 
18071da177e4SLinus Torvalds 		case fneg_op:
18081da177e4SLinus Torvalds 			handler.u = ieee754dp_neg;
18091da177e4SLinus Torvalds 			goto dcopuop;
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds 		case fmov_op:
18121da177e4SLinus Torvalds 			/* an easy one */
18131da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
18141da177e4SLinus Torvalds 			goto copcsr;
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 			/* binary op on handler */
18173f7cac41SRalf Baechle dcopbop:
18181da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18191da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 			rv.d = (*handler.b) (fs, ft);
18221da177e4SLinus Torvalds 			goto copcsr;
18233f7cac41SRalf Baechle dcopuop:
18241da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18251da177e4SLinus Torvalds 			rv.d = (*handler.u) (fs);
18261da177e4SLinus Torvalds 			goto copcsr;
18271da177e4SLinus Torvalds 
18283f7cac41SRalf Baechle 		/*
18293f7cac41SRalf Baechle 		 * unary conv ops
18303f7cac41SRalf Baechle 		 */
18313f7cac41SRalf Baechle 		case fcvts_op:
18321da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18331da177e4SLinus Torvalds 			rv.s = ieee754sp_fdp(fs);
18341da177e4SLinus Torvalds 			rfmt = s_fmt;
18351da177e4SLinus Torvalds 			goto copcsr;
18363f7cac41SRalf Baechle 
18371da177e4SLinus Torvalds 		case fcvtd_op:
18381da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
18391da177e4SLinus Torvalds 
18403f7cac41SRalf Baechle 		case fcvtw_op:
18411da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18421da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);	/* wrong */
18431da177e4SLinus Torvalds 			rfmt = w_fmt;
18441da177e4SLinus Torvalds 			goto copcsr;
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 		case fround_op:
18471da177e4SLinus Torvalds 		case ftrunc_op:
18481da177e4SLinus Torvalds 		case fceil_op:
18493f7cac41SRalf Baechle 		case ffloor_op:
185008a07904SRalf Baechle 			if (!cpu_has_mips_2_3_4_5_r)
185108a07904SRalf Baechle 				return SIGILL;
185208a07904SRalf Baechle 
18533f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
18541da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
185556a64733SRalf Baechle 			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
18561da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);
18571da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
18581da177e4SLinus Torvalds 			rfmt = w_fmt;
18591da177e4SLinus Torvalds 			goto copcsr;
18601da177e4SLinus Torvalds 
18613f7cac41SRalf Baechle 		case fcvtl_op:
186208a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
186308a07904SRalf Baechle 				return SIGILL;
186408a07904SRalf Baechle 
18651da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18661da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
18671da177e4SLinus Torvalds 			rfmt = l_fmt;
18681da177e4SLinus Torvalds 			goto copcsr;
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 		case froundl_op:
18711da177e4SLinus Torvalds 		case ftruncl_op:
18721da177e4SLinus Torvalds 		case fceill_op:
18733f7cac41SRalf Baechle 		case ffloorl_op:
187408a07904SRalf Baechle 			if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
187508a07904SRalf Baechle 				return SIGILL;
187608a07904SRalf Baechle 
18773f7cac41SRalf Baechle 			oldrm = ieee754_csr.rm;
18781da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
187956a64733SRalf Baechle 			ieee754_csr.rm = modeindex(MIPSInst_FUNC(ir));
18801da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
18811da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
18821da177e4SLinus Torvalds 			rfmt = l_fmt;
18831da177e4SLinus Torvalds 			goto copcsr;
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 		default:
18861da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
18871da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
18882209bcb1SRalf Baechle 				union ieee754dp fs, ft;
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
18911da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
18921da177e4SLinus Torvalds 				rv.w = ieee754dp_cmp(fs, ft,
18931da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
18941da177e4SLinus Torvalds 				rfmt = -1;
18951da177e4SLinus Torvalds 				if ((cmpop & 0x8)
18961da177e4SLinus Torvalds 					&&
18971da177e4SLinus Torvalds 					ieee754_cxtest
18981da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
18991da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
19001da177e4SLinus Torvalds 				else
19011da177e4SLinus Torvalds 					goto copcsr;
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds 			}
19041da177e4SLinus Torvalds 			else {
19051da177e4SLinus Torvalds 				return SIGILL;
19061da177e4SLinus Torvalds 			}
19071da177e4SLinus Torvalds 			break;
19081da177e4SLinus Torvalds 		}
19091da177e4SLinus Torvalds 		break;
19101da177e4SLinus Torvalds 
19113f7cac41SRalf Baechle 	case w_fmt:
19121da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
19131da177e4SLinus Torvalds 		case fcvts_op:
19141da177e4SLinus Torvalds 			/* convert word to single precision real */
19151da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19161da177e4SLinus Torvalds 			rv.s = ieee754sp_fint(fs.bits);
19171da177e4SLinus Torvalds 			rfmt = s_fmt;
19181da177e4SLinus Torvalds 			goto copcsr;
19191da177e4SLinus Torvalds 		case fcvtd_op:
19201da177e4SLinus Torvalds 			/* convert word to double precision real */
19211da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19221da177e4SLinus Torvalds 			rv.d = ieee754dp_fint(fs.bits);
19231da177e4SLinus Torvalds 			rfmt = d_fmt;
19241da177e4SLinus Torvalds 			goto copcsr;
19251da177e4SLinus Torvalds 		default:
19261da177e4SLinus Torvalds 			return SIGILL;
19271da177e4SLinus Torvalds 		}
19281da177e4SLinus Torvalds 		break;
19291da177e4SLinus Torvalds 	}
19301da177e4SLinus Torvalds 
19313f7cac41SRalf Baechle 	case l_fmt:
193208a07904SRalf Baechle 
193308a07904SRalf Baechle 		if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
193408a07904SRalf Baechle 			return SIGILL;
193508a07904SRalf Baechle 
1936bbd426f5SPaul Burton 		DIFROMREG(bits, MIPSInst_FS(ir));
1937bbd426f5SPaul Burton 
19381da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
19391da177e4SLinus Torvalds 		case fcvts_op:
19401da177e4SLinus Torvalds 			/* convert long to single precision real */
1941bbd426f5SPaul Burton 			rv.s = ieee754sp_flong(bits);
19421da177e4SLinus Torvalds 			rfmt = s_fmt;
19431da177e4SLinus Torvalds 			goto copcsr;
19441da177e4SLinus Torvalds 		case fcvtd_op:
19451da177e4SLinus Torvalds 			/* convert long to double precision real */
1946bbd426f5SPaul Burton 			rv.d = ieee754dp_flong(bits);
19471da177e4SLinus Torvalds 			rfmt = d_fmt;
19481da177e4SLinus Torvalds 			goto copcsr;
19491da177e4SLinus Torvalds 		default:
19501da177e4SLinus Torvalds 			return SIGILL;
19511da177e4SLinus Torvalds 		}
19521da177e4SLinus Torvalds 		break;
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds 	default:
19551da177e4SLinus Torvalds 		return SIGILL;
19561da177e4SLinus Torvalds 	}
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 	/*
19591da177e4SLinus Torvalds 	 * Update the fpu CSR register for this operation.
19601da177e4SLinus Torvalds 	 * If an exception is required, generate a tidy SIGFPE exception,
19611da177e4SLinus Torvalds 	 * without updating the result register.
19621da177e4SLinus Torvalds 	 * Note: cause exception bits do not accumulate, they are rewritten
19631da177e4SLinus Torvalds 	 * for each op; only the flag/sticky bits accumulate.
19641da177e4SLinus Torvalds 	 */
19651da177e4SLinus Torvalds 	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
19661da177e4SLinus Torvalds 	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
19673f7cac41SRalf Baechle 		/*printk ("SIGFPE: FPU csr = %08x\n",ctx->fcr31); */
19681da177e4SLinus Torvalds 		return SIGFPE;
19691da177e4SLinus Torvalds 	}
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	/*
19721da177e4SLinus Torvalds 	 * Now we can safely write the result back to the register file.
19731da177e4SLinus Torvalds 	 */
19741da177e4SLinus Torvalds 	switch (rfmt) {
197508a07904SRalf Baechle 	case -1:
197608a07904SRalf Baechle 
197708a07904SRalf Baechle 		if (cpu_has_mips_4_5_r)
1978c3b9b945SRob Kendrick 			cbit = fpucondbit[MIPSInst_FD(ir) >> 2];
19791da177e4SLinus Torvalds 		else
198008a07904SRalf Baechle 			cbit = FPU_CSR_COND;
198108a07904SRalf Baechle 		if (rv.w)
198208a07904SRalf Baechle 			ctx->fcr31 |= cbit;
198308a07904SRalf Baechle 		else
198408a07904SRalf Baechle 			ctx->fcr31 &= ~cbit;
19851da177e4SLinus Torvalds 		break;
198608a07904SRalf Baechle 
19871da177e4SLinus Torvalds 	case d_fmt:
19881da177e4SLinus Torvalds 		DPTOREG(rv.d, MIPSInst_FD(ir));
19891da177e4SLinus Torvalds 		break;
19901da177e4SLinus Torvalds 	case s_fmt:
19911da177e4SLinus Torvalds 		SPTOREG(rv.s, MIPSInst_FD(ir));
19921da177e4SLinus Torvalds 		break;
19931da177e4SLinus Torvalds 	case w_fmt:
19941da177e4SLinus Torvalds 		SITOREG(rv.w, MIPSInst_FD(ir));
19951da177e4SLinus Torvalds 		break;
19961da177e4SLinus Torvalds 	case l_fmt:
199708a07904SRalf Baechle 		if (!cpu_has_mips_3_4_5 && !cpu_has_mips64)
199808a07904SRalf Baechle 			return SIGILL;
199908a07904SRalf Baechle 
20001da177e4SLinus Torvalds 		DITOREG(rv.l, MIPSInst_FD(ir));
20011da177e4SLinus Torvalds 		break;
20021da177e4SLinus Torvalds 	default:
20031da177e4SLinus Torvalds 		return SIGILL;
20041da177e4SLinus Torvalds 	}
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 	return 0;
20071da177e4SLinus Torvalds }
20081da177e4SLinus Torvalds 
2009e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2010515b029dSDavid Daney 	int has_fpu, void *__user *fault_addr)
20111da177e4SLinus Torvalds {
2012333d1f67SRalf Baechle 	unsigned long oldepc, prevepc;
2013102cedc3SLeonid Yegoshin 	struct mm_decoded_insn dec_insn;
2014102cedc3SLeonid Yegoshin 	u16 instr[4];
2015102cedc3SLeonid Yegoshin 	u16 *instr_ptr;
20161da177e4SLinus Torvalds 	int sig = 0;
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 	oldepc = xcp->cp0_epc;
20191da177e4SLinus Torvalds 	do {
20201da177e4SLinus Torvalds 		prevepc = xcp->cp0_epc;
20211da177e4SLinus Torvalds 
2022102cedc3SLeonid Yegoshin 		if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2023102cedc3SLeonid Yegoshin 			/*
2024102cedc3SLeonid Yegoshin 			 * Get next 2 microMIPS instructions and convert them
2025102cedc3SLeonid Yegoshin 			 * into 32-bit instructions.
2026102cedc3SLeonid Yegoshin 			 */
2027102cedc3SLeonid Yegoshin 			if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2028102cedc3SLeonid Yegoshin 			    (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2029102cedc3SLeonid Yegoshin 			    (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2030102cedc3SLeonid Yegoshin 			    (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2031b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
20321da177e4SLinus Torvalds 				return SIGBUS;
20331da177e4SLinus Torvalds 			}
2034102cedc3SLeonid Yegoshin 			instr_ptr = instr;
2035102cedc3SLeonid Yegoshin 
2036102cedc3SLeonid Yegoshin 			/* Get first instruction. */
2037102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2038102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2039102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2040102cedc3SLeonid Yegoshin 					(*instr_ptr);
2041102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2042102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 2;
2043102cedc3SLeonid Yegoshin 				instr_ptr += 1;
2044102cedc3SLeonid Yegoshin 			} else {
2045102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2046102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2047102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2048102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 4;
2049102cedc3SLeonid Yegoshin 				instr_ptr += 2;
2050515b029dSDavid Daney 			}
2051102cedc3SLeonid Yegoshin 			/* Get second instruction. */
2052102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2053102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2054102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2055102cedc3SLeonid Yegoshin 					(*instr_ptr);
2056102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2057102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 2;
2058102cedc3SLeonid Yegoshin 			} else {
2059102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2060102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2061102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2062102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 4;
2063102cedc3SLeonid Yegoshin 			}
2064102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 1;
2065102cedc3SLeonid Yegoshin 		} else {
2066102cedc3SLeonid Yegoshin 			if ((get_user(dec_insn.insn,
2067102cedc3SLeonid Yegoshin 			    (mips_instruction __user *) xcp->cp0_epc)) ||
2068102cedc3SLeonid Yegoshin 			    (get_user(dec_insn.next_insn,
2069102cedc3SLeonid Yegoshin 			    (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2070102cedc3SLeonid Yegoshin 				MIPS_FPU_EMU_INC_STATS(errors);
2071102cedc3SLeonid Yegoshin 				return SIGBUS;
2072102cedc3SLeonid Yegoshin 			}
2073102cedc3SLeonid Yegoshin 			dec_insn.pc_inc = 4;
2074102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc = 4;
2075102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 0;
2076102cedc3SLeonid Yegoshin 		}
2077102cedc3SLeonid Yegoshin 
2078102cedc3SLeonid Yegoshin 		if ((dec_insn.insn == 0) ||
2079102cedc3SLeonid Yegoshin 		   ((dec_insn.pc_inc == 2) &&
2080102cedc3SLeonid Yegoshin 		   ((dec_insn.insn & 0xffff) == MM_NOP16)))
2081102cedc3SLeonid Yegoshin 			xcp->cp0_epc += dec_insn.pc_inc;	/* Skip NOPs */
20821da177e4SLinus Torvalds 		else {
2083cd21dfcfSRalf Baechle 			/*
2084cd21dfcfSRalf Baechle 			 * The 'ieee754_csr' is an alias of
2085cd21dfcfSRalf Baechle 			 * ctx->fcr31.	No need to copy ctx->fcr31 to
2086cd21dfcfSRalf Baechle 			 * ieee754_csr.	 But ieee754_csr.rm is ieee
2087cd21dfcfSRalf Baechle 			 * library modes. (not mips rounding mode)
2088cd21dfcfSRalf Baechle 			 */
2089102cedc3SLeonid Yegoshin 			sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
20901da177e4SLinus Torvalds 		}
20911da177e4SLinus Torvalds 
2092e04582b7SAtsushi Nemoto 		if (has_fpu)
20931da177e4SLinus Torvalds 			break;
20941da177e4SLinus Torvalds 		if (sig)
20951da177e4SLinus Torvalds 			break;
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 		cond_resched();
20981da177e4SLinus Torvalds 	} while (xcp->cp0_epc > prevepc);
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 	/* SIGILL indicates a non-fpu instruction */
21011da177e4SLinus Torvalds 	if (sig == SIGILL && xcp->cp0_epc != oldepc)
21023f7cac41SRalf Baechle 		/* but if EPC has advanced, then ignore it */
21031da177e4SLinus Torvalds 		sig = 0;
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds 	return sig;
21061da177e4SLinus Torvalds }
2107