Merge remote-tracking branch 'remotes/rth/tags/tcg-pull-20150313' into staging
[qemu/qmp-unstable.git] / dump.c
blob9c7dad8f865af3b778589dd0847e450ba9a75b9d
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-common.h"
15 #include "elf.h"
16 #include "cpu.h"
17 #include "exec/cpu-all.h"
18 #include "exec/hwaddr.h"
19 #include "monitor/monitor.h"
20 #include "sysemu/kvm.h"
21 #include "sysemu/dump.h"
22 #include "sysemu/sysemu.h"
23 #include "sysemu/memory_mapping.h"
24 #include "sysemu/cpus.h"
25 #include "qapi/error.h"
26 #include "qmp-commands.h"
28 #include <zlib.h>
29 #ifdef CONFIG_LZO
30 #include <lzo/lzo1x.h>
31 #endif
32 #ifdef CONFIG_SNAPPY
33 #include <snappy-c.h>
34 #endif
35 #ifndef ELF_MACHINE_UNAME
36 #define ELF_MACHINE_UNAME "Unknown"
37 #endif
39 uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
41 if (s->dump_info.d_endian == ELFDATA2LSB) {
42 val = cpu_to_le16(val);
43 } else {
44 val = cpu_to_be16(val);
47 return val;
50 uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
52 if (s->dump_info.d_endian == ELFDATA2LSB) {
53 val = cpu_to_le32(val);
54 } else {
55 val = cpu_to_be32(val);
58 return val;
61 uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
63 if (s->dump_info.d_endian == ELFDATA2LSB) {
64 val = cpu_to_le64(val);
65 } else {
66 val = cpu_to_be64(val);
69 return val;
72 static int dump_cleanup(DumpState *s)
74 guest_phys_blocks_free(&s->guest_phys_blocks);
75 memory_mapping_list_free(&s->list);
76 close(s->fd);
77 if (s->resume) {
78 vm_start();
81 return 0;
84 static void dump_error(DumpState *s, const char *reason, Error **errp)
86 dump_cleanup(s);
87 error_setg(errp, "%s", reason);
90 static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
92 DumpState *s = opaque;
93 size_t written_size;
95 written_size = qemu_write_full(s->fd, buf, size);
96 if (written_size != size) {
97 return -1;
100 return 0;
103 static void write_elf64_header(DumpState *s, Error **errp)
105 Elf64_Ehdr elf_header;
106 int ret;
108 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
109 memcpy(&elf_header, ELFMAG, SELFMAG);
110 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
111 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
112 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
113 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
114 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
115 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
116 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
117 elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
118 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
119 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
120 if (s->have_section) {
121 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
123 elf_header.e_shoff = cpu_to_dump64(s, shoff);
124 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
125 elf_header.e_shnum = cpu_to_dump16(s, 1);
128 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
129 if (ret < 0) {
130 dump_error(s, "dump: failed to write elf header", errp);
134 static void write_elf32_header(DumpState *s, Error **errp)
136 Elf32_Ehdr elf_header;
137 int ret;
139 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
140 memcpy(&elf_header, ELFMAG, SELFMAG);
141 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
142 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
143 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
144 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
145 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
146 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
147 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
148 elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
149 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
150 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
151 if (s->have_section) {
152 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
154 elf_header.e_shoff = cpu_to_dump32(s, shoff);
155 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
156 elf_header.e_shnum = cpu_to_dump16(s, 1);
159 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
160 if (ret < 0) {
161 dump_error(s, "dump: failed to write elf header", errp);
165 static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
166 int phdr_index, hwaddr offset,
167 hwaddr filesz, Error **errp)
169 Elf64_Phdr phdr;
170 int ret;
172 memset(&phdr, 0, sizeof(Elf64_Phdr));
173 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
174 phdr.p_offset = cpu_to_dump64(s, offset);
175 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
176 phdr.p_filesz = cpu_to_dump64(s, filesz);
177 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
178 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr);
180 assert(memory_mapping->length >= filesz);
182 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
183 if (ret < 0) {
184 dump_error(s, "dump: failed to write program header table", errp);
188 static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
189 int phdr_index, hwaddr offset,
190 hwaddr filesz, Error **errp)
192 Elf32_Phdr phdr;
193 int ret;
195 memset(&phdr, 0, sizeof(Elf32_Phdr));
196 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
197 phdr.p_offset = cpu_to_dump32(s, offset);
198 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
199 phdr.p_filesz = cpu_to_dump32(s, filesz);
200 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
201 phdr.p_vaddr = cpu_to_dump32(s, memory_mapping->virt_addr);
203 assert(memory_mapping->length >= filesz);
205 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
206 if (ret < 0) {
207 dump_error(s, "dump: failed to write program header table", errp);
211 static void write_elf64_note(DumpState *s, Error **errp)
213 Elf64_Phdr phdr;
214 hwaddr begin = s->memory_offset - s->note_size;
215 int ret;
217 memset(&phdr, 0, sizeof(Elf64_Phdr));
218 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
219 phdr.p_offset = cpu_to_dump64(s, begin);
220 phdr.p_paddr = 0;
221 phdr.p_filesz = cpu_to_dump64(s, s->note_size);
222 phdr.p_memsz = cpu_to_dump64(s, s->note_size);
223 phdr.p_vaddr = 0;
225 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
226 if (ret < 0) {
227 dump_error(s, "dump: failed to write program header table", errp);
231 static inline int cpu_index(CPUState *cpu)
233 return cpu->cpu_index + 1;
236 static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
237 Error **errp)
239 CPUState *cpu;
240 int ret;
241 int id;
243 CPU_FOREACH(cpu) {
244 id = cpu_index(cpu);
245 ret = cpu_write_elf64_note(f, cpu, id, s);
246 if (ret < 0) {
247 dump_error(s, "dump: failed to write elf notes", errp);
248 return;
252 CPU_FOREACH(cpu) {
253 ret = cpu_write_elf64_qemunote(f, cpu, s);
254 if (ret < 0) {
255 dump_error(s, "dump: failed to write CPU status", errp);
256 return;
261 static void write_elf32_note(DumpState *s, Error **errp)
263 hwaddr begin = s->memory_offset - s->note_size;
264 Elf32_Phdr phdr;
265 int ret;
267 memset(&phdr, 0, sizeof(Elf32_Phdr));
268 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
269 phdr.p_offset = cpu_to_dump32(s, begin);
270 phdr.p_paddr = 0;
271 phdr.p_filesz = cpu_to_dump32(s, s->note_size);
272 phdr.p_memsz = cpu_to_dump32(s, s->note_size);
273 phdr.p_vaddr = 0;
275 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
276 if (ret < 0) {
277 dump_error(s, "dump: failed to write program header table", errp);
281 static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
282 Error **errp)
284 CPUState *cpu;
285 int ret;
286 int id;
288 CPU_FOREACH(cpu) {
289 id = cpu_index(cpu);
290 ret = cpu_write_elf32_note(f, cpu, id, s);
291 if (ret < 0) {
292 dump_error(s, "dump: failed to write elf notes", errp);
293 return;
297 CPU_FOREACH(cpu) {
298 ret = cpu_write_elf32_qemunote(f, cpu, s);
299 if (ret < 0) {
300 dump_error(s, "dump: failed to write CPU status", errp);
301 return;
306 static void write_elf_section(DumpState *s, int type, Error **errp)
308 Elf32_Shdr shdr32;
309 Elf64_Shdr shdr64;
310 int shdr_size;
311 void *shdr;
312 int ret;
314 if (type == 0) {
315 shdr_size = sizeof(Elf32_Shdr);
316 memset(&shdr32, 0, shdr_size);
317 shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
318 shdr = &shdr32;
319 } else {
320 shdr_size = sizeof(Elf64_Shdr);
321 memset(&shdr64, 0, shdr_size);
322 shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
323 shdr = &shdr64;
326 ret = fd_write_vmcore(&shdr, shdr_size, s);
327 if (ret < 0) {
328 dump_error(s, "dump: failed to write section header table", errp);
332 static void write_data(DumpState *s, void *buf, int length, Error **errp)
334 int ret;
336 ret = fd_write_vmcore(buf, length, s);
337 if (ret < 0) {
338 dump_error(s, "dump: failed to save memory", errp);
342 /* write the memory to vmcore. 1 page per I/O. */
343 static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
344 int64_t size, Error **errp)
346 int64_t i;
347 Error *local_err = NULL;
349 for (i = 0; i < size / TARGET_PAGE_SIZE; i++) {
350 write_data(s, block->host_addr + start + i * TARGET_PAGE_SIZE,
351 TARGET_PAGE_SIZE, &local_err);
352 if (local_err) {
353 error_propagate(errp, local_err);
354 return;
358 if ((size % TARGET_PAGE_SIZE) != 0) {
359 write_data(s, block->host_addr + start + i * TARGET_PAGE_SIZE,
360 size % TARGET_PAGE_SIZE, &local_err);
361 if (local_err) {
362 error_propagate(errp, local_err);
363 return;
368 /* get the memory's offset and size in the vmcore */
369 static void get_offset_range(hwaddr phys_addr,
370 ram_addr_t mapping_length,
371 DumpState *s,
372 hwaddr *p_offset,
373 hwaddr *p_filesz)
375 GuestPhysBlock *block;
376 hwaddr offset = s->memory_offset;
377 int64_t size_in_block, start;
379 /* When the memory is not stored into vmcore, offset will be -1 */
380 *p_offset = -1;
381 *p_filesz = 0;
383 if (s->has_filter) {
384 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
385 return;
389 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
390 if (s->has_filter) {
391 if (block->target_start >= s->begin + s->length ||
392 block->target_end <= s->begin) {
393 /* This block is out of the range */
394 continue;
397 if (s->begin <= block->target_start) {
398 start = block->target_start;
399 } else {
400 start = s->begin;
403 size_in_block = block->target_end - start;
404 if (s->begin + s->length < block->target_end) {
405 size_in_block -= block->target_end - (s->begin + s->length);
407 } else {
408 start = block->target_start;
409 size_in_block = block->target_end - block->target_start;
412 if (phys_addr >= start && phys_addr < start + size_in_block) {
413 *p_offset = phys_addr - start + offset;
415 /* The offset range mapped from the vmcore file must not spill over
416 * the GuestPhysBlock, clamp it. The rest of the mapping will be
417 * zero-filled in memory at load time; see
418 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
420 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
421 mapping_length :
422 size_in_block - (phys_addr - start);
423 return;
426 offset += size_in_block;
430 static void write_elf_loads(DumpState *s, Error **errp)
432 hwaddr offset, filesz;
433 MemoryMapping *memory_mapping;
434 uint32_t phdr_index = 1;
435 uint32_t max_index;
436 Error *local_err = NULL;
438 if (s->have_section) {
439 max_index = s->sh_info;
440 } else {
441 max_index = s->phdr_num;
444 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
445 get_offset_range(memory_mapping->phys_addr,
446 memory_mapping->length,
447 s, &offset, &filesz);
448 if (s->dump_info.d_class == ELFCLASS64) {
449 write_elf64_load(s, memory_mapping, phdr_index++, offset,
450 filesz, &local_err);
451 } else {
452 write_elf32_load(s, memory_mapping, phdr_index++, offset,
453 filesz, &local_err);
456 if (local_err) {
457 error_propagate(errp, local_err);
458 return;
461 if (phdr_index >= max_index) {
462 break;
467 /* write elf header, PT_NOTE and elf note to vmcore. */
468 static void dump_begin(DumpState *s, Error **errp)
470 Error *local_err = NULL;
473 * the vmcore's format is:
474 * --------------
475 * | elf header |
476 * --------------
477 * | PT_NOTE |
478 * --------------
479 * | PT_LOAD |
480 * --------------
481 * | ...... |
482 * --------------
483 * | PT_LOAD |
484 * --------------
485 * | sec_hdr |
486 * --------------
487 * | elf note |
488 * --------------
489 * | memory |
490 * --------------
492 * we only know where the memory is saved after we write elf note into
493 * vmcore.
496 /* write elf header to vmcore */
497 if (s->dump_info.d_class == ELFCLASS64) {
498 write_elf64_header(s, &local_err);
499 } else {
500 write_elf32_header(s, &local_err);
502 if (local_err) {
503 error_propagate(errp, local_err);
504 return;
507 if (s->dump_info.d_class == ELFCLASS64) {
508 /* write PT_NOTE to vmcore */
509 write_elf64_note(s, &local_err);
510 if (local_err) {
511 error_propagate(errp, local_err);
512 return;
515 /* write all PT_LOAD to vmcore */
516 write_elf_loads(s, &local_err);
517 if (local_err) {
518 error_propagate(errp, local_err);
519 return;
522 /* write section to vmcore */
523 if (s->have_section) {
524 write_elf_section(s, 1, &local_err);
525 if (local_err) {
526 error_propagate(errp, local_err);
527 return;
531 /* write notes to vmcore */
532 write_elf64_notes(fd_write_vmcore, s, &local_err);
533 if (local_err) {
534 error_propagate(errp, local_err);
535 return;
537 } else {
538 /* write PT_NOTE to vmcore */
539 write_elf32_note(s, &local_err);
540 if (local_err) {
541 error_propagate(errp, local_err);
542 return;
545 /* write all PT_LOAD to vmcore */
546 write_elf_loads(s, &local_err);
547 if (local_err) {
548 error_propagate(errp, local_err);
549 return;
552 /* write section to vmcore */
553 if (s->have_section) {
554 write_elf_section(s, 0, &local_err);
555 if (local_err) {
556 error_propagate(errp, local_err);
557 return;
561 /* write notes to vmcore */
562 write_elf32_notes(fd_write_vmcore, s, &local_err);
563 if (local_err) {
564 error_propagate(errp, local_err);
565 return;
570 static void dump_completed(DumpState *s)
572 dump_cleanup(s);
575 static int get_next_block(DumpState *s, GuestPhysBlock *block)
577 while (1) {
578 block = QTAILQ_NEXT(block, next);
579 if (!block) {
580 /* no more block */
581 return 1;
584 s->start = 0;
585 s->next_block = block;
586 if (s->has_filter) {
587 if (block->target_start >= s->begin + s->length ||
588 block->target_end <= s->begin) {
589 /* This block is out of the range */
590 continue;
593 if (s->begin > block->target_start) {
594 s->start = s->begin - block->target_start;
598 return 0;
602 /* write all memory to vmcore */
603 static void dump_iterate(DumpState *s, Error **errp)
605 GuestPhysBlock *block;
606 int64_t size;
607 Error *local_err = NULL;
609 do {
610 block = s->next_block;
612 size = block->target_end - block->target_start;
613 if (s->has_filter) {
614 size -= s->start;
615 if (s->begin + s->length < block->target_end) {
616 size -= block->target_end - (s->begin + s->length);
619 write_memory(s, block, s->start, size, &local_err);
620 if (local_err) {
621 error_propagate(errp, local_err);
622 return;
625 } while (!get_next_block(s, block));
627 dump_completed(s);
630 static void create_vmcore(DumpState *s, Error **errp)
632 Error *local_err = NULL;
634 dump_begin(s, &local_err);
635 if (local_err) {
636 error_propagate(errp, local_err);
637 return;
640 dump_iterate(s, errp);
643 static int write_start_flat_header(int fd)
645 MakedumpfileHeader *mh;
646 int ret = 0;
648 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
649 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
651 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
652 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
654 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
655 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
657 size_t written_size;
658 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
659 if (written_size != MAX_SIZE_MDF_HEADER) {
660 ret = -1;
663 g_free(mh);
664 return ret;
667 static int write_end_flat_header(int fd)
669 MakedumpfileDataHeader mdh;
671 mdh.offset = END_FLAG_FLAT_HEADER;
672 mdh.buf_size = END_FLAG_FLAT_HEADER;
674 size_t written_size;
675 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
676 if (written_size != sizeof(mdh)) {
677 return -1;
680 return 0;
683 static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
685 size_t written_size;
686 MakedumpfileDataHeader mdh;
688 mdh.offset = cpu_to_be64(offset);
689 mdh.buf_size = cpu_to_be64(size);
691 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
692 if (written_size != sizeof(mdh)) {
693 return -1;
696 written_size = qemu_write_full(fd, buf, size);
697 if (written_size != size) {
698 return -1;
701 return 0;
704 static int buf_write_note(const void *buf, size_t size, void *opaque)
706 DumpState *s = opaque;
708 /* note_buf is not enough */
709 if (s->note_buf_offset + size > s->note_size) {
710 return -1;
713 memcpy(s->note_buf + s->note_buf_offset, buf, size);
715 s->note_buf_offset += size;
717 return 0;
720 /* write common header, sub header and elf note to vmcore */
721 static void create_header32(DumpState *s, Error **errp)
723 DiskDumpHeader32 *dh = NULL;
724 KdumpSubHeader32 *kh = NULL;
725 size_t size;
726 uint32_t block_size;
727 uint32_t sub_hdr_size;
728 uint32_t bitmap_blocks;
729 uint32_t status = 0;
730 uint64_t offset_note;
731 Error *local_err = NULL;
733 /* write common header, the version of kdump-compressed format is 6th */
734 size = sizeof(DiskDumpHeader32);
735 dh = g_malloc0(size);
737 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
738 dh->header_version = cpu_to_dump32(s, 6);
739 block_size = TARGET_PAGE_SIZE;
740 dh->block_size = cpu_to_dump32(s, block_size);
741 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
742 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
743 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
744 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
745 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
746 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
747 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
748 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
749 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
751 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
752 status |= DUMP_DH_COMPRESSED_ZLIB;
754 #ifdef CONFIG_LZO
755 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
756 status |= DUMP_DH_COMPRESSED_LZO;
758 #endif
759 #ifdef CONFIG_SNAPPY
760 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
761 status |= DUMP_DH_COMPRESSED_SNAPPY;
763 #endif
764 dh->status = cpu_to_dump32(s, status);
766 if (write_buffer(s->fd, 0, dh, size) < 0) {
767 dump_error(s, "dump: failed to write disk dump header", errp);
768 goto out;
771 /* write sub header */
772 size = sizeof(KdumpSubHeader32);
773 kh = g_malloc0(size);
775 /* 64bit max_mapnr_64 */
776 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
777 kh->phys_base = cpu_to_dump32(s, PHYS_BASE);
778 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
780 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
781 kh->offset_note = cpu_to_dump64(s, offset_note);
782 kh->note_size = cpu_to_dump32(s, s->note_size);
784 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
785 block_size, kh, size) < 0) {
786 dump_error(s, "dump: failed to write kdump sub header", errp);
787 goto out;
790 /* write note */
791 s->note_buf = g_malloc0(s->note_size);
792 s->note_buf_offset = 0;
794 /* use s->note_buf to store notes temporarily */
795 write_elf32_notes(buf_write_note, s, &local_err);
796 if (local_err) {
797 error_propagate(errp, local_err);
798 goto out;
800 if (write_buffer(s->fd, offset_note, s->note_buf,
801 s->note_size) < 0) {
802 dump_error(s, "dump: failed to write notes", errp);
803 goto out;
806 /* get offset of dump_bitmap */
807 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
808 block_size;
810 /* get offset of page */
811 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
812 block_size;
814 out:
815 g_free(dh);
816 g_free(kh);
817 g_free(s->note_buf);
820 /* write common header, sub header and elf note to vmcore */
821 static void create_header64(DumpState *s, Error **errp)
823 DiskDumpHeader64 *dh = NULL;
824 KdumpSubHeader64 *kh = NULL;
825 size_t size;
826 uint32_t block_size;
827 uint32_t sub_hdr_size;
828 uint32_t bitmap_blocks;
829 uint32_t status = 0;
830 uint64_t offset_note;
831 Error *local_err = NULL;
833 /* write common header, the version of kdump-compressed format is 6th */
834 size = sizeof(DiskDumpHeader64);
835 dh = g_malloc0(size);
837 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
838 dh->header_version = cpu_to_dump32(s, 6);
839 block_size = TARGET_PAGE_SIZE;
840 dh->block_size = cpu_to_dump32(s, block_size);
841 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
842 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
843 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
844 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
845 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
846 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
847 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
848 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
849 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
851 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
852 status |= DUMP_DH_COMPRESSED_ZLIB;
854 #ifdef CONFIG_LZO
855 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
856 status |= DUMP_DH_COMPRESSED_LZO;
858 #endif
859 #ifdef CONFIG_SNAPPY
860 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
861 status |= DUMP_DH_COMPRESSED_SNAPPY;
863 #endif
864 dh->status = cpu_to_dump32(s, status);
866 if (write_buffer(s->fd, 0, dh, size) < 0) {
867 dump_error(s, "dump: failed to write disk dump header", errp);
868 goto out;
871 /* write sub header */
872 size = sizeof(KdumpSubHeader64);
873 kh = g_malloc0(size);
875 /* 64bit max_mapnr_64 */
876 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
877 kh->phys_base = cpu_to_dump64(s, PHYS_BASE);
878 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
880 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
881 kh->offset_note = cpu_to_dump64(s, offset_note);
882 kh->note_size = cpu_to_dump64(s, s->note_size);
884 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
885 block_size, kh, size) < 0) {
886 dump_error(s, "dump: failed to write kdump sub header", errp);
887 goto out;
890 /* write note */
891 s->note_buf = g_malloc0(s->note_size);
892 s->note_buf_offset = 0;
894 /* use s->note_buf to store notes temporarily */
895 write_elf64_notes(buf_write_note, s, &local_err);
896 if (local_err) {
897 error_propagate(errp, local_err);
898 goto out;
901 if (write_buffer(s->fd, offset_note, s->note_buf,
902 s->note_size) < 0) {
903 dump_error(s, "dump: failed to write notes", errp);
904 goto out;
907 /* get offset of dump_bitmap */
908 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
909 block_size;
911 /* get offset of page */
912 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
913 block_size;
915 out:
916 g_free(dh);
917 g_free(kh);
918 g_free(s->note_buf);
921 static void write_dump_header(DumpState *s, Error **errp)
923 Error *local_err = NULL;
925 if (s->dump_info.d_class == ELFCLASS32) {
926 create_header32(s, &local_err);
927 } else {
928 create_header64(s, &local_err);
930 if (local_err) {
931 error_propagate(errp, local_err);
936 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
937 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
938 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
939 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
940 * vmcore, ie. synchronizing un-sync bit into vmcore.
942 static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
943 uint8_t *buf, DumpState *s)
945 off_t old_offset, new_offset;
946 off_t offset_bitmap1, offset_bitmap2;
947 uint32_t byte, bit;
949 /* should not set the previous place */
950 assert(last_pfn <= pfn);
953 * if the bit needed to be set is not cached in buf, flush the data in buf
954 * to vmcore firstly.
955 * making new_offset be bigger than old_offset can also sync remained data
956 * into vmcore.
958 old_offset = BUFSIZE_BITMAP * (last_pfn / PFN_BUFBITMAP);
959 new_offset = BUFSIZE_BITMAP * (pfn / PFN_BUFBITMAP);
961 while (old_offset < new_offset) {
962 /* calculate the offset and write dump_bitmap */
963 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
964 if (write_buffer(s->fd, offset_bitmap1, buf,
965 BUFSIZE_BITMAP) < 0) {
966 return -1;
969 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
970 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
971 old_offset;
972 if (write_buffer(s->fd, offset_bitmap2, buf,
973 BUFSIZE_BITMAP) < 0) {
974 return -1;
977 memset(buf, 0, BUFSIZE_BITMAP);
978 old_offset += BUFSIZE_BITMAP;
981 /* get the exact place of the bit in the buf, and set it */
982 byte = (pfn % PFN_BUFBITMAP) / CHAR_BIT;
983 bit = (pfn % PFN_BUFBITMAP) % CHAR_BIT;
984 if (value) {
985 buf[byte] |= 1u << bit;
986 } else {
987 buf[byte] &= ~(1u << bit);
990 return 0;
994 * exam every page and return the page frame number and the address of the page.
995 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
996 * blocks, so block->target_start and block->target_end should be interal
997 * multiples of the target page size.
999 static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1000 uint8_t **bufptr, DumpState *s)
1002 GuestPhysBlock *block = *blockptr;
1003 hwaddr addr;
1004 uint8_t *buf;
1006 /* block == NULL means the start of the iteration */
1007 if (!block) {
1008 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1009 *blockptr = block;
1010 assert((block->target_start & ~TARGET_PAGE_MASK) == 0);
1011 assert((block->target_end & ~TARGET_PAGE_MASK) == 0);
1012 *pfnptr = paddr_to_pfn(block->target_start);
1013 if (bufptr) {
1014 *bufptr = block->host_addr;
1016 return true;
1019 *pfnptr = *pfnptr + 1;
1020 addr = pfn_to_paddr(*pfnptr);
1022 if ((addr >= block->target_start) &&
1023 (addr + TARGET_PAGE_SIZE <= block->target_end)) {
1024 buf = block->host_addr + (addr - block->target_start);
1025 } else {
1026 /* the next page is in the next block */
1027 block = QTAILQ_NEXT(block, next);
1028 *blockptr = block;
1029 if (!block) {
1030 return false;
1032 assert((block->target_start & ~TARGET_PAGE_MASK) == 0);
1033 assert((block->target_end & ~TARGET_PAGE_MASK) == 0);
1034 *pfnptr = paddr_to_pfn(block->target_start);
1035 buf = block->host_addr;
1038 if (bufptr) {
1039 *bufptr = buf;
1042 return true;
1045 static void write_dump_bitmap(DumpState *s, Error **errp)
1047 int ret = 0;
1048 uint64_t last_pfn, pfn;
1049 void *dump_bitmap_buf;
1050 size_t num_dumpable;
1051 GuestPhysBlock *block_iter = NULL;
1053 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1054 dump_bitmap_buf = g_malloc0(BUFSIZE_BITMAP);
1056 num_dumpable = 0;
1057 last_pfn = 0;
1060 * exam memory page by page, and set the bit in dump_bitmap corresponded
1061 * to the existing page.
1063 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1064 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1065 if (ret < 0) {
1066 dump_error(s, "dump: failed to set dump_bitmap", errp);
1067 goto out;
1070 last_pfn = pfn;
1071 num_dumpable++;
1075 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1076 * set last_pfn + PFN_BUFBITMAP to 0 and those set but un-sync bit will be
1077 * synchronized into vmcore.
1079 if (num_dumpable > 0) {
1080 ret = set_dump_bitmap(last_pfn, last_pfn + PFN_BUFBITMAP, false,
1081 dump_bitmap_buf, s);
1082 if (ret < 0) {
1083 dump_error(s, "dump: failed to sync dump_bitmap", errp);
1084 goto out;
1088 /* number of dumpable pages that will be dumped later */
1089 s->num_dumpable = num_dumpable;
1091 out:
1092 g_free(dump_bitmap_buf);
1095 static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1096 off_t offset)
1098 data_cache->fd = s->fd;
1099 data_cache->data_size = 0;
1100 data_cache->buf_size = BUFSIZE_DATA_CACHE;
1101 data_cache->buf = g_malloc0(BUFSIZE_DATA_CACHE);
1102 data_cache->offset = offset;
1105 static int write_cache(DataCache *dc, const void *buf, size_t size,
1106 bool flag_sync)
1109 * dc->buf_size should not be less than size, otherwise dc will never be
1110 * enough
1112 assert(size <= dc->buf_size);
1115 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1116 * otherwise check if the space is enough for caching data in buf, if not,
1117 * write the data in dc->buf to dc->fd and reset dc->buf
1119 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1120 (flag_sync && dc->data_size > 0)) {
1121 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1122 return -1;
1125 dc->offset += dc->data_size;
1126 dc->data_size = 0;
1129 if (!flag_sync) {
1130 memcpy(dc->buf + dc->data_size, buf, size);
1131 dc->data_size += size;
1134 return 0;
1137 static void free_data_cache(DataCache *data_cache)
1139 g_free(data_cache->buf);
1142 static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1144 switch (flag_compress) {
1145 case DUMP_DH_COMPRESSED_ZLIB:
1146 return compressBound(page_size);
1148 case DUMP_DH_COMPRESSED_LZO:
1150 * LZO will expand incompressible data by a little amount. Please check
1151 * the following URL to see the expansion calculation:
1152 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1154 return page_size + page_size / 16 + 64 + 3;
1156 #ifdef CONFIG_SNAPPY
1157 case DUMP_DH_COMPRESSED_SNAPPY:
1158 return snappy_max_compressed_length(page_size);
1159 #endif
1161 return 0;
1165 * check if the page is all 0
1167 static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1169 return buffer_is_zero(buf, page_size);
1172 static void write_dump_pages(DumpState *s, Error **errp)
1174 int ret = 0;
1175 DataCache page_desc, page_data;
1176 size_t len_buf_out, size_out;
1177 #ifdef CONFIG_LZO
1178 lzo_bytep wrkmem = NULL;
1179 #endif
1180 uint8_t *buf_out = NULL;
1181 off_t offset_desc, offset_data;
1182 PageDescriptor pd, pd_zero;
1183 uint8_t *buf;
1184 GuestPhysBlock *block_iter = NULL;
1185 uint64_t pfn_iter;
1187 /* get offset of page_desc and page_data in dump file */
1188 offset_desc = s->offset_page;
1189 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1191 prepare_data_cache(&page_desc, s, offset_desc);
1192 prepare_data_cache(&page_data, s, offset_data);
1194 /* prepare buffer to store compressed data */
1195 len_buf_out = get_len_buf_out(TARGET_PAGE_SIZE, s->flag_compress);
1196 assert(len_buf_out != 0);
1198 #ifdef CONFIG_LZO
1199 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1200 #endif
1202 buf_out = g_malloc(len_buf_out);
1205 * init zero page's page_desc and page_data, because every zero page
1206 * uses the same page_data
1208 pd_zero.size = cpu_to_dump32(s, TARGET_PAGE_SIZE);
1209 pd_zero.flags = cpu_to_dump32(s, 0);
1210 pd_zero.offset = cpu_to_dump64(s, offset_data);
1211 pd_zero.page_flags = cpu_to_dump64(s, 0);
1212 buf = g_malloc0(TARGET_PAGE_SIZE);
1213 ret = write_cache(&page_data, buf, TARGET_PAGE_SIZE, false);
1214 g_free(buf);
1215 if (ret < 0) {
1216 dump_error(s, "dump: failed to write page data (zero page)", errp);
1217 goto out;
1220 offset_data += TARGET_PAGE_SIZE;
1223 * dump memory to vmcore page by page. zero page will all be resided in the
1224 * first page of page section
1226 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1227 /* check zero page */
1228 if (is_zero_page(buf, TARGET_PAGE_SIZE)) {
1229 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1230 false);
1231 if (ret < 0) {
1232 dump_error(s, "dump: failed to write page desc", errp);
1233 goto out;
1235 } else {
1237 * not zero page, then:
1238 * 1. compress the page
1239 * 2. write the compressed page into the cache of page_data
1240 * 3. get page desc of the compressed page and write it into the
1241 * cache of page_desc
1243 * only one compression format will be used here, for
1244 * s->flag_compress is set. But when compression fails to work,
1245 * we fall back to save in plaintext.
1247 size_out = len_buf_out;
1248 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
1249 (compress2(buf_out, (uLongf *)&size_out, buf,
1250 TARGET_PAGE_SIZE, Z_BEST_SPEED) == Z_OK) &&
1251 (size_out < TARGET_PAGE_SIZE)) {
1252 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1253 pd.size = cpu_to_dump32(s, size_out);
1255 ret = write_cache(&page_data, buf_out, size_out, false);
1256 if (ret < 0) {
1257 dump_error(s, "dump: failed to write page data", errp);
1258 goto out;
1260 #ifdef CONFIG_LZO
1261 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
1262 (lzo1x_1_compress(buf, TARGET_PAGE_SIZE, buf_out,
1263 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
1264 (size_out < TARGET_PAGE_SIZE)) {
1265 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1266 pd.size = cpu_to_dump32(s, size_out);
1268 ret = write_cache(&page_data, buf_out, size_out, false);
1269 if (ret < 0) {
1270 dump_error(s, "dump: failed to write page data", errp);
1271 goto out;
1273 #endif
1274 #ifdef CONFIG_SNAPPY
1275 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
1276 (snappy_compress((char *)buf, TARGET_PAGE_SIZE,
1277 (char *)buf_out, &size_out) == SNAPPY_OK) &&
1278 (size_out < TARGET_PAGE_SIZE)) {
1279 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1280 pd.size = cpu_to_dump32(s, size_out);
1282 ret = write_cache(&page_data, buf_out, size_out, false);
1283 if (ret < 0) {
1284 dump_error(s, "dump: failed to write page data", errp);
1285 goto out;
1287 #endif
1288 } else {
1290 * fall back to save in plaintext, size_out should be
1291 * assigned TARGET_PAGE_SIZE
1293 pd.flags = cpu_to_dump32(s, 0);
1294 size_out = TARGET_PAGE_SIZE;
1295 pd.size = cpu_to_dump32(s, size_out);
1297 ret = write_cache(&page_data, buf, TARGET_PAGE_SIZE, false);
1298 if (ret < 0) {
1299 dump_error(s, "dump: failed to write page data", errp);
1300 goto out;
1304 /* get and write page desc here */
1305 pd.page_flags = cpu_to_dump64(s, 0);
1306 pd.offset = cpu_to_dump64(s, offset_data);
1307 offset_data += size_out;
1309 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1310 if (ret < 0) {
1311 dump_error(s, "dump: failed to write page desc", errp);
1312 goto out;
1317 ret = write_cache(&page_desc, NULL, 0, true);
1318 if (ret < 0) {
1319 dump_error(s, "dump: failed to sync cache for page_desc", errp);
1320 goto out;
1322 ret = write_cache(&page_data, NULL, 0, true);
1323 if (ret < 0) {
1324 dump_error(s, "dump: failed to sync cache for page_data", errp);
1325 goto out;
1328 out:
1329 free_data_cache(&page_desc);
1330 free_data_cache(&page_data);
1332 #ifdef CONFIG_LZO
1333 g_free(wrkmem);
1334 #endif
1336 g_free(buf_out);
1339 static void create_kdump_vmcore(DumpState *s, Error **errp)
1341 int ret;
1342 Error *local_err = NULL;
1345 * the kdump-compressed format is:
1346 * File offset
1347 * +------------------------------------------+ 0x0
1348 * | main header (struct disk_dump_header) |
1349 * |------------------------------------------+ block 1
1350 * | sub header (struct kdump_sub_header) |
1351 * |------------------------------------------+ block 2
1352 * | 1st-dump_bitmap |
1353 * |------------------------------------------+ block 2 + X blocks
1354 * | 2nd-dump_bitmap | (aligned by block)
1355 * |------------------------------------------+ block 2 + 2 * X blocks
1356 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1357 * | page desc for pfn 1 (struct page_desc) |
1358 * | : |
1359 * |------------------------------------------| (not aligned by block)
1360 * | page data (pfn 0) |
1361 * | page data (pfn 1) |
1362 * | : |
1363 * +------------------------------------------+
1366 ret = write_start_flat_header(s->fd);
1367 if (ret < 0) {
1368 dump_error(s, "dump: failed to write start flat header", errp);
1369 return;
1372 write_dump_header(s, &local_err);
1373 if (local_err) {
1374 error_propagate(errp, local_err);
1375 return;
1378 write_dump_bitmap(s, &local_err);
1379 if (local_err) {
1380 error_propagate(errp, local_err);
1381 return;
1384 write_dump_pages(s, &local_err);
1385 if (local_err) {
1386 error_propagate(errp, local_err);
1387 return;
1390 ret = write_end_flat_header(s->fd);
1391 if (ret < 0) {
1392 dump_error(s, "dump: failed to write end flat header", errp);
1393 return;
1396 dump_completed(s);
1399 static ram_addr_t get_start_block(DumpState *s)
1401 GuestPhysBlock *block;
1403 if (!s->has_filter) {
1404 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1405 return 0;
1408 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1409 if (block->target_start >= s->begin + s->length ||
1410 block->target_end <= s->begin) {
1411 /* This block is out of the range */
1412 continue;
1415 s->next_block = block;
1416 if (s->begin > block->target_start) {
1417 s->start = s->begin - block->target_start;
1418 } else {
1419 s->start = 0;
1421 return s->start;
1424 return -1;
1427 static void get_max_mapnr(DumpState *s)
1429 GuestPhysBlock *last_block;
1431 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead);
1432 s->max_mapnr = paddr_to_pfn(last_block->target_end);
1435 static void dump_init(DumpState *s, int fd, bool has_format,
1436 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1437 int64_t begin, int64_t length, Error **errp)
1439 CPUState *cpu;
1440 int nr_cpus;
1441 Error *err = NULL;
1442 int ret;
1444 /* kdump-compressed is conflict with paging and filter */
1445 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1446 assert(!paging && !has_filter);
1449 if (runstate_is_running()) {
1450 vm_stop(RUN_STATE_SAVE_VM);
1451 s->resume = true;
1452 } else {
1453 s->resume = false;
1456 /* If we use KVM, we should synchronize the registers before we get dump
1457 * info or physmap info.
1459 cpu_synchronize_all_states();
1460 nr_cpus = 0;
1461 CPU_FOREACH(cpu) {
1462 nr_cpus++;
1465 s->fd = fd;
1466 s->has_filter = has_filter;
1467 s->begin = begin;
1468 s->length = length;
1470 memory_mapping_list_init(&s->list);
1472 guest_phys_blocks_init(&s->guest_phys_blocks);
1473 guest_phys_blocks_append(&s->guest_phys_blocks);
1475 s->start = get_start_block(s);
1476 if (s->start == -1) {
1477 error_set(errp, QERR_INVALID_PARAMETER, "begin");
1478 goto cleanup;
1481 /* get dump info: endian, class and architecture.
1482 * If the target architecture is not supported, cpu_get_dump_info() will
1483 * return -1.
1485 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
1486 if (ret < 0) {
1487 error_set(errp, QERR_UNSUPPORTED);
1488 goto cleanup;
1491 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1492 s->dump_info.d_machine, nr_cpus);
1493 if (s->note_size < 0) {
1494 error_set(errp, QERR_UNSUPPORTED);
1495 goto cleanup;
1498 /* get memory mapping */
1499 if (paging) {
1500 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
1501 if (err != NULL) {
1502 error_propagate(errp, err);
1503 goto cleanup;
1505 } else {
1506 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
1509 s->nr_cpus = nr_cpus;
1511 get_max_mapnr(s);
1513 uint64_t tmp;
1514 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), TARGET_PAGE_SIZE);
1515 s->len_dump_bitmap = tmp * TARGET_PAGE_SIZE;
1517 /* init for kdump-compressed format */
1518 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1519 switch (format) {
1520 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1521 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1522 break;
1524 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
1525 #ifdef CONFIG_LZO
1526 if (lzo_init() != LZO_E_OK) {
1527 error_setg(errp, "failed to initialize the LZO library");
1528 goto cleanup;
1530 #endif
1531 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1532 break;
1534 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1535 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1536 break;
1538 default:
1539 s->flag_compress = 0;
1542 return;
1545 if (s->has_filter) {
1546 memory_mapping_filter(&s->list, s->begin, s->length);
1550 * calculate phdr_num
1552 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1554 s->phdr_num = 1; /* PT_NOTE */
1555 if (s->list.num < UINT16_MAX - 2) {
1556 s->phdr_num += s->list.num;
1557 s->have_section = false;
1558 } else {
1559 s->have_section = true;
1560 s->phdr_num = PN_XNUM;
1561 s->sh_info = 1; /* PT_NOTE */
1563 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1564 if (s->list.num <= UINT32_MAX - 1) {
1565 s->sh_info += s->list.num;
1566 } else {
1567 s->sh_info = UINT32_MAX;
1571 if (s->dump_info.d_class == ELFCLASS64) {
1572 if (s->have_section) {
1573 s->memory_offset = sizeof(Elf64_Ehdr) +
1574 sizeof(Elf64_Phdr) * s->sh_info +
1575 sizeof(Elf64_Shdr) + s->note_size;
1576 } else {
1577 s->memory_offset = sizeof(Elf64_Ehdr) +
1578 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1580 } else {
1581 if (s->have_section) {
1582 s->memory_offset = sizeof(Elf32_Ehdr) +
1583 sizeof(Elf32_Phdr) * s->sh_info +
1584 sizeof(Elf32_Shdr) + s->note_size;
1585 } else {
1586 s->memory_offset = sizeof(Elf32_Ehdr) +
1587 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1591 return;
1593 cleanup:
1594 dump_cleanup(s);
1597 void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin,
1598 int64_t begin, bool has_length,
1599 int64_t length, bool has_format,
1600 DumpGuestMemoryFormat format, Error **errp)
1602 const char *p;
1603 int fd = -1;
1604 DumpState *s;
1605 Error *local_err = NULL;
1608 * kdump-compressed format need the whole memory dumped, so paging or
1609 * filter is not supported here.
1611 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1612 (paging || has_begin || has_length)) {
1613 error_setg(errp, "kdump-compressed format doesn't support paging or "
1614 "filter");
1615 return;
1617 if (has_begin && !has_length) {
1618 error_set(errp, QERR_MISSING_PARAMETER, "length");
1619 return;
1621 if (!has_begin && has_length) {
1622 error_set(errp, QERR_MISSING_PARAMETER, "begin");
1623 return;
1626 /* check whether lzo/snappy is supported */
1627 #ifndef CONFIG_LZO
1628 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1629 error_setg(errp, "kdump-lzo is not available now");
1630 return;
1632 #endif
1634 #ifndef CONFIG_SNAPPY
1635 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1636 error_setg(errp, "kdump-snappy is not available now");
1637 return;
1639 #endif
1641 #if !defined(WIN32)
1642 if (strstart(file, "fd:", &p)) {
1643 fd = monitor_get_fd(cur_mon, p, errp);
1644 if (fd == -1) {
1645 return;
1648 #endif
1650 if (strstart(file, "file:", &p)) {
1651 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1652 if (fd < 0) {
1653 error_setg_file_open(errp, errno, p);
1654 return;
1658 if (fd == -1) {
1659 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
1660 return;
1663 s = g_malloc0(sizeof(DumpState));
1665 dump_init(s, fd, has_format, format, paging, has_begin,
1666 begin, length, &local_err);
1667 if (local_err) {
1668 g_free(s);
1669 error_propagate(errp, local_err);
1670 return;
1673 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1674 create_kdump_vmcore(s, errp);
1675 } else {
1676 create_vmcore(s, errp);
1679 g_free(s);
1682 DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
1684 DumpGuestMemoryFormatList *item;
1685 DumpGuestMemoryCapability *cap =
1686 g_malloc0(sizeof(DumpGuestMemoryCapability));
1688 /* elf is always available */
1689 item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1690 cap->formats = item;
1691 item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
1693 /* kdump-zlib is always available */
1694 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1695 item = item->next;
1696 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1698 /* add new item if kdump-lzo is available */
1699 #ifdef CONFIG_LZO
1700 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1701 item = item->next;
1702 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1703 #endif
1705 /* add new item if kdump-snappy is available */
1706 #ifdef CONFIG_SNAPPY
1707 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1708 item = item->next;
1709 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1710 #endif
1712 return cap;