target-riscv: support QMP dump-guest-memory
Subsystems (2)
v4 cover target-riscv: support QMP dump-guest-memory
Hi,
This patch supports QMP dump-guest-memory in RISC-V. We tested this feature by using following command: dump-guest-memory guest.memory.
Then we used the gdb tool to debug guest.memory: gdb vmlinux guest.memory. The test result is as follow:
1. info registers
ra 0xffffffe0008cb83c 0xffffffe0008cb83c <_raw_spin_lock_irqsave+28> sp 0xffffffe0012c3f70 0xffffffe0012c3f70 gp 0xffffffe0010d6048 0xffffffe0010d6048 <__compound_literal.109> tp 0xffffffe00127f200 0xffffffe00127f200 t0 0x1f8 504 t1 0x0 0 t2 0x3fd9bf5c3c 274236136508 fp 0xffffffe0012c3f80 0xffffffe0012c3f80 s1 0xffffffe0010d7228 -137421295064 a0 0x1 1 a1 0xffffffe00127f200 -137419558400 a2 0xffffffe00110a0b8 -137421086536 a3 0x3af32000 989011968 a4 0x35b2 13746 a5 0xffffffe03af6b880 -136449705856 a6 0x1c5d09af00 121820000000 a7 0x54494d45 1414090053 s2 0x1 1 s3 0xffffffe0010d73f0 -137421294608 s4 0x0 0 s5 0x0 0 s6 0x0 0 s7 0xc 12 s8 0x2000 8192 s9 0x80015708 2147571464 s10 0x0 0 s11 0x0 0 t3 0x2257d71 36011377 t4 0x0 0 t5 0x3ab0030 61538352 t6 0x3fffefb3a0 274876838816 pc 0xffffffe000201478 0xffffffe000201478 <arch_cpu_idle+10>
2. x/1024ux 0x800000000x80000000: 0x00050433 0x000584b3 0x00060933 0x62c000ef0x80000010: 0x00050833 0x00040533 0x000485b3 0x000906330x80000020: 0x046358fd 0x1d630118 0x08171305 0x081300000x80000030: 0x48854868 0x0118282f 0x12081463 0x000002970x80000040: 0x48428293 0x00000317 0xfbc30313 0x0062b023
...
Changes since v3
1. Fix commit information and code comments
Changes since v2
1. Add build-bugs.
Changes since v1
1. Fix the build failure for RISC-V linux user.
Yifei Jiang (1):
target-riscv: support QMP dump-guest-memory
target/riscv/arch_dump.c | 202 +++++++++++++++++++++++++++++++++++++++ target/riscv/cpu.c | 2 + target/riscv/cpu.h | 4 + target/riscv/cpu_bits.h | 1 + target/riscv/meson.build | 1 + 5 files changed, 210 insertions(+) create mode 100644 target/riscv/arch_dump.c
Signature · 1 line
2.19.1
PATCH v4 1/1 target-riscv: support QMP dump-guest-memory
Add the support needed for creating prstatus elf notes. This allows us to use QMP dump-guest-memory.
Now ELF notes of RISC-V only contain prstatus elf notes.
--- target/riscv/arch_dump.c | 202 +++++++++++++++++++++++++++++++++++++++ target/riscv/cpu.c | 2 + target/riscv/cpu.h | 4 + target/riscv/cpu_bits.h | 1 + target/riscv/meson.build | 1 + 5 files changed, 210 insertions(+) create mode 100644 target/riscv/arch_dump.c
target/riscv/arch_dump.c new file +202 -0
@@ -0,0 +1,202 @@ +/* Support for writing ELF notes for RISC-V architectures +* +* Copyright (C) 2021 Huawei Technologies Co., Ltd +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "qemu/osdep.h" +#include "cpu.h" +#include "elf.h" +#include "sysemu/dump.h" + +/* struct user_regs_struct from arch/riscv/include/uapi/asm/ptrace.h */ +struct riscv64_user_regs { +uint64_t pc; +uint64_t regs[31 ]; +}QEMU_PACKED ; + +QEMU_BUILD_BUG_ON (sizeof (struct riscv64_user_regs ) !=256 ); + +/* struct elf_prstatus from include/linux/elfcore.h */ +struct riscv64_elf_prstatus { +char pad1[32 ];/* 32 == offsetof(struct elf_prstatus, pr_pid) */ +uint32_t pr_pid; +char pad2[76 ];/* 76 == offsetof(struct elf_prstatus, pr_reg) - +offsetof(struct elf_prstatus, pr_ppid) */ +struct riscv64_user_regs pr_reg; +char pad3[8 ]; +}QEMU_PACKED ; + +QEMU_BUILD_BUG_ON (sizeof (struct riscv64_elf_prstatus ) !=376 ); + +struct riscv64_note { +Elf64_Nhdr hdr; +char name[8 ];/* align_up(sizeof("CORE"), 4) */ +struct riscv64_elf_prstatus prstatus; +}QEMU_PACKED ; + +#define RISCV64_NOTE_HEADER_SIZE offsetof(struct riscv64_note, prstatus) +#define RISCV64_PRSTATUS_NOTE_SIZE \ + (RISCV64_NOTE_HEADER_SIZE + sizeof(struct riscv64_elf_prstatus)) + +static void riscv64_note_init (struct riscv64_note *note,DumpState *s, +const char *name,Elf64_Word namesz, +Elf64_Word type,Elf64_Word descsz) +{ +memset (note,0 ,sizeof (*note)); + + note->hdr.n_namesz =cpu_to_dump32 (s, namesz); + note->hdr.n_descsz =cpu_to_dump32 (s, descsz); + note->hdr.n_type =cpu_to_dump32 (s, type); + +memcpy (note->name, name, namesz); +} + +int riscv_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, +int cpuid,void *opaque) +{ +struct riscv64_note note; +RISCVCPU *cpu =RISCV_CPU (cs); +CPURISCVState *env = &cpu->env; +DumpState *s = opaque; +int ret, i =0 ; +const char name[] ="CORE" ; + +riscv64_note_init (¬e, s, name,sizeof (name), +NT_PRSTATUS ,sizeof (note.prstatus)); + + note.prstatus.pr_pid =cpu_to_dump32 (s, cpuid); + + note.prstatus.pr_reg.pc =cpu_to_dump64 (s, env->pc); + +for (i =0 ; i <31 ; i++) { + note.prstatus.pr_reg.regs[i] =cpu_to_dump64 (s, env->gpr[i +1 ]); + } + + ret =f (¬e,RISCV64_PRSTATUS_NOTE_SIZE , s); +if (ret <0 ) { +return -1 ; + } + +return ret; +} + +struct riscv32_user_regs { +uint32_t pc; +uint32_t regs[31 ]; +}QEMU_PACKED ; + +QEMU_BUILD_BUG_ON (sizeof (struct riscv32_user_regs ) !=128 ); + +struct riscv32_elf_prstatus { +char pad1[24 ];/* 24 == offsetof(struct elf_prstatus, pr_pid) */ +uint32_t pr_pid; +char pad2[44 ];/* 44 == offsetof(struct elf_prstatus, pr_reg) - +offsetof(struct elf_prstatus, pr_ppid) */ +struct riscv32_user_regs pr_reg; +char pad3[4 ]; +}QEMU_PACKED ; + +QEMU_BUILD_BUG_ON (sizeof (struct riscv32_elf_prstatus ) !=204 ); + +struct riscv32_note { +Elf32_Nhdr hdr; +char name[8 ];/* align_up(sizeof("CORE"), 4) */ +struct riscv32_elf_prstatus prstatus; +}QEMU_PACKED ; + +#define RISCV32_NOTE_HEADER_SIZE offsetof(struct riscv32_note, prstatus) +#define RISCV32_PRSTATUS_NOTE_SIZE \ + (RISCV32_NOTE_HEADER_SIZE + sizeof(struct riscv32_elf_prstatus)) + +static void riscv32_note_init (struct riscv32_note *note,DumpState *s, +const char *name,Elf32_Word namesz, +Elf32_Word type,Elf32_Word descsz) +{ +memset (note,0 ,sizeof (*note)); + + note->hdr.n_namesz =cpu_to_dump32 (s, namesz); + note->hdr.n_descsz =cpu_to_dump32 (s, descsz); + note->hdr.n_type =cpu_to_dump32 (s, type); + +memcpy (note->name, name, namesz); +} + +int riscv_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cs, +int cpuid,void *opaque) +{ +struct riscv32_note note; +RISCVCPU *cpu =RISCV_CPU (cs); +CPURISCVState *env = &cpu->env; +DumpState *s = opaque; +int ret, i; +const char name[] ="CORE" ; + +riscv32_note_init (¬e, s, name,sizeof (name), +NT_PRSTATUS ,sizeof (note.prstatus)); + + note.prstatus.pr_pid =cpu_to_dump32 (s, cpuid); + + note.prstatus.pr_reg.pc =cpu_to_dump32 (s, env->pc); + +for (i =0 ; i <31 ; i++) { + note.prstatus.pr_reg.regs[i] =cpu_to_dump32 (s, env->gpr[i +1 ]); + } + + ret =f (¬e,RISCV32_PRSTATUS_NOTE_SIZE , s); +if (ret <0 ) { +return -1 ; + } + +return ret; +} + +int cpu_get_dump_info (ArchDumpInfo *info, +const GuestPhysBlockList *guest_phys_blocks) +{ +RISCVCPU *cpu; +CPURISCVState *env; + +if (first_cpu ==NULL ) { +return -1 ; + } + cpu =RISCV_CPU (first_cpu); + env = &cpu->env; + + info->d_machine =EM_RISCV ; + +#if defined(TARGET_RISCV64 ) + info->d_class =ELFCLASS64 ; +#else + info->d_class =ELFCLASS32 ; +#endif + + info->d_endian = (env->mstatus &MSTATUS_UBE ) !=0 + ?ELFDATA2MSB :ELFDATA2LSB ; + +return 0 ; +} + +ssize_t cpu_get_note_size (int class,int machine,int nr_cpus) +{ +size_t note_size; + +if (class ==ELFCLASS64 ) { + note_size =RISCV64_PRSTATUS_NOTE_SIZE ; + }else { + note_size =RISCV32_PRSTATUS_NOTE_SIZE ; + } + +return note_size * nr_cpus; +}
target/riscv/cpu.c +2 -0
@@ -614,6 +614,8 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data) cc->get_phys_page_debug = riscv_cpu_get_phys_page_debug;/* For now, mark unmigratable: */ cc->vmsd = &vmstate_riscv_cpu; + cc->write_elf64_note = riscv_cpu_write_elf64_note; + cc->write_elf32_note = riscv_cpu_write_elf32_note;#endif cc->gdb_arch_name = riscv_gdb_arch_name; cc->gdb_get_dynamic_xml = riscv_gdb_get_dynamic_xml;
target/riscv/cpu.h +4 -0
@@ -323,6 +323,10 @@ extern const char * const riscv_intr_names[];const char *riscv_cpu_get_trap_name (target_ulong cause,bool async);void riscv_cpu_do_interrupt (CPUState *cpu); +int riscv_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, +int cpuid,void *opaque); +int riscv_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cs, +int cpuid,void *opaque);int riscv_cpu_gdb_read_register (CPUState *cpu,GByteArray *buf,int reg);int riscv_cpu_gdb_write_register (CPUState *cpu,uint8_t *buf,int reg);bool riscv_cpu_exec_interrupt (CPUState *cs,int interrupt_request);
target/riscv/cpu_bits.h +1 -0
@@ -368,6 +368,7 @@#define MSTATUS_MIE 0x00000008#define MSTATUS_UPIE 0x00000010#define MSTATUS_SPIE 0x00000020 +#define MSTATUS_UBE 0x00000040#define MSTATUS_MPIE 0x00000080#define MSTATUS_SPP 0x00000100#define MSTATUS_MPP 0x00001800
target/riscv/meson.build +1 -0
@@ -26,6 +26,7 @@ riscv_ss.add(files( riscv_softmmu_ss = ss.source_set() riscv_softmmu_ss.add(files( + 'arch_dump.c', 'pmp.c', 'monitor.c', 'machine.c'
Signature · 1 line
2.19.1
Alistair Francis
AppliedQuoted 11 lines · Yifei Jiang
Add the support needed for creating prstatus elf notes. This allows us to use QMP dump-guest-memory.
Now ELF notes of RISC-V only contain prstatus elf notes.
Signed-off-by Yifei Jiang <[email protected]>Signed-off-by Mingwang Li <[email protected]>Reviewed-by Alistair Francis <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Palmer Dabbelt <[email protected]>
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 273 lines
--- target/riscv/arch_dump.c | 202 +++++++++++++++++++++++++++++++++++++++ target/riscv/cpu.c | 2 + target/riscv/cpu.h | 4 + target/riscv/cpu_bits.h | 1 + target/riscv/meson.build | 1 + 5 files changed, 210 insertions(+) create mode 100644 target/riscv/arch_dump.ctarget/riscv/arch_dump.c new file +202
-0@@ -0,0 +1,202 @@ +/* Support for writing ELF notes for RISC-V architectures +* +* Copyright (C) 2021 Huawei Technologies Co., Ltd +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "qemu/osdep.h" +#include "cpu.h" +#include "elf.h" +#include "sysemu/dump.h" + +/* struct user_regs_struct from arch/riscv/include/uapi/asm/ptrace.h */ +struct riscv64_user_regs { +uint64_t pc; +uint64_t regs[31 ]; +}QEMU_PACKED ; + +QEMU_BUILD_BUG_ON (sizeof (struct riscv64_user_regs ) !=256 ); + +/* struct elf_prstatus from include/linux/elfcore.h */ +struct riscv64_elf_prstatus { +char pad1[32 ];/* 32 == offsetof(struct elf_prstatus, pr_pid) */ +uint32_t pr_pid; +char pad2[76 ];/* 76 == offsetof(struct elf_prstatus, pr_reg) - +offsetof(struct elf_prstatus, pr_ppid) */ +struct riscv64_user_regs pr_reg; +char pad3[8 ]; +}QEMU_PACKED ; + +QEMU_BUILD_BUG_ON (sizeof (struct riscv64_elf_prstatus ) !=376 ); + +struct riscv64_note { +Elf64_Nhdr hdr; +char name[8 ];/* align_up(sizeof("CORE"), 4) */ +struct riscv64_elf_prstatus prstatus; +}QEMU_PACKED ; + +#define RISCV64_NOTE_HEADER_SIZE offsetof(struct riscv64_note, prstatus) +#define RISCV64_PRSTATUS_NOTE_SIZE \ + (RISCV64_NOTE_HEADER_SIZE + sizeof(struct riscv64_elf_prstatus)) + +static void riscv64_note_init (struct riscv64_note *note,DumpState *s, +const char *name,Elf64_Word namesz, +Elf64_Word type,Elf64_Word descsz) +{ +memset (note,0 ,sizeof (*note)); + + note->hdr.n_namesz =cpu_to_dump32 (s, namesz); + note->hdr.n_descsz =cpu_to_dump32 (s, descsz); + note->hdr.n_type =cpu_to_dump32 (s, type); + +memcpy (note->name, name, namesz); +} + +int riscv_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, +int cpuid,void *opaque) +{ +struct riscv64_note note; +RISCVCPU *cpu =RISCV_CPU (cs); +CPURISCVState *env = &cpu->env; +DumpState *s = opaque; +int ret, i =0 ; +const char name[] ="CORE" ; + +riscv64_note_init (¬e, s, name,sizeof (name), +NT_PRSTATUS ,sizeof (note.prstatus)); + + note.prstatus.pr_pid =cpu_to_dump32 (s, cpuid); + + note.prstatus.pr_reg.pc =cpu_to_dump64 (s, env->pc); + +for (i =0 ; i <31 ; i++) { + note.prstatus.pr_reg.regs[i] =cpu_to_dump64 (s, env->gpr[i +1 ]); + } + + ret =f (¬e,RISCV64_PRSTATUS_NOTE_SIZE , s); +if (ret <0 ) { +return -1 ; + } + +return ret; +} + +struct riscv32_user_regs { +uint32_t pc; +uint32_t regs[31 ]; +}QEMU_PACKED ; + +QEMU_BUILD_BUG_ON (sizeof (struct riscv32_user_regs ) !=128 ); + +struct riscv32_elf_prstatus { +char pad1[24 ];/* 24 == offsetof(struct elf_prstatus, pr_pid) */ +uint32_t pr_pid; +char pad2[44 ];/* 44 == offsetof(struct elf_prstatus, pr_reg) - +offsetof(struct elf_prstatus, pr_ppid) */ +struct riscv32_user_regs pr_reg; +char pad3[4 ]; +}QEMU_PACKED ; + +QEMU_BUILD_BUG_ON (sizeof (struct riscv32_elf_prstatus ) !=204 ); + +struct riscv32_note { +Elf32_Nhdr hdr; +char name[8 ];/* align_up(sizeof("CORE"), 4) */ +struct riscv32_elf_prstatus prstatus; +}QEMU_PACKED ; + +#define RISCV32_NOTE_HEADER_SIZE offsetof(struct riscv32_note, prstatus) +#define RISCV32_PRSTATUS_NOTE_SIZE \ + (RISCV32_NOTE_HEADER_SIZE + sizeof(struct riscv32_elf_prstatus)) + +static void riscv32_note_init (struct riscv32_note *note,DumpState *s, +const char *name,Elf32_Word namesz, +Elf32_Word type,Elf32_Word descsz) +{ +memset (note,0 ,sizeof (*note)); + + note->hdr.n_namesz =cpu_to_dump32 (s, namesz); + note->hdr.n_descsz =cpu_to_dump32 (s, descsz); + note->hdr.n_type =cpu_to_dump32 (s, type); + +memcpy (note->name, name, namesz); +} + +int riscv_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cs, +int cpuid,void *opaque) +{ +struct riscv32_note note; +RISCVCPU *cpu =RISCV_CPU (cs); +CPURISCVState *env = &cpu->env; +DumpState *s = opaque; +int ret, i; +const char name[] ="CORE" ; + +riscv32_note_init (¬e, s, name,sizeof (name), +NT_PRSTATUS ,sizeof (note.prstatus)); + + note.prstatus.pr_pid =cpu_to_dump32 (s, cpuid); + + note.prstatus.pr_reg.pc =cpu_to_dump32 (s, env->pc); + +for (i =0 ; i <31 ; i++) { + note.prstatus.pr_reg.regs[i] =cpu_to_dump32 (s, env->gpr[i +1 ]); + } + + ret =f (¬e,RISCV32_PRSTATUS_NOTE_SIZE , s); +if (ret <0 ) { +return -1 ; + } + +return ret; +} + +int cpu_get_dump_info (ArchDumpInfo *info, +const GuestPhysBlockList *guest_phys_blocks) +{ +RISCVCPU *cpu; +CPURISCVState *env; + +if (first_cpu ==NULL ) { +return -1 ; + } + cpu =RISCV_CPU (first_cpu); + env = &cpu->env; + + info->d_machine =EM_RISCV ; + +#if defined(TARGET_RISCV64 ) + info->d_class =ELFCLASS64 ; +#else + info->d_class =ELFCLASS32 ; +#endif + + info->d_endian = (env->mstatus &MSTATUS_UBE ) !=0 + ?ELFDATA2MSB :ELFDATA2LSB ; + +return 0 ; +} + +ssize_t cpu_get_note_size (int class,int machine,int nr_cpus) +{ +size_t note_size; + +if (class ==ELFCLASS64 ) { + note_size =RISCV64_PRSTATUS_NOTE_SIZE ; + }else { + note_size =RISCV32_PRSTATUS_NOTE_SIZE ; + } + +return note_size * nr_cpus; +}target/riscv/cpu.c +2
-0@@ -614,6 +614,8 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data) cc->get_phys_page_debug = riscv_cpu_get_phys_page_debug;/* For now, mark unmigratable: */ cc->vmsd = &vmstate_riscv_cpu; + cc->write_elf64_note = riscv_cpu_write_elf64_note; + cc->write_elf32_note = riscv_cpu_write_elf32_note;#endif cc->gdb_arch_name = riscv_gdb_arch_name; cc->gdb_get_dynamic_xml = riscv_gdb_get_dynamic_xml;target/riscv/cpu.h +4
-0@@ -323,6 +323,10 @@ extern const char * const riscv_intr_names[];const char *riscv_cpu_get_trap_name (target_ulong cause,bool async);void riscv_cpu_do_interrupt (CPUState *cpu); +int riscv_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, +int cpuid,void *opaque); +int riscv_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cs, +int cpuid,void *opaque);int riscv_cpu_gdb_read_register (CPUState *cpu,GByteArray *buf,int reg);int riscv_cpu_gdb_write_register (CPUState *cpu,uint8_t *buf,int reg);bool riscv_cpu_exec_interrupt (CPUState *cs,int interrupt_request);target/riscv/cpu_bits.h +1
-0@@ -368,6 +368,7 @@#define MSTATUS_MIE 0x00000008#define MSTATUS_UPIE 0x00000010#define MSTATUS_SPIE 0x00000020 +#define MSTATUS_UBE 0x00000040#define MSTATUS_MPIE 0x00000080#define MSTATUS_SPP 0x00000100#define MSTATUS_MPP 0x00001800target/riscv/meson.build +1
-1@@ -26,6 +26,7 @@ riscv_ss.add(files( riscv_softmmu_ss = ss.source_set() riscv_softmmu_ss.add(files( + 'arch_dump.c', 'pmp.c', 'monitor.c', 'machine.c' --2.19.1