Skip to contentSkip to conversation

target/m68k: Map FPU exceptions to FPSR register

PATCH In review Keith Packard via 2 participants 3 messages Aug 3, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/m68k: Map FPU exceptions to FPSR register

Add helpers for reading/writing the 68881 FPSR register so that changes in floating point exception state can be seen by the application.

---
 target/m68k/cpu.h        |  2 ++
 target/m68k/fpu_helper.c | 72 ++++++++++++++++++++++++++++++++++++++++
 target/m68k/helper.c     |  4 +--
 target/m68k/helper.h     |  2 ++
 target/m68k/translate.c  |  4 +--
 5 files changed, 80 insertions(+), 4 deletions(-)
target/m68k/cpu.h +2 -0
index b741c50a8f..1e491fda77 100644
@@ -173,6 +173,8 @@ struct ArchCPU {
     CPUM68KState env;
 };
 
+uint32_t cpu_m68k_get_fpsr(CPUM68KState *env);
+void cpu_m68k_set_fpsr(CPUM68KState *env, uint32_t val);
 
 #ifndef CONFIG_USER_ONLY
 void m68k_cpu_do_interrupt(CPUState *cpu);
target/m68k/fpu_helper.c +72 -0

78 lines not on this page · raw message

target/m68k/helper.c +2 -2
index 0a1544cd68..beab4b96bc 100644
@@ -118,7 +118,7 @@ static int m68k_fpu_gdb_get_reg(CPUM68KState *env, GByteArray *mem_buf, int n)
     case 8: /* fpcontrol */
         return gdb_get_reg32(mem_buf, env->fpcr);
     case 9: /* fpstatus */
-        return gdb_get_reg32(mem_buf, env->fpsr);
+        return gdb_get_reg32(mem_buf, cpu_m68k_get_fpsr(env));
     case 10: /* fpiar, not implemented */
         return gdb_get_reg32(mem_buf, 0);
     }
@@ -137,7 +137,7 @@ static int m68k_fpu_gdb_set_reg(CPUM68KState *env, uint8_t *mem_buf, int n)
         cpu_m68k_set_fpcr(env, ldl_p(mem_buf));
         return 4;
     case 9: /* fpstatus */
-        env->fpsr = ldl_p(mem_buf);
+        cpu_m68k_set_fpsr(env, ldl_p(mem_buf));
         return 4;
     case 10: /* fpiar, not implemented */
         return 4;
target/m68k/helper.h +2 -0
index 2bbe0dc032..95aa5e53bb 100644
@@ -54,6 +54,8 @@ DEF_HELPER_4(fsdiv, void, env, fp, fp, fp)
 DEF_HELPER_4(fddiv, void, env, fp, fp, fp)
 DEF_HELPER_4(fsgldiv, void, env, fp, fp, fp)
 DEF_HELPER_FLAGS_3(fcmp, TCG_CALL_NO_RWG, void, env, fp, fp)
+DEF_HELPER_2(set_fpsr, void, env, i32)
+DEF_HELPER_1(get_fpsr, i32, env)
 DEF_HELPER_FLAGS_2(set_fpcr, TCG_CALL_NO_RWG, void, env, i32)
 DEF_HELPER_FLAGS_2(ftst, TCG_CALL_NO_RWG, void, env, fp)
 DEF_HELPER_3(fconst, void, env, fp, i32)
target/m68k/translate.c +2 -2
index d037c57453..360d054162 100644
@@ -4718,7 +4718,7 @@ static void gen_load_fcr(DisasContext *s, TCGv res, int reg)
         tcg_gen_movi_i32(res, 0);
         break;
     case M68K_FPSR:
-        tcg_gen_ld_i32(res, cpu_env, offsetof(CPUM68KState, fpsr));
+        gen_helper_get_fpsr(res, cpu_env);
         break;
     case M68K_FPCR:
         tcg_gen_ld_i32(res, cpu_env, offsetof(CPUM68KState, fpcr));
@@ -4732,7 +4732,7 @@ static void gen_store_fcr(DisasContext *s, TCGv val, int reg)
     case M68K_FPIAR:
         break;
     case M68K_FPSR:
-        tcg_gen_st_i32(val, cpu_env, offsetof(CPUM68KState, fpsr));
+        gen_helper_set_fpsr(cpu_env, val);
         break;
     case M68K_FPCR:
         gen_helper_set_fpcr(cpu_env, val);
Signature · 1 line
2.40.1
#1 lore raw
Quoted 4 lines · Keith Packard via

Add helpers for reading/writing the 68881 FPSR register so that changes in floating point exception state can be seen by the application.

Quoted 8 lines
---
  target/m68k/cpu.h        |  2 ++
  target/m68k/fpu_helper.c | 72 ++++++++++++++++++++++++++++++++++++++++
  target/m68k/helper.c     |  4 +--
  target/m68k/helper.h     |  2 ++
  target/m68k/translate.c  |  4 +--
  5 files changed, 80 insertions(+), 4 deletions(-)

Good catch. Mostly ok.

Quoted 1 line

+static inline int cpu_m68k_exceptbits_from_host(int host_bits)

...

Quoted 1 line

+static inline int cpu_m68k_exceptbits_to_host(int target_bits)

No need for inline markers.

target/m68k/fpu_helper.c void HELPER(set_fpcr)(CPUM68KState *env, uint32_t val) · line 224

Quoted 12 lines
target/m68k/fpu_helper.c +12 -0
+uint32_t HELPER(get_fpsr)(CPUM68KState *env)
+{
+    int host_flags = get_float_exception_flags(&env->fp_status);
+    int target_flags = cpu_m68k_exceptbits_from_host(host_flags);
+    int except = (env->fpsr & ~(0xf8)) | target_flags;
+    return except;
+}
+
+uint32_t cpu_m68k_get_fpsr(CPUM68KState *env)
+{
+    return HELPER(get_fpsr)(env);
+}

In general it is bad form to call HELPER(foo) directly.
In this case it doesn't hurt, but better form to reverse the implementations.

target/m68k/fpu_helper.c void HELPER(set_fpcr)(CPUM68KState *env, uint32_t val) · line 237

Quoted 12 lines
target/m68k/fpu_helper.c +12 -0
+void HELPER(set_fpsr)(CPUM68KState *env, uint32_t val)
+{
+    env->fpsr = val;
+
+    int host_flags = cpu_m68k_exceptbits_to_host((int) env->fpsr);
+    set_float_exception_flags(host_flags, &env->fp_status);
+}
+
+void cpu_m68k_set_fpsr(CPUM68KState *env, uint32_t val)
+{
+    return HELPER(set_fpsr)(env, val);
+}

Likewise.

What's missing is an update to vmstate, to make sure all the architectural bits are properly saved. Add

    env->fpsr = cpu_m68k_get_fpsr(env);

to a (new) fpu_pre_save and

    cpu_m68k_set_fpsr(env, env->fpsr);

to fpu_post_load in target/m68k/cpu.c.

r~

#2 lore raw
Quoted 2 lines

Good catch. Mostly ok.

Thanks much for looking at this.

Quoted 1 line

No need for inline markers.

Thanks.

Quoted 2 lines

In general it is bad form to call HELPER(foo) directly. In this case it doesn't hurt, but better form to reverse the implementations.

Good point. I had copied this from the arm vfp code which flipped the order. I've flipped those around.

Quoted 2 lines

What's missing is an update to vmstate, to make sure all the architectural bits are properly saved. Add

Yup, thanks. I've sent another version of the patch along.

Signature · 1 line
-keith