Skip to contentSkip to conversation

linux-user/i386: Emulate orig_ax

PATCH New Ilya Leoshkevich 3 participants 11 messages Aug 2, 2024 – Sep 8, 2024

lore
Share & tools
lore
#0 lore raw

cover linux-user/i386: Emulate orig_ax

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                             |   5 +
 target/i386/cpu.c                             |   1 +
 target/i386/cpu.h                             |   1 +
 target/i386/gdbstub.c                         | 103 ++++++++++++++----
 .../multiarch/gdbstub/test-proc-mappings.py   |  17 +--
 12 files changed, 137 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.45.2
#1 lore raw

PATCH 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 2e1b499cb71..638dc806a5f 100644
@@ -238,6 +238,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
@@ -246,7 +257,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.45.2
#3 lore raw

PATCH 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          | 5 +++++
 3 files changed, 10 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 92beb6830cc..7a35215278a 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);
@@ -221,6 +222,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],
@@ -239,6 +241,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 +5 -0
index a2961f503f4..85ba5a53869 100644
@@ -159,6 +159,11 @@ struct TaskState {
 
     /* Start time of task after system boot in clock ticks */
     uint64_t start_boottime;
+
+#if defined(TARGET_I386)
+    /* Last syscall number. */
+    target_ulong orig_ax;
+#endif
 };
 
 abi_long do_brk(abi_ulong new_brk);
Signature · 1 line
2.45.2
Reviewed-by on v1 2/5
#4 lore raw

linux-user/qemu.h struct TaskState { · line 166

Quoted 13 lines · Ilya Leoshkevich
linux-user/qemu.h +5 -0
index a2961f503f4..85ba5a53869 100644
@@ -159,6 +159,11 @@ struct TaskState {
 
      /* Start time of task after system boot in clock ticks */
      uint64_t start_boottime;
+
+#if defined(TARGET_I386)
+    /* Last syscall number. */
+    target_ulong orig_ax;
+#endif

Maybe place this higher, in the existing TARGET_I386 block?

Otherwise,

r~

#5 lore raw

PATCH 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.

---
 target/i386/gdbstub.c | 52 ++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 21 deletions(-)
target/i386/gdbstub.c +31 -21
index 4acf485879e..ec64ab6c53f 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,22 @@ 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 &= ~0xffffffffUL;
+        *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 +309,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.45.2
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] target/i386/gdbstub: Factor out gdb_get_reg() and gdb_write_reg()

Quoted 30 lines · Ilya Leoshkevich
target/i386/gdbstub.c +16 -3
@@ -248,6 +253,22 @@ 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 &= ~0xffffffffUL;
+        *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 +309,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);

target/i386/gdbstub.c int x86_cpu_gdb_write_register(CPUState *cs, uint8_t *mem_buf, int n) · line 312

Quoted 10 lines
target/i386/gdbstub.c +1 -9
-                } 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);

Existing bug, but the insert in the !(TARGET_LONG_BITS == 64) case is silly.
Because TARGET_LONG_BITS == 32, target_ulong eip is 32-bits, so the "insert" doesn't really insert anything.

Otherwise,

r~

#7 lore raw

PATCH 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 c6cc035df3d..f2819e91007 100644
@@ -2226,6 +2226,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.45.2
Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] target/i386/gdbstub: Expose orig_ax

Quoted 13 lines · 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(-)
Quoted 2 lines

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

r~

#9 lore raw

PATCH 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.45.2
Acked-by on v1 5/5
#10 lore raw

Re: [PATCH 5/5] tests/tcg: Run test-proc-mappings.py on i386

Quoted 5 lines · 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.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro