Skip to contentSkip to conversation

target/nios2: add DISAS_NORETURN case for nothing more to generate

PATCH Superseded Wentong Wu 4 participants 20 messages Jun 29, 2020 – Jul 7, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wentong Wu

author
#0 lore raw

PATCH 1/3 target/nios2: add DISAS_NORETURN case for nothing more to generate

Add DISAS_NORETURN case for nothing more to generate because at runtime execution will never return from some helper call. And at the same time replace DISAS_UPDATE in t_gen_helper_raise_exception and gen_exception with the newly added DISAS_NORETURN.

---
 target/nios2/translate.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
target/nios2/translate.c +3 -2
index e17656e6..b052be85 100644
@@ -149,7 +149,7 @@ static void t_gen_helper_raise_exception(DisasContext *dc,
     tcg_gen_movi_tl(dc->cpu_R[R_PC], dc->pc);
     gen_helper_raise_exception(dc->cpu_env, tmp);
     tcg_temp_free_i32(tmp);
-    dc->is_jmp = DISAS_UPDATE;
+    dc->is_jmp = DISAS_NORETURN;
 }
 
 static bool use_goto_tb(DisasContext *dc, uint32_t dest)
@@ -802,7 +802,7 @@ static void gen_exception(DisasContext *dc, uint32_t excp)
     tcg_gen_movi_tl(cpu_R[R_PC], dc->pc);
     gen_helper_raise_exception(cpu_env, tmp);
     tcg_temp_free_i32(tmp);
-    dc->is_jmp = DISAS_UPDATE;
+    dc->is_jmp = DISAS_NORETURN;
 }
 
 /* generate intermediate code for basic block 'tb'.  */
@@ -877,6 +877,7 @@ void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
         tcg_gen_exit_tb(NULL, 0);
         break;
 
+    case DISAS_NORETURN:
     case DISAS_TB_JUMP:
         /* nothing more to generate */
         break;
Signature · 1 line
2.21.3
author
#1 lore raw

PATCH 2/3 target/nios2: in line the semantics of DISAS_UPDATE with other targets

In line the semantics of DISAS_UPDATE on nios2 target with other targets which is to explicitly write the PC back into the cpu state before doing a tcg_gen_exit_tb().

---
 target/nios2/translate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/nios2/translate.c +1 -1
index b052be85..83c10eb2 100644
@@ -865,6 +865,7 @@ void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
     /* Indicate where the next block should start */
     switch (dc->is_jmp) {
     case DISAS_NEXT:
+    case DISAS_UPDATE:
         /* Save the current PC back into the CPU register */
         tcg_gen_movi_tl(cpu_R[R_PC], dc->pc);
         tcg_gen_exit_tb(NULL, 0);
@@ -872,7 +873,6 @@ void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
 
     default:
     case DISAS_JUMP:
-    case DISAS_UPDATE:
         /* The jump will already have updated the PC register */
         tcg_gen_exit_tb(NULL, 0);
         break;
Signature · 1 line
2.21.3
#2 lore raw

Re: [PATCH 2/3] target/nios2: in line the semantics of DISAS_UPDATE with other targets

Quoted 8 lines · Wentong Wu

In line the semantics of DISAS_UPDATE on nios2 target with other targets which is to explicitly write the PC back into the cpu state before doing a tcg_gen_exit_tb().

---
 target/nios2/translate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Or simply remove it as unused, now that you've replaced the existing users with DISAS_NORETURN.

r~

Reviewed-by
#3 lore raw

Re: [PATCH 2/3] target/nios2: in line the semantics of DISAS_UPDATE with other targets

Quoted 5 lines · Richard Henderson
Quoted 4 lines · Wentong Wu

In line the semantics of DISAS_UPDATE on nios2 target with other targets which is to explicitly write the PC back into the cpu state before doing a tcg_gen_exit_tb().

Quoted 7 lines
Quoted 4 lines
---
 target/nios2/translate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Or simply remove it as unused, now that you've replaced the existing users with DISAS_NORETURN.

Nevermind, you're using it in the next patch.

r~

author
#4 lore raw

PATCH 3/3 target/nios2: Use gen_io_start around wrctl instruction

In reply to Wentong Wu

wrctl instruction on nios2 target will cause checking cpu interrupt but tcg_handle_interrupt() will call cpu_abort() if the CPU gets an interrupt while it's not in 'can do IO' state, so add gen_io_start around wrctl instruction. Also at the same time, end the onging TB with DISAS_UPDATE.

---
 target/nios2/translate.c | 5 +++++
 1 file changed, 5 insertions(+)
target/nios2/translate.c +5 -0
index 83c10eb2..51347ada 100644
@@ -32,6 +32,7 @@
 #include "exec/cpu_ldst.h"
 #include "exec/translator.h"
 #include "qemu/qemu-print.h"
+#include "exec/gen-icount.h"
 
 /* is_jmp field values */
 #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
@@ -518,7 +519,11 @@ static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags)
     /* If interrupts were enabled using WRCTL, trigger them. */
 #if !defined(CONFIG_USER_ONLY)
     if ((instr.imm5 + CR_BASE) == CR_STATUS) {
+        if (tb_cflags(dc->tb) & CF_USE_ICOUNT) {
+            gen_io_start();
+        }
         gen_helper_check_interrupts(dc->cpu_env);
+        dc->is_jmp = DISAS_UPDATE;
     }
 #endif
 }
Signature · 1 line
2.21.3
#5 lore raw

RE: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

Hi,
Could you please take a look the new patch?

Thanks

Quoted 34 lines

-----Original Message-----
Subject: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction
wrctl instruction on nios2 target will cause checking cpu interrupt but tcg_handle_interrupt() will call cpu_abort() if the CPU gets an interrupt while it's not in 'can do IO' state, so add gen_io_start around wrctl instruction. Also at the same time, end the onging TB with DISAS_UPDATE.

---
target/nios2/translate.c | 5 +++++

1 file changed, 5 insertions(+)
target/nios2/translate.c +5 -1
@@ -32,6 +32,7 @@
 #include "exec/cpu_ldst.h"
 #include "exec/translator.h"
 #include "qemu/qemu-print.h"
+#include "exec/gen-icount.h"
 
 /* is_jmp field values */
 #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
@@ -518,7 +519,11 @@ static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags)
    /* If interrupts were enabled using WRCTL, trigger them. */  #if !defined(CONFIG_USER_ONLY)
   if ((instr.imm5 + CR_BASE) == CR_STATUS) {
+        if (tb_cflags(dc->tb) & CF_USE_ICOUNT) {
+            gen_io_start();
+        }
         gen_helper_check_interrupts(dc->cpu_env);
+        dc->is_jmp = DISAS_UPDATE;
   }
 #endif
 }
--

2.21.3

#6 lore raw

Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

In reply to Wentong Wu
Quoted 24 lines · Wentong Wu

wrctl instruction on nios2 target will cause checking cpu interrupt but tcg_handle_interrupt() will call cpu_abort() if the CPU gets an interrupt while it's not in 'can do IO' state, so add gen_io_start around wrctl instruction. Also at the same time, end the onging TB with DISAS_UPDATE.

---
 target/nios2/translate.c | 5 +++++
 1 file changed, 5 insertions(+)
target/nios2/translate.c +1 -0
index 83c10eb2..51347ada 100644
@@ -32,6 +32,7 @@
 #include "exec/cpu_ldst.h"
 #include "exec/translator.h"
 #include "qemu/qemu-print.h"
+#include "exec/gen-icount.h"
 
 /* is_jmp field values */
 #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */

target/nios2/translate.c static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags) · line 528

Quoted 10 lines
target/nios2/translate.c +4 -0
@@ -518,7 +519,11 @@ static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags)
     /* If interrupts were enabled using WRCTL, trigger them. */
 #if !defined(CONFIG_USER_ONLY)
     if ((instr.imm5 + CR_BASE) == CR_STATUS) {
+        if (tb_cflags(dc->tb) & CF_USE_ICOUNT) {
+            gen_io_start();
+        }
         gen_helper_check_interrupts(dc->cpu_env);
+        dc->is_jmp = DISAS_UPDATE;
     }
 #endif

This isn't right. Not so much the gen_io_start portion, but the entire existence of helper_check_interrupt.

The correct way to acknowledge interrupts after changing an interrupt mask bit is to exit the TB back to the cpu main loop.
Which you are doing here with DISAS_UPDATE, so that part is fine. (Although you could merge that into the switch statement above.)

Looking at nios_pic_cpu_handler, there are two other bugs:

1) Get rid of env->irq_pending and use cpu_interrupt/cpu_reset_interrupt instead.

2) Do not check env->regs[CR_STATUS] & CR_STATUS_PIE. That variable does not belong to the pic and should not be checked there. The check belongs in nios2_cpu_exec_interrupt, and is in fact already there.

r~

#7 lore raw

RE: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

-----Original Message-----
From: Richard Henderson
Sent: Friday, July 3, 2020 2:54 AM
To: Wu, Wentong <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

On 6/29/20 9:05 AM, Wentong Wu wrote:
> wrctl instruction on nios2 target will cause checking cpu interrupt
> but tcg_handle_interrupt() will call cpu_abort() if the CPU gets an
> interrupt while it's not in 'can do IO'
> state, so add gen_io_start around wrctl instruction. Also at the same
> time, end the onging TB with DISAS_UPDATE.
>
> Signed-off-by: Wentong Wu <[email protected]<mailto:[email protected]>> > ---
> target/nios2/translate.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/target/nios2/translate.c b/target/nios2/translate.c index
> 83c10eb2..51347ada 100644
> --- a/target/nios2/translate.c
> +++ b/target/nios2/translate.c
> @@ -32,6 +32,7 @@
> #include "exec/cpu_ldst.h"
> #include "exec/translator.h"
> #include "qemu/qemu-print.h"
> +#include "exec/gen-icount.h"
>
> /* is_jmp field values */
> #define DISAS_JUMP DISAS_TARGET_0 /* only pc was modified dynamically */
> @@ -518,7 +519,11 @@ static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags)
> /* If interrupts were enabled using WRCTL, trigger them. */ #if
> !defined(CONFIG_USER_ONLY)
> if ((instr.imm5 + CR_BASE) == CR_STATUS) {
> + if (tb_cflags(dc->tb) & CF_USE_ICOUNT) {
> + gen_io_start();
> + }
> gen_helper_check_interrupts(dc->cpu_env);
> + dc->is_jmp = DISAS_UPDATE;
> }
> #endif

This isn't right. Not so much the gen_io_start portion, but the entire existence of helper_check_interrupt.

The correct way to acknowledge interrupts after changing an interrupt mask bit is to exit the TB back to the cpu main loop. Which you are doing here with DISAS_UPDATE, so that part is fine. (Although you could merge that into the switch statement above.)

Looking at nios_pic_cpu_handler, there are two other bugs:

1) Get rid of env->irq_pending and use cpu_interrupt/cpu_reset_interrupt instead

IMHO, we need env->irq_pending, because if ((env->regs[CR_STATUS] & CR_STATUS_PIE) == False), it can indicate there is interrupt pending, we can exit to main loop to handle the interrupt at once if guest code enable irq later.

2) Do not check env->regs[CR_STATUS] & CR_STATUS_PIE. That variable does not belong to the pic and should not be checked there. The check belongs in nios2_cpu_exec_interrupt, and is in fact already there.
But that will cause some assert happen if device trigger irq with env->regs[CR_STATUS] & CR_STATUS_PIE = False

BR

#8 lore raw

RE: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

In reply to Richard Henderson

Correct the format

Quoted 45 lines

-----Original Message-----
From: Richard Henderson <[email protected]>
Sent: Friday, July 3, 2020 2:54 AM
To: Wu, Wentong <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

Quoted 24 lines · Wentong Wu

wrctl instruction on nios2 target will cause checking cpu interrupt but tcg_handle_interrupt() will call cpu_abort() if the CPU gets an interrupt while it's not in 'can do IO'
state, so add gen_io_start around wrctl instruction. Also at the same time, end the onging TB with DISAS_UPDATE.

---
 target/nios2/translate.c | 5 +++++
 1 file changed, 5 insertions(+)
target/nios2/translate.c index renamed from target/nios2/translate.c +0 -0

83c10eb2..51347ada 100644

target/nios2/translate.c +1 -0
@@ -32,6 +32,7 @@
 #include "exec/cpu_ldst.h"
 #include "exec/translator.h"
 #include "qemu/qemu-print.h"
+#include "exec/gen-icount.h"
 
 /* is_jmp field values */
 #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */

target/nios2/translate.c static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags) · line 528

Quoted 10 lines
target/nios2/translate.c +4 -0
@@ -518,7 +519,11 @@ static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags)
     /* If interrupts were enabled using WRCTL, trigger them. */  #if 
 !defined(CONFIG_USER_ONLY)
     if ((instr.imm5 + CR_BASE) == CR_STATUS) {
+        if (tb_cflags(dc->tb) & CF_USE_ICOUNT) {
+            gen_io_start();
+        }
         gen_helper_check_interrupts(dc->cpu_env);
+        dc->is_jmp = DISAS_UPDATE;
     }
 #endif

This isn't right. Not so much the gen_io_start portion, but the entire existence of helper_check_interrupt.

Quoted 6 lines

The correct way to acknowledge interrupts after changing an interrupt mask bit is to exit the TB back to the cpu main loop. Which you are doing here with DISAS_UPDATE, so that part is fine. (Although you could merge that into the switch statement above.)

Looking at nios_pic_cpu_handler, there are two other bugs:

1) Get rid of env->irq_pending and use cpu_interrupt/cpu_reset_interrupt instead.

IMHO, we need env->irq_pending, because if ((env->regs[CR_STATUS] & CR_STATUS_PIE) == False), it can indicate there is interrupt pending, we can exit to main loop to handle the interrupt at once if guest code enable irq later.

Quoted 1 line

2) Do not check env->regs[CR_STATUS] & CR_STATUS_PIE. That variable does not belong to the pic and should not be checked there. The check belongs in nios2_cpu_exec_interrupt, and is in fact already there.

But that will cause some assert happen if device trigger irq with env->regs[CR_STATUS] & CR_STATUS_PIE = False

BR

#9 lore raw

RE: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

In reply to Richard Henderson

Correct the format

Quoted 45 lines

-----Original Message-----
From: Richard Henderson <[email protected]>
Sent: Friday, July 3, 2020 2:54 AM
To: Wu, Wentong <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

Quoted 24 lines · Wentong Wu

wrctl instruction on nios2 target will cause checking cpu interrupt but tcg_handle_interrupt() will call cpu_abort() if the CPU gets an interrupt while it's not in 'can do IO'
state, so add gen_io_start around wrctl instruction. Also at the same time, end the onging TB with DISAS_UPDATE.

---
 target/nios2/translate.c | 5 +++++
 1 file changed, 5 insertions(+)
target/nios2/translate.c index renamed from target/nios2/translate.c +0 -0

83c10eb2..51347ada 100644

target/nios2/translate.c +1 -0
@@ -32,6 +32,7 @@
 #include "exec/cpu_ldst.h"
 #include "exec/translator.h"
 #include "qemu/qemu-print.h"
+#include "exec/gen-icount.h"
 
 /* is_jmp field values */
 #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */

target/nios2/translate.c static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags) · line 528

Quoted 10 lines
target/nios2/translate.c +4 -0
@@ -518,7 +519,11 @@ static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags)
     /* If interrupts were enabled using WRCTL, trigger them. */  #if 
 !defined(CONFIG_USER_ONLY)
     if ((instr.imm5 + CR_BASE) == CR_STATUS) {
+        if (tb_cflags(dc->tb) & CF_USE_ICOUNT) {
+            gen_io_start();
+        }
         gen_helper_check_interrupts(dc->cpu_env);
+        dc->is_jmp = DISAS_UPDATE;
     }
 #endif

This isn't right. Not so much the gen_io_start portion, but the entire existence of helper_check_interrupt.

Quoted 6 lines

The correct way to acknowledge interrupts after changing an interrupt mask bit is to exit the TB back to the cpu main loop. Which you are doing here with DISAS_UPDATE, so that part is fine. (Although you could merge that into the switch statement above.)

Looking at nios_pic_cpu_handler, there are two other bugs:

1) Get rid of env->irq_pending and use cpu_interrupt/cpu_reset_interrupt instead.

IMHO, we need env->irq_pending, because if ((env->regs[CR_STATUS] & CR_STATUS_PIE) == False), it can indicate there is interrupt pending, we can exit to main loop to handle the interrupt at once if guest code enable irq later.

Quoted 1 line

2) Do not check env->regs[CR_STATUS] & CR_STATUS_PIE. That variable does not belong to the pic and should not be checked there. The check belongs in nios2_cpu_exec_interrupt, and is in fact already there.

But that will cause some assert happen if device trigger irq with env->regs[CR_STATUS] & CR_STATUS_PIE = False

BR

#10 lore raw

Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

In reply to Richard Henderson
Quoted 2 lines · Richard Henderson

This isn't right. Not so much the gen_io_start portion, but the entire existence of helper_check_interrupt.

I agree that it looks bogus (xtensa has a similar helper as well, incidentally), but fixing all that stuff up is more effort than the relatively small job of getting the icount handling right for the code we have today, which is why I suggested to Wentong that we could just do this bit for now.

thanks
-- PMM

#11 lore raw

Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

Quoted 6 lines · Peter Maydell
Quoted 2 lines · Richard Henderson

This isn't right. Not so much the gen_io_start portion, but the entire existence of helper_check_interrupt.

I agree that it looks bogus (xtensa has a similar helper as well, incidentally),

I think there was a reason for it. According to Richard

Quoted 2 lines

The correct way to acknowledge interrupts after changing an interrupt mask bit is to exit the TB back to the cpu main loop.

But if I do this change for Xtensa I get a bunch of test_interrupt failures that indicate that the interrupt that should have been taken wasn't taken. FTR here's the change that I tested, did I miss something?

target/xtensa/translate.c +1 -9
index fdf47642e6f1..85e8d65f169d 100644
@@ -631,18 +631,10 @@ static int gen_postprocess(DisasContext *dc, int slot)
 {
     uint32_t op_flags = dc->op_flags;
 
-#ifndef CONFIG_USER_ONLY
-    if (op_flags & XTENSA_OP_CHECK_INTERRUPTS) {
-        if (tb_cflags(dc->base.tb) & CF_USE_ICOUNT) {
-            gen_io_start();
-        }
-        gen_helper_check_interrupts(cpu_env);
-    }
-#endif
     if (op_flags & XTENSA_OP_SYNC_REGISTER_WINDOW) {
         gen_helper_sync_windowbase(cpu_env);
     }
-    if (op_flags & XTENSA_OP_EXIT_TB_M1) {
+    if (op_flags & (XTENSA_OP_EXIT_TB_M1 | XTENSA_OP_CHECK_INTERRUPTS)) {
         slot = -1;
     }
     return slot;
@@ -1175,7 +1167,7 @@ static void disas_xtensa_insn(CPUXtensaState

*env, DisasContext *dc)

     if (dc->base.is_jmp == DISAS_NEXT) {
         gen_postprocess(dc, 0);
         dc->op_flags = 0;
-        if (op_flags & XTENSA_OP_EXIT_TB_M1) {
+        if (op_flags & (XTENSA_OP_EXIT_TB_M1 | XTENSA_OP_CHECK_INTERRUPTS)) {
             /* Change in mmu index, memory mapping or tb->flags; exit tb */
             gen_jumpi_check_loop_end(dc, -1);
         } else if (op_flags & XTENSA_OP_EXIT_TB_0) {
Signature · 2 lines
Thanks.
-- Max
#12 lore raw

Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

Quoted 7 lines · Max Filippov
Quoted 5 lines · Peter Maydell
Quoted 2 lines · Richard Henderson

This isn't right. Not so much the gen_io_start portion, but the entire existence of helper_check_interrupt.

I agree that it looks bogus (xtensa has a similar helper as well, incidentally),

I think there was a reason for it.

...and the reason is that this helper calls cpu_[re]set_interrupt to update CPU_INTERRUPT_HARD, which makes exit to the main CPU loop do something to handle IRQ.
Maybe 'check_interrupt' is not a good name for that, but the action taken there seems right to me.

Signature · 2 lines
Thanks.
-- Max
#13 lore raw

Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

Quoted 7 lines · Max Filippov
Quoted 5 lines · Max Filippov
Quoted 4 lines · Peter Maydell
Quoted 2 lines · Richard Henderson

This isn't right. Not so much the gen_io_start portion, but the entire existence of helper_check_interrupt.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

I agree that it looks bogus (xtensa has a similar helper as well, incidentally),

I think there was a reason for it.

...and the reason is that this helper calls cpu_[re]set_interrupt to update CPU_INTERRUPT_HARD, which makes exit to the main CPU loop do something to handle IRQ.
Maybe 'check_interrupt' is not a good name for that, but the action taken there seems right to me.

Usually I would expect that CPU_INTERRUPT_HARD would be set by whatever was setting the interrupt (often a handler for an inbound qemu_irq line to the CPU). Then the cpu_exec_interrupt hook only actually does something if both the INTERRUPT_HARD bit is set and the CPU register says "and interrupts are unmasked". If you have a design like that then "unmasking interrupts just means going out to the main loop" will work.

thanks
-- PMM

#14 lore raw

RE: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

In reply to Richard Henderson

Maybe below patch will reduce some overhead, because currently it will exit to main loop to handle interrupt but if with (env->regs[CR_STATUS] & CR_STATUS_PIE) = False, it does nothing except set env->irq_pending again.

hw/nios2/cpu_pic.c +2 -1
index 1c1989d5..5ea7e52a 100644
@@ -54,7 +54,8 @@ static void nios2_pic_cpu_handler(void *opaque, int irq, int level)
 
 void nios2_check_interrupts(CPUNios2State *env)
 {
-    if (env->irq_pending) {
+    if (env->irq_pending &&
+        (env->regs[CR_STATUS] & CR_STATUS_PIE)) {
         env->irq_pending = 0;
         cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HARD);
     }

-----Original Message-----
From: Richard Henderson <[email protected]>
Sent: Friday, July 3, 2020 2:54 AM
To: Wu, Wentong <[email protected]>; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

Quoted 24 lines · Wentong Wu

wrctl instruction on nios2 target will cause checking cpu interrupt but tcg_handle_interrupt() will call cpu_abort() if the CPU gets an interrupt while it's not in 'can do IO'
state, so add gen_io_start around wrctl instruction. Also at the same time, end the onging TB with DISAS_UPDATE.

---
 target/nios2/translate.c | 5 +++++
 1 file changed, 5 insertions(+)
target/nios2/translate.c index renamed from target/nios2/translate.c +0 -0

83c10eb2..51347ada 100644

target/nios2/translate.c +1 -0
@@ -32,6 +32,7 @@
 #include "exec/cpu_ldst.h"
 #include "exec/translator.h"
 #include "qemu/qemu-print.h"
+#include "exec/gen-icount.h"
 
 /* is_jmp field values */
 #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */

target/nios2/translate.c static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags) · line 528

Quoted 10 lines
target/nios2/translate.c +4 -0
@@ -518,7 +519,11 @@ static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags)
     /* If interrupts were enabled using WRCTL, trigger them. */  #if 
 !defined(CONFIG_USER_ONLY)
     if ((instr.imm5 + CR_BASE) == CR_STATUS) {
+        if (tb_cflags(dc->tb) & CF_USE_ICOUNT) {
+            gen_io_start();
+        }
         gen_helper_check_interrupts(dc->cpu_env);
+        dc->is_jmp = DISAS_UPDATE;
     }
 #endif

This isn't right. Not so much the gen_io_start portion, but the entire existence of helper_check_interrupt.

The correct way to acknowledge interrupts after changing an interrupt mask bit is to exit the TB back to the cpu main loop. Which you are doing here with DISAS_UPDATE, so that part is fine. (Although you could merge that into the switch statement above.)

Looking at nios_pic_cpu_handler, there are two other bugs:

1) Get rid of env->irq_pending and use cpu_interrupt/cpu_reset_interrupt instead.

2) Do not check env->regs[CR_STATUS] & CR_STATUS_PIE. That variable does not belong to the pic and should not be checked there. The check belongs in nios2_cpu_exec_interrupt, and is in fact already there.

r~

#15 lore raw

RE: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

In reply to Wentong Wu

HI Peter,
Could you please take a look at this patch which is following your pervious suggestion?

Thanks

-----Original Message-----
From: Wu, Wentong <[email protected]>
Sent: Tuesday, June 30, 2020 12:06 AM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; Wu, Wentong <[email protected]>
Subject: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

wrctl instruction on nios2 target will cause checking cpu interrupt but tcg_handle_interrupt() will call cpu_abort() if the CPU gets an interrupt while it's not in 'can do IO' state, so add gen_io_start around wrctl instruction. Also at the same time, end the onging TB with DISAS_UPDATE.

---
 target/nios2/translate.c | 5 +++++
 1 file changed, 5 insertions(+)
target/nios2/translate.c +5 -1
@@ -32,6 +32,7 @@
 #include "exec/cpu_ldst.h"
 #include "exec/translator.h"
 #include "qemu/qemu-print.h"
+#include "exec/gen-icount.h"
 
 /* is_jmp field values */
 #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
@@ -518,7 +519,11 @@ static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags)
     /* If interrupts were enabled using WRCTL, trigger them. */  #if !defined(CONFIG_USER_ONLY)
     if ((instr.imm5 + CR_BASE) == CR_STATUS) {
+        if (tb_cflags(dc->tb) & CF_USE_ICOUNT) {
+            gen_io_start();
+        }
         gen_helper_check_interrupts(dc->cpu_env);
+        dc->is_jmp = DISAS_UPDATE;
     }
 #endif
 }
--

2.21.3

Reviewed-by on v1 3/3
#16 lore raw

Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

In reply to Wentong Wu
Quoted 26 lines · Wentong Wu

wrctl instruction on nios2 target will cause checking cpu interrupt but tcg_handle_interrupt() will call cpu_abort() if the CPU gets an interrupt while it's not in 'can do IO' state, so add gen_io_start around wrctl instruction. Also at the same time, end the onging TB with DISAS_UPDATE.

---
 target/nios2/translate.c | 5 +++++
 1 file changed, 5 insertions(+)
target/nios2/translate.c +1 -0
index 83c10eb2..51347ada 100644
@@ -32,6 +32,7 @@
 #include "exec/cpu_ldst.h"
 #include "exec/translator.h"
 #include "qemu/qemu-print.h"
+#include "exec/gen-icount.h"
 
 /* is_jmp field values */
 #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
@@ -518,7 +519,11 @@ static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags)
     /* If interrupts were enabled using WRCTL, trigger them. */

target/nios2/translate.c static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags) · line 529

Quoted 10 lines
target/nios2/translate.c +4 -0
 #if !defined(CONFIG_USER_ONLY)
     if ((instr.imm5 + CR_BASE) == CR_STATUS) {
+        if (tb_cflags(dc->tb) & CF_USE_ICOUNT) {
+            gen_io_start();
+        }
         gen_helper_check_interrupts(dc->cpu_env);
+        dc->is_jmp = DISAS_UPDATE;
     }
 #endif
 }

though as Richard notes ideally the interrupt handling code here should be rewritten because the check_interrupts helper is a very weird way to implement things.

thanks
-- PMM

author Reviewed-by Peter Maydell
#17 lore raw

RE: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

Thanks, I think we can get this series merged currently.

-----Original Message-----
From: Peter Maydell <[email protected]>
Sent: Monday, July 6, 2020 1:10 AM
To: Wu, Wentong <[email protected]>
Cc: QEMU Developers <[email protected]>; QEMU Trivial <[email protected]>; Chris Wulff <[email protected]>; Marek Vasut <[email protected]>
Subject: Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

Quoted 26 lines · Wentong Wu

wrctl instruction on nios2 target will cause checking cpu interrupt but tcg_handle_interrupt() will call cpu_abort() if the CPU gets an interrupt while it's not in 'can do IO'
state, so add gen_io_start around wrctl instruction. Also at the same time, end the onging TB with DISAS_UPDATE.

---
 target/nios2/translate.c | 5 +++++
 1 file changed, 5 insertions(+)
target/nios2/translate.c index renamed from target/nios2/translate.c +0 -0

83c10eb2..51347ada 100644

target/nios2/translate.c +1 -0
@@ -32,6 +32,7 @@
 #include "exec/cpu_ldst.h"
 #include "exec/translator.h"
 #include "qemu/qemu-print.h"
+#include "exec/gen-icount.h"
 
 /* is_jmp field values */
 #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
@@ -518,7 +519,11 @@ static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags)
     /* If interrupts were enabled using WRCTL, trigger them. */  #if 

target/nios2/translate.c static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags) · line 529

Quoted 10 lines
target/nios2/translate.c +4 -0
 !defined(CONFIG_USER_ONLY)
     if ((instr.imm5 + CR_BASE) == CR_STATUS) {
+        if (tb_cflags(dc->tb) & CF_USE_ICOUNT) {
+            gen_io_start();
+        }
         gen_helper_check_interrupts(dc->cpu_env);
+        dc->is_jmp = DISAS_UPDATE;
     }
 #endif
 }

though as Richard notes ideally the interrupt handling code here should be rewritten because the check_interrupts helper is a very weird way to implement things.

thanks
-- PMM

#18 lore raw

RE: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

In reply to Peter Maydell

Hi,
I think we can get this patch series merged first in order to get qemu_nios2 working with icount, actually we are blocked by it for some time. BTW if maintainers(Chris Wulff and Marek Vasut) don't have time for the re-work, I'd like to take it.

Thanks

Quoted 51 lines

-----Original Message-----
From: Peter Maydell <[email protected]>
Sent: Monday, July 6, 2020 1:10 AM
To: Wu, Wentong <[email protected]>
Cc: QEMU Developers <[email protected]>; QEMU Trivial <[email protected]>; Chris Wulff <[email protected]>; Marek Vasut <[email protected]>
Subject: Re: [PATCH 3/3] target/nios2: Use gen_io_start around wrctl instruction

Quoted 26 lines · Wentong Wu

wrctl instruction on nios2 target will cause checking cpu interrupt but tcg_handle_interrupt() will call cpu_abort() if the CPU gets an interrupt while it's not in 'can do IO'
state, so add gen_io_start around wrctl instruction. Also at the same time, end the onging TB with DISAS_UPDATE.

---
 target/nios2/translate.c | 5 +++++
 1 file changed, 5 insertions(+)
target/nios2/translate.c index renamed from target/nios2/translate.c +0 -0

83c10eb2..51347ada 100644

target/nios2/translate.c +1 -0
@@ -32,6 +32,7 @@
 #include "exec/cpu_ldst.h"
 #include "exec/translator.h"
 #include "qemu/qemu-print.h"
+#include "exec/gen-icount.h"
 
 /* is_jmp field values */
 #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
@@ -518,7 +519,11 @@ static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags)
     /* If interrupts were enabled using WRCTL, trigger them. */  #if 

target/nios2/translate.c static void wrctl(DisasContext *dc, uint32_t code, uint32_t flags) · line 529

Quoted 10 lines
target/nios2/translate.c +4 -0
 !defined(CONFIG_USER_ONLY)
     if ((instr.imm5 + CR_BASE) == CR_STATUS) {
+        if (tb_cflags(dc->tb) & CF_USE_ICOUNT) {
+            gen_io_start();
+        }
         gen_helper_check_interrupts(dc->cpu_env);
+        dc->is_jmp = DISAS_UPDATE;
     }
 #endif
 }

though as Richard notes ideally the interrupt handling code here should be rewritten because the check_interrupts helper is a very weird way to implement things.

thanks
-- PMM

Reviewed-by on v1 1/3
#19 lore raw
In reply to Wentong Wu
Quoted 9 lines · Wentong Wu

Add DISAS_NORETURN case for nothing more to generate because at runtime execution will never return from some helper call. And at the same time replace DISAS_UPDATE in t_gen_helper_raise_exception and gen_exception with the newly added DISAS_NORETURN.

---
 target/nios2/translate.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

r~