1290001Sglebius/*-
2290001Sglebius * Copyright (c) 1990 The Regents of the University of California.
3290001Sglebius * All rights reserved.
4290001Sglebius *
5290001Sglebius * This code is derived from software contributed to Berkeley by
6290001Sglebius * William Jolitz.
7290001Sglebius *
8290001Sglebius * Redistribution and use in source and binary forms, with or without
9290001Sglebius * modification, are permitted provided that the following conditions
10290001Sglebius * are met:
11290001Sglebius * 1. Redistributions of source code must retain the above copyright
12290001Sglebius *    notice, this list of conditions and the following disclaimer.
13290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright
14290001Sglebius *    notice, this list of conditions and the following disclaimer in the
15290001Sglebius *    documentation and/or other materials provided with the distribution.
16290001Sglebius * 4. Neither the name of the University nor the names of its contributors
17290001Sglebius *    may be used to endorse or promote products derived from this software
18290001Sglebius *    without specific prior written permission.
19290001Sglebius *
20290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21290001Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22290001Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23290001Sglebius * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24290001Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25290001Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26290001Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27290001Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28290001Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29290001Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30290001Sglebius * SUCH DAMAGE.
31290001Sglebius *
32290001Sglebius *	from: @(#)npx.h	5.3 (Berkeley) 1/18/91
33290001Sglebius * $FreeBSD$
34290001Sglebius */
35290001Sglebius
36290001Sglebius/*
37290001Sglebius * Floating Point Data Structures and Constants
38290001Sglebius * W. Jolitz 1/90
39290001Sglebius */
40290001Sglebius
41290001Sglebius#ifndef _X86_FPU_H_
42290001Sglebius#define	_X86_FPU_H_
43290001Sglebius
44290001Sglebius/* Environment information of floating point unit. */
45290001Sglebiusstruct env87 {
46290001Sglebius	int32_t		en_cw;		/* control word (16bits) */
47290001Sglebius	int32_t		en_sw;		/* status word (16bits) */
48290001Sglebius	int32_t		en_tw;		/* tag word (16bits) */
49290001Sglebius	int32_t		en_fip;		/* fp instruction pointer */
50290001Sglebius	uint16_t	en_fcs;		/* fp code segment selector */
51290001Sglebius	uint16_t	en_opcode;	/* opcode last executed (11 bits) */
52290001Sglebius	int32_t		en_foo;		/* fp operand offset */
53290001Sglebius	int32_t		en_fos;		/* fp operand segment selector */
54290001Sglebius};
55290001Sglebius
56290001Sglebius/* Contents of each x87 floating point accumulator. */
57290001Sglebiusstruct fpacc87 {
58290001Sglebius	uint8_t		fp_bytes[10];
59290001Sglebius};
60290001Sglebius
61290001Sglebius/* Floating point context. (i386 fnsave/frstor) */
62290001Sglebiusstruct save87 {
63290001Sglebius	struct env87	sv_env;		/* floating point control/status */
64290001Sglebius	struct fpacc87	sv_ac[8];	/* accumulator contents, 0-7 */
65290001Sglebius	uint8_t		sv_pad0[4];	/* saved status word (now unused) */
66290001Sglebius	uint8_t		sv_pad[64];
67290001Sglebius};
68290001Sglebius
69290001Sglebius/* Contents of each SSE extended accumulator. */
70290001Sglebiusstruct xmmacc {
71290001Sglebius	uint8_t		xmm_bytes[16];
72290001Sglebius};
73290001Sglebius
74290001Sglebius/* Contents of the upper 16 bytes of each AVX extended accumulator. */
75290001Sglebiusstruct ymmacc {
76290001Sglebius	uint8_t		ymm_bytes[16];
77290001Sglebius};
78290001Sglebius
79290001Sglebius/* Rename structs below depending on machine architecture. */
80290001Sglebius#ifdef __i386__
81290001Sglebius#define	__envxmm32	envxmm
82290001Sglebius#else
83290001Sglebius#define	__envxmm32	envxmm32
84290001Sglebius#define	__envxmm64	envxmm
85290001Sglebius#endif
86290001Sglebius
87290001Sglebiusstruct __envxmm32 {
88290001Sglebius	uint16_t	en_cw;		/* control word (16bits) */
89290001Sglebius	uint16_t	en_sw;		/* status word (16bits) */
90290001Sglebius	uint16_t	en_tw;		/* tag word (16bits) */
91290001Sglebius	uint16_t	en_opcode;	/* opcode last executed (11 bits) */
92290001Sglebius	uint32_t	en_fip;		/* fp instruction pointer */
93290001Sglebius	uint16_t	en_fcs;		/* fp code segment selector */
94290001Sglebius	uint16_t	en_pad0;	/* padding */
95290001Sglebius	uint32_t	en_foo;		/* fp operand offset */
96290001Sglebius	uint16_t	en_fos;		/* fp operand segment selector */
97290001Sglebius	uint16_t	en_pad1;	/* padding */
98290001Sglebius	uint32_t	en_mxcsr;	/* SSE control/status register */
99290001Sglebius	uint32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
100290001Sglebius};
101290001Sglebius
102290001Sglebiusstruct __envxmm64 {
103290001Sglebius	uint16_t	en_cw;		/* control word (16bits) */
104290001Sglebius	uint16_t	en_sw;		/* status word (16bits) */
105290001Sglebius	uint8_t		en_tw;		/* tag word (8bits) */
106290001Sglebius	uint8_t		en_zero;
107290001Sglebius	uint16_t	en_opcode;	/* opcode last executed (11 bits ) */
108290001Sglebius	uint64_t	en_rip;		/* fp instruction pointer */
109290001Sglebius	uint64_t	en_rdp;		/* fp operand pointer */
110290001Sglebius	uint32_t	en_mxcsr;	/* SSE control/status register */
111290001Sglebius	uint32_t	en_mxcsr_mask;	/* valid bits in mxcsr */
112290001Sglebius};
113290001Sglebius
114290001Sglebius/* Floating point context. (i386 fxsave/fxrstor) */
115290001Sglebiusstruct savexmm {
116290001Sglebius	struct __envxmm32	sv_env;
117290001Sglebius	struct {
118290001Sglebius		struct fpacc87	fp_acc;
119290001Sglebius		uint8_t		fp_pad[6];      /* padding */
120290001Sglebius	} sv_fp[8];
121290001Sglebius	struct xmmacc		sv_xmm[8];
122290001Sglebius	uint8_t			sv_pad[224];
123290001Sglebius} __aligned(16);
124290001Sglebius
125290001Sglebius#ifdef __i386__
126290001Sglebiusunion savefpu {
127290001Sglebius	struct save87	sv_87;
128290001Sglebius	struct savexmm	sv_xmm;
129290001Sglebius};
130290001Sglebius#else
131290001Sglebius/* Floating point context. (amd64 fxsave/fxrstor) */
132290001Sglebiusstruct savefpu {
133290001Sglebius	struct __envxmm64	sv_env;
134290001Sglebius	struct {
135290001Sglebius		struct fpacc87	fp_acc;
136290001Sglebius		uint8_t		fp_pad[6];	/* padding */
137290001Sglebius	} sv_fp[8];
138290001Sglebius	struct xmmacc		sv_xmm[16];
139290001Sglebius	uint8_t			sv_pad[96];
140290001Sglebius} __aligned(16);
141290001Sglebius#endif
142290001Sglebius
143290001Sglebiusstruct xstate_hdr {
144290001Sglebius	uint64_t	xstate_bv;
145290001Sglebius	uint64_t	xstate_xcomp_bv;
146290001Sglebius	uint8_t		xstate_rsrv0[8];
147290001Sglebius	uint8_t		xstate_rsrv[40];
148290001Sglebius};
149290001Sglebius#define	XSTATE_XCOMP_BV_COMPACT	(1ULL << 63)
150290001Sglebius
151290001Sglebiusstruct savexmm_xstate {
152290001Sglebius	struct xstate_hdr	sx_hd;
153290001Sglebius	struct ymmacc		sx_ymm[16];
154290001Sglebius};
155290001Sglebius
156290001Sglebiusstruct savexmm_ymm {
157290001Sglebius	struct __envxmm32	sv_env;
158290001Sglebius	struct {
159290001Sglebius		struct fpacc87	fp_acc;
160290001Sglebius		int8_t		fp_pad[6];	/* padding */
161290001Sglebius	} sv_fp[8];
162290001Sglebius	struct xmmacc		sv_xmm[16];
163290001Sglebius	uint8_t			sv_pad[96];
164290001Sglebius	struct savexmm_xstate	sv_xstate;
165290001Sglebius} __aligned(64);
166290001Sglebius
167290001Sglebiusstruct savefpu_xstate {
168290001Sglebius	struct xstate_hdr	sx_hd;
169290001Sglebius	struct ymmacc		sx_ymm[16];
170290001Sglebius};
171290001Sglebius
172290001Sglebiusstruct savefpu_ymm {
173290001Sglebius	struct __envxmm64	sv_env;
174290001Sglebius	struct {
175290001Sglebius		struct fpacc87	fp_acc;
176290001Sglebius		int8_t		fp_pad[6];	/* padding */
177290001Sglebius	} sv_fp[8];
178290001Sglebius	struct xmmacc		sv_xmm[16];
179290001Sglebius	uint8_t			sv_pad[96];
180290001Sglebius	struct savefpu_xstate	sv_xstate;
181290001Sglebius} __aligned(64);
182290001Sglebius
183290001Sglebius#undef __envxmm32
184290001Sglebius#undef __envxmm64
185290001Sglebius
186290001Sglebius/*
187290001Sglebius * The hardware default control word for i387's and later coprocessors is
188290001Sglebius * 0x37F, giving:
189290001Sglebius *
190290001Sglebius *	round to nearest
191290001Sglebius *	64-bit precision
192290001Sglebius *	all exceptions masked.
193290001Sglebius *
194290001Sglebius * FreeBSD/i386 uses 53 bit precision for things like fadd/fsub/fsqrt etc
195290001Sglebius * because of the difference between memory and fpu register stack arguments.
196290001Sglebius * If its using an intermediate fpu register, it has 80/64 bits to work
197290001Sglebius * with.  If it uses memory, it has 64/53 bits to work with.  However,
198290001Sglebius * gcc is aware of this and goes to a fair bit of trouble to make the
199290001Sglebius * best use of it.
200290001Sglebius *
201290001Sglebius * This is mostly academic for AMD64, because the ABI prefers the use
202290001Sglebius * SSE2 based math.  For FreeBSD/amd64, we go with the default settings.
203290001Sglebius */
204290001Sglebius#define	__INITIAL_FPUCW__	0x037F
205290001Sglebius#define	__INITIAL_FPUCW_I386__	0x127F
206290001Sglebius#define	__INITIAL_NPXCW__	__INITIAL_FPUCW_I386__
207290001Sglebius#define	__INITIAL_MXCSR__	0x1F80
208290001Sglebius#define	__INITIAL_MXCSR_MASK__	0xFFBF
209290001Sglebius
210290001Sglebius/*
211290001Sglebius * The current value of %xcr0 is saved in the sv_pad[] field of the FPU
212290001Sglebius * state in the NT_X86_XSTATE note in core dumps.  This offset is chosen
213290001Sglebius * to match the offset used by NT_X86_XSTATE in other systems.
214290001Sglebius */
215290001Sglebius#define	X86_XSTATE_XCR0_OFFSET	464
216290001Sglebius
217290001Sglebius#endif /* !_X86_FPU_H_ */
218290001Sglebius