xref: /dragonfly/sys/cpu/x86_64/include/npx.h (revision bbb35c81)
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 struct envxmm64 {
98 	uint16_t	en_cw;		/* control word (16bits) */
99 	uint16_t	en_sw;		/* status word (16bits) */
100 	uint8_t		en_tw;		/* tag word (8bits) */
101 	uint8_t		en_zero;
102 	uint16_t	en_opcode;	/* opcode last executed (11 bits ) */
103 	uint64_t	en_rip;		/* fp instruction pointer */
104 	uint64_t	en_rdp;		/* fp operand pointer */
105 	uint32_t	en_mxcsr;	/* SSE control/status register */
106 	uint32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
107 };
108 
109 /* Contents of each SSE extended accumulator */
110 struct  xmmacc {
111 	uint8_t		xmm_bytes[16];
112 };
113 
114 /* Contents of the upper 16 bytes of each AVX extended accumulator */
115 struct ymmacc {
116 	uint8_t		ymm_bytes[16];
117 };
118 
119 /*
120  * Floating point context. (i386 fxsave/fxrstor)
121  * savexmm is a 512-byte structure
122  */
123 struct  savexmm {
124 	struct	envxmm	sv_env;			/*  32 */
125 	struct {
126 		struct fpacc87	fp_acc;		/*     -- 10 */
127 		uint8_t		fp_pad[6];      /*     -- 6  */
128 	} sv_fp[8];				/* 128 */
129 	struct xmmacc	sv_xmm[8];		/* 128 */
130 	uint8_t			sv_pad[224];	/* 224 (padding) */
131 } __aligned(16);
132 
133 /*
134  * Floating point context. (amd64 fxsave/fxrstor)
135  * savexmm64 is a 512-byte structure
136  */
137 struct	savexmm64 {
138 	struct	envxmm64	sv_env;		/*  32 */
139 	struct {
140 		struct fpacc87	fp_acc;
141 		uint8_t		fp_pad[6];
142 	} sv_fp[8];				/* 128 */
143 	struct xmmacc	sv_xmm[8];		/* 128 */
144 	uint8_t			sv_pad[224];	/* 224 */
145 } __aligned(16);
146 
147 /* xstate_hdr is a 64-byte structure */
148 struct	xstate_hdr {
149 	uint64_t	xstate_bv;
150 	uint64_t	xstate_xcomp_bv;
151 	uint8_t		xstate_rsrv0[8];
152 	uint8_t		xstate_rsrv[40];
153 };
154 #define	XSTATE_XCOMP_BV_COMPACT	(1ULL << 63)
155 
156 /* savexmm_xstate is a 320-byte structure (64 + 256) */
157 struct	savexmm_xstate {
158 	struct xstate_hdr	sx_hd;
159 	struct ymmacc		sx_ymm[16];
160 };
161 
162 /* saveymm is a 832-byte structure (i386) */
163 struct	saveymm {
164 	struct envxmm	sv_env;			/*  32 */
165 	struct {
166 		struct fpacc87	fp_acc;
167 		uint8_t		fp_pad[6];
168 	} sv_fp[8];				/* 128 */
169 	struct xmmacc	sv_xmm[16];		/* 256 */
170 	uint8_t			sv_pad[96];	/*  96 */
171 	struct savexmm_xstate	sv_xstate;	/* 320 */
172 } __aligned(64);
173 
174 /* saveymm64 is a 832-byte structure (amd64) */
175 struct	saveymm64 {
176 	struct envxmm64	sv_env;			/*  32 */
177 	struct {
178 		struct fpacc87	fp_acc;
179 		int8_t		fp_pad[6];
180 	} sv_fp[8];				/* 128 */
181 	struct xmmacc	sv_xmm[16];		/* 256 */
182 	uint8_t			sv_pad[96];	/*  96 */
183 	struct savexmm_xstate	sv_xstate;	/* 320 */
184 } __aligned(64);
185 
186 union	savefpu {
187 	struct	save87	sv_87;
188 	struct	savexmm	sv_xmm;
189 	struct  saveymm sv_ymm;
190 	struct	savexmm64	sv_xmm64;
191 	struct	saveymm64	sv_ymm64;
192 	char		sv_savearea[1024];	/* see mcontext_t */
193 };
194 
195 /*
196  * The hardware default control word for i387's and later coprocessors is
197  * 0x37F, giving:
198  *
199  *	round to nearest
200  *	64-bit precision
201  *	all exceptions masked.
202  *
203  * We modify the affine mode bit and precision bits in this to give:
204  *
205  *	affine mode for 287's (if they work at all) (1 in bitfield 1<<12)
206  *	53-bit precision (2 in bitfield 3<<8)
207  *
208  * 64-bit precision often gives bad results with high level languages
209  * because it makes the results of calculations depend on whether
210  * intermediate values are stored in memory or in FPU registers.
211  */
212 #define	__INITIAL_NPXCW__	0x127F
213 
214 #define __INITIAL_FPUCW__       0x037F	/* used by libm/arch/x86_64/fenv.c */
215 #define __INITIAL_FPUCW_I386__  0x127F
216 #define __INITIAL_MXCSR__       0x1F80	/* used by libm/arch/x86_64/fenv.c */
217 #define __INITIAL_MXCSR_MASK__  0xFFBF
218 
219 #ifdef _KERNEL
220 
221 struct proc;
222 struct trapframe;
223 
224 extern uint32_t npx_mxcsr_mask;
225 extern uint64_t npx_xcr0_mask;
226 
227 void	npxprobemask (void);
228 void	npxexit (void);
229 void	npxinit (void);
230 void	npxsave (union savefpu *addr);
231 #endif
232 
233 #endif /* !_CPU_NPX_H_ */
234