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