target: i386: Check float overflow about register stack
PATCH 1/1 target: i386: Check float overflow about register stack
From: Chen Gang <[email protected]>
The fxam instruction also checks the register stack overflow, which can be get by the following fstsw instruction. The related code is below, it works well under real x86_64 hardware, but can not work under qemu-i386.
0006b63c <_CIsqrt>:
6b63c: 55 push %ebp 6b63d: 89 e5 mov %esp,%ebp 6b63f: 83 ec 44 sub $0x44,%esp 6b642: dd 1c 24 fstpl (%esp) 6b645: 9b fwait 6b646: e8 d5 04 00 00 call 6bb20 <wine_backtrace> 6b64b: b9 01 00 00 00 mov $0x1,%ecx 6b650: d9 e5 fxam 6b652: 9b df e0 fstsw %ax 6b655: 66 25 00 45 and $0x4500,%ax 6b659: 66 3d 00 41 cmp $0x4100,%ax 6b65d: 74 07 je 6b666 <_CIsqrt+0x2a> 6b65f: dd 1c cc fstpl (%esp,%ecx,8) 6b662: 9b fwait 6b663: 41 inc %ecx 6b664: eb ea jmp 6b650 <_CIsqrt+0x14> 6b666: 89 4d fc mov %ecx,-0x4(%ebp) 6b669: e8 b2 0f 00 00 call 6c620 <MSVCRT_sqrt> 6b66e: 8b 4d fc mov -0x4(%ebp),%ecx 6b671: dd 1c 24 fstpl (%esp) 6b674: 49 dec %ecx 6b675: dd 04 cc fldl (%esp,%ecx,8) 6b678: 83 f9 00 cmp $0x0,%ecx 6b67b: 75 f7 jne 6b674 <_CIsqrt+0x38> 6b67d: c9 leave 6b67e: c3 ret 6b67f: 90 nop
--- target/i386/cpu.h | 1 + target/i386/fpu_helper.c | 70 ++++++++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 27 deletions(-)
target/i386/cpu.h +1 -0
@@ -1394,6 +1394,7 @@ typedef struct CPUX86State {struct {} start_init_save;/* FPU state */ +bool foverflow;unsigned int fpstt;/* top of stack index */ uint16_t fpus;uint16_t fpuc;
target/i386/fpu_helper.c +43 -27
150 lines not on this page · raw message
Signature · 1 line
2.24.0.308.g228f53135a
Review
Quoted 5 lines · On 21/02/20 04:45, [email protected] wrote:
static inline void
fpush(CPUX86State *env)
{(quoted diff) +1
-2- env->fpstt = (env->fpstt - 1) & 7; - env->fptags[env->fpstt] = 0; /* validate stack entry */ + set_fpstt(env, env->fpstt - 1, false, true);
On overflow fpstt is ~0, so this does:
env->foverflow = true;
env->fpstt = 7;
env->fptags[7] = 0; /* validate stack entry */Is this correct? You are going to set ST0 so the register should not be marked empty.
Review
Quoted 5 lines
static inline void
fpop(CPUX86State *env)
{(quoted diff) +1
-2- env->fptags[env->fpstt] = 1; /* invalidate stack entry */ - env->fpstt = (env->fpstt + 1) & 7; + set_fpstt(env, env->fpstt + 1, true, true);
While here:
env->foverflow = true;
env->fptags[7] = 1;
env->fpstt = 0;Review
Quoted 4 lines
void
helper_fdecstp(CPUX86State *env)
{(quoted diff) +1
-1- env->fpstt = (env->fpstt - 1) & 7; + set_fpstt(env, env->fpstt - 1, false, false);
This is clearing env->foverflow. But after 8 consecutive fdecstp or fincstp the result of FXAM should not change.
Review
Quoted 7 lines
env->fpus &= ~0x4700;}
void
helper_fincstp(CPUX86State *env)
{(quoted diff) +1
-1- env->fpstt = (env->fpstt + 1) & 7; + set_fpstt(env, env->fpstt + 1, true, false);
Same here.
The actual bug is hinted in helper_fxam_ST0:
/* XXX: test fptags too */
I think the correct fix should be something like
target/i386/fpu_helper.c +5 -1
@@ -991,7 +991,11 @@ void helper_fxam_ST0(CPUX86State *env) env->fpus |=0x200 ;/* C1 <-- 1 */ } -/* XXX: test fptags too */ +if (env->fptags[env->fpstt]) { + env->fpus |=0x4100 ;/* Empty */ +return ; + } + expdif =EXPD (temp);if (expdif ==MAXEXPD ) {if (MANTD (temp) ==0x8000000000000000ULL ) {
Paolo
Quoted 7 lines · Paolo Bonzini
Review
Quoted 5 lines · On 21/02/20 04:45, [email protected] wrote:
static inline void
fpush(CPUX86State *env)
{(quoted diff) +1
-2- env->fpstt = (env->fpstt - 1) & 7; - env->fptags[env->fpstt] = 0; /* validate stack entry */ + set_fpstt(env, env->fpstt - 1, false, true);
Quoted 9 lines
On overflow fpstt is ~0, so this does:
env->foverflow = true; env->fpstt = 7; env->fptags[7] = 0; /* validate stack entry */Is this correct? You are going to set ST0 so the register should not be marked empty.
Originally, I wanted to add foverflow to mark the stack overflow only, and kept another things no touch.
But I think what you said above is correct, for me, if fpush/f[i]ld*_STO are overflow, the env->fpstt, env->fpregs and env->fptags should be kept no touch, and foverflow is set true, so there is no negative effect.
Welcome your idea.
Quoted 11 lines
Review
Quoted 4 lines
void
helper_fdecstp(CPUX86State *env)
{(quoted diff) +1
-1- env->fpstt = (env->fpstt - 1) & 7; + set_fpstt(env, env->fpstt - 1, false, false);This is clearing
env->foverflow. But after 8 consecutive fdecstp or fincstp the result of FXAM should not change.Quoted 3 lines
env->fpus &= ~0x4700;}
Quoted 7 lines
Review
Quoted 4 lines
void
helper_fincstp(CPUX86State *env)
{(quoted diff) +1
-1- env->fpstt = (env->fpstt + 1) & 7; + set_fpstt(env, env->fpstt + 1, true, false);Same here.
OK. thanks.
Now if foverflow is only for fpush/f[i]ld*_ST0, I guess fincstp/fdecstp can clear foverflow. The env->fptags are only for fpop, which keep no touch in fincstp/fdecstp.
Quoted 11 lines
The actual bug is hinted in
helper_fxam_ST0:/* XXX: test fptags too */I think the correct fix should be something like
target/i386/fpu_helper.c +0
-0
target/i386/fpu_helper.c void helper_fxam_ST0(CPUX86State *env) · line 998
Quoted 9 lines
target/i386/fpu_helper.c +5
-1@@ -991,7 +991,11 @@ void helper_fxam_ST0(CPUX86State *env) env->fpus |=0x200 ;/* C1 <-- 1 */ } -/* XXX: test fptags too */ +if (env->fptags[env->fpstt]) { + env->fpus |=0x4100 ;/* Empty */ +return ; + } +
For fpop overflow, this fix is enough, but for me, we still need foverflow to check fpush/fld*_ST0 overflow.
Don't you think we need check fpush/f[i]ld*_ST0 overflow?
Thanks
Quoted 10 lines · Chen Gang
target/i386/fpu_helper.c void helper_fxam_ST0(CPUX86State *env) · line 998
Quoted 6 lines
target/i386/fpu_helper.c +5
-1-/* XXX: test fptags too */ +if (env->fptags[env->fpstt]) { + env->fpus |=0x4100 ;/* Empty */ +return ; + } +For fpop overflow, this fix is enough, but for me, we still need foverflow to check fpush/fld*_ST0 overflow.
Don't you think we need check fpush/f[i]ld*_ST0 overflow?
After fld/fild or any other push, FXAM ST0 should not return empty in my opinion.
Paolo
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Chen Gang
Quoted 6 lines
target/i386/fpu_helper.c +5
-1-/* XXX: test fptags too */ +if (env->fptags[env->fpstt]) { + env->fpus |=0x4100 ;/* Empty */ +return ; + } +
Quoted 8 lines
Quoted 4 lines
For fpop overflow, this fix is enough, but for me, we still need foverflow to check fpush/fld*_ST0 overflow.
Don't you think we need check fpush/f[i]ld*_ST0 overflow?
After fld/fild or any other push, FXAM ST0 should not return empty in my opinion.
OK, it sounds reasonable.
After check the intel document for f[i]ld* instructions, it says:
"Set C1 to 1 if stack overflow occurred; set to 0 otherwise".
In helper_fxam_ST0, I guess, we need "env->fpus |= 0x200" (but I don't know wheter it will be conflict with SIGND(temp)). And we have to still need foverflow, because all env->fptags being 0 doesn't mean overflow.
Thanks.
Quoted 19 lines · Chen Gang
Quoted 7 lines · Paolo Bonzini
Quoted 6 lines · Chen Gang
Quoted 6 lines
target/i386/fpu_helper.c +5
-1-/* XXX: test fptags too */ +if (env->fptags[env->fpstt]) { + env->fpus |=0x4100 ;/* Empty */ +return ; + } +Quoted 8 lines
Quoted 4 lines
For fpop overflow, this fix is enough, but for me, we still need foverflow to check fpush/fld*_ST0 overflow.
Don't you think we need check fpush/f[i]ld*_ST0 overflow?
After fld/fild or any other push, FXAM ST0 should not return empty in my opinion.
OK, it sounds reasonable.
Quoted 8 lines
After check the intel document for f[i]ld* instructions, it says:
"Set C1 to 1 if stack overflow occurred; set to 0 otherwise".
In
helper_fxam_ST0, I guess, we need "env->fpus|=0x200" (but I don't know wheter it will be conflict with SIGND(temp)). And we have to still need foverflow, because allenv->fptagsbeing 0 doesn't mean overflow.
After read the document more about fstp* and fld* instructions, I find that fstp* are also related with C1, and fld* will generate IS exception when stack underflow or overflow occurred.
It seems more modifications should be done (but they will be several patches).
Thanks.
Quoted 5 lines · Chen Gang
Set C1 to 1 if stack overflow occurred; set to 0 otherwise".
In
helper_fxam_ST0, I guess, we need "env->fpus|=0x200" (but I don't know wheter it will be conflict with SIGND(temp)). And we have to still need foverflow, because allenv->fptagsbeing 0 doesn't mean overflow.
No, you need to add "env->fpus |= 0x200" and "env->fpus &= ~0x200" directly to fpush, fpop, etc.
Polo
Quoted 10 lines · Paolo Bonzini
Quoted 5 lines · Chen Gang
Set C1 to 1 if stack overflow occurred; set to 0 otherwise".
In
helper_fxam_ST0, I guess, we need "env->fpus|=0x200" (but I don't know wheter it will be conflict with SIGND(temp)). And we have to still need foverflow, because allenv->fptagsbeing 0 doesn't mean overflow.No, you need to add "
env->fpus|=0x200" and "env->fpus&= ~0x200" directly to fpush, fpop, etc.
OK. The content below is my next TODO, welcome your opinions.
When overflow occurs, for me, we need keep everything no touch except set C1 flag. In fxam, we don't clear C1, but keep no touch for clearning C1 in another places.
When underflow occurs, for me, we need keep everything no touch except set env->fpstt 8, so the next consecutive fpop/f[i]stp* can be checked easier, and the next fpush/f[i]ld* can work well in normal.
For fxam, we check env->fpstt == 8 and env->fptags for empty. And when env->fpstt is 8, it need be set 7 before used in fincstp and ffree_STN.
Thanks.
Quoted 8 lines · Chen Gang
Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Chen Gang
Set C1 to 1 if stack overflow occurred; set to 0 otherwise".
In
helper_fxam_ST0, I guess, we need "env->fpus|=0x200" (but I don't know wheter it will be conflict with SIGND(temp)). And we have to still need foverflow, because allenv->fptagsbeing 0 doesn't mean overflow.
Quoted 8 lines
Quoted 3 lines
No, you need to add "
env->fpus|=0x200" and "env->fpus&= ~0x200" directly to fpush, fpop, etc.OK. The content below is my next TODO, welcome your opinions.
When overflow occurs, for me, we need keep everything no touch except set C1 flag.
No, push will overwrite the top entry if there is overflow.
Quoted 2 lines
In fxam, we don't clear C1, but keep no touch for clearning C1 in another places.
FXAM is neither push nor pop, it just detects an empty slot via fptags.
FXAM should be okay with my patch.
Quoted 5 lines
When underflow occurs, for me, we need keep everything no touch except set
env->fpstt8, so the next consecutive fpop/f[i]stp* can be checked easier, and the next fpush/f[i]ld* can work well in normal. For fxam, we checkenv->fpstt== 8 andenv->fptagsfor empty. And whenenv->fpsttis 8, it need be set 7 before used in fincstp andffree_STN.
I don't think you need env->fpstt to be set to 8 in any case. Also, pop must mark ST(0) as empty always, even if underflow occurs, and also clear C1 if underflow occurs.
Paolo
Quoted 18 lines · Paolo Bonzini
Quoted 8 lines · Chen Gang
Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Chen Gang
Set C1 to 1 if stack overflow occurred; set to 0 otherwise".
In
helper_fxam_ST0, I guess, we need "env->fpus|=0x200" (but I don't know wheter it will be conflict with SIGND(temp)). And we have to still need foverflow, because allenv->fptagsbeing 0 doesn't mean overflow.Quoted 8 lines
Quoted 3 lines
No, you need to add "
env->fpus|=0x200" and "env->fpus&= ~0x200" directly to fpush, fpop, etc.OK. The content below is my next TODO, welcome your opinions.
When overflow occurs, for me, we need keep everything no touch except set C1 flag.
Quoted 8 lines
No, push will overwrite the top entry if there is overflow.
Quoted 2 lines
In fxam, we don't clear C1, but keep no touch for clearning C1 in another places.
FXAM is neither push nor pop, it just detects an empty slot via fptags.
FXAM should be okay with my patch.
OK. I am not quite clear about it, but it fixes the current issues at least. Please apply your patch.
Thanks.