fsdev/virtfs-proxy-helper: Use g_mkdir()
[qemu/ar7.git] / dump / dump.c
blob236559b03a8aa01e34452bce7ecbe3c5ae98d47e
1 /*
2 * QEMU dump
4 * Copyright Fujitsu, Corp. 2011, 2012
6 * Authors:
7 * Wen Congyang <wency@cn.fujitsu.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qemu/cutils.h"
16 #include "elf.h"
17 #include "exec/hwaddr.h"
18 #include "monitor/monitor.h"
19 #include "sysemu/kvm.h"
20 #include "sysemu/dump.h"
21 #include "sysemu/memory_mapping.h"
22 #include "sysemu/runstate.h"
23 #include "sysemu/cpus.h"
24 #include "qapi/error.h"
25 #include "qapi/qapi-commands-dump.h"
26 #include "qapi/qapi-events-dump.h"
27 #include "qapi/qmp/qerror.h"
28 #include "qemu/error-report.h"
29 #include "qemu/main-loop.h"
30 #include "hw/misc/vmcoreinfo.h"
31 #include "migration/blocker.h"
33 #ifdef TARGET_X86_64
34 #include "win_dump.h"
35 #endif
37 #include <zlib.h>
38 #ifdef CONFIG_LZO
39 #include <lzo/lzo1x.h>
40 #endif
41 #ifdef CONFIG_SNAPPY
42 #include <snappy-c.h>
43 #endif
44 #ifndef ELF_MACHINE_UNAME
45 #define ELF_MACHINE_UNAME "Unknown"
46 #endif
48 #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
50 static Error *dump_migration_blocker;
52 #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
53 ((DIV_ROUND_UP((hdr_size), 4) + \
54 DIV_ROUND_UP((name_size), 4) + \
55 DIV_ROUND_UP((desc_size), 4)) * 4)
57 static inline bool dump_is_64bit(DumpState *s)
59 return s->dump_info.d_class == ELFCLASS64;
62 static inline bool dump_has_filter(DumpState *s)
64 return s->filter_area_length > 0;
67 uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
69 if (s->dump_info.d_endian == ELFDATA2LSB) {
70 val = cpu_to_le16(val);
71 } else {
72 val = cpu_to_be16(val);
75 return val;
78 uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
80 if (s->dump_info.d_endian == ELFDATA2LSB) {
81 val = cpu_to_le32(val);
82 } else {
83 val = cpu_to_be32(val);
86 return val;
89 uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
91 if (s->dump_info.d_endian == ELFDATA2LSB) {
92 val = cpu_to_le64(val);
93 } else {
94 val = cpu_to_be64(val);
97 return val;
100 static int dump_cleanup(DumpState *s)
102 guest_phys_blocks_free(&s->guest_phys_blocks);
103 memory_mapping_list_free(&s->list);
104 close(s->fd);
105 g_free(s->guest_note);
106 s->guest_note = NULL;
107 if (s->resume) {
108 if (s->detached) {
109 qemu_mutex_lock_iothread();
111 vm_start();
112 if (s->detached) {
113 qemu_mutex_unlock_iothread();
116 migrate_del_blocker(dump_migration_blocker);
118 return 0;
121 static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
123 DumpState *s = opaque;
124 size_t written_size;
126 written_size = qemu_write_full(s->fd, buf, size);
127 if (written_size != size) {
128 return -errno;
131 return 0;
134 static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header)
137 * phnum in the elf header is 16 bit, if we have more segments we
138 * set phnum to PN_XNUM and write the real number of segments to a
139 * special section.
141 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
143 memset(elf_header, 0, sizeof(Elf64_Ehdr));
144 memcpy(elf_header, ELFMAG, SELFMAG);
145 elf_header->e_ident[EI_CLASS] = ELFCLASS64;
146 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
147 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
148 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
149 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
150 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
151 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
152 elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset);
153 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
154 elf_header->e_phnum = cpu_to_dump16(s, phnum);
155 if (s->shdr_num) {
156 elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset);
157 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
158 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
162 static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
165 * phnum in the elf header is 16 bit, if we have more segments we
166 * set phnum to PN_XNUM and write the real number of segments to a
167 * special section.
169 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
171 memset(elf_header, 0, sizeof(Elf32_Ehdr));
172 memcpy(elf_header, ELFMAG, SELFMAG);
173 elf_header->e_ident[EI_CLASS] = ELFCLASS32;
174 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
175 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
176 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
177 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
178 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
179 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
180 elf_header->e_phoff = cpu_to_dump32(s, s->phdr_offset);
181 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
182 elf_header->e_phnum = cpu_to_dump16(s, phnum);
183 if (s->shdr_num) {
184 elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
185 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
186 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
190 static void write_elf_header(DumpState *s, Error **errp)
192 Elf32_Ehdr elf32_header;
193 Elf64_Ehdr elf64_header;
194 size_t header_size;
195 void *header_ptr;
196 int ret;
198 if (dump_is_64bit(s)) {
199 prepare_elf64_header(s, &elf64_header);
200 header_size = sizeof(elf64_header);
201 header_ptr = &elf64_header;
202 } else {
203 prepare_elf32_header(s, &elf32_header);
204 header_size = sizeof(elf32_header);
205 header_ptr = &elf32_header;
208 ret = fd_write_vmcore(header_ptr, header_size, s);
209 if (ret < 0) {
210 error_setg_errno(errp, -ret, "dump: failed to write elf header");
214 static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
215 int phdr_index, hwaddr offset,
216 hwaddr filesz, Error **errp)
218 Elf64_Phdr phdr;
219 int ret;
221 memset(&phdr, 0, sizeof(Elf64_Phdr));
222 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
223 phdr.p_offset = cpu_to_dump64(s, offset);
224 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
225 phdr.p_filesz = cpu_to_dump64(s, filesz);
226 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
227 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
229 assert(memory_mapping->length >= filesz);
231 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
232 if (ret < 0) {
233 error_setg_errno(errp, -ret,
234 "dump: failed to write program header table");
238 static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
239 int phdr_index, hwaddr offset,
240 hwaddr filesz, Error **errp)
242 Elf32_Phdr phdr;
243 int ret;
245 memset(&phdr, 0, sizeof(Elf32_Phdr));
246 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
247 phdr.p_offset = cpu_to_dump32(s, offset);
248 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
249 phdr.p_filesz = cpu_to_dump32(s, filesz);
250 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
251 phdr.p_vaddr =
252 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
254 assert(memory_mapping->length >= filesz);
256 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
257 if (ret < 0) {
258 error_setg_errno(errp, -ret,
259 "dump: failed to write program header table");
263 static void prepare_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
265 memset(phdr, 0, sizeof(*phdr));
266 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
267 phdr->p_offset = cpu_to_dump64(s, s->note_offset);
268 phdr->p_paddr = 0;
269 phdr->p_filesz = cpu_to_dump64(s, s->note_size);
270 phdr->p_memsz = cpu_to_dump64(s, s->note_size);
271 phdr->p_vaddr = 0;
274 static inline int cpu_index(CPUState *cpu)
276 return cpu->cpu_index + 1;
279 static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
280 Error **errp)
282 int ret;
284 if (s->guest_note) {
285 ret = f(s->guest_note, s->guest_note_size, s);
286 if (ret < 0) {
287 error_setg(errp, "dump: failed to write guest note");
292 static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
293 Error **errp)
295 CPUState *cpu;
296 int ret;
297 int id;
299 CPU_FOREACH(cpu) {
300 id = cpu_index(cpu);
301 ret = cpu_write_elf64_note(f, cpu, id, s);
302 if (ret < 0) {
303 error_setg(errp, "dump: failed to write elf notes");
304 return;
308 CPU_FOREACH(cpu) {
309 ret = cpu_write_elf64_qemunote(f, cpu, s);
310 if (ret < 0) {
311 error_setg(errp, "dump: failed to write CPU status");
312 return;
316 write_guest_note(f, s, errp);
319 static void prepare_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
321 memset(phdr, 0, sizeof(*phdr));
322 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
323 phdr->p_offset = cpu_to_dump32(s, s->note_offset);
324 phdr->p_paddr = 0;
325 phdr->p_filesz = cpu_to_dump32(s, s->note_size);
326 phdr->p_memsz = cpu_to_dump32(s, s->note_size);
327 phdr->p_vaddr = 0;
330 static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
331 Error **errp)
333 CPUState *cpu;
334 int ret;
335 int id;
337 CPU_FOREACH(cpu) {
338 id = cpu_index(cpu);
339 ret = cpu_write_elf32_note(f, cpu, id, s);
340 if (ret < 0) {
341 error_setg(errp, "dump: failed to write elf notes");
342 return;
346 CPU_FOREACH(cpu) {
347 ret = cpu_write_elf32_qemunote(f, cpu, s);
348 if (ret < 0) {
349 error_setg(errp, "dump: failed to write CPU status");
350 return;
354 write_guest_note(f, s, errp);
357 static void write_elf_phdr_note(DumpState *s, Error **errp)
359 ERRP_GUARD();
360 Elf32_Phdr phdr32;
361 Elf64_Phdr phdr64;
362 void *phdr;
363 size_t size;
364 int ret;
366 if (dump_is_64bit(s)) {
367 prepare_elf64_phdr_note(s, &phdr64);
368 size = sizeof(phdr64);
369 phdr = &phdr64;
370 } else {
371 prepare_elf32_phdr_note(s, &phdr32);
372 size = sizeof(phdr32);
373 phdr = &phdr32;
376 ret = fd_write_vmcore(phdr, size, s);
377 if (ret < 0) {
378 error_setg_errno(errp, -ret,
379 "dump: failed to write program header table");
383 static void write_elf_section(DumpState *s, int type, Error **errp)
385 Elf32_Shdr shdr32;
386 Elf64_Shdr shdr64;
387 int shdr_size;
388 void *shdr;
389 int ret;
391 if (type == 0) {
392 shdr_size = sizeof(Elf32_Shdr);
393 memset(&shdr32, 0, shdr_size);
394 shdr32.sh_info = cpu_to_dump32(s, s->phdr_num);
395 shdr = &shdr32;
396 } else {
397 shdr_size = sizeof(Elf64_Shdr);
398 memset(&shdr64, 0, shdr_size);
399 shdr64.sh_info = cpu_to_dump32(s, s->phdr_num);
400 shdr = &shdr64;
403 ret = fd_write_vmcore(shdr, shdr_size, s);
404 if (ret < 0) {
405 error_setg_errno(errp, -ret,
406 "dump: failed to write section header table");
410 static void write_data(DumpState *s, void *buf, int length, Error **errp)
412 int ret;
414 ret = fd_write_vmcore(buf, length, s);
415 if (ret < 0) {
416 error_setg_errno(errp, -ret, "dump: failed to save memory");
417 } else {
418 s->written_size += length;
422 /* write the memory to vmcore. 1 page per I/O. */
423 static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
424 int64_t size, Error **errp)
426 ERRP_GUARD();
427 int64_t i;
429 for (i = 0; i < size / s->dump_info.page_size; i++) {
430 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
431 s->dump_info.page_size, errp);
432 if (*errp) {
433 return;
437 if ((size % s->dump_info.page_size) != 0) {
438 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
439 size % s->dump_info.page_size, errp);
440 if (*errp) {
441 return;
446 /* get the memory's offset and size in the vmcore */
447 static void get_offset_range(hwaddr phys_addr,
448 ram_addr_t mapping_length,
449 DumpState *s,
450 hwaddr *p_offset,
451 hwaddr *p_filesz)
453 GuestPhysBlock *block;
454 hwaddr offset = s->memory_offset;
455 int64_t size_in_block, start;
457 /* When the memory is not stored into vmcore, offset will be -1 */
458 *p_offset = -1;
459 *p_filesz = 0;
461 if (dump_has_filter(s)) {
462 if (phys_addr < s->filter_area_begin ||
463 phys_addr >= s->filter_area_begin + s->filter_area_length) {
464 return;
468 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
469 if (dump_has_filter(s)) {
470 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
471 block->target_end <= s->filter_area_begin) {
472 /* This block is out of the range */
473 continue;
476 if (s->filter_area_begin <= block->target_start) {
477 start = block->target_start;
478 } else {
479 start = s->filter_area_begin;
482 size_in_block = block->target_end - start;
483 if (s->filter_area_begin + s->filter_area_length < block->target_end) {
484 size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
486 } else {
487 start = block->target_start;
488 size_in_block = block->target_end - block->target_start;
491 if (phys_addr >= start && phys_addr < start + size_in_block) {
492 *p_offset = phys_addr - start + offset;
494 /* The offset range mapped from the vmcore file must not spill over
495 * the GuestPhysBlock, clamp it. The rest of the mapping will be
496 * zero-filled in memory at load time; see
497 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
499 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
500 mapping_length :
501 size_in_block - (phys_addr - start);
502 return;
505 offset += size_in_block;
509 static void write_elf_phdr_loads(DumpState *s, Error **errp)
511 ERRP_GUARD();
512 hwaddr offset, filesz;
513 MemoryMapping *memory_mapping;
514 uint32_t phdr_index = 1;
516 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
517 get_offset_range(memory_mapping->phys_addr,
518 memory_mapping->length,
519 s, &offset, &filesz);
520 if (dump_is_64bit(s)) {
521 write_elf64_load(s, memory_mapping, phdr_index++, offset,
522 filesz, errp);
523 } else {
524 write_elf32_load(s, memory_mapping, phdr_index++, offset,
525 filesz, errp);
528 if (*errp) {
529 return;
532 if (phdr_index >= s->phdr_num) {
533 break;
538 static void write_elf_notes(DumpState *s, Error **errp)
540 if (dump_is_64bit(s)) {
541 write_elf64_notes(fd_write_vmcore, s, errp);
542 } else {
543 write_elf32_notes(fd_write_vmcore, s, errp);
547 /* write elf header, PT_NOTE and elf note to vmcore. */
548 static void dump_begin(DumpState *s, Error **errp)
550 ERRP_GUARD();
553 * the vmcore's format is:
554 * --------------
555 * | elf header |
556 * --------------
557 * | PT_NOTE |
558 * --------------
559 * | PT_LOAD |
560 * --------------
561 * | ...... |
562 * --------------
563 * | PT_LOAD |
564 * --------------
565 * | sec_hdr |
566 * --------------
567 * | elf note |
568 * --------------
569 * | memory |
570 * --------------
572 * we only know where the memory is saved after we write elf note into
573 * vmcore.
576 /* write elf header to vmcore */
577 write_elf_header(s, errp);
578 if (*errp) {
579 return;
582 /* write PT_NOTE to vmcore */
583 write_elf_phdr_note(s, errp);
584 if (*errp) {
585 return;
588 /* write all PT_LOADs to vmcore */
589 write_elf_phdr_loads(s, errp);
590 if (*errp) {
591 return;
594 /* write section to vmcore */
595 if (s->shdr_num) {
596 write_elf_section(s, 1, errp);
597 if (*errp) {
598 return;
602 /* write notes to vmcore */
603 write_elf_notes(s, errp);
606 static int64_t dump_filtered_memblock_size(GuestPhysBlock *block,
607 int64_t filter_area_start,
608 int64_t filter_area_length)
610 int64_t size, left, right;
612 /* No filter, return full size */
613 if (!filter_area_length) {
614 return block->target_end - block->target_start;
617 /* calculate the overlapped region. */
618 left = MAX(filter_area_start, block->target_start);
619 right = MIN(filter_area_start + filter_area_length, block->target_end);
620 size = right - left;
621 size = size > 0 ? size : 0;
623 return size;
626 static int64_t dump_filtered_memblock_start(GuestPhysBlock *block,
627 int64_t filter_area_start,
628 int64_t filter_area_length)
630 if (filter_area_length) {
631 /* return -1 if the block is not within filter area */
632 if (block->target_start >= filter_area_start + filter_area_length ||
633 block->target_end <= filter_area_start) {
634 return -1;
637 if (filter_area_start > block->target_start) {
638 return filter_area_start - block->target_start;
642 return 0;
645 /* write all memory to vmcore */
646 static void dump_iterate(DumpState *s, Error **errp)
648 ERRP_GUARD();
649 GuestPhysBlock *block;
650 int64_t memblock_size, memblock_start;
652 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
653 memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
654 if (memblock_start == -1) {
655 continue;
658 memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
660 /* Write the memory to file */
661 write_memory(s, block, memblock_start, memblock_size, errp);
662 if (*errp) {
663 return;
668 static void create_vmcore(DumpState *s, Error **errp)
670 ERRP_GUARD();
672 dump_begin(s, errp);
673 if (*errp) {
674 return;
677 dump_iterate(s, errp);
680 static int write_start_flat_header(int fd)
682 MakedumpfileHeader *mh;
683 int ret = 0;
685 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
686 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
688 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
689 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
691 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
692 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
694 size_t written_size;
695 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
696 if (written_size != MAX_SIZE_MDF_HEADER) {
697 ret = -1;
700 g_free(mh);
701 return ret;
704 static int write_end_flat_header(int fd)
706 MakedumpfileDataHeader mdh;
708 mdh.offset = END_FLAG_FLAT_HEADER;
709 mdh.buf_size = END_FLAG_FLAT_HEADER;
711 size_t written_size;
712 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
713 if (written_size != sizeof(mdh)) {
714 return -1;
717 return 0;
720 static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
722 size_t written_size;
723 MakedumpfileDataHeader mdh;
725 mdh.offset = cpu_to_be64(offset);
726 mdh.buf_size = cpu_to_be64(size);
728 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
729 if (written_size != sizeof(mdh)) {
730 return -1;
733 written_size = qemu_write_full(fd, buf, size);
734 if (written_size != size) {
735 return -1;
738 return 0;
741 static int buf_write_note(const void *buf, size_t size, void *opaque)
743 DumpState *s = opaque;
745 /* note_buf is not enough */
746 if (s->note_buf_offset + size > s->note_size) {
747 return -1;
750 memcpy(s->note_buf + s->note_buf_offset, buf, size);
752 s->note_buf_offset += size;
754 return 0;
758 * This function retrieves various sizes from an elf header.
760 * @note has to be a valid ELF note. The return sizes are unmodified
761 * (not padded or rounded up to be multiple of 4).
763 static void get_note_sizes(DumpState *s, const void *note,
764 uint64_t *note_head_size,
765 uint64_t *name_size,
766 uint64_t *desc_size)
768 uint64_t note_head_sz;
769 uint64_t name_sz;
770 uint64_t desc_sz;
772 if (dump_is_64bit(s)) {
773 const Elf64_Nhdr *hdr = note;
774 note_head_sz = sizeof(Elf64_Nhdr);
775 name_sz = tswap64(hdr->n_namesz);
776 desc_sz = tswap64(hdr->n_descsz);
777 } else {
778 const Elf32_Nhdr *hdr = note;
779 note_head_sz = sizeof(Elf32_Nhdr);
780 name_sz = tswap32(hdr->n_namesz);
781 desc_sz = tswap32(hdr->n_descsz);
784 if (note_head_size) {
785 *note_head_size = note_head_sz;
787 if (name_size) {
788 *name_size = name_sz;
790 if (desc_size) {
791 *desc_size = desc_sz;
795 static bool note_name_equal(DumpState *s,
796 const uint8_t *note, const char *name)
798 int len = strlen(name) + 1;
799 uint64_t head_size, name_size;
801 get_note_sizes(s, note, &head_size, &name_size, NULL);
802 head_size = ROUND_UP(head_size, 4);
804 return name_size == len && memcmp(note + head_size, name, len) == 0;
807 /* write common header, sub header and elf note to vmcore */
808 static void create_header32(DumpState *s, Error **errp)
810 ERRP_GUARD();
811 DiskDumpHeader32 *dh = NULL;
812 KdumpSubHeader32 *kh = NULL;
813 size_t size;
814 uint32_t block_size;
815 uint32_t sub_hdr_size;
816 uint32_t bitmap_blocks;
817 uint32_t status = 0;
818 uint64_t offset_note;
820 /* write common header, the version of kdump-compressed format is 6th */
821 size = sizeof(DiskDumpHeader32);
822 dh = g_malloc0(size);
824 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
825 dh->header_version = cpu_to_dump32(s, 6);
826 block_size = s->dump_info.page_size;
827 dh->block_size = cpu_to_dump32(s, block_size);
828 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
829 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
830 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
831 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
832 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
833 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
834 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
835 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
836 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
838 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
839 status |= DUMP_DH_COMPRESSED_ZLIB;
841 #ifdef CONFIG_LZO
842 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
843 status |= DUMP_DH_COMPRESSED_LZO;
845 #endif
846 #ifdef CONFIG_SNAPPY
847 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
848 status |= DUMP_DH_COMPRESSED_SNAPPY;
850 #endif
851 dh->status = cpu_to_dump32(s, status);
853 if (write_buffer(s->fd, 0, dh, size) < 0) {
854 error_setg(errp, "dump: failed to write disk dump header");
855 goto out;
858 /* write sub header */
859 size = sizeof(KdumpSubHeader32);
860 kh = g_malloc0(size);
862 /* 64bit max_mapnr_64 */
863 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
864 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
865 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
867 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
868 if (s->guest_note &&
869 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
870 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
872 get_note_sizes(s, s->guest_note,
873 &hsize, &name_size, &size_vmcoreinfo_desc);
874 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
875 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
876 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
877 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
880 kh->offset_note = cpu_to_dump64(s, offset_note);
881 kh->note_size = cpu_to_dump32(s, s->note_size);
883 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
884 block_size, kh, size) < 0) {
885 error_setg(errp, "dump: failed to write kdump sub header");
886 goto out;
889 /* write note */
890 s->note_buf = g_malloc0(s->note_size);
891 s->note_buf_offset = 0;
893 /* use s->note_buf to store notes temporarily */
894 write_elf32_notes(buf_write_note, s, errp);
895 if (*errp) {
896 goto out;
898 if (write_buffer(s->fd, offset_note, s->note_buf,
899 s->note_size) < 0) {
900 error_setg(errp, "dump: failed to write notes");
901 goto out;
904 /* get offset of dump_bitmap */
905 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
906 block_size;
908 /* get offset of page */
909 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
910 block_size;
912 out:
913 g_free(dh);
914 g_free(kh);
915 g_free(s->note_buf);
918 /* write common header, sub header and elf note to vmcore */
919 static void create_header64(DumpState *s, Error **errp)
921 ERRP_GUARD();
922 DiskDumpHeader64 *dh = NULL;
923 KdumpSubHeader64 *kh = NULL;
924 size_t size;
925 uint32_t block_size;
926 uint32_t sub_hdr_size;
927 uint32_t bitmap_blocks;
928 uint32_t status = 0;
929 uint64_t offset_note;
931 /* write common header, the version of kdump-compressed format is 6th */
932 size = sizeof(DiskDumpHeader64);
933 dh = g_malloc0(size);
935 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
936 dh->header_version = cpu_to_dump32(s, 6);
937 block_size = s->dump_info.page_size;
938 dh->block_size = cpu_to_dump32(s, block_size);
939 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
940 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
941 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
942 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
943 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
944 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
945 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
946 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
947 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
949 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
950 status |= DUMP_DH_COMPRESSED_ZLIB;
952 #ifdef CONFIG_LZO
953 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
954 status |= DUMP_DH_COMPRESSED_LZO;
956 #endif
957 #ifdef CONFIG_SNAPPY
958 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
959 status |= DUMP_DH_COMPRESSED_SNAPPY;
961 #endif
962 dh->status = cpu_to_dump32(s, status);
964 if (write_buffer(s->fd, 0, dh, size) < 0) {
965 error_setg(errp, "dump: failed to write disk dump header");
966 goto out;
969 /* write sub header */
970 size = sizeof(KdumpSubHeader64);
971 kh = g_malloc0(size);
973 /* 64bit max_mapnr_64 */
974 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
975 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
976 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
978 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
979 if (s->guest_note &&
980 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
981 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
983 get_note_sizes(s, s->guest_note,
984 &hsize, &name_size, &size_vmcoreinfo_desc);
985 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
986 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
987 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
988 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
991 kh->offset_note = cpu_to_dump64(s, offset_note);
992 kh->note_size = cpu_to_dump64(s, s->note_size);
994 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
995 block_size, kh, size) < 0) {
996 error_setg(errp, "dump: failed to write kdump sub header");
997 goto out;
1000 /* write note */
1001 s->note_buf = g_malloc0(s->note_size);
1002 s->note_buf_offset = 0;
1004 /* use s->note_buf to store notes temporarily */
1005 write_elf64_notes(buf_write_note, s, errp);
1006 if (*errp) {
1007 goto out;
1010 if (write_buffer(s->fd, offset_note, s->note_buf,
1011 s->note_size) < 0) {
1012 error_setg(errp, "dump: failed to write notes");
1013 goto out;
1016 /* get offset of dump_bitmap */
1017 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1018 block_size;
1020 /* get offset of page */
1021 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1022 block_size;
1024 out:
1025 g_free(dh);
1026 g_free(kh);
1027 g_free(s->note_buf);
1030 static void write_dump_header(DumpState *s, Error **errp)
1032 if (dump_is_64bit(s)) {
1033 create_header64(s, errp);
1034 } else {
1035 create_header32(s, errp);
1039 static size_t dump_bitmap_get_bufsize(DumpState *s)
1041 return s->dump_info.page_size;
1045 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1046 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1047 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1048 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1049 * vmcore, ie. synchronizing un-sync bit into vmcore.
1051 static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1052 uint8_t *buf, DumpState *s)
1054 off_t old_offset, new_offset;
1055 off_t offset_bitmap1, offset_bitmap2;
1056 uint32_t byte, bit;
1057 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1058 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1060 /* should not set the previous place */
1061 assert(last_pfn <= pfn);
1064 * if the bit needed to be set is not cached in buf, flush the data in buf
1065 * to vmcore firstly.
1066 * making new_offset be bigger than old_offset can also sync remained data
1067 * into vmcore.
1069 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1070 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
1072 while (old_offset < new_offset) {
1073 /* calculate the offset and write dump_bitmap */
1074 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1075 if (write_buffer(s->fd, offset_bitmap1, buf,
1076 bitmap_bufsize) < 0) {
1077 return -1;
1080 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1081 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1082 old_offset;
1083 if (write_buffer(s->fd, offset_bitmap2, buf,
1084 bitmap_bufsize) < 0) {
1085 return -1;
1088 memset(buf, 0, bitmap_bufsize);
1089 old_offset += bitmap_bufsize;
1092 /* get the exact place of the bit in the buf, and set it */
1093 byte = (pfn % bits_per_buf) / CHAR_BIT;
1094 bit = (pfn % bits_per_buf) % CHAR_BIT;
1095 if (value) {
1096 buf[byte] |= 1u << bit;
1097 } else {
1098 buf[byte] &= ~(1u << bit);
1101 return 0;
1104 static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1106 int target_page_shift = ctz32(s->dump_info.page_size);
1108 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1111 static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1113 int target_page_shift = ctz32(s->dump_info.page_size);
1115 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1119 * Return the page frame number and the page content in *bufptr. bufptr can be
1120 * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
1121 * memory. This is not necessarily the memory returned.
1123 static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1124 uint8_t **bufptr, DumpState *s)
1126 GuestPhysBlock *block = *blockptr;
1127 uint32_t page_size = s->dump_info.page_size;
1128 uint8_t *buf = NULL, *hbuf;
1129 hwaddr addr;
1131 /* block == NULL means the start of the iteration */
1132 if (!block) {
1133 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1134 *blockptr = block;
1135 addr = block->target_start;
1136 *pfnptr = dump_paddr_to_pfn(s, addr);
1137 } else {
1138 *pfnptr += 1;
1139 addr = dump_pfn_to_paddr(s, *pfnptr);
1141 assert(block != NULL);
1143 while (1) {
1144 if (addr >= block->target_start && addr < block->target_end) {
1145 size_t n = MIN(block->target_end - addr, page_size - addr % page_size);
1146 hbuf = block->host_addr + (addr - block->target_start);
1147 if (!buf) {
1148 if (n == page_size) {
1149 /* this is a whole target page, go for it */
1150 assert(addr % page_size == 0);
1151 buf = hbuf;
1152 break;
1153 } else if (bufptr) {
1154 assert(*bufptr);
1155 buf = *bufptr;
1156 memset(buf, 0, page_size);
1157 } else {
1158 return true;
1162 memcpy(buf + addr % page_size, hbuf, n);
1163 addr += n;
1164 if (addr % page_size == 0) {
1165 /* we filled up the page */
1166 break;
1168 } else {
1169 /* the next page is in the next block */
1170 *blockptr = block = QTAILQ_NEXT(block, next);
1171 if (!block) {
1172 break;
1175 addr = block->target_start;
1176 /* are we still in the same page? */
1177 if (dump_paddr_to_pfn(s, addr) != *pfnptr) {
1178 if (buf) {
1179 /* no, but we already filled something earlier, return it */
1180 break;
1181 } else {
1182 /* else continue from there */
1183 *pfnptr = dump_paddr_to_pfn(s, addr);
1189 if (bufptr) {
1190 *bufptr = buf;
1193 return buf != NULL;
1196 static void write_dump_bitmap(DumpState *s, Error **errp)
1198 int ret = 0;
1199 uint64_t last_pfn, pfn;
1200 void *dump_bitmap_buf;
1201 size_t num_dumpable;
1202 GuestPhysBlock *block_iter = NULL;
1203 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1204 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1206 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1207 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
1209 num_dumpable = 0;
1210 last_pfn = 0;
1213 * exam memory page by page, and set the bit in dump_bitmap corresponded
1214 * to the existing page.
1216 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1217 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1218 if (ret < 0) {
1219 error_setg(errp, "dump: failed to set dump_bitmap");
1220 goto out;
1223 last_pfn = pfn;
1224 num_dumpable++;
1228 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1229 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1230 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1232 if (num_dumpable > 0) {
1233 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
1234 dump_bitmap_buf, s);
1235 if (ret < 0) {
1236 error_setg(errp, "dump: failed to sync dump_bitmap");
1237 goto out;
1241 /* number of dumpable pages that will be dumped later */
1242 s->num_dumpable = num_dumpable;
1244 out:
1245 g_free(dump_bitmap_buf);
1248 static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1249 off_t offset)
1251 data_cache->fd = s->fd;
1252 data_cache->data_size = 0;
1253 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1254 data_cache->buf = g_malloc0(data_cache->buf_size);
1255 data_cache->offset = offset;
1258 static int write_cache(DataCache *dc, const void *buf, size_t size,
1259 bool flag_sync)
1262 * dc->buf_size should not be less than size, otherwise dc will never be
1263 * enough
1265 assert(size <= dc->buf_size);
1268 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1269 * otherwise check if the space is enough for caching data in buf, if not,
1270 * write the data in dc->buf to dc->fd and reset dc->buf
1272 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1273 (flag_sync && dc->data_size > 0)) {
1274 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1275 return -1;
1278 dc->offset += dc->data_size;
1279 dc->data_size = 0;
1282 if (!flag_sync) {
1283 memcpy(dc->buf + dc->data_size, buf, size);
1284 dc->data_size += size;
1287 return 0;
1290 static void free_data_cache(DataCache *data_cache)
1292 g_free(data_cache->buf);
1295 static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1297 switch (flag_compress) {
1298 case DUMP_DH_COMPRESSED_ZLIB:
1299 return compressBound(page_size);
1301 case DUMP_DH_COMPRESSED_LZO:
1303 * LZO will expand incompressible data by a little amount. Please check
1304 * the following URL to see the expansion calculation:
1305 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1307 return page_size + page_size / 16 + 64 + 3;
1309 #ifdef CONFIG_SNAPPY
1310 case DUMP_DH_COMPRESSED_SNAPPY:
1311 return snappy_max_compressed_length(page_size);
1312 #endif
1314 return 0;
1317 static void write_dump_pages(DumpState *s, Error **errp)
1319 int ret = 0;
1320 DataCache page_desc, page_data;
1321 size_t len_buf_out, size_out;
1322 #ifdef CONFIG_LZO
1323 lzo_bytep wrkmem = NULL;
1324 #endif
1325 uint8_t *buf_out = NULL;
1326 off_t offset_desc, offset_data;
1327 PageDescriptor pd, pd_zero;
1328 uint8_t *buf;
1329 GuestPhysBlock *block_iter = NULL;
1330 uint64_t pfn_iter;
1331 g_autofree uint8_t *page = NULL;
1333 /* get offset of page_desc and page_data in dump file */
1334 offset_desc = s->offset_page;
1335 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1337 prepare_data_cache(&page_desc, s, offset_desc);
1338 prepare_data_cache(&page_data, s, offset_data);
1340 /* prepare buffer to store compressed data */
1341 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
1342 assert(len_buf_out != 0);
1344 #ifdef CONFIG_LZO
1345 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1346 #endif
1348 buf_out = g_malloc(len_buf_out);
1351 * init zero page's page_desc and page_data, because every zero page
1352 * uses the same page_data
1354 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
1355 pd_zero.flags = cpu_to_dump32(s, 0);
1356 pd_zero.offset = cpu_to_dump64(s, offset_data);
1357 pd_zero.page_flags = cpu_to_dump64(s, 0);
1358 buf = g_malloc0(s->dump_info.page_size);
1359 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
1360 g_free(buf);
1361 if (ret < 0) {
1362 error_setg(errp, "dump: failed to write page data (zero page)");
1363 goto out;
1366 offset_data += s->dump_info.page_size;
1367 page = g_malloc(s->dump_info.page_size);
1370 * dump memory to vmcore page by page. zero page will all be resided in the
1371 * first page of page section
1373 for (buf = page; get_next_page(&block_iter, &pfn_iter, &buf, s); buf = page) {
1374 /* check zero page */
1375 if (buffer_is_zero(buf, s->dump_info.page_size)) {
1376 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1377 false);
1378 if (ret < 0) {
1379 error_setg(errp, "dump: failed to write page desc");
1380 goto out;
1382 } else {
1384 * not zero page, then:
1385 * 1. compress the page
1386 * 2. write the compressed page into the cache of page_data
1387 * 3. get page desc of the compressed page and write it into the
1388 * cache of page_desc
1390 * only one compression format will be used here, for
1391 * s->flag_compress is set. But when compression fails to work,
1392 * we fall back to save in plaintext.
1394 size_out = len_buf_out;
1395 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
1396 (compress2(buf_out, (uLongf *)&size_out, buf,
1397 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1398 (size_out < s->dump_info.page_size)) {
1399 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1400 pd.size = cpu_to_dump32(s, size_out);
1402 ret = write_cache(&page_data, buf_out, size_out, false);
1403 if (ret < 0) {
1404 error_setg(errp, "dump: failed to write page data");
1405 goto out;
1407 #ifdef CONFIG_LZO
1408 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
1409 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
1410 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
1411 (size_out < s->dump_info.page_size)) {
1412 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1413 pd.size = cpu_to_dump32(s, size_out);
1415 ret = write_cache(&page_data, buf_out, size_out, false);
1416 if (ret < 0) {
1417 error_setg(errp, "dump: failed to write page data");
1418 goto out;
1420 #endif
1421 #ifdef CONFIG_SNAPPY
1422 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
1423 (snappy_compress((char *)buf, s->dump_info.page_size,
1424 (char *)buf_out, &size_out) == SNAPPY_OK) &&
1425 (size_out < s->dump_info.page_size)) {
1426 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1427 pd.size = cpu_to_dump32(s, size_out);
1429 ret = write_cache(&page_data, buf_out, size_out, false);
1430 if (ret < 0) {
1431 error_setg(errp, "dump: failed to write page data");
1432 goto out;
1434 #endif
1435 } else {
1437 * fall back to save in plaintext, size_out should be
1438 * assigned the target's page size
1440 pd.flags = cpu_to_dump32(s, 0);
1441 size_out = s->dump_info.page_size;
1442 pd.size = cpu_to_dump32(s, size_out);
1444 ret = write_cache(&page_data, buf,
1445 s->dump_info.page_size, false);
1446 if (ret < 0) {
1447 error_setg(errp, "dump: failed to write page data");
1448 goto out;
1452 /* get and write page desc here */
1453 pd.page_flags = cpu_to_dump64(s, 0);
1454 pd.offset = cpu_to_dump64(s, offset_data);
1455 offset_data += size_out;
1457 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1458 if (ret < 0) {
1459 error_setg(errp, "dump: failed to write page desc");
1460 goto out;
1463 s->written_size += s->dump_info.page_size;
1466 ret = write_cache(&page_desc, NULL, 0, true);
1467 if (ret < 0) {
1468 error_setg(errp, "dump: failed to sync cache for page_desc");
1469 goto out;
1471 ret = write_cache(&page_data, NULL, 0, true);
1472 if (ret < 0) {
1473 error_setg(errp, "dump: failed to sync cache for page_data");
1474 goto out;
1477 out:
1478 free_data_cache(&page_desc);
1479 free_data_cache(&page_data);
1481 #ifdef CONFIG_LZO
1482 g_free(wrkmem);
1483 #endif
1485 g_free(buf_out);
1488 static void create_kdump_vmcore(DumpState *s, Error **errp)
1490 ERRP_GUARD();
1491 int ret;
1494 * the kdump-compressed format is:
1495 * File offset
1496 * +------------------------------------------+ 0x0
1497 * | main header (struct disk_dump_header) |
1498 * |------------------------------------------+ block 1
1499 * | sub header (struct kdump_sub_header) |
1500 * |------------------------------------------+ block 2
1501 * | 1st-dump_bitmap |
1502 * |------------------------------------------+ block 2 + X blocks
1503 * | 2nd-dump_bitmap | (aligned by block)
1504 * |------------------------------------------+ block 2 + 2 * X blocks
1505 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1506 * | page desc for pfn 1 (struct page_desc) |
1507 * | : |
1508 * |------------------------------------------| (not aligned by block)
1509 * | page data (pfn 0) |
1510 * | page data (pfn 1) |
1511 * | : |
1512 * +------------------------------------------+
1515 ret = write_start_flat_header(s->fd);
1516 if (ret < 0) {
1517 error_setg(errp, "dump: failed to write start flat header");
1518 return;
1521 write_dump_header(s, errp);
1522 if (*errp) {
1523 return;
1526 write_dump_bitmap(s, errp);
1527 if (*errp) {
1528 return;
1531 write_dump_pages(s, errp);
1532 if (*errp) {
1533 return;
1536 ret = write_end_flat_header(s->fd);
1537 if (ret < 0) {
1538 error_setg(errp, "dump: failed to write end flat header");
1539 return;
1543 static int validate_start_block(DumpState *s)
1545 GuestPhysBlock *block;
1547 if (!dump_has_filter(s)) {
1548 return 0;
1551 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1552 /* This block is out of the range */
1553 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
1554 block->target_end <= s->filter_area_begin) {
1555 continue;
1557 return 0;
1560 return -1;
1563 static void get_max_mapnr(DumpState *s)
1565 GuestPhysBlock *last_block;
1567 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
1568 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
1571 static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1573 static void dump_state_prepare(DumpState *s)
1575 /* zero the struct, setting status to active */
1576 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1579 bool qemu_system_dump_in_progress(void)
1581 DumpState *state = &dump_state_global;
1582 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
1586 * calculate total size of memory to be dumped (taking filter into
1587 * account.)
1589 static int64_t dump_calculate_size(DumpState *s)
1591 GuestPhysBlock *block;
1592 int64_t total = 0;
1594 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1595 total += dump_filtered_memblock_size(block,
1596 s->filter_area_begin,
1597 s->filter_area_length);
1600 return total;
1603 static void vmcoreinfo_update_phys_base(DumpState *s)
1605 uint64_t size, note_head_size, name_size, phys_base;
1606 char **lines;
1607 uint8_t *vmci;
1608 size_t i;
1610 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1611 return;
1614 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1615 note_head_size = ROUND_UP(note_head_size, 4);
1617 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1618 *(vmci + size) = '\0';
1620 lines = g_strsplit((char *)vmci, "\n", -1);
1621 for (i = 0; lines[i]; i++) {
1622 const char *prefix = NULL;
1624 if (s->dump_info.d_machine == EM_X86_64) {
1625 prefix = "NUMBER(phys_base)=";
1626 } else if (s->dump_info.d_machine == EM_AARCH64) {
1627 prefix = "NUMBER(PHYS_OFFSET)=";
1630 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1631 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
1632 &phys_base) < 0) {
1633 warn_report("Failed to read %s", prefix);
1634 } else {
1635 s->dump_info.phys_base = phys_base;
1637 break;
1641 g_strfreev(lines);
1644 static void dump_init(DumpState *s, int fd, bool has_format,
1645 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1646 int64_t begin, int64_t length, Error **errp)
1648 ERRP_GUARD();
1649 VMCoreInfoState *vmci = vmcoreinfo_find();
1650 CPUState *cpu;
1651 int nr_cpus;
1652 int ret;
1654 s->has_format = has_format;
1655 s->format = format;
1656 s->written_size = 0;
1658 /* kdump-compressed is conflict with paging and filter */
1659 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1660 assert(!paging && !has_filter);
1663 if (runstate_is_running()) {
1664 vm_stop(RUN_STATE_SAVE_VM);
1665 s->resume = true;
1666 } else {
1667 s->resume = false;
1670 /* If we use KVM, we should synchronize the registers before we get dump
1671 * info or physmap info.
1673 cpu_synchronize_all_states();
1674 nr_cpus = 0;
1675 CPU_FOREACH(cpu) {
1676 nr_cpus++;
1679 s->fd = fd;
1680 if (has_filter && !length) {
1681 error_setg(errp, QERR_INVALID_PARAMETER, "length");
1682 goto cleanup;
1684 s->filter_area_begin = begin;
1685 s->filter_area_length = length;
1687 memory_mapping_list_init(&s->list);
1689 guest_phys_blocks_init(&s->guest_phys_blocks);
1690 guest_phys_blocks_append(&s->guest_phys_blocks);
1691 s->total_size = dump_calculate_size(s);
1692 #ifdef DEBUG_DUMP_GUEST_MEMORY
1693 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1694 #endif
1696 /* it does not make sense to dump non-existent memory */
1697 if (!s->total_size) {
1698 error_setg(errp, "dump: no guest memory to dump");
1699 goto cleanup;
1702 /* Is the filter filtering everything? */
1703 if (validate_start_block(s) == -1) {
1704 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
1705 goto cleanup;
1708 /* get dump info: endian, class and architecture.
1709 * If the target architecture is not supported, cpu_get_dump_info() will
1710 * return -1.
1712 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
1713 if (ret < 0) {
1714 error_setg(errp, QERR_UNSUPPORTED);
1715 goto cleanup;
1718 if (!s->dump_info.page_size) {
1719 s->dump_info.page_size = TARGET_PAGE_SIZE;
1722 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1723 s->dump_info.d_machine, nr_cpus);
1724 if (s->note_size < 0) {
1725 error_setg(errp, QERR_UNSUPPORTED);
1726 goto cleanup;
1730 * The goal of this block is to (a) update the previously guessed
1731 * phys_base, (b) copy the guest note out of the guest.
1732 * Failure to do so is not fatal for dumping.
1734 if (vmci) {
1735 uint64_t addr, note_head_size, name_size, desc_size;
1736 uint32_t size;
1737 uint16_t format;
1739 note_head_size = dump_is_64bit(s) ?
1740 sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
1742 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1743 size = le32_to_cpu(vmci->vmcoreinfo.size);
1744 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1745 if (!vmci->has_vmcoreinfo) {
1746 warn_report("guest note is not present");
1747 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1748 warn_report("guest note size is invalid: %" PRIu32, size);
1749 } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
1750 warn_report("guest note format is unsupported: %" PRIu16, format);
1751 } else {
1752 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1753 cpu_physical_memory_read(addr, s->guest_note, size);
1755 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1756 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1757 desc_size);
1758 if (name_size > MAX_GUEST_NOTE_SIZE ||
1759 desc_size > MAX_GUEST_NOTE_SIZE ||
1760 s->guest_note_size > size) {
1761 warn_report("Invalid guest note header");
1762 g_free(s->guest_note);
1763 s->guest_note = NULL;
1764 } else {
1765 vmcoreinfo_update_phys_base(s);
1766 s->note_size += s->guest_note_size;
1771 /* get memory mapping */
1772 if (paging) {
1773 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1774 if (*errp) {
1775 goto cleanup;
1777 } else {
1778 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
1781 s->nr_cpus = nr_cpus;
1783 get_max_mapnr(s);
1785 uint64_t tmp;
1786 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1787 s->dump_info.page_size);
1788 s->len_dump_bitmap = tmp * s->dump_info.page_size;
1790 /* init for kdump-compressed format */
1791 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1792 switch (format) {
1793 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1794 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1795 break;
1797 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
1798 #ifdef CONFIG_LZO
1799 if (lzo_init() != LZO_E_OK) {
1800 error_setg(errp, "failed to initialize the LZO library");
1801 goto cleanup;
1803 #endif
1804 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1805 break;
1807 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1808 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1809 break;
1811 default:
1812 s->flag_compress = 0;
1815 return;
1818 if (dump_has_filter(s)) {
1819 memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
1823 * calculate phdr_num
1825 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1827 s->phdr_num = 1; /* PT_NOTE */
1828 if (s->list.num < UINT16_MAX - 2) {
1829 s->shdr_num = 0;
1830 s->phdr_num += s->list.num;
1831 } else {
1832 /* sh_info of section 0 holds the real number of phdrs */
1833 s->shdr_num = 1;
1835 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1836 if (s->list.num <= UINT32_MAX - 1) {
1837 s->phdr_num += s->list.num;
1838 } else {
1839 s->phdr_num = UINT32_MAX;
1843 if (dump_is_64bit(s)) {
1844 s->phdr_offset = sizeof(Elf64_Ehdr);
1845 s->shdr_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
1846 s->note_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
1847 s->memory_offset = s->note_offset + s->note_size;
1848 } else {
1850 s->phdr_offset = sizeof(Elf32_Ehdr);
1851 s->shdr_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
1852 s->note_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
1853 s->memory_offset = s->note_offset + s->note_size;
1856 return;
1858 cleanup:
1859 dump_cleanup(s);
1862 /* this operation might be time consuming. */
1863 static void dump_process(DumpState *s, Error **errp)
1865 ERRP_GUARD();
1866 DumpQueryResult *result = NULL;
1868 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1869 #ifdef TARGET_X86_64
1870 create_win_dump(s, errp);
1871 #endif
1872 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1873 create_kdump_vmcore(s, errp);
1874 } else {
1875 create_vmcore(s, errp);
1878 /* make sure status is written after written_size updates */
1879 smp_wmb();
1880 qatomic_set(&s->status,
1881 (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
1883 /* send DUMP_COMPLETED message (unconditionally) */
1884 result = qmp_query_dump(NULL);
1885 /* should never fail */
1886 assert(result);
1887 qapi_event_send_dump_completed(result, !!*errp, (*errp ?
1888 error_get_pretty(*errp) : NULL));
1889 qapi_free_DumpQueryResult(result);
1891 dump_cleanup(s);
1894 static void *dump_thread(void *data)
1896 DumpState *s = (DumpState *)data;
1897 dump_process(s, NULL);
1898 return NULL;
1901 DumpQueryResult *qmp_query_dump(Error **errp)
1903 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1904 DumpState *state = &dump_state_global;
1905 result->status = qatomic_read(&state->status);
1906 /* make sure we are reading status and written_size in order */
1907 smp_rmb();
1908 result->completed = state->written_size;
1909 result->total = state->total_size;
1910 return result;
1913 void qmp_dump_guest_memory(bool paging, const char *file,
1914 bool has_detach, bool detach,
1915 bool has_begin, int64_t begin, bool has_length,
1916 int64_t length, bool has_format,
1917 DumpGuestMemoryFormat format, Error **errp)
1919 ERRP_GUARD();
1920 const char *p;
1921 int fd = -1;
1922 DumpState *s;
1923 bool detach_p = false;
1925 if (runstate_check(RUN_STATE_INMIGRATE)) {
1926 error_setg(errp, "Dump not allowed during incoming migration.");
1927 return;
1930 /* if there is a dump in background, we should wait until the dump
1931 * finished */
1932 if (qemu_system_dump_in_progress()) {
1933 error_setg(errp, "There is a dump in process, please wait.");
1934 return;
1938 * kdump-compressed format need the whole memory dumped, so paging or
1939 * filter is not supported here.
1941 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1942 (paging || has_begin || has_length)) {
1943 error_setg(errp, "kdump-compressed format doesn't support paging or "
1944 "filter");
1945 return;
1947 if (has_begin && !has_length) {
1948 error_setg(errp, QERR_MISSING_PARAMETER, "length");
1949 return;
1951 if (!has_begin && has_length) {
1952 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
1953 return;
1955 if (has_detach) {
1956 detach_p = detach;
1959 /* check whether lzo/snappy is supported */
1960 #ifndef CONFIG_LZO
1961 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1962 error_setg(errp, "kdump-lzo is not available now");
1963 return;
1965 #endif
1967 #ifndef CONFIG_SNAPPY
1968 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1969 error_setg(errp, "kdump-snappy is not available now");
1970 return;
1972 #endif
1974 #ifndef TARGET_X86_64
1975 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1976 error_setg(errp, "Windows dump is only available for x86-64");
1977 return;
1979 #endif
1981 #if !defined(WIN32)
1982 if (strstart(file, "fd:", &p)) {
1983 fd = monitor_get_fd(monitor_cur(), p, errp);
1984 if (fd == -1) {
1985 return;
1988 #endif
1990 if (strstart(file, "file:", &p)) {
1991 fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1992 if (fd < 0) {
1993 error_setg_file_open(errp, errno, p);
1994 return;
1998 if (fd == -1) {
1999 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
2000 return;
2003 if (!dump_migration_blocker) {
2004 error_setg(&dump_migration_blocker,
2005 "Live migration disabled: dump-guest-memory in progress");
2009 * Allows even for -only-migratable, but forbid migration during the
2010 * process of dump guest memory.
2012 if (migrate_add_blocker_internal(dump_migration_blocker, errp)) {
2013 /* Remember to release the fd before passing it over to dump state */
2014 close(fd);
2015 return;
2018 s = &dump_state_global;
2019 dump_state_prepare(s);
2021 dump_init(s, fd, has_format, format, paging, has_begin,
2022 begin, length, errp);
2023 if (*errp) {
2024 qatomic_set(&s->status, DUMP_STATUS_FAILED);
2025 return;
2028 if (detach_p) {
2029 /* detached dump */
2030 s->detached = true;
2031 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2032 s, QEMU_THREAD_DETACHED);
2033 } else {
2034 /* sync dump */
2035 dump_process(s, errp);
2039 DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2041 DumpGuestMemoryCapability *cap =
2042 g_new0(DumpGuestMemoryCapability, 1);
2043 DumpGuestMemoryFormatList **tail = &cap->formats;
2045 /* elf is always available */
2046 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
2048 /* kdump-zlib is always available */
2049 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
2051 /* add new item if kdump-lzo is available */
2052 #ifdef CONFIG_LZO
2053 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
2054 #endif
2056 /* add new item if kdump-snappy is available */
2057 #ifdef CONFIG_SNAPPY
2058 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
2059 #endif
2061 /* Windows dump is available only if target is x86_64 */
2062 #ifdef TARGET_X86_64
2063 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
2064 #endif
2066 return cap;