1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Linux/PA-RISC Project (http://www.parisc-linux.org/)
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Floating-point emulation code
6*4882a593Smuzhiyun * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun * BEGIN_DESC
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * File:
12*4882a593Smuzhiyun * @(#) pa/fp/decode_exc.c $ Revision: $
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * Purpose:
15*4882a593Smuzhiyun * <<please update with a synopsis of the functionality provided by this file>>
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * External Interfaces:
18*4882a593Smuzhiyun * <<the following list was autogenerated, please review>>
19*4882a593Smuzhiyun * decode_fpu(Fpu_register, trap_counts)
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * Internal Interfaces:
22*4882a593Smuzhiyun * <<please update>>
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Theory:
25*4882a593Smuzhiyun * <<please update with a overview of the operation of this file>>
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * END_DESC
28*4882a593Smuzhiyun */
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #include <linux/kernel.h>
31*4882a593Smuzhiyun #include "float.h"
32*4882a593Smuzhiyun #include "sgl_float.h"
33*4882a593Smuzhiyun #include "dbl_float.h"
34*4882a593Smuzhiyun #include "cnv_float.h"
35*4882a593Smuzhiyun /* #include "types.h" */
36*4882a593Smuzhiyun #include <asm/signal.h>
37*4882a593Smuzhiyun #include <asm/siginfo.h>
38*4882a593Smuzhiyun /* #include <machine/sys/mdep_private.h> */
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #undef Fpustatus_register
41*4882a593Smuzhiyun #define Fpustatus_register Fpu_register[0]
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* General definitions */
44*4882a593Smuzhiyun #define DOESTRAP 1
45*4882a593Smuzhiyun #define NOTRAP 0
46*4882a593Smuzhiyun #define SIGNALCODE(signal, code) ((signal) << 24 | (code))
47*4882a593Smuzhiyun #define copropbit 1<<31-2 /* bit position 2 */
48*4882a593Smuzhiyun #define opclass 9 /* bits 21 & 22 */
49*4882a593Smuzhiyun #define fmt 11 /* bits 19 & 20 */
50*4882a593Smuzhiyun #define df 13 /* bits 17 & 18 */
51*4882a593Smuzhiyun #define twobits 3 /* mask low-order 2 bits */
52*4882a593Smuzhiyun #define fivebits 31 /* mask low-order 5 bits */
53*4882a593Smuzhiyun #define MAX_EXCP_REG 7 /* number of excpeption registers to check */
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /* Exception register definitions */
56*4882a593Smuzhiyun #define Excp_type(index) Exceptiontype(Fpu_register[index])
57*4882a593Smuzhiyun #define Excp_instr(index) Instructionfield(Fpu_register[index])
58*4882a593Smuzhiyun #define Clear_excp_register(index) Allexception(Fpu_register[index]) = 0
59*4882a593Smuzhiyun #define Excp_format() \
60*4882a593Smuzhiyun (current_ir >> ((current_ir>>opclass & twobits)==1 ? df : fmt) & twobits)
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* Miscellaneous definitions */
63*4882a593Smuzhiyun #define Fpu_sgl(index) Fpu_register[index*2]
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun #define Fpu_dblp1(index) Fpu_register[index*2]
66*4882a593Smuzhiyun #define Fpu_dblp2(index) Fpu_register[(index*2)+1]
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun #define Fpu_quadp1(index) Fpu_register[index*2]
69*4882a593Smuzhiyun #define Fpu_quadp2(index) Fpu_register[(index*2)+1]
70*4882a593Smuzhiyun #define Fpu_quadp3(index) Fpu_register[(index*2)+2]
71*4882a593Smuzhiyun #define Fpu_quadp4(index) Fpu_register[(index*2)+3]
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Single precision floating-point definitions */
74*4882a593Smuzhiyun #ifndef Sgl_decrement
75*4882a593Smuzhiyun # define Sgl_decrement(sgl_value) Sall(sgl_value)--
76*4882a593Smuzhiyun #endif
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /* Double precision floating-point definitions */
79*4882a593Smuzhiyun #ifndef Dbl_decrement
80*4882a593Smuzhiyun # define Dbl_decrement(dbl_valuep1,dbl_valuep2) \
81*4882a593Smuzhiyun if ((Dallp2(dbl_valuep2)--) == 0) Dallp1(dbl_valuep1)--
82*4882a593Smuzhiyun #endif
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun #define update_trap_counts(Fpu_register, aflags, bflags, trap_counts) { \
86*4882a593Smuzhiyun aflags=(Fpu_register[0])>>27; /* assumes zero fill. 32 bit */ \
87*4882a593Smuzhiyun Fpu_register[0] |= bflags; \
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun u_int
decode_fpu(unsigned int Fpu_register[],unsigned int trap_counts[])91*4882a593Smuzhiyun decode_fpu(unsigned int Fpu_register[], unsigned int trap_counts[])
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun unsigned int current_ir, excp;
94*4882a593Smuzhiyun int target, exception_index = 1;
95*4882a593Smuzhiyun boolean inexact;
96*4882a593Smuzhiyun unsigned int aflags;
97*4882a593Smuzhiyun unsigned int bflags;
98*4882a593Smuzhiyun unsigned int excptype;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* Keep stats on how many floating point exceptions (based on type)
102*4882a593Smuzhiyun * that happen. Want to keep this overhead low, but still provide
103*4882a593Smuzhiyun * some information to the customer. All exits from this routine
104*4882a593Smuzhiyun * need to restore Fpu_register[0]
105*4882a593Smuzhiyun */
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun bflags=(Fpu_register[0] & 0xf8000000);
108*4882a593Smuzhiyun Fpu_register[0] &= 0x07ffffff;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /* exception_index is used to index the exception register queue. It
111*4882a593Smuzhiyun * always points at the last register that contains a valid exception. A
112*4882a593Smuzhiyun * zero value implies no exceptions (also the initialized value). Setting
113*4882a593Smuzhiyun * the T-bit resets the exception_index to zero.
114*4882a593Smuzhiyun */
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /*
117*4882a593Smuzhiyun * Check for reserved-op exception. A reserved-op exception does not
118*4882a593Smuzhiyun * set any exception registers nor does it set the T-bit. If the T-bit
119*4882a593Smuzhiyun * is not set then a reserved-op exception occurred.
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * At some point, we may want to report reserved op exceptions as
122*4882a593Smuzhiyun * illegal instructions.
123*4882a593Smuzhiyun */
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun if (!Is_tbit_set()) {
126*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
127*4882a593Smuzhiyun return SIGNALCODE(SIGILL, ILL_COPROC);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /*
131*4882a593Smuzhiyun * Is a coprocessor op.
132*4882a593Smuzhiyun *
133*4882a593Smuzhiyun * Now we need to determine what type of exception occurred.
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun for (exception_index=1; exception_index<=MAX_EXCP_REG; exception_index++) {
136*4882a593Smuzhiyun current_ir = Excp_instr(exception_index);
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun * On PA89: there are 5 different unimplemented exception
139*4882a593Smuzhiyun * codes: 0x1, 0x9, 0xb, 0x3, and 0x23. PA-RISC 2.0 adds
140*4882a593Smuzhiyun * another, 0x2b. Only these have the low order bit set.
141*4882a593Smuzhiyun */
142*4882a593Smuzhiyun excptype = Excp_type(exception_index);
143*4882a593Smuzhiyun if (excptype & UNIMPLEMENTEDEXCEPTION) {
144*4882a593Smuzhiyun /*
145*4882a593Smuzhiyun * Clear T-bit and exception register so that
146*4882a593Smuzhiyun * we can tell if a trap really occurs while
147*4882a593Smuzhiyun * emulating the instruction.
148*4882a593Smuzhiyun */
149*4882a593Smuzhiyun Clear_tbit();
150*4882a593Smuzhiyun Clear_excp_register(exception_index);
151*4882a593Smuzhiyun /*
152*4882a593Smuzhiyun * Now emulate this instruction. If a trap occurs,
153*4882a593Smuzhiyun * fpudispatch will return a non-zero number
154*4882a593Smuzhiyun */
155*4882a593Smuzhiyun excp = fpudispatch(current_ir,excptype,0,Fpu_register);
156*4882a593Smuzhiyun /* accumulate the status flags, don't lose them as in hpux */
157*4882a593Smuzhiyun if (excp) {
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun * We now need to make sure that the T-bit and the
160*4882a593Smuzhiyun * exception register contain the correct values
161*4882a593Smuzhiyun * before continuing.
162*4882a593Smuzhiyun */
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun * Set t-bit since it might still be needed for a
165*4882a593Smuzhiyun * subsequent real trap (I don't understand fully -PB)
166*4882a593Smuzhiyun */
167*4882a593Smuzhiyun Set_tbit();
168*4882a593Smuzhiyun /* some of the following code uses
169*4882a593Smuzhiyun * Excp_type(exception_index) so fix that up */
170*4882a593Smuzhiyun Set_exceptiontype_and_instr_field(excp,current_ir,
171*4882a593Smuzhiyun Fpu_register[exception_index]);
172*4882a593Smuzhiyun if (excp == UNIMPLEMENTEDEXCEPTION) {
173*4882a593Smuzhiyun /*
174*4882a593Smuzhiyun * it is really unimplemented, so restore the
175*4882a593Smuzhiyun * TIMEX extended unimplemented exception code
176*4882a593Smuzhiyun */
177*4882a593Smuzhiyun excp = excptype;
178*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags,
179*4882a593Smuzhiyun trap_counts);
180*4882a593Smuzhiyun return SIGNALCODE(SIGILL, ILL_COPROC);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun /* some of the following code uses excptype, so
183*4882a593Smuzhiyun * fix that up too */
184*4882a593Smuzhiyun excptype = excp;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun /* handle exceptions other than the real UNIMPLIMENTED the
187*4882a593Smuzhiyun * same way as if the hardware had caused them */
188*4882a593Smuzhiyun if (excp == NOEXCEPTION)
189*4882a593Smuzhiyun /* For now use 'break', should technically be 'continue' */
190*4882a593Smuzhiyun break;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /*
194*4882a593Smuzhiyun * In PA89, the underflow exception has been extended to encode
195*4882a593Smuzhiyun * additional information. The exception looks like pp01x0,
196*4882a593Smuzhiyun * where x is 1 if inexact and pp represent the inexact bit (I)
197*4882a593Smuzhiyun * and the round away bit (RA)
198*4882a593Smuzhiyun */
199*4882a593Smuzhiyun if (excptype & UNDERFLOWEXCEPTION) {
200*4882a593Smuzhiyun /* check for underflow trap enabled */
201*4882a593Smuzhiyun if (Is_underflowtrap_enabled()) {
202*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags,
203*4882a593Smuzhiyun trap_counts);
204*4882a593Smuzhiyun return SIGNALCODE(SIGFPE, FPE_FLTUND);
205*4882a593Smuzhiyun } else {
206*4882a593Smuzhiyun /*
207*4882a593Smuzhiyun * Isn't a real trap; we need to
208*4882a593Smuzhiyun * return the default value.
209*4882a593Smuzhiyun */
210*4882a593Smuzhiyun target = current_ir & fivebits;
211*4882a593Smuzhiyun #ifndef lint
212*4882a593Smuzhiyun if (Ibit(Fpu_register[exception_index])) inexact = TRUE;
213*4882a593Smuzhiyun else inexact = FALSE;
214*4882a593Smuzhiyun #endif
215*4882a593Smuzhiyun switch (Excp_format()) {
216*4882a593Smuzhiyun case SGL:
217*4882a593Smuzhiyun /*
218*4882a593Smuzhiyun * If ra (round-away) is set, will
219*4882a593Smuzhiyun * want to undo the rounding done
220*4882a593Smuzhiyun * by the hardware.
221*4882a593Smuzhiyun */
222*4882a593Smuzhiyun if (Rabit(Fpu_register[exception_index]))
223*4882a593Smuzhiyun Sgl_decrement(Fpu_sgl(target));
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /* now denormalize */
226*4882a593Smuzhiyun sgl_denormalize(&Fpu_sgl(target),&inexact,Rounding_mode());
227*4882a593Smuzhiyun break;
228*4882a593Smuzhiyun case DBL:
229*4882a593Smuzhiyun /*
230*4882a593Smuzhiyun * If ra (round-away) is set, will
231*4882a593Smuzhiyun * want to undo the rounding done
232*4882a593Smuzhiyun * by the hardware.
233*4882a593Smuzhiyun */
234*4882a593Smuzhiyun if (Rabit(Fpu_register[exception_index]))
235*4882a593Smuzhiyun Dbl_decrement(Fpu_dblp1(target),Fpu_dblp2(target));
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun /* now denormalize */
238*4882a593Smuzhiyun dbl_denormalize(&Fpu_dblp1(target),&Fpu_dblp2(target),
239*4882a593Smuzhiyun &inexact,Rounding_mode());
240*4882a593Smuzhiyun break;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun if (inexact) Set_underflowflag();
243*4882a593Smuzhiyun /*
244*4882a593Smuzhiyun * Underflow can generate an inexact
245*4882a593Smuzhiyun * exception. If inexact trap is enabled,
246*4882a593Smuzhiyun * want to do an inexact trap, otherwise
247*4882a593Smuzhiyun * set inexact flag.
248*4882a593Smuzhiyun */
249*4882a593Smuzhiyun if (inexact && Is_inexacttrap_enabled()) {
250*4882a593Smuzhiyun /*
251*4882a593Smuzhiyun * Set exception field of exception register
252*4882a593Smuzhiyun * to inexact, parm field to zero.
253*4882a593Smuzhiyun * Underflow bit should be cleared.
254*4882a593Smuzhiyun */
255*4882a593Smuzhiyun Set_exceptiontype(Fpu_register[exception_index],
256*4882a593Smuzhiyun INEXACTEXCEPTION);
257*4882a593Smuzhiyun Set_parmfield(Fpu_register[exception_index],0);
258*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags,
259*4882a593Smuzhiyun trap_counts);
260*4882a593Smuzhiyun return SIGNALCODE(SIGFPE, FPE_FLTRES);
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun else {
263*4882a593Smuzhiyun /*
264*4882a593Smuzhiyun * Exception register needs to be cleared.
265*4882a593Smuzhiyun * Inexact flag needs to be set if inexact.
266*4882a593Smuzhiyun */
267*4882a593Smuzhiyun Clear_excp_register(exception_index);
268*4882a593Smuzhiyun if (inexact) Set_inexactflag();
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun continue;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun switch(Excp_type(exception_index)) {
274*4882a593Smuzhiyun case OVERFLOWEXCEPTION:
275*4882a593Smuzhiyun case OVERFLOWEXCEPTION | INEXACTEXCEPTION:
276*4882a593Smuzhiyun /* check for overflow trap enabled */
277*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags,
278*4882a593Smuzhiyun trap_counts);
279*4882a593Smuzhiyun if (Is_overflowtrap_enabled()) {
280*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags,
281*4882a593Smuzhiyun trap_counts);
282*4882a593Smuzhiyun return SIGNALCODE(SIGFPE, FPE_FLTOVF);
283*4882a593Smuzhiyun } else {
284*4882a593Smuzhiyun /*
285*4882a593Smuzhiyun * Isn't a real trap; we need to
286*4882a593Smuzhiyun * return the default value.
287*4882a593Smuzhiyun */
288*4882a593Smuzhiyun target = current_ir & fivebits;
289*4882a593Smuzhiyun switch (Excp_format()) {
290*4882a593Smuzhiyun case SGL:
291*4882a593Smuzhiyun Sgl_setoverflow(Fpu_sgl(target));
292*4882a593Smuzhiyun break;
293*4882a593Smuzhiyun case DBL:
294*4882a593Smuzhiyun Dbl_setoverflow(Fpu_dblp1(target),Fpu_dblp2(target));
295*4882a593Smuzhiyun break;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun Set_overflowflag();
298*4882a593Smuzhiyun /*
299*4882a593Smuzhiyun * Overflow always generates an inexact
300*4882a593Smuzhiyun * exception. If inexact trap is enabled,
301*4882a593Smuzhiyun * want to do an inexact trap, otherwise
302*4882a593Smuzhiyun * set inexact flag.
303*4882a593Smuzhiyun */
304*4882a593Smuzhiyun if (Is_inexacttrap_enabled()) {
305*4882a593Smuzhiyun /*
306*4882a593Smuzhiyun * Set exception field of exception
307*4882a593Smuzhiyun * register to inexact. Overflow
308*4882a593Smuzhiyun * bit should be cleared.
309*4882a593Smuzhiyun */
310*4882a593Smuzhiyun Set_exceptiontype(Fpu_register[exception_index],
311*4882a593Smuzhiyun INEXACTEXCEPTION);
312*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags,
313*4882a593Smuzhiyun trap_counts);
314*4882a593Smuzhiyun return SIGNALCODE(SIGFPE, FPE_FLTRES);
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun else {
317*4882a593Smuzhiyun /*
318*4882a593Smuzhiyun * Exception register needs to be cleared.
319*4882a593Smuzhiyun * Inexact flag needs to be set.
320*4882a593Smuzhiyun */
321*4882a593Smuzhiyun Clear_excp_register(exception_index);
322*4882a593Smuzhiyun Set_inexactflag();
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun break;
326*4882a593Smuzhiyun case INVALIDEXCEPTION:
327*4882a593Smuzhiyun case OPC_2E_INVALIDEXCEPTION:
328*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
329*4882a593Smuzhiyun return SIGNALCODE(SIGFPE, FPE_FLTINV);
330*4882a593Smuzhiyun case DIVISIONBYZEROEXCEPTION:
331*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
332*4882a593Smuzhiyun Clear_excp_register(exception_index);
333*4882a593Smuzhiyun return SIGNALCODE(SIGFPE, FPE_FLTDIV);
334*4882a593Smuzhiyun case INEXACTEXCEPTION:
335*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
336*4882a593Smuzhiyun return SIGNALCODE(SIGFPE, FPE_FLTRES);
337*4882a593Smuzhiyun default:
338*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
339*4882a593Smuzhiyun printk("%s(%d) Unknown FPU exception 0x%x\n", __FILE__,
340*4882a593Smuzhiyun __LINE__, Excp_type(exception_index));
341*4882a593Smuzhiyun return SIGNALCODE(SIGILL, ILL_COPROC);
342*4882a593Smuzhiyun case NOEXCEPTION: /* no exception */
343*4882a593Smuzhiyun /*
344*4882a593Smuzhiyun * Clear exception register in case
345*4882a593Smuzhiyun * other fields are non-zero.
346*4882a593Smuzhiyun */
347*4882a593Smuzhiyun Clear_excp_register(exception_index);
348*4882a593Smuzhiyun break;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun /*
352*4882a593Smuzhiyun * No real exceptions occurred.
353*4882a593Smuzhiyun */
354*4882a593Smuzhiyun Clear_tbit();
355*4882a593Smuzhiyun update_trap_counts(Fpu_register, aflags, bflags, trap_counts);
356*4882a593Smuzhiyun return(NOTRAP);
357*4882a593Smuzhiyun }
358