xref: /freebsd/sys/x86/include/fpu.h (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * William Jolitz.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	from: @(#)npx.h	5.3 (Berkeley) 1/18/91
33  * $FreeBSD$
34  */
35 
36 /*
37  * Floating Point Data Structures and Constants
38  * W. Jolitz 1/90
39  */
40 
41 #ifndef _X86_FPU_H_
42 #define	_X86_FPU_H_
43 
44 /* Environment information of floating point unit. */
45 struct env87 {
46 	int32_t		en_cw;		/* control word (16bits) */
47 	int32_t		en_sw;		/* status word (16bits) */
48 	int32_t		en_tw;		/* tag word (16bits) */
49 	int32_t		en_fip;		/* fp instruction pointer */
50 	uint16_t	en_fcs;		/* fp code segment selector */
51 	uint16_t	en_opcode;	/* opcode last executed (11 bits) */
52 	int32_t		en_foo;		/* fp operand offset */
53 	int32_t		en_fos;		/* fp operand segment selector */
54 };
55 
56 /* Contents of each x87 floating point accumulator. */
57 struct fpacc87 {
58 	uint8_t		fp_bytes[10];
59 };
60 
61 /* Floating point context. (i386 fnsave/frstor) */
62 struct save87 {
63 	struct env87	sv_env;		/* floating point control/status */
64 	struct fpacc87	sv_ac[8];	/* accumulator contents, 0-7 */
65 	uint8_t		sv_pad0[4];	/* saved status word (now unused) */
66 	/*
67 	 * Bogus padding for emulators.  Emulators should use their own
68 	 * struct and arrange to store into this struct (ending here)
69 	 * before it is inspected for ptracing or for core dumps.  Some
70 	 * emulators overwrite the whole struct.  We have no good way of
71 	 * knowing how much padding to leave.  Leave just enough for the
72 	 * GPL emulator's i387_union (176 bytes total).
73 	 */
74 	uint8_t		sv_pad[64];	/* padding; used by emulators */
75 };
76 
77 /* Contents of each SSE extended accumulator. */
78 struct xmmacc {
79 	uint8_t		xmm_bytes[16];
80 };
81 
82 /* Contents of the upper 16 bytes of each AVX extended accumulator. */
83 struct ymmacc {
84 	uint8_t		ymm_bytes[16];
85 };
86 
87 /* Rename structs below depending on machine architecture. */
88 #ifdef __i386__
89 #define	__envxmm32	envxmm
90 #else
91 #define	__envxmm32	envxmm32
92 #define	__envxmm64	envxmm
93 #endif
94 
95 struct __envxmm32 {
96 	uint16_t	en_cw;		/* control word (16bits) */
97 	uint16_t	en_sw;		/* status word (16bits) */
98 	uint16_t	en_tw;		/* tag word (16bits) */
99 	uint16_t	en_opcode;	/* opcode last executed (11 bits) */
100 	uint32_t	en_fip;		/* fp instruction pointer */
101 	uint16_t	en_fcs;		/* fp code segment selector */
102 	uint16_t	en_pad0;	/* padding */
103 	uint32_t	en_foo;		/* fp operand offset */
104 	uint16_t	en_fos;		/* fp operand segment selector */
105 	uint16_t	en_pad1;	/* padding */
106 	uint32_t	en_mxcsr;	/* SSE control/status register */
107 	uint32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
108 };
109 
110 struct __envxmm64 {
111 	uint16_t	en_cw;		/* control word (16bits) */
112 	uint16_t	en_sw;		/* status word (16bits) */
113 	uint8_t		en_tw;		/* tag word (8bits) */
114 	uint8_t		en_zero;
115 	uint16_t	en_opcode;	/* opcode last executed (11 bits ) */
116 	uint64_t	en_rip;		/* fp instruction pointer */
117 	uint64_t	en_rdp;		/* fp operand pointer */
118 	uint32_t	en_mxcsr;	/* SSE control/status register */
119 	uint32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
120 };
121 
122 /* Floating point context. (i386 fxsave/fxrstor) */
123 struct savexmm {
124 	struct __envxmm32	sv_env;
125 	struct {
126 		struct fpacc87	fp_acc;
127 		uint8_t		fp_pad[6];      /* padding */
128 	} sv_fp[8];
129 	struct xmmacc		sv_xmm[8];
130 	uint8_t			sv_pad[224];
131 } __aligned(16);
132 
133 #ifdef __i386__
134 union savefpu {
135 	struct save87	sv_87;
136 	struct savexmm	sv_xmm;
137 };
138 #else
139 /* Floating point context. (amd64 fxsave/fxrstor) */
140 struct savefpu {
141 	struct __envxmm64	sv_env;
142 	struct {
143 		struct fpacc87	fp_acc;
144 		uint8_t		fp_pad[6];	/* padding */
145 	} sv_fp[8];
146 	struct xmmacc		sv_xmm[16];
147 	uint8_t			sv_pad[96];
148 } __aligned(16);
149 #endif
150 
151 struct xstate_hdr {
152 	uint64_t	xstate_bv;
153 	uint64_t	xstate_xcomp_bv;
154 	uint8_t		xstate_rsrv0[8];
155 	uint8_t		xstate_rsrv[40];
156 };
157 #define	XSTATE_XCOMP_BV_COMPACT	(1ULL << 63)
158 
159 struct savexmm_xstate {
160 	struct xstate_hdr	sx_hd;
161 	struct ymmacc		sx_ymm[16];
162 };
163 
164 struct savexmm_ymm {
165 	struct __envxmm32	sv_env;
166 	struct {
167 		struct fpacc87	fp_acc;
168 		int8_t		fp_pad[6];	/* padding */
169 	} sv_fp[8];
170 	struct xmmacc		sv_xmm[16];
171 	uint8_t			sv_pad[96];
172 	struct savexmm_xstate	sv_xstate;
173 } __aligned(64);
174 
175 struct savefpu_xstate {
176 	struct xstate_hdr	sx_hd;
177 	struct ymmacc		sx_ymm[16];
178 };
179 
180 struct savefpu_ymm {
181 	struct __envxmm64	sv_env;
182 	struct {
183 		struct fpacc87	fp_acc;
184 		int8_t		fp_pad[6];	/* padding */
185 	} sv_fp[8];
186 	struct xmmacc		sv_xmm[16];
187 	uint8_t			sv_pad[96];
188 	struct savefpu_xstate	sv_xstate;
189 } __aligned(64);
190 
191 #undef __envxmm32
192 #undef __envxmm64
193 
194 /*
195  * The hardware default control word for i387's and later coprocessors is
196  * 0x37F, giving:
197  *
198  *	round to nearest
199  *	64-bit precision
200  *	all exceptions masked.
201  *
202  * FreeBSD/i386 uses 53 bit precision for things like fadd/fsub/fsqrt etc
203  * because of the difference between memory and fpu register stack arguments.
204  * If its using an intermediate fpu register, it has 80/64 bits to work
205  * with.  If it uses memory, it has 64/53 bits to work with.  However,
206  * gcc is aware of this and goes to a fair bit of trouble to make the
207  * best use of it.
208  *
209  * This is mostly academic for AMD64, because the ABI prefers the use
210  * SSE2 based math.  For FreeBSD/amd64, we go with the default settings.
211  */
212 #define	__INITIAL_FPUCW__	0x037F
213 #define	__INITIAL_FPUCW_I386__	0x127F
214 #define	__INITIAL_NPXCW__	__INITIAL_FPUCW_I386__
215 #define	__INITIAL_MXCSR__	0x1F80
216 #define	__INITIAL_MXCSR_MASK__	0xFFBF
217 
218 #endif /* !_X86_FPU_H_ */
219