Skip to contentSkip to conversation

target/ppc: fix single-step exception regression

PATCH Superseded Luis Pires 3 participants 5 messages Jun 1, 2021 – Jun 2, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Luis Pires

author
#0 lore raw

PATCH 1/1 target/ppc: fix single-step exception regression

Commit 6086c75 (target/ppc: Replace POWERPC_EXCP_BRANCH with DISAS_NORETURN) broke the generation of exceptions when CPU_SINGLE_STEP or CPU_BRANCH_STEP were set, due to nip always being reset to the address of the current instruction.
This fix leaves nip untouched when generating the exception.

---
 target/ppc/translate.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
target/ppc/translate.c +6 -3
index ea200f9637..0dd04696a6 100644
@@ -4646,8 +4646,7 @@ static void gen_lookup_and_goto_ptr(DisasContext *ctx)
         if (sse & GDBSTUB_SINGLE_STEP) {
             gen_debug_exception(ctx);
         } else if (sse & (CPU_SINGLE_STEP | CPU_BRANCH_STEP)) {
-            uint32_t excp = gen_prep_dbgex(ctx);
-            gen_exception(ctx, excp);
+            gen_helper_raise_exception(cpu_env, tcg_constant_i32(gen_prep_dbgex(ctx)));
         } else {
             tcg_gen_exit_tb(NULL, 0);
         }
@@ -9128,7 +9127,11 @@ static void ppc_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
         }
         /* else CPU_SINGLE_STEP... */
         if (nip <= 0x100 || nip > 0xf00) {
-            gen_exception(ctx, gen_prep_dbgex(ctx));
+            if (is_jmp == DISAS_EXIT || is_jmp == DISAS_CHAIN) {
+                /* We have not updated nip yet, so do it now */
+                gen_update_nip(ctx, nip);
+            }
+            gen_helper_raise_exception(cpu_env, tcg_constant_i32(gen_prep_dbgex(ctx)));
             return;
         }
     }
Signature · 1 line
2.25.1
#1 lore raw

target/ppc/translate.c static void ppc_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs) · line 9133

Quoted 4 lines · Luis Pires
target/ppc/translate.c +4 -0
+            if (is_jmp == DISAS_EXIT || is_jmp == DISAS_CHAIN) {
+                /* We have not updated nip yet, so do it now */
+                gen_update_nip(ctx, nip);
+            }

This is incorrect. Both EXIT and CHAIN *have* updated nip, but to something that isn't the next instruction. E.g. return from interrupt.

r~

#2 lore raw
Quoted 8 lines · Richard Henderson

target/ppc/translate.c static void ppc_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs) · line 9133

Quoted 4 lines · Luis Pires
target/ppc/translate.c +4 -0
+            if (is_jmp == DISAS_EXIT || is_jmp == DISAS_CHAIN) {
+                /* We have not updated nip yet, so do it now */
+                gen_update_nip(ctx, nip);
+            }

This is incorrect. Both EXIT and CHAIN *have* updated nip, but to something that isn't the next instruction. E.g. return from interrupt.

Any theories on what's actually causing the regression, then?

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#3 lore raw
Quoted 7 lines · David Gibson
Quoted 6 lines · Richard Henderson
Quoted 4 lines · Luis Pires
target/ppc/translate.c +4 -0
+            if (is_jmp == DISAS_EXIT || is_jmp == DISAS_CHAIN) {
+                /* We have not updated nip yet, so do it now */
+                gen_update_nip(ctx, nip);
+            }
Quoted 5 lines
Quoted 2 lines

This is incorrect. Both EXIT and CHAIN *have* updated nip, but to something that isn't the next instruction. E.g. return from interrupt.

Any theories on what's actually causing the regression, then?

I would have thought the first hunk would have some effect. But otherwise this is the first I've heard of the problem. Description? Reproduction instruction?

r~

#4 lore raw

From: Richard Henderson <[email protected]>

Quoted 8 lines · David Gibson
Quoted 7 lines · Richard Henderson
Quoted 6 lines · Luis Pires
Quoted 4 lines
target/ppc/translate.c +4 -0
+            if (is_jmp == DISAS_EXIT || is_jmp == DISAS_CHAIN) {
+                /* We have not updated nip yet, so do it now */
+                gen_update_nip(ctx, nip);
+            }
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

This is incorrect. Both EXIT and CHAIN *have* updated nip, but to something that isn't the next instruction. E.g. return from interrupt.

Any theories on what's actually causing the regression, then?

I would have thought the first hunk would have some effect. But otherwise this is the first I've heard of the problem. Description? Reproduction instruction?

While trying to debug his implementation for one of the new Power ISA 3.1 instructions, Matheus (cc'ed) hit a problem where he'd get a SIGSEGV when using gdb to debug a process inside a guest after commit 6086c75.

Steps to reproduce:
- Inside a ppc64 VM (running with qemu-system), run gdb to start debugging any program (I tested with /bin/ls, /bin/true and also a simple hello world)
- Run the binary from within gdb and you'll get a SIGSEGV
Running the same program outside gdb works fine.

By looking at 6086c75, I noticed this was happening because when gen_exception() was called from gen_lookup_and_goto_ptr() (due to CPU_SINGLE_STEP | CPU_BRANCH_STEP), nip was being reset to ctx->cia. Before that commit this didn't happen, because ctx->exception != POWERPC_EXCP_NONE.

--
Luis Pires
Instituto de Pesquisas ELDORADO
Aviso Legal - Disclaimer <https://www.eldorado.org.br/disclaimer.html>