Dump patches
From: Marc-André Lureau <[email protected]>
The following changes since commit f1d33f55c47dfdaf8daacd618588ad3ae4c452d1:
Merge tag 'pull-testing-gdbstub-plugins-gitdm-061022-3' of https://github.com/stsquad/qemu into staging (2022-10-06 07:11:56 -0400)
are available in the Git repository at:
https://gitlab.com/marcandre.lureau/qemu.git tags/dump-pull-request
for you to fetch changes up to 94d788408d2d5a6474c99b2c9cf06913b9db7c58:
dump: fix kdump to work over non-aligned blocks (2022-10-06 19:31:54 +0400)
----------------------------------------------------------------
dump patches
Hi
Includes:
- the first patches from "[PATCH v5 00/18] dump: Add arch section and s390x PV dump"
- "[PATCH v2 0/2] Fix dumping in kdump format with non-aligned memory"
----------------------------------------------------------------
Janosch Frank (8):
dump: Replace opaque DumpState pointer with a typed one
dump: Rename write_elf_loads to write_elf_phdr_loads
dump: Refactor dump_iterate and introduce dump_filter_memblock_*()
dump: Rework get_start_block
dump: Rework filter area variables
dump: Rework dump_calculate_size function
dump: Split elf header functions into prepare and write
dump: Rename write_elf*_phdr_note to prepare_elf*_phdr_note
Marc-André Lureau (2):
dump: simplify a bit kdump get_next_page()
dump: fix kdump to work over non-aligned blocks
include/hw/core/sysemu-cpu-ops.h | 8 +- include/qemu/typedefs.h | 1 + include/sysemu/dump.h | 15 +- target/arm/cpu.h | 4 +- target/i386/cpu.h | 8 +- target/ppc/cpu.h | 4 +- target/riscv/cpu.h | 4 +- target/s390x/s390x-internal.h | 2 +- dump/dump.c | 361 +++++++++++++++++-------------- target/arm/arch_dump.c | 6 +- target/i386/arch_dump.c | 30 +-- target/ppc/arch_dump.c | 18 +- target/riscv/arch_dump.c | 6 +- target/s390x/arch_dump.c | 10 +- 14 files changed, 259 insertions(+), 218 deletions(-)
Signature · 1 line
2.37.3
[PULL 01/10] dump: Replace opaque DumpState pointer with a typed one
From: Janosch Frank <[email protected]>
It's always better to convey the type of a pointer if at all possible. So let's add the DumpState typedef to typedefs.h and move the dump note functions from the opaque pointers to DumpState pointers.
Message-Id: <[email protected]>
--- include/hw/core/sysemu-cpu-ops.h | 8 ++++---- include/qemu/typedefs.h | 1 + target/arm/cpu.h | 4 ++-- target/i386/cpu.h | 8 ++++---- target/ppc/cpu.h | 4 ++-- target/riscv/cpu.h | 4 ++-- target/s390x/s390x-internal.h | 2 +- target/arm/arch_dump.c | 6 ++---- target/i386/arch_dump.c | 30 +++++++++++++++--------------- target/ppc/arch_dump.c | 18 +++++++++--------- target/riscv/arch_dump.c | 6 ++---- target/s390x/arch_dump.c | 10 +++++----- 12 files changed, 49 insertions(+), 52 deletions(-)
include/hw/core/sysemu-cpu-ops.h +4 -4
@@ -53,25 +53,25 @@ typedef struct SysemuCPUOps {* 32-bit VM coredump. */ int (*write_elf32_note)(WriteCoreDumpFunction f,CPUState *cpu, -int cpuid,void *opaque); +int cpuid,DumpState *s);/** * @write_elf64_note: Callback for writing a CPU-specific ELF note to a * 64-bit VM coredump. */ int (*write_elf64_note)(WriteCoreDumpFunction f,CPUState *cpu, -int cpuid,void *opaque); +int cpuid,DumpState *s);/** * @write_elf32_qemunote: Callback for writing a CPU- and QEMU-specific ELF * note to a 32-bit VM coredump. */ int (*write_elf32_qemunote)(WriteCoreDumpFunction f,CPUState *cpu, -void *opaque); +DumpState *s);/** * @write_elf64_qemunote: Callback for writing a CPU- and QEMU-specific ELF * note to a 64-bit VM coredump. */ int (*write_elf64_qemunote)(WriteCoreDumpFunction f,CPUState *cpu, -void *opaque); +DumpState *s);/** * @virtio_is_big_endian: Callback to return %true if a CPU which supports * runtime configurable endianness is currently big-endian.
include/qemu/typedefs.h +1 -0
@@ -131,6 +131,7 @@ typedef struct VirtIODevice VirtIODevice;typedef struct Visitor Visitor ;typedef struct VMChangeStateEntry VMChangeStateEntry ;typedef struct VMStateDescription VMStateDescription ; +typedef struct DumpState DumpState ;/* * Pointer types
target/arm/cpu.h +2 -2
@@ -1102,9 +1102,9 @@ int arm_gen_dynamic_svereg_xml(CPUState *cpu, int base_reg);const char *arm_gdb_get_dynamic_xml (CPUState *cpu,const char *xmlname);int arm_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque); +int cpuid,DumpState *s);int arm_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque); +int cpuid,DumpState *s);#ifdef TARGET_AARCH64 int aarch64_cpu_gdb_read_register (CPUState *cpu,GByteArray *buf,int reg);
target/i386/cpu.h +4 -4
@@ -1938,13 +1938,13 @@ extern const VMStateDescription vmstate_x86_cpu;int x86_cpu_pending_interrupt (CPUState *cs,int interrupt_request);int x86_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cpu, -int cpuid,void *opaque); +int cpuid,DumpState *s);int x86_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cpu, -int cpuid,void *opaque); +int cpuid,DumpState *s);int x86_cpu_write_elf64_qemunote (WriteCoreDumpFunction f,CPUState *cpu, -void *opaque); +DumpState *s);int x86_cpu_write_elf32_qemunote (WriteCoreDumpFunction f,CPUState *cpu, -void *opaque); +DumpState *s);void x86_cpu_get_memory_mapping (CPUState *cpu,MemoryMappingList *list,Error **errp);
target/ppc/cpu.h +2 -2
@@ -1354,9 +1354,9 @@ void ppc_gdb_gen_spr_xml(PowerPCCPU *cpu);const char *ppc_gdb_get_dynamic_xml (CPUState *cs,const char *xml_name);#endif int ppc64_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque); +int cpuid,DumpState *s);int ppc32_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque); +int cpuid,DumpState *s);#ifndef CONFIG_USER_ONLY void ppc_cpu_do_interrupt (CPUState *cpu);bool ppc_cpu_exec_interrupt (CPUState *cpu,int int_req);
target/riscv/cpu.h +2 -2
@@ -534,9 +534,9 @@ extern const char * const riscv_fpr_regnames[];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 cpuid,DumpState *s);int riscv_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque); +int cpuid,DumpState *s);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);int riscv_cpu_hviprio_index2irq (int index,int *out_irq,int *out_rdzero);
target/s390x/s390x-internal.h +1 -1
@@ -227,7 +227,7 @@ static inline hwaddr decode_basedisp_s(CPUS390XState *env, uint32_t ipb,/* arch_dump.c */ int s390_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque); +int cpuid,DumpState *s);/* cc_helper.c */
target/arm/arch_dump.c +2 -4
@@ -232,12 +232,11 @@ static int aarch64_write_elf64_sve(WriteCoreDumpFunction f,#endif int arm_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque) +int cpuid,DumpState *s) {struct aarch64_note note;ARMCPU *cpu =ARM_CPU (cs);CPUARMState *env = &cpu->env; -DumpState *s = opaque;uint64_t pstate, sp;int ret, i;
@@ -360,12 +359,11 @@ static int arm_write_elf32_vfp(WriteCoreDumpFunction f, CPUARMState *env, }int arm_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque) +int cpuid,DumpState *s) {struct arm_note note;ARMCPU *cpu =ARM_CPU (cs);CPUARMState *env = &cpu->env; -DumpState *s = opaque;int ret, i;bool fpvalid =cpu_isar_feature (aa32_vfp_simd, cpu);
target/i386/arch_dump.c +15 -15
@@ -42,7 +42,7 @@ typedef struct {static int x86_64_write_elf64_note (WriteCoreDumpFunction f,CPUX86State *env,int id, -void *opaque) +DumpState *s) {x86_64_user_regs_struct regs;Elf64_Nhdr *note;
@@ -94,7 +94,7 @@ static int x86_64_write_elf64_note(WriteCoreDumpFunction f, buf += descsz -sizeof (x86_64_user_regs_struct)-sizeof (target_ulong);memcpy (buf, ®s,sizeof (x86_64_user_regs_struct)); - ret =f (note, note_size, opaque); + ret =f (note, note_size, s);g_free (note);if (ret <0 ) {return -1 ;
@@ -148,7 +148,7 @@ static void x86_fill_elf_prstatus(x86_elf_prstatus *prstatus, CPUX86State *env, }static int x86_write_elf64_note (WriteCoreDumpFunction f,CPUX86State *env, -int id,void *opaque) +int id,DumpState *s) {x86_elf_prstatus prstatus;Elf64_Nhdr *note;
@@ -170,7 +170,7 @@ static int x86_write_elf64_note(WriteCoreDumpFunction f, CPUX86State *env, buf +=ROUND_UP (name_size,4 );memcpy (buf, &prstatus,sizeof (prstatus)); - ret =f (note, note_size, opaque); + ret =f (note, note_size, s);g_free (note);if (ret <0 ) {return -1 ;
@@ -180,7 +180,7 @@ static int x86_write_elf64_note(WriteCoreDumpFunction f, CPUX86State *env, }int x86_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque) +int cpuid,DumpState *s) {X86CPU *cpu =X86_CPU (cs);int ret;
@@ -189,10 +189,10 @@ int x86_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs,bool lma = !!(first_x86_cpu->env.hflags &HF_LMA_MASK );if (lma) { - ret =x86_64_write_elf64_note (f, &cpu->env, cpuid, opaque); + ret =x86_64_write_elf64_note (f, &cpu->env, cpuid, s); }else {#endif - ret =x86_write_elf64_note (f, &cpu->env, cpuid, opaque); + ret =x86_write_elf64_note (f, &cpu->env, cpuid, s); #ifdef TARGET_X86_64 }#endif
@@ -201,7 +201,7 @@ int x86_cpu_write_elf64_note(WriteCoreDumpFunction f, CPUState *cs, }int x86_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque) +int cpuid,DumpState *s) {X86CPU *cpu =X86_CPU (cs);x86_elf_prstatus prstatus;
@@ -224,7 +224,7 @@ int x86_cpu_write_elf32_note(WriteCoreDumpFunction f, CPUState *cs, buf +=ROUND_UP (name_size,4 );memcpy (buf, &prstatus,sizeof (prstatus)); - ret =f (note, note_size, opaque); + ret =f (note, note_size, s);g_free (note);if (ret <0 ) {return -1 ;
@@ -329,7 +329,7 @@ static void qemu_get_cpustate(QEMUCPUState *s, CPUX86State *env)static inline int cpu_write_qemu_note (WriteCoreDumpFunction f,CPUX86State *env, -void *opaque, +DumpState *s,int type) {QEMUCPUState state;
@@ -369,7 +369,7 @@ static inline int cpu_write_qemu_note(WriteCoreDumpFunction f, buf +=ROUND_UP (name_size,4 );memcpy (buf, &state,sizeof (state)); - ret =f (note, note_size, opaque); + ret =f (note, note_size, s);g_free (note);if (ret <0 ) {return -1 ;
@@ -379,19 +379,19 @@ static inline int cpu_write_qemu_note(WriteCoreDumpFunction f, }int x86_cpu_write_elf64_qemunote (WriteCoreDumpFunction f,CPUState *cs, -void *opaque) +DumpState *s) {X86CPU *cpu =X86_CPU (cs); -return cpu_write_qemu_note (f, &cpu->env, opaque,1 ); +return cpu_write_qemu_note (f, &cpu->env, s,1 ); }int x86_cpu_write_elf32_qemunote (WriteCoreDumpFunction f,CPUState *cs, -void *opaque) +DumpState *s) {X86CPU *cpu =X86_CPU (cs); -return cpu_write_qemu_note (f, &cpu->env, opaque,0 ); +return cpu_write_qemu_note (f, &cpu->env, s,0 ); } int cpu_get_dump_info(ArchDumpInfo *info,
target/ppc/arch_dump.c +9 -9
@@ -270,23 +270,23 @@ ssize_t cpu_get_note_size(int class, int machine, int nr_cpus)static int ppc_write_all_elf_notes (const char *note_name,WriteCoreDumpFunction f,PowerPCCPU *cpu,int id, -void *opaque) +DumpState *s) { -NoteFuncArg arg = { .state = opaque }; +NoteFuncArg arg = { .state = s };int ret =-1 ;int note_size;const NoteFuncDesc *nf;for (nf = note_func; nf->note_contents_func; nf++) { - arg.note.hdr.n_namesz =cpu_to_dump32 (opaque,sizeof (arg.note.name)); - arg.note.hdr.n_descsz =cpu_to_dump32 (opaque, nf->contents_size); + arg.note.hdr.n_namesz =cpu_to_dump32 (s,sizeof (arg.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); note_size =sizeof (arg.note) -sizeof (arg.note.contents) + nf->contents_size; - ret =f (&arg.note, note_size, opaque); + ret =f (&arg.note, note_size, s);if (ret <0 ) {return -1 ; }
@@ -295,15 +295,15 @@ static int ppc_write_all_elf_notes(const char *note_name, }int ppc64_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque) +int cpuid,DumpState *s) {PowerPCCPU *cpu =POWERPC_CPU (cs); -return ppc_write_all_elf_notes ("CORE" , f, cpu, cpuid, opaque); +return ppc_write_all_elf_notes ("CORE" , f, cpu, cpuid, s); }int ppc32_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque) +int cpuid,DumpState *s) {PowerPCCPU *cpu =POWERPC_CPU (cs); -return ppc_write_all_elf_notes ("CORE" , f, cpu, cpuid, opaque); +return ppc_write_all_elf_notes ("CORE" , f, cpu, cpuid, s); }
target/riscv/arch_dump.c +2 -4
@@ -64,12 +64,11 @@ static void riscv64_note_init(struct riscv64_note *note, DumpState *s, }int riscv_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque) +int cpuid,DumpState *s) {struct riscv64_note note;RISCVCPU *cpu =RISCV_CPU (cs);CPURISCVState *env = &cpu->env; -DumpState *s = opaque;int ret, i =0 ;const char name[] ="CORE" ;
@@ -134,12 +133,11 @@ static void riscv32_note_init(struct riscv32_note *note, DumpState *s, }int riscv_cpu_write_elf32_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque) +int cpuid,DumpState *s) {struct riscv32_note note;RISCVCPU *cpu =RISCV_CPU (cs);CPURISCVState *env = &cpu->env; -DumpState *s = opaque;int ret, i;const char name[] ="CORE" ;
target/s390x/arch_dump.c +5 -5
@@ -204,7 +204,7 @@ static const NoteFuncDesc note_linux[] = {static int s390x_write_elf64_notes (const char *note_name,WriteCoreDumpFunction f,S390CPU *cpu,int id, -void *opaque, +DumpState *s,const NoteFuncDesc *funcs) {Note note;
@@ -222,7 +222,7 @@ static int s390x_write_elf64_notes(const char *note_name, (*nf->note_contents_func)(¬e, cpu, id); note_size =sizeof (note) -sizeof (note.contents) + nf->contents_size; - ret =f (¬e, note_size, opaque); + ret =f (¬e, note_size, s);if (ret <0 ) {return -1 ;
@@ -235,16 +235,16 @@ static int s390x_write_elf64_notes(const char *note_name,int s390_cpu_write_elf64_note (WriteCoreDumpFunction f,CPUState *cs, -int cpuid,void *opaque) +int cpuid,DumpState *s) {S390CPU *cpu =S390_CPU (cs);int r; - r =s390x_write_elf64_notes ("CORE" , f, cpu, cpuid, opaque, note_core); + r =s390x_write_elf64_notes ("CORE" , f, cpu, cpuid, s, note_core);if (r) {return r; } -return s390x_write_elf64_notes ("LINUX" , f, cpu, cpuid, opaque, note_linux); +return s390x_write_elf64_notes ("LINUX" , f, cpu, cpuid, s, note_linux); } int cpu_get_dump_info(ArchDumpInfo *info,
Signature · 1 line
2.37.3
[PULL 03/10] dump: Refactor dump_iterate and introduce dump_filter_memblock_*()
In reply to [email protected]From: Janosch Frank <[email protected]>
The iteration over the memblocks in dump_iterate() is hard to understand so it's about time to clean it up. Instead of manually grabbing the next memblock we can use QTAILQ_FOREACH to iterate over all memblocks.
Additionally we move the calculation of the offset and length out by introducing and using the dump_filter_memblock_*() functions. These functions will later be used to cleanup other parts of dump.c.
Message-Id: <[email protected]>
--- dump/dump.c | 74 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 32 deletions(-)
dump/dump.c +42 -32
@@ -591,31 +591,43 @@ static void dump_begin(DumpState *s, Error **errp)write_elf_notes (s, errp); } -static int get_next_block (DumpState *s,GuestPhysBlock *block) +static int64_t dump_filtered_memblock_size (GuestPhysBlock *block, +int64_t filter_area_start, +int64_t filter_area_length) { -while (1 ) { - block =QTAILQ_NEXT (block, next); -if (!block) { -/* no more block */ -return 1 ; - } +int64_t size, left, right; - s->start =0 ; - s->next_block = block; -if (s->has_filter) { -if (block->target_start >= s->begin + s->length || - block->target_end <= s->begin) { -/* This block is out of the range */ -continue ; - } +/* No filter, return full size */ +if (!filter_area_length) { +return block->target_end - block->target_start; + } -if (s->begin > block->target_start) { - s->start = s->begin - block->target_start; - } +/* calculate the overlapped region. */ + left =MAX (filter_area_start, block->target_start); + right =MIN (filter_area_start + filter_area_length, block->target_end); + size = right - left; + size = size >0 ? size :0 ; + +return size; +} + +static int64_t dump_filtered_memblock_start (GuestPhysBlock *block, +int64_t filter_area_start, +int64_t filter_area_length) +{ +if (filter_area_length) { +/* return -1 if the block is not within filter area */ +if (block->target_start >= filter_area_start + filter_area_length || + block->target_end <= filter_area_start) { +return -1 ; } -return 0 ; +if (filter_area_start > block->target_start) { +return filter_area_start - block->target_start; + } } + +return 0 ; }/* write all memory to vmcore */
@@ -623,24 +635,22 @@ static void dump_iterate(DumpState *s, Error **errp) {ERRP_GUARD ();GuestPhysBlock *block; -int64_t size; - -do { - block = s->next_block; +int64_t memblock_size, memblock_start; - size = block->target_end - block->target_start; -if (s->has_filter) { - size -= s->start; -if (s->begin + s->length < block->target_end) { - size -= block->target_end - (s->begin + s->length); - } + QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { + memblock_start =dump_filtered_memblock_start (block, s->begin, s->length); +if (memblock_start ==-1 ) { +continue ; } -write_memory (s, block, s->start, size, errp); + + memblock_size =dump_filtered_memblock_size (block, s->begin, s->length); + +/* Write the memory to file */ +write_memory (s, block, memblock_start, memblock_size, errp);if (*errp) {return ; } - - }while (!get_next_block (s, block)); + } }static void create_vmcore (DumpState *s,Error **errp)
Signature · 1 line
2.37.3
[PULL 02/10] dump: Rename write_elf_loads to write_elf_phdr_loads
In reply to [email protected]From: Janosch Frank <[email protected]>
Let's make it a bit clearer that we write the program headers of the PT_LOAD type.
Message-Id: <[email protected]>
--- dump/dump.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
dump/dump.c +3 -3
@@ -490,7 +490,7 @@ static void get_offset_range(hwaddr phys_addr, } } -static void write_elf_loads (DumpState *s,Error **errp) +static void write_elf_phdr_loads (DumpState *s,Error **errp) {ERRP_GUARD ();hwaddr offset, filesz;
@@ -573,8 +573,8 @@ static void dump_begin(DumpState *s, Error **errp)return ; } -/* write all PT_LOAD to vmcore */ -write_elf_loads (s, errp); +/* write all PT_LOADs to vmcore */ +write_elf_phdr_loads (s, errp);if (*errp) {return ; }
Signature · 1 line
2.37.3
[PULL 04/10] dump: Rework get_start_block
In reply to [email protected]From: Janosch Frank <[email protected]>
get_start_block() returns the start address of the first memory block or -1.
With the GuestPhysBlock iterator conversion we don't need to set the start address and can therefore remove that code and the "start" DumpState struct member. The only functionality left is the validation of the start block so it only makes sense to re-name the function to validate_start_block()
Message-Id: <[email protected]>
--- include/sysemu/dump.h | 2 -- dump/dump.c | 20 ++++++-------------- 2 files changed, 6 insertions(+), 16 deletions(-)
include/sysemu/dump.h +0 -2
@@ -166,8 +166,6 @@ typedef struct DumpState {hwaddr memory_offset;int fd; -GuestPhysBlock *next_block; -ram_addr_t start;bool has_filter;int64_t begin;int64_t length;
dump/dump.c +6 -14
@@ -1500,30 +1500,22 @@ static void create_kdump_vmcore(DumpState *s, Error **errp) } } -static ram_addr_t get_start_block (DumpState *s) +static int validate_start_block (DumpState *s) {GuestPhysBlock *block;if (!s->has_filter) { - s->next_block =QTAILQ_FIRST (&s->guest_phys_blocks.head);return 0 ; } QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { +/* This block is out of the range */ if (block->target_start >= s->begin + s->length || block->target_end <= s->begin) { -/* This block is out of the range */ continue ; } - - s->next_block = block; -if (s->begin > block->target_start) { - s->start = s->begin - block->target_start; - }else { - s->start =0 ; - } -return s->start; - } +return 0 ; + }return -1 ; }
@@ -1670,8 +1662,8 @@ static void dump_init(DumpState *s, int fd, bool has_format, goto cleanup; } - s->start =get_start_block (s); -if (s->start ==-1 ) { +/* Is the filter filtering everything? */ +if (validate_start_block (s) ==-1 ) {error_setg (errp,QERR_INVALID_PARAMETER ,"begin" ); goto cleanup; }
Signature · 1 line
2.37.3
[PULL 05/10] dump: Rework filter area variables
In reply to [email protected]From: Janosch Frank <[email protected]>
While the DumpState begin and length variables directly mirror the API variable names they are not very descriptive. So let's add a "filter_area_" prefix and make has_filter a function checking length > 0.
Message-Id: <[email protected]>
--- include/sysemu/dump.h | 13 ++++++++--- dump/dump.c | 53 +++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 25 deletions(-)
include/sysemu/dump.h +10 -3
@@ -166,9 +166,16 @@ typedef struct DumpState {hwaddr memory_offset;int fd; -bool has_filter; -int64_t begin; -int64_t length; +/* +* Dump filter area variables +* +* A filtered dump only contains the guest memory designated by +* the start address and length variables defined below. +* +* If length is 0, no filtering is applied. +*/ +int64_t filter_area_begin;/* Start address of partial guest memory area */ +int64_t filter_area_length;/* Length of partial guest memory area */ uint8_t *note_buf;/* buffer for notes */ size_t note_buf_offset;/* the writing place in note_buf */
dump/dump.c +31 -22
119 lines not on this page · raw message
Signature · 1 line
2.37.3
[PULL 07/10] dump: Split elf header functions into prepare and write
In reply to [email protected]From: Janosch Frank <[email protected]>
Let's split the write from the modification of the elf header so we can consolidate the write of the data in one function.
Message-Id: <[email protected]>
--- dump/dump.c | 100 ++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 47 deletions(-)
dump/dump.c +53 -47
@@ -131,7 +131,7 @@ static int fd_write_vmcore(const void *buf, size_t size, void *opaque)return 0 ; } -static void write_elf64_header (DumpState *s,Error **errp) +static void prepare_elf64_header (DumpState *s,Elf64_Ehdr *elf_header) {/* * phnum in the elf header is 16 bit, if we have more segments we
@@ -139,34 +139,27 @@ static void write_elf64_header(DumpState *s, Error **errp)* special section. */ uint16_t phnum =MIN (s->phdr_num,PN_XNUM ); -Elf64_Ehdr elf_header; -int ret; -memset (&elf_header,0 ,sizeof (Elf64_Ehdr)); -memcpy (&elf_header,ELFMAG ,SELFMAG ); - elf_header.e_ident[EI_CLASS ] =ELFCLASS64 ; - elf_header.e_ident[EI_DATA ] = s->dump_info.d_endian; - elf_header.e_ident[EI_VERSION ] =EV_CURRENT ; - elf_header.e_type =cpu_to_dump16 (s,ET_CORE ); - elf_header.e_machine =cpu_to_dump16 (s, s->dump_info.d_machine); - elf_header.e_version =cpu_to_dump32 (s,EV_CURRENT ); - elf_header.e_ehsize =cpu_to_dump16 (s,sizeof (elf_header)); - elf_header.e_phoff =cpu_to_dump64 (s, s->phdr_offset); - elf_header.e_phentsize =cpu_to_dump16 (s,sizeof (Elf64_Phdr)); - elf_header.e_phnum =cpu_to_dump16 (s, phnum); +memset (elf_header,0 ,sizeof (Elf64_Ehdr)); +memcpy (elf_header,ELFMAG ,SELFMAG ); + elf_header->e_ident[EI_CLASS ] =ELFCLASS64 ; + elf_header->e_ident[EI_DATA ] = s->dump_info.d_endian; + elf_header->e_ident[EI_VERSION ] =EV_CURRENT ; + elf_header->e_type =cpu_to_dump16 (s,ET_CORE ); + elf_header->e_machine =cpu_to_dump16 (s, s->dump_info.d_machine); + elf_header->e_version =cpu_to_dump32 (s,EV_CURRENT ); + elf_header->e_ehsize =cpu_to_dump16 (s,sizeof (elf_header)); + elf_header->e_phoff =cpu_to_dump64 (s, s->phdr_offset); + elf_header->e_phentsize =cpu_to_dump16 (s,sizeof (Elf64_Phdr)); + elf_header->e_phnum =cpu_to_dump16 (s, phnum);if (s->shdr_num) { - elf_header.e_shoff =cpu_to_dump64 (s, s->shdr_offset); - elf_header.e_shentsize =cpu_to_dump16 (s,sizeof (Elf64_Shdr)); - elf_header.e_shnum =cpu_to_dump16 (s, s->shdr_num); - } - - ret =fd_write_vmcore (&elf_header,sizeof (elf_header), s); -if (ret <0 ) { -error_setg_errno (errp, -ret,"dump: failed to write elf header" ); + elf_header->e_shoff =cpu_to_dump64 (s, s->shdr_offset); + elf_header->e_shentsize =cpu_to_dump16 (s,sizeof (Elf64_Shdr)); + elf_header->e_shnum =cpu_to_dump16 (s, s->shdr_num); } } -static void write_elf32_header (DumpState *s,Error **errp) +static void prepare_elf32_header (DumpState *s,Elf32_Ehdr *elf_header) {/* * phnum in the elf header is 16 bit, if we have more segments we
@@ -174,28 +167,45 @@ static void write_elf32_header(DumpState *s, Error **errp)* special section. */ uint16_t phnum =MIN (s->phdr_num,PN_XNUM ); -Elf32_Ehdr elf_header; -int ret; -memset (&elf_header,0 ,sizeof (Elf32_Ehdr)); -memcpy (&elf_header,ELFMAG ,SELFMAG ); - elf_header.e_ident[EI_CLASS ] =ELFCLASS32 ; - elf_header.e_ident[EI_DATA ] = s->dump_info.d_endian; - elf_header.e_ident[EI_VERSION ] =EV_CURRENT ; - elf_header.e_type =cpu_to_dump16 (s,ET_CORE ); - elf_header.e_machine =cpu_to_dump16 (s, s->dump_info.d_machine); - elf_header.e_version =cpu_to_dump32 (s,EV_CURRENT ); - elf_header.e_ehsize =cpu_to_dump16 (s,sizeof (elf_header)); - elf_header.e_phoff =cpu_to_dump32 (s, s->phdr_offset); - elf_header.e_phentsize =cpu_to_dump16 (s,sizeof (Elf32_Phdr)); - elf_header.e_phnum =cpu_to_dump16 (s, phnum); +memset (elf_header,0 ,sizeof (Elf32_Ehdr)); +memcpy (elf_header,ELFMAG ,SELFMAG ); + elf_header->e_ident[EI_CLASS ] =ELFCLASS32 ; + elf_header->e_ident[EI_DATA ] = s->dump_info.d_endian; + elf_header->e_ident[EI_VERSION ] =EV_CURRENT ; + elf_header->e_type =cpu_to_dump16 (s,ET_CORE ); + elf_header->e_machine =cpu_to_dump16 (s, s->dump_info.d_machine); + elf_header->e_version =cpu_to_dump32 (s,EV_CURRENT ); + elf_header->e_ehsize =cpu_to_dump16 (s,sizeof (elf_header)); + elf_header->e_phoff =cpu_to_dump32 (s, s->phdr_offset); + elf_header->e_phentsize =cpu_to_dump16 (s,sizeof (Elf32_Phdr)); + elf_header->e_phnum =cpu_to_dump16 (s, phnum);if (s->shdr_num) { - elf_header.e_shoff =cpu_to_dump32 (s, s->shdr_offset); - elf_header.e_shentsize =cpu_to_dump16 (s,sizeof (Elf32_Shdr)); - elf_header.e_shnum =cpu_to_dump16 (s, s->shdr_num); + elf_header->e_shoff =cpu_to_dump32 (s, s->shdr_offset); + elf_header->e_shentsize =cpu_to_dump16 (s,sizeof (Elf32_Shdr)); + elf_header->e_shnum =cpu_to_dump16 (s, s->shdr_num); } +} - ret =fd_write_vmcore (&elf_header,sizeof (elf_header), s); +static void write_elf_header (DumpState *s,Error **errp) +{ +Elf32_Ehdr elf32_header; +Elf64_Ehdr elf64_header; +size_t header_size; +void *header_ptr; +int ret; + +if (dump_is_64bit (s)) { +prepare_elf64_header (s, &elf64_header); + header_size =sizeof (elf64_header); + header_ptr = &elf64_header; + }else { +prepare_elf32_header (s, &elf32_header); + header_size =sizeof (elf32_header); + header_ptr = &elf32_header; + } + + ret =fd_write_vmcore (header_ptr, header_size, s);if (ret <0 ) {error_setg_errno (errp, -ret,"dump: failed to write elf header" ); }
@@ -564,11 +574,7 @@ static void dump_begin(DumpState *s, Error **errp)*/ /* write elf header to vmcore */ -if (dump_is_64bit (s)) { -write_elf64_header (s, errp); - }else { -write_elf32_header (s, errp); - } +write_elf_header (s, errp);if (*errp) {return ; }
Signature · 1 line
2.37.3
[PULL 06/10] dump: Rework dump_calculate_size function
In reply to [email protected]From: Janosch Frank <[email protected]>
dump_calculate_size() sums up all the sizes of the guest memory blocks. Since we already have a function that calculates the size of a single memory block (dump_get_memblock_size()) we can simply iterate over the blocks and use the function instead of calculating the size ourselves.
Message-Id: <[email protected]>
--- dump/dump.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-)
dump/dump.c +8 -14
@@ -1548,25 +1548,19 @@ bool qemu_system_dump_in_progress(void)return (qatomic_read (&state->status) ==DUMP_STATUS_ACTIVE ); } -/* calculate total size of memory to be dumped (taking filter into -* acoount.) */ +/* +* calculate total size of memory to be dumped (taking filter into +* account.) +*/ static int64_t dump_calculate_size (DumpState *s) {GuestPhysBlock *block; -int64_t size =0 , total =0 , left =0 , right =0 ; +int64_t total =0 ; QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) { -if (dump_has_filter (s)) { -/* calculate the overlapped region. */ - left =MAX (s->filter_area_begin, block->target_start); - right =MIN (s->filter_area_begin + s->filter_area_length, block->target_end); - size = right - left; - size = size >0 ? size :0 ; - }else { -/* count the whole region in */ - size = (block->target_end - block->target_start); - } - total += size; + total +=dump_filtered_memblock_size (block, + s->filter_area_begin, + s->filter_area_length); }return total;
Signature · 1 line
2.37.3
[PULL 08/10] dump: Rename write_elf*_phdr_note to prepare_elf*_phdr_note
In reply to [email protected]From: Janosch Frank <[email protected]>
The functions in question do not actually write to the file descriptor they set up a buffer which is later written to the fd.
Message-Id: <[email protected]>
--- dump/dump.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
dump/dump.c +4 -4
@@ -260,7 +260,7 @@ static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping, } } -static void write_elf64_phdr_note (DumpState *s,Elf64_Phdr *phdr) +static void prepare_elf64_phdr_note (DumpState *s,Elf64_Phdr *phdr) {memset (phdr,0 ,sizeof (*phdr)); phdr->p_type =cpu_to_dump32 (s,PT_NOTE );
@@ -316,7 +316,7 @@ static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,write_guest_note (f, s, errp); } -static void write_elf32_phdr_note (DumpState *s,Elf32_Phdr *phdr) +static void prepare_elf32_phdr_note (DumpState *s,Elf32_Phdr *phdr) {memset (phdr,0 ,sizeof (*phdr)); phdr->p_type =cpu_to_dump32 (s,PT_NOTE );
@@ -364,11 +364,11 @@ static void write_elf_phdr_note(DumpState *s, Error **errp)int ret;if (dump_is_64bit (s)) { -write_elf64_phdr_note (s, &phdr64); +prepare_elf64_phdr_note (s, &phdr64); size =sizeof (phdr64); phdr = &phdr64; }else { -write_elf32_phdr_note (s, &phdr32); +prepare_elf32_phdr_note (s, &phdr32); size =sizeof (phdr32); phdr = &phdr32; }
Signature · 1 line
2.37.3
[PULL 09/10] dump: simplify a bit kdump get_next_page()
In reply to [email protected]From: Marc-André Lureau <[email protected]>
This should be functionally equivalent, but slightly easier to read, with simplified paths and checks at the end of the function.
The following patch is a major rewrite to get rid of the assert().
--- dump/dump.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
dump/dump.c +8 -13
@@ -1132,17 +1132,11 @@ static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,if (!block) { block =QTAILQ_FIRST (&s->guest_phys_blocks.head); *blockptr = block; -assert ((block->target_start & ~target_page_mask) ==0 ); -assert ((block->target_end & ~target_page_mask) ==0 ); - *pfnptr =dump_paddr_to_pfn (s, block->target_start); -if (bufptr) { - *bufptr = block->host_addr; - } -return true; + addr = block->target_start; + }else { + addr =dump_pfn_to_paddr (s, *pfnptr +1 ); } - - *pfnptr = *pfnptr +1 ; - addr =dump_pfn_to_paddr (s, *pfnptr); +assert (block !=NULL );if ((addr >= block->target_start) && (addr + s->dump_info.page_size <= block->target_end)) {
@@ -1154,12 +1148,13 @@ static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,if (!block) {return false; } -assert ((block->target_start & ~target_page_mask) ==0 ); -assert ((block->target_end & ~target_page_mask) ==0 ); - *pfnptr =dump_paddr_to_pfn (s, block->target_start); + addr = block->target_start; buf = block->host_addr; } +assert ((block->target_start & ~target_page_mask) ==0 ); +assert ((block->target_end & ~target_page_mask) ==0 ); + *pfnptr =dump_paddr_to_pfn (s, addr);if (bufptr) { *bufptr = buf; }
Signature · 1 line
2.37.3
[PULL 10/10] dump: fix kdump to work over non-aligned blocks
In reply to [email protected]From: Marc-André Lureau <[email protected]>
Rewrite get_next_page() to work over non-aligned blocks. When it encounters non aligned addresses, it will try to fill a page provided by the caller.
This solves a kdump crash with "tpm-crb-cmd" RAM memory region,
qemu-kvm: ../dump/dump.c:1162: _Bool get_next_page(GuestPhysBlock **, uint64_t *, uint8_t **, DumpState *): Assertion `(block->target_start & ~target_page_mask) == 0' failed.
because:guest_phys_block_add_section: target_start=00000000fed40080 target_end=00000000fed41000: added (count: 4)
Fixes:
https://bugzilla.redhat.com/show_bug.cgi?id=2120480
--- dump/dump.c | 79 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 23 deletions(-)
dump/dump.c +56 -23
@@ -1116,50 +1116,81 @@ static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn) }/* -* exam every page and return the page frame number and the address of the page. -* bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys -* blocks, so block->target_start and block->target_end should be interal -* multiples of the target page size. +* Return the page frame number and the page content in *bufptr. bufptr can be +* NULL. If not NULL, *bufptr must contains a target page size of pre-allocated +* memory. This is not necessarily the memory returned. */ static bool get_next_page (GuestPhysBlock **blockptr,uint64_t *pfnptr,uint8_t **bufptr,DumpState *s) {GuestPhysBlock *block = *blockptr; -hwaddr addr, target_page_mask = ~((hwaddr )s->dump_info.page_size -1 ); -uint8_t *buf; +uint32_t page_size = s->dump_info.page_size; +uint8_t *buf =NULL , *hbuf; +hwaddr addr;/* block == NULL means the start of the iteration */ if (!block) { block =QTAILQ_FIRST (&s->guest_phys_blocks.head); *blockptr = block; addr = block->target_start; + *pfnptr =dump_paddr_to_pfn (s, addr); }else { - addr =dump_pfn_to_paddr (s, *pfnptr +1 ); + *pfnptr +=1 ; + addr =dump_pfn_to_paddr (s, *pfnptr); }assert (block !=NULL ); -if ((addr >= block->target_start) && - (addr + s->dump_info.page_size <= block->target_end)) { - buf = block->host_addr + (addr - block->target_start); - }else { -/* the next page is in the next block */ - block =QTAILQ_NEXT (block, next); - *blockptr = block; -if (!block) { -return false; +while (1 ) { +if (addr >= block->target_start && addr < block->target_end) { +size_t n =MIN (block->target_end - addr, page_size - addr % page_size); + hbuf = block->host_addr + (addr - block->target_start); +if (!buf) { +if (n == page_size) { +/* this is a whole target page, go for it */ +assert (addr % page_size ==0 ); + buf = hbuf; +break ; + }else if (bufptr) { +assert (*bufptr); + buf = *bufptr; +memset (buf,0 , page_size); + }else { +return true; + } + } + +memcpy (buf + addr % page_size, hbuf, n); + addr += n; +if (addr % page_size ==0 ) { +/* we filled up the page */ +break ; + } + }else { +/* the next page is in the next block */ + *blockptr = block =QTAILQ_NEXT (block, next); +if (!block) { +break ; + } + + addr = block->target_start; +/* are we still in the same page? */ +if (dump_paddr_to_pfn (s, addr) != *pfnptr) { +if (buf) { +/* no, but we already filled something earlier, return it */ +break ; + }else { +/* else continue from there */ + *pfnptr =dump_paddr_to_pfn (s, addr); + } + } } - addr = block->target_start; - buf = block->host_addr; } -assert ((block->target_start & ~target_page_mask) ==0 ); -assert ((block->target_end & ~target_page_mask) ==0 ); - *pfnptr =dump_paddr_to_pfn (s, addr);if (bufptr) { *bufptr = buf; } -return true; +return buf !=NULL ; }static void write_dump_bitmap (DumpState *s,Error **errp)
@@ -1297,6 +1328,7 @@ static void write_dump_pages(DumpState *s, Error **errp)uint8_t *buf;GuestPhysBlock *block_iter =NULL ;uint64_t pfn_iter; + g_autofree uint8_t *page = NULL;/* get offset of page_desc and page_data in dump file */ offset_desc = s->offset_page;
@@ -1332,12 +1364,13 @@ static void write_dump_pages(DumpState *s, Error **errp) } offset_data += s->dump_info.page_size; + page =g_malloc (s->dump_info.page_size);/* * dump memory to vmcore page by page. zero page will all be resided in the * first page of page section */ -while (get_next_page (&block_iter, &pfn_iter, &buf, s)) { +for (buf = page;get_next_page (&block_iter, &pfn_iter, &buf, s); buf = page) {/* check zero page */ if (buffer_is_zero (buf, s->dump_info.page_size)) { ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
Signature · 1 line
2.37.3
Stefan Hajnoczi
AppliedApplied, thanks.
Please update the changelog at https://wiki.qemu.org/ChangeLog/7.2 for any user-visible changes.