xref: /OK3568_Linux_fs/kernel/arch/sparc/math-emu/math_32.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * arch/sparc/math-emu/math.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 1998 Peter Maydell (pmaydell@chiark.greenend.org.uk)
6*4882a593Smuzhiyun  * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
7*4882a593Smuzhiyun  * Copyright (C) 1999 David S. Miller (davem@redhat.com)
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This is a good place to start if you're trying to understand the
10*4882a593Smuzhiyun  * emulation code, because it's pretty simple. What we do is
11*4882a593Smuzhiyun  * essentially analyse the instruction to work out what the operation
12*4882a593Smuzhiyun  * is and which registers are involved. We then execute the appropriate
13*4882a593Smuzhiyun  * FXXXX function. [The floating point queue introduces a minor wrinkle;
14*4882a593Smuzhiyun  * see below...]
15*4882a593Smuzhiyun  * The fxxxxx.c files each emulate a single insn. They look relatively
16*4882a593Smuzhiyun  * simple because the complexity is hidden away in an unholy tangle
17*4882a593Smuzhiyun  * of preprocessor macros.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * The first layer of macros is single.h, double.h, quad.h. Generally
20*4882a593Smuzhiyun  * these files define macros for working with floating point numbers
21*4882a593Smuzhiyun  * of the three IEEE formats. FP_ADD_D(R,A,B) is for adding doubles,
22*4882a593Smuzhiyun  * for instance. These macros are usually defined as calls to more
23*4882a593Smuzhiyun  * generic macros (in this case _FP_ADD(D,2,R,X,Y) where the number
24*4882a593Smuzhiyun  * of machine words required to store the given IEEE format is passed
25*4882a593Smuzhiyun  * as a parameter. [double.h and co check the number of bits in a word
26*4882a593Smuzhiyun  * and define FP_ADD_D & co appropriately].
27*4882a593Smuzhiyun  * The generic macros are defined in op-common.h. This is where all
28*4882a593Smuzhiyun  * the grotty stuff like handling NaNs is coded. To handle the possible
29*4882a593Smuzhiyun  * word sizes macros in op-common.h use macros like _FP_FRAC_SLL_##wc()
30*4882a593Smuzhiyun  * where wc is the 'number of machine words' parameter (here 2).
31*4882a593Smuzhiyun  * These are defined in the third layer of macros: op-1.h, op-2.h
32*4882a593Smuzhiyun  * and op-4.h. These handle operations on floating point numbers composed
33*4882a593Smuzhiyun  * of 1,2 and 4 machine words respectively. [For example, on sparc64
34*4882a593Smuzhiyun  * doubles are one machine word so macros in double.h eventually use
35*4882a593Smuzhiyun  * constructs in op-1.h, but on sparc32 they use op-2.h definitions.]
36*4882a593Smuzhiyun  * soft-fp.h is on the same level as op-common.h, and defines some
37*4882a593Smuzhiyun  * macros which are independent of both word size and FP format.
38*4882a593Smuzhiyun  * Finally, sfp-machine.h is the machine dependent part of the
39*4882a593Smuzhiyun  * code: it defines the word size and what type a word is. It also
40*4882a593Smuzhiyun  * defines how _FP_MUL_MEAT_t() maps to _FP_MUL_MEAT_n_* : op-n.h
41*4882a593Smuzhiyun  * provide several possible flavours of multiply algorithm, most
42*4882a593Smuzhiyun  * of which require that you supply some form of asm or C primitive to
43*4882a593Smuzhiyun  * do the actual multiply. (such asm primitives should be defined
44*4882a593Smuzhiyun  * in sfp-machine.h too). udivmodti4.c is the same sort of thing.
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * There may be some errors here because I'm working from a
47*4882a593Smuzhiyun  * SPARC architecture manual V9, and what I really want is V8...
48*4882a593Smuzhiyun  * Also, the insns which can generate exceptions seem to be a
49*4882a593Smuzhiyun  * greater subset of the FPops than for V9 (for example, FCMPED
50*4882a593Smuzhiyun  * has to be emulated on V8). So I think I'm going to have
51*4882a593Smuzhiyun  * to emulate them all just to be on the safe side...
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * Emulation routines originate from soft-fp package, which is
54*4882a593Smuzhiyun  * part of glibc and has appropriate copyrights in it (allegedly).
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * NB: on sparc int == long == 4 bytes, long long == 8 bytes.
57*4882a593Smuzhiyun  * Most bits of the kernel seem to go for long rather than int,
58*4882a593Smuzhiyun  * so we follow that practice...
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /* TODO:
62*4882a593Smuzhiyun  * fpsave() saves the FP queue but fpload() doesn't reload it.
63*4882a593Smuzhiyun  * Therefore when we context switch or change FPU ownership
64*4882a593Smuzhiyun  * we have to check to see if the queue had anything in it and
65*4882a593Smuzhiyun  * emulate it if it did. This is going to be a pain.
66*4882a593Smuzhiyun  */
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun #include <linux/types.h>
69*4882a593Smuzhiyun #include <linux/sched.h>
70*4882a593Smuzhiyun #include <linux/mm.h>
71*4882a593Smuzhiyun #include <linux/perf_event.h>
72*4882a593Smuzhiyun #include <linux/uaccess.h>
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun #include "sfp-util_32.h"
75*4882a593Smuzhiyun #include <math-emu/soft-fp.h>
76*4882a593Smuzhiyun #include <math-emu/single.h>
77*4882a593Smuzhiyun #include <math-emu/double.h>
78*4882a593Smuzhiyun #include <math-emu/quad.h>
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun #define FLOATFUNC(x) extern int x(void *,void *,void *)
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /* The Vn labels indicate what version of the SPARC architecture gas thinks
83*4882a593Smuzhiyun  * each insn is. This is from the binutils source :->
84*4882a593Smuzhiyun  */
85*4882a593Smuzhiyun /* quadword instructions */
86*4882a593Smuzhiyun #define FSQRTQ	0x02b		/* v8 */
87*4882a593Smuzhiyun #define FADDQ	0x043		/* v8 */
88*4882a593Smuzhiyun #define FSUBQ	0x047		/* v8 */
89*4882a593Smuzhiyun #define FMULQ	0x04b		/* v8 */
90*4882a593Smuzhiyun #define FDIVQ	0x04f		/* v8 */
91*4882a593Smuzhiyun #define FDMULQ	0x06e		/* v8 */
92*4882a593Smuzhiyun #define FQTOS	0x0c7		/* v8 */
93*4882a593Smuzhiyun #define FQTOD	0x0cb		/* v8 */
94*4882a593Smuzhiyun #define FITOQ	0x0cc		/* v8 */
95*4882a593Smuzhiyun #define FSTOQ	0x0cd		/* v8 */
96*4882a593Smuzhiyun #define FDTOQ	0x0ce		/* v8 */
97*4882a593Smuzhiyun #define FQTOI	0x0d3		/* v8 */
98*4882a593Smuzhiyun #define FCMPQ	0x053		/* v8 */
99*4882a593Smuzhiyun #define FCMPEQ	0x057		/* v8 */
100*4882a593Smuzhiyun /* single/double instructions (subnormal): should all work */
101*4882a593Smuzhiyun #define FSQRTS	0x029		/* v7 */
102*4882a593Smuzhiyun #define FSQRTD	0x02a		/* v7 */
103*4882a593Smuzhiyun #define FADDS	0x041		/* v6 */
104*4882a593Smuzhiyun #define FADDD	0x042		/* v6 */
105*4882a593Smuzhiyun #define FSUBS	0x045		/* v6 */
106*4882a593Smuzhiyun #define FSUBD	0x046		/* v6 */
107*4882a593Smuzhiyun #define FMULS	0x049		/* v6 */
108*4882a593Smuzhiyun #define FMULD	0x04a		/* v6 */
109*4882a593Smuzhiyun #define FDIVS	0x04d		/* v6 */
110*4882a593Smuzhiyun #define FDIVD	0x04e		/* v6 */
111*4882a593Smuzhiyun #define FSMULD	0x069		/* v6 */
112*4882a593Smuzhiyun #define FDTOS	0x0c6		/* v6 */
113*4882a593Smuzhiyun #define FSTOD	0x0c9		/* v6 */
114*4882a593Smuzhiyun #define FSTOI	0x0d1		/* v6 */
115*4882a593Smuzhiyun #define FDTOI	0x0d2		/* v6 */
116*4882a593Smuzhiyun #define FABSS	0x009		/* v6 */
117*4882a593Smuzhiyun #define FCMPS	0x051		/* v6 */
118*4882a593Smuzhiyun #define FCMPES	0x055		/* v6 */
119*4882a593Smuzhiyun #define FCMPD	0x052		/* v6 */
120*4882a593Smuzhiyun #define FCMPED	0x056		/* v6 */
121*4882a593Smuzhiyun #define FMOVS	0x001		/* v6 */
122*4882a593Smuzhiyun #define FNEGS	0x005		/* v6 */
123*4882a593Smuzhiyun #define FITOS	0x0c4		/* v6 */
124*4882a593Smuzhiyun #define FITOD	0x0c8		/* v6 */
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun #define FSR_TEM_SHIFT	23UL
127*4882a593Smuzhiyun #define FSR_TEM_MASK	(0x1fUL << FSR_TEM_SHIFT)
128*4882a593Smuzhiyun #define FSR_AEXC_SHIFT	5UL
129*4882a593Smuzhiyun #define FSR_AEXC_MASK	(0x1fUL << FSR_AEXC_SHIFT)
130*4882a593Smuzhiyun #define FSR_CEXC_SHIFT	0UL
131*4882a593Smuzhiyun #define FSR_CEXC_MASK	(0x1fUL << FSR_CEXC_SHIFT)
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun static int do_one_mathemu(u32 insn, unsigned long *fsr, unsigned long *fregs);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun /* Unlike the Sparc64 version (which has a struct fpustate), we
136*4882a593Smuzhiyun  * pass the taskstruct corresponding to the task which currently owns the
137*4882a593Smuzhiyun  * FPU. This is partly because we don't have the fpustate struct and
138*4882a593Smuzhiyun  * partly because the task owning the FPU isn't always current (as is
139*4882a593Smuzhiyun  * the case for the Sparc64 port). This is probably SMP-related...
140*4882a593Smuzhiyun  * This function returns 1 if all queued insns were emulated successfully.
141*4882a593Smuzhiyun  * The test for unimplemented FPop in kernel mode has been moved into
142*4882a593Smuzhiyun  * kernel/traps.c for simplicity.
143*4882a593Smuzhiyun  */
do_mathemu(struct pt_regs * regs,struct task_struct * fpt)144*4882a593Smuzhiyun int do_mathemu(struct pt_regs *regs, struct task_struct *fpt)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	/* regs->pc isn't necessarily the PC at which the offending insn is sitting.
147*4882a593Smuzhiyun 	 * The FPU maintains a queue of FPops which cause traps.
148*4882a593Smuzhiyun 	 * When it hits an instruction that requires that the trapped op succeeded
149*4882a593Smuzhiyun 	 * (usually because it reads a reg. that the trapped op wrote) then it
150*4882a593Smuzhiyun 	 * causes this exception. We need to emulate all the insns on the queue
151*4882a593Smuzhiyun 	 * and then allow the op to proceed.
152*4882a593Smuzhiyun 	 * This code should also handle the case where the trap was precise,
153*4882a593Smuzhiyun 	 * in which case the queue length is zero and regs->pc points at the
154*4882a593Smuzhiyun 	 * single FPop to be emulated. (this case is untested, though :->)
155*4882a593Smuzhiyun 	 * You'll need this case if you want to be able to emulate all FPops
156*4882a593Smuzhiyun 	 * because the FPU either doesn't exist or has been software-disabled.
157*4882a593Smuzhiyun 	 * [The UltraSPARC makes FP a precise trap; this isn't as stupid as it
158*4882a593Smuzhiyun 	 * might sound because the Ultra does funky things with a superscalar
159*4882a593Smuzhiyun 	 * architecture.]
160*4882a593Smuzhiyun 	 */
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	/* You wouldn't believe how often I typed 'ftp' when I meant 'fpt' :-> */
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	int i;
165*4882a593Smuzhiyun 	int retcode = 0;                               /* assume all succeed */
166*4882a593Smuzhiyun 	unsigned long insn;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun #ifdef DEBUG_MATHEMU
171*4882a593Smuzhiyun 	printk("In do_mathemu()... pc is %08lx\n", regs->pc);
172*4882a593Smuzhiyun 	printk("fpqdepth is %ld\n", fpt->thread.fpqdepth);
173*4882a593Smuzhiyun 	for (i = 0; i < fpt->thread.fpqdepth; i++)
174*4882a593Smuzhiyun 		printk("%d: %08lx at %08lx\n", i, fpt->thread.fpqueue[i].insn,
175*4882a593Smuzhiyun 		       (unsigned long)fpt->thread.fpqueue[i].insn_addr);
176*4882a593Smuzhiyun #endif
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	if (fpt->thread.fpqdepth == 0) {                   /* no queue, guilty insn is at regs->pc */
179*4882a593Smuzhiyun #ifdef DEBUG_MATHEMU
180*4882a593Smuzhiyun 		printk("precise trap at %08lx\n", regs->pc);
181*4882a593Smuzhiyun #endif
182*4882a593Smuzhiyun 		if (!get_user(insn, (u32 __user *) regs->pc)) {
183*4882a593Smuzhiyun 			retcode = do_one_mathemu(insn, &fpt->thread.fsr, fpt->thread.float_regs);
184*4882a593Smuzhiyun 			if (retcode) {
185*4882a593Smuzhiyun 				/* in this case we need to fix up PC & nPC */
186*4882a593Smuzhiyun 				regs->pc = regs->npc;
187*4882a593Smuzhiyun 				regs->npc += 4;
188*4882a593Smuzhiyun 			}
189*4882a593Smuzhiyun 		}
190*4882a593Smuzhiyun 		return retcode;
191*4882a593Smuzhiyun 	}
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	/* Normal case: need to empty the queue... */
194*4882a593Smuzhiyun 	for (i = 0; i < fpt->thread.fpqdepth; i++) {
195*4882a593Smuzhiyun 		retcode = do_one_mathemu(fpt->thread.fpqueue[i].insn, &(fpt->thread.fsr), fpt->thread.float_regs);
196*4882a593Smuzhiyun 		if (!retcode)                               /* insn failed, no point doing any more */
197*4882a593Smuzhiyun 			break;
198*4882a593Smuzhiyun 	}
199*4882a593Smuzhiyun 	/* Now empty the queue and clear the queue_not_empty flag */
200*4882a593Smuzhiyun 	if (retcode)
201*4882a593Smuzhiyun 		fpt->thread.fsr &= ~(0x3000 | FSR_CEXC_MASK);
202*4882a593Smuzhiyun 	else
203*4882a593Smuzhiyun 		fpt->thread.fsr &= ~0x3000;
204*4882a593Smuzhiyun 	fpt->thread.fpqdepth = 0;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	return retcode;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun /* All routines returning an exception to raise should detect
210*4882a593Smuzhiyun  * such exceptions _before_ rounding to be consistent with
211*4882a593Smuzhiyun  * the behavior of the hardware in the implemented cases
212*4882a593Smuzhiyun  * (and thus with the recommendations in the V9 architecture
213*4882a593Smuzhiyun  * manual).
214*4882a593Smuzhiyun  *
215*4882a593Smuzhiyun  * We return 0 if a SIGFPE should be sent, 1 otherwise.
216*4882a593Smuzhiyun  */
record_exception(unsigned long * pfsr,int eflag)217*4882a593Smuzhiyun static inline int record_exception(unsigned long *pfsr, int eflag)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	unsigned long fsr = *pfsr;
220*4882a593Smuzhiyun 	int would_trap;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	/* Determine if this exception would have generated a trap. */
223*4882a593Smuzhiyun 	would_trap = (fsr & ((long)eflag << FSR_TEM_SHIFT)) != 0UL;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/* If trapping, we only want to signal one bit. */
226*4882a593Smuzhiyun 	if (would_trap != 0) {
227*4882a593Smuzhiyun 		eflag &= ((fsr & FSR_TEM_MASK) >> FSR_TEM_SHIFT);
228*4882a593Smuzhiyun 		if ((eflag & (eflag - 1)) != 0) {
229*4882a593Smuzhiyun 			if (eflag & FP_EX_INVALID)
230*4882a593Smuzhiyun 				eflag = FP_EX_INVALID;
231*4882a593Smuzhiyun 			else if (eflag & FP_EX_OVERFLOW)
232*4882a593Smuzhiyun 				eflag = FP_EX_OVERFLOW;
233*4882a593Smuzhiyun 			else if (eflag & FP_EX_UNDERFLOW)
234*4882a593Smuzhiyun 				eflag = FP_EX_UNDERFLOW;
235*4882a593Smuzhiyun 			else if (eflag & FP_EX_DIVZERO)
236*4882a593Smuzhiyun 				eflag = FP_EX_DIVZERO;
237*4882a593Smuzhiyun 			else if (eflag & FP_EX_INEXACT)
238*4882a593Smuzhiyun 				eflag = FP_EX_INEXACT;
239*4882a593Smuzhiyun 		}
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	/* Set CEXC, here is the rule:
243*4882a593Smuzhiyun 	 *
244*4882a593Smuzhiyun 	 *    In general all FPU ops will set one and only one
245*4882a593Smuzhiyun 	 *    bit in the CEXC field, this is always the case
246*4882a593Smuzhiyun 	 *    when the IEEE exception trap is enabled in TEM.
247*4882a593Smuzhiyun 	 */
248*4882a593Smuzhiyun 	fsr &= ~(FSR_CEXC_MASK);
249*4882a593Smuzhiyun 	fsr |= ((long)eflag << FSR_CEXC_SHIFT);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	/* Set the AEXC field, rule is:
252*4882a593Smuzhiyun 	 *
253*4882a593Smuzhiyun 	 *    If a trap would not be generated, the
254*4882a593Smuzhiyun 	 *    CEXC just generated is OR'd into the
255*4882a593Smuzhiyun 	 *    existing value of AEXC.
256*4882a593Smuzhiyun 	 */
257*4882a593Smuzhiyun 	if (would_trap == 0)
258*4882a593Smuzhiyun 		fsr |= ((long)eflag << FSR_AEXC_SHIFT);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/* If trapping, indicate fault trap type IEEE. */
261*4882a593Smuzhiyun 	if (would_trap != 0)
262*4882a593Smuzhiyun 		fsr |= (1UL << 14);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	*pfsr = fsr;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	return (would_trap ? 0 : 1);
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun typedef union {
270*4882a593Smuzhiyun 	u32 s;
271*4882a593Smuzhiyun 	u64 d;
272*4882a593Smuzhiyun 	u64 q[2];
273*4882a593Smuzhiyun } *argp;
274*4882a593Smuzhiyun 
do_one_mathemu(u32 insn,unsigned long * pfsr,unsigned long * fregs)275*4882a593Smuzhiyun static int do_one_mathemu(u32 insn, unsigned long *pfsr, unsigned long *fregs)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	/* Emulate the given insn, updating fsr and fregs appropriately. */
278*4882a593Smuzhiyun 	int type = 0;
279*4882a593Smuzhiyun 	/* r is rd, b is rs2 and a is rs1. The *u arg tells
280*4882a593Smuzhiyun 	   whether the argument should be packed/unpacked (0 - do not unpack/pack, 1 - unpack/pack)
281*4882a593Smuzhiyun 	   non-u args tells the size of the argument (0 - no argument, 1 - single, 2 - double, 3 - quad */
282*4882a593Smuzhiyun #define TYPE(dummy, r, ru, b, bu, a, au) type = (au << 2) | (a << 0) | (bu << 5) | (b << 3) | (ru << 8) | (r << 6)
283*4882a593Smuzhiyun 	int freg;
284*4882a593Smuzhiyun 	argp rs1 = NULL, rs2 = NULL, rd = NULL;
285*4882a593Smuzhiyun 	FP_DECL_EX;
286*4882a593Smuzhiyun 	FP_DECL_S(SA); FP_DECL_S(SB); FP_DECL_S(SR);
287*4882a593Smuzhiyun 	FP_DECL_D(DA); FP_DECL_D(DB); FP_DECL_D(DR);
288*4882a593Smuzhiyun 	FP_DECL_Q(QA); FP_DECL_Q(QB); FP_DECL_Q(QR);
289*4882a593Smuzhiyun 	int IR;
290*4882a593Smuzhiyun 	long fsr;
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun #ifdef DEBUG_MATHEMU
293*4882a593Smuzhiyun 	printk("In do_mathemu(), emulating %08lx\n", insn);
294*4882a593Smuzhiyun #endif
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	if ((insn & 0xc1f80000) == 0x81a00000)	/* FPOP1 */ {
297*4882a593Smuzhiyun 		switch ((insn >> 5) & 0x1ff) {
298*4882a593Smuzhiyun 		case FSQRTQ: TYPE(3,3,1,3,1,0,0); break;
299*4882a593Smuzhiyun 		case FADDQ:
300*4882a593Smuzhiyun 		case FSUBQ:
301*4882a593Smuzhiyun 		case FMULQ:
302*4882a593Smuzhiyun 		case FDIVQ: TYPE(3,3,1,3,1,3,1); break;
303*4882a593Smuzhiyun 		case FDMULQ: TYPE(3,3,1,2,1,2,1); break;
304*4882a593Smuzhiyun 		case FQTOS: TYPE(3,1,1,3,1,0,0); break;
305*4882a593Smuzhiyun 		case FQTOD: TYPE(3,2,1,3,1,0,0); break;
306*4882a593Smuzhiyun 		case FITOQ: TYPE(3,3,1,1,0,0,0); break;
307*4882a593Smuzhiyun 		case FSTOQ: TYPE(3,3,1,1,1,0,0); break;
308*4882a593Smuzhiyun 		case FDTOQ: TYPE(3,3,1,2,1,0,0); break;
309*4882a593Smuzhiyun 		case FQTOI: TYPE(3,1,0,3,1,0,0); break;
310*4882a593Smuzhiyun 		case FSQRTS: TYPE(2,1,1,1,1,0,0); break;
311*4882a593Smuzhiyun 		case FSQRTD: TYPE(2,2,1,2,1,0,0); break;
312*4882a593Smuzhiyun 		case FADDD:
313*4882a593Smuzhiyun 		case FSUBD:
314*4882a593Smuzhiyun 		case FMULD:
315*4882a593Smuzhiyun 		case FDIVD: TYPE(2,2,1,2,1,2,1); break;
316*4882a593Smuzhiyun 		case FADDS:
317*4882a593Smuzhiyun 		case FSUBS:
318*4882a593Smuzhiyun 		case FMULS:
319*4882a593Smuzhiyun 		case FDIVS: TYPE(2,1,1,1,1,1,1); break;
320*4882a593Smuzhiyun 		case FSMULD: TYPE(2,2,1,1,1,1,1); break;
321*4882a593Smuzhiyun 		case FDTOS: TYPE(2,1,1,2,1,0,0); break;
322*4882a593Smuzhiyun 		case FSTOD: TYPE(2,2,1,1,1,0,0); break;
323*4882a593Smuzhiyun 		case FSTOI: TYPE(2,1,0,1,1,0,0); break;
324*4882a593Smuzhiyun 		case FDTOI: TYPE(2,1,0,2,1,0,0); break;
325*4882a593Smuzhiyun 		case FITOS: TYPE(2,1,1,1,0,0,0); break;
326*4882a593Smuzhiyun 		case FITOD: TYPE(2,2,1,1,0,0,0); break;
327*4882a593Smuzhiyun 		case FMOVS:
328*4882a593Smuzhiyun 		case FABSS:
329*4882a593Smuzhiyun 		case FNEGS: TYPE(2,1,0,1,0,0,0); break;
330*4882a593Smuzhiyun 		}
331*4882a593Smuzhiyun 	} else if ((insn & 0xc1f80000) == 0x81a80000)	/* FPOP2 */ {
332*4882a593Smuzhiyun 		switch ((insn >> 5) & 0x1ff) {
333*4882a593Smuzhiyun 		case FCMPS: TYPE(3,0,0,1,1,1,1); break;
334*4882a593Smuzhiyun 		case FCMPES: TYPE(3,0,0,1,1,1,1); break;
335*4882a593Smuzhiyun 		case FCMPD: TYPE(3,0,0,2,1,2,1); break;
336*4882a593Smuzhiyun 		case FCMPED: TYPE(3,0,0,2,1,2,1); break;
337*4882a593Smuzhiyun 		case FCMPQ: TYPE(3,0,0,3,1,3,1); break;
338*4882a593Smuzhiyun 		case FCMPEQ: TYPE(3,0,0,3,1,3,1); break;
339*4882a593Smuzhiyun 		}
340*4882a593Smuzhiyun 	}
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	if (!type) {	/* oops, didn't recognise that FPop */
343*4882a593Smuzhiyun #ifdef DEBUG_MATHEMU
344*4882a593Smuzhiyun 		printk("attempt to emulate unrecognised FPop!\n");
345*4882a593Smuzhiyun #endif
346*4882a593Smuzhiyun 		return 0;
347*4882a593Smuzhiyun 	}
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	/* Decode the registers to be used */
350*4882a593Smuzhiyun 	freg = (*pfsr >> 14) & 0xf;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	*pfsr &= ~0x1c000;				/* clear the traptype bits */
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	freg = ((insn >> 14) & 0x1f);
355*4882a593Smuzhiyun 	switch (type & 0x3) {				/* is rs1 single, double or quad? */
356*4882a593Smuzhiyun 	case 3:
357*4882a593Smuzhiyun 		if (freg & 3) {				/* quadwords must have bits 4&5 of the */
358*4882a593Smuzhiyun 							/* encoded reg. number set to zero. */
359*4882a593Smuzhiyun 			*pfsr |= (6 << 14);
360*4882a593Smuzhiyun 			return 0;			/* simulate invalid_fp_register exception */
361*4882a593Smuzhiyun 		}
362*4882a593Smuzhiyun 		fallthrough;
363*4882a593Smuzhiyun 	case 2:
364*4882a593Smuzhiyun 		if (freg & 1) {				/* doublewords must have bit 5 zeroed */
365*4882a593Smuzhiyun 			*pfsr |= (6 << 14);
366*4882a593Smuzhiyun 			return 0;
367*4882a593Smuzhiyun 		}
368*4882a593Smuzhiyun 	}
369*4882a593Smuzhiyun 	rs1 = (argp)&fregs[freg];
370*4882a593Smuzhiyun 	switch (type & 0x7) {
371*4882a593Smuzhiyun 	case 7: FP_UNPACK_QP (QA, rs1); break;
372*4882a593Smuzhiyun 	case 6: FP_UNPACK_DP (DA, rs1); break;
373*4882a593Smuzhiyun 	case 5: FP_UNPACK_SP (SA, rs1); break;
374*4882a593Smuzhiyun 	}
375*4882a593Smuzhiyun 	freg = (insn & 0x1f);
376*4882a593Smuzhiyun 	switch ((type >> 3) & 0x3) {			/* same again for rs2 */
377*4882a593Smuzhiyun 	case 3:
378*4882a593Smuzhiyun 		if (freg & 3) {				/* quadwords must have bits 4&5 of the */
379*4882a593Smuzhiyun 							/* encoded reg. number set to zero. */
380*4882a593Smuzhiyun 			*pfsr |= (6 << 14);
381*4882a593Smuzhiyun 			return 0;			/* simulate invalid_fp_register exception */
382*4882a593Smuzhiyun 		}
383*4882a593Smuzhiyun 		fallthrough;
384*4882a593Smuzhiyun 	case 2:
385*4882a593Smuzhiyun 		if (freg & 1) {				/* doublewords must have bit 5 zeroed */
386*4882a593Smuzhiyun 			*pfsr |= (6 << 14);
387*4882a593Smuzhiyun 			return 0;
388*4882a593Smuzhiyun 		}
389*4882a593Smuzhiyun 	}
390*4882a593Smuzhiyun 	rs2 = (argp)&fregs[freg];
391*4882a593Smuzhiyun 	switch ((type >> 3) & 0x7) {
392*4882a593Smuzhiyun 	case 7: FP_UNPACK_QP (QB, rs2); break;
393*4882a593Smuzhiyun 	case 6: FP_UNPACK_DP (DB, rs2); break;
394*4882a593Smuzhiyun 	case 5: FP_UNPACK_SP (SB, rs2); break;
395*4882a593Smuzhiyun 	}
396*4882a593Smuzhiyun 	freg = ((insn >> 25) & 0x1f);
397*4882a593Smuzhiyun 	switch ((type >> 6) & 0x3) {			/* and finally rd. This one's a bit different */
398*4882a593Smuzhiyun 	case 0:						/* dest is fcc. (this must be FCMPQ or FCMPEQ) */
399*4882a593Smuzhiyun 		if (freg) {				/* V8 has only one set of condition codes, so */
400*4882a593Smuzhiyun 							/* anything but 0 in the rd field is an error */
401*4882a593Smuzhiyun 			*pfsr |= (6 << 14);		/* (should probably flag as invalid opcode */
402*4882a593Smuzhiyun 			return 0;			/* but SIGFPE will do :-> ) */
403*4882a593Smuzhiyun 		}
404*4882a593Smuzhiyun 		break;
405*4882a593Smuzhiyun 	case 3:
406*4882a593Smuzhiyun 		if (freg & 3) {				/* quadwords must have bits 4&5 of the */
407*4882a593Smuzhiyun 							/* encoded reg. number set to zero. */
408*4882a593Smuzhiyun 			*pfsr |= (6 << 14);
409*4882a593Smuzhiyun 			return 0;			/* simulate invalid_fp_register exception */
410*4882a593Smuzhiyun 		}
411*4882a593Smuzhiyun 		fallthrough;
412*4882a593Smuzhiyun 	case 2:
413*4882a593Smuzhiyun 		if (freg & 1) {				/* doublewords must have bit 5 zeroed */
414*4882a593Smuzhiyun 			*pfsr |= (6 << 14);
415*4882a593Smuzhiyun 			return 0;
416*4882a593Smuzhiyun 		}
417*4882a593Smuzhiyun 		fallthrough;
418*4882a593Smuzhiyun 	case 1:
419*4882a593Smuzhiyun 		rd = (void *)&fregs[freg];
420*4882a593Smuzhiyun 		break;
421*4882a593Smuzhiyun 	}
422*4882a593Smuzhiyun #ifdef DEBUG_MATHEMU
423*4882a593Smuzhiyun 	printk("executing insn...\n");
424*4882a593Smuzhiyun #endif
425*4882a593Smuzhiyun 	/* do the Right Thing */
426*4882a593Smuzhiyun 	switch ((insn >> 5) & 0x1ff) {
427*4882a593Smuzhiyun 	/* + */
428*4882a593Smuzhiyun 	case FADDS: FP_ADD_S (SR, SA, SB); break;
429*4882a593Smuzhiyun 	case FADDD: FP_ADD_D (DR, DA, DB); break;
430*4882a593Smuzhiyun 	case FADDQ: FP_ADD_Q (QR, QA, QB); break;
431*4882a593Smuzhiyun 	/* - */
432*4882a593Smuzhiyun 	case FSUBS: FP_SUB_S (SR, SA, SB); break;
433*4882a593Smuzhiyun 	case FSUBD: FP_SUB_D (DR, DA, DB); break;
434*4882a593Smuzhiyun 	case FSUBQ: FP_SUB_Q (QR, QA, QB); break;
435*4882a593Smuzhiyun 	/* * */
436*4882a593Smuzhiyun 	case FMULS: FP_MUL_S (SR, SA, SB); break;
437*4882a593Smuzhiyun 	case FSMULD: FP_CONV (D, S, 2, 1, DA, SA);
438*4882a593Smuzhiyun 		     FP_CONV (D, S, 2, 1, DB, SB);
439*4882a593Smuzhiyun 	case FMULD: FP_MUL_D (DR, DA, DB); break;
440*4882a593Smuzhiyun 	case FDMULQ: FP_CONV (Q, D, 4, 2, QA, DA);
441*4882a593Smuzhiyun 		     FP_CONV (Q, D, 4, 2, QB, DB);
442*4882a593Smuzhiyun 	case FMULQ: FP_MUL_Q (QR, QA, QB); break;
443*4882a593Smuzhiyun 	/* / */
444*4882a593Smuzhiyun 	case FDIVS: FP_DIV_S (SR, SA, SB); break;
445*4882a593Smuzhiyun 	case FDIVD: FP_DIV_D (DR, DA, DB); break;
446*4882a593Smuzhiyun 	case FDIVQ: FP_DIV_Q (QR, QA, QB); break;
447*4882a593Smuzhiyun 	/* sqrt */
448*4882a593Smuzhiyun 	case FSQRTS: FP_SQRT_S (SR, SB); break;
449*4882a593Smuzhiyun 	case FSQRTD: FP_SQRT_D (DR, DB); break;
450*4882a593Smuzhiyun 	case FSQRTQ: FP_SQRT_Q (QR, QB); break;
451*4882a593Smuzhiyun 	/* mov */
452*4882a593Smuzhiyun 	case FMOVS: rd->s = rs2->s; break;
453*4882a593Smuzhiyun 	case FABSS: rd->s = rs2->s & 0x7fffffff; break;
454*4882a593Smuzhiyun 	case FNEGS: rd->s = rs2->s ^ 0x80000000; break;
455*4882a593Smuzhiyun 	/* float to int */
456*4882a593Smuzhiyun 	case FSTOI: FP_TO_INT_S (IR, SB, 32, 1); break;
457*4882a593Smuzhiyun 	case FDTOI: FP_TO_INT_D (IR, DB, 32, 1); break;
458*4882a593Smuzhiyun 	case FQTOI: FP_TO_INT_Q (IR, QB, 32, 1); break;
459*4882a593Smuzhiyun 	/* int to float */
460*4882a593Smuzhiyun 	case FITOS: IR = rs2->s; FP_FROM_INT_S (SR, IR, 32, int); break;
461*4882a593Smuzhiyun 	case FITOD: IR = rs2->s; FP_FROM_INT_D (DR, IR, 32, int); break;
462*4882a593Smuzhiyun 	case FITOQ: IR = rs2->s; FP_FROM_INT_Q (QR, IR, 32, int); break;
463*4882a593Smuzhiyun 	/* float to float */
464*4882a593Smuzhiyun 	case FSTOD: FP_CONV (D, S, 2, 1, DR, SB); break;
465*4882a593Smuzhiyun 	case FSTOQ: FP_CONV (Q, S, 4, 1, QR, SB); break;
466*4882a593Smuzhiyun 	case FDTOQ: FP_CONV (Q, D, 4, 2, QR, DB); break;
467*4882a593Smuzhiyun 	case FDTOS: FP_CONV (S, D, 1, 2, SR, DB); break;
468*4882a593Smuzhiyun 	case FQTOS: FP_CONV (S, Q, 1, 4, SR, QB); break;
469*4882a593Smuzhiyun 	case FQTOD: FP_CONV (D, Q, 2, 4, DR, QB); break;
470*4882a593Smuzhiyun 	/* comparison */
471*4882a593Smuzhiyun 	case FCMPS:
472*4882a593Smuzhiyun 	case FCMPES:
473*4882a593Smuzhiyun 		FP_CMP_S(IR, SB, SA, 3);
474*4882a593Smuzhiyun 		if (IR == 3 &&
475*4882a593Smuzhiyun 		    (((insn >> 5) & 0x1ff) == FCMPES ||
476*4882a593Smuzhiyun 		     FP_ISSIGNAN_S(SA) ||
477*4882a593Smuzhiyun 		     FP_ISSIGNAN_S(SB)))
478*4882a593Smuzhiyun 			FP_SET_EXCEPTION (FP_EX_INVALID);
479*4882a593Smuzhiyun 		break;
480*4882a593Smuzhiyun 	case FCMPD:
481*4882a593Smuzhiyun 	case FCMPED:
482*4882a593Smuzhiyun 		FP_CMP_D(IR, DB, DA, 3);
483*4882a593Smuzhiyun 		if (IR == 3 &&
484*4882a593Smuzhiyun 		    (((insn >> 5) & 0x1ff) == FCMPED ||
485*4882a593Smuzhiyun 		     FP_ISSIGNAN_D(DA) ||
486*4882a593Smuzhiyun 		     FP_ISSIGNAN_D(DB)))
487*4882a593Smuzhiyun 			FP_SET_EXCEPTION (FP_EX_INVALID);
488*4882a593Smuzhiyun 		break;
489*4882a593Smuzhiyun 	case FCMPQ:
490*4882a593Smuzhiyun 	case FCMPEQ:
491*4882a593Smuzhiyun 		FP_CMP_Q(IR, QB, QA, 3);
492*4882a593Smuzhiyun 		if (IR == 3 &&
493*4882a593Smuzhiyun 		    (((insn >> 5) & 0x1ff) == FCMPEQ ||
494*4882a593Smuzhiyun 		     FP_ISSIGNAN_Q(QA) ||
495*4882a593Smuzhiyun 		     FP_ISSIGNAN_Q(QB)))
496*4882a593Smuzhiyun 			FP_SET_EXCEPTION (FP_EX_INVALID);
497*4882a593Smuzhiyun 	}
498*4882a593Smuzhiyun 	if (!FP_INHIBIT_RESULTS) {
499*4882a593Smuzhiyun 		switch ((type >> 6) & 0x7) {
500*4882a593Smuzhiyun 		case 0: fsr = *pfsr;
501*4882a593Smuzhiyun 			if (IR == -1) IR = 2;
502*4882a593Smuzhiyun 			/* fcc is always fcc0 */
503*4882a593Smuzhiyun 			fsr &= ~0xc00; fsr |= (IR << 10);
504*4882a593Smuzhiyun 			*pfsr = fsr;
505*4882a593Smuzhiyun 			break;
506*4882a593Smuzhiyun 		case 1: rd->s = IR; break;
507*4882a593Smuzhiyun 		case 5: FP_PACK_SP (rd, SR); break;
508*4882a593Smuzhiyun 		case 6: FP_PACK_DP (rd, DR); break;
509*4882a593Smuzhiyun 		case 7: FP_PACK_QP (rd, QR); break;
510*4882a593Smuzhiyun 		}
511*4882a593Smuzhiyun 	}
512*4882a593Smuzhiyun 	if (_fex == 0)
513*4882a593Smuzhiyun 		return 1;				/* success! */
514*4882a593Smuzhiyun 	return record_exception(pfsr, _fex);
515*4882a593Smuzhiyun }
516