Skip to contentSkip to conversation

arch_dump: fix prstatus pid on s390x and ppc

PATCH In review Omar Sandoval 3 participants 7 messages Jun 19, 2024 – Jun 24, 2024 Kcore

lore
Share & tools
lore

Omar Sandoval

author maintainer
#0 lore raw

cover arch_dump: fix prstatus pid on s390x and ppc

Hello,

I maintain drgn [1], a debugger for the Linux kernel. I ran into a quirk of the NT_PRSTATUS note in kernel core dumps [2], so I looked into how QEMU's dump-guest-memory command generates NT_PRSTATUS. I noticed that on most architectures, the note's PID field is set to the CPU ID plus 1. There are two exceptions: on s390x, there's an endianness bug (it's not byte swapped if the host is little endian), and on ppc, it's not set at all (it defaults to zero). They're both easy fixes.

Thanks,
Omar

1: https://github.com/osandov/drgn
2: https://github.com/osandov/drgn/issues/404

Omar Sandoval (2):
target/s390x/arch_dump: use correct byte order for pid target/ppc/arch_dump: set prstatus pid to cpuid

 target/ppc/arch_dump.c   | 21 ++++++++++++---------
 target/s390x/arch_dump.c |  2 +-
 2 files changed, 13 insertions(+), 10 deletions(-)
Signature · 1 line
2.45.2
author maintainer
#1 lore raw

PATCH 1/2 target/s390x/arch_dump: use correct byte order for pid

The pid field of prstatus needs to be big endian like all of the other fields.

Fixes f738f296eaae ("s390x/arch_dump: pass cpuid into notes sections")
---
 target/s390x/arch_dump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/s390x/arch_dump.c +1 -1
index 7e8a1b4fc0..029d91d93a 100644
@@ -102,7 +102,7 @@ static void s390x_write_elf64_prstatus(Note *note, S390CPU *cpu, int id)
         regs->acrs[i] = cpu_to_be32(cpu->env.aregs[i]);
         regs->gprs[i] = cpu_to_be64(cpu->env.regs[i]);
     }
-    note->contents.prstatus.pid = id;
+    note->contents.prstatus.pid = cpu_to_be32(id);
 }
 
 static void s390x_write_elf64_fpregset(Note *note, S390CPU *cpu, int id)
Signature · 1 line
2.45.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] target/s390x/arch_dump: use correct byte order for pid

Quoted 14 lines · Omar Sandoval

The pid field of prstatus needs to be big endian like all of the other fields.

Fixes f738f296eaae ("s390x/arch_dump: pass cpuid into notes sections")
---
  target/s390x/arch_dump.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/s390x/arch_dump.c +0 -0
index 7e8a1b4fc0..029d91d93a 100644

target/s390x/arch_dump.c static void s390x_write_elf64_prstatus(Note *note, S390CPU *cpu, int id) · line 108

Quoted 8 lines
target/s390x/arch_dump.c +1 -1
@@ -102,7 +102,7 @@ static void s390x_write_elf64_prstatus(Note *note, S390CPU *cpu, int id)
          regs->acrs[i] = cpu_to_be32(cpu->env.aregs[i]);
          regs->gprs[i] = cpu_to_be64(cpu->env.regs[i]);
      }
-    note->contents.prstatus.pid = id;
+    note->contents.prstatus.pid = cpu_to_be32(id);
  }
 
  static void s390x_write_elf64_fpregset(Note *note, S390CPU *cpu, int id)
author maintainer
#3 lore raw

PATCH 2/2 target/ppc/arch_dump: set prstatus pid to cpuid

In reply to Omar Sandoval

Every other architecture does this, and debuggers need it to be able to identify which prstatus note corresponds to which CPU.

---
 target/ppc/arch_dump.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
target/ppc/arch_dump.c +12 -9
index a8315659d9..78b4205319 100644
@@ -47,9 +47,11 @@ struct PPCUserRegStruct {
 } QEMU_PACKED;
 
 struct PPCElfPrstatus {
-    char pad1[112];
+    char pad1[32];
+    uint32_t pid;
+    uint8_t pad2[76];
     struct PPCUserRegStruct pr_reg;
-    char pad2[40];
+    char pad3[40];
 } QEMU_PACKED;
 
 
@@ -96,7 +98,7 @@ typedef struct NoteFuncArg {
     DumpState *state;
 } NoteFuncArg;
 
-static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
 {
     int i;
     reg_t cr;
@@ -109,6 +111,7 @@ static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
 
     prstatus = &note->contents.prstatus;
     memset(prstatus, 0, sizeof(*prstatus));
+    prstatus->pid = cpu_to_dump32(s, id);
     reg = &prstatus->pr_reg;
 
     for (i = 0; i < 32; i++) {
@@ -127,7 +130,7 @@ static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
     reg->ccr = cpu_to_dump_reg(s, cr);
 }
 
-static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
 {
     int i;
     struct PPCElfFpregset  *fpregset;
@@ -146,7 +149,7 @@ static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
     fpregset->fpscr = cpu_to_dump_reg(s, cpu->env.fpscr);
 }
 
-static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
 {
     int i;
     struct PPCElfVmxregset *vmxregset;
@@ -178,7 +181,7 @@ static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
     vmxregset->vscr.u32[3] = cpu_to_dump32(s, ppc_get_vscr(&cpu->env));
 }
 
-static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
 {
     int i;
     struct PPCElfVsxregset *vsxregset;
@@ -195,7 +198,7 @@ static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
     }
 }
 
-static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
 {
     struct PPCElfSperegset *speregset;
     Note *note = &arg->note;
@@ -211,7 +214,7 @@ static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
 
 static const struct NoteFuncDescStruct {
     int contents_size;
-    void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu);
+    void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu, int id);
 } note_func[] = {
     {sizeof_field(Note, contents.prstatus),  ppc_write_elf_prstatus},
     {sizeof_field(Note, contents.fpregset),  ppc_write_elf_fpregset},
@@ -282,7 +285,7 @@ static int ppc_write_all_elf_notes(const char *note_name,
         arg.note.hdr.n_descsz = cpu_to_dump32(s, nf->contents_size);
         strncpy(arg.note.name, note_name, sizeof(arg.note.name));
 
-        (*nf->note_contents_func)(&arg, cpu);
+        (*nf->note_contents_func)(&arg, cpu, id);
 
         note_size =
             sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size;
Signature · 1 line
2.45.2
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] target/ppc/arch_dump: set prstatus pid to cpuid

Quoted 89 lines · Omar Sandoval

Every other architecture does this, and debuggers need it to be able to identify which prstatus note corresponds to which CPU.

---
  target/ppc/arch_dump.c | 21 ++++++++++++---------
  1 file changed, 12 insertions(+), 9 deletions(-)
target/ppc/arch_dump.c +11 -8
index a8315659d9..78b4205319 100644
@@ -47,9 +47,11 @@ struct PPCUserRegStruct {
  } QEMU_PACKED;
 
  struct PPCElfPrstatus {
-    char pad1[112];
+    char pad1[32];
+    uint32_t pid;
+    uint8_t pad2[76];
      struct PPCUserRegStruct pr_reg;
-    char pad2[40];
+    char pad3[40];
  } QEMU_PACKED;
 
 
@@ -96,7 +98,7 @@ typedef struct NoteFuncArg {
      DumpState *state;
  } NoteFuncArg;
 
-static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
  {
      int i;
      reg_t cr;
@@ -109,6 +111,7 @@ static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
 
      prstatus = &note->contents.prstatus;
      memset(prstatus, 0, sizeof(*prstatus));
+    prstatus->pid = cpu_to_dump32(s, id);
      reg = &prstatus->pr_reg;
 
      for (i = 0; i < 32; i++) {
@@ -127,7 +130,7 @@ static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
      reg->ccr = cpu_to_dump_reg(s, cr);
  }
 
-static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
  {
      int i;
      struct PPCElfFpregset  *fpregset;
@@ -146,7 +149,7 @@ static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
      fpregset->fpscr = cpu_to_dump_reg(s, cpu->env.fpscr);
  }
 
-static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
  {
      int i;
      struct PPCElfVmxregset *vmxregset;
@@ -178,7 +181,7 @@ static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
      vmxregset->vscr.u32[3] = cpu_to_dump32(s, ppc_get_vscr(&cpu->env));
  }
 
-static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
  {
      int i;
      struct PPCElfVsxregset *vsxregset;
@@ -195,7 +198,7 @@ static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
      }
  }
 
-static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
  {
      struct PPCElfSperegset *speregset;
      Note *note = &arg->note;
@@ -211,7 +214,7 @@ static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
 
  static const struct NoteFuncDescStruct {
      int contents_size;
-    void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu);
+    void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu, int id);
  } note_func[] = {
      {sizeof_field(Note, contents.prstatus),  ppc_write_elf_prstatus},
      {sizeof_field(Note, contents.fpregset),  ppc_write_elf_fpregset},

target/ppc/arch_dump.c static int ppc_write_all_elf_notes(const char *note_name, · line 291

Quoted 8 lines
target/ppc/arch_dump.c +1 -1
@@ -282,7 +285,7 @@ static int ppc_write_all_elf_notes(const char *note_name,
          arg.note.hdr.n_descsz = cpu_to_dump32(s, nf->contents_size);
          strncpy(arg.note.name, note_name, sizeof(arg.note.name));
 
-        (*nf->note_contents_func)(&arg, cpu);
+        (*nf->note_contents_func)(&arg, cpu, id);
 
          note_size =
              sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size;
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] target/ppc/arch_dump: set prstatus pid to cpuid

In reply to Omar Sandoval

Hi Omar,

Quoted 15 lines · Omar Sandoval

Every other architecture does this, and debuggers need it to be able to identify which prstatus note corresponds to which CPU.

---
  target/ppc/arch_dump.c | 21 ++++++++++++---------
  1 file changed, 12 insertions(+), 9 deletions(-)
target/ppc/arch_dump.c +0 -0
index a8315659d9..78b4205319 100644
@@ -47,9 +47,11 @@ struct PPCUserRegStruct {
  } QEMU_PACKED;
 

target/ppc/arch_dump.c struct PPCUserRegStruct { · line 56

Quoted 10 lines
target/ppc/arch_dump.c +4 -2
  struct PPCElfPrstatus {
-    char pad1[112];
+    char pad1[32];
+    uint32_t pid;
+    uint8_t pad2[76];
      struct PPCUserRegStruct pr_reg;
-    char pad2[40];
+    char pad3[40];
  } QEMU_PACKED;
 

Could you please add a comment above the struct providing reference to the spec being referred here for member position across the status bits?

With that,

Quoted 72 lines
target/ppc/arch_dump.c +8 -7
@@ -96,7 +98,7 @@ typedef struct NoteFuncArg {
      DumpState *state;
  } NoteFuncArg;
 
-static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
  {
      int i;
      reg_t cr;
@@ -109,6 +111,7 @@ static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
 
      prstatus = &note->contents.prstatus;
      memset(prstatus, 0, sizeof(*prstatus));
+    prstatus->pid = cpu_to_dump32(s, id);
      reg = &prstatus->pr_reg;
 
      for (i = 0; i < 32; i++) {
@@ -127,7 +130,7 @@ static void ppc_write_elf_prstatus(NoteFuncArg *arg, PowerPCCPU *cpu)
      reg->ccr = cpu_to_dump_reg(s, cr);
  }
 
-static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
  {
      int i;
      struct PPCElfFpregset  *fpregset;
@@ -146,7 +149,7 @@ static void ppc_write_elf_fpregset(NoteFuncArg *arg, PowerPCCPU *cpu)
      fpregset->fpscr = cpu_to_dump_reg(s, cpu->env.fpscr);
  }
 
-static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
  {
      int i;
      struct PPCElfVmxregset *vmxregset;
@@ -178,7 +181,7 @@ static void ppc_write_elf_vmxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
      vmxregset->vscr.u32[3] = cpu_to_dump32(s, ppc_get_vscr(&cpu->env));
  }
 
-static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
  {
      int i;
      struct PPCElfVsxregset *vsxregset;
@@ -195,7 +198,7 @@ static void ppc_write_elf_vsxregset(NoteFuncArg *arg, PowerPCCPU *cpu)
      }
  }
 
-static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
+static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu, int id)
  {
      struct PPCElfSperegset *speregset;
      Note *note = &arg->note;
@@ -211,7 +214,7 @@ static void ppc_write_elf_speregset(NoteFuncArg *arg, PowerPCCPU *cpu)
 
  static const struct NoteFuncDescStruct {
      int contents_size;
-    void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu);
+    void (*note_contents_func)(NoteFuncArg *arg, PowerPCCPU *cpu, int id);
  } note_func[] = {
      {sizeof_field(Note, contents.prstatus),  ppc_write_elf_prstatus},
      {sizeof_field(Note, contents.fpregset),  ppc_write_elf_fpregset},
@@ -282,7 +285,7 @@ static int ppc_write_all_elf_notes(const char *note_name,
          arg.note.hdr.n_descsz = cpu_to_dump32(s, nf->contents_size);
          strncpy(arg.note.name, note_name, sizeof(arg.note.name));
 
-        (*nf->note_contents_func)(&arg, cpu);
+        (*nf->note_contents_func)(&arg, cpu, id);
 
          note_size =
              sizeof(arg.note) - sizeof(arg.note.contents) + nf->contents_size;
#6 lore raw
In reply to Omar Sandoval
Quoted 16 lines · Omar Sandoval

Hello,

I maintain drgn [1], a debugger for the Linux kernel. I ran into a quirk of the NT_PRSTATUS note in kernel core dumps [2], so I looked into how QEMU's dump-guest-memory command generates NT_PRSTATUS. I noticed that on most architectures, the note's PID field is set to the CPU ID plus 1. There are two exceptions: on s390x, there's an endianness bug (it's not byte swapped if the host is little endian), and on ppc, it's not set at all (it defaults to zero). They're both easy fixes.

Thanks,
Omar

1: https://github.com/osandov/drgn
2: https://github.com/osandov/drgn/issues/404

Quoted 7 lines

Omar Sandoval (2):
target/s390x/arch_dump: use correct byte order for pid target/ppc/arch_dump: set prstatus pid to cpuid

  target/ppc/arch_dump.c   | 21 ++++++++++++---------
  target/s390x/arch_dump.c |  2 +-
  2 files changed, 13 insertions(+), 10 deletions(-)

I'm going to pick up patch 1 for my s390x tree ... for the second patch, it would be good if someone of the ppc guys could have a look at it first.

Thomas