dump-guest-memory: disable dump when in INMIGRATE state
[qemu/rayw.git] / dump.c
blob923e3a5756c489c8c87ee9259bbd3879736d8a8d
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-common.h"
16 #include "elf.h"
17 #include "cpu.h"
18 #include "exec/cpu-all.h"
19 #include "exec/hwaddr.h"
20 #include "monitor/monitor.h"
21 #include "sysemu/kvm.h"
22 #include "sysemu/dump.h"
23 #include "sysemu/sysemu.h"
24 #include "sysemu/memory_mapping.h"
25 #include "sysemu/cpus.h"
26 #include "qapi/qmp/qerror.h"
27 #include "qmp-commands.h"
29 #include <zlib.h>
30 #ifdef CONFIG_LZO
31 #include <lzo/lzo1x.h>
32 #endif
33 #ifdef CONFIG_SNAPPY
34 #include <snappy-c.h>
35 #endif
36 #ifndef ELF_MACHINE_UNAME
37 #define ELF_MACHINE_UNAME "Unknown"
38 #endif
40 uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
42 if (s->dump_info.d_endian == ELFDATA2LSB) {
43 val = cpu_to_le16(val);
44 } else {
45 val = cpu_to_be16(val);
48 return val;
51 uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
53 if (s->dump_info.d_endian == ELFDATA2LSB) {
54 val = cpu_to_le32(val);
55 } else {
56 val = cpu_to_be32(val);
59 return val;
62 uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
64 if (s->dump_info.d_endian == ELFDATA2LSB) {
65 val = cpu_to_le64(val);
66 } else {
67 val = cpu_to_be64(val);
70 return val;
73 static int dump_cleanup(DumpState *s)
75 guest_phys_blocks_free(&s->guest_phys_blocks);
76 memory_mapping_list_free(&s->list);
77 close(s->fd);
78 if (s->resume) {
79 vm_start();
82 return 0;
85 static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
87 DumpState *s = opaque;
88 size_t written_size;
90 written_size = qemu_write_full(s->fd, buf, size);
91 if (written_size != size) {
92 return -1;
95 return 0;
98 static void write_elf64_header(DumpState *s, Error **errp)
100 Elf64_Ehdr elf_header;
101 int ret;
103 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
104 memcpy(&elf_header, ELFMAG, SELFMAG);
105 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
106 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
107 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
108 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
109 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
110 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
111 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
112 elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
113 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
114 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
115 if (s->have_section) {
116 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
118 elf_header.e_shoff = cpu_to_dump64(s, shoff);
119 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
120 elf_header.e_shnum = cpu_to_dump16(s, 1);
123 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
124 if (ret < 0) {
125 error_setg(errp, "dump: failed to write elf header");
129 static void write_elf32_header(DumpState *s, Error **errp)
131 Elf32_Ehdr elf_header;
132 int ret;
134 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
135 memcpy(&elf_header, ELFMAG, SELFMAG);
136 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
137 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
138 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
139 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
140 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
141 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
142 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
143 elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
144 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
145 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
146 if (s->have_section) {
147 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
149 elf_header.e_shoff = cpu_to_dump32(s, shoff);
150 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
151 elf_header.e_shnum = cpu_to_dump16(s, 1);
154 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
155 if (ret < 0) {
156 error_setg(errp, "dump: failed to write elf header");
160 static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
161 int phdr_index, hwaddr offset,
162 hwaddr filesz, Error **errp)
164 Elf64_Phdr phdr;
165 int ret;
167 memset(&phdr, 0, sizeof(Elf64_Phdr));
168 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
169 phdr.p_offset = cpu_to_dump64(s, offset);
170 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
171 phdr.p_filesz = cpu_to_dump64(s, filesz);
172 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
173 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr);
175 assert(memory_mapping->length >= filesz);
177 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
178 if (ret < 0) {
179 error_setg(errp, "dump: failed to write program header table");
183 static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
184 int phdr_index, hwaddr offset,
185 hwaddr filesz, Error **errp)
187 Elf32_Phdr phdr;
188 int ret;
190 memset(&phdr, 0, sizeof(Elf32_Phdr));
191 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
192 phdr.p_offset = cpu_to_dump32(s, offset);
193 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
194 phdr.p_filesz = cpu_to_dump32(s, filesz);
195 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
196 phdr.p_vaddr = cpu_to_dump32(s, memory_mapping->virt_addr);
198 assert(memory_mapping->length >= filesz);
200 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
201 if (ret < 0) {
202 error_setg(errp, "dump: failed to write program header table");
206 static void write_elf64_note(DumpState *s, Error **errp)
208 Elf64_Phdr phdr;
209 hwaddr begin = s->memory_offset - s->note_size;
210 int ret;
212 memset(&phdr, 0, sizeof(Elf64_Phdr));
213 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
214 phdr.p_offset = cpu_to_dump64(s, begin);
215 phdr.p_paddr = 0;
216 phdr.p_filesz = cpu_to_dump64(s, s->note_size);
217 phdr.p_memsz = cpu_to_dump64(s, s->note_size);
218 phdr.p_vaddr = 0;
220 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
221 if (ret < 0) {
222 error_setg(errp, "dump: failed to write program header table");
226 static inline int cpu_index(CPUState *cpu)
228 return cpu->cpu_index + 1;
231 static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
232 Error **errp)
234 CPUState *cpu;
235 int ret;
236 int id;
238 CPU_FOREACH(cpu) {
239 id = cpu_index(cpu);
240 ret = cpu_write_elf64_note(f, cpu, id, s);
241 if (ret < 0) {
242 error_setg(errp, "dump: failed to write elf notes");
243 return;
247 CPU_FOREACH(cpu) {
248 ret = cpu_write_elf64_qemunote(f, cpu, s);
249 if (ret < 0) {
250 error_setg(errp, "dump: failed to write CPU status");
251 return;
256 static void write_elf32_note(DumpState *s, Error **errp)
258 hwaddr begin = s->memory_offset - s->note_size;
259 Elf32_Phdr phdr;
260 int ret;
262 memset(&phdr, 0, sizeof(Elf32_Phdr));
263 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
264 phdr.p_offset = cpu_to_dump32(s, begin);
265 phdr.p_paddr = 0;
266 phdr.p_filesz = cpu_to_dump32(s, s->note_size);
267 phdr.p_memsz = cpu_to_dump32(s, s->note_size);
268 phdr.p_vaddr = 0;
270 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
271 if (ret < 0) {
272 error_setg(errp, "dump: failed to write program header table");
276 static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
277 Error **errp)
279 CPUState *cpu;
280 int ret;
281 int id;
283 CPU_FOREACH(cpu) {
284 id = cpu_index(cpu);
285 ret = cpu_write_elf32_note(f, cpu, id, s);
286 if (ret < 0) {
287 error_setg(errp, "dump: failed to write elf notes");
288 return;
292 CPU_FOREACH(cpu) {
293 ret = cpu_write_elf32_qemunote(f, cpu, s);
294 if (ret < 0) {
295 error_setg(errp, "dump: failed to write CPU status");
296 return;
301 static void write_elf_section(DumpState *s, int type, Error **errp)
303 Elf32_Shdr shdr32;
304 Elf64_Shdr shdr64;
305 int shdr_size;
306 void *shdr;
307 int ret;
309 if (type == 0) {
310 shdr_size = sizeof(Elf32_Shdr);
311 memset(&shdr32, 0, shdr_size);
312 shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
313 shdr = &shdr32;
314 } else {
315 shdr_size = sizeof(Elf64_Shdr);
316 memset(&shdr64, 0, shdr_size);
317 shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
318 shdr = &shdr64;
321 ret = fd_write_vmcore(&shdr, shdr_size, s);
322 if (ret < 0) {
323 error_setg(errp, "dump: failed to write section header table");
327 static void write_data(DumpState *s, void *buf, int length, Error **errp)
329 int ret;
331 ret = fd_write_vmcore(buf, length, s);
332 if (ret < 0) {
333 error_setg(errp, "dump: failed to save memory");
337 /* write the memory to vmcore. 1 page per I/O. */
338 static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
339 int64_t size, Error **errp)
341 int64_t i;
342 Error *local_err = NULL;
344 for (i = 0; i < size / s->dump_info.page_size; i++) {
345 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
346 s->dump_info.page_size, &local_err);
347 if (local_err) {
348 error_propagate(errp, local_err);
349 return;
353 if ((size % s->dump_info.page_size) != 0) {
354 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
355 size % s->dump_info.page_size, &local_err);
356 if (local_err) {
357 error_propagate(errp, local_err);
358 return;
363 /* get the memory's offset and size in the vmcore */
364 static void get_offset_range(hwaddr phys_addr,
365 ram_addr_t mapping_length,
366 DumpState *s,
367 hwaddr *p_offset,
368 hwaddr *p_filesz)
370 GuestPhysBlock *block;
371 hwaddr offset = s->memory_offset;
372 int64_t size_in_block, start;
374 /* When the memory is not stored into vmcore, offset will be -1 */
375 *p_offset = -1;
376 *p_filesz = 0;
378 if (s->has_filter) {
379 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
380 return;
384 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
385 if (s->has_filter) {
386 if (block->target_start >= s->begin + s->length ||
387 block->target_end <= s->begin) {
388 /* This block is out of the range */
389 continue;
392 if (s->begin <= block->target_start) {
393 start = block->target_start;
394 } else {
395 start = s->begin;
398 size_in_block = block->target_end - start;
399 if (s->begin + s->length < block->target_end) {
400 size_in_block -= block->target_end - (s->begin + s->length);
402 } else {
403 start = block->target_start;
404 size_in_block = block->target_end - block->target_start;
407 if (phys_addr >= start && phys_addr < start + size_in_block) {
408 *p_offset = phys_addr - start + offset;
410 /* The offset range mapped from the vmcore file must not spill over
411 * the GuestPhysBlock, clamp it. The rest of the mapping will be
412 * zero-filled in memory at load time; see
413 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
415 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
416 mapping_length :
417 size_in_block - (phys_addr - start);
418 return;
421 offset += size_in_block;
425 static void write_elf_loads(DumpState *s, Error **errp)
427 hwaddr offset, filesz;
428 MemoryMapping *memory_mapping;
429 uint32_t phdr_index = 1;
430 uint32_t max_index;
431 Error *local_err = NULL;
433 if (s->have_section) {
434 max_index = s->sh_info;
435 } else {
436 max_index = s->phdr_num;
439 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
440 get_offset_range(memory_mapping->phys_addr,
441 memory_mapping->length,
442 s, &offset, &filesz);
443 if (s->dump_info.d_class == ELFCLASS64) {
444 write_elf64_load(s, memory_mapping, phdr_index++, offset,
445 filesz, &local_err);
446 } else {
447 write_elf32_load(s, memory_mapping, phdr_index++, offset,
448 filesz, &local_err);
451 if (local_err) {
452 error_propagate(errp, local_err);
453 return;
456 if (phdr_index >= max_index) {
457 break;
462 /* write elf header, PT_NOTE and elf note to vmcore. */
463 static void dump_begin(DumpState *s, Error **errp)
465 Error *local_err = NULL;
468 * the vmcore's format is:
469 * --------------
470 * | elf header |
471 * --------------
472 * | PT_NOTE |
473 * --------------
474 * | PT_LOAD |
475 * --------------
476 * | ...... |
477 * --------------
478 * | PT_LOAD |
479 * --------------
480 * | sec_hdr |
481 * --------------
482 * | elf note |
483 * --------------
484 * | memory |
485 * --------------
487 * we only know where the memory is saved after we write elf note into
488 * vmcore.
491 /* write elf header to vmcore */
492 if (s->dump_info.d_class == ELFCLASS64) {
493 write_elf64_header(s, &local_err);
494 } else {
495 write_elf32_header(s, &local_err);
497 if (local_err) {
498 error_propagate(errp, local_err);
499 return;
502 if (s->dump_info.d_class == ELFCLASS64) {
503 /* write PT_NOTE to vmcore */
504 write_elf64_note(s, &local_err);
505 if (local_err) {
506 error_propagate(errp, local_err);
507 return;
510 /* write all PT_LOAD to vmcore */
511 write_elf_loads(s, &local_err);
512 if (local_err) {
513 error_propagate(errp, local_err);
514 return;
517 /* write section to vmcore */
518 if (s->have_section) {
519 write_elf_section(s, 1, &local_err);
520 if (local_err) {
521 error_propagate(errp, local_err);
522 return;
526 /* write notes to vmcore */
527 write_elf64_notes(fd_write_vmcore, s, &local_err);
528 if (local_err) {
529 error_propagate(errp, local_err);
530 return;
532 } else {
533 /* write PT_NOTE to vmcore */
534 write_elf32_note(s, &local_err);
535 if (local_err) {
536 error_propagate(errp, local_err);
537 return;
540 /* write all PT_LOAD to vmcore */
541 write_elf_loads(s, &local_err);
542 if (local_err) {
543 error_propagate(errp, local_err);
544 return;
547 /* write section to vmcore */
548 if (s->have_section) {
549 write_elf_section(s, 0, &local_err);
550 if (local_err) {
551 error_propagate(errp, local_err);
552 return;
556 /* write notes to vmcore */
557 write_elf32_notes(fd_write_vmcore, s, &local_err);
558 if (local_err) {
559 error_propagate(errp, local_err);
560 return;
565 static int get_next_block(DumpState *s, GuestPhysBlock *block)
567 while (1) {
568 block = QTAILQ_NEXT(block, next);
569 if (!block) {
570 /* no more block */
571 return 1;
574 s->start = 0;
575 s->next_block = block;
576 if (s->has_filter) {
577 if (block->target_start >= s->begin + s->length ||
578 block->target_end <= s->begin) {
579 /* This block is out of the range */
580 continue;
583 if (s->begin > block->target_start) {
584 s->start = s->begin - block->target_start;
588 return 0;
592 /* write all memory to vmcore */
593 static void dump_iterate(DumpState *s, Error **errp)
595 GuestPhysBlock *block;
596 int64_t size;
597 Error *local_err = NULL;
599 do {
600 block = s->next_block;
602 size = block->target_end - block->target_start;
603 if (s->has_filter) {
604 size -= s->start;
605 if (s->begin + s->length < block->target_end) {
606 size -= block->target_end - (s->begin + s->length);
609 write_memory(s, block, s->start, size, &local_err);
610 if (local_err) {
611 error_propagate(errp, local_err);
612 return;
615 } while (!get_next_block(s, block));
618 static void create_vmcore(DumpState *s, Error **errp)
620 Error *local_err = NULL;
622 dump_begin(s, &local_err);
623 if (local_err) {
624 error_propagate(errp, local_err);
625 return;
628 dump_iterate(s, errp);
631 static int write_start_flat_header(int fd)
633 MakedumpfileHeader *mh;
634 int ret = 0;
636 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
637 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
639 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
640 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
642 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
643 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
645 size_t written_size;
646 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
647 if (written_size != MAX_SIZE_MDF_HEADER) {
648 ret = -1;
651 g_free(mh);
652 return ret;
655 static int write_end_flat_header(int fd)
657 MakedumpfileDataHeader mdh;
659 mdh.offset = END_FLAG_FLAT_HEADER;
660 mdh.buf_size = END_FLAG_FLAT_HEADER;
662 size_t written_size;
663 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
664 if (written_size != sizeof(mdh)) {
665 return -1;
668 return 0;
671 static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
673 size_t written_size;
674 MakedumpfileDataHeader mdh;
676 mdh.offset = cpu_to_be64(offset);
677 mdh.buf_size = cpu_to_be64(size);
679 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
680 if (written_size != sizeof(mdh)) {
681 return -1;
684 written_size = qemu_write_full(fd, buf, size);
685 if (written_size != size) {
686 return -1;
689 return 0;
692 static int buf_write_note(const void *buf, size_t size, void *opaque)
694 DumpState *s = opaque;
696 /* note_buf is not enough */
697 if (s->note_buf_offset + size > s->note_size) {
698 return -1;
701 memcpy(s->note_buf + s->note_buf_offset, buf, size);
703 s->note_buf_offset += size;
705 return 0;
708 /* write common header, sub header and elf note to vmcore */
709 static void create_header32(DumpState *s, Error **errp)
711 DiskDumpHeader32 *dh = NULL;
712 KdumpSubHeader32 *kh = NULL;
713 size_t size;
714 uint32_t block_size;
715 uint32_t sub_hdr_size;
716 uint32_t bitmap_blocks;
717 uint32_t status = 0;
718 uint64_t offset_note;
719 Error *local_err = NULL;
721 /* write common header, the version of kdump-compressed format is 6th */
722 size = sizeof(DiskDumpHeader32);
723 dh = g_malloc0(size);
725 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
726 dh->header_version = cpu_to_dump32(s, 6);
727 block_size = s->dump_info.page_size;
728 dh->block_size = cpu_to_dump32(s, block_size);
729 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
730 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
731 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
732 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
733 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
734 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
735 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
736 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
737 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
739 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
740 status |= DUMP_DH_COMPRESSED_ZLIB;
742 #ifdef CONFIG_LZO
743 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
744 status |= DUMP_DH_COMPRESSED_LZO;
746 #endif
747 #ifdef CONFIG_SNAPPY
748 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
749 status |= DUMP_DH_COMPRESSED_SNAPPY;
751 #endif
752 dh->status = cpu_to_dump32(s, status);
754 if (write_buffer(s->fd, 0, dh, size) < 0) {
755 error_setg(errp, "dump: failed to write disk dump header");
756 goto out;
759 /* write sub header */
760 size = sizeof(KdumpSubHeader32);
761 kh = g_malloc0(size);
763 /* 64bit max_mapnr_64 */
764 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
765 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
766 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
768 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
769 kh->offset_note = cpu_to_dump64(s, offset_note);
770 kh->note_size = cpu_to_dump32(s, s->note_size);
772 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
773 block_size, kh, size) < 0) {
774 error_setg(errp, "dump: failed to write kdump sub header");
775 goto out;
778 /* write note */
779 s->note_buf = g_malloc0(s->note_size);
780 s->note_buf_offset = 0;
782 /* use s->note_buf to store notes temporarily */
783 write_elf32_notes(buf_write_note, s, &local_err);
784 if (local_err) {
785 error_propagate(errp, local_err);
786 goto out;
788 if (write_buffer(s->fd, offset_note, s->note_buf,
789 s->note_size) < 0) {
790 error_setg(errp, "dump: failed to write notes");
791 goto out;
794 /* get offset of dump_bitmap */
795 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
796 block_size;
798 /* get offset of page */
799 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
800 block_size;
802 out:
803 g_free(dh);
804 g_free(kh);
805 g_free(s->note_buf);
808 /* write common header, sub header and elf note to vmcore */
809 static void create_header64(DumpState *s, Error **errp)
811 DiskDumpHeader64 *dh = NULL;
812 KdumpSubHeader64 *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;
819 Error *local_err = NULL;
821 /* write common header, the version of kdump-compressed format is 6th */
822 size = sizeof(DiskDumpHeader64);
823 dh = g_malloc0(size);
825 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
826 dh->header_version = cpu_to_dump32(s, 6);
827 block_size = s->dump_info.page_size;
828 dh->block_size = cpu_to_dump32(s, block_size);
829 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
830 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
831 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
832 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
833 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
834 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
835 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
836 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
837 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
839 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
840 status |= DUMP_DH_COMPRESSED_ZLIB;
842 #ifdef CONFIG_LZO
843 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
844 status |= DUMP_DH_COMPRESSED_LZO;
846 #endif
847 #ifdef CONFIG_SNAPPY
848 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
849 status |= DUMP_DH_COMPRESSED_SNAPPY;
851 #endif
852 dh->status = cpu_to_dump32(s, status);
854 if (write_buffer(s->fd, 0, dh, size) < 0) {
855 error_setg(errp, "dump: failed to write disk dump header");
856 goto out;
859 /* write sub header */
860 size = sizeof(KdumpSubHeader64);
861 kh = g_malloc0(size);
863 /* 64bit max_mapnr_64 */
864 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
865 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
866 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
868 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
869 kh->offset_note = cpu_to_dump64(s, offset_note);
870 kh->note_size = cpu_to_dump64(s, s->note_size);
872 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
873 block_size, kh, size) < 0) {
874 error_setg(errp, "dump: failed to write kdump sub header");
875 goto out;
878 /* write note */
879 s->note_buf = g_malloc0(s->note_size);
880 s->note_buf_offset = 0;
882 /* use s->note_buf to store notes temporarily */
883 write_elf64_notes(buf_write_note, s, &local_err);
884 if (local_err) {
885 error_propagate(errp, local_err);
886 goto out;
889 if (write_buffer(s->fd, offset_note, s->note_buf,
890 s->note_size) < 0) {
891 error_setg(errp, "dump: failed to write notes");
892 goto out;
895 /* get offset of dump_bitmap */
896 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
897 block_size;
899 /* get offset of page */
900 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
901 block_size;
903 out:
904 g_free(dh);
905 g_free(kh);
906 g_free(s->note_buf);
909 static void write_dump_header(DumpState *s, Error **errp)
911 Error *local_err = NULL;
913 if (s->dump_info.d_class == ELFCLASS32) {
914 create_header32(s, &local_err);
915 } else {
916 create_header64(s, &local_err);
918 if (local_err) {
919 error_propagate(errp, local_err);
923 static size_t dump_bitmap_get_bufsize(DumpState *s)
925 return s->dump_info.page_size;
929 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
930 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
931 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
932 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
933 * vmcore, ie. synchronizing un-sync bit into vmcore.
935 static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
936 uint8_t *buf, DumpState *s)
938 off_t old_offset, new_offset;
939 off_t offset_bitmap1, offset_bitmap2;
940 uint32_t byte, bit;
941 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
942 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
944 /* should not set the previous place */
945 assert(last_pfn <= pfn);
948 * if the bit needed to be set is not cached in buf, flush the data in buf
949 * to vmcore firstly.
950 * making new_offset be bigger than old_offset can also sync remained data
951 * into vmcore.
953 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
954 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
956 while (old_offset < new_offset) {
957 /* calculate the offset and write dump_bitmap */
958 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
959 if (write_buffer(s->fd, offset_bitmap1, buf,
960 bitmap_bufsize) < 0) {
961 return -1;
964 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
965 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
966 old_offset;
967 if (write_buffer(s->fd, offset_bitmap2, buf,
968 bitmap_bufsize) < 0) {
969 return -1;
972 memset(buf, 0, bitmap_bufsize);
973 old_offset += bitmap_bufsize;
976 /* get the exact place of the bit in the buf, and set it */
977 byte = (pfn % bits_per_buf) / CHAR_BIT;
978 bit = (pfn % bits_per_buf) % CHAR_BIT;
979 if (value) {
980 buf[byte] |= 1u << bit;
981 } else {
982 buf[byte] &= ~(1u << bit);
985 return 0;
988 static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
990 int target_page_shift = ctz32(s->dump_info.page_size);
992 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
995 static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
997 int target_page_shift = ctz32(s->dump_info.page_size);
999 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1003 * exam every page and return the page frame number and the address of the page.
1004 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1005 * blocks, so block->target_start and block->target_end should be interal
1006 * multiples of the target page size.
1008 static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1009 uint8_t **bufptr, DumpState *s)
1011 GuestPhysBlock *block = *blockptr;
1012 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
1013 uint8_t *buf;
1015 /* block == NULL means the start of the iteration */
1016 if (!block) {
1017 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1018 *blockptr = block;
1019 assert((block->target_start & ~target_page_mask) == 0);
1020 assert((block->target_end & ~target_page_mask) == 0);
1021 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
1022 if (bufptr) {
1023 *bufptr = block->host_addr;
1025 return true;
1028 *pfnptr = *pfnptr + 1;
1029 addr = dump_pfn_to_paddr(s, *pfnptr);
1031 if ((addr >= block->target_start) &&
1032 (addr + s->dump_info.page_size <= block->target_end)) {
1033 buf = block->host_addr + (addr - block->target_start);
1034 } else {
1035 /* the next page is in the next block */
1036 block = QTAILQ_NEXT(block, next);
1037 *blockptr = block;
1038 if (!block) {
1039 return false;
1041 assert((block->target_start & ~target_page_mask) == 0);
1042 assert((block->target_end & ~target_page_mask) == 0);
1043 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
1044 buf = block->host_addr;
1047 if (bufptr) {
1048 *bufptr = buf;
1051 return true;
1054 static void write_dump_bitmap(DumpState *s, Error **errp)
1056 int ret = 0;
1057 uint64_t last_pfn, pfn;
1058 void *dump_bitmap_buf;
1059 size_t num_dumpable;
1060 GuestPhysBlock *block_iter = NULL;
1061 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1062 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1064 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1065 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
1067 num_dumpable = 0;
1068 last_pfn = 0;
1071 * exam memory page by page, and set the bit in dump_bitmap corresponded
1072 * to the existing page.
1074 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1075 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1076 if (ret < 0) {
1077 error_setg(errp, "dump: failed to set dump_bitmap");
1078 goto out;
1081 last_pfn = pfn;
1082 num_dumpable++;
1086 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1087 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1088 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1090 if (num_dumpable > 0) {
1091 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
1092 dump_bitmap_buf, s);
1093 if (ret < 0) {
1094 error_setg(errp, "dump: failed to sync dump_bitmap");
1095 goto out;
1099 /* number of dumpable pages that will be dumped later */
1100 s->num_dumpable = num_dumpable;
1102 out:
1103 g_free(dump_bitmap_buf);
1106 static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1107 off_t offset)
1109 data_cache->fd = s->fd;
1110 data_cache->data_size = 0;
1111 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1112 data_cache->buf = g_malloc0(data_cache->buf_size);
1113 data_cache->offset = offset;
1116 static int write_cache(DataCache *dc, const void *buf, size_t size,
1117 bool flag_sync)
1120 * dc->buf_size should not be less than size, otherwise dc will never be
1121 * enough
1123 assert(size <= dc->buf_size);
1126 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1127 * otherwise check if the space is enough for caching data in buf, if not,
1128 * write the data in dc->buf to dc->fd and reset dc->buf
1130 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1131 (flag_sync && dc->data_size > 0)) {
1132 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1133 return -1;
1136 dc->offset += dc->data_size;
1137 dc->data_size = 0;
1140 if (!flag_sync) {
1141 memcpy(dc->buf + dc->data_size, buf, size);
1142 dc->data_size += size;
1145 return 0;
1148 static void free_data_cache(DataCache *data_cache)
1150 g_free(data_cache->buf);
1153 static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1155 switch (flag_compress) {
1156 case DUMP_DH_COMPRESSED_ZLIB:
1157 return compressBound(page_size);
1159 case DUMP_DH_COMPRESSED_LZO:
1161 * LZO will expand incompressible data by a little amount. Please check
1162 * the following URL to see the expansion calculation:
1163 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1165 return page_size + page_size / 16 + 64 + 3;
1167 #ifdef CONFIG_SNAPPY
1168 case DUMP_DH_COMPRESSED_SNAPPY:
1169 return snappy_max_compressed_length(page_size);
1170 #endif
1172 return 0;
1176 * check if the page is all 0
1178 static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1180 return buffer_is_zero(buf, page_size);
1183 static void write_dump_pages(DumpState *s, Error **errp)
1185 int ret = 0;
1186 DataCache page_desc, page_data;
1187 size_t len_buf_out, size_out;
1188 #ifdef CONFIG_LZO
1189 lzo_bytep wrkmem = NULL;
1190 #endif
1191 uint8_t *buf_out = NULL;
1192 off_t offset_desc, offset_data;
1193 PageDescriptor pd, pd_zero;
1194 uint8_t *buf;
1195 GuestPhysBlock *block_iter = NULL;
1196 uint64_t pfn_iter;
1198 /* get offset of page_desc and page_data in dump file */
1199 offset_desc = s->offset_page;
1200 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1202 prepare_data_cache(&page_desc, s, offset_desc);
1203 prepare_data_cache(&page_data, s, offset_data);
1205 /* prepare buffer to store compressed data */
1206 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
1207 assert(len_buf_out != 0);
1209 #ifdef CONFIG_LZO
1210 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1211 #endif
1213 buf_out = g_malloc(len_buf_out);
1216 * init zero page's page_desc and page_data, because every zero page
1217 * uses the same page_data
1219 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
1220 pd_zero.flags = cpu_to_dump32(s, 0);
1221 pd_zero.offset = cpu_to_dump64(s, offset_data);
1222 pd_zero.page_flags = cpu_to_dump64(s, 0);
1223 buf = g_malloc0(s->dump_info.page_size);
1224 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
1225 g_free(buf);
1226 if (ret < 0) {
1227 error_setg(errp, "dump: failed to write page data (zero page)");
1228 goto out;
1231 offset_data += s->dump_info.page_size;
1234 * dump memory to vmcore page by page. zero page will all be resided in the
1235 * first page of page section
1237 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1238 /* check zero page */
1239 if (is_zero_page(buf, s->dump_info.page_size)) {
1240 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1241 false);
1242 if (ret < 0) {
1243 error_setg(errp, "dump: failed to write page desc");
1244 goto out;
1246 } else {
1248 * not zero page, then:
1249 * 1. compress the page
1250 * 2. write the compressed page into the cache of page_data
1251 * 3. get page desc of the compressed page and write it into the
1252 * cache of page_desc
1254 * only one compression format will be used here, for
1255 * s->flag_compress is set. But when compression fails to work,
1256 * we fall back to save in plaintext.
1258 size_out = len_buf_out;
1259 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
1260 (compress2(buf_out, (uLongf *)&size_out, buf,
1261 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1262 (size_out < s->dump_info.page_size)) {
1263 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1264 pd.size = cpu_to_dump32(s, size_out);
1266 ret = write_cache(&page_data, buf_out, size_out, false);
1267 if (ret < 0) {
1268 error_setg(errp, "dump: failed to write page data");
1269 goto out;
1271 #ifdef CONFIG_LZO
1272 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
1273 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
1274 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
1275 (size_out < s->dump_info.page_size)) {
1276 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1277 pd.size = cpu_to_dump32(s, size_out);
1279 ret = write_cache(&page_data, buf_out, size_out, false);
1280 if (ret < 0) {
1281 error_setg(errp, "dump: failed to write page data");
1282 goto out;
1284 #endif
1285 #ifdef CONFIG_SNAPPY
1286 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
1287 (snappy_compress((char *)buf, s->dump_info.page_size,
1288 (char *)buf_out, &size_out) == SNAPPY_OK) &&
1289 (size_out < s->dump_info.page_size)) {
1290 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1291 pd.size = cpu_to_dump32(s, size_out);
1293 ret = write_cache(&page_data, buf_out, size_out, false);
1294 if (ret < 0) {
1295 error_setg(errp, "dump: failed to write page data");
1296 goto out;
1298 #endif
1299 } else {
1301 * fall back to save in plaintext, size_out should be
1302 * assigned the target's page size
1304 pd.flags = cpu_to_dump32(s, 0);
1305 size_out = s->dump_info.page_size;
1306 pd.size = cpu_to_dump32(s, size_out);
1308 ret = write_cache(&page_data, buf,
1309 s->dump_info.page_size, false);
1310 if (ret < 0) {
1311 error_setg(errp, "dump: failed to write page data");
1312 goto out;
1316 /* get and write page desc here */
1317 pd.page_flags = cpu_to_dump64(s, 0);
1318 pd.offset = cpu_to_dump64(s, offset_data);
1319 offset_data += size_out;
1321 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1322 if (ret < 0) {
1323 error_setg(errp, "dump: failed to write page desc");
1324 goto out;
1329 ret = write_cache(&page_desc, NULL, 0, true);
1330 if (ret < 0) {
1331 error_setg(errp, "dump: failed to sync cache for page_desc");
1332 goto out;
1334 ret = write_cache(&page_data, NULL, 0, true);
1335 if (ret < 0) {
1336 error_setg(errp, "dump: failed to sync cache for page_data");
1337 goto out;
1340 out:
1341 free_data_cache(&page_desc);
1342 free_data_cache(&page_data);
1344 #ifdef CONFIG_LZO
1345 g_free(wrkmem);
1346 #endif
1348 g_free(buf_out);
1351 static void create_kdump_vmcore(DumpState *s, Error **errp)
1353 int ret;
1354 Error *local_err = NULL;
1357 * the kdump-compressed format is:
1358 * File offset
1359 * +------------------------------------------+ 0x0
1360 * | main header (struct disk_dump_header) |
1361 * |------------------------------------------+ block 1
1362 * | sub header (struct kdump_sub_header) |
1363 * |------------------------------------------+ block 2
1364 * | 1st-dump_bitmap |
1365 * |------------------------------------------+ block 2 + X blocks
1366 * | 2nd-dump_bitmap | (aligned by block)
1367 * |------------------------------------------+ block 2 + 2 * X blocks
1368 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1369 * | page desc for pfn 1 (struct page_desc) |
1370 * | : |
1371 * |------------------------------------------| (not aligned by block)
1372 * | page data (pfn 0) |
1373 * | page data (pfn 1) |
1374 * | : |
1375 * +------------------------------------------+
1378 ret = write_start_flat_header(s->fd);
1379 if (ret < 0) {
1380 error_setg(errp, "dump: failed to write start flat header");
1381 return;
1384 write_dump_header(s, &local_err);
1385 if (local_err) {
1386 error_propagate(errp, local_err);
1387 return;
1390 write_dump_bitmap(s, &local_err);
1391 if (local_err) {
1392 error_propagate(errp, local_err);
1393 return;
1396 write_dump_pages(s, &local_err);
1397 if (local_err) {
1398 error_propagate(errp, local_err);
1399 return;
1402 ret = write_end_flat_header(s->fd);
1403 if (ret < 0) {
1404 error_setg(errp, "dump: failed to write end flat header");
1405 return;
1409 static ram_addr_t get_start_block(DumpState *s)
1411 GuestPhysBlock *block;
1413 if (!s->has_filter) {
1414 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1415 return 0;
1418 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1419 if (block->target_start >= s->begin + s->length ||
1420 block->target_end <= s->begin) {
1421 /* This block is out of the range */
1422 continue;
1425 s->next_block = block;
1426 if (s->begin > block->target_start) {
1427 s->start = s->begin - block->target_start;
1428 } else {
1429 s->start = 0;
1431 return s->start;
1434 return -1;
1437 static void get_max_mapnr(DumpState *s)
1439 GuestPhysBlock *last_block;
1441 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead);
1442 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
1445 static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1447 static void dump_state_prepare(DumpState *s)
1449 /* zero the struct, setting status to active */
1450 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1453 bool dump_in_progress(void)
1455 DumpState *state = &dump_state_global;
1456 return (state->status == DUMP_STATUS_ACTIVE);
1459 static void dump_init(DumpState *s, int fd, bool has_format,
1460 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1461 int64_t begin, int64_t length, Error **errp)
1463 CPUState *cpu;
1464 int nr_cpus;
1465 Error *err = NULL;
1466 int ret;
1468 s->has_format = has_format;
1469 s->format = format;
1471 /* kdump-compressed is conflict with paging and filter */
1472 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1473 assert(!paging && !has_filter);
1476 if (runstate_is_running()) {
1477 vm_stop(RUN_STATE_SAVE_VM);
1478 s->resume = true;
1479 } else {
1480 s->resume = false;
1483 /* If we use KVM, we should synchronize the registers before we get dump
1484 * info or physmap info.
1486 cpu_synchronize_all_states();
1487 nr_cpus = 0;
1488 CPU_FOREACH(cpu) {
1489 nr_cpus++;
1492 s->fd = fd;
1493 s->has_filter = has_filter;
1494 s->begin = begin;
1495 s->length = length;
1497 memory_mapping_list_init(&s->list);
1499 guest_phys_blocks_init(&s->guest_phys_blocks);
1500 guest_phys_blocks_append(&s->guest_phys_blocks);
1502 s->start = get_start_block(s);
1503 if (s->start == -1) {
1504 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
1505 goto cleanup;
1508 /* get dump info: endian, class and architecture.
1509 * If the target architecture is not supported, cpu_get_dump_info() will
1510 * return -1.
1512 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
1513 if (ret < 0) {
1514 error_setg(errp, QERR_UNSUPPORTED);
1515 goto cleanup;
1518 if (!s->dump_info.page_size) {
1519 s->dump_info.page_size = TARGET_PAGE_SIZE;
1522 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1523 s->dump_info.d_machine, nr_cpus);
1524 if (s->note_size < 0) {
1525 error_setg(errp, QERR_UNSUPPORTED);
1526 goto cleanup;
1529 /* get memory mapping */
1530 if (paging) {
1531 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
1532 if (err != NULL) {
1533 error_propagate(errp, err);
1534 goto cleanup;
1536 } else {
1537 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
1540 s->nr_cpus = nr_cpus;
1542 get_max_mapnr(s);
1544 uint64_t tmp;
1545 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1546 s->dump_info.page_size);
1547 s->len_dump_bitmap = tmp * s->dump_info.page_size;
1549 /* init for kdump-compressed format */
1550 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1551 switch (format) {
1552 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1553 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1554 break;
1556 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
1557 #ifdef CONFIG_LZO
1558 if (lzo_init() != LZO_E_OK) {
1559 error_setg(errp, "failed to initialize the LZO library");
1560 goto cleanup;
1562 #endif
1563 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1564 break;
1566 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1567 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1568 break;
1570 default:
1571 s->flag_compress = 0;
1574 return;
1577 if (s->has_filter) {
1578 memory_mapping_filter(&s->list, s->begin, s->length);
1582 * calculate phdr_num
1584 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1586 s->phdr_num = 1; /* PT_NOTE */
1587 if (s->list.num < UINT16_MAX - 2) {
1588 s->phdr_num += s->list.num;
1589 s->have_section = false;
1590 } else {
1591 s->have_section = true;
1592 s->phdr_num = PN_XNUM;
1593 s->sh_info = 1; /* PT_NOTE */
1595 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1596 if (s->list.num <= UINT32_MAX - 1) {
1597 s->sh_info += s->list.num;
1598 } else {
1599 s->sh_info = UINT32_MAX;
1603 if (s->dump_info.d_class == ELFCLASS64) {
1604 if (s->have_section) {
1605 s->memory_offset = sizeof(Elf64_Ehdr) +
1606 sizeof(Elf64_Phdr) * s->sh_info +
1607 sizeof(Elf64_Shdr) + s->note_size;
1608 } else {
1609 s->memory_offset = sizeof(Elf64_Ehdr) +
1610 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1612 } else {
1613 if (s->have_section) {
1614 s->memory_offset = sizeof(Elf32_Ehdr) +
1615 sizeof(Elf32_Phdr) * s->sh_info +
1616 sizeof(Elf32_Shdr) + s->note_size;
1617 } else {
1618 s->memory_offset = sizeof(Elf32_Ehdr) +
1619 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1623 return;
1625 cleanup:
1626 dump_cleanup(s);
1629 /* this operation might be time consuming. */
1630 static void dump_process(DumpState *s, Error **errp)
1632 Error *local_err = NULL;
1634 if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1635 create_kdump_vmcore(s, &local_err);
1636 } else {
1637 create_vmcore(s, &local_err);
1640 s->status = (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED);
1641 error_propagate(errp, local_err);
1643 dump_cleanup(s);
1646 void qmp_dump_guest_memory(bool paging, const char *file,
1647 bool has_detach, bool detach,
1648 bool has_begin, int64_t begin, bool has_length,
1649 int64_t length, bool has_format,
1650 DumpGuestMemoryFormat format, Error **errp)
1652 const char *p;
1653 int fd = -1;
1654 DumpState *s;
1655 Error *local_err = NULL;
1657 if (runstate_check(RUN_STATE_INMIGRATE)) {
1658 error_setg(errp, "Dump not allowed during incoming migration.");
1659 return;
1662 /* if there is a dump in background, we should wait until the dump
1663 * finished */
1664 if (dump_in_progress()) {
1665 error_setg(errp, "There is a dump in process, please wait.");
1666 return;
1670 * kdump-compressed format need the whole memory dumped, so paging or
1671 * filter is not supported here.
1673 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1674 (paging || has_begin || has_length)) {
1675 error_setg(errp, "kdump-compressed format doesn't support paging or "
1676 "filter");
1677 return;
1679 if (has_begin && !has_length) {
1680 error_setg(errp, QERR_MISSING_PARAMETER, "length");
1681 return;
1683 if (!has_begin && has_length) {
1684 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
1685 return;
1688 /* check whether lzo/snappy is supported */
1689 #ifndef CONFIG_LZO
1690 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1691 error_setg(errp, "kdump-lzo is not available now");
1692 return;
1694 #endif
1696 #ifndef CONFIG_SNAPPY
1697 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1698 error_setg(errp, "kdump-snappy is not available now");
1699 return;
1701 #endif
1703 #if !defined(WIN32)
1704 if (strstart(file, "fd:", &p)) {
1705 fd = monitor_get_fd(cur_mon, p, errp);
1706 if (fd == -1) {
1707 return;
1710 #endif
1712 if (strstart(file, "file:", &p)) {
1713 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1714 if (fd < 0) {
1715 error_setg_file_open(errp, errno, p);
1716 return;
1720 if (fd == -1) {
1721 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
1722 return;
1725 s = &dump_state_global;
1726 dump_state_prepare(s);
1728 dump_init(s, fd, has_format, format, paging, has_begin,
1729 begin, length, &local_err);
1730 if (local_err) {
1731 error_propagate(errp, local_err);
1732 s->status = DUMP_STATUS_FAILED;
1733 return;
1736 dump_process(s, errp);
1739 DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
1741 DumpGuestMemoryFormatList *item;
1742 DumpGuestMemoryCapability *cap =
1743 g_malloc0(sizeof(DumpGuestMemoryCapability));
1745 /* elf is always available */
1746 item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1747 cap->formats = item;
1748 item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
1750 /* kdump-zlib is always available */
1751 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1752 item = item->next;
1753 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1755 /* add new item if kdump-lzo is available */
1756 #ifdef CONFIG_LZO
1757 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1758 item = item->next;
1759 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1760 #endif
1762 /* add new item if kdump-snappy is available */
1763 #ifdef CONFIG_SNAPPY
1764 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1765 item = item->next;
1766 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1767 #endif
1769 return cap;