Home | History | Annotate | Line # | Download | only in include
cpu_extended_state.h revision 1.17.28.1
      1  1.17.28.1  martin /*	$NetBSD: cpu_extended_state.h,v 1.17.28.1 2023/07/25 11:41:42 martin Exp $	*/
      2        1.1     dsl 
      3        1.1     dsl #ifndef _X86_CPU_EXTENDED_STATE_H_
      4        1.1     dsl #define _X86_CPU_EXTENDED_STATE_H_
      5        1.1     dsl 
      6        1.1     dsl #ifdef __lint__
      7        1.1     dsl /* Lint has different packing rules and doesn't understand __aligned() */
      8        1.1     dsl #define __CTASSERT_NOLINT(x) __CTASSERT(1)
      9        1.1     dsl #else
     10        1.1     dsl #define __CTASSERT_NOLINT(x) __CTASSERT(x)
     11        1.1     dsl #endif
     12        1.1     dsl 
     13        1.1     dsl /*
     14       1.10    maxv  * This file contains definitions of structures that match the memory layouts
     15       1.10    maxv  * used on x86 processors to save floating point registers and other extended
     16       1.10    maxv  * cpu states.
     17       1.10    maxv  *
     18       1.10    maxv  * This includes registers (etc) used by SSE/SSE2/SSE3/SSSE3/SSE4 and the later
     19       1.10    maxv  * AVX instructions.
     20       1.10    maxv  *
     21       1.16    maxv  * The definitions are such that any future 'extended state' should be handled,
     22       1.16    maxv  * provided the kernel doesn't need to know the actual contents.
     23       1.10    maxv  *
     24       1.10    maxv  * The actual structures the cpu accesses must be aligned to 16 bytes for FXSAVE
     25       1.10    maxv  * and 64 for XSAVE. The types aren't aligned because copies do not need extra
     26       1.10    maxv  * alignment.
     27       1.10    maxv  *
     28       1.10    maxv  * The slightly different layout saved by the i387 fsave is also defined.
     29       1.10    maxv  * This is only normally written by pre Pentium II type cpus that don't
     30       1.10    maxv  * support the fxsave instruction.
     31       1.10    maxv  *
     32       1.10    maxv  * Associated save instructions:
     33       1.16    maxv  * FNSAVE:   Saves x87 state in 108 bytes (original i387 layout). Then
     34       1.16    maxv  *           reinitializes the fpu.
     35       1.10    maxv  * FSAVE:    Encodes to FWAIT followed by FNSAVE.
     36       1.16    maxv  * FXSAVE:   Saves the x87 state and XMM (aka SSE) registers to the first
     37       1.16    maxv  *           448 (max) bytes of a 512 byte area. This layout does not match
     38       1.16    maxv  *           that written by FNSAVE.
     39       1.16    maxv  * XSAVE:    Uses the same layout for the x87 and XMM registers, followed by
     40       1.16    maxv  *           a 64byte header and separate save areas for additional extended
     41       1.16    maxv  *           cpu states. The x87 state is always saved, the others
     42       1.16    maxv  *           conditionally.
     43       1.16    maxv  * XSAVEOPT: Same as XSAVE but only writes the registers blocks that have
     44       1.16    maxv  *           been modified.
     45       1.10    maxv  */
     46       1.10    maxv 
     47       1.10    maxv /*
     48       1.10    maxv  * Layout for code/data pointers relating to FP exceptions. Marked 'packed'
     49       1.10    maxv  * because they aren't always 64bit aligned. Since the x86 cpu supports
     50       1.10    maxv  * misaligned accesses it isn't worth avoiding the 'packed' attribute.
     51        1.1     dsl  */
     52        1.1     dsl union fp_addr {
     53        1.1     dsl 	uint64_t fa_64;	/* Linear address for 64bit systems */
     54        1.1     dsl 	struct {
     55        1.1     dsl 		uint32_t fa_off;	/* linear address for 32 bit */
     56        1.1     dsl 		uint16_t fa_seg;	/* code/data (etc) segment */
     57        1.1     dsl 		uint16_t fa_opcode;	/* last opcode (sometimes) */
     58        1.1     dsl 	} fa_32;
     59        1.6     dsl } __packed __aligned(4);
     60        1.1     dsl 
     61        1.1     dsl /* The x87 registers are 80 bits */
     62        1.1     dsl struct fpacc87 {
     63       1.10    maxv 	uint64_t f87_mantissa;	/* mantissa */
     64       1.10    maxv 	uint16_t f87_exp_sign;	/* exponent and sign */
     65        1.6     dsl } __packed __aligned(2);
     66        1.1     dsl 
     67        1.1     dsl /* The x87 registers padded out to 16 bytes for fxsave */
     68        1.1     dsl struct fpaccfx {
     69        1.1     dsl 	struct fpacc87 r __aligned(16);
     70        1.1     dsl };
     71        1.1     dsl 
     72        1.1     dsl /* The SSE/SSE2 registers are 128 bits */
     73        1.1     dsl struct xmmreg {
     74        1.1     dsl 	uint8_t xmm_bytes[16];
     75        1.1     dsl };
     76        1.1     dsl 
     77        1.1     dsl /* The AVX registers are 256 bits, but the low bits are the xmmregs */
     78        1.1     dsl struct ymmreg {
     79        1.1     dsl 	uint8_t ymm_bytes[16];
     80        1.1     dsl };
     81        1.1     dsl 
     82       1.17  mgorny /* The AVX-512 registers are 512 bits but the low bits are in xmmregs
     83       1.17  mgorny  * and ymmregs */
     84       1.17  mgorny struct zmmreg {
     85       1.17  mgorny 	uint8_t zmm_bytes[32];
     86       1.17  mgorny };
     87       1.17  mgorny 
     88       1.17  mgorny /* 512-bit ZMM register. */
     89       1.17  mgorny struct hi16_zmmreg {
     90       1.17  mgorny 	uint8_t zmm_bytes[64];
     91       1.17  mgorny };
     92       1.17  mgorny 
     93        1.1     dsl /*
     94       1.16    maxv  * Floating point unit registers (FSAVE instruction).
     95       1.16    maxv  *
     96       1.16    maxv  * The s87_ac[] and fx_87_ac[] are relative to the stack top. The 'tag word'
     97       1.16    maxv  * contains 2 bits per register and refers to absolute register numbers.
     98       1.16    maxv  *
     99        1.1     dsl  * The cpu sets the tag values 0b01 (zero) and 0b10 (special) when a value
    100        1.1     dsl  * is loaded. The software need only set 0b00 (used) and 0xb11 (unused).
    101        1.1     dsl  * The fxsave 'Abridged tag word' in inverted.
    102        1.1     dsl  */
    103        1.1     dsl struct save87 {
    104       1.10    maxv 	uint16_t s87_cw __aligned(4);	/* control word */
    105       1.10    maxv 	uint16_t s87_sw __aligned(4);	/* status word  */
    106       1.10    maxv 	uint16_t s87_tw __aligned(4);	/* tag word */
    107       1.10    maxv 	union fp_addr s87_ip;		/* floating point instruction pointer */
    108        1.1     dsl #define s87_opcode s87_ip.fa_32.fa_opcode	/* opcode last executed (11bits) */
    109       1.10    maxv 	union fp_addr s87_dp;		/* floating operand offset */
    110       1.10    maxv 	struct fpacc87 s87_ac[8];	/* accumulator contents */
    111        1.1     dsl };
    112       1.10    maxv __CTASSERT_NOLINT(sizeof(struct save87) == 108);
    113        1.1     dsl 
    114       1.10    maxv /*
    115       1.16    maxv  * FPU/MMX/SSE/SSE2 context (FXSAVE instruction).
    116       1.10    maxv  */
    117        1.1     dsl struct fxsave {
    118       1.10    maxv 	uint16_t fx_cw;		/* FPU Control Word */
    119       1.10    maxv 	uint16_t fx_sw;		/* FPU Status Word */
    120       1.10    maxv 	uint8_t fx_tw;		/* FPU Tag Word (abridged) */
    121       1.12    maxv 	uint8_t fx_zero;	/* zero */
    122       1.10    maxv 	uint16_t fx_opcode;	/* FPU Opcode */
    123       1.10    maxv 	union fp_addr fx_ip;	/* FPU Instruction Pointer */
    124       1.10    maxv 	union fp_addr fx_dp;	/* FPU Data pointer */
    125       1.10    maxv 	uint32_t fx_mxcsr;	/* MXCSR Register State */
    126       1.10    maxv 	uint32_t fx_mxcsr_mask;
    127       1.10    maxv 	struct fpaccfx fx_87_ac[8];	/* 8 x87 registers */
    128       1.10    maxv 	struct xmmreg fx_xmm[16];	/* XMM regs (8 in 32bit modes) */
    129       1.15    maxv 	uint8_t fx_rsvd[96];
    130        1.1     dsl } __aligned(16);
    131       1.10    maxv __CTASSERT_NOLINT(sizeof(struct fxsave) == 512);
    132        1.1     dsl 
    133       1.10    maxv /*
    134       1.10    maxv  * For XSAVE, a 64byte header follows the fxsave data.
    135        1.1     dsl  */
    136        1.1     dsl struct xsave_header {
    137       1.16    maxv 	uint8_t xsh_fxsave[512];	/* struct fxsave */
    138       1.10    maxv 	uint64_t xsh_xstate_bv;		/* bitmap of saved sub structures */
    139       1.12    maxv 	uint64_t xsh_xcomp_bv;		/* bitmap of compact sub structures */
    140       1.12    maxv 	uint8_t xsh_rsrvd[8];		/* must be zero */
    141       1.12    maxv 	uint8_t xsh_reserved[40];	/* best if zero */
    142        1.1     dsl };
    143       1.10    maxv __CTASSERT(sizeof(struct xsave_header) == 512 + 64);
    144        1.1     dsl 
    145        1.1     dsl /*
    146        1.1     dsl  * The ymm save area actually follows the xsave_header.
    147        1.1     dsl  */
    148        1.1     dsl struct xsave_ymm {
    149       1.10    maxv 	struct ymmreg xs_ymm[16];	/* High bits of YMM registers */
    150        1.1     dsl };
    151       1.10    maxv __CTASSERT(sizeof(struct xsave_ymm) == 256);
    152        1.1     dsl 
    153        1.9     dsl /*
    154       1.17  mgorny  * AVX-512: opmask state.
    155       1.17  mgorny  */
    156       1.17  mgorny struct xsave_opmask {
    157       1.17  mgorny 	uint64_t xs_k[8];			/* k0..k7 registers. */
    158       1.17  mgorny };
    159       1.17  mgorny __CTASSERT(sizeof(struct xsave_opmask) == 64);
    160       1.17  mgorny 
    161       1.17  mgorny /*
    162       1.17  mgorny  * AVX-512: ZMM_Hi256 state.
    163       1.17  mgorny  */
    164       1.17  mgorny struct xsave_zmm_hi256 {
    165       1.17  mgorny 	struct zmmreg xs_zmm[16];	/* High bits of zmm0..zmm15 registers. */
    166       1.17  mgorny };
    167       1.17  mgorny __CTASSERT(sizeof(struct xsave_zmm_hi256) == 512);
    168       1.17  mgorny 
    169       1.17  mgorny /*
    170       1.17  mgorny  * AVX-512: Hi16_ZMM state.
    171       1.17  mgorny  */
    172       1.17  mgorny struct xsave_hi16_zmm {
    173       1.17  mgorny 	struct hi16_zmmreg xs_hi16_zmm[16];	/* zmm16..zmm31 registers. */
    174       1.17  mgorny };
    175       1.17  mgorny __CTASSERT(sizeof(struct xsave_hi16_zmm) == 1024);
    176       1.17  mgorny 
    177       1.17  mgorny /*
    178       1.17  mgorny  * Structure used to hold all interesting data from XSAVE, in predictable form.
    179       1.17  mgorny  * Note that this structure can have new members added to the end.
    180       1.17  mgorny  */
    181       1.17  mgorny struct xstate {
    182       1.17  mgorny 	/*
    183       1.17  mgorny 	 * The two following fields are bitmaps of XSAVE components.  They can be
    184       1.17  mgorny 	 * matched against XCR0_* constants from <machine/specialreg.h>).
    185       1.17  mgorny 	 */
    186       1.17  mgorny 	/*
    187       1.17  mgorny 	 * XSAVE/XRSTOR RFBM parameter.
    188       1.17  mgorny 	 *
    189       1.17  mgorny 	 * PT_GETXSTATE: 1 indicates that the respective XSAVE component is
    190       1.17  mgorny 	 * supported and has been enabled for saving.  0 indicates that it is not
    191       1.17  mgorny 	 * supported by the platform or kernel.
    192       1.17  mgorny 	 *
    193       1.17  mgorny 	 * PT_SETXSTATE: 1 indicates that the respective XSAVE component should
    194       1.17  mgorny 	 * be updated to the value of respective field (or reset if xs_xsave_bv
    195       1.17  mgorny 	 * bit is 0).  0 indicates that it should be left intact.  It is an error
    196       1.17  mgorny 	 * to enable bits that are not supported by the platform or kernel.
    197       1.17  mgorny 	 */
    198       1.17  mgorny 	uint64_t xs_rfbm;
    199       1.17  mgorny 	/*
    200       1.17  mgorny 	 * XSAVE/XRSTOR xstate header.
    201       1.17  mgorny 	 *
    202       1.17  mgorny 	 * PT_GETXSTATE: 1 indicates that the respective XSAVE component has been
    203       1.17  mgorny 	 * saved.  0 indicates that it had been in its CPU-defined initial value
    204       1.17  mgorny 	 * at the time of saving (i.e. was not used by the program).
    205       1.17  mgorny 	 *
    206       1.17  mgorny 	 * PT_SETXSTATE: 1 indicates that the respective XSAVE component (if present
    207       1.17  mgorny 	 * in xs_rfbm) should be set to the values in respective field.  0 indicates
    208       1.17  mgorny 	 * that it should be reset to CPU-defined initial value.
    209       1.17  mgorny 	 */
    210       1.17  mgorny 	uint64_t xs_xstate_bv;
    211       1.17  mgorny 
    212       1.17  mgorny 	/* legacy FXSAVE area (used for x87 & SSE state) */
    213       1.17  mgorny 	struct fxsave xs_fxsave;
    214       1.17  mgorny 	/* AVX state: high bits of ymm0..ymm15 registers */
    215       1.17  mgorny 	struct xsave_ymm xs_ymm_hi128;
    216       1.17  mgorny 	/* AVX-512: opmask */
    217       1.17  mgorny 	struct xsave_opmask xs_opmask;
    218       1.17  mgorny 	/* AVX-512: high bits of zmm0..zmm15 registers */
    219       1.17  mgorny 	struct xsave_zmm_hi256 xs_zmm_hi256;
    220       1.17  mgorny 	/* AVX-512: whole zmm16..zmm31 registers */
    221       1.17  mgorny 	struct xsave_hi16_zmm xs_hi16_zmm;
    222       1.17  mgorny };
    223       1.17  mgorny 
    224       1.17  mgorny /*
    225        1.9     dsl  * The following union is placed at the end of the pcb.
    226        1.9     dsl  * It is defined this way to separate the definitions and to
    227        1.9     dsl  * minimise the number of union/struct selectors.
    228        1.9     dsl  * NB: Some userspace stuff (eg firefox) uses it to parse ucontext.
    229        1.9     dsl  */
    230        1.9     dsl union savefpu {
    231       1.16    maxv 	struct save87 sv_87;
    232       1.16    maxv 	struct fxsave sv_xmm;
    233        1.9     dsl #ifdef _KERNEL
    234       1.16    maxv 	struct xsave_header sv_xsave_hdr;
    235        1.9     dsl #endif
    236        1.9     dsl };
    237        1.1     dsl 
    238        1.1     dsl /*
    239        1.1     dsl  * 80387 control and status word bits
    240        1.1     dsl  *
    241        1.3     dsl  * The only reference I can find to bits 0x40 and 0x80 in the control word
    242        1.3     dsl  * is for the Weitek 1167/3167.
    243        1.1     dsl  * I (dsl) can't find why the default word has 0x40 set.
    244        1.3     dsl  *
    245        1.3     dsl  * A stack error is signalled as an INVOP that also sets STACK_FAULT
    246        1.3     dsl  * (other INVOP do not clear STACK_FAULT).
    247        1.1     dsl  */
    248        1.1     dsl /* Interrupt masks (set masks interrupt) and status bits */
    249        1.1     dsl #define EN_SW_INVOP		0x0001  /* Invalid operation */
    250        1.1     dsl #define EN_SW_DENORM		0x0002  /* Denormalized operand */
    251        1.1     dsl #define EN_SW_ZERODIV		0x0004  /* Divide by zero */
    252        1.1     dsl #define EN_SW_OVERFLOW		0x0008  /* Overflow */
    253        1.1     dsl #define EN_SW_UNDERFLOW		0x0010  /* Underflow */
    254        1.1     dsl #define EN_SW_PRECLOSS		0x0020  /* Loss of precision */
    255        1.3     dsl /* Status word bits (reserved in control word) */
    256        1.3     dsl #define EN_SW_STACK_FAULT	0x0040	/* Stack under/overflow */
    257       1.10    maxv #define EN_SW_ERROR_SUMMARY	0x0080	/* Unmasked error has occurred */
    258        1.1     dsl /* Control bits (badly named) */
    259        1.1     dsl #define EN_SW_CTL_PREC		0x0300	/* Precision control */
    260        1.1     dsl #define EN_SW_PREC_24		0x0000	/* Single precision */
    261        1.1     dsl #define EN_SW_PREC_53		0x0200	/* Double precision */
    262        1.1     dsl #define EN_SW_PREC_64		0x0300	/* Extended precision */
    263        1.1     dsl #define EN_SW_CTL_ROUND		0x0c00	/* Rounding control */
    264        1.1     dsl #define EN_SW_ROUND_EVEN	0x0000	/* Round to nearest even */
    265        1.1     dsl #define EN_SW_ROUND_DOWN	0x0400	/* Round towards minus infinity */
    266        1.1     dsl #define EN_SW_ROUND_UP		0x0800	/* Round towards plus infinity */
    267        1.1     dsl #define EN_SW_ROUND_ZERO	0x0c00	/* Round towards zero (truncates) */
    268        1.1     dsl #define EN_SW_CTL_INF		0x1000	/* Infinity control, not used  */
    269        1.1     dsl 
    270        1.1     dsl /*
    271        1.1     dsl  * The standard 0x87 control word from finit is 0x37F, giving:
    272        1.1     dsl  *	round to nearest
    273        1.1     dsl  *	64-bit precision
    274        1.1     dsl  *	all exceptions masked.
    275        1.1     dsl  *
    276        1.1     dsl  * NetBSD used to select:
    277        1.1     dsl  *	round to nearest
    278        1.1     dsl  *	53-bit precision
    279        1.1     dsl  *	all exceptions masked.
    280        1.1     dsl  * Stating: 64-bit precision often gives bad results with high level
    281        1.1     dsl  * languages because it makes the results of calculations depend on whether
    282        1.1     dsl  * intermediate values are stored in memory or in FPU registers.
    283        1.1     dsl  * Also some 'pathological divisions' give an error in the LSB because
    284        1.1     dsl  * the value is first rounded up when the 64bit mantissa is generated,
    285        1.1     dsl  * and then again when it is truncated to 53 bits.
    286        1.1     dsl  *
    287        1.1     dsl  * However the C language explicitly allows the extra precision.
    288        1.1     dsl  */
    289        1.1     dsl #define	__INITIAL_NPXCW__	0x037f
    290        1.1     dsl /* Modern NetBSD uses the default control word.. */
    291        1.1     dsl #define	__NetBSD_NPXCW__	__INITIAL_NPXCW__
    292        1.1     dsl /* NetBSD before 6.99.26 forced IEEE double precision. */
    293        1.1     dsl #define	__NetBSD_COMPAT_NPXCW__	0x127f
    294        1.1     dsl /* FreeBSD leaves some exceptions unmasked as well. */
    295        1.1     dsl #define	__FreeBSD_NPXCW__	0x1272
    296        1.1     dsl /* Linux just uses the default control word. */
    297        1.1     dsl #define	__Linux_NPXCW__		__INITIAL_NPXCW__
    298        1.1     dsl 
    299        1.1     dsl /*
    300        1.1     dsl  * The default MXCSR value at reset is 0x1f80, IA-32 Instruction
    301        1.1     dsl  * Set Reference, pg. 3-369.
    302        1.1     dsl  *
    303        1.1     dsl  * The low 6 bits of the mxcsr are the fp status bits (same order as x87).
    304        1.1     dsl  * Bit 6 is 'denormals are zero' (speeds up calculations).
    305        1.1     dsl  * Bits 7-16 are the interrupt mask bits (same order, 1 to mask).
    306        1.1     dsl  * Bits 13 and 14 are rounding control.
    307        1.1     dsl  * Bit 15 is 'flush to zero' - affects underflow.
    308        1.1     dsl  * Bits 16-31 must be zero.
    309  1.17.28.1  martin  *
    310  1.17.28.1  martin  * The safe MXCSR is fit for constant-time use, e.g. in crypto.  Some
    311  1.17.28.1  martin  * CPU instructions take input- dependent time if an exception status
    312  1.17.28.1  martin  * bit is not set; __SAFE_MXCSR__ has the exception status bits all set
    313  1.17.28.1  martin  * already to mitigate this.  See:
    314  1.17.28.1  martin  * https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/best-practices/mxcsr-configuration-dependent-timing.html
    315        1.1     dsl  */
    316        1.1     dsl #define	__INITIAL_MXCSR__	0x1f80
    317        1.2     dsl #define	__INITIAL_MXCSR_MASK__	0xffbf
    318  1.17.28.1  martin #define	__SAFE_MXCSR__		0x1fbf
    319        1.2     dsl 
    320        1.1     dsl #endif /* _X86_CPU_EXTENDED_STATE_H_ */
    321