xref: /dragonfly/sys/cpu/x86_64/include/npx.h (revision d30a46c2)
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 #include <stdint.h>
46 
47 /* Environment information of floating point unit */
48 struct	env87 {
49 	int32_t		en_cw;		/* control word (16bits) */
50 	int32_t		en_sw;		/* status word (16bits) */
51 	int32_t		en_tw;		/* tag word (16bits) */
52 	int32_t		en_fip;		/* floating point instruction pointer */
53 	uint16_t	en_fcs;		/* floating code segment selector */
54 	uint16_t	en_opcode;	/* opcode last executed (11 bits ) */
55 	int32_t		en_foo;		/* floating operand offset */
56 	int32_t		en_fos;		/* floating operand segment selector */
57 };
58 
59 /* Contents of each x87 floating point accumulator */
60 struct	fpacc87 {
61 	uint8_t		fp_bytes[10];
62 };
63 
64 /* Floating point context (i386 fnsave/frstor) */
65 struct	save87 {
66 	struct	env87 sv_env;	/* floating point control/status */
67 	struct	fpacc87	sv_ac[8];	/* accumulator contents, 0-7 */
68 	uint8_t		sv_pad0[4];	/* saved status word (now unused) */
69 	/*
70 	 * Bogus padding for emulators.  Emulators should use their own
71 	 * struct and arrange to store into this struct (ending here)
72 	 * before it is inspected for ptracing or for core dumps.  Some
73 	 * emulators overwrite the whole struct.  We have no good way of
74 	 * knowing how much padding to leave.  Leave just enough for the
75 	 * GPL emulator's i387_union (176 bytes total).
76 	 */
77 	uint8_t		sv_pad[64];
78 };
79 
80 struct envxmm {
81 	uint16_t	en_cw;		/* control word (16bits) */
82 	uint16_t	en_sw;		/* status word (16bits) */
83 	uint16_t	en_tw;		/* tag word (16bits) */
84 	uint16_t	en_opcode;	/* opcode last executed (11 bits) */
85 	uint32_t	en_fip;		/* fp instruction pointer */
86 	uint16_t	en_fcs;		/* fp code segment selector */
87 	uint16_t	en_pad0;	/* padding */
88 	uint32_t	en_foo;		/* fp operand offset */
89 	uint16_t	en_fos;		/* fp operand segment selector */
90 	uint16_t	en_pad1;	/* padding */
91 	uint32_t	en_mxcsr;	/* SSE control/status register */
92 	uint32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
93 };
94 
95 struct envxmm64 {
96 	uint16_t	en_cw;		/* control word (16bits) */
97 	uint16_t	en_sw;		/* status word (16bits) */
98 	uint8_t		en_tw;		/* tag word (8bits) */
99 	uint8_t		en_zero;
100 	uint16_t	en_opcode;	/* opcode last executed (11 bits ) */
101 	uint64_t	en_rip;		/* fp instruction pointer */
102 	uint64_t	en_rdp;		/* fp operand pointer */
103 	uint32_t	en_mxcsr;	/* SSE control/status register */
104 	uint32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
105 };
106 
107 /* Contents of each SSE extended accumulator */
108 struct  xmmacc {
109 	uint8_t		xmm_bytes[16];
110 };
111 
112 /* Contents of the upper 16 bytes of each AVX extended accumulator */
113 struct ymmacc {
114 	uint8_t		ymm_bytes[16];
115 };
116 
117 /*
118  * Floating point context. (i386 fxsave/fxrstor)
119  * savexmm is a 512-byte structure
120  */
121 struct  savexmm {
122 	struct	envxmm	sv_env;			/*  32 */
123 	struct {
124 		struct fpacc87	fp_acc;		/*     -- 10 */
125 		uint8_t		fp_pad[6];      /*     -- 6  */
126 	} sv_fp[8];				/* 128 */
127 	struct xmmacc	sv_xmm[8];		/* 128 */
128 	uint8_t			sv_pad[224];	/* 224 (padding) */
129 } __attribute__((aligned(16)));
130 
131 /*
132  * Floating point context. (amd64 fxsave/fxrstor)
133  * savexmm64 is a 512-byte structure
134  */
135 struct	savexmm64 {
136 	struct	envxmm64	sv_env;		/*  32 */
137 	struct {
138 		struct fpacc87	fp_acc;
139 		uint8_t		fp_pad[6];
140 	} sv_fp[8];				/* 128 */
141 	struct xmmacc	sv_xmm[8];		/* 128 */
142 	uint8_t			sv_pad[224];	/* 224 */
143 } __attribute__((aligned(16)));
144 
145 /* xstate_hdr is a 64-byte structure */
146 struct	xstate_hdr {
147 	uint64_t	xstate_bv;
148 	uint64_t	xstate_xcomp_bv;
149 	uint8_t		xstate_rsrv0[8];
150 	uint8_t		xstate_rsrv[40];
151 };
152 #define	XSTATE_XCOMP_BV_COMPACT	(1ULL << 63)
153 
154 /* savexmm_xstate is a 320-byte structure (64 + 256) */
155 struct	savexmm_xstate {
156 	struct xstate_hdr	sx_hd;
157 	struct ymmacc		sx_ymm[16];
158 };
159 
160 /* saveymm is a 832-byte structure (i386) */
161 struct	saveymm {
162 	struct envxmm	sv_env;			/*  32 */
163 	struct {
164 		struct fpacc87	fp_acc;
165 		uint8_t		fp_pad[6];
166 	} sv_fp[8];				/* 128 */
167 	struct xmmacc	sv_xmm[16];		/* 256 */
168 	uint8_t			sv_pad[96];	/*  96 */
169 	struct savexmm_xstate	sv_xstate;	/* 320 */
170 } __attribute__((aligned(64)));
171 
172 /* saveymm64 is a 832-byte structure (amd64) */
173 struct	saveymm64 {
174 	struct envxmm64	sv_env;			/*  32 */
175 	struct {
176 		struct fpacc87	fp_acc;
177 		int8_t		fp_pad[6];
178 	} sv_fp[8];				/* 128 */
179 	struct xmmacc	sv_xmm[16];		/* 256 */
180 	uint8_t			sv_pad[96];	/*  96 */
181 	struct savexmm_xstate	sv_xstate;	/* 320 */
182 } __attribute__((aligned(64)));
183 
184 union	savefpu {
185 	struct	save87	sv_87;
186 	struct	savexmm	sv_xmm;
187 	struct  saveymm sv_ymm;
188 	struct	savexmm64	sv_xmm64;
189 	struct	saveymm64	sv_ymm64;
190 	char		sv_savearea[1024];	/* see mcontext_t */
191 };
192 
193 /*
194  * The hardware default control word for i387's and later coprocessors is
195  * 0x37F, giving:
196  *
197  *	round to nearest
198  *	64-bit precision
199  *	all exceptions masked.
200  *
201  * We modify the affine mode bit and precision bits in this to give:
202  *
203  *	affine mode for 287's (if they work at all) (1 in bitfield 1<<12)
204  *	53-bit precision (2 in bitfield 3<<8)
205  *
206  * 64-bit precision often gives bad results with high level languages
207  * because it makes the results of calculations depend on whether
208  * intermediate values are stored in memory or in FPU registers.
209  */
210 #define	__INITIAL_NPXCW__	0x127F
211 
212 #define __INITIAL_FPUCW__       0x037F	/* used by libm/arch/x86_64/fenv.c */
213 #define __INITIAL_FPUCW_I386__  0x127F
214 #define __INITIAL_MXCSR__       0x1F80	/* used by libm/arch/x86_64/fenv.c */
215 #define __INITIAL_MXCSR_MASK__  0xFFBF
216 
217 #ifdef _KERNEL
218 
219 struct proc;
220 struct trapframe;
221 
222 extern uint32_t npx_mxcsr_mask;
223 
224 void	npxprobemask (void);
225 void	npxexit (void);
226 void	npxinit (void);
227 void	npxsave (union savefpu *addr);
228 #endif
229 
230 #endif /* !_CPU_NPX_H_ */
231