xref: /freebsd/sys/i386/include/npx.h (revision b7c60aadbbd5c846a250c05791fe7406d6d78bf4)
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  * 287/387 NPX Coprocessor Data Structures and Constants
38  * W. Jolitz 1/90
39  */
40 
41 #ifndef _MACHINE_NPX_H_
42 #define	_MACHINE_NPX_H_
43 
44 /* Environment information of floating point unit */
45 struct env87 {
46 	long	en_cw;		/* control word (16bits) */
47 	long	en_sw;		/* status word (16bits) */
48 	long	en_tw;		/* tag word (16bits) */
49 	long	en_fip;		/* floating point instruction pointer */
50 	u_short	en_fcs;		/* floating code segment selector */
51 	u_short	en_opcode;	/* opcode last executed (11 bits ) */
52 	long	en_foo;		/* floating operand offset */
53 	long	en_fos;		/* floating operand segment selector */
54 };
55 
56 /* Contents of each floating point accumulator */
57 struct fpacc87 {
58 #ifdef dontdef /* too unportable */
59 	u_long	fp_mantlo;	/* mantissa low (31:0) */
60 	u_long	fp_manthi;	/* mantissa high (63:32) */
61 	int	fp_exp:15;	/* exponent */
62 	int	fp_sgn:1;	/* mantissa sign */
63 #else
64 	u_char	fp_bytes[10];
65 #endif
66 };
67 
68 /* Floating point context */
69 struct save87 {
70 	struct	env87 sv_env;	/* floating point control/status */
71 	struct	fpacc87	sv_ac[8];	/* accumulator contents, 0-7 */
72 	u_char	sv_pad0[4];	/* padding for (now unused) saved status word */
73 	/*
74 	 * Bogus padding for emulators.  Emulators should use their own
75 	 * struct and arrange to store into this struct (ending here)
76 	 * before it is inspected for ptracing or for core dumps.  Some
77 	 * emulators overwrite the whole struct.  We have no good way of
78 	 * knowing how much padding to leave.  Leave just enough for the
79 	 * GPL emulator's i387_union (176 bytes total).
80 	 */
81 	u_char	sv_pad[64];	/* padding; used by emulators */
82 };
83 
84 struct  envxmm {
85 	u_int16_t	en_cw;		/* control word (16bits) */
86 	u_int16_t	en_sw;		/* status word (16bits) */
87 	u_int16_t	en_tw;		/* tag word (16bits) */
88 	u_int16_t	en_opcode;	/* opcode last executed (11 bits ) */
89 	u_int32_t	en_fip;		/* floating point instruction pointer */
90 	u_int16_t	en_fcs;		/* floating code segment selector */
91 	u_int16_t	en_pad0;	/* padding */
92 	u_int32_t	en_foo;		/* floating operand offset */
93 	u_int16_t	en_fos;		/* floating operand segment selector */
94 	u_int16_t	en_pad1;	/* padding */
95 	u_int32_t	en_mxcsr;	/* SSE control/status register */
96 	u_int32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
97 };
98 
99 /* Contents of each SSE extended accumulator */
100 struct  xmmacc {
101 	u_char	xmm_bytes[16];
102 };
103 
104 /* Contents of the upper 16 bytes of each AVX extended accumulator */
105 struct  ymmacc {
106 	uint8_t  ymm_bytes[16];
107 };
108 
109 struct  savexmm {
110 	struct	envxmm	sv_env;
111 	struct {
112 		struct fpacc87	fp_acc;
113 		u_char		fp_pad[6];      /* padding */
114 	} sv_fp[8];
115 	struct xmmacc	sv_xmm[8];
116 	u_char sv_pad[224];
117 } __aligned(16);
118 
119 union	savefpu {
120 	struct	save87	sv_87;
121 	struct	savexmm	sv_xmm;
122 };
123 
124 struct xstate_hdr {
125 	uint64_t xstate_bv;
126 	uint8_t xstate_rsrv0[16];
127 	uint8_t	xstate_rsrv[40];
128 };
129 
130 struct savexmm_xstate {
131 	struct xstate_hdr sx_hd;
132 	struct ymmacc	sx_ymm[16];
133 };
134 
135 struct savexmm_ymm {
136 	struct	envxmm	sv_env;
137 	struct {
138 		struct fpacc87	fp_acc;
139 		int8_t		fp_pad[6];      /* padding */
140 	} sv_fp[8];
141 	struct xmmacc	sv_xmm[16];
142 	uint8_t sv_pad[96];
143 	struct savexmm_xstate sv_xstate;
144 } __aligned(64);
145 
146 /*
147  * The hardware default control word for i387's and later coprocessors is
148  * 0x37F, giving:
149  *
150  *	round to nearest
151  *	64-bit precision
152  *	all exceptions masked.
153  *
154  * We modify the affine mode bit and precision bits in this to give:
155  *
156  *	affine mode for 287's (if they work at all) (1 in bitfield 1<<12)
157  *	53-bit precision (2 in bitfield 3<<8)
158  *
159  * 64-bit precision often gives bad results with high level languages
160  * because it makes the results of calculations depend on whether
161  * intermediate values are stored in memory or in FPU registers.
162  */
163 #define	__INITIAL_NPXCW__	0x127F
164 #define	__INITIAL_MXCSR__	0x1F80
165 
166 #ifdef _KERNEL
167 
168 #define	PCB_USER_FPU(pcb) (((pcb)->pcb_flags & PCB_KERNNPX) == 0)
169 
170 int	npxdna(void);
171 void	npxdrop(void);
172 void	npxexit(struct thread *td);
173 int	npxformat(void);
174 int	npxgetregs(struct thread *td);
175 void	npxinit(void);
176 void	npxsave(union savefpu *addr);
177 void	npxsetregs(struct thread *td, union savefpu *addr);
178 int	npxtrap(void);
179 void	npxuserinited(struct thread *);
180 struct fpu_kern_ctx *fpu_kern_alloc_ctx(u_int flags);
181 void	fpu_kern_free_ctx(struct fpu_kern_ctx *ctx);
182 int	fpu_kern_enter(struct thread *td, struct fpu_kern_ctx *ctx,
183 	    u_int flags);
184 int	fpu_kern_leave(struct thread *td, struct fpu_kern_ctx *ctx);
185 int	fpu_kern_thread(u_int flags);
186 int	is_fpu_kern_thread(u_int flags);
187 
188 /*
189  * Flags for fpu_kern_enter() and fpu_kern_thread().
190  */
191 #define	FPU_KERN_NORMAL	0x0000
192 #define	FPU_KERN_NOWAIT	0x0001
193 
194 #endif
195 
196 #endif /* !_MACHINE_NPX_H_ */
197