14
#include "qemu/osdep.h"
15
#include "qemu/cutils.h"
17
#include "qemu/bswap.h"
18
#include "exec/target_page.h"
19
#include "monitor/monitor.h"
20
#include "sysemu/dump.h"
21
#include "sysemu/runstate.h"
22
#include "sysemu/cpus.h"
23
#include "qapi/error.h"
24
#include "qapi/qapi-commands-dump.h"
25
#include "qapi/qapi-events-dump.h"
26
#include "qapi/qmp/qerror.h"
27
#include "qemu/error-report.h"
28
#include "qemu/main-loop.h"
29
#include "hw/misc/vmcoreinfo.h"
30
#include "migration/blocker.h"
31
#include "hw/core/cpu.h"
33
#include "qemu/range.h"
42
#ifndef ELF_MACHINE_UNAME
43
#define ELF_MACHINE_UNAME "Unknown"
46
#define MAX_GUEST_NOTE_SIZE (1 << 20)
48
static Error *dump_migration_blocker;
50
#define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
51
((DIV_ROUND_UP((hdr_size), 4) + \
52
DIV_ROUND_UP((name_size), 4) + \
53
DIV_ROUND_UP((desc_size), 4)) * 4)
55
static inline bool dump_is_64bit(DumpState *s)
57
return s->dump_info.d_class == ELFCLASS64;
60
static inline bool dump_has_filter(DumpState *s)
62
return s->filter_area_length > 0;
65
uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
67
if (s->dump_info.d_endian == ELFDATA2LSB) {
68
val = cpu_to_le16(val);
70
val = cpu_to_be16(val);
76
uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
78
if (s->dump_info.d_endian == ELFDATA2LSB) {
79
val = cpu_to_le32(val);
81
val = cpu_to_be32(val);
87
uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
89
if (s->dump_info.d_endian == ELFDATA2LSB) {
90
val = cpu_to_le64(val);
92
val = cpu_to_be64(val);
98
static int dump_cleanup(DumpState *s)
100
if (s->dump_info.arch_cleanup_fn) {
101
s->dump_info.arch_cleanup_fn(s);
104
guest_phys_blocks_free(&s->guest_phys_blocks);
105
memory_mapping_list_free(&s->list);
107
g_free(s->guest_note);
108
g_clear_pointer(&s->string_table_buf, g_array_unref);
109
s->guest_note = NULL;
119
migrate_del_blocker(&dump_migration_blocker);
124
static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
126
DumpState *s = opaque;
129
written_size = qemu_write_full(s->fd, buf, size);
130
if (written_size != size) {
137
static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header)
144
uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
146
memset(elf_header, 0, sizeof(Elf64_Ehdr));
147
memcpy(elf_header, ELFMAG, SELFMAG);
148
elf_header->e_ident[EI_CLASS] = ELFCLASS64;
149
elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
150
elf_header->e_ident[EI_VERSION] = EV_CURRENT;
151
elf_header->e_type = cpu_to_dump16(s, ET_CORE);
152
elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
153
elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
154
elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
155
elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset);
156
elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
157
elf_header->e_phnum = cpu_to_dump16(s, phnum);
158
elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset);
159
elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
160
elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
161
elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
164
static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
171
uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
173
memset(elf_header, 0, sizeof(Elf32_Ehdr));
174
memcpy(elf_header, ELFMAG, SELFMAG);
175
elf_header->e_ident[EI_CLASS] = ELFCLASS32;
176
elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
177
elf_header->e_ident[EI_VERSION] = EV_CURRENT;
178
elf_header->e_type = cpu_to_dump16(s, ET_CORE);
179
elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
180
elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
181
elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
182
elf_header->e_phoff = cpu_to_dump32(s, s->phdr_offset);
183
elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
184
elf_header->e_phnum = cpu_to_dump16(s, phnum);
185
elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
186
elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
187
elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
188
elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
191
static void write_elf_header(DumpState *s, Error **errp)
193
Elf32_Ehdr elf32_header;
194
Elf64_Ehdr elf64_header;
200
assert(s->shdr_num >= 2);
201
if (dump_is_64bit(s)) {
202
prepare_elf64_header(s, &elf64_header);
203
header_size = sizeof(elf64_header);
204
header_ptr = &elf64_header;
206
prepare_elf32_header(s, &elf32_header);
207
header_size = sizeof(elf32_header);
208
header_ptr = &elf32_header;
211
ret = fd_write_vmcore(header_ptr, header_size, s);
213
error_setg_errno(errp, -ret, "dump: failed to write elf header");
217
static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
218
int phdr_index, hwaddr offset,
219
hwaddr filesz, Error **errp)
224
memset(&phdr, 0, sizeof(Elf64_Phdr));
225
phdr.p_type = cpu_to_dump32(s, PT_LOAD);
226
phdr.p_offset = cpu_to_dump64(s, offset);
227
phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
228
phdr.p_filesz = cpu_to_dump64(s, filesz);
229
phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
230
phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
232
assert(memory_mapping->length >= filesz);
234
ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
236
error_setg_errno(errp, -ret,
237
"dump: failed to write program header table");
241
static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
242
int phdr_index, hwaddr offset,
243
hwaddr filesz, Error **errp)
248
memset(&phdr, 0, sizeof(Elf32_Phdr));
249
phdr.p_type = cpu_to_dump32(s, PT_LOAD);
250
phdr.p_offset = cpu_to_dump32(s, offset);
251
phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
252
phdr.p_filesz = cpu_to_dump32(s, filesz);
253
phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
255
cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
257
assert(memory_mapping->length >= filesz);
259
ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
261
error_setg_errno(errp, -ret,
262
"dump: failed to write program header table");
266
static void prepare_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
268
memset(phdr, 0, sizeof(*phdr));
269
phdr->p_type = cpu_to_dump32(s, PT_NOTE);
270
phdr->p_offset = cpu_to_dump64(s, s->note_offset);
272
phdr->p_filesz = cpu_to_dump64(s, s->note_size);
273
phdr->p_memsz = cpu_to_dump64(s, s->note_size);
277
static inline int cpu_index(CPUState *cpu)
279
return cpu->cpu_index + 1;
282
static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
288
ret = f(s->guest_note, s->guest_note_size, s);
290
error_setg(errp, "dump: failed to write guest note");
295
static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
304
ret = cpu_write_elf64_note(f, cpu, id, s);
306
error_setg(errp, "dump: failed to write elf notes");
312
ret = cpu_write_elf64_qemunote(f, cpu, s);
314
error_setg(errp, "dump: failed to write CPU status");
319
write_guest_note(f, s, errp);
322
static void prepare_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
324
memset(phdr, 0, sizeof(*phdr));
325
phdr->p_type = cpu_to_dump32(s, PT_NOTE);
326
phdr->p_offset = cpu_to_dump32(s, s->note_offset);
328
phdr->p_filesz = cpu_to_dump32(s, s->note_size);
329
phdr->p_memsz = cpu_to_dump32(s, s->note_size);
333
static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
342
ret = cpu_write_elf32_note(f, cpu, id, s);
344
error_setg(errp, "dump: failed to write elf notes");
350
ret = cpu_write_elf32_qemunote(f, cpu, s);
352
error_setg(errp, "dump: failed to write CPU status");
357
write_guest_note(f, s, errp);
360
static void write_elf_phdr_note(DumpState *s, Error **errp)
368
if (dump_is_64bit(s)) {
369
prepare_elf64_phdr_note(s, &phdr64);
370
size = sizeof(phdr64);
373
prepare_elf32_phdr_note(s, &phdr32);
374
size = sizeof(phdr32);
378
ret = fd_write_vmcore(phdr, size, s);
380
error_setg_errno(errp, -ret,
381
"dump: failed to write program header table");
385
static void prepare_elf_section_hdr_zero(DumpState *s)
387
if (dump_is_64bit(s)) {
388
Elf64_Shdr *shdr64 = s->elf_section_hdrs;
390
shdr64->sh_info = cpu_to_dump32(s, s->phdr_num);
392
Elf32_Shdr *shdr32 = s->elf_section_hdrs;
394
shdr32->sh_info = cpu_to_dump32(s, s->phdr_num);
398
static void prepare_elf_section_hdr_string(DumpState *s, void *buff)
400
uint64_t index = s->string_table_buf->len;
401
const char strtab[] = ".shstrtab";
402
Elf32_Shdr shdr32 = {};
403
Elf64_Shdr shdr64 = {};
407
g_array_append_vals(s->string_table_buf, strtab, sizeof(strtab));
408
if (dump_is_64bit(s)) {
409
shdr_size = sizeof(Elf64_Shdr);
410
shdr64.sh_type = SHT_STRTAB;
411
shdr64.sh_offset = s->section_offset + s->elf_section_data_size;
412
shdr64.sh_name = index;
413
shdr64.sh_size = s->string_table_buf->len;
416
shdr_size = sizeof(Elf32_Shdr);
417
shdr32.sh_type = SHT_STRTAB;
418
shdr32.sh_offset = s->section_offset + s->elf_section_data_size;
419
shdr32.sh_name = index;
420
shdr32.sh_size = s->string_table_buf->len;
423
memcpy(buff, shdr, shdr_size);
426
static bool prepare_elf_section_hdrs(DumpState *s, Error **errp)
428
size_t len, sizeof_shdr;
437
sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
438
len = sizeof_shdr * s->shdr_num;
439
s->elf_section_hdrs = g_malloc0(len);
440
buff_hdr = s->elf_section_hdrs;
453
if (s->phdr_num >= PN_XNUM) {
454
prepare_elf_section_hdr_zero(s);
456
buff_hdr += sizeof_shdr;
459
if (s->dump_info.arch_sections_write_hdr_fn
460
&& s->shdr_num > 2) {
461
buff_hdr += s->dump_info.arch_sections_write_hdr_fn(s, buff_hdr);
463
if (s->shdr_num >= SHN_LORESERVE) {
464
error_setg_errno(errp, EINVAL,
465
"dump: too many architecture defined sections");
474
prepare_elf_section_hdr_string(s, buff_hdr);
478
static void write_elf_section_headers(DumpState *s, Error **errp)
480
size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
483
if (!prepare_elf_section_hdrs(s, errp)) {
487
ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s);
489
error_setg_errno(errp, -ret, "dump: failed to write section headers");
492
g_free(s->elf_section_hdrs);
495
static void write_elf_sections(DumpState *s, Error **errp)
499
if (s->elf_section_data_size) {
501
ret = fd_write_vmcore(s->elf_section_data,
502
s->elf_section_data_size, s);
504
error_setg_errno(errp, -ret,
505
"dump: failed to write architecture section data");
511
ret = fd_write_vmcore(s->string_table_buf->data,
512
s->string_table_buf->len, s);
514
error_setg_errno(errp, -ret, "dump: failed to write string table data");
518
static void write_data(DumpState *s, void *buf, int length, Error **errp)
522
ret = fd_write_vmcore(buf, length, s);
524
error_setg_errno(errp, -ret, "dump: failed to save memory");
526
s->written_size += length;
531
static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
532
int64_t size, Error **errp)
537
for (i = 0; i < size / s->dump_info.page_size; i++) {
538
write_data(s, block->host_addr + start + i * s->dump_info.page_size,
539
s->dump_info.page_size, errp);
545
if ((size % s->dump_info.page_size) != 0) {
546
write_data(s, block->host_addr + start + i * s->dump_info.page_size,
547
size % s->dump_info.page_size, errp);
555
static void get_offset_range(hwaddr phys_addr,
556
ram_addr_t mapping_length,
561
GuestPhysBlock *block;
562
hwaddr offset = s->memory_offset;
563
int64_t size_in_block, start;
569
if (dump_has_filter(s)) {
570
if (phys_addr < s->filter_area_begin ||
571
phys_addr >= s->filter_area_begin + s->filter_area_length) {
576
QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
577
if (dump_has_filter(s)) {
578
if (!ranges_overlap(block->target_start,
579
block->target_end - block->target_start,
580
s->filter_area_begin,
581
s->filter_area_length)) {
586
if (s->filter_area_begin <= block->target_start) {
587
start = block->target_start;
589
start = s->filter_area_begin;
592
size_in_block = block->target_end - start;
593
if (s->filter_area_begin + s->filter_area_length < block->target_end) {
594
size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
597
start = block->target_start;
598
size_in_block = block->target_end - block->target_start;
601
if (phys_addr >= start && phys_addr < start + size_in_block) {
602
*p_offset = phys_addr - start + offset;
609
*p_filesz = phys_addr + mapping_length <= start + size_in_block ?
611
size_in_block - (phys_addr - start);
615
offset += size_in_block;
619
static void write_elf_phdr_loads(DumpState *s, Error **errp)
622
hwaddr offset, filesz;
623
MemoryMapping *memory_mapping;
624
uint32_t phdr_index = 1;
626
QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
627
get_offset_range(memory_mapping->phys_addr,
628
memory_mapping->length,
629
s, &offset, &filesz);
630
if (dump_is_64bit(s)) {
631
write_elf64_load(s, memory_mapping, phdr_index++, offset,
634
write_elf32_load(s, memory_mapping, phdr_index++, offset,
642
if (phdr_index >= s->phdr_num) {
648
static void write_elf_notes(DumpState *s, Error **errp)
650
if (dump_is_64bit(s)) {
651
write_elf64_notes(fd_write_vmcore, s, errp);
653
write_elf32_notes(fd_write_vmcore, s, errp);
658
static void dump_begin(DumpState *s, Error **errp)
687
write_elf_header(s, errp);
693
write_elf_section_headers(s, errp);
699
write_elf_phdr_note(s, errp);
705
write_elf_phdr_loads(s, errp);
711
write_elf_notes(s, errp);
714
int64_t dump_filtered_memblock_size(GuestPhysBlock *block,
715
int64_t filter_area_start,
716
int64_t filter_area_length)
718
int64_t size, left, right;
721
if (!filter_area_length) {
722
return block->target_end - block->target_start;
726
left = MAX(filter_area_start, block->target_start);
727
right = MIN(filter_area_start + filter_area_length, block->target_end);
729
size = size > 0 ? size : 0;
734
int64_t dump_filtered_memblock_start(GuestPhysBlock *block,
735
int64_t filter_area_start,
736
int64_t filter_area_length)
738
if (filter_area_length) {
740
if (!ranges_overlap(block->target_start,
741
block->target_end - block->target_start,
742
filter_area_start, filter_area_length)) {
746
if (filter_area_start > block->target_start) {
747
return filter_area_start - block->target_start;
755
static void dump_iterate(DumpState *s, Error **errp)
758
GuestPhysBlock *block;
759
int64_t memblock_size, memblock_start;
761
QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
762
memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
763
if (memblock_start == -1) {
767
memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
770
write_memory(s, block, memblock_start, memblock_size, errp);
777
static void dump_end(DumpState *s, Error **errp)
781
if (s->elf_section_data_size) {
782
s->elf_section_data = g_malloc0(s->elf_section_data_size);
786
if (s->dump_info.arch_sections_write_fn &&
787
s->elf_section_data_size) {
788
rc = s->dump_info.arch_sections_write_fn(s, s->elf_section_data);
790
error_setg_errno(errp, rc,
791
"dump: failed to get arch section data");
792
g_free(s->elf_section_data);
798
write_elf_sections(s, errp);
801
static void create_vmcore(DumpState *s, Error **errp)
811
dump_iterate(s, errp);
820
static int write_start_flat_header(DumpState *s)
822
MakedumpfileHeader *mh;
829
QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
830
mh = g_malloc0(MAX_SIZE_MDF_HEADER);
832
memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
833
MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
835
mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
836
mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
839
written_size = qemu_write_full(s->fd, mh, MAX_SIZE_MDF_HEADER);
840
if (written_size != MAX_SIZE_MDF_HEADER) {
848
static int write_end_flat_header(DumpState *s)
850
MakedumpfileDataHeader mdh;
856
mdh.offset = END_FLAG_FLAT_HEADER;
857
mdh.buf_size = END_FLAG_FLAT_HEADER;
860
written_size = qemu_write_full(s->fd, &mdh, sizeof(mdh));
861
if (written_size != sizeof(mdh)) {
868
static int write_buffer(DumpState *s, off_t offset, const void *buf, size_t size)
871
MakedumpfileDataHeader mdh;
875
seek_loc = lseek(s->fd, offset, SEEK_SET);
876
if (seek_loc == (off_t) -1) {
880
mdh.offset = cpu_to_be64(offset);
881
mdh.buf_size = cpu_to_be64(size);
883
written_size = qemu_write_full(s->fd, &mdh, sizeof(mdh));
884
if (written_size != sizeof(mdh)) {
889
written_size = qemu_write_full(s->fd, buf, size);
890
if (written_size != size) {
897
static int buf_write_note(const void *buf, size_t size, void *opaque)
899
DumpState *s = opaque;
902
if (s->note_buf_offset + size > s->note_size) {
906
memcpy(s->note_buf + s->note_buf_offset, buf, size);
908
s->note_buf_offset += size;
919
static void get_note_sizes(DumpState *s, const void *note,
920
uint64_t *note_head_size,
924
uint64_t note_head_sz;
928
if (dump_is_64bit(s)) {
929
const Elf64_Nhdr *hdr = note;
930
note_head_sz = sizeof(Elf64_Nhdr);
931
name_sz = cpu_to_dump64(s, hdr->n_namesz);
932
desc_sz = cpu_to_dump64(s, hdr->n_descsz);
934
const Elf32_Nhdr *hdr = note;
935
note_head_sz = sizeof(Elf32_Nhdr);
936
name_sz = cpu_to_dump32(s, hdr->n_namesz);
937
desc_sz = cpu_to_dump32(s, hdr->n_descsz);
940
if (note_head_size) {
941
*note_head_size = note_head_sz;
944
*name_size = name_sz;
947
*desc_size = desc_sz;
951
static bool note_name_equal(DumpState *s,
952
const uint8_t *note, const char *name)
954
int len = strlen(name) + 1;
955
uint64_t head_size, name_size;
957
get_note_sizes(s, note, &head_size, &name_size, NULL);
958
head_size = ROUND_UP(head_size, 4);
960
return name_size == len && memcmp(note + head_size, name, len) == 0;
964
static void create_header32(DumpState *s, Error **errp)
967
DiskDumpHeader32 *dh = NULL;
968
KdumpSubHeader32 *kh = NULL;
971
uint32_t sub_hdr_size;
972
uint32_t bitmap_blocks;
974
uint64_t offset_note;
977
size = sizeof(DiskDumpHeader32);
978
dh = g_malloc0(size);
980
memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
981
dh->header_version = cpu_to_dump32(s, 6);
982
block_size = s->dump_info.page_size;
983
dh->block_size = cpu_to_dump32(s, block_size);
984
sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
985
sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
986
dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
988
dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
989
dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
990
bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
991
dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
992
strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
994
if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
995
status |= DUMP_DH_COMPRESSED_ZLIB;
998
if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
999
status |= DUMP_DH_COMPRESSED_LZO;
1003
if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
1004
status |= DUMP_DH_COMPRESSED_SNAPPY;
1007
dh->status = cpu_to_dump32(s, status);
1009
if (write_buffer(s, 0, dh, size) < 0) {
1010
error_setg(errp, "dump: failed to write disk dump header");
1015
size = sizeof(KdumpSubHeader32);
1016
kh = g_malloc0(size);
1019
kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
1020
kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
1021
kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
1023
offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
1024
if (s->guest_note &&
1025
note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1026
uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1028
get_note_sizes(s, s->guest_note,
1029
&hsize, &name_size, &size_vmcoreinfo_desc);
1030
offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1031
(DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1032
kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1033
kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
1036
kh->offset_note = cpu_to_dump64(s, offset_note);
1037
kh->note_size = cpu_to_dump32(s, s->note_size);
1039
if (write_buffer(s, DISKDUMP_HEADER_BLOCKS *
1040
block_size, kh, size) < 0) {
1041
error_setg(errp, "dump: failed to write kdump sub header");
1046
s->note_buf = g_malloc0(s->note_size);
1047
s->note_buf_offset = 0;
1050
write_elf32_notes(buf_write_note, s, errp);
1054
if (write_buffer(s, offset_note, s->note_buf,
1055
s->note_size) < 0) {
1056
error_setg(errp, "dump: failed to write notes");
1061
s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1065
s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1071
g_free(s->note_buf);
1075
static void create_header64(DumpState *s, Error **errp)
1078
DiskDumpHeader64 *dh = NULL;
1079
KdumpSubHeader64 *kh = NULL;
1081
uint32_t block_size;
1082
uint32_t sub_hdr_size;
1083
uint32_t bitmap_blocks;
1084
uint32_t status = 0;
1085
uint64_t offset_note;
1088
size = sizeof(DiskDumpHeader64);
1089
dh = g_malloc0(size);
1091
memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
1092
dh->header_version = cpu_to_dump32(s, 6);
1093
block_size = s->dump_info.page_size;
1094
dh->block_size = cpu_to_dump32(s, block_size);
1095
sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
1096
sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
1097
dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
1099
dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
1100
dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
1101
bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
1102
dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
1103
strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
1105
if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
1106
status |= DUMP_DH_COMPRESSED_ZLIB;
1109
if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
1110
status |= DUMP_DH_COMPRESSED_LZO;
1114
if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
1115
status |= DUMP_DH_COMPRESSED_SNAPPY;
1118
dh->status = cpu_to_dump32(s, status);
1120
if (write_buffer(s, 0, dh, size) < 0) {
1121
error_setg(errp, "dump: failed to write disk dump header");
1126
size = sizeof(KdumpSubHeader64);
1127
kh = g_malloc0(size);
1130
kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
1131
kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
1132
kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
1134
offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
1135
if (s->guest_note &&
1136
note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1137
uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1139
get_note_sizes(s, s->guest_note,
1140
&hsize, &name_size, &size_vmcoreinfo_desc);
1141
offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1142
(DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1143
kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1144
kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
1147
kh->offset_note = cpu_to_dump64(s, offset_note);
1148
kh->note_size = cpu_to_dump64(s, s->note_size);
1150
if (write_buffer(s, DISKDUMP_HEADER_BLOCKS *
1151
block_size, kh, size) < 0) {
1152
error_setg(errp, "dump: failed to write kdump sub header");
1157
s->note_buf = g_malloc0(s->note_size);
1158
s->note_buf_offset = 0;
1161
write_elf64_notes(buf_write_note, s, errp);
1166
if (write_buffer(s, offset_note, s->note_buf,
1167
s->note_size) < 0) {
1168
error_setg(errp, "dump: failed to write notes");
1173
s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1177
s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1183
g_free(s->note_buf);
1186
static void write_dump_header(DumpState *s, Error **errp)
1188
if (dump_is_64bit(s)) {
1189
create_header64(s, errp);
1191
create_header32(s, errp);
1195
static size_t dump_bitmap_get_bufsize(DumpState *s)
1197
return s->dump_info.page_size;
1207
static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1208
uint8_t *buf, DumpState *s)
1210
off_t old_offset, new_offset;
1211
off_t offset_bitmap1, offset_bitmap2;
1213
size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1214
size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1217
assert(last_pfn <= pfn);
1225
old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1226
new_offset = bitmap_bufsize * (pfn / bits_per_buf);
1228
while (old_offset < new_offset) {
1230
offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1231
if (write_buffer(s, offset_bitmap1, buf,
1232
bitmap_bufsize) < 0) {
1237
offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1239
if (write_buffer(s, offset_bitmap2, buf,
1240
bitmap_bufsize) < 0) {
1244
memset(buf, 0, bitmap_bufsize);
1245
old_offset += bitmap_bufsize;
1249
byte = (pfn % bits_per_buf) / CHAR_BIT;
1250
bit = (pfn % bits_per_buf) % CHAR_BIT;
1252
buf[byte] |= 1u << bit;
1254
buf[byte] &= ~(1u << bit);
1260
static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1262
int target_page_shift = ctz32(s->dump_info.page_size);
1264
return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1267
static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1269
int target_page_shift = ctz32(s->dump_info.page_size);
1271
return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1279
static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1280
uint8_t **bufptr, DumpState *s)
1282
GuestPhysBlock *block = *blockptr;
1283
uint32_t page_size = s->dump_info.page_size;
1284
uint8_t *buf = NULL, *hbuf;
1289
block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1291
addr = block->target_start;
1292
*pfnptr = dump_paddr_to_pfn(s, addr);
1295
addr = dump_pfn_to_paddr(s, *pfnptr);
1297
assert(block != NULL);
1300
if (addr >= block->target_start && addr < block->target_end) {
1301
size_t n = MIN(block->target_end - addr, page_size - addr % page_size);
1302
hbuf = block->host_addr + (addr - block->target_start);
1304
if (n == page_size) {
1306
assert(addr % page_size == 0);
1309
} else if (bufptr) {
1312
memset(buf, 0, page_size);
1318
memcpy(buf + addr % page_size, hbuf, n);
1320
if (addr % page_size == 0 || addr >= block->target_end) {
1326
*blockptr = block = QTAILQ_NEXT(block, next);
1331
addr = block->target_start;
1333
if (dump_paddr_to_pfn(s, addr) != *pfnptr) {
1339
*pfnptr = dump_paddr_to_pfn(s, addr);
1352
static void write_dump_bitmap(DumpState *s, Error **errp)
1355
uint64_t last_pfn, pfn;
1356
void *dump_bitmap_buf;
1357
size_t num_dumpable;
1358
GuestPhysBlock *block_iter = NULL;
1359
size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1360
size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1363
dump_bitmap_buf = g_malloc0(bitmap_bufsize);
1372
while (get_next_page(&block_iter, &pfn, NULL, s)) {
1373
ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1375
error_setg(errp, "dump: failed to set dump_bitmap");
1388
if (num_dumpable > 0) {
1389
ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
1390
dump_bitmap_buf, s);
1392
error_setg(errp, "dump: failed to sync dump_bitmap");
1398
s->num_dumpable = num_dumpable;
1401
g_free(dump_bitmap_buf);
1404
static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1407
data_cache->state = s;
1408
data_cache->data_size = 0;
1409
data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1410
data_cache->buf = g_malloc0(data_cache->buf_size);
1411
data_cache->offset = offset;
1414
static int write_cache(DataCache *dc, const void *buf, size_t size,
1421
assert(size <= dc->buf_size);
1428
if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1429
(flag_sync && dc->data_size > 0)) {
1430
if (write_buffer(dc->state, dc->offset, dc->buf, dc->data_size) < 0) {
1434
dc->offset += dc->data_size;
1439
memcpy(dc->buf + dc->data_size, buf, size);
1440
dc->data_size += size;
1446
static void free_data_cache(DataCache *data_cache)
1448
g_free(data_cache->buf);
1451
static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1453
switch (flag_compress) {
1454
case DUMP_DH_COMPRESSED_ZLIB:
1455
return compressBound(page_size);
1457
case DUMP_DH_COMPRESSED_LZO:
1463
return page_size + page_size / 16 + 64 + 3;
1466
case DUMP_DH_COMPRESSED_SNAPPY:
1467
return snappy_max_compressed_length(page_size);
1473
static void write_dump_pages(DumpState *s, Error **errp)
1476
DataCache page_desc, page_data;
1477
size_t len_buf_out, size_out;
1479
lzo_bytep wrkmem = NULL;
1481
uint8_t *buf_out = NULL;
1482
off_t offset_desc, offset_data;
1483
PageDescriptor pd, pd_zero;
1485
GuestPhysBlock *block_iter = NULL;
1487
g_autofree uint8_t *page = NULL;
1490
offset_desc = s->offset_page;
1491
offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1493
prepare_data_cache(&page_desc, s, offset_desc);
1494
prepare_data_cache(&page_data, s, offset_data);
1497
len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
1498
assert(len_buf_out != 0);
1501
wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1504
buf_out = g_malloc(len_buf_out);
1510
pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
1511
pd_zero.flags = cpu_to_dump32(s, 0);
1512
pd_zero.offset = cpu_to_dump64(s, offset_data);
1513
pd_zero.page_flags = cpu_to_dump64(s, 0);
1514
buf = g_malloc0(s->dump_info.page_size);
1515
ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
1518
error_setg(errp, "dump: failed to write page data (zero page)");
1522
offset_data += s->dump_info.page_size;
1523
page = g_malloc(s->dump_info.page_size);
1529
for (buf = page; get_next_page(&block_iter, &pfn_iter, &buf, s); buf = page) {
1531
if (buffer_is_zero(buf, s->dump_info.page_size)) {
1532
ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1535
error_setg(errp, "dump: failed to write page desc");
1550
size_out = len_buf_out;
1551
if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
1552
(compress2(buf_out, (uLongf *)&size_out, buf,
1553
s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1554
(size_out < s->dump_info.page_size)) {
1555
pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1556
pd.size = cpu_to_dump32(s, size_out);
1558
ret = write_cache(&page_data, buf_out, size_out, false);
1560
error_setg(errp, "dump: failed to write page data");
1564
} else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
1565
(lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
1566
(lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
1567
(size_out < s->dump_info.page_size)) {
1568
pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1569
pd.size = cpu_to_dump32(s, size_out);
1571
ret = write_cache(&page_data, buf_out, size_out, false);
1573
error_setg(errp, "dump: failed to write page data");
1578
} else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
1579
(snappy_compress((char *)buf, s->dump_info.page_size,
1580
(char *)buf_out, &size_out) == SNAPPY_OK) &&
1581
(size_out < s->dump_info.page_size)) {
1582
pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1583
pd.size = cpu_to_dump32(s, size_out);
1585
ret = write_cache(&page_data, buf_out, size_out, false);
1587
error_setg(errp, "dump: failed to write page data");
1596
pd.flags = cpu_to_dump32(s, 0);
1597
size_out = s->dump_info.page_size;
1598
pd.size = cpu_to_dump32(s, size_out);
1600
ret = write_cache(&page_data, buf,
1601
s->dump_info.page_size, false);
1603
error_setg(errp, "dump: failed to write page data");
1609
pd.page_flags = cpu_to_dump64(s, 0);
1610
pd.offset = cpu_to_dump64(s, offset_data);
1611
offset_data += size_out;
1613
ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1615
error_setg(errp, "dump: failed to write page desc");
1619
s->written_size += s->dump_info.page_size;
1622
ret = write_cache(&page_desc, NULL, 0, true);
1624
error_setg(errp, "dump: failed to sync cache for page_desc");
1627
ret = write_cache(&page_data, NULL, 0, true);
1629
error_setg(errp, "dump: failed to sync cache for page_data");
1634
free_data_cache(&page_desc);
1635
free_data_cache(&page_data);
1644
static void create_kdump_vmcore(DumpState *s, Error **errp)
1671
ret = write_start_flat_header(s);
1673
error_setg(errp, "dump: failed to write start flat header");
1677
write_dump_header(s, errp);
1682
write_dump_bitmap(s, errp);
1687
write_dump_pages(s, errp);
1692
ret = write_end_flat_header(s);
1694
error_setg(errp, "dump: failed to write end flat header");
1699
static void get_max_mapnr(DumpState *s)
1701
GuestPhysBlock *last_block;
1703
last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
1704
s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
1707
static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1709
static void dump_state_prepare(DumpState *s)
1712
*s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1715
bool qemu_system_dump_in_progress(void)
1717
DumpState *state = &dump_state_global;
1718
return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
1725
static int64_t dump_calculate_size(DumpState *s)
1727
GuestPhysBlock *block;
1730
QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1731
total += dump_filtered_memblock_size(block,
1732
s->filter_area_begin,
1733
s->filter_area_length);
1739
static void vmcoreinfo_update_phys_base(DumpState *s)
1741
uint64_t size, note_head_size, name_size, phys_base;
1746
if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1750
get_note_sizes(s, s->guest_note, ¬e_head_size, &name_size, &size);
1751
note_head_size = ROUND_UP(note_head_size, 4);
1753
vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1754
*(vmci + size) = '\0';
1756
lines = g_strsplit((char *)vmci, "\n", -1);
1757
for (i = 0; lines[i]; i++) {
1758
const char *prefix = NULL;
1760
if (s->dump_info.d_machine == EM_X86_64) {
1761
prefix = "NUMBER(phys_base)=";
1762
} else if (s->dump_info.d_machine == EM_AARCH64) {
1763
prefix = "NUMBER(PHYS_OFFSET)=";
1766
if (prefix && g_str_has_prefix(lines[i], prefix)) {
1767
if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
1769
warn_report("Failed to read %s", prefix);
1771
s->dump_info.phys_base = phys_base;
1780
static void dump_init(DumpState *s, int fd, bool has_format,
1781
DumpGuestMemoryFormat format, bool paging, bool has_filter,
1782
int64_t begin, int64_t length, bool kdump_raw,
1786
VMCoreInfoState *vmci = vmcoreinfo_find();
1791
s->has_format = has_format;
1793
s->written_size = 0;
1794
s->kdump_raw = kdump_raw;
1797
if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1798
assert(!paging && !has_filter);
1801
if (runstate_is_running()) {
1802
vm_stop(RUN_STATE_SAVE_VM);
1811
cpu_synchronize_all_states();
1818
if (has_filter && !length) {
1819
error_setg(errp, "parameter 'length' expects a non-zero size");
1822
s->filter_area_begin = begin;
1823
s->filter_area_length = length;
1826
s->string_table_buf = g_array_new(FALSE, TRUE, 1);
1831
g_array_set_size(s->string_table_buf, 1);
1833
memory_mapping_list_init(&s->list);
1835
guest_phys_blocks_init(&s->guest_phys_blocks);
1836
guest_phys_blocks_append(&s->guest_phys_blocks);
1837
s->total_size = dump_calculate_size(s);
1838
#ifdef DEBUG_DUMP_GUEST_MEMORY
1839
fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1843
if (!s->total_size) {
1844
error_setg(errp, "dump: no guest memory to dump");
1852
ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
1855
"dumping guest memory is not supported on this target");
1859
if (!s->dump_info.page_size) {
1860
s->dump_info.page_size = qemu_target_page_size();
1863
s->note_size = cpu_get_note_size(s->dump_info.d_class,
1864
s->dump_info.d_machine, nr_cpus);
1865
assert(s->note_size >= 0);
1873
uint64_t addr, note_head_size, name_size, desc_size;
1875
uint16_t guest_format;
1877
note_head_size = dump_is_64bit(s) ?
1878
sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
1880
guest_format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1881
size = le32_to_cpu(vmci->vmcoreinfo.size);
1882
addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1883
if (!vmci->has_vmcoreinfo) {
1884
warn_report("guest note is not present");
1885
} else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1886
warn_report("guest note size is invalid: %" PRIu32, size);
1887
} else if (guest_format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
1888
warn_report("guest note format is unsupported: %" PRIu16, guest_format);
1890
s->guest_note = g_malloc(size + 1);
1891
cpu_physical_memory_read(addr, s->guest_note, size);
1893
get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1894
s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1896
if (name_size > MAX_GUEST_NOTE_SIZE ||
1897
desc_size > MAX_GUEST_NOTE_SIZE ||
1898
s->guest_note_size > size) {
1899
warn_report("Invalid guest note header");
1900
g_free(s->guest_note);
1901
s->guest_note = NULL;
1903
vmcoreinfo_update_phys_base(s);
1904
s->note_size += s->guest_note_size;
1911
qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1916
qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
1919
s->nr_cpus = nr_cpus;
1924
tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1925
s->dump_info.page_size);
1926
s->len_dump_bitmap = tmp * s->dump_info.page_size;
1929
if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1931
case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1932
s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1935
case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
1937
if (lzo_init() != LZO_E_OK) {
1938
error_setg(errp, "failed to initialize the LZO library");
1942
s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1945
case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1946
s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1950
s->flag_compress = 0;
1956
if (dump_has_filter(s)) {
1957
memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
1972
if (s->dump_info.arch_sections_add_fn) {
1973
s->dump_info.arch_sections_add_fn(s);
1987
if (s->list.num <= UINT32_MAX - 1) {
1988
s->phdr_num += s->list.num;
1990
s->phdr_num = UINT32_MAX;
1997
if (dump_is_64bit(s)) {
1998
s->shdr_offset = sizeof(Elf64_Ehdr);
1999
s->phdr_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
2000
s->note_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
2002
s->shdr_offset = sizeof(Elf32_Ehdr);
2003
s->phdr_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
2004
s->note_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
2006
s->memory_offset = s->note_offset + s->note_size;
2007
s->section_offset = s->memory_offset + s->total_size;
2016
static void dump_process(DumpState *s, Error **errp)
2019
DumpQueryResult *result = NULL;
2021
if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
2022
create_win_dump(s, errp);
2023
} else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
2024
create_kdump_vmcore(s, errp);
2026
create_vmcore(s, errp);
2031
qatomic_set(&s->status,
2032
(*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
2035
result = qmp_query_dump(NULL);
2038
qapi_event_send_dump_completed(result,
2039
*errp ? error_get_pretty(*errp) : NULL);
2040
qapi_free_DumpQueryResult(result);
2045
static void *dump_thread(void *data)
2047
DumpState *s = (DumpState *)data;
2048
dump_process(s, NULL);
2052
DumpQueryResult *qmp_query_dump(Error **errp)
2054
DumpQueryResult *result = g_new(DumpQueryResult, 1);
2055
DumpState *state = &dump_state_global;
2056
result->status = qatomic_read(&state->status);
2059
result->completed = state->written_size;
2060
result->total = state->total_size;
2064
void qmp_dump_guest_memory(bool paging, const char *protocol,
2065
bool has_detach, bool detach,
2066
bool has_begin, int64_t begin,
2067
bool has_length, int64_t length,
2068
bool has_format, DumpGuestMemoryFormat format,
2075
bool detach_p = false;
2076
bool kdump_raw = false;
2078
if (runstate_check(RUN_STATE_INMIGRATE)) {
2079
error_setg(errp, "Dump not allowed during incoming migration.");
2085
if (qemu_system_dump_in_progress()) {
2086
error_setg(errp, "There is a dump in process, please wait.");
2096
case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_ZLIB:
2097
format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2100
case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_LZO:
2101
format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2104
case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_SNAPPY:
2105
format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2117
if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
2118
(paging || has_begin || has_length)) {
2119
error_setg(errp, "kdump-compressed format doesn't support paging or "
2123
if (has_begin && !has_length) {
2124
error_setg(errp, QERR_MISSING_PARAMETER, "length");
2127
if (!has_begin && has_length) {
2128
error_setg(errp, QERR_MISSING_PARAMETER, "begin");
2137
if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
2138
error_setg(errp, "kdump-lzo is not available now");
2143
#ifndef CONFIG_SNAPPY
2144
if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
2145
error_setg(errp, "kdump-snappy is not available now");
2150
if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
2151
&& !win_dump_available(errp)) {
2155
if (strstart(protocol, "fd:", &p)) {
2156
fd = monitor_get_fd(monitor_cur(), p, errp);
2160
} else if (strstart(protocol, "file:", &p)) {
2161
fd = qemu_create(p, O_WRONLY | O_TRUNC | O_BINARY, S_IRUSR, errp);
2167
"parameter 'protocol' must start with 'file:' or 'fd:'");
2170
if (kdump_raw && lseek(fd, 0, SEEK_CUR) == (off_t) -1) {
2172
error_setg(errp, "kdump-raw formats require a seekable file");
2176
if (!dump_migration_blocker) {
2177
error_setg(&dump_migration_blocker,
2178
"Live migration disabled: dump-guest-memory in progress");
2185
if (migrate_add_blocker_internal(&dump_migration_blocker, errp)) {
2191
s = &dump_state_global;
2192
dump_state_prepare(s);
2194
dump_init(s, fd, has_format, format, paging, has_begin,
2195
begin, length, kdump_raw, errp);
2197
qatomic_set(&s->status, DUMP_STATUS_FAILED);
2204
qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2205
s, QEMU_THREAD_DETACHED);
2208
dump_process(s, errp);
2212
DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2214
DumpGuestMemoryCapability *cap =
2215
g_new0(DumpGuestMemoryCapability, 1);
2216
DumpGuestMemoryFormatList **tail = &cap->formats;
2219
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
2222
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
2223
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_ZLIB);
2227
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
2228
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_LZO);
2233
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
2234
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_SNAPPY);
2237
if (win_dump_available(NULL)) {
2238
QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);