target-i386: x87 exception pointers using TCG.
v4 cover target-i386: x87 exception pointers using TCG.
I submit a patch to fix bugs 661696 and 1248376. This is the fourth version of this patch.
As mentioned in a previous email, the patch implements, for TCG, the specifications provided in Intel and AMD programmer's manuals regarding the x87 exception pointers. That is, when executing instructions fstenv/fnstenv, fsave and fxsave the values for the instruction pointer, data pointer and opcode of the last non-control x87 instruction executed, are correctly saved to the specified memory address. When executing instructions fldenv, frstor and fxrstor the values that are going to be considered the instruction pointer, data pointer and opcode of the last non-control x87 instruction are obtained from the specified memory address. I have added an option in the configure script to enable and disable this development (option tcg-exception-pointers). After testing, I found no performance change between having this option activated and having it disabled.
Best regards,
Jaume
configure | 10 ++ linux-user/signal.c | 4 +- target-i386/cpu.h | 29 ++- target-i386/fpu_helper.c | 238 ++++++++++++++++++++----- target-i386/machine.c | 11 +- target-i386/translate.c | 455 ++++++++++++++++++++++++++++++++++------------- tests/tcg/test-i386.c | 75 +++++++- 7 files changed, 645 insertions(+), 177 deletions(-)
Signature · 1 line
2.1.0
PATCH v4 1/3 target-i386: x87 exception pointers using TCG.
This adds new fields in the CPUX86State struct to store the x87 exception pointers.
Also it adds a new enum type that encodes the operand size and the processor operating mode (protected and real mode).
The patch also adds a new option (tcg-exception-pointers) in the
configure script to enable or disable the functionality
implemented in this development.
--- configure | 10 ++++++++++ target-i386/cpu.h | 25 +++++++++++++++++++++++-- target-i386/machine.c | 11 ++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-)
configure +10 -0
@@ -264,6 +264,7 @@ debug_tcg="no" debug="no" strip_opt="yes" tcg_interpreter="no" +tcg_exception_pointers="no" bigendian="no" mingw32="no" gcov="no"
@@ -926,6 +927,10 @@ for opt do ;; --enable-tcg-interpreter) tcg_interpreter="yes" ;; + --disable-tcg-exception-pointers) tcg_exception_pointers="no" + ;; + --enable-tcg-exception-pointers) tcg_exception_pointers="yes" + ;; --disable-cap-ng) cap_ng="no" ;; --enable-cap-ng) cap_ng="yes"
@@ -1321,6 +1326,7 @@ Advanced options (experts only): --disable-rdma disable RDMA-based migration support --enable-rdma enable RDMA-based migration support --enable-tcg-interpreter enable TCG with bytecode interpreter (TCI) + --enable-tcg-exception-pointers enable exception pointers support for TCG based emmulation --enable-system enable all system emulation targets --disable-system disable all system emulation targets --enable-user enable supported user emulation targets
@@ -4296,6 +4302,7 @@ echo "Install blobs $blobs" echo "KVM support $kvm" echo "RDMA support $rdma" echo "TCG interpreter $tcg_interpreter" +echo "TCG exception pointers $tcg_exception_pointers" echo "fdt support $fdt" echo "preadv support $preadv" echo "fdatasync $fdatasync"
@@ -4651,6 +4658,9 @@ fi if test "$tcg_interpreter" = "yes" ; then echo "CONFIG_TCG_INTERPRETER=y" >> $config_host_mak fi +if test "$tcg_exception_pointers" = "yes" ; then + echo "CONFIG_TCG_EXCEPTION_POINTERS=y" >> $config_host_mak +fi if test "$fdatasync" = "yes" ; then echo "CONFIG_FDATASYNC=y" >> $config_host_mak fi
target-i386/cpu.h +23 -2
@@ -697,6 +697,24 @@ typedef enum {CC_OP_NB , } CCOp; +typedef enum { +B8 =0 , +B16 =1 , +B32 =2 , +B64 =3 , +BSIZE =3 ,/* Mask for the above. */ + +RM =4 , +PM =8 , + +PM32 =PM |B32 , +PM16 =PM |B16 , +RM32 =RM |B32 , +RM16 =RM |B16 , +}OMode ; + +#define TO_OMODE (bsize, is_pm) ((bsize & BSIZE) | (1 << (is_pm ? 3 : 2))) + typedef struct SegmentCache { uint32_t selector; target_ulong base;
@@ -876,10 +894,13 @@ typedef struct CPUX86State {uint16_t fpuc;uint8_t fptags[8 ];/* 0 = valid, 1 = empty */ FPReg fpregs[8 ]; -/* KVM-only so far */ -uint16_t fpop; +uint32_t fpop;uint64_t fpip;uint64_t fpdp; +#ifdef CONFIG_TCG_EXCEPTION_POINTERS +uint32_t fpcs; +uint32_t fpds; +#endif /* emulator internal variables */ float_status fp_status;
target-i386/machine.c +10 -1
@@ -427,7 +427,12 @@ static bool fpop_ip_dp_needed(void *opaque)X86CPU *cpu = opaque;CPUX86State *env = &cpu->env; +#ifdef CONFIG_TCG_EXCEPTION_POINTERS +return env->fpop !=0 || env->fpip !=0 || env->fpdp !=0 + || env->fpcs !=0 || env->fpds !=0 ; +#else return env->fpop !=0 || env->fpip !=0 || env->fpdp !=0 ; +#endif }static const VMStateDescription vmstate_fpop_ip_dp = {
@@ -435,9 +440,13 @@ static const VMStateDescription vmstate_fpop_ip_dp = { .version_id = 1, .minimum_version_id = 1, .fields = (VMStateField []) { -VMSTATE_UINT16 (env.fpop,X86CPU ), +VMSTATE_UINT32 (env.fpop,X86CPU ),VMSTATE_UINT64 (env.fpip,X86CPU ),VMSTATE_UINT64 (env.fpdp,X86CPU ), +#ifdef CONFIG_TCG_EXCEPTION_POINTERS +VMSTATE_UINT32 (env.fpcs,X86CPU ), +VMSTATE_UINT32 (env.fpds,X86CPU ), +#endifVMSTATE_END_OF_LIST () } };
Signature · 1 line
2.1.0
PATCH v4 2/3 target-i386: x87 exception pointers using TCG.
In reply to Jaume Marti FarriolThis modifies the helpers for instructions fstenv/fnstenv, fsave, fxsave, fldenv, frstor and fxrstor, to store to memory and restore from memory the x87 exception pointers.
Also it add logic that efficiently stores the values of the exception pointers, to be used in the instructions mentioned above.
--- linux-user/signal.c | 4 +- target-i386/cpu.h | 4 +- target-i386/fpu_helper.c | 238 ++++++++++++++++++++----- target-i386/translate.c | 455 ++++++++++++++++++++++++++++++++++------------- 4 files changed, 533 insertions(+), 168 deletions(-)
linux-user/signal.c +2 -2
@@ -875,7 +875,7 @@ static void setup_sigcontext(struct target_sigcontext *sc,__put_user (env->regs[R_ESP ], &sc->esp_at_signal);__put_user (env->segs[R_SS ].selector, (unsigned int *)&sc->ss); -cpu_x86_fsave (env, fpstate_addr,1 ); +cpu_x86_fsave (env, fpstate_addr); fpstate->status = fpstate->sw; magic =0xffff ;__put_user (magic, &fpstate->magic);
@@ -1078,7 +1078,7 @@ restore_sigcontext(CPUX86State *env, struct target_sigcontext *sc, int *peax)if (!access_ok (VERIFY_READ , fpstate_addr,sizeof (struct target_fpstate ))) goto badframe; -cpu_x86_frstor (env, fpstate_addr,1 ); +cpu_x86_frstor (env, fpstate_addr); } *peax =tswapl (sc->eax);
target-i386/cpu.h +2 -2
@@ -1151,8 +1151,8 @@ floatx80 cpu_set_fp80(uint64_t mant, uint16_t upper);/* the following helpers are only usable in user mode simulation as they can trigger unexpected exceptions */ void cpu_x86_load_seg (CPUX86State *s,int seg_reg,int selector); -void cpu_x86_fsave (CPUX86State *s,target_ulong ptr,int data32); -void cpu_x86_frstor (CPUX86State *s,target_ulong ptr,int data32); +void cpu_x86_fsave (CPUX86State *s,target_ulong ptr); +void cpu_x86_frstor (CPUX86State *s,target_ulong ptr);/* you can call this signal handler from your SIGBUS and SIGSEGV signal handlers to inform the virtual CPU of exceptions. non zero
target-i386/fpu_helper.c +198 -40
371 lines not on this page · raw message
target-i386/translate.c +331 -124
1,371 lines not on this page · raw message
Signature · 1 line
2.1.0
PATCH v4 3/3 target-i386: x87 exception pointers using TCG.
In reply to Jaume Marti FarriolThis adds tests for the x87 exception pointers.
--- tests/tcg/test-i386.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 6 deletions(-)
tests/tcg/test-i386.c +69 -6
@@ -2327,12 +2327,22 @@ struct fpxstate {uint16_t fpus;uint16_t fptag;uint16_t fop; -uint32_t fpuip; -uint16_t cs_sel; -uint16_t dummy0; -uint32_t fpudp; -uint16_t ds_sel; -uint16_t dummy1; +union { +struct { +uint32_t fpuip; +uint16_t cs_sel; +uint16_t dummy0; + } mode_non_64_ins; +uint64_t mode_64_fpuip; + }; +union { +struct { +uint32_t fpudp; +uint16_t ds_sel; +uint16_t dummy1; + } mode_non_64_data; +uint64_t mode_64_fpudp; + };uint32_t mxcsr;uint32_t mxcsr_mask;uint8_t fpregs1[8 *16 ];
@@ -2342,6 +2352,7 @@ struct fpxstate { static struct fpxstate fpx_state __attribute__((aligned(16))); static struct fpxstate fpx_state2 __attribute__((aligned(16))); +float fxsave_mem_operand;void test_fxsave (void ) {
@@ -2362,6 +2373,7 @@ void test_fxsave(void)" fld1\n" " fldpi\n" " fldln2\n" +" fmul fxsave_mem_operand\n" " fxsave %0\n" " fxrstor %0\n" " fxsave %1\n"
@@ -2371,6 +2383,20 @@ void test_fxsave(void)printf ("fpuc=%04x\n" , fp->fpuc);printf ("fpus=%04x\n" , fp->fpus);printf ("fptag=%04x\n" , fp->fptag); +#ifdef CONFIG_TCG_EXCEPTION_POINTERS +#if defined(__x86_64__) +printf ("fpuip=%04llx\n" , (long long unsigned int ) fp->mode_64_fpuip); +#else +printf ("fpuip=%04x\n" , fp->mode_non_64_ins.fpuip); +printf ("cs_sel=%04x\n" , fp->mode_non_64_ins.cs_sel); +#endif +#if defined(__x86_64__) +printf ("fpudp=%04llx\n" , (long long unsigned int ) fp->mode_64_fpudp); +#else +printf ("fpudp=%04x\n" , fp->mode_non_64_data.fpudp); +printf ("ds_sel=%04x\n" , fp->mode_non_64_data.ds_sel); +#endif +#endif for (i =0 ; i <3 ; i++) { printf("ST%d: " FMT64X " %04x\n", i,
@@ -2687,6 +2713,42 @@ void test_sse(void) printf("%-10s A=" FMTLX " R=" FMTLX ":" FMTLX "\n", #op, a, r, rh); \ } +struct fnstate { +uint16_t fpuc; +uint16_t dummy0; +uint16_t fpus; +uint16_t dummy1; +uint16_t fptag; +uint16_t dummy2; +uint32_t fpuip; +uint16_t cs_sel; +uint16_t fop; +uint32_t fpudp; +uint16_t ds_sel; +}; + +static struct fnstate fn_state __attribute__((aligned(16))); +float fnstenv_mem_operand; + +void test_fnstenv (void ) +{ +struct fnstate *fp = &fn_state; + + asm(" fld1\n" +" fmul fnstenv_mem_operand\n" +" fnstenv %0\n" + :"=m" (*(uint32_t *)fp) : ); +printf ("fpuc=%04x\n" , fp->fpuc); +printf ("fpus=%04x\n" , fp->fpus); +printf ("fptag=%04x\n" , fp->fptag); +#ifdef CONFIG_TCG_EXCEPTION_POINTERS +printf ("fpuip=%04x\n" , fp->fpuip); +printf ("cs_sel=%04x\n" , fp->cs_sel); +printf ("fpudp=%04x\n" , fp->fpudp); +printf ("ds_sel=%04x\n" , fp->ds_sel); +#endif +} +void test_conv (void ) {TEST_CONV_RAX (cbw);
@@ -2757,6 +2819,7 @@ int main(int argc, char **argv)#endif test_enter ();test_conv (); +test_fnstenv ();#ifdef TEST_SSE test_sse ();test_fxsave ();
Signature · 1 line
2.1.0
Hello,
I bump this set of patches to the top, to make sure they are not lost. I would appreciate if someone could take a look at them and provide some feedback.
Many thanks in advance.
Best regards,
Jaume
Quoted 133 lines · Jaume Marti Farriol
This adds tests for the x87 exception pointers.
Signed-off-by [email protected]--- tests/tcg/test-i386.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 6 deletions(-)tests/tcg/test-i386.c +69
-7@@ -2327,12 +2327,22 @@ struct fpxstate {uint16_t fpus;uint16_t fptag;uint16_t fop; -uint32_t fpuip; -uint16_t cs_sel; -uint16_t dummy0; -uint32_t fpudp; -uint16_t ds_sel; -uint16_t dummy1; +union { +struct { +uint32_t fpuip; +uint16_t cs_sel; +uint16_t dummy0; + } mode_non_64_ins; +uint64_t mode_64_fpuip; + }; +union { +struct { +uint32_t fpudp; +uint16_t ds_sel; +uint16_t dummy1; + } mode_non_64_data; +uint64_t mode_64_fpudp; + };uint32_t mxcsr;uint32_t mxcsr_mask;uint8_t fpregs1[8 *16 ];@@ -2342,6 +2352,7 @@ struct fpxstate { static struct fpxstate fpx_state __attribute__((aligned(16))); static struct fpxstate fpx_state2 __attribute__((aligned(16))); +float fxsave_mem_operand;void test_fxsave (void ) {@@ -2362,6 +2373,7 @@ void test_fxsave(void)" fld1\n" " fldpi\n" " fldln2\n" +" fmul fxsave_mem_operand\n" " fxsave %0\n" " fxrstor %0\n" " fxsave %1\n" @@ -2371,6 +2383,20 @@ void test_fxsave(void)printf ("fpuc=%04x\n" , fp->fpuc);printf ("fpus=%04x\n" , fp->fpus);printf ("fptag=%04x\n" , fp->fptag); +#ifdef CONFIG_TCG_EXCEPTION_POINTERS +#if defined(__x86_64__) +printf ("fpuip=%04llx\n" , (long long unsigned int ) fp->mode_64_fpuip); +#else +printf ("fpuip=%04x\n" , fp->mode_non_64_ins.fpuip); +printf ("cs_sel=%04x\n" , fp->mode_non_64_ins.cs_sel); +#endif +#if defined(__x86_64__) +printf ("fpudp=%04llx\n" , (long long unsigned int ) fp->mode_64_fpudp); +#else +printf ("fpudp=%04x\n" , fp->mode_non_64_data.fpudp); +printf ("ds_sel=%04x\n" , fp->mode_non_64_data.ds_sel); +#endif +#endif for (i =0 ; i <3 ; i++) { printf("ST%d: " FMT64X " %04x\n", i,@@ -2687,6 +2713,42 @@ void test_sse(void) printf("%-10s A=" FMTLX " R=" FMTLX ":" FMTLX "\n", #op, a, r, rh); \ } +struct fnstate { +uint16_t fpuc; +uint16_t dummy0; +uint16_t fpus; +uint16_t dummy1; +uint16_t fptag; +uint16_t dummy2; +uint32_t fpuip; +uint16_t cs_sel; +uint16_t fop; +uint32_t fpudp; +uint16_t ds_sel; +}; + +static struct fnstate fn_state __attribute__((aligned(16))); +float fnstenv_mem_operand; + +void test_fnstenv (void ) +{ +struct fnstate *fp = &fn_state; + + asm(" fld1\n" +" fmul fnstenv_mem_operand\n" +" fnstenv %0\n" + :"=m" (*(uint32_t *)fp) : ); +printf ("fpuc=%04x\n" , fp->fpuc); +printf ("fpus=%04x\n" , fp->fpus); +printf ("fptag=%04x\n" , fp->fptag); +#ifdef CONFIG_TCG_EXCEPTION_POINTERS +printf ("fpuip=%04x\n" , fp->fpuip); +printf ("cs_sel=%04x\n" , fp->cs_sel); +printf ("fpudp=%04x\n" , fp->fpudp); +printf ("ds_sel=%04x\n" , fp->ds_sel); +#endif +} +void test_conv (void ) {TEST_CONV_RAX (cbw);@@ -2757,6 +2819,7 @@ int main(int argc, char **argv)#endif test_enter ();test_conv (); +test_fnstenv ();#ifdef TEST_SSE test_sse ();test_fxsave (); --2.1.0
Signature · 1 line
Jaume