Improve FPU Tag Word reconstruction on i386 to indicate register states.

Improve the code reconstructing en_tw in struct fpreg32 from FXSAVE
results so that all register states are indicated correctly.  The
previous code unconditionally mapped non-empty register state to
'normalized value' constant.  The new code explicitly distinguishes
the 'zero value' and 'special value' constants as well.  This improves
consistency between real FSAVE and translation from FXSAVE, and
ensures that tests using PT_GETFPREGS can rely on a single correct
value independently of the underlying implementation.

PR:	250454
Sponsored by:	The FreeBSD Foundation
Obtained from:	Moritz Systems
Submitted by:	Michał Górny <mgorny@moritz.systems>
Discussed with:	emaste
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D26856
This commit is contained in:
Konstantin Belousov 2020-10-21 00:15:12 +00:00
parent a2b559df1e
commit c0b5fcf692
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=366904
2 changed files with 74 additions and 15 deletions

View File

@ -145,7 +145,11 @@ fill_fpregs32(struct thread *td, struct fpreg32 *regs)
struct save87 *sv_87; struct save87 *sv_87;
struct env87 *penv_87; struct env87 *penv_87;
struct envxmm *penv_xmm; struct envxmm *penv_xmm;
int i; struct fpacc87 *fx_reg;
int i, st;
uint64_t mantissa;
uint16_t tw, exp;
uint8_t ab_tw;
bzero(regs, sizeof(*regs)); bzero(regs, sizeof(*regs));
sv_87 = (struct save87 *)regs; sv_87 = (struct save87 *)regs;
@ -171,13 +175,39 @@ fill_fpregs32(struct thread *td, struct fpreg32 *regs)
/* Entry into the kernel always sets TF_HASSEGS */ /* Entry into the kernel always sets TF_HASSEGS */
penv_87->en_fos = td->td_frame->tf_ds; penv_87->en_fos = td->td_frame->tf_ds;
/* FPU registers and tags */ /*
penv_87->en_tw = 0xffff; * FPU registers and tags.
for (i = 0; i < 8; ++i) { * For ST(i), i = fpu_reg - top; we start with fpu_reg=7.
sv_87->sv_ac[i] = sv_fpu->sv_fp[i].fp_acc; */
if ((penv_xmm->en_tw & (1 << i)) != 0) st = 7 - ((penv_xmm->en_sw >> 11) & 7);
penv_87->en_tw &= ~(3 << i * 2); ab_tw = penv_xmm->en_tw;
tw = 0;
for (i = 0x80; i != 0; i >>= 1) {
sv_87->sv_ac[st] = sv_fpu->sv_fp[st].fp_acc;
tw <<= 2;
if ((ab_tw & i) != 0) {
/* Non-empty - we need to check ST(i) */
fx_reg = &sv_fpu->sv_fp[st].fp_acc;
/* The first 64 bits contain the mantissa. */
mantissa = *((uint64_t *)fx_reg->fp_bytes);
/*
* The final 16 bits contain the sign bit and the exponent.
* Mask the sign bit since it is of no consequence to these
* tests.
*/
exp = *((uint16_t *)&fx_reg->fp_bytes[8]) & 0x7fff;
if (exp == 0) {
if (mantissa == 0)
tw |= 1; /* Zero */
else
tw |= 2; /* Denormal */
} else if (exp == 0x7fff)
tw |= 2; /* Infinity or NaN */
} else
tw |= 3; /* Empty */
st = (st - 1) & 7;
} }
penv_87->en_tw = tw;
return (0); return (0);
} }

View File

@ -1154,7 +1154,11 @@ npx_fill_fpregs_xmm1(struct savexmm *sv_xmm, struct save87 *sv_87)
{ {
struct env87 *penv_87; struct env87 *penv_87;
struct envxmm *penv_xmm; struct envxmm *penv_xmm;
int i; struct fpacc87 *fx_reg;
int i, st;
uint64_t mantissa;
uint16_t tw, exp;
uint8_t ab_tw;
penv_87 = &sv_87->sv_env; penv_87 = &sv_87->sv_env;
penv_xmm = &sv_xmm->sv_env; penv_xmm = &sv_xmm->sv_env;
@ -1168,14 +1172,39 @@ npx_fill_fpregs_xmm1(struct savexmm *sv_xmm, struct save87 *sv_87)
penv_87->en_foo = penv_xmm->en_foo; penv_87->en_foo = penv_xmm->en_foo;
penv_87->en_fos = penv_xmm->en_fos; penv_87->en_fos = penv_xmm->en_fos;
/* FPU registers and tags */ /*
penv_87->en_tw = 0xffff; * FPU registers and tags.
for (i = 0; i < 8; ++i) { * For ST(i), i = fpu_reg - top; we start with fpu_reg=7.
sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc; */
if ((penv_xmm->en_tw & (1 << i)) != 0) st = 7 - ((penv_xmm->en_sw >> 11) & 7);
/* zero and special are set as valid */ ab_tw = penv_xmm->en_tw;
penv_87->en_tw &= ~(3 << i * 2); tw = 0;
for (i = 0x80; i != 0; i >>= 1) {
sv_87->sv_ac[st] = sv_xmm->sv_fp[st].fp_acc;
tw <<= 2;
if (ab_tw & i) {
/* Non-empty - we need to check ST(i) */
fx_reg = &sv_xmm->sv_fp[st].fp_acc;
/* The first 64 bits contain the mantissa. */
mantissa = *((uint64_t *)fx_reg->fp_bytes);
/*
* The final 16 bits contain the sign bit and the exponent.
* Mask the sign bit since it is of no consequence to these
* tests.
*/
exp = *((uint16_t *)&fx_reg->fp_bytes[8]) & 0x7fff;
if (exp == 0) {
if (mantissa == 0)
tw |= 1; /* Zero */
else
tw |= 2; /* Denormal */
} else if (exp == 0x7fff)
tw |= 2; /* Infinity or NaN */
} else
tw |= 3; /* Empty */
st = (st - 1) & 7;
} }
penv_87->en_tw = tw;
} }
void void