Skip to contentSkip to conversation

linux-user/s390x: Fix single-stepping SVC

PATCH In review Ilya Leoshkevich 4 participants 11 messages May 10, 2023 – Jul 7, 2023

lore
Share & tools
lore
#0 lore raw

cover linux-user/s390x: Fix single-stepping SVC

Hi,

I noticed that single-stepping SVC runs two instructions instead of one. The reason is that EXCP_SVC masks EXCP_DEBUG.
Patch 1 fixes this problem, patch 2 adds a test.

Btw, there is at least one more problem in that area, namely single-stepping instructions that cause e.g. SIGILL. Using the existing signals-s390x test as an example:

    (gdb) x/i $pc
    => 0x1001740 <illegal_op>:      .long   0x000007fe

    (gdb) si
    Program received signal SIGILL, Illegal instruction.
    (gdb) x/i $pc
    => 0x1001742 <after_illegal_op>:        br      %r14
    # So far so good.

    (gdb) si
    (gdb) x/i $pc
    => 0x10017b6 <handle_signal+6>: lay     %r15,-344(%r15)
    # Missed the first signal handler instruction!

I'm not sure what to do about it - the trivial fix to add gdb_handlesig(cpu, 0) to the end of handle_pending_signal() caused GDB to hang, and I haven't looked further yet.

Best regards,
Ilya

Ilya Leoshkevich (2):
linux-user/s390x: Fix single-stepping SVC
tests/tcg/s390x: Test single-stepping SVC

 linux-user/s390x/cpu_loop.c         |  9 ++++
 tests/tcg/s390x/Makefile.target     | 11 ++++-
 tests/tcg/s390x/gdbstub/test-svc.py | 64 +++++++++++++++++++++++++++++
 tests/tcg/s390x/hello-s390x-asm.S   | 20 +++++++++
 4 files changed, 103 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/s390x/gdbstub/test-svc.py
 create mode 100644 tests/tcg/s390x/hello-s390x-asm.S
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/2 linux-user/s390x: Fix single-stepping SVC

Currently single-stepping SVC executes two instructions. The reason is that EXCP_DEBUG for the SVC instruction itself is masked by EXCP_SVC. Fix by re-raising EXCP_DEBUG.

---
 linux-user/s390x/cpu_loop.c | 9 +++++++++
 1 file changed, 9 insertions(+)
linux-user/s390x/cpu_loop.c +9 -0
index 285bc60071a..8b7ac2879ef 100644
@@ -86,6 +86,15 @@ void cpu_loop(CPUS390XState *env)
             } else if (ret != -QEMU_ESIGRETURN) {
                 env->regs[2] = ret;
             }
+
+            if (unlikely(cs->singlestep_enabled)) {
+                /*
+                 * cpu_tb_exec() did not raise EXCP_DEBUG, because it has seen
+                 * that EXCP_SVC was already pending.
+                 */
+                cs->exception_index = EXCP_DEBUG;
+            }
+
             break;
 
         case EXCP_DEBUG:
Signature · 1 line
2.40.1
#2 lore raw
Quoted 3 lines · Ilya Leoshkevich

Currently single-stepping SVC executes two instructions. The reason is that EXCP_DEBUG for the SVC instruction itself is masked by EXCP_SVC. Fix by re-raising EXCP_DEBUG.

Is it a -stable material?

/mjt

#3 lore raw
Quoted 10 lines · Michael Tokarev
Quoted 5 lines · Ilya Leoshkevich

Currently single-stepping SVC executes two instructions. The reason is
that EXCP_DEBUG for the SVC instruction itself is masked by EXCP_SVC.
Fix by re-raising EXCP_DEBUG.

Is it a -stable material?

/mjt

While I would personally love to see this in -stable, I don't think it fits the official criteria - it's not a security fix and it's not a regression.

#4 lore raw
Quoted 7 lines · Ilya Leoshkevich
Quoted 1 line · Michael Tokarev

Is it a -stable material?

While I would personally love to see this in -stable, I don't think it fits the official criteria - it's not a security fix and it's not a regression.

I'm not sure I follow. It's definitely okay to include a bug fix into -stable, this has been done countless times in the past..

/mjt

#5 lore raw
Quoted 10 lines · Michael Tokarev
Quoted 8 lines · Ilya Leoshkevich
Quoted 1 line · Michael Tokarev

Is it a -stable material?

While I would personally love to see this in -stable, I don't think it
fits the official criteria - it's not a security fix and it's not a regression.

Quoted 4 lines

I'm not sure I follow. It's definitely okay to include a bug fix into -stable, this has been done countless times in the past..

/mjt

Okay, then let's include it into -stable.

It's just that I'm not too familiar with the QEMU -stable process, so I read [1], and it sounded quite strict.

[1] https://www.qemu.org/docs/master/devel/stable-process.html

#6 lore raw
Quoted 6 lines · Ilya Leoshkevich
Quoted 5 lines
Quoted 3 lines · Ilya Leoshkevich

While I would personally love to see this in -stable, I don't think it fits the official criteria - it's not a security fix and it's not a regression.

Quoted 6 lines

Okay, then let's include it into -stable.

It's just that I'm not too familiar with the QEMU -stable process, so I read [1], and it sounded quite strict.

[1] https://www.qemu.org/docs/master/devel/stable-process.html

The text there reads:

If you think the patch would be important for users of the current release (or for a distribution picking fixes), it is usually a good candidate for stable.

:)

Please Cc: [email protected] the next time you think something is good to have there.

/mjt

#7 lore raw

PATCH 2/2 tests/tcg/s390x: Test single-stepping SVC

In reply to Ilya Leoshkevich

Add a small test to prevent regressions.

---
 tests/tcg/s390x/Makefile.target     | 11 ++++-
 tests/tcg/s390x/gdbstub/test-svc.py | 64 +++++++++++++++++++++++++++++
 tests/tcg/s390x/hello-s390x-asm.S   | 20 +++++++++
 3 files changed, 94 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/s390x/gdbstub/test-svc.py
 create mode 100644 tests/tcg/s390x/hello-s390x-asm.S
tests/tcg/s390x/Makefile.target +10 -1
index 0031868b136..bd435e4dd63 100644
@@ -74,7 +74,16 @@ run-gdbstub-signals-s390x: signals-s390x
 		--bin $< --test $(S390X_SRC)/gdbstub/test-signals-s390x.py, \
 	mixing signals and debugging)
 
-EXTRA_RUNS += run-gdbstub-signals-s390x
+hello-s390x-asm: CFLAGS+=-nostdlib
+
+run-gdbstub-svc: hello-s390x-asm
+	$(call run-test, $@, $(GDB_SCRIPT) \
+		--gdb $(HAVE_GDB_BIN) \
+		--qemu $(QEMU) --qargs "$(QEMU_OPTS)" \
+		--bin $< --test $(S390X_SRC)/gdbstub/test-svc.py, \
+	single-stepping svc)
+
+EXTRA_RUNS += run-gdbstub-signals-s390x run-gdbstub-svc
 endif
 
 # MVX versions of sha512
tests/tcg/s390x/gdbstub/test-svc.py new file +64 -0

64 lines not on this page · raw message

tests/tcg/s390x/hello-s390x-asm.S new file +20 -0
new file mode 100644
index 00000000000..2e9faa16047
@@ -0,0 +1,20 @@
+/*
+ * Hello, World! in assembly.
+ */
+
+.globl _start
+_start:
+
+/* puts("Hello, World!"); */
+lghi %r2,1
+larl %r3,foo
+lghi %r4,foo_end-foo
+svc 4
+
+/* exit(0); */
+xgr %r2,%r2
+svc 1
+
+.align 2
+foo: .asciz "Hello, World!\n"
+foo_end:
Signature · 1 line
2.40.1
#9 lore raw

Re: [PATCH 2/2] tests/tcg/s390x: Test single-stepping SVC

In reply to Ilya Leoshkevich
Quoted 4 lines · Ilya Leoshkevich

Add a small test to prevent regressions.

---

...

Quoted 7 lines
tests/tcg/s390x/hello-s390x-asm.S new file +1 -0
new file mode 100644
index 00000000000..2e9faa16047
@@ -0,0 +1,20 @@
+/*

tests/tcg/s390x/hello-s390x-asm.S line 11

Quoted 10 lines
tests/tcg/s390x/hello-s390x-asm.S new file +10 -0
+ * Hello, World! in assembly.
+ */
+
+.globl _start
+_start:
+
+/* puts("Hello, World!"); */
+lghi %r2,1
+larl %r3,foo
+lghi %r4,foo_end-foo

Hi Ilya!

While testing your other currently pending s390x TCG patches, I noticed that this is failing with Clang (v16.0.1):

tests/tcg/s390x/hello-s390x-asm.S:11:10: error: invalid operand for instruction lghi %r4,foo_end-foo

         ^

make[1]: *** [Makefile:121: hello-s390x-asm] Error 1

Any ideas how to fix this?

Thomas

#10 lore raw
In reply to Ilya Leoshkevich
Quoted 28 lines · Ilya Leoshkevich

Hi,

I noticed that single-stepping SVC runs two instructions instead of one. The reason is that EXCP_SVC masks EXCP_DEBUG.
Patch 1 fixes this problem, patch 2 adds a test.

Btw, there is at least one more problem in that area, namely single-stepping instructions that cause e.g. SIGILL. Using the existing signals-s390x test as an example:

     (gdb) x/i $pc
     => 0x1001740 <illegal_op>:      .long   0x000007fe

     (gdb) si
     Program received signal SIGILL, Illegal instruction.
     (gdb) x/i $pc
     => 0x1001742 <after_illegal_op>:        br      %r14
     # So far so good.

     (gdb) si
     (gdb) x/i $pc
     => 0x10017b6 <handle_signal+6>: lay     %r15,-344(%r15)
     # Missed the first signal handler instruction!

I'm not sure what to do about it - the trivial fix to add gdb_handlesig(cpu, 0) to the end of handle_pending_signal() caused GDB to hang, and I haven't looked further yet.

Quoted 6 lines

Best regards,
Ilya

Ilya Leoshkevich (2):

   linux-user/s390x: Fix single-stepping SVC
   tests/tcg/s390x: Test single-stepping SVC

If there are no disagreements, I can take this through my s390x tree. Queued it for my next pull request now.

Thomas