Skip to contentSkip to conversation

target-ppc: Add support for dumping guest memory using qemu gdb server

PATCH RESEND In review Aneesh Kumar K.V 2 participants 8 messages Oct 1, 2013 – Oct 2, 2013

lore
Share & tools
lore
#0 lore raw

cover target-ppc: Add support for dumping guest memory using qemu gdb server

Hi,

This patch series implement support for dumping guest memory using qemu gdb server. The last patch also enable qemu monitor command dump-guest-memory

With this patch series we can now do

(gdb) x/4i htab_call_hpte_insert1

   0xc0000000000470d8 <.htab_call_hpte_insert1>:        bl      0xc0000000000470d8 <.htab_call_hpte_insert1>
   0xc0000000000470dc <.htab_call_hpte_insert1+4>:      cmpdi   r3,0
   0xc0000000000470e0 <.htab_call_hpte_insert1+8>:      bge     0xc000000000047190 <htab_pte_insert_ok>
   0xc0000000000470e4 <.htab_call_hpte_insert1+12>:     cmpdi   r3,-2

(gdb) target remote localhost:1234
Remote debugging using localhost:1234
.plpar_hcall_norets () at arch/powerpc/platforms/pseries/hvCall.S:119
119 HCALL_INST_POSTCALL_NORETS
(gdb) x/4i htab_call_hpte_insert1

   0xc0000000000470d8 <.htab_call_hpte_insert1>:        bl      0xc00000000005f8f0 <pSeries_lpar_hpte_insert>
   0xc0000000000470dc <.htab_call_hpte_insert1+4>:      cmpdi   r3,0
   0xc0000000000470e0 <.htab_call_hpte_insert1+8>:      bge     0xc000000000047190 <htab_pte_insert_ok>
   0xc0000000000470e4 <.htab_call_hpte_insert1+12>:     cmpdi   r3,-2

(gdb)

NOTE: We still don't support inserting breakpoints.

Before Fix:
(qemu) memsave 0xc0000000000470d8 10 memdump
Invalid parameter 'addr'
(qemu)

After fix:

(qemu) memsave 0xc0000000000470d8 10 memdump
(qemu)

Changes from V3:
* Address review feedback
* Updated "target-ppc: Fix page table lookup with kvm enabled" to not reopen the htab in loop

Changes from V2:
* Address review comments updating "target-ppc: Update slb array with correct index values"
* Add new patch "target-ppc: Use #define for max slb entries"
* Drop the patch "target-ppc: Use #define instead of opencoding SLB valid bit" because it got picked from last series.

#1 lore raw

PATCH RESEND 1/6 target-ppc: Update slb array with correct index values.

From: "Aneesh Kumar K.V" <[email protected]>

Without this, a value of rb=0 and rs=0 results in replacing the 0th index. This can be observed when using gdb remote debugging support.

(gdb) x/10i do_fork
0xc000000000085330 <do_fork>: Cannot access memory at address 0xc000000000085330
(gdb)

This is because when we do the slb sync via kvm_cpu_synchronize_state, we overwrite the slb entry (0th entry) for 0xc000000000085330

---
 target-ppc/kvm.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
target-ppc/kvm.c +15 -2
index 8a196c6..e318250 100644
@@ -1033,9 +1033,22 @@ int kvm_arch_get_registers(CPUState *cs)
 
         /* Sync SLB */
 #ifdef TARGET_PPC64
+        /*
+         * The packed SLB array we get from KVM_GET_SREGS only contains
+         * information about valid entries. So we flush our internal
+         * copy to get rid of stale ones, then put all valid SLB entries
+         * back in.
+         */
+        memset(env->slb, 0, sizeof(env->slb));
         for (i = 0; i < 64; i++) {
-            ppc_store_slb(env, sregs.u.s.ppc64.slb[i].slbe,
-                               sregs.u.s.ppc64.slb[i].slbv);
+            target_ulong rb = sregs.u.s.ppc64.slb[i].slbe;
+            target_ulong rs = sregs.u.s.ppc64.slb[i].slbv;
+            /*
+             * Only restore valid entries
+             */
+            if (rb & SLB_ESID_V) {
+                ppc_store_slb(env, rb, rs);
+            }
         }
 #endif
 
Signature · 1 line
1.8.1.2
#2 lore raw

PATCH RESEND 2/6 target-ppc: Fix page table lookup with kvm enabled

In reply to Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

With kvm enabled, we store the hash page table information in the hypervisor. Use ioctl to read the htab contents. Without this we get the below error when trying to read the guest address

(gdb) x/10 do_fork
0xc000000000098660 <do_fork>: Cannot access memory at address 0xc000000000098660
(gdb)

---
 target-ppc/kvm.c        | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
 target-ppc/kvm_ppc.h    | 12 +++++++++-
 target-ppc/mmu-hash64.c | 57 ++++++++++++++++++++++++++++-------------------
 3 files changed, 104 insertions(+), 24 deletions(-)
target-ppc/kvm.c +59 -0
index e318250..8a53c75 100644
@@ -1888,3 +1888,62 @@ int kvm_arch_on_sigbus(int code, void *addr)
 void kvm_arch_init_irq_routing(KVMState *s)
 {
 }
+
+hwaddr kvmppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
+                                 bool secondary, target_ulong ptem,
+                                 target_ulong *hpte0, target_ulong *hpte1)
+{
+    int htab_fd;
+    uint64_t index;
+    hwaddr pte_offset;
+    target_ulong pte0, pte1;
+    struct kvm_get_htab_fd ghf;
+    struct kvm_get_htab_buf {
+        struct kvm_get_htab_header header;
+        /*
+         * Older kernel required one extra byte.
+         */
+        unsigned long hpte[(HPTES_PER_GROUP * 2) + 1];
+    } hpte_buf;
+
+    index = (hash * HPTES_PER_GROUP) & cpu->env.htab_mask;
+    *hpte0 = 0;
+    *hpte1 = 0;
+    if (!cap_htab_fd) {
+        return 0;
+    }
+
+    ghf.flags = 0;
+    ghf.start_index = index;
+    htab_fd = kvm_vm_ioctl(kvm_state, KVM_PPC_GET_HTAB_FD, &ghf);
+    if (htab_fd < 0) {
+        goto error_out;
+    }
+    /*
+     * Read the hpte group
+     */
+    if (read(htab_fd, &hpte_buf, sizeof(hpte_buf)) < 0) {
+        goto out;
+    }
+
+    index = 0;
+    pte_offset = (hash * HASH_PTEG_SIZE_64) & cpu->env.htab_mask;;
+    while (index < hpte_buf.header.n_valid) {
+        pte0 = hpte_buf.hpte[(index * 2)];
+        pte1 = hpte_buf.hpte[(index * 2) + 1];
+        if ((pte0 & HPTE64_V_VALID)
+            && (secondary == !!(pte0 & HPTE64_V_SECONDARY))
+            && HPTE64_V_COMPARE(pte0, ptem)) {
+            *hpte0 = pte0;
+            *hpte1 = pte1;
+            close(htab_fd);
+            return pte_offset;
+        }
+        index++;
+        pte_offset += HASH_PTE_SIZE_64;
+    }
+out:
+    close(htab_fd);
+error_out:
+    return -1;
+}
target-ppc/kvm_ppc.h +11 -1
index 4ae7bf2..dad0e57 100644
@@ -42,7 +42,9 @@ int kvmppc_get_htab_fd(bool write);
 int kvmppc_save_htab(QEMUFile *f, int fd, size_t bufsize, int64_t max_ns);
 int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
                            uint16_t n_valid, uint16_t n_invalid);
-
+hwaddr kvmppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
+                                 bool secondary, target_ulong ptem,
+                                 target_ulong *hpte0, target_ulong *hpte1);
 #else
 
 static inline uint32_t kvmppc_get_tbfreq(void)
@@ -181,6 +183,14 @@ static inline int kvmppc_load_htab_chunk(QEMUFile *f, int fd, uint32_t index,
     abort();
 }
 
+static inline hwaddr kvmppc_hash64_pteg_search(PowerPCCPU *cpu, hwaddr hash,
+                                               bool secondary,
+                                               target_ulong ptem,
+                                               target_ulong *hpte0,
+                                               target_ulong *hpte1)
+{
+    abort();
+}
 #endif
 
 #ifndef CONFIG_KVM
target-ppc/mmu-hash64.c +34 -23

87 lines not on this page · raw message

Signature · 1 line
1.8.1.2
#3 lore raw

PATCH RESEND 3/6 target-ppc: Check for error on address translation in memsave command

In reply to Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

When we translate the virtual address to physical check for error.

---
 cpus.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
cpus.c +4 -1
index e566297..52470a6 100644
@@ -1333,7 +1333,10 @@ void qmp_memsave(int64_t addr, int64_t size, const char *filename,
         l = sizeof(buf);
         if (l > size)
             l = size;
-        cpu_memory_rw_debug(cpu, addr, buf, l, 0);
+        if (cpu_memory_rw_debug(cpu, addr, buf, l, 0) != 0) {
+            error_setg(errp, "Invalid addr 0x%016" PRIx64 "specified", addr);
+            goto exit;
+        }
         if (fwrite(buf, 1, l, f) != l) {
             error_set(errp, QERR_IO_ERROR);
             goto exit;
Signature · 1 line
1.8.1.2
#4 lore raw

PATCH RESEND 4/6 target-ppc: Use #define for max slb entries

In reply to Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

Instead of opencoding 64 use MAX_SLB_ENTRIES. We don't update the kernel header here.

---
 target-ppc/cpu.h     | 3 ++-
 target-ppc/kvm.c     | 4 ++--
 target-ppc/machine.c | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)
target-ppc/cpu.h +2 -1
index 422a6bb..26acdba 100644
@@ -405,6 +405,7 @@ struct ppc_slb_t {
     uint64_t vsid;
 };
 
+#define MAX_SLB_ENTRIES         64
 #define SEGMENT_SHIFT_256M      28
 #define SEGMENT_MASK_256M       (~((1ULL << SEGMENT_SHIFT_256M) - 1))
 
@@ -949,7 +950,7 @@ struct CPUPPCState {
 #if !defined(CONFIG_USER_ONLY)
 #if defined(TARGET_PPC64)
     /* PowerPC 64 SLB area */
-    ppc_slb_t slb[64];
+    ppc_slb_t slb[MAX_SLB_ENTRIES];
     int32_t slb_nr;
 #endif
     /* segment registers */
target-ppc/kvm.c +2 -2
index 8a53c75..30f14c6 100644
@@ -818,7 +818,7 @@ int kvm_arch_put_registers(CPUState *cs, int level)
 
         /* Sync SLB */
 #ifdef TARGET_PPC64
-        for (i = 0; i < 64; i++) {
+        for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
             sregs.u.s.ppc64.slb[i].slbe = env->slb[i].esid;
             sregs.u.s.ppc64.slb[i].slbv = env->slb[i].vsid;
         }
@@ -1040,7 +1040,7 @@ int kvm_arch_get_registers(CPUState *cs)
          * back in.
          */
         memset(env->slb, 0, sizeof(env->slb));
-        for (i = 0; i < 64; i++) {
+        for (i = 0; i < ARRAY_SIZE(env->slb); i++) {
             target_ulong rb = sregs.u.s.ppc64.slb[i].slbe;
             target_ulong rs = sregs.u.s.ppc64.slb[i].slbv;
             /*
target-ppc/machine.c +1 -1
index 12e1512..12c174f 100644
@@ -312,7 +312,7 @@ static const VMStateDescription vmstate_slb = {
     .minimum_version_id_old = 1,
     .fields      = (VMStateField []) {
         VMSTATE_INT32_EQUAL(env.slb_nr, PowerPCCPU),
-        VMSTATE_SLB_ARRAY(env.slb, PowerPCCPU, 64),
+        VMSTATE_SLB_ARRAY(env.slb, PowerPCCPU, MAX_SLB_ENTRIES),
         VMSTATE_END_OF_LIST()
     }
 };
Signature · 1 line
1.8.1.2
#5 lore raw

PATCH RESEND 5/6 dump-guest-memory: Check for the correct return value

In reply to Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

We should check for error with s->note_size

---
 dump.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
dump.c +2 -2
index 846155c..80a9116 100644
@@ -66,7 +66,7 @@ typedef struct DumpState {
     uint32_t sh_info;
     bool have_section;
     bool resume;
-    size_t note_size;
+    ssize_t note_size;
     hwaddr memory_offset;
     int fd;
 
@@ -765,7 +765,7 @@ static int dump_init(DumpState *s, int fd, bool paging, bool has_filter,
 
     s->note_size = cpu_get_note_size(s->dump_info.d_class,
                                      s->dump_info.d_machine, nr_cpus);
-    if (ret < 0) {
+    if (s->note_size < 0) {
         error_set(errp, QERR_UNSUPPORTED);
         goto cleanup;
     }
Signature · 1 line
1.8.1.2
#6 lore raw

PATCH RESEND 6/6 target-ppc: dump-guest-memory support

In reply to Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

This patch add support for dumping guest memory using dump-guest-memory monitor command.

Before patch:

(qemu) dump-guest-memory testcrash
this feature or command is not currently supported (qemu)

After patch:

(qemu) dump-guest-memory testcrash
(qemu)

crash was able to read the file

crash> bt
PID: 0 TASK: c000000000c0d0d0 CPU: 0 COMMAND: "swapper/0"

 R0:  0000000028000084    R1:  c000000000cafa50    R2:  c000000000cb05b0
 R3:  0000000000000000    R4:  c000000000bc4cb0    R5:  0000000000000000
 R6:  001efe93b8000000    R7:  0000000000000000    R8:  0000000000000000
 R9:  b000000000001032    R10: 0000000000000001    R11: 0001eb2117e00d55

....
...

NOTE: Currently crash tools doesn't look at ELF notes in the dump on ppc64.

---
 include/elf.h               |   3 +
 target-ppc/Makefile.objs    |   2 +-
 target-ppc/arch_dump.c      | 253 ++++++++++++++++++++++++++++++++++++++++++++
 target-ppc/cpu-qom.h        |   5 +-
 target-ppc/translate_init.c |   4 +
 5 files changed, 265 insertions(+), 2 deletions(-)
 create mode 100644 target-ppc/arch_dump.c
include/elf.h +3 -0
index 58bfbf8..b818091 100644
@@ -1359,6 +1359,9 @@ typedef struct elf64_shdr {
 #define NT_S390_TODPREG 0x303           /* s390 TOD programmable register */
 #define NT_S390_TODCMP  0x302           /* s390 TOD clock comparator register */
 #define NT_S390_TIMER   0x301           /* s390 timer register */
+#define NT_PPC_VMX       0x100          /* PowerPC Altivec/VMX registers */
+#define NT_PPC_SPE       0x101          /* PowerPC SPE/EVR registers */
+#define NT_PPC_VSX       0x102          /* PowerPC VSX registers */
 
 
 /* Note header in a PT_NOTE section */
target-ppc/Makefile.objs +1 -1
index f72e399..ec06e50 100644
@@ -2,7 +2,7 @@ obj-y += cpu-models.o
 obj-y += translate.o
 ifeq ($(CONFIG_SOFTMMU),y)
 obj-y += machine.o mmu_helper.o mmu-hash32.o
-obj-$(TARGET_PPC64) += mmu-hash64.o
+obj-$(TARGET_PPC64) += mmu-hash64.o arch_dump.o
 endif
 obj-$(CONFIG_KVM) += kvm.o kvm_ppc.o
 obj-$(CONFIG_NO_KVM) += kvm-stub.o
target-ppc/arch_dump.c new file +253 -0

253 lines not on this page · raw message

target-ppc/cpu-qom.h +4 -1
index f3c710a..827e5dd 100644
@@ -108,7 +108,10 @@ void ppc_cpu_dump_statistics(CPUState *cpu, FILE *f,
 hwaddr ppc_cpu_get_phys_page_debug(CPUState *cpu, vaddr addr);
 int ppc_cpu_gdb_read_register(CPUState *cpu, uint8_t *buf, int reg);
 int ppc_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
-
+int ppc64_cpu_write_elf64_qemunote(WriteCoreDumpFunction f,
+                                   CPUState *cpu, void *opaque);
+int ppc64_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,
+                               int cpuid, void *opaque);
 #ifndef CONFIG_USER_ONLY
 extern const struct VMStateDescription vmstate_ppc_cpu;
 #endif
target-ppc/translate_init.c +4 -0
index d2645ba..4c0f4c6 100644
@@ -8570,6 +8570,10 @@ static void ppc_cpu_class_init(ObjectClass *oc, void *data)
 #ifndef CONFIG_USER_ONLY
     cc->get_phys_page_debug = ppc_cpu_get_phys_page_debug;
     cc->vmsd = &vmstate_ppc_cpu;
+#if defined(TARGET_PPC64)
+    cc->write_elf64_note = ppc64_cpu_write_elf64_note;
+    cc->write_elf64_qemunote = ppc64_cpu_write_elf64_qemunote;
+#endif
 #endif
 
     cc->gdb_num_core_regs = 71;
Signature · 1 line
1.8.1.2
#7 lore raw
In reply to Aneesh Kumar K.V
Quoted 3 lines · Aneesh Kumar K.V

Hi,

This patch series implement support for dumping guest memory using qemu gdb server. The last patch also enable qemu monitor command dump-guest-memory

Thanks, applied all but 2/6 to ppc-next. I think the core dump bits should be more smart eventually to determine whether we want to save ppc32 or ppc64 dumps, as qemu-system-ppc64 can execute 32bit CPUs. But for now your patch as is should work.

Alex