xref: /freebsd/sys/x86/include/fpu.h (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
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  * 3. 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 
33 /*
34  * Floating Point Data Structures and Constants
35  * W. Jolitz 1/90
36  */
37 
38 #ifndef _X86_FPU_H_
39 #define	_X86_FPU_H_
40 
41 /* Environment information of floating point unit. */
42 struct env87 {
43 	int32_t		en_cw;		/* control word (16bits) */
44 	int32_t		en_sw;		/* status word (16bits) */
45 	int32_t		en_tw;		/* tag word (16bits) */
46 	int32_t		en_fip;		/* fp instruction pointer */
47 	uint16_t	en_fcs;		/* fp code segment selector */
48 	uint16_t	en_opcode;	/* opcode last executed (11 bits) */
49 	int32_t		en_foo;		/* fp operand offset */
50 	int32_t		en_fos;		/* fp operand segment selector */
51 };
52 
53 /* Contents of each x87 floating point accumulator. */
54 struct fpacc87 {
55 	uint8_t		fp_bytes[10];
56 };
57 
58 /* Floating point context. (i386 fnsave/frstor) */
59 struct save87 {
60 	struct env87	sv_env;		/* floating point control/status */
61 	struct fpacc87	sv_ac[8];	/* accumulator contents, 0-7 */
62 	uint8_t		sv_pad0[4];	/* saved status word (now unused) */
63 	uint8_t		sv_pad[64];
64 };
65 
66 /* Contents of each SSE extended accumulator. */
67 struct xmmacc {
68 	uint8_t		xmm_bytes[16];
69 };
70 
71 /* Contents of the upper 16 bytes of each AVX extended accumulator. */
72 struct ymmacc {
73 	uint8_t		ymm_bytes[16];
74 };
75 
76 /* Rename structs below depending on machine architecture. */
77 #ifdef __i386__
78 #define	__envxmm32	envxmm
79 #else
80 #define	__envxmm32	envxmm32
81 #define	__envxmm64	envxmm
82 #endif
83 
84 struct __envxmm32 {
85 	uint16_t	en_cw;		/* control word (16bits) */
86 	uint16_t	en_sw;		/* status word (16bits) */
87 	uint16_t	en_tw;		/* tag word (16bits) */
88 	uint16_t	en_opcode;	/* opcode last executed (11 bits) */
89 	uint32_t	en_fip;		/* fp instruction pointer */
90 	uint16_t	en_fcs;		/* fp code segment selector */
91 	uint16_t	en_pad0;	/* padding */
92 	uint32_t	en_foo;		/* fp operand offset */
93 	uint16_t	en_fos;		/* fp operand segment selector */
94 	uint16_t	en_pad1;	/* padding */
95 	uint32_t	en_mxcsr;	/* SSE control/status register */
96 	uint32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
97 };
98 
99 struct __envxmm64 {
100 	uint16_t	en_cw;		/* control word (16bits) */
101 	uint16_t	en_sw;		/* status word (16bits) */
102 	uint8_t		en_tw;		/* tag word (8bits) */
103 	uint8_t		en_zero;
104 	uint16_t	en_opcode;	/* opcode last executed (11 bits ) */
105 	uint64_t	en_rip;		/* fp instruction pointer */
106 	uint64_t	en_rdp;		/* fp operand pointer */
107 	uint32_t	en_mxcsr;	/* SSE control/status register */
108 	uint32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
109 };
110 
111 /* Floating point context. (i386 fxsave/fxrstor) */
112 struct savexmm {
113 	struct __envxmm32	sv_env;
114 	struct {
115 		struct fpacc87	fp_acc;
116 		uint8_t		fp_pad[6];      /* padding */
117 	} sv_fp[8];
118 	struct xmmacc		sv_xmm[8];
119 	uint8_t			sv_pad[224];
120 } __aligned(16);
121 
122 #ifdef __i386__
123 union savefpu {
124 	struct save87	sv_87;
125 	struct savexmm	sv_xmm;
126 };
127 #else
128 /* Floating point context. (amd64 fxsave/fxrstor) */
129 struct savefpu {
130 	struct __envxmm64	sv_env;
131 	struct {
132 		struct fpacc87	fp_acc;
133 		uint8_t		fp_pad[6];	/* padding */
134 	} sv_fp[8];
135 	struct xmmacc		sv_xmm[16];
136 	uint8_t			sv_pad[96];
137 } __aligned(16);
138 #endif
139 
140 struct xstate_hdr {
141 	uint64_t	xstate_bv;
142 	uint64_t	xstate_xcomp_bv;
143 	uint8_t		xstate_rsrv0[8];
144 	uint8_t		xstate_rsrv[40];
145 };
146 #define	XSTATE_XCOMP_BV_COMPACT	(1ULL << 63)
147 
148 struct savexmm_xstate {
149 	struct xstate_hdr	sx_hd;
150 	struct ymmacc		sx_ymm[16];
151 };
152 
153 struct savexmm_ymm {
154 	struct __envxmm32	sv_env;
155 	struct {
156 		struct fpacc87	fp_acc;
157 		int8_t		fp_pad[6];	/* padding */
158 	} sv_fp[8];
159 	struct xmmacc		sv_xmm[16];
160 	uint8_t			sv_pad[96];
161 	struct savexmm_xstate	sv_xstate;
162 } __aligned(64);
163 
164 struct savefpu_xstate {
165 	struct xstate_hdr	sx_hd;
166 	struct ymmacc		sx_ymm[16];
167 };
168 
169 struct savefpu_ymm {
170 	struct __envxmm64	sv_env;
171 	struct {
172 		struct fpacc87	fp_acc;
173 		int8_t		fp_pad[6];	/* padding */
174 	} sv_fp[8];
175 	struct xmmacc		sv_xmm[16];
176 	uint8_t			sv_pad[96];
177 	struct savefpu_xstate	sv_xstate;
178 } __aligned(64);
179 
180 #undef __envxmm32
181 #undef __envxmm64
182 
183 /*
184  * The hardware default control word for i387's and later coprocessors is
185  * 0x37F, giving:
186  *
187  *	round to nearest
188  *	64-bit precision
189  *	all exceptions masked.
190  *
191  * FreeBSD/i386 uses 53 bit precision for things like fadd/fsub/fsqrt etc
192  * because of the difference between memory and fpu register stack arguments.
193  * If its using an intermediate fpu register, it has 80/64 bits to work
194  * with.  If it uses memory, it has 64/53 bits to work with.  However,
195  * gcc is aware of this and goes to a fair bit of trouble to make the
196  * best use of it.
197  *
198  * This is mostly academic for AMD64, because the ABI prefers the use
199  * SSE2 based math.  For FreeBSD/amd64, we go with the default settings.
200  */
201 #define	__INITIAL_FPUCW__	0x037F
202 #define	__INITIAL_FPUCW_I386__	0x127F
203 #define	__INITIAL_NPXCW__	__INITIAL_FPUCW_I386__
204 #define	__INITIAL_MXCSR__	0x1F80
205 #define	__INITIAL_MXCSR_MASK__	0xFFBF
206 
207 /*
208  * The current value of %xcr0 is saved in the sv_pad[] field of the FPU
209  * state in the NT_X86_XSTATE note in core dumps.  This offset is chosen
210  * to match the offset used by NT_X86_XSTATE in other systems.
211  */
212 #define	X86_XSTATE_XCR0_OFFSET	464
213 
214 #ifdef _KERNEL
215 /*
216  * CR0_MP and CR0_EM are always set.  Use CR0_TS to force traps when
217  * FPU access is disabled.
218  */
219 #define	fpu_enable()	clts()
220 #define	fpu_disable()	load_cr0(rcr0() | CR0_TS)
221 #endif
222 
223 #endif /* !_X86_FPU_H_ */
224