xref: /linux/arch/mips/math-emu/cp1emu.c (revision bbd426f542cb61f2322e15dab4507f2661090c06)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * 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.,
211da177e4SLinus Torvalds  *  59 Temple Place - Suite 330, Boston MA 02111-1307, 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
281da177e4SLinus Torvalds  * 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  *
331da177e4SLinus Torvalds  * 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>
37b6ee75edSDavid Daney #include <linux/module.h>
3883fd38caSAtsushi Nemoto #include <linux/debugfs.h>
397f788d2dSDeng-Cheng Zhu #include <linux/perf_event.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <asm/inst.h>
421da177e4SLinus Torvalds #include <asm/bootinfo.h>
431da177e4SLinus Torvalds #include <asm/processor.h>
441da177e4SLinus Torvalds #include <asm/ptrace.h>
451da177e4SLinus Torvalds #include <asm/signal.h>
461da177e4SLinus Torvalds #include <asm/mipsregs.h>
471da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
48102cedc3SLeonid Yegoshin #include <asm/fpu.h>
491da177e4SLinus Torvalds #include <asm/uaccess.h>
501da177e4SLinus Torvalds #include <asm/branch.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include "ieee754.h"
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /* Strap kernel emulator for full MIPS IV emulation */
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #ifdef __mips
571da177e4SLinus Torvalds #undef __mips
581da177e4SLinus Torvalds #endif
591da177e4SLinus Torvalds #define __mips 4
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /* Function which emulates a floating point instruction. */
621da177e4SLinus Torvalds 
63eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *, struct mips_fpu_struct *,
641da177e4SLinus Torvalds 	mips_instruction);
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
671da177e4SLinus Torvalds static int fpux_emu(struct pt_regs *,
68515b029dSDavid Daney 	struct mips_fpu_struct *, mips_instruction, void *__user *);
691da177e4SLinus Torvalds #endif
701da177e4SLinus Torvalds 
71eae89076SAtsushi Nemoto /* Further private data for which no space exists in mips_fpu_struct */
721da177e4SLinus Torvalds 
73b6ee75edSDavid Daney #ifdef CONFIG_DEBUG_FS
74b6ee75edSDavid Daney DEFINE_PER_CPU(struct mips_fpu_emulator_stats, fpuemustats);
75b6ee75edSDavid Daney #endif
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /* Control registers */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds #define FPCREG_RID	0	/* $0  = revision id */
801da177e4SLinus Torvalds #define FPCREG_CSR	31	/* $31 = csr */
811da177e4SLinus Torvalds 
8295e8f634SShane McDonald /* Determine rounding mode from the RM bits of the FCSR */
8395e8f634SShane McDonald #define modeindex(v) ((v) & FPU_CSR_RM)
8495e8f634SShane McDonald 
85102cedc3SLeonid Yegoshin /* microMIPS bitfields */
86102cedc3SLeonid Yegoshin #define MM_POOL32A_MINOR_MASK	0x3f
87102cedc3SLeonid Yegoshin #define MM_POOL32A_MINOR_SHIFT	0x6
88102cedc3SLeonid Yegoshin #define MM_MIPS32_COND_FC	0x30
89102cedc3SLeonid Yegoshin 
901da177e4SLinus Torvalds /* Convert Mips rounding mode (0..3) to IEEE library modes. */
911da177e4SLinus Torvalds static const unsigned char ieee_rm[4] = {
92cd21dfcfSRalf Baechle 	[FPU_CSR_RN] = IEEE754_RN,
93cd21dfcfSRalf Baechle 	[FPU_CSR_RZ] = IEEE754_RZ,
94cd21dfcfSRalf Baechle 	[FPU_CSR_RU] = IEEE754_RU,
95cd21dfcfSRalf Baechle 	[FPU_CSR_RD] = IEEE754_RD,
96cd21dfcfSRalf Baechle };
97cd21dfcfSRalf Baechle /* Convert IEEE library modes to Mips rounding mode (0..3). */
98cd21dfcfSRalf Baechle static const unsigned char mips_rm[4] = {
99cd21dfcfSRalf Baechle 	[IEEE754_RN] = FPU_CSR_RN,
100cd21dfcfSRalf Baechle 	[IEEE754_RZ] = FPU_CSR_RZ,
101cd21dfcfSRalf Baechle 	[IEEE754_RD] = FPU_CSR_RD,
102cd21dfcfSRalf Baechle 	[IEEE754_RU] = FPU_CSR_RU,
1031da177e4SLinus Torvalds };
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #if __mips >= 4
1061da177e4SLinus Torvalds /* convert condition code register number to csr bit */
1071da177e4SLinus Torvalds static const unsigned int fpucondbit[8] = {
1081da177e4SLinus Torvalds 	FPU_CSR_COND0,
1091da177e4SLinus Torvalds 	FPU_CSR_COND1,
1101da177e4SLinus Torvalds 	FPU_CSR_COND2,
1111da177e4SLinus Torvalds 	FPU_CSR_COND3,
1121da177e4SLinus Torvalds 	FPU_CSR_COND4,
1131da177e4SLinus Torvalds 	FPU_CSR_COND5,
1141da177e4SLinus Torvalds 	FPU_CSR_COND6,
1151da177e4SLinus Torvalds 	FPU_CSR_COND7
1161da177e4SLinus Torvalds };
1171da177e4SLinus Torvalds #endif
1181da177e4SLinus Torvalds 
119102cedc3SLeonid Yegoshin /* (microMIPS) Convert 16-bit register encoding to 32-bit register encoding. */
120102cedc3SLeonid Yegoshin static const unsigned int reg16to32map[8] = {16, 17, 2, 3, 4, 5, 6, 7};
121102cedc3SLeonid Yegoshin 
122102cedc3SLeonid Yegoshin /* (microMIPS) Convert certain microMIPS instructions to MIPS32 format. */
123102cedc3SLeonid Yegoshin static const int sd_format[] = {16, 17, 0, 0, 0, 0, 0, 0};
124102cedc3SLeonid Yegoshin static const int sdps_format[] = {16, 17, 22, 0, 0, 0, 0, 0};
125102cedc3SLeonid Yegoshin static const int dwl_format[] = {17, 20, 21, 0, 0, 0, 0, 0};
126102cedc3SLeonid Yegoshin static const int swl_format[] = {16, 20, 21, 0, 0, 0, 0, 0};
127102cedc3SLeonid Yegoshin 
128102cedc3SLeonid Yegoshin /*
129102cedc3SLeonid Yegoshin  * This functions translates a 32-bit microMIPS instruction
130102cedc3SLeonid Yegoshin  * into a 32-bit MIPS32 instruction. Returns 0 on success
131102cedc3SLeonid Yegoshin  * and SIGILL otherwise.
132102cedc3SLeonid Yegoshin  */
133102cedc3SLeonid Yegoshin static int microMIPS32_to_MIPS32(union mips_instruction *insn_ptr)
134102cedc3SLeonid Yegoshin {
135102cedc3SLeonid Yegoshin 	union mips_instruction insn = *insn_ptr;
136102cedc3SLeonid Yegoshin 	union mips_instruction mips32_insn = insn;
137102cedc3SLeonid Yegoshin 	int func, fmt, op;
138102cedc3SLeonid Yegoshin 
139102cedc3SLeonid Yegoshin 	switch (insn.mm_i_format.opcode) {
140102cedc3SLeonid Yegoshin 	case mm_ldc132_op:
141102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = ldc1_op;
142102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
143102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
144102cedc3SLeonid Yegoshin 		break;
145102cedc3SLeonid Yegoshin 	case mm_lwc132_op:
146102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = lwc1_op;
147102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
148102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
149102cedc3SLeonid Yegoshin 		break;
150102cedc3SLeonid Yegoshin 	case mm_sdc132_op:
151102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = sdc1_op;
152102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
153102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
154102cedc3SLeonid Yegoshin 		break;
155102cedc3SLeonid Yegoshin 	case mm_swc132_op:
156102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.opcode = swc1_op;
157102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rt = insn.mm_i_format.rs;
158102cedc3SLeonid Yegoshin 		mips32_insn.mm_i_format.rs = insn.mm_i_format.rt;
159102cedc3SLeonid Yegoshin 		break;
160102cedc3SLeonid Yegoshin 	case mm_pool32i_op:
161102cedc3SLeonid Yegoshin 		/* NOTE: offset is << by 1 if in microMIPS mode. */
162102cedc3SLeonid Yegoshin 		if ((insn.mm_i_format.rt == mm_bc1f_op) ||
163102cedc3SLeonid Yegoshin 		    (insn.mm_i_format.rt == mm_bc1t_op)) {
164102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.opcode = cop1_op;
165102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.bc = bc_op;
166102cedc3SLeonid Yegoshin 			mips32_insn.fb_format.flag =
167102cedc3SLeonid Yegoshin 				(insn.mm_i_format.rt == mm_bc1t_op) ? 1 : 0;
168102cedc3SLeonid Yegoshin 		} else
169102cedc3SLeonid Yegoshin 			return SIGILL;
170102cedc3SLeonid Yegoshin 		break;
171102cedc3SLeonid Yegoshin 	case mm_pool32f_op:
172102cedc3SLeonid Yegoshin 		switch (insn.mm_fp0_format.func) {
173102cedc3SLeonid Yegoshin 		case mm_32f_01_op:
174102cedc3SLeonid Yegoshin 		case mm_32f_11_op:
175102cedc3SLeonid Yegoshin 		case mm_32f_02_op:
176102cedc3SLeonid Yegoshin 		case mm_32f_12_op:
177102cedc3SLeonid Yegoshin 		case mm_32f_41_op:
178102cedc3SLeonid Yegoshin 		case mm_32f_51_op:
179102cedc3SLeonid Yegoshin 		case mm_32f_42_op:
180102cedc3SLeonid Yegoshin 		case mm_32f_52_op:
181102cedc3SLeonid Yegoshin 			op = insn.mm_fp0_format.func;
182102cedc3SLeonid Yegoshin 			if (op == mm_32f_01_op)
183102cedc3SLeonid Yegoshin 				func = madd_s_op;
184102cedc3SLeonid Yegoshin 			else if (op == mm_32f_11_op)
185102cedc3SLeonid Yegoshin 				func = madd_d_op;
186102cedc3SLeonid Yegoshin 			else if (op == mm_32f_02_op)
187102cedc3SLeonid Yegoshin 				func = nmadd_s_op;
188102cedc3SLeonid Yegoshin 			else if (op == mm_32f_12_op)
189102cedc3SLeonid Yegoshin 				func = nmadd_d_op;
190102cedc3SLeonid Yegoshin 			else if (op == mm_32f_41_op)
191102cedc3SLeonid Yegoshin 				func = msub_s_op;
192102cedc3SLeonid Yegoshin 			else if (op == mm_32f_51_op)
193102cedc3SLeonid Yegoshin 				func = msub_d_op;
194102cedc3SLeonid Yegoshin 			else if (op == mm_32f_42_op)
195102cedc3SLeonid Yegoshin 				func = nmsub_s_op;
196102cedc3SLeonid Yegoshin 			else
197102cedc3SLeonid Yegoshin 				func = nmsub_d_op;
198102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.opcode = cop1x_op;
199102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fr = insn.mm_fp6_format.fr;
200102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.ft = insn.mm_fp6_format.ft;
201102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fs = insn.mm_fp6_format.fs;
202102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.fd = insn.mm_fp6_format.fd;
203102cedc3SLeonid Yegoshin 			mips32_insn.fp6_format.func = func;
204102cedc3SLeonid Yegoshin 			break;
205102cedc3SLeonid Yegoshin 		case mm_32f_10_op:
206102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
207102cedc3SLeonid Yegoshin 			op = insn.mm_fp5_format.op & 0x7;
208102cedc3SLeonid Yegoshin 			if (op == mm_ldxc1_op)
209102cedc3SLeonid Yegoshin 				func = ldxc1_op;
210102cedc3SLeonid Yegoshin 			else if (op == mm_sdxc1_op)
211102cedc3SLeonid Yegoshin 				func = sdxc1_op;
212102cedc3SLeonid Yegoshin 			else if (op == mm_lwxc1_op)
213102cedc3SLeonid Yegoshin 				func = lwxc1_op;
214102cedc3SLeonid Yegoshin 			else if (op == mm_swxc1_op)
215102cedc3SLeonid Yegoshin 				func = swxc1_op;
216102cedc3SLeonid Yegoshin 
217102cedc3SLeonid Yegoshin 			if (func != -1) {
218102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = cop1x_op;
219102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs =
220102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.base;
221102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
222102cedc3SLeonid Yegoshin 					insn.mm_fp5_format.index;
223102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = 0;
224102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = insn.mm_fp5_format.fd;
225102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = func;
226102cedc3SLeonid Yegoshin 			} else
227102cedc3SLeonid Yegoshin 				return SIGILL;
228102cedc3SLeonid Yegoshin 			break;
229102cedc3SLeonid Yegoshin 		case mm_32f_40_op:
230102cedc3SLeonid Yegoshin 			op = -1;	/* Invalid */
231102cedc3SLeonid Yegoshin 			if (insn.mm_fp2_format.op == mm_fmovt_op)
232102cedc3SLeonid Yegoshin 				op = 1;
233102cedc3SLeonid Yegoshin 			else if (insn.mm_fp2_format.op == mm_fmovf_op)
234102cedc3SLeonid Yegoshin 				op = 0;
235102cedc3SLeonid Yegoshin 			if (op != -1) {
236102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
237102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
238102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp2_format.fmt];
239102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
240102cedc3SLeonid Yegoshin 					(insn.mm_fp2_format.cc<<2) + op;
241102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
242102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fs;
243102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
244102cedc3SLeonid Yegoshin 					insn.mm_fp2_format.fd;
245102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = fmovc_op;
246102cedc3SLeonid Yegoshin 			} else
247102cedc3SLeonid Yegoshin 				return SIGILL;
248102cedc3SLeonid Yegoshin 			break;
249102cedc3SLeonid Yegoshin 		case mm_32f_60_op:
250102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
251102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fadd_op)
252102cedc3SLeonid Yegoshin 				func = fadd_op;
253102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fsub_op)
254102cedc3SLeonid Yegoshin 				func = fsub_op;
255102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmul_op)
256102cedc3SLeonid Yegoshin 				func = fmul_op;
257102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fdiv_op)
258102cedc3SLeonid Yegoshin 				func = fdiv_op;
259102cedc3SLeonid Yegoshin 			if (func != -1) {
260102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
261102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
262102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
263102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
264102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
265102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
266102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
267102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
268102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
269102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
270102cedc3SLeonid Yegoshin 			} else
271102cedc3SLeonid Yegoshin 				return SIGILL;
272102cedc3SLeonid Yegoshin 			break;
273102cedc3SLeonid Yegoshin 		case mm_32f_70_op:
274102cedc3SLeonid Yegoshin 			func = -1;	/* Invalid */
275102cedc3SLeonid Yegoshin 			if (insn.mm_fp0_format.op == mm_fmovn_op)
276102cedc3SLeonid Yegoshin 				func = fmovn_op;
277102cedc3SLeonid Yegoshin 			else if (insn.mm_fp0_format.op == mm_fmovz_op)
278102cedc3SLeonid Yegoshin 				func = fmovz_op;
279102cedc3SLeonid Yegoshin 			if (func != -1) {
280102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
281102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
282102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp0_format.fmt];
283102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft =
284102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.ft;
285102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
286102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fs;
287102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
288102cedc3SLeonid Yegoshin 					insn.mm_fp0_format.fd;
289102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
290102cedc3SLeonid Yegoshin 			} else
291102cedc3SLeonid Yegoshin 				return SIGILL;
292102cedc3SLeonid Yegoshin 			break;
293102cedc3SLeonid Yegoshin 		case mm_32f_73_op:    /* POOL32FXF */
294102cedc3SLeonid Yegoshin 			switch (insn.mm_fp1_format.op) {
295102cedc3SLeonid Yegoshin 			case mm_movf0_op:
296102cedc3SLeonid Yegoshin 			case mm_movf1_op:
297102cedc3SLeonid Yegoshin 			case mm_movt0_op:
298102cedc3SLeonid Yegoshin 			case mm_movt1_op:
299102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
300102cedc3SLeonid Yegoshin 				    mm_movf0_op)
301102cedc3SLeonid Yegoshin 					op = 0;
302102cedc3SLeonid Yegoshin 				else
303102cedc3SLeonid Yegoshin 					op = 1;
304102cedc3SLeonid Yegoshin 				mips32_insn.r_format.opcode = spec_op;
305102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rs = insn.mm_fp4_format.fs;
306102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rt =
307102cedc3SLeonid Yegoshin 					(insn.mm_fp4_format.cc << 2) + op;
308102cedc3SLeonid Yegoshin 				mips32_insn.r_format.rd = insn.mm_fp4_format.rt;
309102cedc3SLeonid Yegoshin 				mips32_insn.r_format.re = 0;
310102cedc3SLeonid Yegoshin 				mips32_insn.r_format.func = movc_op;
311102cedc3SLeonid Yegoshin 				break;
312102cedc3SLeonid Yegoshin 			case mm_fcvtd0_op:
313102cedc3SLeonid Yegoshin 			case mm_fcvtd1_op:
314102cedc3SLeonid Yegoshin 			case mm_fcvts0_op:
315102cedc3SLeonid Yegoshin 			case mm_fcvts1_op:
316102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
317102cedc3SLeonid Yegoshin 				    mm_fcvtd0_op) {
318102cedc3SLeonid Yegoshin 					func = fcvtd_op;
319102cedc3SLeonid Yegoshin 					fmt = swl_format[insn.mm_fp3_format.fmt];
320102cedc3SLeonid Yegoshin 				} else {
321102cedc3SLeonid Yegoshin 					func = fcvts_op;
322102cedc3SLeonid Yegoshin 					fmt = dwl_format[insn.mm_fp3_format.fmt];
323102cedc3SLeonid Yegoshin 				}
324102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
325102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt = fmt;
326102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
327102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
328102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
329102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
330102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
331102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
332102cedc3SLeonid Yegoshin 				break;
333102cedc3SLeonid Yegoshin 			case mm_fmov0_op:
334102cedc3SLeonid Yegoshin 			case mm_fmov1_op:
335102cedc3SLeonid Yegoshin 			case mm_fabs0_op:
336102cedc3SLeonid Yegoshin 			case mm_fabs1_op:
337102cedc3SLeonid Yegoshin 			case mm_fneg0_op:
338102cedc3SLeonid Yegoshin 			case mm_fneg1_op:
339102cedc3SLeonid Yegoshin 				if ((insn.mm_fp1_format.op & 0x7f) ==
340102cedc3SLeonid Yegoshin 				    mm_fmov0_op)
341102cedc3SLeonid Yegoshin 					func = fmov_op;
342102cedc3SLeonid Yegoshin 				else if ((insn.mm_fp1_format.op & 0x7f) ==
343102cedc3SLeonid Yegoshin 					 mm_fabs0_op)
344102cedc3SLeonid Yegoshin 					func = fabs_op;
345102cedc3SLeonid Yegoshin 				else
346102cedc3SLeonid Yegoshin 					func = fneg_op;
347102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
348102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
349102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp3_format.fmt];
350102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
351102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
352102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.fs;
353102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
354102cedc3SLeonid Yegoshin 					insn.mm_fp3_format.rt;
355102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
356102cedc3SLeonid Yegoshin 				break;
357102cedc3SLeonid Yegoshin 			case mm_ffloorl_op:
358102cedc3SLeonid Yegoshin 			case mm_ffloorw_op:
359102cedc3SLeonid Yegoshin 			case mm_fceill_op:
360102cedc3SLeonid Yegoshin 			case mm_fceilw_op:
361102cedc3SLeonid Yegoshin 			case mm_ftruncl_op:
362102cedc3SLeonid Yegoshin 			case mm_ftruncw_op:
363102cedc3SLeonid Yegoshin 			case mm_froundl_op:
364102cedc3SLeonid Yegoshin 			case mm_froundw_op:
365102cedc3SLeonid Yegoshin 			case mm_fcvtl_op:
366102cedc3SLeonid Yegoshin 			case mm_fcvtw_op:
367102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_ffloorl_op)
368102cedc3SLeonid Yegoshin 					func = ffloorl_op;
369102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ffloorw_op)
370102cedc3SLeonid Yegoshin 					func = ffloor_op;
371102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceill_op)
372102cedc3SLeonid Yegoshin 					func = fceill_op;
373102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fceilw_op)
374102cedc3SLeonid Yegoshin 					func = fceil_op;
375102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncl_op)
376102cedc3SLeonid Yegoshin 					func = ftruncl_op;
377102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_ftruncw_op)
378102cedc3SLeonid Yegoshin 					func = ftrunc_op;
379102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundl_op)
380102cedc3SLeonid Yegoshin 					func = froundl_op;
381102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_froundw_op)
382102cedc3SLeonid Yegoshin 					func = fround_op;
383102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fcvtl_op)
384102cedc3SLeonid Yegoshin 					func = fcvtl_op;
385102cedc3SLeonid Yegoshin 				else
386102cedc3SLeonid Yegoshin 					func = fcvtw_op;
387102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
388102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
389102cedc3SLeonid Yegoshin 					sd_format[insn.mm_fp1_format.fmt];
390102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
391102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
392102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
393102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
394102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
395102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
396102cedc3SLeonid Yegoshin 				break;
397102cedc3SLeonid Yegoshin 			case mm_frsqrt_op:
398102cedc3SLeonid Yegoshin 			case mm_fsqrt_op:
399102cedc3SLeonid Yegoshin 			case mm_frecip_op:
400102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_frsqrt_op)
401102cedc3SLeonid Yegoshin 					func = frsqrt_op;
402102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_fsqrt_op)
403102cedc3SLeonid Yegoshin 					func = fsqrt_op;
404102cedc3SLeonid Yegoshin 				else
405102cedc3SLeonid Yegoshin 					func = frecip_op;
406102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.opcode = cop1_op;
407102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fmt =
408102cedc3SLeonid Yegoshin 					sdps_format[insn.mm_fp1_format.fmt];
409102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.ft = 0;
410102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fs =
411102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
412102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.fd =
413102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
414102cedc3SLeonid Yegoshin 				mips32_insn.fp0_format.func = func;
415102cedc3SLeonid Yegoshin 				break;
416102cedc3SLeonid Yegoshin 			case mm_mfc1_op:
417102cedc3SLeonid Yegoshin 			case mm_mtc1_op:
418102cedc3SLeonid Yegoshin 			case mm_cfc1_op:
419102cedc3SLeonid Yegoshin 			case mm_ctc1_op:
4209355e59cSSteven J. Hill 			case mm_mfhc1_op:
4219355e59cSSteven J. Hill 			case mm_mthc1_op:
422102cedc3SLeonid Yegoshin 				if (insn.mm_fp1_format.op == mm_mfc1_op)
423102cedc3SLeonid Yegoshin 					op = mfc_op;
424102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_mtc1_op)
425102cedc3SLeonid Yegoshin 					op = mtc_op;
426102cedc3SLeonid Yegoshin 				else if (insn.mm_fp1_format.op == mm_cfc1_op)
427102cedc3SLeonid Yegoshin 					op = cfc_op;
4289355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_ctc1_op)
429102cedc3SLeonid Yegoshin 					op = ctc_op;
4309355e59cSSteven J. Hill 				else if (insn.mm_fp1_format.op == mm_mfhc1_op)
4319355e59cSSteven J. Hill 					op = mfhc_op;
4329355e59cSSteven J. Hill 				else
4339355e59cSSteven J. Hill 					op = mthc_op;
434102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.opcode = cop1_op;
435102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.op = op;
436102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.rt =
437102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.rt;
438102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fs =
439102cedc3SLeonid Yegoshin 					insn.mm_fp1_format.fs;
440102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.fd = 0;
441102cedc3SLeonid Yegoshin 				mips32_insn.fp1_format.func = 0;
442102cedc3SLeonid Yegoshin 				break;
443102cedc3SLeonid Yegoshin 			default:
444102cedc3SLeonid Yegoshin 				return SIGILL;
445102cedc3SLeonid Yegoshin 			}
446102cedc3SLeonid Yegoshin 			break;
447102cedc3SLeonid Yegoshin 		case mm_32f_74_op:	/* c.cond.fmt */
448102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.opcode = cop1_op;
449102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fmt =
450102cedc3SLeonid Yegoshin 				sdps_format[insn.mm_fp4_format.fmt];
451102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.ft = insn.mm_fp4_format.rt;
452102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fs = insn.mm_fp4_format.fs;
453102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.fd = insn.mm_fp4_format.cc << 2;
454102cedc3SLeonid Yegoshin 			mips32_insn.fp0_format.func =
455102cedc3SLeonid Yegoshin 				insn.mm_fp4_format.cond | MM_MIPS32_COND_FC;
456102cedc3SLeonid Yegoshin 			break;
457102cedc3SLeonid Yegoshin 		default:
458102cedc3SLeonid Yegoshin 			return SIGILL;
459102cedc3SLeonid Yegoshin 		}
460102cedc3SLeonid Yegoshin 		break;
461102cedc3SLeonid Yegoshin 	default:
462102cedc3SLeonid Yegoshin 		return SIGILL;
463102cedc3SLeonid Yegoshin 	}
464102cedc3SLeonid Yegoshin 
465102cedc3SLeonid Yegoshin 	*insn_ptr = mips32_insn;
466102cedc3SLeonid Yegoshin 	return 0;
467102cedc3SLeonid Yegoshin }
468102cedc3SLeonid Yegoshin 
469102cedc3SLeonid Yegoshin int mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
470102cedc3SLeonid Yegoshin 		     unsigned long *contpc)
471102cedc3SLeonid Yegoshin {
472102cedc3SLeonid Yegoshin 	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
473102cedc3SLeonid Yegoshin 	int bc_false = 0;
474102cedc3SLeonid Yegoshin 	unsigned int fcr31;
475102cedc3SLeonid Yegoshin 	unsigned int bit;
476102cedc3SLeonid Yegoshin 
477fe6d2909SDavid Daney 	if (!cpu_has_mmips)
478fe6d2909SDavid Daney 		return 0;
479fe6d2909SDavid Daney 
480102cedc3SLeonid Yegoshin 	switch (insn.mm_i_format.opcode) {
481102cedc3SLeonid Yegoshin 	case mm_pool32a_op:
482102cedc3SLeonid Yegoshin 		if ((insn.mm_i_format.simmediate & MM_POOL32A_MINOR_MASK) ==
483102cedc3SLeonid Yegoshin 		    mm_pool32axf_op) {
484102cedc3SLeonid Yegoshin 			switch (insn.mm_i_format.simmediate >>
485102cedc3SLeonid Yegoshin 				MM_POOL32A_MINOR_SHIFT) {
486102cedc3SLeonid Yegoshin 			case mm_jalr_op:
487102cedc3SLeonid Yegoshin 			case mm_jalrhb_op:
488102cedc3SLeonid Yegoshin 			case mm_jalrs_op:
489102cedc3SLeonid Yegoshin 			case mm_jalrshb_op:
490102cedc3SLeonid Yegoshin 				if (insn.mm_i_format.rt != 0)	/* Not mm_jr */
491102cedc3SLeonid Yegoshin 					regs->regs[insn.mm_i_format.rt] =
492102cedc3SLeonid Yegoshin 						regs->cp0_epc +
493102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
494102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
495102cedc3SLeonid Yegoshin 				*contpc = regs->regs[insn.mm_i_format.rs];
496102cedc3SLeonid Yegoshin 				return 1;
497102cedc3SLeonid Yegoshin 			}
498102cedc3SLeonid Yegoshin 		}
499102cedc3SLeonid Yegoshin 		break;
500102cedc3SLeonid Yegoshin 	case mm_pool32i_op:
501102cedc3SLeonid Yegoshin 		switch (insn.mm_i_format.rt) {
502102cedc3SLeonid Yegoshin 		case mm_bltzals_op:
503102cedc3SLeonid Yegoshin 		case mm_bltzal_op:
504102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
505102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
506102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
507102cedc3SLeonid Yegoshin 			/* Fall through */
508102cedc3SLeonid Yegoshin 		case mm_bltz_op:
509102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.mm_i_format.rs] < 0)
510102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
511102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
512102cedc3SLeonid Yegoshin 					(insn.mm_i_format.simmediate << 1);
513102cedc3SLeonid Yegoshin 			else
514102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
515102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
516102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
517102cedc3SLeonid Yegoshin 			return 1;
518102cedc3SLeonid Yegoshin 		case mm_bgezals_op:
519102cedc3SLeonid Yegoshin 		case mm_bgezal_op:
520102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
521102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
522102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
523102cedc3SLeonid Yegoshin 			/* Fall through */
524102cedc3SLeonid Yegoshin 		case mm_bgez_op:
525102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.mm_i_format.rs] >= 0)
526102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
527102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
528102cedc3SLeonid Yegoshin 					(insn.mm_i_format.simmediate << 1);
529102cedc3SLeonid Yegoshin 			else
530102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
531102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
532102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
533102cedc3SLeonid Yegoshin 			return 1;
534102cedc3SLeonid Yegoshin 		case mm_blez_op:
535102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
536102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
537102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
538102cedc3SLeonid Yegoshin 					(insn.mm_i_format.simmediate << 1);
539102cedc3SLeonid Yegoshin 			else
540102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
541102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
542102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
543102cedc3SLeonid Yegoshin 			return 1;
544102cedc3SLeonid Yegoshin 		case mm_bgtz_op:
545102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
546102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
547102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
548102cedc3SLeonid Yegoshin 					(insn.mm_i_format.simmediate << 1);
549102cedc3SLeonid Yegoshin 			else
550102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
551102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
552102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
553102cedc3SLeonid Yegoshin 			return 1;
554102cedc3SLeonid Yegoshin 		case mm_bc2f_op:
555102cedc3SLeonid Yegoshin 		case mm_bc1f_op:
556102cedc3SLeonid Yegoshin 			bc_false = 1;
557102cedc3SLeonid Yegoshin 			/* Fall through */
558102cedc3SLeonid Yegoshin 		case mm_bc2t_op:
559102cedc3SLeonid Yegoshin 		case mm_bc1t_op:
560102cedc3SLeonid Yegoshin 			preempt_disable();
561102cedc3SLeonid Yegoshin 			if (is_fpu_owner())
562102cedc3SLeonid Yegoshin 				asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
563102cedc3SLeonid Yegoshin 			else
564102cedc3SLeonid Yegoshin 				fcr31 = current->thread.fpu.fcr31;
565102cedc3SLeonid Yegoshin 			preempt_enable();
566102cedc3SLeonid Yegoshin 
567102cedc3SLeonid Yegoshin 			if (bc_false)
568102cedc3SLeonid Yegoshin 				fcr31 = ~fcr31;
569102cedc3SLeonid Yegoshin 
570102cedc3SLeonid Yegoshin 			bit = (insn.mm_i_format.rs >> 2);
571102cedc3SLeonid Yegoshin 			bit += (bit != 0);
572102cedc3SLeonid Yegoshin 			bit += 23;
573102cedc3SLeonid Yegoshin 			if (fcr31 & (1 << bit))
574102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
575102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
576102cedc3SLeonid Yegoshin 					(insn.mm_i_format.simmediate << 1);
577102cedc3SLeonid Yegoshin 			else
578102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
579102cedc3SLeonid Yegoshin 					dec_insn.pc_inc + dec_insn.next_pc_inc;
580102cedc3SLeonid Yegoshin 			return 1;
581102cedc3SLeonid Yegoshin 		}
582102cedc3SLeonid Yegoshin 		break;
583102cedc3SLeonid Yegoshin 	case mm_pool16c_op:
584102cedc3SLeonid Yegoshin 		switch (insn.mm_i_format.rt) {
585102cedc3SLeonid Yegoshin 		case mm_jalr16_op:
586102cedc3SLeonid Yegoshin 		case mm_jalrs16_op:
587102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
588102cedc3SLeonid Yegoshin 				dec_insn.pc_inc + dec_insn.next_pc_inc;
589102cedc3SLeonid Yegoshin 			/* Fall through */
590102cedc3SLeonid Yegoshin 		case mm_jr16_op:
591102cedc3SLeonid Yegoshin 			*contpc = regs->regs[insn.mm_i_format.rs];
592102cedc3SLeonid Yegoshin 			return 1;
593102cedc3SLeonid Yegoshin 		}
594102cedc3SLeonid Yegoshin 		break;
595102cedc3SLeonid Yegoshin 	case mm_beqz16_op:
596102cedc3SLeonid Yegoshin 		if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] == 0)
597102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
598102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
599102cedc3SLeonid Yegoshin 				(insn.mm_b1_format.simmediate << 1);
600102cedc3SLeonid Yegoshin 		else
601102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
602102cedc3SLeonid Yegoshin 				dec_insn.pc_inc + dec_insn.next_pc_inc;
603102cedc3SLeonid Yegoshin 		return 1;
604102cedc3SLeonid Yegoshin 	case mm_bnez16_op:
605102cedc3SLeonid Yegoshin 		if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] != 0)
606102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
607102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
608102cedc3SLeonid Yegoshin 				(insn.mm_b1_format.simmediate << 1);
609102cedc3SLeonid Yegoshin 		else
610102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
611102cedc3SLeonid Yegoshin 				dec_insn.pc_inc + dec_insn.next_pc_inc;
612102cedc3SLeonid Yegoshin 		return 1;
613102cedc3SLeonid Yegoshin 	case mm_b16_op:
614102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
615102cedc3SLeonid Yegoshin 			 (insn.mm_b0_format.simmediate << 1);
616102cedc3SLeonid Yegoshin 		return 1;
617102cedc3SLeonid Yegoshin 	case mm_beq32_op:
618102cedc3SLeonid Yegoshin 		if (regs->regs[insn.mm_i_format.rs] ==
619102cedc3SLeonid Yegoshin 		    regs->regs[insn.mm_i_format.rt])
620102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
621102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
622102cedc3SLeonid Yegoshin 				(insn.mm_i_format.simmediate << 1);
623102cedc3SLeonid Yegoshin 		else
624102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
625102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
626102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
627102cedc3SLeonid Yegoshin 		return 1;
628102cedc3SLeonid Yegoshin 	case mm_bne32_op:
629102cedc3SLeonid Yegoshin 		if (regs->regs[insn.mm_i_format.rs] !=
630102cedc3SLeonid Yegoshin 		    regs->regs[insn.mm_i_format.rt])
631102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
632102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
633102cedc3SLeonid Yegoshin 				(insn.mm_i_format.simmediate << 1);
634102cedc3SLeonid Yegoshin 		else
635102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
636102cedc3SLeonid Yegoshin 				dec_insn.pc_inc + dec_insn.next_pc_inc;
637102cedc3SLeonid Yegoshin 		return 1;
638102cedc3SLeonid Yegoshin 	case mm_jalx32_op:
639102cedc3SLeonid Yegoshin 		regs->regs[31] = regs->cp0_epc +
640102cedc3SLeonid Yegoshin 			dec_insn.pc_inc + dec_insn.next_pc_inc;
641102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
642102cedc3SLeonid Yegoshin 		*contpc >>= 28;
643102cedc3SLeonid Yegoshin 		*contpc <<= 28;
644102cedc3SLeonid Yegoshin 		*contpc |= (insn.j_format.target << 2);
645102cedc3SLeonid Yegoshin 		return 1;
646102cedc3SLeonid Yegoshin 	case mm_jals32_op:
647102cedc3SLeonid Yegoshin 	case mm_jal32_op:
648102cedc3SLeonid Yegoshin 		regs->regs[31] = regs->cp0_epc +
649102cedc3SLeonid Yegoshin 			dec_insn.pc_inc + dec_insn.next_pc_inc;
650102cedc3SLeonid Yegoshin 		/* Fall through */
651102cedc3SLeonid Yegoshin 	case mm_j32_op:
652102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
653102cedc3SLeonid Yegoshin 		*contpc >>= 27;
654102cedc3SLeonid Yegoshin 		*contpc <<= 27;
655102cedc3SLeonid Yegoshin 		*contpc |= (insn.j_format.target << 1);
656102cedc3SLeonid Yegoshin 		set_isa16_mode(*contpc);
657102cedc3SLeonid Yegoshin 		return 1;
658102cedc3SLeonid Yegoshin 	}
659102cedc3SLeonid Yegoshin 	return 0;
660102cedc3SLeonid Yegoshin }
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds /*
6631da177e4SLinus Torvalds  * Redundant with logic already in kernel/branch.c,
6641da177e4SLinus Torvalds  * embedded in compute_return_epc.  At some point,
6651da177e4SLinus Torvalds  * a single subroutine should be used across both
6661da177e4SLinus Torvalds  * modules.
6671da177e4SLinus Torvalds  */
668102cedc3SLeonid Yegoshin static int isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
669102cedc3SLeonid Yegoshin 			 unsigned long *contpc)
6701da177e4SLinus Torvalds {
671102cedc3SLeonid Yegoshin 	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
672102cedc3SLeonid Yegoshin 	unsigned int fcr31;
673102cedc3SLeonid Yegoshin 	unsigned int bit = 0;
674102cedc3SLeonid Yegoshin 
675102cedc3SLeonid Yegoshin 	switch (insn.i_format.opcode) {
6761da177e4SLinus Torvalds 	case spec_op:
677102cedc3SLeonid Yegoshin 		switch (insn.r_format.func) {
6781da177e4SLinus Torvalds 		case jalr_op:
679102cedc3SLeonid Yegoshin 			regs->regs[insn.r_format.rd] =
680102cedc3SLeonid Yegoshin 				regs->cp0_epc + dec_insn.pc_inc +
681102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
682102cedc3SLeonid Yegoshin 			/* Fall through */
6831da177e4SLinus Torvalds 		case jr_op:
684102cedc3SLeonid Yegoshin 			*contpc = regs->regs[insn.r_format.rs];
6851da177e4SLinus Torvalds 			return 1;
6861da177e4SLinus Torvalds 		}
6871da177e4SLinus Torvalds 		break;
6881da177e4SLinus Torvalds 	case bcond_op:
689102cedc3SLeonid Yegoshin 		switch (insn.i_format.rt) {
6901da177e4SLinus Torvalds 		case bltzal_op:
6911da177e4SLinus Torvalds 		case bltzall_op:
692102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
693102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
694102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
695102cedc3SLeonid Yegoshin 			/* Fall through */
696102cedc3SLeonid Yegoshin 		case bltz_op:
697102cedc3SLeonid Yegoshin 		case bltzl_op:
698102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] < 0)
699102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
700102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
701102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
702102cedc3SLeonid Yegoshin 			else
703102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
704102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
705102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
7061da177e4SLinus Torvalds 			return 1;
707102cedc3SLeonid Yegoshin 		case bgezal_op:
708102cedc3SLeonid Yegoshin 		case bgezall_op:
709102cedc3SLeonid Yegoshin 			regs->regs[31] = regs->cp0_epc +
710102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
711102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
712102cedc3SLeonid Yegoshin 			/* Fall through */
713102cedc3SLeonid Yegoshin 		case bgez_op:
714102cedc3SLeonid Yegoshin 		case bgezl_op:
715102cedc3SLeonid Yegoshin 			if ((long)regs->regs[insn.i_format.rs] >= 0)
716102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
717102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
718102cedc3SLeonid Yegoshin 					(insn.i_format.simmediate << 2);
719102cedc3SLeonid Yegoshin 			else
720102cedc3SLeonid Yegoshin 				*contpc = regs->cp0_epc +
721102cedc3SLeonid Yegoshin 					dec_insn.pc_inc +
722102cedc3SLeonid Yegoshin 					dec_insn.next_pc_inc;
723102cedc3SLeonid Yegoshin 			return 1;
7241da177e4SLinus Torvalds 		}
7251da177e4SLinus Torvalds 		break;
7261da177e4SLinus Torvalds 	case jalx_op:
727102cedc3SLeonid Yegoshin 		set_isa16_mode(bit);
728102cedc3SLeonid Yegoshin 	case jal_op:
729102cedc3SLeonid Yegoshin 		regs->regs[31] = regs->cp0_epc +
730102cedc3SLeonid Yegoshin 			dec_insn.pc_inc +
731102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc;
732102cedc3SLeonid Yegoshin 		/* Fall through */
733102cedc3SLeonid Yegoshin 	case j_op:
734102cedc3SLeonid Yegoshin 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
735102cedc3SLeonid Yegoshin 		*contpc >>= 28;
736102cedc3SLeonid Yegoshin 		*contpc <<= 28;
737102cedc3SLeonid Yegoshin 		*contpc |= (insn.j_format.target << 2);
738102cedc3SLeonid Yegoshin 		/* Set microMIPS mode bit: XOR for jalx. */
739102cedc3SLeonid Yegoshin 		*contpc ^= bit;
7401da177e4SLinus Torvalds 		return 1;
741102cedc3SLeonid Yegoshin 	case beq_op:
742102cedc3SLeonid Yegoshin 	case beql_op:
743102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] ==
744102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
745102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
746102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
747102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
748102cedc3SLeonid Yegoshin 		else
749102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
750102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
751102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
752102cedc3SLeonid Yegoshin 		return 1;
753102cedc3SLeonid Yegoshin 	case bne_op:
754102cedc3SLeonid Yegoshin 	case bnel_op:
755102cedc3SLeonid Yegoshin 		if (regs->regs[insn.i_format.rs] !=
756102cedc3SLeonid Yegoshin 		    regs->regs[insn.i_format.rt])
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;
764102cedc3SLeonid Yegoshin 		return 1;
765102cedc3SLeonid Yegoshin 	case blez_op:
766102cedc3SLeonid Yegoshin 	case blezl_op:
767102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] <= 0)
768102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
769102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
770102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
771102cedc3SLeonid Yegoshin 		else
772102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
773102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
774102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
775102cedc3SLeonid Yegoshin 		return 1;
776102cedc3SLeonid Yegoshin 	case bgtz_op:
777102cedc3SLeonid Yegoshin 	case bgtzl_op:
778102cedc3SLeonid Yegoshin 		if ((long)regs->regs[insn.i_format.rs] > 0)
779102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
780102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
781102cedc3SLeonid Yegoshin 				(insn.i_format.simmediate << 2);
782102cedc3SLeonid Yegoshin 		else
783102cedc3SLeonid Yegoshin 			*contpc = regs->cp0_epc +
784102cedc3SLeonid Yegoshin 				dec_insn.pc_inc +
785102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc;
786102cedc3SLeonid Yegoshin 		return 1;
787c26d4219SDavid Daney #ifdef CONFIG_CPU_CAVIUM_OCTEON
788c26d4219SDavid Daney 	case lwc2_op: /* This is bbit0 on Octeon */
789c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt)) == 0)
790c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
791c26d4219SDavid Daney 		else
792c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
793c26d4219SDavid Daney 		return 1;
794c26d4219SDavid Daney 	case ldc2_op: /* This is bbit032 on Octeon */
795c26d4219SDavid Daney 		if ((regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32))) == 0)
796c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
797c26d4219SDavid Daney 		else
798c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
799c26d4219SDavid Daney 		return 1;
800c26d4219SDavid Daney 	case swc2_op: /* This is bbit1 on Octeon */
801c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
802c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
803c26d4219SDavid Daney 		else
804c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
805c26d4219SDavid Daney 		return 1;
806c26d4219SDavid Daney 	case sdc2_op: /* This is bbit132 on Octeon */
807c26d4219SDavid Daney 		if (regs->regs[insn.i_format.rs] & (1ull<<(insn.i_format.rt + 32)))
808c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 4 + (insn.i_format.simmediate << 2);
809c26d4219SDavid Daney 		else
810c26d4219SDavid Daney 			*contpc = regs->cp0_epc + 8;
811c26d4219SDavid Daney 		return 1;
812c26d4219SDavid Daney #endif
8131da177e4SLinus Torvalds 	case cop0_op:
8141da177e4SLinus Torvalds 	case cop1_op:
8151da177e4SLinus Torvalds 	case cop2_op:
8161da177e4SLinus Torvalds 	case cop1x_op:
817102cedc3SLeonid Yegoshin 		if (insn.i_format.rs == bc_op) {
818102cedc3SLeonid Yegoshin 			preempt_disable();
819102cedc3SLeonid Yegoshin 			if (is_fpu_owner())
820102cedc3SLeonid Yegoshin 				asm volatile("cfc1\t%0,$31" : "=r" (fcr31));
821102cedc3SLeonid Yegoshin 			else
822102cedc3SLeonid Yegoshin 				fcr31 = current->thread.fpu.fcr31;
823102cedc3SLeonid Yegoshin 			preempt_enable();
824102cedc3SLeonid Yegoshin 
825102cedc3SLeonid Yegoshin 			bit = (insn.i_format.rt >> 2);
826102cedc3SLeonid Yegoshin 			bit += (bit != 0);
827102cedc3SLeonid Yegoshin 			bit += 23;
828102cedc3SLeonid Yegoshin 			switch (insn.i_format.rt & 3) {
829102cedc3SLeonid Yegoshin 			case 0:	/* bc1f */
830102cedc3SLeonid Yegoshin 			case 2:	/* bc1fl */
831102cedc3SLeonid Yegoshin 				if (~fcr31 & (1 << bit))
832102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
833102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
834102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
835102cedc3SLeonid Yegoshin 				else
836102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
837102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
838102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
839102cedc3SLeonid Yegoshin 				return 1;
840102cedc3SLeonid Yegoshin 			case 1:	/* bc1t */
841102cedc3SLeonid Yegoshin 			case 3:	/* bc1tl */
842102cedc3SLeonid Yegoshin 				if (fcr31 & (1 << bit))
843102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
844102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
845102cedc3SLeonid Yegoshin 						(insn.i_format.simmediate << 2);
846102cedc3SLeonid Yegoshin 				else
847102cedc3SLeonid Yegoshin 					*contpc = regs->cp0_epc +
848102cedc3SLeonid Yegoshin 						dec_insn.pc_inc +
849102cedc3SLeonid Yegoshin 						dec_insn.next_pc_inc;
8501da177e4SLinus Torvalds 				return 1;
8511da177e4SLinus Torvalds 			}
852102cedc3SLeonid Yegoshin 		}
853102cedc3SLeonid Yegoshin 		break;
854102cedc3SLeonid Yegoshin 	}
8551da177e4SLinus Torvalds 	return 0;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds /*
8591da177e4SLinus Torvalds  * In the Linux kernel, we support selection of FPR format on the
860da0bac33SDavid Daney  * basis of the Status.FR bit.	If an FPU is not present, the FR bit
861da0bac33SDavid Daney  * is hardwired to zero, which would imply a 32-bit FPU even for
862597ce172SPaul Burton  * 64-bit CPUs so we rather look at TIF_32BIT_FPREGS.
86351d943f0SRalf Baechle  * FPU emu is slow and bulky and optimizing this function offers fairly
86451d943f0SRalf Baechle  * sizeable benefits so we try to be clever and make this function return
86551d943f0SRalf Baechle  * a constant whenever possible, that is on 64-bit kernels without O32
866597ce172SPaul Burton  * compatibility enabled and on 32-bit without 64-bit FPU support.
8671da177e4SLinus Torvalds  */
868da0bac33SDavid Daney static inline int cop1_64bit(struct pt_regs *xcp)
869da0bac33SDavid Daney {
87051d943f0SRalf Baechle #if defined(CONFIG_64BIT) && !defined(CONFIG_MIPS32_O32)
87151d943f0SRalf Baechle 	return 1;
872597ce172SPaul Burton #elif defined(CONFIG_32BIT) && !defined(CONFIG_MIPS_O32_FP64_SUPPORT)
873da0bac33SDavid Daney 	return 0;
874597ce172SPaul Burton #else
875597ce172SPaul Burton 	return !test_thread_flag(TIF_32BIT_FPREGS);
8761da177e4SLinus Torvalds #endif
877da0bac33SDavid Daney }
8781da177e4SLinus Torvalds 
879*bbd426f5SPaul Burton #define SIFROMREG(si, x) do {						\
880*bbd426f5SPaul Burton 	if (cop1_64bit(xcp))						\
881*bbd426f5SPaul Burton 		(si) = get_fpr32(&ctx->fpr[x], 0);			\
882*bbd426f5SPaul Burton 	else								\
883*bbd426f5SPaul Burton 		(si) = get_fpr32(&ctx->fpr[(x) & ~1], (x) & 1);		\
884*bbd426f5SPaul Burton } while (0)
885da0bac33SDavid Daney 
886*bbd426f5SPaul Burton #define SITOREG(si, x) do {						\
887*bbd426f5SPaul Burton 	if (cop1_64bit(xcp))						\
888*bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[x], 0, si);				\
889*bbd426f5SPaul Burton 	else								\
890*bbd426f5SPaul Burton 		set_fpr32(&ctx->fpr[(x) & ~1], (x) & 1, si);		\
891*bbd426f5SPaul Burton } while (0)
8921da177e4SLinus Torvalds 
893*bbd426f5SPaul Burton #define SIFROMHREG(si, x)	((si) = get_fpr32(&ctx->fpr[x], 1))
894*bbd426f5SPaul Burton #define SITOHREG(si, x)		set_fpr32(&ctx->fpr[x], 1, si)
8951ac94400SLeonid Yegoshin 
896*bbd426f5SPaul Burton #define DIFROMREG(di, x) \
897*bbd426f5SPaul Burton 	((di) = get_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0))
898*bbd426f5SPaul Burton 
899*bbd426f5SPaul Burton #define DITOREG(di, x) \
900*bbd426f5SPaul Burton 	set_fpr64(&ctx->fpr[(x) & ~(cop1_64bit(xcp) == 0)], 0, di)
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds #define SPFROMREG(sp, x) SIFROMREG((sp).bits, x)
9031da177e4SLinus Torvalds #define SPTOREG(sp, x)	SITOREG((sp).bits, x)
9041da177e4SLinus Torvalds #define DPFROMREG(dp, x)	DIFROMREG((dp).bits, x)
9051da177e4SLinus Torvalds #define DPTOREG(dp, x)	DITOREG((dp).bits, x)
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds /*
9081da177e4SLinus Torvalds  * Emulate the single floating point instruction pointed at by EPC.
9091da177e4SLinus Torvalds  * Two instructions if the instruction is in a branch delay slot.
9101da177e4SLinus Torvalds  */
9111da177e4SLinus Torvalds 
912515b029dSDavid Daney static int cop1Emulate(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
913102cedc3SLeonid Yegoshin 		struct mm_decoded_insn dec_insn, void *__user *fault_addr)
9141da177e4SLinus Torvalds {
9151da177e4SLinus Torvalds 	mips_instruction ir;
916102cedc3SLeonid Yegoshin 	unsigned long contpc = xcp->cp0_epc + dec_insn.pc_inc;
9171da177e4SLinus Torvalds 	unsigned int cond;
918102cedc3SLeonid Yegoshin 	int pc_inc;
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	/* XXX NEC Vr54xx bug workaround */
921102cedc3SLeonid Yegoshin 	if (xcp->cp0_cause & CAUSEF_BD) {
922102cedc3SLeonid Yegoshin 		if (dec_insn.micro_mips_mode) {
923102cedc3SLeonid Yegoshin 			if (!mm_isBranchInstr(xcp, dec_insn, &contpc))
9241da177e4SLinus Torvalds 				xcp->cp0_cause &= ~CAUSEF_BD;
925102cedc3SLeonid Yegoshin 		} else {
926102cedc3SLeonid Yegoshin 			if (!isBranchInstr(xcp, dec_insn, &contpc))
927102cedc3SLeonid Yegoshin 				xcp->cp0_cause &= ~CAUSEF_BD;
928102cedc3SLeonid Yegoshin 		}
929102cedc3SLeonid Yegoshin 	}
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	if (xcp->cp0_cause & CAUSEF_BD) {
9321da177e4SLinus Torvalds 		/*
9331da177e4SLinus Torvalds 		 * The instruction to be emulated is in a branch delay slot
9341da177e4SLinus Torvalds 		 * which means that we have to	emulate the branch instruction
9351da177e4SLinus Torvalds 		 * BEFORE we do the cop1 instruction.
9361da177e4SLinus Torvalds 		 *
9371da177e4SLinus Torvalds 		 * This branch could be a COP1 branch, but in that case we
9381da177e4SLinus Torvalds 		 * would have had a trap for that instruction, and would not
9391da177e4SLinus Torvalds 		 * come through this route.
9401da177e4SLinus Torvalds 		 *
9411da177e4SLinus Torvalds 		 * Linux MIPS branch emulator operates on context, updating the
9421da177e4SLinus Torvalds 		 * cp0_epc.
9431da177e4SLinus Torvalds 		 */
944102cedc3SLeonid Yegoshin 		ir = dec_insn.next_insn;  /* process delay slot instr */
945102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.next_pc_inc;
946333d1f67SRalf Baechle 	} else {
947102cedc3SLeonid Yegoshin 		ir = dec_insn.insn;       /* process current instr */
948102cedc3SLeonid Yegoshin 		pc_inc = dec_insn.pc_inc;
949102cedc3SLeonid Yegoshin 	}
950102cedc3SLeonid Yegoshin 
951102cedc3SLeonid Yegoshin 	/*
952102cedc3SLeonid Yegoshin 	 * Since microMIPS FPU instructios are a subset of MIPS32 FPU
953102cedc3SLeonid Yegoshin 	 * instructions, we want to convert microMIPS FPU instructions
954102cedc3SLeonid Yegoshin 	 * into MIPS32 instructions so that we could reuse all of the
955102cedc3SLeonid Yegoshin 	 * FPU emulation code.
956102cedc3SLeonid Yegoshin 	 *
957102cedc3SLeonid Yegoshin 	 * NOTE: We cannot do this for branch instructions since they
958102cedc3SLeonid Yegoshin 	 *       are not a subset. Example: Cannot emulate a 16-bit
959102cedc3SLeonid Yegoshin 	 *       aligned target address with a MIPS32 instruction.
960102cedc3SLeonid Yegoshin 	 */
961102cedc3SLeonid Yegoshin 	if (dec_insn.micro_mips_mode) {
962102cedc3SLeonid Yegoshin 		/*
963102cedc3SLeonid Yegoshin 		 * If next instruction is a 16-bit instruction, then it
964102cedc3SLeonid Yegoshin 		 * it cannot be a FPU instruction. This could happen
965102cedc3SLeonid Yegoshin 		 * since we can be called for non-FPU instructions.
966102cedc3SLeonid Yegoshin 		 */
967102cedc3SLeonid Yegoshin 		if ((pc_inc == 2) ||
968102cedc3SLeonid Yegoshin 			(microMIPS32_to_MIPS32((union mips_instruction *)&ir)
969102cedc3SLeonid Yegoshin 			 == SIGILL))
970102cedc3SLeonid Yegoshin 			return SIGILL;
9711da177e4SLinus Torvalds 	}
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds       emul:
974a8b0ca17SPeter Zijlstra 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, xcp, 0);
975b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(emulated);
9761da177e4SLinus Torvalds 	switch (MIPSInst_OPCODE(ir)) {
9771da177e4SLinus Torvalds 	case ldc1_op:{
9783fccc015SRalf Baechle 		u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
9791da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
9801da177e4SLinus Torvalds 		u64 val;
9811da177e4SLinus Torvalds 
982b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
983515b029dSDavid Daney 
984515b029dSDavid Daney 		if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
985b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
986515b029dSDavid Daney 			*fault_addr = va;
9871da177e4SLinus Torvalds 			return SIGBUS;
9881da177e4SLinus Torvalds 		}
989515b029dSDavid Daney 		if (__get_user(val, va)) {
990515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
991515b029dSDavid Daney 			*fault_addr = va;
992515b029dSDavid Daney 			return SIGSEGV;
993515b029dSDavid Daney 		}
9941da177e4SLinus Torvalds 		DITOREG(val, MIPSInst_RT(ir));
9951da177e4SLinus Torvalds 		break;
9961da177e4SLinus Torvalds 	}
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	case sdc1_op:{
9993fccc015SRalf Baechle 		u64 __user *va = (u64 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10001da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
10011da177e4SLinus Torvalds 		u64 val;
10021da177e4SLinus Torvalds 
1003b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
10041da177e4SLinus Torvalds 		DIFROMREG(val, MIPSInst_RT(ir));
1005515b029dSDavid Daney 		if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1006b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
1007515b029dSDavid Daney 			*fault_addr = va;
10081da177e4SLinus Torvalds 			return SIGBUS;
10091da177e4SLinus Torvalds 		}
1010515b029dSDavid Daney 		if (__put_user(val, va)) {
1011515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
1012515b029dSDavid Daney 			*fault_addr = va;
1013515b029dSDavid Daney 			return SIGSEGV;
1014515b029dSDavid Daney 		}
10151da177e4SLinus Torvalds 		break;
10161da177e4SLinus Torvalds 	}
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	case lwc1_op:{
10193fccc015SRalf Baechle 		u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10201da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
10211da177e4SLinus Torvalds 		u32 val;
10221da177e4SLinus Torvalds 
1023b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(loads);
1024515b029dSDavid Daney 		if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1025b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
1026515b029dSDavid Daney 			*fault_addr = va;
10271da177e4SLinus Torvalds 			return SIGBUS;
10281da177e4SLinus Torvalds 		}
1029515b029dSDavid Daney 		if (__get_user(val, va)) {
1030515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
1031515b029dSDavid Daney 			*fault_addr = va;
1032515b029dSDavid Daney 			return SIGSEGV;
1033515b029dSDavid Daney 		}
10341da177e4SLinus Torvalds 		SITOREG(val, MIPSInst_RT(ir));
10351da177e4SLinus Torvalds 		break;
10361da177e4SLinus Torvalds 	}
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	case swc1_op:{
10393fccc015SRalf Baechle 		u32 __user *va = (u32 __user *) (xcp->regs[MIPSInst_RS(ir)] +
10401da177e4SLinus Torvalds 			MIPSInst_SIMM(ir));
10411da177e4SLinus Torvalds 		u32 val;
10421da177e4SLinus Torvalds 
1043b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(stores);
10441da177e4SLinus Torvalds 		SIFROMREG(val, MIPSInst_RT(ir));
1045515b029dSDavid Daney 		if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1046b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
1047515b029dSDavid Daney 			*fault_addr = va;
10481da177e4SLinus Torvalds 			return SIGBUS;
10491da177e4SLinus Torvalds 		}
1050515b029dSDavid Daney 		if (__put_user(val, va)) {
1051515b029dSDavid Daney 			MIPS_FPU_EMU_INC_STATS(errors);
1052515b029dSDavid Daney 			*fault_addr = va;
1053515b029dSDavid Daney 			return SIGSEGV;
1054515b029dSDavid Daney 		}
10551da177e4SLinus Torvalds 		break;
10561da177e4SLinus Torvalds 	}
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	case cop1_op:
10591da177e4SLinus Torvalds 		switch (MIPSInst_RS(ir)) {
10601da177e4SLinus Torvalds 
10614b724efdSRalf Baechle #if defined(__mips64)
10621da177e4SLinus Torvalds 		case dmfc_op:
10631da177e4SLinus Torvalds 			/* copregister fs -> gpr[rt] */
10641da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
10651da177e4SLinus Torvalds 				DIFROMREG(xcp->regs[MIPSInst_RT(ir)],
10661da177e4SLinus Torvalds 					MIPSInst_RD(ir));
10671da177e4SLinus Torvalds 			}
10681da177e4SLinus Torvalds 			break;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 		case dmtc_op:
10711da177e4SLinus Torvalds 			/* copregister fs <- rt */
10721da177e4SLinus Torvalds 			DITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
10731da177e4SLinus Torvalds 			break;
10741da177e4SLinus Torvalds #endif
10751da177e4SLinus Torvalds 
10761ac94400SLeonid Yegoshin 		case mfhc_op:
10771ac94400SLeonid Yegoshin 			if (!cpu_has_mips_r2)
10781ac94400SLeonid Yegoshin 				goto sigill;
10791ac94400SLeonid Yegoshin 
10801ac94400SLeonid Yegoshin 			/* copregister rd -> gpr[rt] */
10811ac94400SLeonid Yegoshin 			if (MIPSInst_RT(ir) != 0) {
10821ac94400SLeonid Yegoshin 				SIFROMHREG(xcp->regs[MIPSInst_RT(ir)],
10831ac94400SLeonid Yegoshin 					MIPSInst_RD(ir));
10841ac94400SLeonid Yegoshin 			}
10851ac94400SLeonid Yegoshin 			break;
10861ac94400SLeonid Yegoshin 
10871ac94400SLeonid Yegoshin 		case mthc_op:
10881ac94400SLeonid Yegoshin 			if (!cpu_has_mips_r2)
10891ac94400SLeonid Yegoshin 				goto sigill;
10901ac94400SLeonid Yegoshin 
10911ac94400SLeonid Yegoshin 			/* copregister rd <- gpr[rt] */
10921ac94400SLeonid Yegoshin 			SITOHREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
10931ac94400SLeonid Yegoshin 			break;
10941ac94400SLeonid Yegoshin 
10951da177e4SLinus Torvalds 		case mfc_op:
10961da177e4SLinus Torvalds 			/* copregister rd -> gpr[rt] */
10971da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) != 0) {
10981da177e4SLinus Torvalds 				SIFROMREG(xcp->regs[MIPSInst_RT(ir)],
10991da177e4SLinus Torvalds 					MIPSInst_RD(ir));
11001da177e4SLinus Torvalds 			}
11011da177e4SLinus Torvalds 			break;
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 		case mtc_op:
11041da177e4SLinus Torvalds 			/* copregister rd <- rt */
11051da177e4SLinus Torvalds 			SITOREG(xcp->regs[MIPSInst_RT(ir)], MIPSInst_RD(ir));
11061da177e4SLinus Torvalds 			break;
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 		case cfc_op:{
11091da177e4SLinus Torvalds 			/* cop control register rd -> gpr[rt] */
11101da177e4SLinus Torvalds 			u32 value;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) == FPCREG_CSR) {
11131da177e4SLinus Torvalds 				value = ctx->fcr31;
11143f135530SShane McDonald 				value = (value & ~FPU_CSR_RM) |
11153f135530SShane McDonald 					mips_rm[modeindex(value)];
11161da177e4SLinus Torvalds #ifdef CSRTRACE
11171da177e4SLinus Torvalds 				printk("%p gpr[%d]<-csr=%08x\n",
1118333d1f67SRalf Baechle 					(void *) (xcp->cp0_epc),
11191da177e4SLinus Torvalds 					MIPSInst_RT(ir), value);
11201da177e4SLinus Torvalds #endif
11211da177e4SLinus Torvalds 			}
11221da177e4SLinus Torvalds 			else if (MIPSInst_RD(ir) == FPCREG_RID)
11231da177e4SLinus Torvalds 				value = 0;
11241da177e4SLinus Torvalds 			else
11251da177e4SLinus Torvalds 				value = 0;
11261da177e4SLinus Torvalds 			if (MIPSInst_RT(ir))
11271da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RT(ir)] = value;
11281da177e4SLinus Torvalds 			break;
11291da177e4SLinus Torvalds 		}
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 		case ctc_op:{
11321da177e4SLinus Torvalds 			/* copregister rd <- rt */
11331da177e4SLinus Torvalds 			u32 value;
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 			if (MIPSInst_RT(ir) == 0)
11361da177e4SLinus Torvalds 				value = 0;
11371da177e4SLinus Torvalds 			else
11381da177e4SLinus Torvalds 				value = xcp->regs[MIPSInst_RT(ir)];
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 			/* we only have one writable control reg
11411da177e4SLinus Torvalds 			 */
11421da177e4SLinus Torvalds 			if (MIPSInst_RD(ir) == FPCREG_CSR) {
11431da177e4SLinus Torvalds #ifdef CSRTRACE
11441da177e4SLinus Torvalds 				printk("%p gpr[%d]->csr=%08x\n",
1145333d1f67SRalf Baechle 					(void *) (xcp->cp0_epc),
11461da177e4SLinus Torvalds 					MIPSInst_RT(ir), value);
11471da177e4SLinus Torvalds #endif
114895e8f634SShane McDonald 
114995e8f634SShane McDonald 				/*
115095e8f634SShane McDonald 				 * Don't write reserved bits,
115195e8f634SShane McDonald 				 * and convert to ieee library modes
115295e8f634SShane McDonald 				 */
115395e8f634SShane McDonald 				ctx->fcr31 = (value &
115495e8f634SShane McDonald 						~(FPU_CSR_RSVD | FPU_CSR_RM)) |
115595e8f634SShane McDonald 						ieee_rm[modeindex(value)];
11561da177e4SLinus Torvalds 			}
11571da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
11581da177e4SLinus Torvalds 				return SIGFPE;
11591da177e4SLinus Torvalds 			}
11601da177e4SLinus Torvalds 			break;
11611da177e4SLinus Torvalds 		}
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 		case bc_op:{
11641da177e4SLinus Torvalds 			int likely = 0;
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 			if (xcp->cp0_cause & CAUSEF_BD)
11671da177e4SLinus Torvalds 				return SIGILL;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds #if __mips >= 4
11701da177e4SLinus Torvalds 			cond = ctx->fcr31 & fpucondbit[MIPSInst_RT(ir) >> 2];
11711da177e4SLinus Torvalds #else
11721da177e4SLinus Torvalds 			cond = ctx->fcr31 & FPU_CSR_COND;
11731da177e4SLinus Torvalds #endif
11741da177e4SLinus Torvalds 			switch (MIPSInst_RT(ir) & 3) {
11751da177e4SLinus Torvalds 			case bcfl_op:
11761da177e4SLinus Torvalds 				likely = 1;
11771da177e4SLinus Torvalds 			case bcf_op:
11781da177e4SLinus Torvalds 				cond = !cond;
11791da177e4SLinus Torvalds 				break;
11801da177e4SLinus Torvalds 			case bctl_op:
11811da177e4SLinus Torvalds 				likely = 1;
11821da177e4SLinus Torvalds 			case bct_op:
11831da177e4SLinus Torvalds 				break;
11841da177e4SLinus Torvalds 			default:
11851da177e4SLinus Torvalds 				/* thats an illegal instruction */
11861da177e4SLinus Torvalds 				return SIGILL;
11871da177e4SLinus Torvalds 			}
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 			xcp->cp0_cause |= CAUSEF_BD;
11901da177e4SLinus Torvalds 			if (cond) {
11911da177e4SLinus Torvalds 				/* branch taken: emulate dslot
11921da177e4SLinus Torvalds 				 * instruction
11931da177e4SLinus Torvalds 				 */
1194102cedc3SLeonid Yegoshin 				xcp->cp0_epc += dec_insn.pc_inc;
11951da177e4SLinus Torvalds 
1196102cedc3SLeonid Yegoshin 				contpc = MIPSInst_SIMM(ir);
1197102cedc3SLeonid Yegoshin 				ir = dec_insn.next_insn;
1198102cedc3SLeonid Yegoshin 				if (dec_insn.micro_mips_mode) {
1199102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 1));
1200102cedc3SLeonid Yegoshin 
1201102cedc3SLeonid Yegoshin 					/* If 16-bit instruction, not FPU. */
1202102cedc3SLeonid Yegoshin 					if ((dec_insn.next_pc_inc == 2) ||
1203102cedc3SLeonid Yegoshin 						(microMIPS32_to_MIPS32((union mips_instruction *)&ir) == SIGILL)) {
1204102cedc3SLeonid Yegoshin 
1205102cedc3SLeonid Yegoshin 						/*
1206102cedc3SLeonid Yegoshin 						 * Since this instruction will
1207102cedc3SLeonid Yegoshin 						 * be put on the stack with
1208102cedc3SLeonid Yegoshin 						 * 32-bit words, get around
1209102cedc3SLeonid Yegoshin 						 * this problem by putting a
1210102cedc3SLeonid Yegoshin 						 * NOP16 as the second one.
1211102cedc3SLeonid Yegoshin 						 */
1212102cedc3SLeonid Yegoshin 						if (dec_insn.next_pc_inc == 2)
1213102cedc3SLeonid Yegoshin 							ir = (ir & (~0xffff)) | MM_NOP16;
1214102cedc3SLeonid Yegoshin 
1215102cedc3SLeonid Yegoshin 						/*
1216102cedc3SLeonid Yegoshin 						 * Single step the non-CP1
1217102cedc3SLeonid Yegoshin 						 * instruction in the dslot.
1218102cedc3SLeonid Yegoshin 						 */
1219102cedc3SLeonid Yegoshin 						return mips_dsemul(xcp, ir, contpc);
1220515b029dSDavid Daney 					}
1221102cedc3SLeonid Yegoshin 				} else
1222102cedc3SLeonid Yegoshin 					contpc = (xcp->cp0_epc + (contpc << 2));
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 				switch (MIPSInst_OPCODE(ir)) {
12251da177e4SLinus Torvalds 				case lwc1_op:
12261da177e4SLinus Torvalds 				case swc1_op:
12274b724efdSRalf Baechle #if (__mips >= 2 || defined(__mips64))
12281da177e4SLinus Torvalds 				case ldc1_op:
12291da177e4SLinus Torvalds 				case sdc1_op:
12301da177e4SLinus Torvalds #endif
12311da177e4SLinus Torvalds 				case cop1_op:
12321da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
12331da177e4SLinus Torvalds 				case cop1x_op:
12341da177e4SLinus Torvalds #endif
12351da177e4SLinus Torvalds 					/* its one of ours */
12361da177e4SLinus Torvalds 					goto emul;
12371da177e4SLinus Torvalds #if __mips >= 4
12381da177e4SLinus Torvalds 				case spec_op:
12391da177e4SLinus Torvalds 					if (MIPSInst_FUNC(ir) == movc_op)
12401da177e4SLinus Torvalds 						goto emul;
12411da177e4SLinus Torvalds 					break;
12421da177e4SLinus Torvalds #endif
12431da177e4SLinus Torvalds 				}
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 				/*
12461da177e4SLinus Torvalds 				 * Single step the non-cp1
12471da177e4SLinus Torvalds 				 * instruction in the dslot
12481da177e4SLinus Torvalds 				 */
1249e70dfc10SAtsushi Nemoto 				return mips_dsemul(xcp, ir, contpc);
12501da177e4SLinus Torvalds 			}
12511da177e4SLinus Torvalds 			else {
12521da177e4SLinus Torvalds 				/* branch not taken */
12531da177e4SLinus Torvalds 				if (likely) {
12541da177e4SLinus Torvalds 					/*
12551da177e4SLinus Torvalds 					 * branch likely nullifies
12561da177e4SLinus Torvalds 					 * dslot if not taken
12571da177e4SLinus Torvalds 					 */
1258102cedc3SLeonid Yegoshin 					xcp->cp0_epc += dec_insn.pc_inc;
1259102cedc3SLeonid Yegoshin 					contpc += dec_insn.pc_inc;
12601da177e4SLinus Torvalds 					/*
12611da177e4SLinus Torvalds 					 * else continue & execute
12621da177e4SLinus Torvalds 					 * dslot as normal insn
12631da177e4SLinus Torvalds 					 */
12641da177e4SLinus Torvalds 				}
12651da177e4SLinus Torvalds 			}
12661da177e4SLinus Torvalds 			break;
12671da177e4SLinus Torvalds 		}
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds 		default:
12701da177e4SLinus Torvalds 			if (!(MIPSInst_RS(ir) & 0x10))
12711da177e4SLinus Torvalds 				return SIGILL;
12721da177e4SLinus Torvalds 			{
12731da177e4SLinus Torvalds 				int sig;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 				/* a real fpu computation instruction */
12761da177e4SLinus Torvalds 				if ((sig = fpu_emu(xcp, ctx, ir)))
12771da177e4SLinus Torvalds 					return sig;
12781da177e4SLinus Torvalds 			}
12791da177e4SLinus Torvalds 		}
12801da177e4SLinus Torvalds 		break;
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
12831da177e4SLinus Torvalds 	case cop1x_op:{
1284515b029dSDavid Daney 		int sig = fpux_emu(xcp, ctx, ir, fault_addr);
1285515b029dSDavid Daney 		if (sig)
12861da177e4SLinus Torvalds 			return sig;
12871da177e4SLinus Torvalds 		break;
12881da177e4SLinus Torvalds 	}
12891da177e4SLinus Torvalds #endif
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds #if __mips >= 4
12921da177e4SLinus Torvalds 	case spec_op:
12931da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != movc_op)
12941da177e4SLinus Torvalds 			return SIGILL;
12951da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_RT(ir) >> 2];
12961da177e4SLinus Torvalds 		if (((ctx->fcr31 & cond) != 0) == ((MIPSInst_RT(ir) & 1) != 0))
12971da177e4SLinus Torvalds 			xcp->regs[MIPSInst_RD(ir)] =
12981da177e4SLinus Torvalds 				xcp->regs[MIPSInst_RS(ir)];
12991da177e4SLinus Torvalds 		break;
13001da177e4SLinus Torvalds #endif
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	default:
13031ac94400SLeonid Yegoshin sigill:
13041da177e4SLinus Torvalds 		return SIGILL;
13051da177e4SLinus Torvalds 	}
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	/* we did it !! */
1308e70dfc10SAtsushi Nemoto 	xcp->cp0_epc = contpc;
13091da177e4SLinus Torvalds 	xcp->cp0_cause &= ~CAUSEF_BD;
1310333d1f67SRalf Baechle 
13111da177e4SLinus Torvalds 	return 0;
13121da177e4SLinus Torvalds }
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds /*
13151da177e4SLinus Torvalds  * Conversion table from MIPS compare ops 48-63
13161da177e4SLinus Torvalds  * cond = ieee754dp_cmp(x,y,IEEE754_UN,sig);
13171da177e4SLinus Torvalds  */
13181da177e4SLinus Torvalds static const unsigned char cmptab[8] = {
13191da177e4SLinus Torvalds 	0,			/* cmp_0 (sig) cmp_sf */
13201da177e4SLinus Torvalds 	IEEE754_CUN,		/* cmp_un (sig) cmp_ngle */
13211da177e4SLinus Torvalds 	IEEE754_CEQ,		/* cmp_eq (sig) cmp_seq */
13221da177e4SLinus Torvalds 	IEEE754_CEQ | IEEE754_CUN,	/* cmp_ueq (sig) cmp_ngl  */
13231da177e4SLinus Torvalds 	IEEE754_CLT,		/* cmp_olt (sig) cmp_lt */
13241da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CUN,	/* cmp_ult (sig) cmp_nge */
13251da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ,	/* cmp_ole (sig) cmp_le */
13261da177e4SLinus Torvalds 	IEEE754_CLT | IEEE754_CEQ | IEEE754_CUN,	/* cmp_ule (sig) cmp_ngt */
13271da177e4SLinus Torvalds };
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds /*
13331da177e4SLinus Torvalds  * Additional MIPS4 instructions
13341da177e4SLinus Torvalds  */
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds #define DEF3OP(name, p, f1, f2, f3) \
13371da177e4SLinus Torvalds static ieee754##p fpemu_##p##_##name(ieee754##p r, ieee754##p s, \
13381da177e4SLinus Torvalds     ieee754##p t) \
13391da177e4SLinus Torvalds { \
1340cd21dfcfSRalf Baechle 	struct _ieee754_csr ieee754_csr_save; \
13411da177e4SLinus Torvalds 	s = f1(s, t); \
13421da177e4SLinus Torvalds 	ieee754_csr_save = ieee754_csr; \
13431da177e4SLinus Torvalds 	s = f2(s, r); \
13441da177e4SLinus Torvalds 	ieee754_csr_save.cx |= ieee754_csr.cx; \
13451da177e4SLinus Torvalds 	ieee754_csr_save.sx |= ieee754_csr.sx; \
13461da177e4SLinus Torvalds 	s = f3(s); \
13471da177e4SLinus Torvalds 	ieee754_csr.cx |= ieee754_csr_save.cx; \
13481da177e4SLinus Torvalds 	ieee754_csr.sx |= ieee754_csr_save.sx; \
13491da177e4SLinus Torvalds 	return s; \
13501da177e4SLinus Torvalds }
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds static ieee754dp fpemu_dp_recip(ieee754dp d)
13531da177e4SLinus Torvalds {
13541da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), d);
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds static ieee754dp fpemu_dp_rsqrt(ieee754dp d)
13581da177e4SLinus Torvalds {
13591da177e4SLinus Torvalds 	return ieee754dp_div(ieee754dp_one(0), ieee754dp_sqrt(d));
13601da177e4SLinus Torvalds }
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds static ieee754sp fpemu_sp_recip(ieee754sp s)
13631da177e4SLinus Torvalds {
13641da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), s);
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds static ieee754sp fpemu_sp_rsqrt(ieee754sp s)
13681da177e4SLinus Torvalds {
13691da177e4SLinus Torvalds 	return ieee754sp_div(ieee754sp_one(0), ieee754sp_sqrt(s));
13701da177e4SLinus Torvalds }
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds DEF3OP(madd, sp, ieee754sp_mul, ieee754sp_add, );
13731da177e4SLinus Torvalds DEF3OP(msub, sp, ieee754sp_mul, ieee754sp_sub, );
13741da177e4SLinus Torvalds DEF3OP(nmadd, sp, ieee754sp_mul, ieee754sp_add, ieee754sp_neg);
13751da177e4SLinus Torvalds DEF3OP(nmsub, sp, ieee754sp_mul, ieee754sp_sub, ieee754sp_neg);
13761da177e4SLinus Torvalds DEF3OP(madd, dp, ieee754dp_mul, ieee754dp_add, );
13771da177e4SLinus Torvalds DEF3OP(msub, dp, ieee754dp_mul, ieee754dp_sub, );
13781da177e4SLinus Torvalds DEF3OP(nmadd, dp, ieee754dp_mul, ieee754dp_add, ieee754dp_neg);
13791da177e4SLinus Torvalds DEF3OP(nmsub, dp, ieee754dp_mul, ieee754dp_sub, ieee754dp_neg);
13801da177e4SLinus Torvalds 
1381eae89076SAtsushi Nemoto static int fpux_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
1382515b029dSDavid Daney 	mips_instruction ir, void *__user *fault_addr)
13831da177e4SLinus Torvalds {
13841da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
13851da177e4SLinus Torvalds 
1386b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1xops);
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	switch (MIPSInst_FMA_FFMT(ir)) {
13891da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 		ieee754sp(*handler) (ieee754sp, ieee754sp, ieee754sp);
13921da177e4SLinus Torvalds 		ieee754sp fd, fr, fs, ft;
13933fccc015SRalf Baechle 		u32 __user *va;
13941da177e4SLinus Torvalds 		u32 val;
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
13971da177e4SLinus Torvalds 		case lwxc1_op:
13983fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
13991da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14001da177e4SLinus Torvalds 
1401b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1402515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u32))) {
1403b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1404515b029dSDavid Daney 				*fault_addr = va;
14051da177e4SLinus Torvalds 				return SIGBUS;
14061da177e4SLinus Torvalds 			}
1407515b029dSDavid Daney 			if (__get_user(val, va)) {
1408515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1409515b029dSDavid Daney 				*fault_addr = va;
1410515b029dSDavid Daney 				return SIGSEGV;
1411515b029dSDavid Daney 			}
14121da177e4SLinus Torvalds 			SITOREG(val, MIPSInst_FD(ir));
14131da177e4SLinus Torvalds 			break;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 		case swxc1_op:
14163fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14171da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14181da177e4SLinus Torvalds 
1419b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 			SIFROMREG(val, MIPSInst_FS(ir));
1422515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u32))) {
1423515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1424515b029dSDavid Daney 				*fault_addr = va;
1425515b029dSDavid Daney 				return SIGBUS;
1426515b029dSDavid Daney 			}
14271da177e4SLinus Torvalds 			if (put_user(val, va)) {
1428b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1429515b029dSDavid Daney 				*fault_addr = va;
1430515b029dSDavid Daney 				return SIGSEGV;
14311da177e4SLinus Torvalds 			}
14321da177e4SLinus Torvalds 			break;
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 		case madd_s_op:
14351da177e4SLinus Torvalds 			handler = fpemu_sp_madd;
14361da177e4SLinus Torvalds 			goto scoptop;
14371da177e4SLinus Torvalds 		case msub_s_op:
14381da177e4SLinus Torvalds 			handler = fpemu_sp_msub;
14391da177e4SLinus Torvalds 			goto scoptop;
14401da177e4SLinus Torvalds 		case nmadd_s_op:
14411da177e4SLinus Torvalds 			handler = fpemu_sp_nmadd;
14421da177e4SLinus Torvalds 			goto scoptop;
14431da177e4SLinus Torvalds 		case nmsub_s_op:
14441da177e4SLinus Torvalds 			handler = fpemu_sp_nmsub;
14451da177e4SLinus Torvalds 			goto scoptop;
14461da177e4SLinus Torvalds 
14471da177e4SLinus Torvalds 		      scoptop:
14481da177e4SLinus Torvalds 			SPFROMREG(fr, MIPSInst_FR(ir));
14491da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
14501da177e4SLinus Torvalds 			SPFROMREG(ft, MIPSInst_FT(ir));
14511da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
14521da177e4SLinus Torvalds 			SPTOREG(fd, MIPSInst_FD(ir));
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 		      copcsr:
14551da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INEXACT))
14561da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
14571da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_UNDERFLOW))
14581da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
14591da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_OVERFLOW))
14601da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
14611da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
14621da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 			ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
14651da177e4SLinus Torvalds 			if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
14661da177e4SLinus Torvalds 				/*printk ("SIGFPE: fpu csr = %08x\n",
14671da177e4SLinus Torvalds 				   ctx->fcr31); */
14681da177e4SLinus Torvalds 				return SIGFPE;
14691da177e4SLinus Torvalds 			}
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 			break;
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 		default:
14741da177e4SLinus Torvalds 			return SIGILL;
14751da177e4SLinus Torvalds 		}
14761da177e4SLinus Torvalds 		break;
14771da177e4SLinus Torvalds 	}
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 	case d_fmt:{		/* 1 */
14801da177e4SLinus Torvalds 		ieee754dp(*handler) (ieee754dp, ieee754dp, ieee754dp);
14811da177e4SLinus Torvalds 		ieee754dp fd, fr, fs, ft;
14823fccc015SRalf Baechle 		u64 __user *va;
14831da177e4SLinus Torvalds 		u64 val;
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
14861da177e4SLinus Torvalds 		case ldxc1_op:
14873fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
14881da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
14891da177e4SLinus Torvalds 
1490b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(loads);
1491515b029dSDavid Daney 			if (!access_ok(VERIFY_READ, va, sizeof(u64))) {
1492b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1493515b029dSDavid Daney 				*fault_addr = va;
14941da177e4SLinus Torvalds 				return SIGBUS;
14951da177e4SLinus Torvalds 			}
1496515b029dSDavid Daney 			if (__get_user(val, va)) {
1497515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1498515b029dSDavid Daney 				*fault_addr = va;
1499515b029dSDavid Daney 				return SIGSEGV;
1500515b029dSDavid Daney 			}
15011da177e4SLinus Torvalds 			DITOREG(val, MIPSInst_FD(ir));
15021da177e4SLinus Torvalds 			break;
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds 		case sdxc1_op:
15053fccc015SRalf Baechle 			va = (void __user *) (xcp->regs[MIPSInst_FR(ir)] +
15061da177e4SLinus Torvalds 				xcp->regs[MIPSInst_FT(ir)]);
15071da177e4SLinus Torvalds 
1508b6ee75edSDavid Daney 			MIPS_FPU_EMU_INC_STATS(stores);
15091da177e4SLinus Torvalds 			DIFROMREG(val, MIPSInst_FS(ir));
1510515b029dSDavid Daney 			if (!access_ok(VERIFY_WRITE, va, sizeof(u64))) {
1511b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1512515b029dSDavid Daney 				*fault_addr = va;
15131da177e4SLinus Torvalds 				return SIGBUS;
15141da177e4SLinus Torvalds 			}
1515515b029dSDavid Daney 			if (__put_user(val, va)) {
1516515b029dSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
1517515b029dSDavid Daney 				*fault_addr = va;
1518515b029dSDavid Daney 				return SIGSEGV;
1519515b029dSDavid Daney 			}
15201da177e4SLinus Torvalds 			break;
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 		case madd_d_op:
15231da177e4SLinus Torvalds 			handler = fpemu_dp_madd;
15241da177e4SLinus Torvalds 			goto dcoptop;
15251da177e4SLinus Torvalds 		case msub_d_op:
15261da177e4SLinus Torvalds 			handler = fpemu_dp_msub;
15271da177e4SLinus Torvalds 			goto dcoptop;
15281da177e4SLinus Torvalds 		case nmadd_d_op:
15291da177e4SLinus Torvalds 			handler = fpemu_dp_nmadd;
15301da177e4SLinus Torvalds 			goto dcoptop;
15311da177e4SLinus Torvalds 		case nmsub_d_op:
15321da177e4SLinus Torvalds 			handler = fpemu_dp_nmsub;
15331da177e4SLinus Torvalds 			goto dcoptop;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 		      dcoptop:
15361da177e4SLinus Torvalds 			DPFROMREG(fr, MIPSInst_FR(ir));
15371da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
15381da177e4SLinus Torvalds 			DPFROMREG(ft, MIPSInst_FT(ir));
15391da177e4SLinus Torvalds 			fd = (*handler) (fr, fs, ft);
15401da177e4SLinus Torvalds 			DPTOREG(fd, MIPSInst_FD(ir));
15411da177e4SLinus Torvalds 			goto copcsr;
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 		default:
15441da177e4SLinus Torvalds 			return SIGILL;
15451da177e4SLinus Torvalds 		}
15461da177e4SLinus Torvalds 		break;
15471da177e4SLinus Torvalds 	}
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	case 0x7:		/* 7 */
15501da177e4SLinus Torvalds 		if (MIPSInst_FUNC(ir) != pfetch_op) {
15511da177e4SLinus Torvalds 			return SIGILL;
15521da177e4SLinus Torvalds 		}
15531da177e4SLinus Torvalds 		/* ignore prefx operation */
15541da177e4SLinus Torvalds 		break;
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	default:
15571da177e4SLinus Torvalds 		return SIGILL;
15581da177e4SLinus Torvalds 	}
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	return 0;
15611da177e4SLinus Torvalds }
15621da177e4SLinus Torvalds #endif
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds /*
15671da177e4SLinus Torvalds  * Emulate a single COP1 arithmetic instruction.
15681da177e4SLinus Torvalds  */
1569eae89076SAtsushi Nemoto static int fpu_emu(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
15701da177e4SLinus Torvalds 	mips_instruction ir)
15711da177e4SLinus Torvalds {
15721da177e4SLinus Torvalds 	int rfmt;		/* resulting format */
15731da177e4SLinus Torvalds 	unsigned rcsr = 0;	/* resulting csr */
15741da177e4SLinus Torvalds 	unsigned cond;
15751da177e4SLinus Torvalds 	union {
15761da177e4SLinus Torvalds 		ieee754dp d;
15771da177e4SLinus Torvalds 		ieee754sp s;
15781da177e4SLinus Torvalds 		int w;
1579766160c2SYoichi Yuasa #ifdef __mips64
15801da177e4SLinus Torvalds 		s64 l;
15811da177e4SLinus Torvalds #endif
15821da177e4SLinus Torvalds 	} rv;			/* resulting value */
15831da177e4SLinus Torvalds 
1584b6ee75edSDavid Daney 	MIPS_FPU_EMU_INC_STATS(cp1ops);
15851da177e4SLinus Torvalds 	switch (rfmt = (MIPSInst_FFMT(ir) & 0xf)) {
15861da177e4SLinus Torvalds 	case s_fmt:{		/* 0 */
15871da177e4SLinus Torvalds 		union {
15881da177e4SLinus Torvalds 			ieee754sp(*b) (ieee754sp, ieee754sp);
15891da177e4SLinus Torvalds 			ieee754sp(*u) (ieee754sp);
15901da177e4SLinus Torvalds 		} handler;
15911da177e4SLinus Torvalds 
15921da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
15931da177e4SLinus Torvalds 			/* binary ops */
15941da177e4SLinus Torvalds 		case fadd_op:
15951da177e4SLinus Torvalds 			handler.b = ieee754sp_add;
15961da177e4SLinus Torvalds 			goto scopbop;
15971da177e4SLinus Torvalds 		case fsub_op:
15981da177e4SLinus Torvalds 			handler.b = ieee754sp_sub;
15991da177e4SLinus Torvalds 			goto scopbop;
16001da177e4SLinus Torvalds 		case fmul_op:
16011da177e4SLinus Torvalds 			handler.b = ieee754sp_mul;
16021da177e4SLinus Torvalds 			goto scopbop;
16031da177e4SLinus Torvalds 		case fdiv_op:
16041da177e4SLinus Torvalds 			handler.b = ieee754sp_div;
16051da177e4SLinus Torvalds 			goto scopbop;
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 			/* unary  ops */
1608587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64)
16091da177e4SLinus Torvalds 		case fsqrt_op:
16101da177e4SLinus Torvalds 			handler.u = ieee754sp_sqrt;
16111da177e4SLinus Torvalds 			goto scopuop;
16121da177e4SLinus Torvalds #endif
16131da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
16141da177e4SLinus Torvalds 		case frsqrt_op:
16151da177e4SLinus Torvalds 			handler.u = fpemu_sp_rsqrt;
16161da177e4SLinus Torvalds 			goto scopuop;
16171da177e4SLinus Torvalds 		case frecip_op:
16181da177e4SLinus Torvalds 			handler.u = fpemu_sp_recip;
16191da177e4SLinus Torvalds 			goto scopuop;
16201da177e4SLinus Torvalds #endif
16211da177e4SLinus Torvalds #if __mips >= 4
16221da177e4SLinus Torvalds 		case fmovc_op:
16231da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
16241da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
16251da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
16261da177e4SLinus Torvalds 				return 0;
16271da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
16281da177e4SLinus Torvalds 			break;
16291da177e4SLinus Torvalds 		case fmovz_op:
16301da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
16311da177e4SLinus Torvalds 				return 0;
16321da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
16331da177e4SLinus Torvalds 			break;
16341da177e4SLinus Torvalds 		case fmovn_op:
16351da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
16361da177e4SLinus Torvalds 				return 0;
16371da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
16381da177e4SLinus Torvalds 			break;
16391da177e4SLinus Torvalds #endif
16401da177e4SLinus Torvalds 		case fabs_op:
16411da177e4SLinus Torvalds 			handler.u = ieee754sp_abs;
16421da177e4SLinus Torvalds 			goto scopuop;
16431da177e4SLinus Torvalds 		case fneg_op:
16441da177e4SLinus Torvalds 			handler.u = ieee754sp_neg;
16451da177e4SLinus Torvalds 			goto scopuop;
16461da177e4SLinus Torvalds 		case fmov_op:
16471da177e4SLinus Torvalds 			/* an easy one */
16481da177e4SLinus Torvalds 			SPFROMREG(rv.s, MIPSInst_FS(ir));
16491da177e4SLinus Torvalds 			goto copcsr;
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 			/* binary op on handler */
16521da177e4SLinus Torvalds 		      scopbop:
16531da177e4SLinus Torvalds 			{
16541da177e4SLinus Torvalds 				ieee754sp fs, ft;
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
16571da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds 				rv.s = (*handler.b) (fs, ft);
16601da177e4SLinus Torvalds 				goto copcsr;
16611da177e4SLinus Torvalds 			}
16621da177e4SLinus Torvalds 		      scopuop:
16631da177e4SLinus Torvalds 			{
16641da177e4SLinus Torvalds 				ieee754sp fs;
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
16671da177e4SLinus Torvalds 				rv.s = (*handler.u) (fs);
16681da177e4SLinus Torvalds 				goto copcsr;
16691da177e4SLinus Torvalds 			}
16701da177e4SLinus Torvalds 		      copcsr:
16711da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INEXACT))
16721da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INE_X | FPU_CSR_INE_S;
16731da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_UNDERFLOW))
16741da177e4SLinus Torvalds 				rcsr |= FPU_CSR_UDF_X | FPU_CSR_UDF_S;
16751da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_OVERFLOW))
16761da177e4SLinus Torvalds 				rcsr |= FPU_CSR_OVF_X | FPU_CSR_OVF_S;
16771da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_ZERO_DIVIDE))
16781da177e4SLinus Torvalds 				rcsr |= FPU_CSR_DIV_X | FPU_CSR_DIV_S;
16791da177e4SLinus Torvalds 			if (ieee754_cxtest(IEEE754_INVALID_OPERATION))
16801da177e4SLinus Torvalds 				rcsr |= FPU_CSR_INV_X | FPU_CSR_INV_S;
16811da177e4SLinus Torvalds 			break;
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 			/* unary conv ops */
16841da177e4SLinus Torvalds 		case fcvts_op:
16851da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
16861da177e4SLinus Torvalds 		case fcvtd_op:{
16871da177e4SLinus Torvalds 			ieee754sp fs;
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
16901da177e4SLinus Torvalds 			rv.d = ieee754dp_fsp(fs);
16911da177e4SLinus Torvalds 			rfmt = d_fmt;
16921da177e4SLinus Torvalds 			goto copcsr;
16931da177e4SLinus Torvalds 		}
16941da177e4SLinus Torvalds 		case fcvtw_op:{
16951da177e4SLinus Torvalds 			ieee754sp fs;
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
16981da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
16991da177e4SLinus Torvalds 			rfmt = w_fmt;
17001da177e4SLinus Torvalds 			goto copcsr;
17011da177e4SLinus Torvalds 		}
17021da177e4SLinus Torvalds 
1703587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64)
17041da177e4SLinus Torvalds 		case fround_op:
17051da177e4SLinus Torvalds 		case ftrunc_op:
17061da177e4SLinus Torvalds 		case fceil_op:
17071da177e4SLinus Torvalds 		case ffloor_op:{
17081da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
17091da177e4SLinus Torvalds 			ieee754sp fs;
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
17123f135530SShane McDonald 			ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
17131da177e4SLinus Torvalds 			rv.w = ieee754sp_tint(fs);
17141da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
17151da177e4SLinus Torvalds 			rfmt = w_fmt;
17161da177e4SLinus Torvalds 			goto copcsr;
17171da177e4SLinus Torvalds 		}
17181da177e4SLinus Torvalds #endif /* __mips >= 2 */
17191da177e4SLinus Torvalds 
17204b724efdSRalf Baechle #if defined(__mips64)
17211da177e4SLinus Torvalds 		case fcvtl_op:{
17221da177e4SLinus Torvalds 			ieee754sp fs;
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
17251da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
17261da177e4SLinus Torvalds 			rfmt = l_fmt;
17271da177e4SLinus Torvalds 			goto copcsr;
17281da177e4SLinus Torvalds 		}
17291da177e4SLinus Torvalds 
17301da177e4SLinus Torvalds 		case froundl_op:
17311da177e4SLinus Torvalds 		case ftruncl_op:
17321da177e4SLinus Torvalds 		case fceill_op:
17331da177e4SLinus Torvalds 		case ffloorl_op:{
17341da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
17351da177e4SLinus Torvalds 			ieee754sp fs;
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
17383f135530SShane McDonald 			ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
17391da177e4SLinus Torvalds 			rv.l = ieee754sp_tlong(fs);
17401da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
17411da177e4SLinus Torvalds 			rfmt = l_fmt;
17421da177e4SLinus Torvalds 			goto copcsr;
17431da177e4SLinus Torvalds 		}
17444b724efdSRalf Baechle #endif /* defined(__mips64) */
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds 		default:
17471da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
17481da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
17491da177e4SLinus Torvalds 				ieee754sp fs, ft;
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 				SPFROMREG(fs, MIPSInst_FS(ir));
17521da177e4SLinus Torvalds 				SPFROMREG(ft, MIPSInst_FT(ir));
17531da177e4SLinus Torvalds 				rv.w = ieee754sp_cmp(fs, ft,
17541da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
17551da177e4SLinus Torvalds 				rfmt = -1;
17561da177e4SLinus Torvalds 				if ((cmpop & 0x8) && ieee754_cxtest
17571da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
17581da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
17591da177e4SLinus Torvalds 				else
17601da177e4SLinus Torvalds 					goto copcsr;
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 			}
17631da177e4SLinus Torvalds 			else {
17641da177e4SLinus Torvalds 				return SIGILL;
17651da177e4SLinus Torvalds 			}
17661da177e4SLinus Torvalds 			break;
17671da177e4SLinus Torvalds 		}
17681da177e4SLinus Torvalds 		break;
17691da177e4SLinus Torvalds 	}
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	case d_fmt:{
17721da177e4SLinus Torvalds 		union {
17731da177e4SLinus Torvalds 			ieee754dp(*b) (ieee754dp, ieee754dp);
17741da177e4SLinus Torvalds 			ieee754dp(*u) (ieee754dp);
17751da177e4SLinus Torvalds 		} handler;
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
17781da177e4SLinus Torvalds 			/* binary ops */
17791da177e4SLinus Torvalds 		case fadd_op:
17801da177e4SLinus Torvalds 			handler.b = ieee754dp_add;
17811da177e4SLinus Torvalds 			goto dcopbop;
17821da177e4SLinus Torvalds 		case fsub_op:
17831da177e4SLinus Torvalds 			handler.b = ieee754dp_sub;
17841da177e4SLinus Torvalds 			goto dcopbop;
17851da177e4SLinus Torvalds 		case fmul_op:
17861da177e4SLinus Torvalds 			handler.b = ieee754dp_mul;
17871da177e4SLinus Torvalds 			goto dcopbop;
17881da177e4SLinus Torvalds 		case fdiv_op:
17891da177e4SLinus Torvalds 			handler.b = ieee754dp_div;
17901da177e4SLinus Torvalds 			goto dcopbop;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 			/* unary  ops */
1793587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64)
17941da177e4SLinus Torvalds 		case fsqrt_op:
17951da177e4SLinus Torvalds 			handler.u = ieee754dp_sqrt;
17961da177e4SLinus Torvalds 			goto dcopuop;
17971da177e4SLinus Torvalds #endif
17981da177e4SLinus Torvalds #if __mips >= 4 && __mips != 32
17991da177e4SLinus Torvalds 		case frsqrt_op:
18001da177e4SLinus Torvalds 			handler.u = fpemu_dp_rsqrt;
18011da177e4SLinus Torvalds 			goto dcopuop;
18021da177e4SLinus Torvalds 		case frecip_op:
18031da177e4SLinus Torvalds 			handler.u = fpemu_dp_recip;
18041da177e4SLinus Torvalds 			goto dcopuop;
18051da177e4SLinus Torvalds #endif
18061da177e4SLinus Torvalds #if __mips >= 4
18071da177e4SLinus Torvalds 		case fmovc_op:
18081da177e4SLinus Torvalds 			cond = fpucondbit[MIPSInst_FT(ir) >> 2];
18091da177e4SLinus Torvalds 			if (((ctx->fcr31 & cond) != 0) !=
18101da177e4SLinus Torvalds 				((MIPSInst_FT(ir) & 1) != 0))
18111da177e4SLinus Torvalds 				return 0;
18121da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
18131da177e4SLinus Torvalds 			break;
18141da177e4SLinus Torvalds 		case fmovz_op:
18151da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] != 0)
18161da177e4SLinus Torvalds 				return 0;
18171da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
18181da177e4SLinus Torvalds 			break;
18191da177e4SLinus Torvalds 		case fmovn_op:
18201da177e4SLinus Torvalds 			if (xcp->regs[MIPSInst_FT(ir)] == 0)
18211da177e4SLinus Torvalds 				return 0;
18221da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
18231da177e4SLinus Torvalds 			break;
18241da177e4SLinus Torvalds #endif
18251da177e4SLinus Torvalds 		case fabs_op:
18261da177e4SLinus Torvalds 			handler.u = ieee754dp_abs;
18271da177e4SLinus Torvalds 			goto dcopuop;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 		case fneg_op:
18301da177e4SLinus Torvalds 			handler.u = ieee754dp_neg;
18311da177e4SLinus Torvalds 			goto dcopuop;
18321da177e4SLinus Torvalds 
18331da177e4SLinus Torvalds 		case fmov_op:
18341da177e4SLinus Torvalds 			/* an easy one */
18351da177e4SLinus Torvalds 			DPFROMREG(rv.d, MIPSInst_FS(ir));
18361da177e4SLinus Torvalds 			goto copcsr;
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds 			/* binary op on handler */
18391da177e4SLinus Torvalds 		      dcopbop:{
18401da177e4SLinus Torvalds 				ieee754dp fs, ft;
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
18431da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds 				rv.d = (*handler.b) (fs, ft);
18461da177e4SLinus Torvalds 				goto copcsr;
18471da177e4SLinus Torvalds 			}
18481da177e4SLinus Torvalds 		      dcopuop:{
18491da177e4SLinus Torvalds 				ieee754dp fs;
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
18521da177e4SLinus Torvalds 				rv.d = (*handler.u) (fs);
18531da177e4SLinus Torvalds 				goto copcsr;
18541da177e4SLinus Torvalds 			}
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 			/* unary conv ops */
18571da177e4SLinus Torvalds 		case fcvts_op:{
18581da177e4SLinus Torvalds 			ieee754dp fs;
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18611da177e4SLinus Torvalds 			rv.s = ieee754sp_fdp(fs);
18621da177e4SLinus Torvalds 			rfmt = s_fmt;
18631da177e4SLinus Torvalds 			goto copcsr;
18641da177e4SLinus Torvalds 		}
18651da177e4SLinus Torvalds 		case fcvtd_op:
18661da177e4SLinus Torvalds 			return SIGILL;	/* not defined */
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 		case fcvtw_op:{
18691da177e4SLinus Torvalds 			ieee754dp fs;
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18721da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);	/* wrong */
18731da177e4SLinus Torvalds 			rfmt = w_fmt;
18741da177e4SLinus Torvalds 			goto copcsr;
18751da177e4SLinus Torvalds 		}
18761da177e4SLinus Torvalds 
1877587cb98fSRalf Baechle #if __mips >= 2 || defined(__mips64)
18781da177e4SLinus Torvalds 		case fround_op:
18791da177e4SLinus Torvalds 		case ftrunc_op:
18801da177e4SLinus Torvalds 		case fceil_op:
18811da177e4SLinus Torvalds 		case ffloor_op:{
18821da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
18831da177e4SLinus Torvalds 			ieee754dp fs;
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18863f135530SShane McDonald 			ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
18871da177e4SLinus Torvalds 			rv.w = ieee754dp_tint(fs);
18881da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
18891da177e4SLinus Torvalds 			rfmt = w_fmt;
18901da177e4SLinus Torvalds 			goto copcsr;
18911da177e4SLinus Torvalds 		}
18921da177e4SLinus Torvalds #endif
18931da177e4SLinus Torvalds 
18944b724efdSRalf Baechle #if defined(__mips64)
18951da177e4SLinus Torvalds 		case fcvtl_op:{
18961da177e4SLinus Torvalds 			ieee754dp fs;
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
18991da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
19001da177e4SLinus Torvalds 			rfmt = l_fmt;
19011da177e4SLinus Torvalds 			goto copcsr;
19021da177e4SLinus Torvalds 		}
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 		case froundl_op:
19051da177e4SLinus Torvalds 		case ftruncl_op:
19061da177e4SLinus Torvalds 		case fceill_op:
19071da177e4SLinus Torvalds 		case ffloorl_op:{
19081da177e4SLinus Torvalds 			unsigned int oldrm = ieee754_csr.rm;
19091da177e4SLinus Torvalds 			ieee754dp fs;
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 			DPFROMREG(fs, MIPSInst_FS(ir));
19123f135530SShane McDonald 			ieee754_csr.rm = ieee_rm[modeindex(MIPSInst_FUNC(ir))];
19131da177e4SLinus Torvalds 			rv.l = ieee754dp_tlong(fs);
19141da177e4SLinus Torvalds 			ieee754_csr.rm = oldrm;
19151da177e4SLinus Torvalds 			rfmt = l_fmt;
19161da177e4SLinus Torvalds 			goto copcsr;
19171da177e4SLinus Torvalds 		}
19184b724efdSRalf Baechle #endif /* __mips >= 3 */
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 		default:
19211da177e4SLinus Torvalds 			if (MIPSInst_FUNC(ir) >= fcmp_op) {
19221da177e4SLinus Torvalds 				unsigned cmpop = MIPSInst_FUNC(ir) - fcmp_op;
19231da177e4SLinus Torvalds 				ieee754dp fs, ft;
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 				DPFROMREG(fs, MIPSInst_FS(ir));
19261da177e4SLinus Torvalds 				DPFROMREG(ft, MIPSInst_FT(ir));
19271da177e4SLinus Torvalds 				rv.w = ieee754dp_cmp(fs, ft,
19281da177e4SLinus Torvalds 					cmptab[cmpop & 0x7], cmpop & 0x8);
19291da177e4SLinus Torvalds 				rfmt = -1;
19301da177e4SLinus Torvalds 				if ((cmpop & 0x8)
19311da177e4SLinus Torvalds 					&&
19321da177e4SLinus Torvalds 					ieee754_cxtest
19331da177e4SLinus Torvalds 					(IEEE754_INVALID_OPERATION))
19341da177e4SLinus Torvalds 					rcsr = FPU_CSR_INV_X | FPU_CSR_INV_S;
19351da177e4SLinus Torvalds 				else
19361da177e4SLinus Torvalds 					goto copcsr;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 			}
19391da177e4SLinus Torvalds 			else {
19401da177e4SLinus Torvalds 				return SIGILL;
19411da177e4SLinus Torvalds 			}
19421da177e4SLinus Torvalds 			break;
19431da177e4SLinus Torvalds 		}
19441da177e4SLinus Torvalds 		break;
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	case w_fmt:{
19481da177e4SLinus Torvalds 		ieee754sp fs;
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
19511da177e4SLinus Torvalds 		case fcvts_op:
19521da177e4SLinus Torvalds 			/* convert word to single precision real */
19531da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19541da177e4SLinus Torvalds 			rv.s = ieee754sp_fint(fs.bits);
19551da177e4SLinus Torvalds 			rfmt = s_fmt;
19561da177e4SLinus Torvalds 			goto copcsr;
19571da177e4SLinus Torvalds 		case fcvtd_op:
19581da177e4SLinus Torvalds 			/* convert word to double precision real */
19591da177e4SLinus Torvalds 			SPFROMREG(fs, MIPSInst_FS(ir));
19601da177e4SLinus Torvalds 			rv.d = ieee754dp_fint(fs.bits);
19611da177e4SLinus Torvalds 			rfmt = d_fmt;
19621da177e4SLinus Torvalds 			goto copcsr;
19631da177e4SLinus Torvalds 		default:
19641da177e4SLinus Torvalds 			return SIGILL;
19651da177e4SLinus Torvalds 		}
19661da177e4SLinus Torvalds 		break;
19671da177e4SLinus Torvalds 	}
19681da177e4SLinus Torvalds 
19694b724efdSRalf Baechle #if defined(__mips64)
19701da177e4SLinus Torvalds 	case l_fmt:{
1971*bbd426f5SPaul Burton 		u64 bits;
1972*bbd426f5SPaul Burton 		DIFROMREG(bits, MIPSInst_FS(ir));
1973*bbd426f5SPaul Burton 
19741da177e4SLinus Torvalds 		switch (MIPSInst_FUNC(ir)) {
19751da177e4SLinus Torvalds 		case fcvts_op:
19761da177e4SLinus Torvalds 			/* convert long to single precision real */
1977*bbd426f5SPaul Burton 			rv.s = ieee754sp_flong(bits);
19781da177e4SLinus Torvalds 			rfmt = s_fmt;
19791da177e4SLinus Torvalds 			goto copcsr;
19801da177e4SLinus Torvalds 		case fcvtd_op:
19811da177e4SLinus Torvalds 			/* convert long to double precision real */
1982*bbd426f5SPaul Burton 			rv.d = ieee754dp_flong(bits);
19831da177e4SLinus Torvalds 			rfmt = d_fmt;
19841da177e4SLinus Torvalds 			goto copcsr;
19851da177e4SLinus Torvalds 		default:
19861da177e4SLinus Torvalds 			return SIGILL;
19871da177e4SLinus Torvalds 		}
19881da177e4SLinus Torvalds 		break;
19891da177e4SLinus Torvalds 	}
19901da177e4SLinus Torvalds #endif
19911da177e4SLinus Torvalds 
19921da177e4SLinus Torvalds 	default:
19931da177e4SLinus Torvalds 		return SIGILL;
19941da177e4SLinus Torvalds 	}
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	/*
19971da177e4SLinus Torvalds 	 * Update the fpu CSR register for this operation.
19981da177e4SLinus Torvalds 	 * If an exception is required, generate a tidy SIGFPE exception,
19991da177e4SLinus Torvalds 	 * without updating the result register.
20001da177e4SLinus Torvalds 	 * Note: cause exception bits do not accumulate, they are rewritten
20011da177e4SLinus Torvalds 	 * for each op; only the flag/sticky bits accumulate.
20021da177e4SLinus Torvalds 	 */
20031da177e4SLinus Torvalds 	ctx->fcr31 = (ctx->fcr31 & ~FPU_CSR_ALL_X) | rcsr;
20041da177e4SLinus Torvalds 	if ((ctx->fcr31 >> 5) & ctx->fcr31 & FPU_CSR_ALL_E) {
20051da177e4SLinus Torvalds 		/*printk ("SIGFPE: fpu csr = %08x\n",ctx->fcr31); */
20061da177e4SLinus Torvalds 		return SIGFPE;
20071da177e4SLinus Torvalds 	}
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds 	/*
20101da177e4SLinus Torvalds 	 * Now we can safely write the result back to the register file.
20111da177e4SLinus Torvalds 	 */
20121da177e4SLinus Torvalds 	switch (rfmt) {
20131da177e4SLinus Torvalds 	case -1:{
20141da177e4SLinus Torvalds #if __mips >= 4
20151da177e4SLinus Torvalds 		cond = fpucondbit[MIPSInst_FD(ir) >> 2];
20161da177e4SLinus Torvalds #else
20171da177e4SLinus Torvalds 		cond = FPU_CSR_COND;
20181da177e4SLinus Torvalds #endif
20191da177e4SLinus Torvalds 		if (rv.w)
20201da177e4SLinus Torvalds 			ctx->fcr31 |= cond;
20211da177e4SLinus Torvalds 		else
20221da177e4SLinus Torvalds 			ctx->fcr31 &= ~cond;
20231da177e4SLinus Torvalds 		break;
20241da177e4SLinus Torvalds 	}
20251da177e4SLinus Torvalds 	case d_fmt:
20261da177e4SLinus Torvalds 		DPTOREG(rv.d, MIPSInst_FD(ir));
20271da177e4SLinus Torvalds 		break;
20281da177e4SLinus Torvalds 	case s_fmt:
20291da177e4SLinus Torvalds 		SPTOREG(rv.s, MIPSInst_FD(ir));
20301da177e4SLinus Torvalds 		break;
20311da177e4SLinus Torvalds 	case w_fmt:
20321da177e4SLinus Torvalds 		SITOREG(rv.w, MIPSInst_FD(ir));
20331da177e4SLinus Torvalds 		break;
20344b724efdSRalf Baechle #if defined(__mips64)
20351da177e4SLinus Torvalds 	case l_fmt:
20361da177e4SLinus Torvalds 		DITOREG(rv.l, MIPSInst_FD(ir));
20371da177e4SLinus Torvalds 		break;
20381da177e4SLinus Torvalds #endif
20391da177e4SLinus Torvalds 	default:
20401da177e4SLinus Torvalds 		return SIGILL;
20411da177e4SLinus Torvalds 	}
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	return 0;
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
2046e04582b7SAtsushi Nemoto int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
2047515b029dSDavid Daney 	int has_fpu, void *__user *fault_addr)
20481da177e4SLinus Torvalds {
2049333d1f67SRalf Baechle 	unsigned long oldepc, prevepc;
2050102cedc3SLeonid Yegoshin 	struct mm_decoded_insn dec_insn;
2051102cedc3SLeonid Yegoshin 	u16 instr[4];
2052102cedc3SLeonid Yegoshin 	u16 *instr_ptr;
20531da177e4SLinus Torvalds 	int sig = 0;
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	oldepc = xcp->cp0_epc;
20561da177e4SLinus Torvalds 	do {
20571da177e4SLinus Torvalds 		prevepc = xcp->cp0_epc;
20581da177e4SLinus Torvalds 
2059102cedc3SLeonid Yegoshin 		if (get_isa16_mode(prevepc) && cpu_has_mmips) {
2060102cedc3SLeonid Yegoshin 			/*
2061102cedc3SLeonid Yegoshin 			 * Get next 2 microMIPS instructions and convert them
2062102cedc3SLeonid Yegoshin 			 * into 32-bit instructions.
2063102cedc3SLeonid Yegoshin 			 */
2064102cedc3SLeonid Yegoshin 			if ((get_user(instr[0], (u16 __user *)msk_isa16_mode(xcp->cp0_epc))) ||
2065102cedc3SLeonid Yegoshin 			    (get_user(instr[1], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 2))) ||
2066102cedc3SLeonid Yegoshin 			    (get_user(instr[2], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 4))) ||
2067102cedc3SLeonid Yegoshin 			    (get_user(instr[3], (u16 __user *)msk_isa16_mode(xcp->cp0_epc + 6)))) {
2068b6ee75edSDavid Daney 				MIPS_FPU_EMU_INC_STATS(errors);
20691da177e4SLinus Torvalds 				return SIGBUS;
20701da177e4SLinus Torvalds 			}
2071102cedc3SLeonid Yegoshin 			instr_ptr = instr;
2072102cedc3SLeonid Yegoshin 
2073102cedc3SLeonid Yegoshin 			/* Get first instruction. */
2074102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2075102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2076102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2077102cedc3SLeonid Yegoshin 					(*instr_ptr);
2078102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2079102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 2;
2080102cedc3SLeonid Yegoshin 				instr_ptr += 1;
2081102cedc3SLeonid Yegoshin 			} else {
2082102cedc3SLeonid Yegoshin 				dec_insn.insn = (*instr_ptr << 16) |
2083102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2084102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2085102cedc3SLeonid Yegoshin 				dec_insn.pc_inc = 4;
2086102cedc3SLeonid Yegoshin 				instr_ptr += 2;
2087515b029dSDavid Daney 			}
2088102cedc3SLeonid Yegoshin 			/* Get second instruction. */
2089102cedc3SLeonid Yegoshin 			if (mm_insn_16bit(*instr_ptr)) {
2090102cedc3SLeonid Yegoshin 				/* Duplicate the half-word. */
2091102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2092102cedc3SLeonid Yegoshin 					(*instr_ptr);
2093102cedc3SLeonid Yegoshin 				/* 16-bit instruction. */
2094102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 2;
2095102cedc3SLeonid Yegoshin 			} else {
2096102cedc3SLeonid Yegoshin 				dec_insn.next_insn = (*instr_ptr << 16) |
2097102cedc3SLeonid Yegoshin 					*(instr_ptr+1);
2098102cedc3SLeonid Yegoshin 				/* 32-bit instruction. */
2099102cedc3SLeonid Yegoshin 				dec_insn.next_pc_inc = 4;
2100102cedc3SLeonid Yegoshin 			}
2101102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 1;
2102102cedc3SLeonid Yegoshin 		} else {
2103102cedc3SLeonid Yegoshin 			if ((get_user(dec_insn.insn,
2104102cedc3SLeonid Yegoshin 			    (mips_instruction __user *) xcp->cp0_epc)) ||
2105102cedc3SLeonid Yegoshin 			    (get_user(dec_insn.next_insn,
2106102cedc3SLeonid Yegoshin 			    (mips_instruction __user *)(xcp->cp0_epc+4)))) {
2107102cedc3SLeonid Yegoshin 				MIPS_FPU_EMU_INC_STATS(errors);
2108102cedc3SLeonid Yegoshin 				return SIGBUS;
2109102cedc3SLeonid Yegoshin 			}
2110102cedc3SLeonid Yegoshin 			dec_insn.pc_inc = 4;
2111102cedc3SLeonid Yegoshin 			dec_insn.next_pc_inc = 4;
2112102cedc3SLeonid Yegoshin 			dec_insn.micro_mips_mode = 0;
2113102cedc3SLeonid Yegoshin 		}
2114102cedc3SLeonid Yegoshin 
2115102cedc3SLeonid Yegoshin 		if ((dec_insn.insn == 0) ||
2116102cedc3SLeonid Yegoshin 		   ((dec_insn.pc_inc == 2) &&
2117102cedc3SLeonid Yegoshin 		   ((dec_insn.insn & 0xffff) == MM_NOP16)))
2118102cedc3SLeonid Yegoshin 			xcp->cp0_epc += dec_insn.pc_inc;	/* Skip NOPs */
21191da177e4SLinus Torvalds 		else {
2120cd21dfcfSRalf Baechle 			/*
2121cd21dfcfSRalf Baechle 			 * The 'ieee754_csr' is an alias of
2122cd21dfcfSRalf Baechle 			 * ctx->fcr31.	No need to copy ctx->fcr31 to
2123cd21dfcfSRalf Baechle 			 * ieee754_csr.	 But ieee754_csr.rm is ieee
2124cd21dfcfSRalf Baechle 			 * library modes. (not mips rounding mode)
2125cd21dfcfSRalf Baechle 			 */
2126cd21dfcfSRalf Baechle 			/* convert to ieee library modes */
2127cd21dfcfSRalf Baechle 			ieee754_csr.rm = ieee_rm[ieee754_csr.rm];
2128102cedc3SLeonid Yegoshin 			sig = cop1Emulate(xcp, ctx, dec_insn, fault_addr);
2129cd21dfcfSRalf Baechle 			/* revert to mips rounding mode */
2130cd21dfcfSRalf Baechle 			ieee754_csr.rm = mips_rm[ieee754_csr.rm];
21311da177e4SLinus Torvalds 		}
21321da177e4SLinus Torvalds 
2133e04582b7SAtsushi Nemoto 		if (has_fpu)
21341da177e4SLinus Torvalds 			break;
21351da177e4SLinus Torvalds 		if (sig)
21361da177e4SLinus Torvalds 			break;
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds 		cond_resched();
21391da177e4SLinus Torvalds 	} while (xcp->cp0_epc > prevepc);
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds 	/* SIGILL indicates a non-fpu instruction */
21421da177e4SLinus Torvalds 	if (sig == SIGILL && xcp->cp0_epc != oldepc)
21431da177e4SLinus Torvalds 		/* but if epc has advanced, then ignore it */
21441da177e4SLinus Torvalds 		sig = 0;
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 	return sig;
21471da177e4SLinus Torvalds }
214883fd38caSAtsushi Nemoto 
214983fd38caSAtsushi Nemoto #ifdef CONFIG_DEBUG_FS
2150b6ee75edSDavid Daney 
2151b6ee75edSDavid Daney static int fpuemu_stat_get(void *data, u64 *val)
2152b6ee75edSDavid Daney {
2153b6ee75edSDavid Daney 	int cpu;
2154b6ee75edSDavid Daney 	unsigned long sum = 0;
2155b6ee75edSDavid Daney 	for_each_online_cpu(cpu) {
2156b6ee75edSDavid Daney 		struct mips_fpu_emulator_stats *ps;
2157b6ee75edSDavid Daney 		local_t *pv;
2158b6ee75edSDavid Daney 		ps = &per_cpu(fpuemustats, cpu);
2159b6ee75edSDavid Daney 		pv = (void *)ps + (unsigned long)data;
2160b6ee75edSDavid Daney 		sum += local_read(pv);
2161b6ee75edSDavid Daney 	}
2162b6ee75edSDavid Daney 	*val = sum;
2163b6ee75edSDavid Daney 	return 0;
2164b6ee75edSDavid Daney }
2165b6ee75edSDavid Daney DEFINE_SIMPLE_ATTRIBUTE(fops_fpuemu_stat, fpuemu_stat_get, NULL, "%llu\n");
2166b6ee75edSDavid Daney 
216783fd38caSAtsushi Nemoto extern struct dentry *mips_debugfs_dir;
216883fd38caSAtsushi Nemoto static int __init debugfs_fpuemu(void)
216983fd38caSAtsushi Nemoto {
217083fd38caSAtsushi Nemoto 	struct dentry *d, *dir;
217183fd38caSAtsushi Nemoto 
217283fd38caSAtsushi Nemoto 	if (!mips_debugfs_dir)
217383fd38caSAtsushi Nemoto 		return -ENODEV;
217483fd38caSAtsushi Nemoto 	dir = debugfs_create_dir("fpuemustats", mips_debugfs_dir);
2175ecab1f44SZhaolei 	if (!dir)
2176ecab1f44SZhaolei 		return -ENOMEM;
2177b6ee75edSDavid Daney 
2178b6ee75edSDavid Daney #define FPU_STAT_CREATE(M)						\
2179b6ee75edSDavid Daney 	do {								\
2180b6ee75edSDavid Daney 		d = debugfs_create_file(#M , S_IRUGO, dir,		\
2181b6ee75edSDavid Daney 			(void *)offsetof(struct mips_fpu_emulator_stats, M), \
2182b6ee75edSDavid Daney 			&fops_fpuemu_stat);				\
2183b6ee75edSDavid Daney 		if (!d)							\
2184b6ee75edSDavid Daney 			return -ENOMEM;					\
2185b6ee75edSDavid Daney 	} while (0)
2186b6ee75edSDavid Daney 
2187b6ee75edSDavid Daney 	FPU_STAT_CREATE(emulated);
2188b6ee75edSDavid Daney 	FPU_STAT_CREATE(loads);
2189b6ee75edSDavid Daney 	FPU_STAT_CREATE(stores);
2190b6ee75edSDavid Daney 	FPU_STAT_CREATE(cp1ops);
2191b6ee75edSDavid Daney 	FPU_STAT_CREATE(cp1xops);
2192b6ee75edSDavid Daney 	FPU_STAT_CREATE(errors);
2193b6ee75edSDavid Daney 
219483fd38caSAtsushi Nemoto 	return 0;
219583fd38caSAtsushi Nemoto }
219683fd38caSAtsushi Nemoto __initcall(debugfs_fpuemu);
219783fd38caSAtsushi Nemoto #endif
2198