Skip to contentSkip to conversation

target/m68k: Conditional traps + trap cleanup

PATCH Superseded Richard Henderson 3 participants 39 messages Apr 30, 2022 – May 27, 2022

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover target/m68k: Conditional traps + trap cleanup

Changes for v4:
- Rebase, which requires QEMU_NORETURN -> G_NORETURN.
- Cast -4096 to abi_ulong for print_syscall_err.

r~

v1: https://lore.kernel.org/qemu-devel/[email protected]/
v2: https://lore.kernel.org/qemu-devel/[email protected]/
v3: https://lore.kernel.org/qemu-devel/[email protected]/

Richard Henderson (17):
target/m68k: Raise the TRAPn exception with the correct pc target/m68k: Switch over exception type in m68k_interrupt_all target/m68k: Fix coding style in m68k_interrupt_all linux-user/m68k: Handle EXCP_TRAP1 through EXCP_TRAP15 target/m68k: Remove retaddr in m68k_interrupt_all target/m68k: Fix address argument for EXCP_CHK target/m68k: Fix pc, c flag, and address argument for EXCP_DIV0 target/m68k: Fix address argument for EXCP_TRACE target/m68k: Fix stack frame for EXCP_ILLEGAL target/m68k: Implement TRAPcc
target/m68k: Implement TPF in terms of TRAPcc target/m68k: Implement TRAPV
target/m68k: Implement FTRAPcc
tests/tcg/m68k: Add trap.c
linux-user/strace: Fix print_syscall_err linux-user/strace: Adjust get_thread_area for m68k target/m68k: Mark helper_raise_exception as noreturn

 target/m68k/cpu.h              |   8 ++
 target/m68k/helper.h           |  14 +--
 linux-user/m68k/cpu_loop.c     |  11 +-
 linux-user/strace.c            |   4 +-
 target/m68k/cpu.c              |   1 +
 target/m68k/op_helper.c        | 173 ++++++++++++++++--------------
 target/m68k/translate.c        | 190 ++++++++++++++++++++++++---------
 tests/tcg/m68k/trap.c          | 129 ++++++++++++++++++++++
 linux-user/strace.list         |   5 +
 tests/tcg/m68k/Makefile.target |   3 +
 10 files changed, 394 insertions(+), 144 deletions(-)
 create mode 100644 tests/tcg/m68k/trap.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/17 target/m68k: Raise the TRAPn exception with the correct pc

Rather than adjust the PC in all of the consumers, raise the exception with the correct PC in the first place.

---
 linux-user/m68k/cpu_loop.c | 1 -
 target/m68k/op_helper.c    | 9 ---------
 target/m68k/translate.c    | 2 +-
 3 files changed, 1 insertion(+), 11 deletions(-)
linux-user/m68k/cpu_loop.c +0 -1
index d1bf8548b7..56417f7401 100644
@@ -56,7 +56,6 @@ void cpu_loop(CPUM68KState *env)
             {
                 abi_long ret;
                 n = env->dregs[0];
-                env->pc += 2;
                 ret = do_syscall(env,
                                  n,
                                  env->dregs[1],
target/m68k/op_helper.c +0 -9
index 8decc61240..d30f988ae0 100644
@@ -217,11 +217,6 @@ static void cf_interrupt_all(CPUM68KState *env, int is_hw)
             cpu_loop_exit(cs);
             return;
         }
-        if (cs->exception_index >= EXCP_TRAP0
-            && cs->exception_index <= EXCP_TRAP15) {
-            /* Move the PC after the trap instruction.  */
-            retaddr += 2;
-        }
     }
 
     vector = cs->exception_index << 2;
@@ -304,10 +299,6 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
             /* Return from an exception.  */
             m68k_rte(env);
             return;
-        case EXCP_TRAP0 ...  EXCP_TRAP15:
-            /* Move the PC after the trap instruction.  */
-            retaddr += 2;
-            break;
         }
     }
 
target/m68k/translate.c +1 -1
index 4026572ed8..6d6d026e3c 100644
@@ -4860,7 +4860,7 @@ DISAS_INSN(wdebug)
 
 DISAS_INSN(trap)
 {
-    gen_exception(s, s->base.pc_next, EXCP_TRAP0 + (insn & 0xf));
+    gen_exception(s, s->pc, EXCP_TRAP0 + (insn & 0xf));
 }
 
 static void gen_load_fcr(DisasContext *s, TCGv res, int reg)
Signature · 1 line
2.34.1
#2 lore raw

PATCH v4 2/17 target/m68k: Switch over exception type in m68k_interrupt_all

In reply to Richard Henderson

Replace an if ladder with a switch for clarity.

---
 target/m68k/op_helper.c | 49 +++++++++++++++++++++++++----------------
 1 file changed, 30 insertions(+), 19 deletions(-)
target/m68k/op_helper.c +30 -19
index d30f988ae0..2b94a6ec84 100644
@@ -333,7 +333,8 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
         sp &= ~1;
     }
 
-    if (cs->exception_index == EXCP_ACCESS) {
+    switch (cs->exception_index) {
+    case EXCP_ACCESS:
         if (env->mmu.fault) {
             cpu_abort(cs, "DOUBLE MMU FAULT\n");
         }
@@ -391,29 +392,39 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
                      "ssw:  %08x ea:   %08x sfc:  %d    dfc: %d\n",
                      env->mmu.ssw, env->mmu.ar, env->sfc, env->dfc);
         }
-    } else if (cs->exception_index == EXCP_ADDRESS) {
+        break;
+
+    case EXCP_ADDRESS:
         do_stack_frame(env, &sp, 2, oldsr, 0, retaddr);
-    } else if (cs->exception_index == EXCP_ILLEGAL ||
-               cs->exception_index == EXCP_DIV0 ||
-               cs->exception_index == EXCP_CHK ||
-               cs->exception_index == EXCP_TRAPCC ||
-               cs->exception_index == EXCP_TRACE) {
+        break;
+
+    case EXCP_ILLEGAL:
+    case EXCP_DIV0:
+    case EXCP_CHK:
+    case EXCP_TRAPCC:
+    case EXCP_TRACE:
         /* FIXME: addr is not only env->pc */
         do_stack_frame(env, &sp, 2, oldsr, env->pc, retaddr);
-    } else if (is_hw && oldsr & SR_M &&
-               cs->exception_index >= EXCP_SPURIOUS &&
-               cs->exception_index <= EXCP_INT_LEVEL_7) {
-        do_stack_frame(env, &sp, 0, oldsr, 0, retaddr);
-        oldsr = sr;
-        env->aregs[7] = sp;
-        cpu_m68k_set_sr(env, sr &= ~SR_M);
-        sp = env->aregs[7];
-        if (!m68k_feature(env, M68K_FEATURE_UNALIGNED_DATA)) {
-            sp &= ~1;
+        break;
+
+    case EXCP_SPURIOUS ... EXCP_INT_LEVEL_7:
+        if (is_hw && oldsr & SR_M) {
+            do_stack_frame(env, &sp, 0, oldsr, 0, retaddr);
+            oldsr = sr;
+            env->aregs[7] = sp;
+            cpu_m68k_set_sr(env, sr &= ~SR_M);
+            sp = env->aregs[7];
+            if (!m68k_feature(env, M68K_FEATURE_UNALIGNED_DATA)) {
+                sp &= ~1;
+            }
+            do_stack_frame(env, &sp, 1, oldsr, 0, retaddr);
+            break;
         }
-        do_stack_frame(env, &sp, 1, oldsr, 0, retaddr);
-    } else {
+        /* fall through */
+
+    default:
         do_stack_frame(env, &sp, 0, oldsr, 0, retaddr);
+        break;
     }
 
     env->aregs[7] = sp;
Signature · 1 line
2.34.1
#3 lore raw

PATCH v4 3/17 target/m68k: Fix coding style in m68k_interrupt_all

In reply to Richard Henderson

Add parenthesis around & vs &&.

Remove assignment to sr in function call argument -- note that sr is unused after the call, so the assignment was never needed, only the result of the & expression.

---
 target/m68k/op_helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/m68k/op_helper.c +2 -2
index 2b94a6ec84..0f41c2dce3 100644
@@ -408,11 +408,11 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
         break;
 
     case EXCP_SPURIOUS ... EXCP_INT_LEVEL_7:
-        if (is_hw && oldsr & SR_M) {
+        if (is_hw && (oldsr & SR_M)) {
             do_stack_frame(env, &sp, 0, oldsr, 0, retaddr);
             oldsr = sr;
             env->aregs[7] = sp;
-            cpu_m68k_set_sr(env, sr &= ~SR_M);
+            cpu_m68k_set_sr(env, sr & ~SR_M);
             sp = env->aregs[7];
             if (!m68k_feature(env, M68K_FEATURE_UNALIGNED_DATA)) {
                 sp &= ~1;
Signature · 1 line
2.34.1
Reviewed-by Philippe Mathieu-Daudé on v4 3/17
#4 lore raw

Re: [PATCH v4 03/17] target/m68k: Fix coding style in m68k_interrupt_all

Quoted 11 lines · Richard Henderson

Add parenthesis around & vs &&.

Remove assignment to sr in function call argument -- note that sr is unused after the call, so the assignment was never needed, only the result of the & expression.

---
  target/m68k/op_helper.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Reviewed-by on v4 3/17
#5 lore raw

Re: [PATCH v4 03/17] target/m68k: Fix coding style in m68k_interrupt_all

In reply to Richard Henderson

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 20 lines

Add parenthesis around & vs &&.

Remove assignment to sr in function call argument -- note that sr is unused after the call, so the assignment was never needed, only the result of the & expression.

---
  target/m68k/op_helper.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
target/m68k/op_helper.c +0 -0
index 2b94a6ec84..0f41c2dce3 100644
@@ -408,11 +408,11 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
          break;
 
      case EXCP_SPURIOUS ... EXCP_INT_LEVEL_7:

target/m68k/op_helper.c static void m68k_interrupt_all(CPUM68KState *env, int is_hw) · line 418

Quoted 10 lines
target/m68k/op_helper.c +2 -2
-        if (is_hw && oldsr & SR_M) {
+        if (is_hw && (oldsr & SR_M)) {
              do_stack_frame(env, &sp, 0, oldsr, 0, retaddr);
              oldsr = sr;
              env->aregs[7] = sp;
-            cpu_m68k_set_sr(env, sr &= ~SR_M);
+            cpu_m68k_set_sr(env, sr & ~SR_M);
              sp = env->aregs[7];
              if (!m68k_feature(env, M68K_FEATURE_UNALIGNED_DATA)) {
                  sp &= ~1;
#6 lore raw

PATCH v4 4/17 linux-user/m68k: Handle EXCP_TRAP1 through EXCP_TRAP15

In reply to Richard Henderson

These are raised by guest instructions, and should not fall through into the default abort case.

---
 linux-user/m68k/cpu_loop.c | 2 ++
 1 file changed, 2 insertions(+)
linux-user/m68k/cpu_loop.c +2 -0
index 56417f7401..6ca3e1e63a 100644
@@ -44,6 +44,7 @@ void cpu_loop(CPUM68KState *env)
         case EXCP_ILLEGAL:
         case EXCP_LINEA:
         case EXCP_LINEF:
+        case EXCP_TRAP0 + 1 ... EXCP_TRAP0 + 14:
             force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPN, env->pc);
             break;
         case EXCP_CHK:
@@ -76,6 +77,7 @@ void cpu_loop(CPUM68KState *env)
             /* just indicate that signals should be handled asap */
             break;
         case EXCP_DEBUG:
+        case EXCP_TRAP15:
             force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc);
             break;
         case EXCP_ATOMIC:
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v4 04/17] linux-user/m68k: Handle EXCP_TRAP1 through EXCP_TRAP15

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 13 lines

These are raised by guest instructions, and should not fall through into the default abort case.

---
  linux-user/m68k/cpu_loop.c | 2 ++
  1 file changed, 2 insertions(+)
linux-user/m68k/cpu_loop.c +0 -0
index 56417f7401..6ca3e1e63a 100644

linux-user/m68k/cpu_loop.c void cpu_loop(CPUM68KState *env) · line 48

Quoted 5 lines
linux-user/m68k/cpu_loop.c +1 -0
@@ -44,6 +44,7 @@ void cpu_loop(CPUM68KState *env)
          case EXCP_ILLEGAL:
          case EXCP_LINEA:
          case EXCP_LINEF:
+        case EXCP_TRAP0 + 1 ... EXCP_TRAP0 + 14:
              force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPN, env->pc);

In kernel, VEC_TRAP1 to VEC_TRAP14 use ILL_ILLTRP for si_code.

Thanks,
Laurent

#8 lore raw

PATCH v4 5/17 target/m68k: Remove retaddr in m68k_interrupt_all

In reply to Richard Henderson

The only value this variable holds is now env->pc.

---
 target/m68k/op_helper.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
target/m68k/op_helper.c +6 -9
index 0f41c2dce3..777869790b 100644
@@ -287,12 +287,9 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
 {
     CPUState *cs = env_cpu(env);
     uint32_t sp;
-    uint32_t retaddr;
     uint32_t vector;
     uint16_t sr, oldsr;
 
-    retaddr = env->pc;
-
     if (!is_hw) {
         switch (cs->exception_index) {
         case EXCP_RTE:
@@ -385,7 +382,7 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
         sp -= 4;
         cpu_stl_mmuidx_ra(env, sp, env->mmu.ar, MMU_KERNEL_IDX, 0);
 
-        do_stack_frame(env, &sp, 7, oldsr, 0, retaddr);
+        do_stack_frame(env, &sp, 7, oldsr, 0, env->pc);
         env->mmu.fault = false;
         if (qemu_loglevel_mask(CPU_LOG_INT)) {
             qemu_log("            "
@@ -395,7 +392,7 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
         break;
 
     case EXCP_ADDRESS:
-        do_stack_frame(env, &sp, 2, oldsr, 0, retaddr);
+        do_stack_frame(env, &sp, 2, oldsr, 0, env->pc);
         break;
 
     case EXCP_ILLEGAL:
@@ -404,12 +401,12 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
     case EXCP_TRAPCC:
     case EXCP_TRACE:
         /* FIXME: addr is not only env->pc */
-        do_stack_frame(env, &sp, 2, oldsr, env->pc, retaddr);
+        do_stack_frame(env, &sp, 2, oldsr, env->pc, env->pc);
         break;
 
     case EXCP_SPURIOUS ... EXCP_INT_LEVEL_7:
         if (is_hw && (oldsr & SR_M)) {
-            do_stack_frame(env, &sp, 0, oldsr, 0, retaddr);
+            do_stack_frame(env, &sp, 0, oldsr, 0, env->pc);
             oldsr = sr;
             env->aregs[7] = sp;
             cpu_m68k_set_sr(env, sr & ~SR_M);
@@ -417,13 +414,13 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
             if (!m68k_feature(env, M68K_FEATURE_UNALIGNED_DATA)) {
                 sp &= ~1;
             }
-            do_stack_frame(env, &sp, 1, oldsr, 0, retaddr);
+            do_stack_frame(env, &sp, 1, oldsr, 0, env->pc);
             break;
         }
         /* fall through */
 
     default:
-        do_stack_frame(env, &sp, 0, oldsr, 0, retaddr);
+        do_stack_frame(env, &sp, 0, oldsr, 0, env->pc);
         break;
     }
 
Signature · 1 line
2.34.1
#9 lore raw

PATCH v4 6/17 target/m68k: Fix address argument for EXCP_CHK

In reply to Richard Henderson

According to the M68040 Users Manual, section 8.4.3, Six word stack frame (format 2), CHK, CHK2 (and others) are supposed to record the next insn in PC and the address of the trapping instruction in ADDRESS.

Create a raise_exception_format2 function to centralize recording of the trapping pc in mmu.ar, plus advancing to the next insn.

Update m68k_interrupt_all to pass mmu.ar to do_stack_frame. Update cpu_loop to pass mmu.ar to siginfo.si_addr, as the kernel does in trap_c().

---
 target/m68k/cpu.h          |  6 +++++
 linux-user/m68k/cpu_loop.c |  2 +-
 target/m68k/op_helper.c    | 54 ++++++++++++++++++++------------------
 3 files changed, 36 insertions(+), 26 deletions(-)
target/m68k/cpu.h +6 -0
index 9b3bf7a448..558c3c67d6 100644
@@ -122,6 +122,12 @@ typedef struct CPUArchState {
 
     /* MMU status.  */
     struct {
+        /*
+         * Holds the "address" value in between raising an exception
+         * and creation of the exception stack frame.
+         * Used for both Format 7 exceptions (Access, i.e. mmu)
+         * and Format 2 exceptions (chk, div0, trapcc, etc).
+         */
         uint32_t ar;
         uint32_t ssw;
         /* 68040 */
linux-user/m68k/cpu_loop.c +1 -1
index 6ca3e1e63a..5d9c1f3753 100644
@@ -48,7 +48,7 @@ void cpu_loop(CPUM68KState *env)
             force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPN, env->pc);
             break;
         case EXCP_CHK:
-            force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTOVF, env->pc);
+            force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTOVF, env->mmu.ar);
             break;
         case EXCP_DIV0:
             force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTDIV, env->pc);
target/m68k/op_helper.c +29 -25

83 lines not on this page · raw message

Signature · 1 line
2.34.1
#10 lore raw

PATCH v4 7/17 target/m68k: Fix pc, c flag, and address argument for EXCP_DIV0

In reply to Richard Henderson

According to the M68040 Users Manual, section 8.4.3, Six word stack frame (format 2), Zero Div (and others) is supposed to record the next insn in PC and the address of the trapping instruction in ADDRESS.

While the N, Z and V flags are documented to be undefine on DIV0, the C flag is documented as always cleared.

Update helper_div* to take the instruction length as an argument and use raise_exception_format2. Hoist the reset of the C flag above the division by zero check.

Update m68k_interrupt_all to pass mmu.ar to do_stack_frame. Update cpu_loop to pass mmu.ar to siginfo.si_addr, as the kernel does in trap_c().

---
 target/m68k/helper.h       | 12 +++++-----
 linux-user/m68k/cpu_loop.c |  2 +-
 target/m68k/op_helper.c    | 48 +++++++++++++++++++++++---------------
 target/m68k/translate.c    | 33 +++++++++++++-------------
 4 files changed, 52 insertions(+), 43 deletions(-)
target/m68k/helper.h +6 -6
index 0a6b4146f6..f016c4c1c2 100644
@@ -1,12 +1,12 @@
 DEF_HELPER_1(bitrev, i32, i32)
 DEF_HELPER_1(ff1, i32, i32)
 DEF_HELPER_FLAGS_2(sats, TCG_CALL_NO_RWG_SE, i32, i32, i32)
-DEF_HELPER_3(divuw, void, env, int, i32)
-DEF_HELPER_3(divsw, void, env, int, s32)
-DEF_HELPER_4(divul, void, env, int, int, i32)
-DEF_HELPER_4(divsl, void, env, int, int, s32)
-DEF_HELPER_4(divull, void, env, int, int, i32)
-DEF_HELPER_4(divsll, void, env, int, int, s32)
+DEF_HELPER_4(divuw, void, env, int, i32, int)
+DEF_HELPER_4(divsw, void, env, int, s32, int)
+DEF_HELPER_5(divul, void, env, int, int, i32, int)
+DEF_HELPER_5(divsl, void, env, int, int, s32, int)
+DEF_HELPER_5(divull, void, env, int, int, i32, int)
+DEF_HELPER_5(divsll, void, env, int, int, s32, int)
 DEF_HELPER_2(set_sr, void, env, i32)
 DEF_HELPER_3(cf_movec_to, void, env, i32, i32)
 DEF_HELPER_3(m68k_movec_to, void, env, i32, i32)
linux-user/m68k/cpu_loop.c +1 -1
index 5d9c1f3753..45419d4471 100644
@@ -51,7 +51,7 @@ void cpu_loop(CPUM68KState *env)
             force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTOVF, env->mmu.ar);
             break;
         case EXCP_DIV0:
-            force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTDIV, env->pc);
+            force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTDIV, env->mmu.ar);
             break;
         case EXCP_TRAP0:
             {
target/m68k/op_helper.c +29 -19

152 lines not on this page · raw message

target/m68k/translate.c +16 -17
index 6d6d026e3c..d775345bfa 100644
@@ -1601,6 +1601,7 @@ DISAS_INSN(divw)
     int sign;
     TCGv src;
     TCGv destr;
+    TCGv ilen;
 
     /* divX.w <EA>,Dn    32/16 -> 16r:16q */
 
@@ -1609,20 +1610,20 @@ DISAS_INSN(divw)
     /* dest.l / src.w */
 
     SRC_EA(env, src, OS_WORD, sign, NULL);
-    destr = tcg_const_i32(REG(insn, 9));
+    destr = tcg_constant_i32(REG(insn, 9));
+    ilen = tcg_constant_i32(s->pc - s->base.pc_next);
     if (sign) {
-        gen_helper_divsw(cpu_env, destr, src);
+        gen_helper_divsw(cpu_env, destr, src, ilen);
     } else {
-        gen_helper_divuw(cpu_env, destr, src);
+        gen_helper_divuw(cpu_env, destr, src, ilen);
     }
-    tcg_temp_free(destr);
 
     set_cc_op(s, CC_OP_FLAGS);
 }
 
 DISAS_INSN(divl)
 {
-    TCGv num, reg, den;
+    TCGv num, reg, den, ilen;
     int sign;
     uint16_t ext;
 
@@ -1639,15 +1640,14 @@ DISAS_INSN(divl)
         /* divX.l <EA>, Dr:Dq    64/32 -> 32r:32q */
 
         SRC_EA(env, den, OS_LONG, 0, NULL);
-        num = tcg_const_i32(REG(ext, 12));
-        reg = tcg_const_i32(REG(ext, 0));
+        num = tcg_constant_i32(REG(ext, 12));
+        reg = tcg_constant_i32(REG(ext, 0));
+        ilen = tcg_constant_i32(s->pc - s->base.pc_next);
         if (sign) {
-            gen_helper_divsll(cpu_env, num, reg, den);
+            gen_helper_divsll(cpu_env, num, reg, den, ilen);
         } else {
-            gen_helper_divull(cpu_env, num, reg, den);
+            gen_helper_divull(cpu_env, num, reg, den, ilen);
         }
-        tcg_temp_free(reg);
-        tcg_temp_free(num);
         set_cc_op(s, CC_OP_FLAGS);
         return;
     }
@@ -1656,15 +1656,14 @@ DISAS_INSN(divl)
     /* divXl.l <EA>, Dr:Dq    32/32 -> 32r:32q */
 
     SRC_EA(env, den, OS_LONG, 0, NULL);
-    num = tcg_const_i32(REG(ext, 12));
-    reg = tcg_const_i32(REG(ext, 0));
+    num = tcg_constant_i32(REG(ext, 12));
+    reg = tcg_constant_i32(REG(ext, 0));
+    ilen = tcg_constant_i32(s->pc - s->base.pc_next);
     if (sign) {
-        gen_helper_divsl(cpu_env, num, reg, den);
+        gen_helper_divsl(cpu_env, num, reg, den, ilen);
     } else {
-        gen_helper_divul(cpu_env, num, reg, den);
+        gen_helper_divul(cpu_env, num, reg, den, ilen);
     }
-    tcg_temp_free(reg);
-    tcg_temp_free(num);
 
     set_cc_op(s, CC_OP_FLAGS);
 }
Signature · 1 line
2.34.1
#11 lore raw

PATCH v4 8/17 target/m68k: Fix address argument for EXCP_TRACE

In reply to Richard Henderson

According to the M68040 Users Manual, section 8.4.3, Six word stack frame (format 2), Trace (and others) is supposed to record the next insn in PC and the address of the trapping instruction in ADDRESS.

Create gen_raise_exception_format2 to record the trapping pc in env->mmu.ar. Update m68k_interrupt_all to pass the value to do_stack_frame. Update cpu_loop to handle EXCP_TRACE.

---
 linux-user/m68k/cpu_loop.c |  3 +++
 target/m68k/op_helper.c    |  2 +-
 target/m68k/translate.c    | 49 +++++++++++++++++++++++++-------------
 3 files changed, 36 insertions(+), 18 deletions(-)
linux-user/m68k/cpu_loop.c +3 -0
index 45419d4471..000bb44cc3 100644
@@ -53,6 +53,9 @@ void cpu_loop(CPUM68KState *env)
         case EXCP_DIV0:
             force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTDIV, env->mmu.ar);
             break;
+        case EXCP_TRACE:
+            force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_TRACE, env->mmu.ar);
+            break;
         case EXCP_TRAP0:
             {
                 abi_long ret;
target/m68k/op_helper.c +1 -1
index 729ee0e934..3cb71c9140 100644
@@ -397,13 +397,13 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
 
     case EXCP_ILLEGAL:
     case EXCP_TRAPCC:
-    case EXCP_TRACE:
         /* FIXME: addr is not only env->pc */
         do_stack_frame(env, &sp, 2, oldsr, env->pc, env->pc);
         break;
 
     case EXCP_CHK:
     case EXCP_DIV0:
+    case EXCP_TRACE:
         do_stack_frame(env, &sp, 2, oldsr, env->mmu.ar, env->pc);
         break;
 
target/m68k/translate.c +32 -17

123 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v4 8/17
#12 lore raw

Re: [PATCH v4 08/17] target/m68k: Fix address argument for EXCP_TRACE

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 178 lines

According to the M68040 Users Manual, section 8.4.3, Six word stack frame (format 2), Trace (and others) is supposed to record the next insn in PC and the address of the trapping instruction in ADDRESS.

Create gen_raise_exception_format2 to record the trapping pc in env->mmu.ar. Update m68k_interrupt_all to pass the value to do_stack_frame. Update cpu_loop to handle EXCP_TRACE.

---
  linux-user/m68k/cpu_loop.c |  3 +++
  target/m68k/op_helper.c    |  2 +-
  target/m68k/translate.c    | 49 +++++++++++++++++++++++++-------------
  3 files changed, 36 insertions(+), 18 deletions(-)
linux-user/m68k/cpu_loop.c +3 -0
index 45419d4471..000bb44cc3 100644
@@ -53,6 +53,9 @@ void cpu_loop(CPUM68KState *env)
          case EXCP_DIV0:
              force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTDIV, env->mmu.ar);
              break;
+        case EXCP_TRACE:
+            force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_TRACE, env->mmu.ar);
+            break;
          case EXCP_TRAP0:
              {
                  abi_long ret;
target/m68k/op_helper.c +1 -1
index 729ee0e934..3cb71c9140 100644
@@ -397,13 +397,13 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
 
      case EXCP_ILLEGAL:
      case EXCP_TRAPCC:
-    case EXCP_TRACE:
          /* FIXME: addr is not only env->pc */
          do_stack_frame(env, &sp, 2, oldsr, env->pc, env->pc);
          break;
 
      case EXCP_CHK:
      case EXCP_DIV0:
+    case EXCP_TRACE:
          do_stack_frame(env, &sp, 2, oldsr, env->mmu.ar, env->pc);
          break;
 
target/m68k/translate.c +31 -16
index d775345bfa..399d9232e4 100644
@@ -114,6 +114,7 @@ typedef struct DisasContext {
      DisasContextBase base;
      CPUM68KState *env;
      target_ulong pc;
+    target_ulong pc_prev;
      CCOp cc_op; /* Current CC operation */
      int cc_op_synced;
      TCGv_i64 mactmp;
@@ -298,6 +299,21 @@ static void gen_raise_exception(int nr)
      tcg_temp_free_i32(tmp);
  }
 
+static void gen_raise_exception_format2(DisasContext *s, int nr,
+                                        target_ulong this_pc)
+{
+    /*
+     * Pass the address of the insn to the exception handler,
+     * for recording in the Format $2 (6-word) stack frame.
+     * Re-use mmu.ar for the purpose, since that's only valid
+     * after tlb_fill.
+     */
+    tcg_gen_st_i32(tcg_constant_i32(this_pc), cpu_env,
+                   offsetof(CPUM68KState, mmu.ar));
+    gen_raise_exception(nr);
+    s->base.is_jmp = DISAS_NORETURN;
+}
+
  static void gen_exception(DisasContext *s, uint32_t dest, int nr)
  {
      update_cc_op(s);
@@ -1494,12 +1510,13 @@ static void gen_exit_tb(DisasContext *s)
      } while (0)
 
  /* Generate a jump to an immediate address.  */
-static void gen_jmp_tb(DisasContext *s, int n, uint32_t dest)
+static void gen_jmp_tb(DisasContext *s, int n, target_ulong dest,
+                       target_ulong src)
  {
      if (unlikely(s->ss_active)) {
          update_cc_op(s);
          tcg_gen_movi_i32(QREG_PC, dest);
-        gen_raise_exception(EXCP_TRACE);
+        gen_raise_exception_format2(s, EXCP_TRACE, src);
      } else if (translator_use_goto_tb(&s->base, dest)) {
          tcg_gen_goto_tb(n);
          tcg_gen_movi_i32(QREG_PC, dest);
@@ -1548,9 +1565,9 @@ DISAS_INSN(dbcc)
      tcg_gen_addi_i32(tmp, tmp, -1);
      gen_partset_reg(OS_WORD, reg, tmp);
      tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, -1, l1);
-    gen_jmp_tb(s, 1, base + offset);
+    gen_jmp_tb(s, 1, base + offset, s->base.pc_next);
      gen_set_label(l1);
-    gen_jmp_tb(s, 0, s->pc);
+    gen_jmp_tb(s, 0, s->pc, s->base.pc_next);
  }
 
  DISAS_INSN(undef_mac)
@@ -3096,13 +3113,13 @@ DISAS_INSN(branch)
          /* Bcc */
          TCGLabel *l1 = gen_new_label();
          gen_jmpcc(s, ((insn >> 8) & 0xf) ^ 1, l1);
-        gen_jmp_tb(s, 1, base + offset);
+        gen_jmp_tb(s, 1, base + offset, s->base.pc_next);
          gen_set_label(l1);
-        gen_jmp_tb(s, 0, s->pc);
+        gen_jmp_tb(s, 0, s->pc, s->base.pc_next);
      } else {
          /* Unconditional branch.  */
          update_cc_op(s);
-        gen_jmp_tb(s, 0, base + offset);
+        gen_jmp_tb(s, 0, base + offset, s->base.pc_next);
      }
  }
 
@@ -5485,9 +5502,9 @@ DISAS_INSN(fbcc)
      l1 = gen_new_label();
      update_cc_op(s);
      gen_fjmpcc(s, insn & 0x3f, l1);
-    gen_jmp_tb(s, 0, s->pc);
+    gen_jmp_tb(s, 0, s->pc, s->base.pc_next);
      gen_set_label(l1);
-    gen_jmp_tb(s, 1, base + offset);
+    gen_jmp_tb(s, 1, base + offset, s->base.pc_next);
  }
 
  DISAS_INSN(fscc)
@@ -6158,6 +6175,8 @@ static void m68k_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cpu)
 
      dc->env = env;
      dc->pc = dc->base.pc_first;
+    /* This value will always be filled in properly before m68k_tr_tb_stop. */
+    dc->pc_prev = 0xdeadbeef;
      dc->cc_op = CC_OP_DYNAMIC;
      dc->cc_op_synced = 1;
      dc->done_mac = 0;
@@ -6191,6 +6210,7 @@ static void m68k_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
      do_writebacks(dc);
      do_release(dc);
 
+    dc->pc_prev = dc->base.pc_next;
      dc->base.pc_next = dc->pc;
 
      if (dc->base.is_jmp == DISAS_NEXT) {
@@ -6225,17 +6245,12 @@ static void m68k_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
          break;
      case DISAS_TOO_MANY:
          update_cc_op(dc);
-        if (dc->ss_active) {
-            tcg_gen_movi_i32(QREG_PC, dc->pc);
-            gen_raise_exception(EXCP_TRACE);
-        } else {
-            gen_jmp_tb(dc, 0, dc->pc);
-        }
+        gen_jmp_tb(dc, 0, dc->pc, dc->pc_prev);
          break;
      case DISAS_JUMP:
          /* We updated CC_OP and PC in gen_jmp/gen_jmp_im.  */
          if (dc->ss_active) {
-            gen_raise_exception(EXCP_TRACE);
+            gen_raise_exception_format2(dc, EXCP_TRACE, dc->pc_prev);
          } else {
              tcg_gen_lookup_and_goto_ptr();
          }

target/m68k/translate.c static void m68k_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu) · line 6267

Quoted 8 lines
target/m68k/translate.c +1 -1
@@ -6246,7 +6261,7 @@ static void m68k_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
           * other state that may require returning to the main loop.
           */
          if (dc->ss_active) {
-            gen_raise_exception(EXCP_TRACE);
+            gen_raise_exception_format2(dc, EXCP_TRACE, dc->pc_prev);
          } else {
              tcg_gen_exit_tb(NULL, 0);
          }
#13 lore raw

PATCH v4 9/17 target/m68k: Fix stack frame for EXCP_ILLEGAL

In reply to Richard Henderson

According to the M68040 Users Manual, section 8.4.3, Four word stack frame (format 0), includes Illegal Instruction. Use the correct frame format, which does not use the ADDR argument.

---
 target/m68k/op_helper.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/m68k/op_helper.c +4 -1
index 3cb71c9140..aa62158eb9 100644
@@ -391,11 +391,14 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
         }
         break;
 
+    case EXCP_ILLEGAL:
+        do_stack_frame(env, &sp, 0, oldsr, 0, env->pc);
+        break;
+
     case EXCP_ADDRESS:
         do_stack_frame(env, &sp, 2, oldsr, 0, env->pc);
         break;
 
-    case EXCP_ILLEGAL:
     case EXCP_TRAPCC:
         /* FIXME: addr is not only env->pc */
         do_stack_frame(env, &sp, 2, oldsr, env->pc, env->pc);
Signature · 1 line
2.34.1
Reviewed-by on v4 9/17
#14 lore raw

Re: [PATCH v4 09/17] target/m68k: Fix stack frame for EXCP_ILLEGAL

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 1 line

According to the M68040 Users Manual, section 8.4.3, Four word

This is in section 8.4.1

Quoted 28 lines

stack frame (format 0), includes Illegal Instruction. Use the correct frame format, which does not use the ADDR argument.

---
  target/m68k/op_helper.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
target/m68k/op_helper.c +4 -1
index 3cb71c9140..aa62158eb9 100644
@@ -391,11 +391,14 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
          }
          break;
 
+    case EXCP_ILLEGAL:
+        do_stack_frame(env, &sp, 0, oldsr, 0, env->pc);
+        break;
+
      case EXCP_ADDRESS:
          do_stack_frame(env, &sp, 2, oldsr, 0, env->pc);
          break;
 
-    case EXCP_ILLEGAL:
      case EXCP_TRAPCC:
          /* FIXME: addr is not only env->pc */
          do_stack_frame(env, &sp, 2, oldsr, env->pc, env->pc);
#15 lore raw

PATCH v4 10/17 target/m68k: Implement TRAPcc

In reply to Richard Henderson

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/754

---
 target/m68k/cpu.h          |  2 ++
 linux-user/m68k/cpu_loop.c |  1 +
 target/m68k/cpu.c          |  1 +
 target/m68k/op_helper.c    |  6 +----
 target/m68k/translate.c    | 49 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 54 insertions(+), 5 deletions(-)
target/m68k/cpu.h +2 -0
index 558c3c67d6..4d8f48e8c7 100644
@@ -534,6 +534,8 @@ enum m68k_features {
     M68K_FEATURE_MOVEC,
     /* Unaligned data accesses (680[2346]0) */
     M68K_FEATURE_UNALIGNED_DATA,
+    /* TRAPcc insn. (680[2346]0, and CPU32) */
+    M68K_FEATURE_TRAPCC,
 };
 
 static inline int m68k_feature(CPUM68KState *env, int feature)
linux-user/m68k/cpu_loop.c +1 -0
index 000bb44cc3..5007b24c03 100644
@@ -48,6 +48,7 @@ void cpu_loop(CPUM68KState *env)
             force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPN, env->pc);
             break;
         case EXCP_CHK:
+        case EXCP_TRAPCC:
             force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTOVF, env->mmu.ar);
             break;
         case EXCP_DIV0:
target/m68k/cpu.c +1 -0
index c7aeb7da9c..5f778773d1 100644
@@ -162,6 +162,7 @@ static void m68020_cpu_initfn(Object *obj)
     m68k_set_feature(env, M68K_FEATURE_CHK2);
     m68k_set_feature(env, M68K_FEATURE_MSP);
     m68k_set_feature(env, M68K_FEATURE_UNALIGNED_DATA);
+    m68k_set_feature(env, M68K_FEATURE_TRAPCC);
 }
 
 /*
target/m68k/op_helper.c +1 -5
index aa62158eb9..61948d92bb 100644
@@ -399,14 +399,10 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
         do_stack_frame(env, &sp, 2, oldsr, 0, env->pc);
         break;
 
-    case EXCP_TRAPCC:
-        /* FIXME: addr is not only env->pc */
-        do_stack_frame(env, &sp, 2, oldsr, env->pc, env->pc);
-        break;
-
     case EXCP_CHK:
     case EXCP_DIV0:
     case EXCP_TRACE:
+    case EXCP_TRAPCC:
         do_stack_frame(env, &sp, 2, oldsr, env->mmu.ar, env->pc);
         break;
 
target/m68k/translate.c +49 -0

61 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v4 10/17
#16 lore raw

Re: [PATCH v4 10/17] target/m68k: Implement TRAPcc

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 101 lines

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/754

---
  target/m68k/cpu.h          |  2 ++
  linux-user/m68k/cpu_loop.c |  1 +
  target/m68k/cpu.c          |  1 +
  target/m68k/op_helper.c    |  6 +----
  target/m68k/translate.c    | 49 ++++++++++++++++++++++++++++++++++++++
  5 files changed, 54 insertions(+), 5 deletions(-)
target/m68k/cpu.h +2 -0
index 558c3c67d6..4d8f48e8c7 100644
@@ -534,6 +534,8 @@ enum m68k_features {
      M68K_FEATURE_MOVEC,
      /* Unaligned data accesses (680[2346]0) */
      M68K_FEATURE_UNALIGNED_DATA,
+    /* TRAPcc insn. (680[2346]0, and CPU32) */
+    M68K_FEATURE_TRAPCC,
  };
 
  static inline int m68k_feature(CPUM68KState *env, int feature)
linux-user/m68k/cpu_loop.c +1 -0
index 000bb44cc3..5007b24c03 100644
@@ -48,6 +48,7 @@ void cpu_loop(CPUM68KState *env)
              force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPN, env->pc);
              break;
          case EXCP_CHK:
+        case EXCP_TRAPCC:
              force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTOVF, env->mmu.ar);
              break;
          case EXCP_DIV0:
target/m68k/cpu.c +1 -0
index c7aeb7da9c..5f778773d1 100644
@@ -162,6 +162,7 @@ static void m68020_cpu_initfn(Object *obj)
      m68k_set_feature(env, M68K_FEATURE_CHK2);
      m68k_set_feature(env, M68K_FEATURE_MSP);
      m68k_set_feature(env, M68K_FEATURE_UNALIGNED_DATA);
+    m68k_set_feature(env, M68K_FEATURE_TRAPCC);
  }
 
  /*
target/m68k/op_helper.c +1 -5
index aa62158eb9..61948d92bb 100644
@@ -399,14 +399,10 @@ static void m68k_interrupt_all(CPUM68KState *env, int is_hw)
          do_stack_frame(env, &sp, 2, oldsr, 0, env->pc);
          break;
 
-    case EXCP_TRAPCC:
-        /* FIXME: addr is not only env->pc */
-        do_stack_frame(env, &sp, 2, oldsr, env->pc, env->pc);
-        break;
-
      case EXCP_CHK:
      case EXCP_DIV0:
      case EXCP_TRACE:
+    case EXCP_TRAPCC:
          do_stack_frame(env, &sp, 2, oldsr, env->mmu.ar, env->pc);
          break;
 
target/m68k/translate.c +26 -0
index 399d9232e4..c4fe8abc03 100644
@@ -4879,6 +4879,54 @@ DISAS_INSN(trap)
      gen_exception(s, s->pc, EXCP_TRAP0 + (insn & 0xf));
  }
 
+static void do_trapcc(DisasContext *s, DisasCompare *c)
+{
+    if (c->tcond != TCG_COND_NEVER) {
+        TCGLabel *over = NULL;
+
+        update_cc_op(s);
+
+        if (c->tcond != TCG_COND_ALWAYS) {
+            /* Jump over if !c. */
+            over = gen_new_label();
+            tcg_gen_brcond_i32(tcg_invert_cond(c->tcond), c->v1, c->v2, over);
+        }
+
+        tcg_gen_movi_i32(QREG_PC, s->pc);
+        gen_raise_exception_format2(s, EXCP_TRAPCC, s->base.pc_next);
+
+        if (over != NULL) {
+            gen_set_label(over);
+            s->base.is_jmp = DISAS_NEXT;
+        }
+    }
+    free_cond(c);
+}
+
+DISAS_INSN(trapcc)
+{

target/m68k/translate.c DISAS_INSN(trap) · line 4917

Quoted 10 lines
target/m68k/translate.c +10 -0
+    DisasCompare c;
+
+    /* Consume and discard the immediate operand. */
+    switch (extract32(insn, 0, 3)) {
+    case 2: /* trapcc.w */
+        (void)read_im16(env, s);
+        break;
+    case 3: /* trapcc.l */
+        (void)read_im32(env, s);
+        break;

Do we really need to read the data or do we only need to increment s->pc (as the data are only here to be available for the trap handler)?

Quoted 16 lines
target/m68k/translate.c +12 -0
+    case 4: /* trapcc (no operand) */
+        break;
+    default:
+        /* Illegal insn */
+        disas_undef(env, s, insn);
+        return;
+    }
+
+    gen_cc_cond(&c, s, extract32(insn, 8, 4));
+    do_trapcc(s, &c);
+}
+
  static void gen_load_fcr(DisasContext *s, TCGv res, int reg)
  {
      switch (reg) {

target/m68k/translate.c void register_m68k_insns (CPUM68KState *env) · line 6104

Quoted 7 lines
target/m68k/translate.c +1 -0
@@ -6050,6 +6098,7 @@ void register_m68k_insns (CPUM68KState *env)
      INSN(scc,       50c0, f0f8, CF_ISA_A); /* Scc.B Dx   */
      INSN(scc,       50c0, f0c0, M68000);   /* Scc.B <EA> */
      INSN(dbcc,      50c8, f0f8, M68000);
+    INSN(trapcc,    50f8, f0f8, TRAPCC);
      INSN(tpf,       51f8, fff8, CF_ISA_A);
 
      /* Branch instructions.  */
#17 lore raw

Re: [PATCH v4 10/17] target/m68k: Implement TRAPcc

Quoted 6 lines · Laurent Vivier
Quoted 6 lines
target/m68k/translate.c +6 -0
+DISAS_INSN(trapcc)
+{
+    DisasCompare c;
+
+    /* Consume and discard the immediate operand. */
+    switch (extract32(insn, 0, 3)) {
Quoted 8 lines
Quoted 6 lines

+    case 2: /* trapcc.w */
+        (void)read_im16(env, s);
+        break;
+    case 3: /* trapcc.l */
+        (void)read_im32(env, s);
+        break;

Do we really need to read the data or do we only need to increment s->pc (as the data are only here to be available for the trap handler)?

We need to read the data to (1) trigger sigsegv when this insn crosses a page and (2) passing to tcg plugins.

r~

#18 lore raw

Re: [PATCH v4 10/17] target/m68k: Implement TRAPcc

Le 26/05/2022 à 00:26, Richard Henderson a écrit :

Quoted 16 lines · Laurent Vivier
Quoted 6 lines
Quoted 6 lines
target/m68k/translate.c +6 -0
+DISAS_INSN(trapcc)
+{
+    DisasCompare c;
+
+    /* Consume and discard the immediate operand. */
+    switch (extract32(insn, 0, 3)) {
Quoted 8 lines
Quoted 6 lines

+    case 2: /* trapcc.w */
+        (void)read_im16(env, s);
+        break;
+    case 3: /* trapcc.l */
+        (void)read_im32(env, s);
+        break;

Do we really need to read the data or do we only need to increment s->pc (as the data are only here to be available for the trap handler)?

Quoted 2 lines

We need to read the data to (1) trigger sigsegv when this insn crosses a page and (2) passing to tcg plugins.

For (1) I was wondering if the real CPU is actually doing it.

Nothing is said about it in the instruction definition.

Thanks,
Laurent

#19 lore raw

Re: [PATCH v4 10/17] target/m68k: Implement TRAPcc

Quoted 17 lines · Laurent Vivier

Le 26/05/2022 à 00:26, Richard Henderson a écrit :

Quoted 16 lines · Laurent Vivier
Quoted 14 lines

target/m68k/translate.c DISAS_INSN(trap) · line 4917

Quoted 12 lines
target/m68k/translate.c +12 -0
+DISAS_INSN(trapcc)
+{
+    DisasCompare c;
+
+    /* Consume and discard the immediate operand. */
+    switch (extract32(insn, 0, 3)) {
+    case 2: /* trapcc.w */
+        (void)read_im16(env, s);
+        break;
+    case 3: /* trapcc.l */
+        (void)read_im32(env, s);
+        break;

Do we really need to read the data or do we only need to increment s->pc (as the data are only here to be available for the trap handler)?

Quoted 6 lines
Quoted 2 lines

We need to read the data to (1) trigger sigsegv when this insn crosses a page and (2) passing to tcg plugins.

For (1) I was wondering if the real CPU is actually doing it.

Nothing is said about it in the instruction definition.

Surely the cpu reads cachelines at a time, so of course it would.

r~

#20 lore raw

PATCH v4 11/17 target/m68k: Implement TPF in terms of TRAPcc

In reply to Richard Henderson

TPF stands for "trap false", and is a long-form nop for ColdFire. Re-use the immediate consumption code from trapcc; the insn will already expand to a nop because of the TCG_COND_NEVER test within do_trapcc.

---
 target/m68k/translate.c | 18 +-----------------
 1 file changed, 1 insertion(+), 17 deletions(-)
target/m68k/translate.c +1 -17
index c4fe8abc03..bb5ed1b7b1 100644
@@ -3075,22 +3075,6 @@ DISAS_INSN(addsubq)
     tcg_temp_free(dest);
 }
 
-DISAS_INSN(tpf)
-{
-    switch (insn & 7) {
-    case 2: /* One extension word.  */
-        s->pc += 2;
-        break;
-    case 3: /* Two extension words.  */
-        s->pc += 4;
-        break;
-    case 4: /* No extension words.  */
-        break;
-    default:
-        disas_undef(env, s, insn);
-    }
-}
-
 DISAS_INSN(branch)
 {
     int32_t offset;
@@ -6099,7 +6083,7 @@ void register_m68k_insns (CPUM68KState *env)
     INSN(scc,       50c0, f0c0, M68000);   /* Scc.B <EA> */
     INSN(dbcc,      50c8, f0f8, M68000);
     INSN(trapcc,    50f8, f0f8, TRAPCC);
-    INSN(tpf,       51f8, fff8, CF_ISA_A);
+    INSN(trapcc,    51f8, fff8, CF_ISA_A); /* TPF (trapf) */
 
     /* Branch instructions.  */
     BASE(branch,    6000, f000);
Signature · 1 line
2.34.1
Reviewed-by Philippe Mathieu-Daudé on v4 11/17
#21 lore raw

Re: [PATCH v4 11/17] target/m68k: Implement TPF in terms of TRAPcc

Quoted 9 lines · Richard Henderson

TPF stands for "trap false", and is a long-form nop for ColdFire. Re-use the immediate consumption code from trapcc; the insn will already expand to a nop because of the TCG_COND_NEVER test within do_trapcc.

---
  target/m68k/translate.c | 18 +-----------------
  1 file changed, 1 insertion(+), 17 deletions(-)
Reviewed-by on v4 11/17
#22 lore raw

Re: [PATCH v4 11/17] target/m68k: Implement TPF in terms of TRAPcc

In reply to Richard Henderson

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 38 lines

TPF stands for "trap false", and is a long-form nop for ColdFire. Re-use the immediate consumption code from trapcc; the insn will already expand to a nop because of the TCG_COND_NEVER test within do_trapcc.

---
  target/m68k/translate.c | 18 +-----------------
  1 file changed, 1 insertion(+), 17 deletions(-)
target/m68k/translate.c +0 -16
index c4fe8abc03..bb5ed1b7b1 100644
@@ -3075,22 +3075,6 @@ DISAS_INSN(addsubq)
      tcg_temp_free(dest);
  }
 
-DISAS_INSN(tpf)
-{
-    switch (insn & 7) {
-    case 2: /* One extension word.  */
-        s->pc += 2;
-        break;
-    case 3: /* Two extension words.  */
-        s->pc += 4;
-        break;
-    case 4: /* No extension words.  */
-        break;
-    default:
-        disas_undef(env, s, insn);
-    }
-}
-
  DISAS_INSN(branch)
  {
      int32_t offset;

target/m68k/translate.c void register_m68k_insns (CPUM68KState *env) · line 6089

Quoted 8 lines
target/m68k/translate.c +1 -1
@@ -6099,7 +6083,7 @@ void register_m68k_insns (CPUM68KState *env)
      INSN(scc,       50c0, f0c0, M68000);   /* Scc.B <EA> */
      INSN(dbcc,      50c8, f0f8, M68000);
      INSN(trapcc,    50f8, f0f8, TRAPCC);
-    INSN(tpf,       51f8, fff8, CF_ISA_A);
+    INSN(trapcc,    51f8, fff8, CF_ISA_A); /* TPF (trapf) */
 
      /* Branch instructions.  */
      BASE(branch,    6000, f000);
#23 lore raw

PATCH v4 12/17 target/m68k: Implement TRAPV

In reply to Richard Henderson
---
 target/m68k/translate.c | 9 +++++++++
 1 file changed, 9 insertions(+)
target/m68k/translate.c +9 -0
index bb5ed1b7b1..0cd7ef89e3 100644
@@ -4911,6 +4911,14 @@ DISAS_INSN(trapcc)
     do_trapcc(s, &c);
 }
 
+DISAS_INSN(trapv)
+{
+    DisasCompare c;
+
+    gen_cc_cond(&c, s, 9); /* V set */
+    do_trapcc(s, &c);
+}
+
 static void gen_load_fcr(DisasContext *s, TCGv res, int reg)
 {
     switch (reg) {
@@ -6074,6 +6082,7 @@ void register_m68k_insns (CPUM68KState *env)
     BASE(nop,       4e71, ffff);
     INSN(rtd,       4e74, ffff, RTD);
     BASE(rts,       4e75, ffff);
+    INSN(trapv,     4e76, ffff, M68000);
     INSN(rtr,       4e77, ffff, M68000);
     BASE(jump,      4e80, ffc0);
     BASE(jump,      4ec0, ffc0);
Signature · 1 line
2.34.1
Reviewed-by on v4 12/17
#24 lore raw

Re: [PATCH v4 12/17] target/m68k: Implement TRAPV

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 25 lines
---
  target/m68k/translate.c | 9 +++++++++
  1 file changed, 9 insertions(+)
target/m68k/translate.c +8 -0
index bb5ed1b7b1..0cd7ef89e3 100644
@@ -4911,6 +4911,14 @@ DISAS_INSN(trapcc)
      do_trapcc(s, &c);
  }
 
+DISAS_INSN(trapv)
+{
+    DisasCompare c;
+
+    gen_cc_cond(&c, s, 9); /* V set */
+    do_trapcc(s, &c);
+}
+
  static void gen_load_fcr(DisasContext *s, TCGv res, int reg)
  {
      switch (reg) {

target/m68k/translate.c void register_m68k_insns (CPUM68KState *env) · line 6088

Quoted 7 lines
target/m68k/translate.c +1 -0
@@ -6074,6 +6082,7 @@ void register_m68k_insns (CPUM68KState *env)
      BASE(nop,       4e71, ffff);
      INSN(rtd,       4e74, ffff, RTD);
      BASE(rts,       4e75, ffff);
+    INSN(trapv,     4e76, ffff, M68000);
      INSN(rtr,       4e77, ffff, M68000);
      BASE(jump,      4e80, ffc0);
      BASE(jump,      4ec0, ffc0);
#25 lore raw

PATCH v4 13/17 target/m68k: Implement FTRAPcc

In reply to Richard Henderson
---
 target/m68k/translate.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
target/m68k/translate.c +30 -0
index 0cd7ef89e3..a3141d7f77 100644
@@ -5567,6 +5567,35 @@ DISAS_INSN(fscc)
     tcg_temp_free(tmp);
 }
 
+DISAS_INSN(ftrapcc)
+{
+    DisasCompare c;
+    uint16_t ext;
+    int cond;
+
+    ext = read_im16(env, s);
+    cond = ext & 0x3f;
+
+    /* Consume and discard the immediate operand. */
+    switch (extract32(insn, 0, 3)) {
+    case 2: /* ftrapcc.w */
+        (void)read_im16(env, s);
+        break;
+    case 3: /* ftrapcc.l */
+        (void)read_im32(env, s);
+        break;
+    case 4: /* ftrapcc (no operand) */
+        break;
+    default:
+        /* Illegal insn */
+        disas_undef(env, s, insn);
+        return;
+    }
+
+    gen_fcc_cond(&c, s, cond);
+    do_trapcc(s, &c);
+}
+
 #if defined(CONFIG_SOFTMMU)
 DISAS_INSN(frestore)
 {
@@ -6190,6 +6219,7 @@ void register_m68k_insns (CPUM68KState *env)
     INSN(fbcc,      f280, ffc0, CF_FPU);
     INSN(fpu,       f200, ffc0, FPU);
     INSN(fscc,      f240, ffc0, FPU);
+    INSN(ftrapcc,   f278, fff8, FPU);
     INSN(fbcc,      f280, ff80, FPU);
 #if defined(CONFIG_SOFTMMU)
     INSN(frestore,  f340, ffc0, CF_FPU);
Signature · 1 line
2.34.1
Reviewed-by on v4 13/17
#26 lore raw

Re: [PATCH v4 13/17] target/m68k: Implement FTRAPcc

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 46 lines
---
  target/m68k/translate.c | 30 ++++++++++++++++++++++++++++++
  1 file changed, 30 insertions(+)
target/m68k/translate.c +29 -0
index 0cd7ef89e3..a3141d7f77 100644
@@ -5567,6 +5567,35 @@ DISAS_INSN(fscc)
      tcg_temp_free(tmp);
  }
 
+DISAS_INSN(ftrapcc)
+{
+    DisasCompare c;
+    uint16_t ext;
+    int cond;
+
+    ext = read_im16(env, s);
+    cond = ext & 0x3f;
+
+    /* Consume and discard the immediate operand. */
+    switch (extract32(insn, 0, 3)) {
+    case 2: /* ftrapcc.w */
+        (void)read_im16(env, s);
+        break;
+    case 3: /* ftrapcc.l */
+        (void)read_im32(env, s);
+        break;
+    case 4: /* ftrapcc (no operand) */
+        break;
+    default:
+        /* Illegal insn */
+        disas_undef(env, s, insn);
+        return;
+    }
+
+    gen_fcc_cond(&c, s, cond);
+    do_trapcc(s, &c);
+}
+
  #if defined(CONFIG_SOFTMMU)
  DISAS_INSN(frestore)
  {

target/m68k/translate.c void register_m68k_insns (CPUM68KState *env) · line 6225

Quoted 7 lines
target/m68k/translate.c +1 -0
@@ -6190,6 +6219,7 @@ void register_m68k_insns (CPUM68KState *env)
      INSN(fbcc,      f280, ffc0, CF_FPU);
      INSN(fpu,       f200, ffc0, FPU);
      INSN(fscc,      f240, ffc0, FPU);
+    INSN(ftrapcc,   f278, fff8, FPU);
      INSN(fbcc,      f280, ff80, FPU);
  #if defined(CONFIG_SOFTMMU)
      INSN(frestore,  f340, ffc0, CF_FPU);
#27 lore raw

PATCH v4 14/17 tests/tcg/m68k: Add trap.c

In reply to Richard Henderson

Test various trap instructions: chk, div, trap, trapv, trapcc, ftrapcc, and the signals and addresses that we expect from them.

---
 tests/tcg/m68k/trap.c          | 129 +++++++++++++++++++++++++++++++++
 tests/tcg/m68k/Makefile.target |   3 +
 2 files changed, 132 insertions(+)
 create mode 100644 tests/tcg/m68k/trap.c
tests/tcg/m68k/trap.c new file +129 -0
new file mode 100644
index 0000000000..08ab56b2ca
@@ -0,0 +1,129 @@
+/*
+ * Test m68k trap addresses.
+ */
+
+#define _GNU_SOURCE 1
+#include <signal.h>
+#include <assert.h>
+#include <limits.h>
+
+static int expect_sig;
+static int expect_si_code;
+static void *expect_si_addr;
+static greg_t expect_mc_pc;
+static volatile int got_signal;
+
+static void sig_handler(int sig, siginfo_t *si, void *puc)
+{
+    ucontext_t *uc = puc;
+    mcontext_t *mc = &uc->uc_mcontext;
+
+    assert(sig == expect_sig);
+    assert(si->si_code == expect_si_code);
+    assert(si->si_addr == expect_si_addr);
+    assert(mc->gregs[R_PC] == expect_mc_pc);
+
+    got_signal = 1;
+}
+
+#define FMT_INS     [ad] "a"(&expect_si_addr), [pc] "a"(&expect_mc_pc)
+#define FMT0_STR(S) \
+    "move.l #1f, (%[ad])\n\tmove.l #1f, (%[pc])\n" S "\n1:\n"
+#define FMT2_STR(S) \
+    "move.l #0f, (%[ad])\n\tmove.l #1f, (%[pc])\n" S "\n1:\n"
+
+#define CHECK_SIG   do { assert(got_signal); got_signal = 0; } while (0)
+
+int main(int argc, char **argv)
+{
+    struct sigaction act = {
+        .sa_sigaction = sig_handler,
+        .sa_flags = SA_SIGINFO
+    };
+    int t0, t1;
+
+    sigaction(SIGILL, &act, NULL);
+    sigaction(SIGTRAP, &act, NULL);
+    sigaction(SIGFPE, &act, NULL);
+
+    expect_sig = SIGFPE;
+    expect_si_code = FPE_INTOVF;
+    asm volatile(FMT2_STR("0:\tchk %0, %1") : : "d"(0), "d"(-1), FMT_INS);
+    CHECK_SIG;
+
+#if 0
+    /* FIXME: chk2 not correctly translated. */
+    int bounds[2] = { 0, 1 };
+    asm volatile(FMT2_STR("0:\tchk2.l %0, %1")
+                 : : "m"(bounds), "d"(2), FMT_INS);
+    CHECK_SIG;
+#endif
+
+    asm volatile(FMT2_STR("cmp.l %0, %1\n0:\ttrapv")
+                 : : "d"(INT_MIN), "d"(1), FMT_INS);
+    CHECK_SIG;
+
+    asm volatile(FMT2_STR("cmp.l %0, %0\n0:\ttrapeq")
+                 : : "d"(0), FMT_INS);
+    CHECK_SIG;
+
+    asm volatile(FMT2_STR("cmp.l %0, %0\n0:\ttrapeq.w #0x1234")
+                 : : "d"(0), FMT_INS);
+    CHECK_SIG;
+
+    asm volatile(FMT2_STR("cmp.l %0, %0\n0:\ttrapeq.l #0x12345678")
+                 : : "d"(0), FMT_INS);
+    CHECK_SIG;
+
+    asm volatile(FMT2_STR("fcmp.x %0, %0\n0:\tftrapeq")
+                 : : "f"(0.0L), FMT_INS);
+    CHECK_SIG;
+
+    expect_si_code = FPE_INTDIV;
+
+    asm volatile(FMT2_STR("0:\tdivs.w %1, %0")
+                 : "=d"(t0) : "d"(0), "0"(1), FMT_INS);
+    CHECK_SIG;
+
+    asm volatile(FMT2_STR("0:\tdivsl.l %2, %1:%0")
+                 : "=d"(t0), "=d"(t1) : "d"(0), "0"(1), FMT_INS);
+    CHECK_SIG;
+
+    expect_sig = SIGILL;
+    expect_si_code = ILL_ILLOPN;
+    asm volatile(FMT0_STR("trap #1") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #2") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #3") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #4") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #5") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #6") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #7") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #8") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #9") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #10") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #11") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #12") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #13") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #14") : : FMT_INS);
+    CHECK_SIG;
+
+    expect_sig = SIGTRAP;
+    expect_si_code = TRAP_BRKPT;
+    asm volatile(FMT0_STR("trap #15") : : FMT_INS);
+    CHECK_SIG;
+
+    return 0;
+}
tests/tcg/m68k/Makefile.target +3 -0
index 62f109eef4..1163c7ef03 100644
@@ -3,5 +3,8 @@
 # m68k specific tweaks - specifically masking out broken tests
 #
 
+VPATH += $(SRC_PATH)/tests/tcg/m68k
+TESTS += trap
+
 # On m68k Linux supports 4k and 8k pages (but 8k is currently broken)
 EXTRA_RUNS+=run-test-mmap-4096 # run-test-mmap-8192
Signature · 1 line
2.34.1
Reviewed-by on v4 14/17
#28 lore raw

Re: [PATCH v4 14/17] tests/tcg/m68k: Add trap.c

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 150 lines

Test various trap instructions: chk, div, trap, trapv, trapcc, ftrapcc, and the signals and addresses that we expect from them.

---
  tests/tcg/m68k/trap.c          | 129 +++++++++++++++++++++++++++++++++
  tests/tcg/m68k/Makefile.target |   3 +
  2 files changed, 132 insertions(+)

create mode 100644 tests/tcg/m68k/trap.c

tests/tcg/m68k/trap.c new file +129 -0
new file mode 100644
index 0000000000..08ab56b2ca
@@ -0,0 +1,129 @@
+/*
+ * Test m68k trap addresses.
+ */
+
+#define _GNU_SOURCE 1
+#include <signal.h>
+#include <assert.h>
+#include <limits.h>
+
+static int expect_sig;
+static int expect_si_code;
+static void *expect_si_addr;
+static greg_t expect_mc_pc;
+static volatile int got_signal;
+
+static void sig_handler(int sig, siginfo_t *si, void *puc)
+{
+    ucontext_t *uc = puc;
+    mcontext_t *mc = &uc->uc_mcontext;
+
+    assert(sig == expect_sig);
+    assert(si->si_code == expect_si_code);
+    assert(si->si_addr == expect_si_addr);
+    assert(mc->gregs[R_PC] == expect_mc_pc);
+
+    got_signal = 1;
+}
+
+#define FMT_INS     [ad] "a"(&expect_si_addr), [pc] "a"(&expect_mc_pc)
+#define FMT0_STR(S) \
+    "move.l #1f, (%[ad])\n\tmove.l #1f, (%[pc])\n" S "\n1:\n"
+#define FMT2_STR(S) \
+    "move.l #0f, (%[ad])\n\tmove.l #1f, (%[pc])\n" S "\n1:\n"
+
+#define CHECK_SIG   do { assert(got_signal); got_signal = 0; } while (0)
+
+int main(int argc, char **argv)
+{
+    struct sigaction act = {
+        .sa_sigaction = sig_handler,
+        .sa_flags = SA_SIGINFO
+    };
+    int t0, t1;
+
+    sigaction(SIGILL, &act, NULL);
+    sigaction(SIGTRAP, &act, NULL);
+    sigaction(SIGFPE, &act, NULL);
+
+    expect_sig = SIGFPE;
+    expect_si_code = FPE_INTOVF;
+    asm volatile(FMT2_STR("0:\tchk %0, %1") : : "d"(0), "d"(-1), FMT_INS);
+    CHECK_SIG;
+
+#if 0
+    /* FIXME: chk2 not correctly translated. */
+    int bounds[2] = { 0, 1 };
+    asm volatile(FMT2_STR("0:\tchk2.l %0, %1")
+                 : : "m"(bounds), "d"(2), FMT_INS);
+    CHECK_SIG;
+#endif
+
+    asm volatile(FMT2_STR("cmp.l %0, %1\n0:\ttrapv")
+                 : : "d"(INT_MIN), "d"(1), FMT_INS);
+    CHECK_SIG;
+
+    asm volatile(FMT2_STR("cmp.l %0, %0\n0:\ttrapeq")
+                 : : "d"(0), FMT_INS);
+    CHECK_SIG;
+
+    asm volatile(FMT2_STR("cmp.l %0, %0\n0:\ttrapeq.w #0x1234")
+                 : : "d"(0), FMT_INS);
+    CHECK_SIG;
+
+    asm volatile(FMT2_STR("cmp.l %0, %0\n0:\ttrapeq.l #0x12345678")
+                 : : "d"(0), FMT_INS);
+    CHECK_SIG;
+
+    asm volatile(FMT2_STR("fcmp.x %0, %0\n0:\tftrapeq")
+                 : : "f"(0.0L), FMT_INS);
+    CHECK_SIG;
+
+    expect_si_code = FPE_INTDIV;
+
+    asm volatile(FMT2_STR("0:\tdivs.w %1, %0")
+                 : "=d"(t0) : "d"(0), "0"(1), FMT_INS);
+    CHECK_SIG;
+
+    asm volatile(FMT2_STR("0:\tdivsl.l %2, %1:%0")
+                 : "=d"(t0), "=d"(t1) : "d"(0), "0"(1), FMT_INS);
+    CHECK_SIG;
+
+    expect_sig = SIGILL;
+    expect_si_code = ILL_ILLOPN;
+    asm volatile(FMT0_STR("trap #1") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #2") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #3") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #4") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #5") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #6") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #7") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #8") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #9") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #10") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #11") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #12") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #13") : : FMT_INS);
+    CHECK_SIG;
+    asm volatile(FMT0_STR("trap #14") : : FMT_INS);
+    CHECK_SIG;
+
+    expect_sig = SIGTRAP;
+    expect_si_code = TRAP_BRKPT;
+    asm volatile(FMT0_STR("trap #15") : : FMT_INS);
+    CHECK_SIG;
+
+    return 0;
+}
tests/tcg/m68k/Makefile.target +0 -0
index 62f109eef4..1163c7ef03 100644

tests/tcg/m68k/Makefile.target line 10

Quoted 8 lines
tests/tcg/m68k/Makefile.target +3 -0
@@ -3,5 +3,8 @@
  # m68k specific tweaks - specifically masking out broken tests
  #
 
+VPATH += $(SRC_PATH)/tests/tcg/m68k
+TESTS += trap
+
  # On m68k Linux supports 4k and 8k pages (but 8k is currently broken)
  EXTRA_RUNS+=run-test-mmap-4096 # run-test-mmap-8192
#29 lore raw

PATCH v4 15/17 linux-user/strace: Fix print_syscall_err

In reply to Richard Henderson

Errors are not all negative numbers, but only the top 4k.

---
 linux-user/strace.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/strace.c +2 -2
index 2cdbf030ba..dc4f810bd3 100644
@@ -684,12 +684,12 @@ print_ipc(void *cpu_env, const struct syscallname *name,
  */
 
 static bool
-print_syscall_err(abi_long ret)
+print_syscall_err(abi_ulong ret)
 {
     const char *errstr;
 
     qemu_log(" = ");
-    if (ret < 0) {
+    if (ret > (abi_ulong)-4096) {
         errstr = target_strerror(-ret);
         if (errstr) {
             qemu_log("-1 errno=%d (%s)", (int)-ret, errstr);
Signature · 1 line
2.34.1
#30 lore raw

Re: [PATCH v4 15/17] linux-user/strace: Fix print_syscall_err

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 17 lines

Errors are not all negative numbers, but only the top 4k.

---
  linux-user/strace.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
linux-user/strace.c +0 -1
index 2cdbf030ba..dc4f810bd3 100644
@@ -684,12 +684,12 @@ print_ipc(void *cpu_env, const struct syscallname *name,
   */
 
  static bool
-print_syscall_err(abi_long ret)

linux-user/strace.c print_ipc(void *cpu_env, const struct syscallname *name, · line 695

Quoted 10 lines
linux-user/strace.c +2 -1
+print_syscall_err(abi_ulong ret)
  {
      const char *errstr;
 
      qemu_log(" = ");
-    if (ret < 0) {
+    if (ret > (abi_ulong)-4096) {
          errstr = target_strerror(-ret);
          if (errstr) {
              qemu_log("-1 errno=%d (%s)", (int)-ret, errstr);

Perhaps we can use is_error() here?

Thanks,
Laurent

#31 lore raw

PATCH v4 16/17 linux-user/strace: Adjust get_thread_area for m68k

In reply to Richard Henderson

Unlike i386, m68k get_thread_area has no arguments.

---
 linux-user/strace.list | 5 +++++
 1 file changed, 5 insertions(+)
linux-user/strace.list +5 -0
index 278596acd1..72e17b1acf 100644
@@ -384,8 +384,13 @@
 { TARGET_NR_getsockopt, "getsockopt" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_get_thread_area
+#if defined(TARGET_I386) && defined(TARGET_ABI32)
 { TARGET_NR_get_thread_area, "get_thread_area", "%s(0x"TARGET_ABI_FMT_lx")",
   NULL, NULL },
+#elif defined(TARGET_M68K)
+{ TARGET_NR_get_thread_area, "get_thread_area" , "%s()",
+  NULL, print_syscall_ret_addr },
+#endif
 #endif
 #ifdef TARGET_NR_gettid
 { TARGET_NR_gettid, "gettid" , "%s()", NULL, NULL },
Signature · 1 line
2.34.1
Reviewed-by on v4 16/17
#32 lore raw

Re: [PATCH v4 16/17] linux-user/strace: Adjust get_thread_area for m68k

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 15 lines

Unlike i386, m68k get_thread_area has no arguments.

---
  linux-user/strace.list | 5 +++++
  1 file changed, 5 insertions(+)
linux-user/strace.list +0 -0
index 278596acd1..72e17b1acf 100644
@@ -384,8 +384,13 @@
  { TARGET_NR_getsockopt, "getsockopt" , NULL, NULL, NULL },
  #endif
  #ifdef TARGET_NR_get_thread_area

linux-user/strace.list line 396

Quoted 10 lines
linux-user/strace.list +5 -0
+#if defined(TARGET_I386) && defined(TARGET_ABI32)
  { TARGET_NR_get_thread_area, "get_thread_area", "%s(0x"TARGET_ABI_FMT_lx")",
    NULL, NULL },
+#elif defined(TARGET_M68K)
+{ TARGET_NR_get_thread_area, "get_thread_area" , "%s()",
+  NULL, print_syscall_ret_addr },
+#endif
  #endif
  #ifdef TARGET_NR_gettid
  { TARGET_NR_gettid, "gettid" , "%s()", NULL, NULL },
#33 lore raw

PATCH v4 17/17 target/m68k: Mark helper_raise_exception as noreturn

In reply to Richard Henderson

Also mark raise_exception_ra and raise_exception, lest we generate a warning about helper_raise_exception returning.

---
 target/m68k/helper.h    | 2 +-
 target/m68k/op_helper.c | 5 +++--
 2 files changed, 4 insertions(+), 3 deletions(-)
target/m68k/helper.h +1 -1
index f016c4c1c2..c9bed2b884 100644
@@ -109,7 +109,7 @@ DEF_HELPER_3(set_mac_extu, void, env, i32, i32)
 DEF_HELPER_2(flush_flags, void, env, i32)
 DEF_HELPER_2(set_ccr, void, env, i32)
 DEF_HELPER_FLAGS_1(get_ccr, TCG_CALL_NO_WG_SE, i32, env)
-DEF_HELPER_2(raise_exception, void, env, i32)
+DEF_HELPER_2(raise_exception, noreturn, env, i32)
 
 DEF_HELPER_FLAGS_3(bfffo_reg, TCG_CALL_NO_RWG_SE, i32, i32, i32, i32)
 
target/m68k/op_helper.c +3 -2
index 61948d92bb..d9937ca8dc 100644
@@ -532,7 +532,8 @@ bool m68k_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
 
 #endif /* !CONFIG_USER_ONLY */
 
-static void raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr)
+G_NORETURN static void
+raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr)
 {
     CPUState *cs = env_cpu(env);
 
@@ -540,7 +541,7 @@ static void raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr)
     cpu_loop_exit_restore(cs, raddr);
 }
 
-static void raise_exception(CPUM68KState *env, int tt)
+G_NORETURN static void raise_exception(CPUM68KState *env, int tt)
 {
     raise_exception_ra(env, tt, 0);
 }
Signature · 1 line
2.34.1
Reviewed-by Philippe Mathieu-Daudé on v4 17/17
#34 lore raw

Re: [PATCH v4 17/17] target/m68k: Mark helper_raise_exception as noreturn

Quoted 8 lines · Richard Henderson

Also mark raise_exception_ra and raise_exception, lest we generate a warning about helper_raise_exception returning.

---
  target/m68k/helper.h    | 2 +-
  target/m68k/op_helper.c | 5 +++--
  2 files changed, 4 insertions(+), 3 deletions(-)
#35 lore raw

Re: [PATCH v4 17/17] target/m68k: Mark helper_raise_exception as noreturn

In reply to Richard Henderson

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 37 lines

Also mark raise_exception_ra and raise_exception, lest we generate a warning about helper_raise_exception returning.

---
  target/m68k/helper.h    | 2 +-
  target/m68k/op_helper.c | 5 +++--
  2 files changed, 4 insertions(+), 3 deletions(-)
target/m68k/helper.h +1 -1
index f016c4c1c2..c9bed2b884 100644
@@ -109,7 +109,7 @@ DEF_HELPER_3(set_mac_extu, void, env, i32, i32)
  DEF_HELPER_2(flush_flags, void, env, i32)
  DEF_HELPER_2(set_ccr, void, env, i32)
  DEF_HELPER_FLAGS_1(get_ccr, TCG_CALL_NO_WG_SE, i32, env)
-DEF_HELPER_2(raise_exception, void, env, i32)
+DEF_HELPER_2(raise_exception, noreturn, env, i32)
 
  DEF_HELPER_FLAGS_3(bfffo_reg, TCG_CALL_NO_RWG_SE, i32, i32, i32, i32)
 
target/m68k/op_helper.c +2 -1
index 61948d92bb..d9937ca8dc 100644
@@ -532,7 +532,8 @@ bool m68k_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
 
  #endif /* !CONFIG_USER_ONLY */
 
-static void raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr)
+G_NORETURN static void
+raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr)
  {
      CPUState *cs = env_cpu(env);
 

target/m68k/op_helper.c static void raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr) · line 547

Quoted 8 lines
target/m68k/op_helper.c +1 -1
@@ -540,7 +541,7 @@ static void raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr)
      cpu_loop_exit_restore(cs, raddr);
  }
 
-static void raise_exception(CPUM68KState *env, int tt)
+G_NORETURN static void raise_exception(CPUM68KState *env, int tt)
  {
      raise_exception_ra(env, tt, 0);
  }

And why not

G_NORETURN void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)

?

Thanks,
Laurent

#36 lore raw

Re: [PATCH v4 17/17] target/m68k: Mark helper_raise_exception as noreturn

Quoted 1 line · Laurent Vivier
Quoted 1 line

+DEF_HELPER_2(raise_exception, noreturn, env, i32)

...

Quoted 17 lines
Quoted 6 lines
target/m68k/op_helper.c +2 -1
-static void raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr)
+G_NORETURN static void
+raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr)

  {
      CPUState *cs = env_cpu(env);

target/m68k/op_helper.c static void raise_exception_ra(CPUM68KState *env, int tt, uintptr_t · line 547

Quoted 8 lines
target/m68k/op_helper.c +1 -1
@@ -540,7 +541,7 @@ static void raise_exception_ra(CPUM68KState *env, int tt, uintptr_t 
 raddr)
       cpu_loop_exit_restore(cs, raddr);
   }
-static void raise_exception(CPUM68KState *env, int tt)
+G_NORETURN static void raise_exception(CPUM68KState *env, int tt)
   {
       raise_exception_ra(env, tt, 0);
   }

And why not

Quoted 3 lines

  G_NORETURN void HELPER(raise_exception)(CPUM68KState *env, uint32_t tt)

?

Because the declaration in the header file takes care of that. No need to replicate it in the definition.

r~

Reviewed-by on v4 17/17
#37 lore raw

Re: [PATCH v4 17/17] target/m68k: Mark helper_raise_exception as noreturn

In reply to Richard Henderson

Le 30/04/2022 à 19:53, Richard Henderson a écrit :

Quoted 37 lines

Also mark raise_exception_ra and raise_exception, lest we generate a warning about helper_raise_exception returning.

---
  target/m68k/helper.h    | 2 +-
  target/m68k/op_helper.c | 5 +++--
  2 files changed, 4 insertions(+), 3 deletions(-)
target/m68k/helper.h +1 -1
index f016c4c1c2..c9bed2b884 100644
@@ -109,7 +109,7 @@ DEF_HELPER_3(set_mac_extu, void, env, i32, i32)
  DEF_HELPER_2(flush_flags, void, env, i32)
  DEF_HELPER_2(set_ccr, void, env, i32)
  DEF_HELPER_FLAGS_1(get_ccr, TCG_CALL_NO_WG_SE, i32, env)
-DEF_HELPER_2(raise_exception, void, env, i32)
+DEF_HELPER_2(raise_exception, noreturn, env, i32)
 
  DEF_HELPER_FLAGS_3(bfffo_reg, TCG_CALL_NO_RWG_SE, i32, i32, i32, i32)
 
target/m68k/op_helper.c +2 -1
index 61948d92bb..d9937ca8dc 100644
@@ -532,7 +532,8 @@ bool m68k_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
 
  #endif /* !CONFIG_USER_ONLY */
 
-static void raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr)
+G_NORETURN static void
+raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr)
  {
      CPUState *cs = env_cpu(env);
 

target/m68k/op_helper.c static void raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr) · line 547

Quoted 8 lines
target/m68k/op_helper.c +1 -1
@@ -540,7 +541,7 @@ static void raise_exception_ra(CPUM68KState *env, int tt, uintptr_t raddr)
      cpu_loop_exit_restore(cs, raddr);
  }
 
-static void raise_exception(CPUM68KState *env, int tt)
+G_NORETURN static void raise_exception(CPUM68KState *env, int tt)
  {
      raise_exception_ra(env, tt, 0);
  }
#38 lore raw
In reply to Richard Henderson

Ping.

r~

Quoted 45 lines · Richard Henderson

Changes for v4:
- Rebase, which requires QEMU_NORETURN -> G_NORETURN.
- Cast -4096 to abi_ulong for print_syscall_err.

r~

v1: https://lore.kernel.org/qemu-devel/[email protected]/
v2: https://lore.kernel.org/qemu-devel/[email protected]/
v3: https://lore.kernel.org/qemu-devel/[email protected]/

Richard Henderson (17):
target/m68k: Raise the TRAPn exception with the correct pc target/m68k: Switch over exception type in m68k_interrupt_all target/m68k: Fix coding style in m68k_interrupt_all linux-user/m68k: Handle EXCP_TRAP1 through EXCP_TRAP15 target/m68k: Remove retaddr in m68k_interrupt_all target/m68k: Fix address argument for EXCP_CHK target/m68k: Fix pc, c flag, and address argument for EXCP_DIV0 target/m68k: Fix address argument for EXCP_TRACE target/m68k: Fix stack frame for EXCP_ILLEGAL target/m68k: Implement TRAPcc
target/m68k: Implement TPF in terms of TRAPcc target/m68k: Implement TRAPV
target/m68k: Implement FTRAPcc
tests/tcg/m68k: Add trap.c
linux-user/strace: Fix print_syscall_err linux-user/strace: Adjust get_thread_area for m68k target/m68k: Mark helper_raise_exception as noreturn

  target/m68k/cpu.h              |   8 ++
  target/m68k/helper.h           |  14 +--
  linux-user/m68k/cpu_loop.c     |  11 +-
  linux-user/strace.c            |   4 +-
  target/m68k/cpu.c              |   1 +
  target/m68k/op_helper.c        | 173 ++++++++++++++++--------------
  target/m68k/translate.c        | 190 ++++++++++++++++++++++++---------
  tests/tcg/m68k/trap.c          | 129 ++++++++++++++++++++++
  linux-user/strace.list         |   5 +
  tests/tcg/m68k/Makefile.target |   3 +
  10 files changed, 394 insertions(+), 144 deletions(-)

create mode 100644 tests/tcg/m68k/trap.c