xref: /dragonfly/sys/cpu/x86_64/include/npx.h (revision 3fe3fa25)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * Copyright (c) 2008 The DragonFly Project.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	from: @(#)npx.h	5.3 (Berkeley) 1/18/91
34  * $FreeBSD: src/sys/i386/include/npx.h,v 1.18.2.1 2001/08/15 01:23:52 peter Exp $
35  */
36 
37 /*
38  * 287/387 NPX Coprocessor Data Structures and Constants
39  * W. Jolitz 1/90
40  */
41 
42 #ifndef _CPU_NPX_H_
43 #define	_CPU_NPX_H_
44 
45 #ifndef _SYS_STDINT_H_
46 #include <sys/stdint.h>
47 #endif
48 
49 /* Environment information of floating point unit */
50 struct	env87 {
51 	int32_t		en_cw;		/* control word (16bits) */
52 	int32_t		en_sw;		/* status word (16bits) */
53 	int32_t		en_tw;		/* tag word (16bits) */
54 	int32_t		en_fip;		/* floating point instruction pointer */
55 	uint16_t	en_fcs;		/* floating code segment selector */
56 	uint16_t	en_opcode;	/* opcode last executed (11 bits ) */
57 	int32_t		en_foo;		/* floating operand offset */
58 	int32_t		en_fos;		/* floating operand segment selector */
59 };
60 
61 /* Contents of each x87 floating point accumulator */
62 struct	fpacc87 {
63 	uint8_t		fp_bytes[10];
64 };
65 
66 /* Floating point context (i386 fnsave/frstor) */
67 struct	save87 {
68 	struct	env87	sv_env;		/* floating point control/status */
69 	struct	fpacc87	sv_ac[8];	/* accumulator contents, 0-7 */
70 	uint8_t		sv_pad0[4];	/* saved status word (now unused) */
71 	/*
72 	 * Bogus padding for emulators.  Emulators should use their own
73 	 * struct and arrange to store into this struct (ending here)
74 	 * before it is inspected for ptracing or for core dumps.  Some
75 	 * emulators overwrite the whole struct.  We have no good way of
76 	 * knowing how much padding to leave.  Leave just enough for the
77 	 * GPL emulator's i387_union (176 bytes total).
78 	 */
79 	uint8_t		sv_pad[64];
80 };
81 
82 struct	envxmm {
83 	uint16_t	en_cw;		/* control word (16bits) */
84 	uint16_t	en_sw;		/* status word (16bits) */
85 	uint16_t	en_tw;		/* tag word (16bits) */
86 	uint16_t	en_opcode;	/* opcode last executed (11 bits) */
87 	uint32_t	en_fip;		/* fp instruction pointer */
88 	uint16_t	en_fcs;		/* fp code segment selector */
89 	uint16_t	en_pad0;	/* padding */
90 	uint32_t	en_foo;		/* fp operand offset */
91 	uint16_t	en_fos;		/* fp operand segment selector */
92 	uint16_t	en_pad1;	/* padding */
93 	uint32_t	en_mxcsr;	/* SSE control/status register */
94 	uint32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
95 };
96 
97 /* Layout for code/data pointers relating to FP exceptions. */
98 union fp_addr {
99 	uint64_t fa_64;			/* linear address for 64-bit system */
100 	struct {
101 		uint32_t fa_off;	/* linear address for 32-bit system */
102 		uint16_t fa_seg;	/* code/data (etc) segment */
103 		uint16_t fa_opcode;	/* last opcode (sometimes) */
104 	} fa_32;
105 };
106 
107 struct	envxmm64 {
108 	uint16_t	en_cw;		/* control word (16bits) */
109 	uint16_t	en_sw;		/* status word (16bits) */
110 	uint8_t		en_tw;		/* tag word (8bits) */
111 	uint8_t		en_zero;
112 	uint16_t	en_opcode;	/* opcode last executed (11 bits ) */
113 	union {				/* fp instruction pointer */
114 		uint64_t	en_rip;
115 		union fp_addr	en_ip64;
116 	};
117 	union {				/* fp operand pointer */
118 		uint64_t	en_rdp;
119 		union fp_addr	en_dp64;
120 	};
121 	uint32_t	en_mxcsr;	/* SSE control/status register */
122 	uint32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
123 };
124 
125 /* Contents of each SSE extended accumulator */
126 struct	xmmacc {
127 	uint8_t		xmm_bytes[16];
128 };
129 
130 /* Contents of the upper 16 bytes of each AVX extended accumulator */
131 struct	ymmacc {
132 	uint8_t		ymm_bytes[16];
133 };
134 
135 /*
136  * Floating point context. (i386 fxsave/fxrstor)
137  * savexmm is a 512-byte structure
138  */
139 struct	savexmm {
140 	struct	envxmm		sv_env;		/*  32 */
141 	struct {
142 		struct fpacc87	fp_acc;
143 		uint8_t		fp_pad[6];
144 	} sv_fp[8];				/* 128 */
145 	struct xmmacc		sv_xmm[8];	/* 128 */
146 	uint8_t			sv_pad[224];	/* 224 */
147 } __aligned(16);
148 
149 /*
150  * Floating point context. (amd64 fxsave/fxrstor)
151  * savexmm64 is a 512-byte structure
152  */
153 struct	savexmm64 {
154 	struct	envxmm64	sv_env;		/*  32 */
155 	struct {
156 		struct fpacc87	fp_acc;
157 		uint8_t		fp_pad[6];
158 	} sv_fp[8];				/* 128 */
159 	struct xmmacc		sv_xmm[16];	/* 256 */
160 	uint8_t			sv_pad[96];	/*  96 */
161 } __aligned(16);
162 
163 /* xstate_hdr is a 64-byte structure */
164 struct	xstate_hdr {
165 	uint64_t	xstate_bv;
166 	uint64_t	xstate_xcomp_bv;
167 	uint8_t		xstate_rsrv0[8];
168 	uint8_t		xstate_rsrv[40];
169 };
170 #define	XSTATE_XCOMP_BV_COMPACT	(1ULL << 63)
171 
172 /* savexmm_xstate is a 320-byte structure (64 + 256) */
173 struct	savexmm_xstate {
174 	struct xstate_hdr	sx_hd;
175 	struct ymmacc		sx_ymm[16];
176 };
177 
178 /* saveymm is a 832-byte structure (i386) */
179 struct	saveymm {
180 	struct envxmm		sv_env;		/*  32 */
181 	struct {
182 		struct fpacc87	fp_acc;
183 		uint8_t		fp_pad[6];
184 	} sv_fp[8];				/* 128 */
185 	struct xmmacc		sv_xmm[16];	/* 256 */
186 	uint8_t			sv_pad[96];	/*  96 */
187 	struct savexmm_xstate	sv_xstate;	/* 320 */
188 } __aligned(64);
189 
190 /* saveymm64 is a 832-byte structure (amd64) */
191 struct	saveymm64 {
192 	struct envxmm64		sv_env;		/*  32 */
193 	struct {
194 		struct fpacc87	fp_acc;
195 		int8_t		fp_pad[6];
196 	} sv_fp[8];				/* 128 */
197 	struct xmmacc		sv_xmm[16];	/* 256 */
198 	uint8_t			sv_pad[96];	/*  96 */
199 	struct savexmm_xstate	sv_xstate;	/* 320 */
200 } __aligned(64);
201 
202 union	savefpu {
203 	struct	save87		sv_87;
204 	struct	savexmm		sv_xmm;
205 	struct	saveymm		sv_ymm;
206 	struct	savexmm64	sv_xmm64;
207 	struct	saveymm64	sv_ymm64;
208 	char sv_savearea[1024];	/* see mcontext_t */
209 };
210 
211 /*
212  * The hardware default control word for i387's and later coprocessors is
213  * 0x37F, giving:
214  *
215  *	round to nearest
216  *	64-bit precision
217  *	all exceptions masked.
218  *
219  * We modify the affine mode bit and precision bits in this to give:
220  *
221  *	affine mode for 287's (if they work at all) (1 in bitfield 1<<12)
222  *	53-bit precision (2 in bitfield 3<<8)
223  *
224  * 64-bit precision often gives bad results with high level languages
225  * because it makes the results of calculations depend on whether
226  * intermediate values are stored in memory or in FPU registers.
227  */
228 #define	__INITIAL_NPXCW__	0x127F
229 
230 #define __INITIAL_FPUCW__       0x037F	/* used by libm/arch/x86_64/fenv.c */
231 #define __INITIAL_FPUCW_I386__  0x127F
232 #define __INITIAL_MXCSR__       0x1F80	/* used by libm/arch/x86_64/fenv.c */
233 #define __INITIAL_MXCSR_MASK__  0xFFBF
234 
235 #ifdef _KERNEL
236 
237 struct proc;
238 struct trapframe;
239 
240 extern uint32_t npx_mxcsr_mask;
241 extern uint64_t npx_xcr0_mask;
242 
243 void	npxprobemask (void);
244 void	npxexit (void);
245 void	npxinit (void);
246 void	npxsave (union savefpu *addr);
247 void	fpusave (union savefpu *addr, uint64_t mask);
248 void	fpurstor (union savefpu *addr, uint64_t mask);
249 #endif
250 
251 #endif /* !_CPU_NPX_H_ */
252