xref: /openbmc/linux/arch/x86/math-emu/fpu_emu.h (revision 0898782247ae533d1f4e47a06bc5d4870931b284)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2da957e11SThomas Gleixner /*---------------------------------------------------------------------------+
3da957e11SThomas Gleixner  |  fpu_emu.h                                                                |
4da957e11SThomas Gleixner  |                                                                           |
5da957e11SThomas Gleixner  | Copyright (C) 1992,1993,1994,1997                                         |
6da957e11SThomas Gleixner  |                       W. Metzenthen, 22 Parker St, Ormond, Vic 3163,      |
7da957e11SThomas Gleixner  |                       Australia.  E-mail   billm@suburbia.net             |
8da957e11SThomas Gleixner  |                                                                           |
9da957e11SThomas Gleixner  +---------------------------------------------------------------------------*/
10da957e11SThomas Gleixner 
11da957e11SThomas Gleixner #ifndef _FPU_EMU_H_
12da957e11SThomas Gleixner #define _FPU_EMU_H_
13da957e11SThomas Gleixner 
14da957e11SThomas Gleixner /*
15da957e11SThomas Gleixner  * Define PECULIAR_486 to get a closer approximation to 80486 behaviour,
16da957e11SThomas Gleixner  * rather than behaviour which appears to be cleaner.
17da957e11SThomas Gleixner  * This is a matter of opinion: for all I know, the 80486 may simply
18da957e11SThomas Gleixner  * be complying with the IEEE spec. Maybe one day I'll get to see the
19da957e11SThomas Gleixner  * spec...
20da957e11SThomas Gleixner  */
21da957e11SThomas Gleixner #define PECULIAR_486
22da957e11SThomas Gleixner 
23da957e11SThomas Gleixner #ifdef __ASSEMBLY__
24da957e11SThomas Gleixner #include "fpu_asm.h"
25da957e11SThomas Gleixner #define	Const(x)	$##x
26da957e11SThomas Gleixner #else
27da957e11SThomas Gleixner #define	Const(x)	x
28da957e11SThomas Gleixner #endif
29da957e11SThomas Gleixner 
30da957e11SThomas Gleixner #define EXP_BIAS	Const(0)
31da957e11SThomas Gleixner #define EXP_OVER	Const(0x4000)	/* smallest invalid large exponent */
32da957e11SThomas Gleixner #define	EXP_UNDER	Const(-0x3fff)	/* largest invalid small exponent */
33da957e11SThomas Gleixner #define EXP_WAY_UNDER   Const(-0x6000)	/* Below the smallest denormal, but
34da957e11SThomas Gleixner 					   still a 16 bit nr. */
35da957e11SThomas Gleixner #define EXP_Infinity    EXP_OVER
36da957e11SThomas Gleixner #define EXP_NaN         EXP_OVER
37da957e11SThomas Gleixner 
38da957e11SThomas Gleixner #define EXTENDED_Ebias Const(0x3fff)
39da957e11SThomas Gleixner #define EXTENDED_Emin (-0x3ffe)	/* smallest valid exponent */
40da957e11SThomas Gleixner 
41da957e11SThomas Gleixner #define SIGN_POS	Const(0)
42da957e11SThomas Gleixner #define SIGN_NEG	Const(0x80)
43da957e11SThomas Gleixner 
44da957e11SThomas Gleixner #define SIGN_Positive	Const(0)
45da957e11SThomas Gleixner #define SIGN_Negative	Const(0x8000)
46da957e11SThomas Gleixner 
47da957e11SThomas Gleixner /* Keep the order TAG_Valid, TAG_Zero, TW_Denormal */
48da957e11SThomas Gleixner /* The following fold to 2 (Special) in the Tag Word */
49da957e11SThomas Gleixner #define TW_Denormal     Const(4)	/* De-normal */
50da957e11SThomas Gleixner #define TW_Infinity	Const(5)	/* + or - infinity */
51da957e11SThomas Gleixner #define	TW_NaN		Const(6)	/* Not a Number */
52da957e11SThomas Gleixner #define	TW_Unsupported	Const(7)	/* Not supported by an 80486 */
53da957e11SThomas Gleixner 
54da957e11SThomas Gleixner #define TAG_Valid	Const(0)	/* valid */
55da957e11SThomas Gleixner #define TAG_Zero	Const(1)	/* zero */
56da957e11SThomas Gleixner #define TAG_Special	Const(2)	/* De-normal, + or - infinity,
57da957e11SThomas Gleixner 					   or Not a Number */
58da957e11SThomas Gleixner #define TAG_Empty	Const(3)	/* empty */
59da957e11SThomas Gleixner #define TAG_Error	Const(0x80)	/* probably need to abort */
60da957e11SThomas Gleixner 
61da957e11SThomas Gleixner #define LOADED_DATA	Const(10101)	/* Special st() number to identify
62da957e11SThomas Gleixner 					   loaded data (not on stack). */
63da957e11SThomas Gleixner 
64da957e11SThomas Gleixner /* A few flags (must be >= 0x10). */
65da957e11SThomas Gleixner #define REV             0x10
66da957e11SThomas Gleixner #define DEST_RM         0x20
67da957e11SThomas Gleixner #define LOADED          0x40
68da957e11SThomas Gleixner 
69da957e11SThomas Gleixner #define FPU_Exception   Const(0x80000000)	/* Added to tag returns. */
70da957e11SThomas Gleixner 
71da957e11SThomas Gleixner #ifndef __ASSEMBLY__
72da957e11SThomas Gleixner 
73da957e11SThomas Gleixner #include "fpu_system.h"
74da957e11SThomas Gleixner 
75decb4c41SIngo Molnar #include <uapi/asm/sigcontext.h>	/* for struct _fpstate */
76da957e11SThomas Gleixner #include <asm/math_emu.h>
77da957e11SThomas Gleixner #include <linux/linkage.h>
78da957e11SThomas Gleixner 
79da957e11SThomas Gleixner /*
80da957e11SThomas Gleixner #define RE_ENTRANT_CHECKING
81da957e11SThomas Gleixner  */
82da957e11SThomas Gleixner 
83da957e11SThomas Gleixner #ifdef RE_ENTRANT_CHECKING
84da957e11SThomas Gleixner extern u_char emulating;
85da957e11SThomas Gleixner #  define RE_ENTRANT_CHECK_OFF emulating = 0
86da957e11SThomas Gleixner #  define RE_ENTRANT_CHECK_ON emulating = 1
87da957e11SThomas Gleixner #else
88da957e11SThomas Gleixner #  define RE_ENTRANT_CHECK_OFF
89da957e11SThomas Gleixner #  define RE_ENTRANT_CHECK_ON
90da957e11SThomas Gleixner #endif /* RE_ENTRANT_CHECKING */
91da957e11SThomas Gleixner 
92da957e11SThomas Gleixner #define FWAIT_OPCODE 0x9b
93da957e11SThomas Gleixner #define OP_SIZE_PREFIX 0x66
94da957e11SThomas Gleixner #define ADDR_SIZE_PREFIX 0x67
95da957e11SThomas Gleixner #define PREFIX_CS 0x2e
96da957e11SThomas Gleixner #define PREFIX_DS 0x3e
97da957e11SThomas Gleixner #define PREFIX_ES 0x26
98da957e11SThomas Gleixner #define PREFIX_SS 0x36
99da957e11SThomas Gleixner #define PREFIX_FS 0x64
100da957e11SThomas Gleixner #define PREFIX_GS 0x65
101da957e11SThomas Gleixner #define PREFIX_REPE 0xf3
102da957e11SThomas Gleixner #define PREFIX_REPNE 0xf2
103da957e11SThomas Gleixner #define PREFIX_LOCK 0xf0
104da957e11SThomas Gleixner #define PREFIX_CS_ 1
105da957e11SThomas Gleixner #define PREFIX_DS_ 2
106da957e11SThomas Gleixner #define PREFIX_ES_ 3
107da957e11SThomas Gleixner #define PREFIX_FS_ 4
108da957e11SThomas Gleixner #define PREFIX_GS_ 5
109da957e11SThomas Gleixner #define PREFIX_SS_ 6
110da957e11SThomas Gleixner #define PREFIX_DEFAULT 7
111da957e11SThomas Gleixner 
112da957e11SThomas Gleixner struct address {
113da957e11SThomas Gleixner 	unsigned int offset;
114da957e11SThomas Gleixner 	unsigned int selector:16;
115da957e11SThomas Gleixner 	unsigned int opcode:11;
116da957e11SThomas Gleixner 	unsigned int empty:5;
117da957e11SThomas Gleixner };
118da957e11SThomas Gleixner struct fpu__reg {
119da957e11SThomas Gleixner 	unsigned sigl;
120da957e11SThomas Gleixner 	unsigned sigh;
121da957e11SThomas Gleixner 	short exp;
122da957e11SThomas Gleixner };
123da957e11SThomas Gleixner 
124da957e11SThomas Gleixner typedef void (*FUNC) (void);
125da957e11SThomas Gleixner typedef struct fpu__reg FPU_REG;
126da957e11SThomas Gleixner typedef void (*FUNC_ST0) (FPU_REG *st0_ptr, u_char st0_tag);
1273d0d14f9SIngo Molnar typedef struct {
1283d0d14f9SIngo Molnar 	u_char address_size, operand_size, segment;
1293d0d14f9SIngo Molnar } overrides;
130da957e11SThomas Gleixner /* This structure is 32 bits: */
1313d0d14f9SIngo Molnar typedef struct {
1323d0d14f9SIngo Molnar 	overrides override;
1333d0d14f9SIngo Molnar 	u_char default_mode;
1343d0d14f9SIngo Molnar } fpu_addr_modes;
135da957e11SThomas Gleixner /* PROTECTED has a restricted meaning in the emulator; it is used
136da957e11SThomas Gleixner    to signal that the emulator needs to do special things to ensure
137da957e11SThomas Gleixner    that protection is respected in a segmented model. */
138da957e11SThomas Gleixner #define PROTECTED 4
139da957e11SThomas Gleixner #define SIXTEEN   1		/* We rely upon this being 1 (true) */
140da957e11SThomas Gleixner #define VM86      SIXTEEN
141da957e11SThomas Gleixner #define PM16      (SIXTEEN | PROTECTED)
142da957e11SThomas Gleixner #define SEG32     PROTECTED
143da957e11SThomas Gleixner extern u_char const data_sizes_16[32];
144da957e11SThomas Gleixner 
145da957e11SThomas Gleixner #define register_base ((u_char *) registers )
146da957e11SThomas Gleixner #define fpu_register(x)  ( * ((FPU_REG *)( register_base + 10 * (x & 7) )) )
147da957e11SThomas Gleixner #define	st(x)      ( * ((FPU_REG *)( register_base + 10 * ((top+x) & 7) )) )
148da957e11SThomas Gleixner 
149da957e11SThomas Gleixner #define	STACK_OVERFLOW	(FPU_stackoverflow(&st_new_ptr))
150da957e11SThomas Gleixner #define	NOT_EMPTY(i)	(!FPU_empty_i(i))
151da957e11SThomas Gleixner 
152da957e11SThomas Gleixner #define	NOT_EMPTY_ST0	(st0_tag ^ TAG_Empty)
153da957e11SThomas Gleixner 
154da957e11SThomas Gleixner #define poppop() { FPU_pop(); FPU_pop(); }
155da957e11SThomas Gleixner 
156da957e11SThomas Gleixner /* push() does not affect the tags */
157da957e11SThomas Gleixner #define push()	{ top--; }
158da957e11SThomas Gleixner 
159da957e11SThomas Gleixner #define signbyte(a) (((u_char *)(a))[9])
160da957e11SThomas Gleixner #define getsign(a) (signbyte(a) & 0x80)
1615623452aSArnd Bergmann #define setsign(a,b) { if ((b) != 0) signbyte(a) |= 0x80; else signbyte(a) &= 0x7f; }
162da957e11SThomas Gleixner #define copysign(a,b) { if (getsign(a)) signbyte(b) |= 0x80; \
163da957e11SThomas Gleixner                         else signbyte(b) &= 0x7f; }
164da957e11SThomas Gleixner #define changesign(a) { signbyte(a) ^= 0x80; }
165da957e11SThomas Gleixner #define setpositive(a) { signbyte(a) &= 0x7f; }
166da957e11SThomas Gleixner #define setnegative(a) { signbyte(a) |= 0x80; }
167da957e11SThomas Gleixner #define signpositive(a) ( (signbyte(a) & 0x80) == 0 )
168da957e11SThomas Gleixner #define signnegative(a) (signbyte(a) & 0x80)
169da957e11SThomas Gleixner 
reg_copy(FPU_REG const * x,FPU_REG * y)170da957e11SThomas Gleixner static inline void reg_copy(FPU_REG const *x, FPU_REG *y)
171da957e11SThomas Gleixner {
172da957e11SThomas Gleixner 	*(short *)&(y->exp) = *(const short *)&(x->exp);
173da957e11SThomas Gleixner 	*(long long *)&(y->sigl) = *(const long long *)&(x->sigl);
174da957e11SThomas Gleixner }
175da957e11SThomas Gleixner 
176da957e11SThomas Gleixner #define exponent(x)  (((*(short *)&((x)->exp)) & 0x7fff) - EXTENDED_Ebias)
177da957e11SThomas Gleixner #define setexponentpos(x,y) { (*(short *)&((x)->exp)) = \
178da957e11SThomas Gleixner   ((y) + EXTENDED_Ebias) & 0x7fff; }
179da957e11SThomas Gleixner #define exponent16(x)         (*(short *)&((x)->exp))
180*29e7e966SArnd Bergmann #define setexponent16(x,y)  { (*(short *)&((x)->exp)) = (u16)(y); }
181da957e11SThomas Gleixner #define addexponent(x,y)    { (*(short *)&((x)->exp)) += (y); }
182da957e11SThomas Gleixner #define stdexp(x)           { (*(short *)&((x)->exp)) += EXTENDED_Ebias; }
183da957e11SThomas Gleixner 
184da957e11SThomas Gleixner #define isdenormal(ptr)   (exponent(ptr) == EXP_BIAS+EXP_UNDER)
185da957e11SThomas Gleixner 
186da957e11SThomas Gleixner #define significand(x) ( ((unsigned long long *)&((x)->sigl))[0] )
187da957e11SThomas Gleixner 
188da957e11SThomas Gleixner /*----- Prototypes for functions written in assembler -----*/
189da957e11SThomas Gleixner /* extern void reg_move(FPU_REG *a, FPU_REG *b); */
190da957e11SThomas Gleixner 
191da957e11SThomas Gleixner asmlinkage int FPU_normalize(FPU_REG *x);
192da957e11SThomas Gleixner asmlinkage int FPU_normalize_nuo(FPU_REG *x);
193da957e11SThomas Gleixner asmlinkage int FPU_u_sub(FPU_REG const *arg1, FPU_REG const *arg2,
194da957e11SThomas Gleixner 			 FPU_REG * answ, unsigned int control_w, u_char sign,
195da957e11SThomas Gleixner 			 int expa, int expb);
196da957e11SThomas Gleixner asmlinkage int FPU_u_mul(FPU_REG const *arg1, FPU_REG const *arg2,
197da957e11SThomas Gleixner 			 FPU_REG * answ, unsigned int control_w, u_char sign,
198da957e11SThomas Gleixner 			 int expon);
199da957e11SThomas Gleixner asmlinkage int FPU_u_div(FPU_REG const *arg1, FPU_REG const *arg2,
200da957e11SThomas Gleixner 			 FPU_REG * answ, unsigned int control_w, u_char sign);
201da957e11SThomas Gleixner asmlinkage int FPU_u_add(FPU_REG const *arg1, FPU_REG const *arg2,
202da957e11SThomas Gleixner 			 FPU_REG * answ, unsigned int control_w, u_char sign,
203da957e11SThomas Gleixner 			 int expa, int expb);
204da957e11SThomas Gleixner asmlinkage int wm_sqrt(FPU_REG *n, int dummy1, int dummy2,
205da957e11SThomas Gleixner 		       unsigned int control_w, u_char sign);
206da957e11SThomas Gleixner asmlinkage unsigned FPU_shrx(void *l, unsigned x);
207da957e11SThomas Gleixner asmlinkage unsigned FPU_shrxs(void *v, unsigned x);
208da957e11SThomas Gleixner asmlinkage unsigned long FPU_div_small(unsigned long long *x, unsigned long y);
209da957e11SThomas Gleixner asmlinkage int FPU_round(FPU_REG *arg, unsigned int extent, int dummy,
210da957e11SThomas Gleixner 			 unsigned int control_w, u_char sign);
211da957e11SThomas Gleixner 
212da957e11SThomas Gleixner #ifndef MAKING_PROTO
213da957e11SThomas Gleixner #include "fpu_proto.h"
214da957e11SThomas Gleixner #endif
215da957e11SThomas Gleixner 
216da957e11SThomas Gleixner #endif /* __ASSEMBLY__ */
217da957e11SThomas Gleixner 
218da957e11SThomas Gleixner #endif /* _FPU_EMU_H_ */
219