Skip to contentSkip to conversation

linux-user/i386: Emulate orig_ax

PATCH In review Ilya Leoshkevich 1 participant 7 messages Sep 12, 2024 – Oct 10, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover linux-user/i386: Emulate orig_ax

v1: https://lore.kernel.org/qemu-devel/[email protected]/
v1 -> v2: Move orig_ax declaration higher, simplify gdb_write_reg()

          for TARGET_LONG_BITS == 32 (Richard).

Hi,

Currently gdbstub is barely usable with i386-linux-user: GDB cannot even parse a shared library list, so no symbols are available. This boils down to unavailability of info proc, which is gated behind org.gnu.gdb.i386.linux. See amd64_linux_init_abi(); info proc is enabled by amd64_linux_init_abi_common() -> linux_init_abi().

This series adds orig_ax support to the emulator and gdbstub, and enables the existing test-proc-mappings.py on i386.

Best regards,
Ilya

Ilya Leoshkevich (5):
include/exec: Introduce env_cpu_const() linux-user/i386: Emulate orig_ax
target/i386/gdbstub: Factor out gdb_get_reg() and gdb_write_reg() target/i386/gdbstub: Expose orig_ax
tests/tcg: Run test-proc-mappings.py on i386

 configs/targets/i386-linux-user.mak           |   2 +-
 configs/targets/x86_64-linux-user.mak         |   2 +-
 gdb-xml/i386-32bit-linux.xml                  |  11 ++
 gdb-xml/i386-64bit-linux.xml                  |  11 ++
 include/exec/cpu-common.h                     |  13 ++-
 linux-user/elfload.c                          |   6 +-
 linux-user/i386/cpu_loop.c                    |   3 +
 linux-user/qemu.h                             |   4 +
 target/i386/cpu.c                             |   1 +
 target/i386/cpu.h                             |   1 +
 target/i386/gdbstub.c                         | 102 ++++++++++++++----
 .../multiarch/gdbstub/test-proc-mappings.py   |  17 ++-
 12 files changed, 135 insertions(+), 38 deletions(-)
 create mode 100644 gdb-xml/i386-32bit-linux.xml
 create mode 100644 gdb-xml/i386-64bit-linux.xml
Signature · 1 line
2.46.0
#1 lore raw

PATCH v2 1/5 include/exec: Introduce env_cpu_const()

It's the same as env_cpu(), but for const objects.

---
 include/exec/cpu-common.h | 13 ++++++++++++-
 linux-user/elfload.c      |  2 +-
 2 files changed, 13 insertions(+), 2 deletions(-)
include/exec/cpu-common.h +12 -1
index 2998c95da4a..dc5cffadf29 100644
@@ -242,6 +242,17 @@ static inline ArchCPU *env_archcpu(CPUArchState *env)
     return (void *)env - sizeof(CPUState);
 }
 
+/**
+ * env_cpu_const(env)
+ * @env: The architecture environment
+ *
+ * Return the CPUState associated with the environment.
+ */
+static inline const CPUState *env_cpu_const(const CPUArchState *env)
+{
+    return (void *)env - sizeof(CPUState);
+}
+
 /**
  * env_cpu(env)
  * @env: The architecture environment
@@ -250,7 +261,7 @@ static inline ArchCPU *env_archcpu(CPUArchState *env)
  */
 static inline CPUState *env_cpu(CPUArchState *env)
 {
-    return (void *)env - sizeof(CPUState);
+    return (CPUState *)env_cpu_const(env);
 }
 
 #ifndef CONFIG_USER_ONLY
linux-user/elfload.c +1 -1
index 0861f115fc4..05292c27776 100644
@@ -4329,7 +4329,7 @@ static int wmr_write_region(void *opaque, target_ulong start,
  */
 static int elf_core_dump(int signr, const CPUArchState *env)
 {
-    const CPUState *cpu = env_cpu((CPUArchState *)env);
+    const CPUState *cpu = env_cpu_const(env);
     const TaskState *ts = (const TaskState *)get_task_state((CPUState *)cpu);
     struct rlimit dumpsize;
     CountAndSizeRegions css;
Signature · 1 line
2.46.0
#2 lore raw

PATCH v2 2/5 linux-user/i386: Emulate orig_ax

In reply to Ilya Leoshkevich

The kernel uses orig_rax/orig_eax to store the syscall number before a syscall. One can see this value in core dumps and ptrace.

---
 linux-user/elfload.c       | 4 ++--
 linux-user/i386/cpu_loop.c | 3 +++
 linux-user/qemu.h          | 4 ++++
 3 files changed, 9 insertions(+), 2 deletions(-)
linux-user/elfload.c +2 -2
index 05292c27776..2d73382ae6b 100644
@@ -203,7 +203,7 @@ static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *en
     (*regs)[12] = tswapreg(env->regs[R_EDX]);
     (*regs)[13] = tswapreg(env->regs[R_ESI]);
     (*regs)[14] = tswapreg(env->regs[R_EDI]);
-    (*regs)[15] = tswapreg(env->regs[R_EAX]); /* XXX */
+    (*regs)[15] = tswapreg(get_task_state(env_cpu_const(env))->orig_ax);
     (*regs)[16] = tswapreg(env->eip);
     (*regs)[17] = tswapreg(env->segs[R_CS].selector & 0xffff);
     (*regs)[18] = tswapreg(env->eflags);
@@ -306,7 +306,7 @@ static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *en
     (*regs)[8] = tswapreg(env->segs[R_ES].selector & 0xffff);
     (*regs)[9] = tswapreg(env->segs[R_FS].selector & 0xffff);
     (*regs)[10] = tswapreg(env->segs[R_GS].selector & 0xffff);
-    (*regs)[11] = tswapreg(env->regs[R_EAX]); /* XXX */
+    (*regs)[11] = tswapreg(get_task_state(env_cpu_const(env))->orig_ax);
     (*regs)[12] = tswapreg(env->eip);
     (*regs)[13] = tswapreg(env->segs[R_CS].selector & 0xffff);
     (*regs)[14] = tswapreg(env->eflags);
linux-user/i386/cpu_loop.c +3 -0
index 8707a3eaf25..43ad4e2f39e 100644
@@ -172,6 +172,7 @@ static void emulate_vsyscall(CPUX86State *env)
     /*
      * Perform the syscall.  None of the vsyscalls should need restarting.
      */
+    get_task_state(env_cpu(env))->orig_ax = syscall;
     ret = do_syscall(env, syscall, env->regs[R_EDI], env->regs[R_ESI],
                      env->regs[R_EDX], env->regs[10], env->regs[8],
                      env->regs[9], 0, 0);
@@ -218,6 +219,7 @@ void cpu_loop(CPUX86State *env)
         case EXCP_SYSCALL:
 #endif
             /* linux syscall from int $0x80 */
+            get_task_state(cs)->orig_ax = env->regs[R_EAX];
             ret = do_syscall(env,
                              env->regs[R_EAX],
                              env->regs[R_EBX],
@@ -236,6 +238,7 @@ void cpu_loop(CPUX86State *env)
 #ifdef TARGET_X86_64
         case EXCP_SYSCALL:
             /* linux syscall from syscall instruction.  */
+            get_task_state(cs)->orig_ax = env->regs[R_EAX];
             ret = do_syscall(env,
                              env->regs[R_EAX],
                              env->regs[R_EDI],
linux-user/qemu.h +4 -0
index a2961f503f4..92b2bee87b1 100644
@@ -113,6 +113,10 @@ struct TaskState {
     struct target_vm86plus_struct vm86plus;
     uint32_t v86flags;
     uint32_t v86mask;
+#endif
+#if defined(TARGET_I386)
+    /* Last syscall number. */
+    target_ulong orig_ax;
 #endif
     abi_ulong child_tidptr;
 #ifdef TARGET_M68K
Signature · 1 line
2.46.0
#3 lore raw

PATCH v2 3/5 target/i386/gdbstub: Factor out gdb_get_reg() and gdb_write_reg()

In reply to Ilya Leoshkevich

i386 gdbstub handles both i386 and x86_64. Factor out two functions for reading and writing registers without knowing their bitness.

While at it, simplify the TARGET_LONG_BITS == 32 case.

---
 target/i386/gdbstub.c | 51 +++++++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 21 deletions(-)
target/i386/gdbstub.c +30 -21
index 4acf485879e..cc5eceeb7eb 100644
@@ -96,6 +96,19 @@ static int gdb_write_reg_cs64(uint32_t hflags, uint8_t *buf, target_ulong *val)
     return 4;
 }
 
+static int gdb_get_reg(CPUX86State *env, GByteArray *mem_buf, target_ulong val)
+{
+    if (TARGET_LONG_BITS == 64) {
+        if (env->hflags & HF_CS64_MASK) {
+            return gdb_get_reg64(mem_buf, val);
+        } else {
+            return gdb_get_reg64(mem_buf, val & 0xffffffffUL);
+        }
+    } else {
+        return gdb_get_reg32(mem_buf, val);
+    }
+}
+
 int x86_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
 {
     X86CPU *cpu = X86_CPU(cs);
@@ -137,15 +150,7 @@ int x86_cpu_gdb_read_register(CPUState *cs, GByteArray *mem_buf, int n)
     } else {
         switch (n) {
         case IDX_IP_REG:
-            if (TARGET_LONG_BITS == 64) {
-                if (env->hflags & HF_CS64_MASK) {
-                    return gdb_get_reg64(mem_buf, env->eip);
-                } else {
-                    return gdb_get_reg64(mem_buf, env->eip & 0xffffffffUL);
-                }
-            } else {
-                return gdb_get_reg32(mem_buf, env->eip);
-            }
+            return gdb_get_reg(env, mem_buf, env->eip);
         case IDX_FLAGS_REG:
             return gdb_get_reg32(mem_buf, env->eflags);
 
@@ -248,6 +253,21 @@ static int x86_cpu_gdb_load_seg(X86CPU *cpu, X86Seg sreg, uint8_t *mem_buf)
     return 4;
 }
 
+static int gdb_write_reg(CPUX86State *env, uint8_t *mem_buf, target_ulong *val)
+{
+    if (TARGET_LONG_BITS == 64) {
+        if (env->hflags & HF_CS64_MASK) {
+            *val = ldq_p(mem_buf);
+        } else {
+            *val = ldq_p(mem_buf) & 0xffffffffUL;
+        }
+        return 8;
+    } else {
+        *val = (uint32_t)ldl_p(mem_buf);
+        return 4;
+    }
+}
+
 int x86_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
 {
     X86CPU *cpu = X86_CPU(cs);
@@ -288,18 +308,7 @@ int x86_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n)
     } else {
         switch (n) {
         case IDX_IP_REG:
-            if (TARGET_LONG_BITS == 64) {
-                if (env->hflags & HF_CS64_MASK) {
-                    env->eip = ldq_p(mem_buf);
-                } else {
-                    env->eip = ldq_p(mem_buf) & 0xffffffffUL;
-                }
-                return 8;
-            } else {
-                env->eip &= ~0xffffffffUL;
-                env->eip |= (uint32_t)ldl_p(mem_buf);
-                return 4;
-            }
+            return gdb_write_reg(env, mem_buf, &env->eip);
         case IDX_FLAGS_REG:
             env->eflags = ldl_p(mem_buf);
             return 4;
Signature · 1 line
2.46.0
#4 lore raw

PATCH v2 4/5 target/i386/gdbstub: Expose orig_ax

In reply to Ilya Leoshkevich

Copy XML files describing orig_ax from GDB and glue them with CPUX86State.orig_ax.

---
 configs/targets/i386-linux-user.mak   |  2 +-
 configs/targets/x86_64-linux-user.mak |  2 +-
 gdb-xml/i386-32bit-linux.xml          | 11 ++++++
 gdb-xml/i386-64bit-linux.xml          | 11 ++++++
 target/i386/cpu.c                     |  1 +
 target/i386/cpu.h                     |  1 +
 target/i386/gdbstub.c                 | 51 +++++++++++++++++++++++++++
 7 files changed, 77 insertions(+), 2 deletions(-)
 create mode 100644 gdb-xml/i386-32bit-linux.xml
 create mode 100644 gdb-xml/i386-64bit-linux.xml
configs/targets/i386-linux-user.mak +1 -1
index 5b2546a4309..b72a156473a 100644
@@ -1,4 +1,4 @@
 TARGET_ARCH=i386
 TARGET_SYSTBL_ABI=i386
 TARGET_SYSTBL=syscall_32.tbl
-TARGET_XML_FILES= gdb-xml/i386-32bit.xml
+TARGET_XML_FILES= gdb-xml/i386-32bit.xml gdb-xml/i386-32bit-linux.xml
configs/targets/x86_64-linux-user.mak +1 -1
index 9ceefbb615a..86042814d39 100644
@@ -2,4 +2,4 @@ TARGET_ARCH=x86_64
 TARGET_BASE_ARCH=i386
 TARGET_SYSTBL_ABI=common,64
 TARGET_SYSTBL=syscall_64.tbl
-TARGET_XML_FILES= gdb-xml/i386-64bit.xml
+TARGET_XML_FILES= gdb-xml/i386-64bit.xml gdb-xml/i386-64bit-linux.xml
gdb-xml/i386-32bit-linux.xml new file +11 -0
new file mode 100644
index 00000000000..5ffe5616e63
@@ -0,0 +1,11 @@
+<?xml version="1.0"?>
+<!-- Copyright (C) 2010-2024 Free Software Foundation, Inc.
+
+     Copying and distribution of this file, with or without modification,
+     are permitted in any medium without royalty provided the copyright
+     notice and this notice are preserved.  -->
+
+<!DOCTYPE feature SYSTEM "gdb-target.dtd">
+<feature name="org.gnu.gdb.i386.linux">
+  <reg name="orig_eax" bitsize="32" type="int"/>
+</feature>
gdb-xml/i386-64bit-linux.xml new file +11 -0
new file mode 100644
index 00000000000..0f26990d2f7
@@ -0,0 +1,11 @@
+<?xml version="1.0"?>
+<!-- Copyright (C) 2010-2024 Free Software Foundation, Inc.
+
+     Copying and distribution of this file, with or without modification,
+     are permitted in any medium without royalty provided the copyright
+     notice and this notice are preserved.  -->
+
+<!DOCTYPE feature SYSTEM "gdb-target.dtd">
+<feature name="org.gnu.gdb.i386.linux">
+  <reg name="orig_rax" bitsize="64" type="int"/>
+</feature>
target/i386/cpu.c +1 -0
index 85ef7452c04..52788e6170d 100644
@@ -7831,6 +7831,7 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
 
     mce_init(cpu);
 
+    x86_cpu_gdb_init(cs);
     qemu_init_vcpu(cs);
 
     /*
target/i386/cpu.h +1 -0
index 14edd57a37b..dc2e074e227 100644
@@ -2231,6 +2231,7 @@ void x86_cpu_dump_state(CPUState *cs, FILE *f, int flags);
 
 int x86_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
 int x86_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
+void x86_cpu_gdb_init(CPUState *cs);
 
 void x86_cpu_list(void);
 int cpu_x86_support_mca_broadcast(CPUX86State *env);
target/i386/gdbstub.c +51 -0

62 lines not on this page · raw message

Signature · 1 line
2.46.0
#5 lore raw

PATCH v2 5/5 tests/tcg: Run test-proc-mappings.py on i386

In reply to Ilya Leoshkevich

Now that orig_ax is exposed and GDB is happy, don't skip test-proc-mappings.py on i386. In fact, it's broken only on m68k now, so skip only this architecture.

---
 .../tcg/multiarch/gdbstub/test-proc-mappings.py | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
tests/tcg/multiarch/gdbstub/test-proc-mappings.py +6 -11
index 564613fabf0..0f687f3284a 100644
@@ -8,17 +8,12 @@
 
 def run_test():
     """Run through the tests one by one"""
-    try:
-        mappings = gdb.execute("info proc mappings", False, True)
-    except gdb.error as exc:
-        exc_str = str(exc)
-        if "Not supported on this target." in exc_str:
-            # Detect failures due to an outstanding issue with how GDB handles
-            # the x86_64 QEMU's target.xml, which does not contain the
-            # definition of orig_rax. Skip the test in this case.
-            print("SKIP: {}".format(exc_str))
-            return
-        raise
+    if gdb.selected_inferior().architecture().name() == "m68k":
+        # m68k GDB supports only GDB_OSABI_SVR4, but GDB_OSABI_LINUX is
+        # required for the info proc support (see set_gdbarch_info_proc()).
+        print("SKIP: m68k GDB does not support GDB_OSABI_LINUX")
+        exit(0)
+    mappings = gdb.execute("info proc mappings", False, True)
     report(isinstance(mappings, str), "Fetched the mappings from the inferior")
     # Broken with host page size > guest page size
     # report("/sha1" in mappings, "Found the test binary name in the mappings")
Signature · 1 line
2.46.0
#6 lore raw

PING: [PATCH v2 0/5] linux-user/i386: Emulate orig_ax

In reply to Ilya Leoshkevich
Quoted 40 lines · Ilya Leoshkevich

v1:
https://lore.kernel.org/qemu-devel/[email protected]/
v1 -> v2: Move orig_ax declaration higher, simplify gdb_write_reg() for TARGET_LONG_BITS == 32 (Richard).

Hi,

Currently gdbstub is barely usable with i386-linux-user: GDB cannot even parse a shared library list, so no symbols are available. This boils down to unavailability of info proc, which is gated behind org.gnu.gdb.i386.linux. See amd64_linux_init_abi(); info proc is enabled by amd64_linux_init_abi_common() -> linux_init_abi().

This series adds orig_ax support to the emulator and gdbstub, and enables the existing test-proc-mappings.py on i386.

Best regards,
Ilya

Ilya Leoshkevich (5):
  include/exec: Introduce env_cpu_const() linux-user/i386: Emulate orig_ax
  target/i386/gdbstub: Factor out gdb_get_reg() and gdb_write_reg() target/i386/gdbstub: Expose orig_ax
  tests/tcg: Run test-proc-mappings.py on i386

 configs/targets/i386-linux-user.mak           |   2 +- configs/targets/x86_64-linux-user.mak         |   2 +- gdb-xml/i386-32bit-linux.xml                  |  11 ++ gdb-xml/i386-64bit-linux.xml                  |  11 ++ include/exec/cpu-common.h                     |  13 ++- linux-user/elfload.c                          |   6 +- linux-user/i386/cpu_loop.c                    |   3 +

 linux-user/qemu.h                             |   4 +
 target/i386/cpu.c                             |   1 +
 target/i386/cpu.h                             |   1 +
 target/i386/gdbstub.c                         | 102 ++++++++++++++--
--
 .../multiarch/gdbstub/test-proc-mappings.py   |  17 ++-
 12 files changed, 135 insertions(+), 38 deletions(-)
Quoted 2 lines

 create mode 100644 gdb-xml/i386-32bit-linux.xml
 create mode 100644 gdb-xml/i386-64bit-linux.xml

Hi,

I would like to ping this series.
Patches 1-4 have R-b, patch 5 has only A-b.

Best regards,
Ilya