ahci: Update byte count after DMA completion
[qemu/ar7.git] / dump.c
blob06a49155a2d4a593784edd2bce86ed72f8d16261
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 int ret;
608 Error *local_err = NULL;
610 while (1) {
611 block = s->next_block;
613 size = block->target_end - block->target_start;
614 if (s->has_filter) {
615 size -= s->start;
616 if (s->begin + s->length < block->target_end) {
617 size -= block->target_end - (s->begin + s->length);
620 write_memory(s, block, s->start, size, &local_err);
621 if (local_err) {
622 error_propagate(errp, local_err);
623 return;
626 ret = get_next_block(s, block);
627 if (ret == 1) {
628 dump_completed(s);
633 static void create_vmcore(DumpState *s, Error **errp)
635 Error *local_err = NULL;
637 dump_begin(s, &local_err);
638 if (local_err) {
639 error_propagate(errp, local_err);
640 return;
643 dump_iterate(s, errp);
646 static int write_start_flat_header(int fd)
648 MakedumpfileHeader *mh;
649 int ret = 0;
651 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
652 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
654 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
655 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
657 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
658 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
660 size_t written_size;
661 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
662 if (written_size != MAX_SIZE_MDF_HEADER) {
663 ret = -1;
666 g_free(mh);
667 return ret;
670 static int write_end_flat_header(int fd)
672 MakedumpfileDataHeader mdh;
674 mdh.offset = END_FLAG_FLAT_HEADER;
675 mdh.buf_size = END_FLAG_FLAT_HEADER;
677 size_t written_size;
678 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
679 if (written_size != sizeof(mdh)) {
680 return -1;
683 return 0;
686 static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
688 size_t written_size;
689 MakedumpfileDataHeader mdh;
691 mdh.offset = cpu_to_be64(offset);
692 mdh.buf_size = cpu_to_be64(size);
694 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
695 if (written_size != sizeof(mdh)) {
696 return -1;
699 written_size = qemu_write_full(fd, buf, size);
700 if (written_size != size) {
701 return -1;
704 return 0;
707 static int buf_write_note(const void *buf, size_t size, void *opaque)
709 DumpState *s = opaque;
711 /* note_buf is not enough */
712 if (s->note_buf_offset + size > s->note_size) {
713 return -1;
716 memcpy(s->note_buf + s->note_buf_offset, buf, size);
718 s->note_buf_offset += size;
720 return 0;
723 /* write common header, sub header and elf note to vmcore */
724 static void create_header32(DumpState *s, Error **errp)
726 DiskDumpHeader32 *dh = NULL;
727 KdumpSubHeader32 *kh = NULL;
728 size_t size;
729 uint32_t block_size;
730 uint32_t sub_hdr_size;
731 uint32_t bitmap_blocks;
732 uint32_t status = 0;
733 uint64_t offset_note;
734 Error *local_err = NULL;
736 /* write common header, the version of kdump-compressed format is 6th */
737 size = sizeof(DiskDumpHeader32);
738 dh = g_malloc0(size);
740 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
741 dh->header_version = cpu_to_dump32(s, 6);
742 block_size = TARGET_PAGE_SIZE;
743 dh->block_size = cpu_to_dump32(s, block_size);
744 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
745 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
746 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
747 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
748 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
749 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
750 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
751 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
752 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
754 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
755 status |= DUMP_DH_COMPRESSED_ZLIB;
757 #ifdef CONFIG_LZO
758 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
759 status |= DUMP_DH_COMPRESSED_LZO;
761 #endif
762 #ifdef CONFIG_SNAPPY
763 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
764 status |= DUMP_DH_COMPRESSED_SNAPPY;
766 #endif
767 dh->status = cpu_to_dump32(s, status);
769 if (write_buffer(s->fd, 0, dh, size) < 0) {
770 dump_error(s, "dump: failed to write disk dump header", errp);
771 goto out;
774 /* write sub header */
775 size = sizeof(KdumpSubHeader32);
776 kh = g_malloc0(size);
778 /* 64bit max_mapnr_64 */
779 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
780 kh->phys_base = cpu_to_dump32(s, PHYS_BASE);
781 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
783 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
784 kh->offset_note = cpu_to_dump64(s, offset_note);
785 kh->note_size = cpu_to_dump32(s, s->note_size);
787 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
788 block_size, kh, size) < 0) {
789 dump_error(s, "dump: failed to write kdump sub header", errp);
790 goto out;
793 /* write note */
794 s->note_buf = g_malloc0(s->note_size);
795 s->note_buf_offset = 0;
797 /* use s->note_buf to store notes temporarily */
798 write_elf32_notes(buf_write_note, s, &local_err);
799 if (local_err) {
800 error_propagate(errp, local_err);
801 goto out;
803 if (write_buffer(s->fd, offset_note, s->note_buf,
804 s->note_size) < 0) {
805 dump_error(s, "dump: failed to write notes", errp);
806 goto out;
809 /* get offset of dump_bitmap */
810 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
811 block_size;
813 /* get offset of page */
814 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
815 block_size;
817 out:
818 g_free(dh);
819 g_free(kh);
820 g_free(s->note_buf);
823 /* write common header, sub header and elf note to vmcore */
824 static void create_header64(DumpState *s, Error **errp)
826 DiskDumpHeader64 *dh = NULL;
827 KdumpSubHeader64 *kh = NULL;
828 size_t size;
829 uint32_t block_size;
830 uint32_t sub_hdr_size;
831 uint32_t bitmap_blocks;
832 uint32_t status = 0;
833 uint64_t offset_note;
834 Error *local_err = NULL;
836 /* write common header, the version of kdump-compressed format is 6th */
837 size = sizeof(DiskDumpHeader64);
838 dh = g_malloc0(size);
840 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
841 dh->header_version = cpu_to_dump32(s, 6);
842 block_size = TARGET_PAGE_SIZE;
843 dh->block_size = cpu_to_dump32(s, block_size);
844 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
845 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
846 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
847 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
848 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
849 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
850 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
851 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
852 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
854 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
855 status |= DUMP_DH_COMPRESSED_ZLIB;
857 #ifdef CONFIG_LZO
858 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
859 status |= DUMP_DH_COMPRESSED_LZO;
861 #endif
862 #ifdef CONFIG_SNAPPY
863 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
864 status |= DUMP_DH_COMPRESSED_SNAPPY;
866 #endif
867 dh->status = cpu_to_dump32(s, status);
869 if (write_buffer(s->fd, 0, dh, size) < 0) {
870 dump_error(s, "dump: failed to write disk dump header", errp);
871 goto out;
874 /* write sub header */
875 size = sizeof(KdumpSubHeader64);
876 kh = g_malloc0(size);
878 /* 64bit max_mapnr_64 */
879 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
880 kh->phys_base = cpu_to_dump64(s, PHYS_BASE);
881 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
883 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
884 kh->offset_note = cpu_to_dump64(s, offset_note);
885 kh->note_size = cpu_to_dump64(s, s->note_size);
887 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
888 block_size, kh, size) < 0) {
889 dump_error(s, "dump: failed to write kdump sub header", errp);
890 goto out;
893 /* write note */
894 s->note_buf = g_malloc0(s->note_size);
895 s->note_buf_offset = 0;
897 /* use s->note_buf to store notes temporarily */
898 write_elf64_notes(buf_write_note, s, &local_err);
899 if (local_err) {
900 error_propagate(errp, local_err);
901 goto out;
904 if (write_buffer(s->fd, offset_note, s->note_buf,
905 s->note_size) < 0) {
906 dump_error(s, "dump: failed to write notes", errp);
907 goto out;
910 /* get offset of dump_bitmap */
911 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
912 block_size;
914 /* get offset of page */
915 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
916 block_size;
918 out:
919 g_free(dh);
920 g_free(kh);
921 g_free(s->note_buf);
924 static void write_dump_header(DumpState *s, Error **errp)
926 Error *local_err = NULL;
928 if (s->dump_info.d_class == ELFCLASS32) {
929 create_header32(s, &local_err);
930 } else {
931 create_header64(s, &local_err);
933 if (local_err) {
934 error_propagate(errp, local_err);
939 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
940 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
941 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
942 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
943 * vmcore, ie. synchronizing un-sync bit into vmcore.
945 static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
946 uint8_t *buf, DumpState *s)
948 off_t old_offset, new_offset;
949 off_t offset_bitmap1, offset_bitmap2;
950 uint32_t byte, bit;
952 /* should not set the previous place */
953 assert(last_pfn <= pfn);
956 * if the bit needed to be set is not cached in buf, flush the data in buf
957 * to vmcore firstly.
958 * making new_offset be bigger than old_offset can also sync remained data
959 * into vmcore.
961 old_offset = BUFSIZE_BITMAP * (last_pfn / PFN_BUFBITMAP);
962 new_offset = BUFSIZE_BITMAP * (pfn / PFN_BUFBITMAP);
964 while (old_offset < new_offset) {
965 /* calculate the offset and write dump_bitmap */
966 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
967 if (write_buffer(s->fd, offset_bitmap1, buf,
968 BUFSIZE_BITMAP) < 0) {
969 return -1;
972 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
973 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
974 old_offset;
975 if (write_buffer(s->fd, offset_bitmap2, buf,
976 BUFSIZE_BITMAP) < 0) {
977 return -1;
980 memset(buf, 0, BUFSIZE_BITMAP);
981 old_offset += BUFSIZE_BITMAP;
984 /* get the exact place of the bit in the buf, and set it */
985 byte = (pfn % PFN_BUFBITMAP) / CHAR_BIT;
986 bit = (pfn % PFN_BUFBITMAP) % CHAR_BIT;
987 if (value) {
988 buf[byte] |= 1u << bit;
989 } else {
990 buf[byte] &= ~(1u << bit);
993 return 0;
997 * exam every page and return the page frame number and the address of the page.
998 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
999 * blocks, so block->target_start and block->target_end should be interal
1000 * multiples of the target page size.
1002 static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1003 uint8_t **bufptr, DumpState *s)
1005 GuestPhysBlock *block = *blockptr;
1006 hwaddr addr;
1007 uint8_t *buf;
1009 /* block == NULL means the start of the iteration */
1010 if (!block) {
1011 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1012 *blockptr = block;
1013 assert((block->target_start & ~TARGET_PAGE_MASK) == 0);
1014 assert((block->target_end & ~TARGET_PAGE_MASK) == 0);
1015 *pfnptr = paddr_to_pfn(block->target_start);
1016 if (bufptr) {
1017 *bufptr = block->host_addr;
1019 return true;
1022 *pfnptr = *pfnptr + 1;
1023 addr = pfn_to_paddr(*pfnptr);
1025 if ((addr >= block->target_start) &&
1026 (addr + TARGET_PAGE_SIZE <= block->target_end)) {
1027 buf = block->host_addr + (addr - block->target_start);
1028 } else {
1029 /* the next page is in the next block */
1030 block = QTAILQ_NEXT(block, next);
1031 *blockptr = block;
1032 if (!block) {
1033 return false;
1035 assert((block->target_start & ~TARGET_PAGE_MASK) == 0);
1036 assert((block->target_end & ~TARGET_PAGE_MASK) == 0);
1037 *pfnptr = paddr_to_pfn(block->target_start);
1038 buf = block->host_addr;
1041 if (bufptr) {
1042 *bufptr = buf;
1045 return true;
1048 static void write_dump_bitmap(DumpState *s, Error **errp)
1050 int ret = 0;
1051 uint64_t last_pfn, pfn;
1052 void *dump_bitmap_buf;
1053 size_t num_dumpable;
1054 GuestPhysBlock *block_iter = NULL;
1056 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1057 dump_bitmap_buf = g_malloc0(BUFSIZE_BITMAP);
1059 num_dumpable = 0;
1060 last_pfn = 0;
1063 * exam memory page by page, and set the bit in dump_bitmap corresponded
1064 * to the existing page.
1066 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1067 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1068 if (ret < 0) {
1069 dump_error(s, "dump: failed to set dump_bitmap", errp);
1070 goto out;
1073 last_pfn = pfn;
1074 num_dumpable++;
1078 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1079 * set last_pfn + PFN_BUFBITMAP to 0 and those set but un-sync bit will be
1080 * synchronized into vmcore.
1082 if (num_dumpable > 0) {
1083 ret = set_dump_bitmap(last_pfn, last_pfn + PFN_BUFBITMAP, false,
1084 dump_bitmap_buf, s);
1085 if (ret < 0) {
1086 dump_error(s, "dump: failed to sync dump_bitmap", errp);
1087 goto out;
1091 /* number of dumpable pages that will be dumped later */
1092 s->num_dumpable = num_dumpable;
1094 out:
1095 g_free(dump_bitmap_buf);
1098 static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1099 off_t offset)
1101 data_cache->fd = s->fd;
1102 data_cache->data_size = 0;
1103 data_cache->buf_size = BUFSIZE_DATA_CACHE;
1104 data_cache->buf = g_malloc0(BUFSIZE_DATA_CACHE);
1105 data_cache->offset = offset;
1108 static int write_cache(DataCache *dc, const void *buf, size_t size,
1109 bool flag_sync)
1112 * dc->buf_size should not be less than size, otherwise dc will never be
1113 * enough
1115 assert(size <= dc->buf_size);
1118 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1119 * otherwise check if the space is enough for caching data in buf, if not,
1120 * write the data in dc->buf to dc->fd and reset dc->buf
1122 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1123 (flag_sync && dc->data_size > 0)) {
1124 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1125 return -1;
1128 dc->offset += dc->data_size;
1129 dc->data_size = 0;
1132 if (!flag_sync) {
1133 memcpy(dc->buf + dc->data_size, buf, size);
1134 dc->data_size += size;
1137 return 0;
1140 static void free_data_cache(DataCache *data_cache)
1142 g_free(data_cache->buf);
1145 static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1147 switch (flag_compress) {
1148 case DUMP_DH_COMPRESSED_ZLIB:
1149 return compressBound(page_size);
1151 case DUMP_DH_COMPRESSED_LZO:
1153 * LZO will expand incompressible data by a little amount. Please check
1154 * the following URL to see the expansion calculation:
1155 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1157 return page_size + page_size / 16 + 64 + 3;
1159 #ifdef CONFIG_SNAPPY
1160 case DUMP_DH_COMPRESSED_SNAPPY:
1161 return snappy_max_compressed_length(page_size);
1162 #endif
1164 return 0;
1168 * check if the page is all 0
1170 static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1172 return buffer_is_zero(buf, page_size);
1175 static void write_dump_pages(DumpState *s, Error **errp)
1177 int ret = 0;
1178 DataCache page_desc, page_data;
1179 size_t len_buf_out, size_out;
1180 #ifdef CONFIG_LZO
1181 lzo_bytep wrkmem = NULL;
1182 #endif
1183 uint8_t *buf_out = NULL;
1184 off_t offset_desc, offset_data;
1185 PageDescriptor pd, pd_zero;
1186 uint8_t *buf;
1187 GuestPhysBlock *block_iter = NULL;
1188 uint64_t pfn_iter;
1190 /* get offset of page_desc and page_data in dump file */
1191 offset_desc = s->offset_page;
1192 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1194 prepare_data_cache(&page_desc, s, offset_desc);
1195 prepare_data_cache(&page_data, s, offset_data);
1197 /* prepare buffer to store compressed data */
1198 len_buf_out = get_len_buf_out(TARGET_PAGE_SIZE, s->flag_compress);
1199 assert(len_buf_out != 0);
1201 #ifdef CONFIG_LZO
1202 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1203 #endif
1205 buf_out = g_malloc(len_buf_out);
1208 * init zero page's page_desc and page_data, because every zero page
1209 * uses the same page_data
1211 pd_zero.size = cpu_to_dump32(s, TARGET_PAGE_SIZE);
1212 pd_zero.flags = cpu_to_dump32(s, 0);
1213 pd_zero.offset = cpu_to_dump64(s, offset_data);
1214 pd_zero.page_flags = cpu_to_dump64(s, 0);
1215 buf = g_malloc0(TARGET_PAGE_SIZE);
1216 ret = write_cache(&page_data, buf, TARGET_PAGE_SIZE, false);
1217 g_free(buf);
1218 if (ret < 0) {
1219 dump_error(s, "dump: failed to write page data (zero page)", errp);
1220 goto out;
1223 offset_data += TARGET_PAGE_SIZE;
1226 * dump memory to vmcore page by page. zero page will all be resided in the
1227 * first page of page section
1229 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1230 /* check zero page */
1231 if (is_zero_page(buf, TARGET_PAGE_SIZE)) {
1232 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1233 false);
1234 if (ret < 0) {
1235 dump_error(s, "dump: failed to write page desc", errp);
1236 goto out;
1238 } else {
1240 * not zero page, then:
1241 * 1. compress the page
1242 * 2. write the compressed page into the cache of page_data
1243 * 3. get page desc of the compressed page and write it into the
1244 * cache of page_desc
1246 * only one compression format will be used here, for
1247 * s->flag_compress is set. But when compression fails to work,
1248 * we fall back to save in plaintext.
1250 size_out = len_buf_out;
1251 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
1252 (compress2(buf_out, (uLongf *)&size_out, buf,
1253 TARGET_PAGE_SIZE, Z_BEST_SPEED) == Z_OK) &&
1254 (size_out < TARGET_PAGE_SIZE)) {
1255 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1256 pd.size = cpu_to_dump32(s, size_out);
1258 ret = write_cache(&page_data, buf_out, size_out, false);
1259 if (ret < 0) {
1260 dump_error(s, "dump: failed to write page data", errp);
1261 goto out;
1263 #ifdef CONFIG_LZO
1264 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
1265 (lzo1x_1_compress(buf, TARGET_PAGE_SIZE, buf_out,
1266 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
1267 (size_out < TARGET_PAGE_SIZE)) {
1268 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1269 pd.size = cpu_to_dump32(s, size_out);
1271 ret = write_cache(&page_data, buf_out, size_out, false);
1272 if (ret < 0) {
1273 dump_error(s, "dump: failed to write page data", errp);
1274 goto out;
1276 #endif
1277 #ifdef CONFIG_SNAPPY
1278 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
1279 (snappy_compress((char *)buf, TARGET_PAGE_SIZE,
1280 (char *)buf_out, &size_out) == SNAPPY_OK) &&
1281 (size_out < TARGET_PAGE_SIZE)) {
1282 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1283 pd.size = cpu_to_dump32(s, size_out);
1285 ret = write_cache(&page_data, buf_out, size_out, false);
1286 if (ret < 0) {
1287 dump_error(s, "dump: failed to write page data", errp);
1288 goto out;
1290 #endif
1291 } else {
1293 * fall back to save in plaintext, size_out should be
1294 * assigned TARGET_PAGE_SIZE
1296 pd.flags = cpu_to_dump32(s, 0);
1297 size_out = TARGET_PAGE_SIZE;
1298 pd.size = cpu_to_dump32(s, size_out);
1300 ret = write_cache(&page_data, buf, TARGET_PAGE_SIZE, false);
1301 if (ret < 0) {
1302 dump_error(s, "dump: failed to write page data", errp);
1303 goto out;
1307 /* get and write page desc here */
1308 pd.page_flags = cpu_to_dump64(s, 0);
1309 pd.offset = cpu_to_dump64(s, offset_data);
1310 offset_data += size_out;
1312 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1313 if (ret < 0) {
1314 dump_error(s, "dump: failed to write page desc", errp);
1315 goto out;
1320 ret = write_cache(&page_desc, NULL, 0, true);
1321 if (ret < 0) {
1322 dump_error(s, "dump: failed to sync cache for page_desc", errp);
1323 goto out;
1325 ret = write_cache(&page_data, NULL, 0, true);
1326 if (ret < 0) {
1327 dump_error(s, "dump: failed to sync cache for page_data", errp);
1328 goto out;
1331 out:
1332 free_data_cache(&page_desc);
1333 free_data_cache(&page_data);
1335 #ifdef CONFIG_LZO
1336 g_free(wrkmem);
1337 #endif
1339 g_free(buf_out);
1342 static void create_kdump_vmcore(DumpState *s, Error **errp)
1344 int ret;
1345 Error *local_err = NULL;
1348 * the kdump-compressed format is:
1349 * File offset
1350 * +------------------------------------------+ 0x0
1351 * | main header (struct disk_dump_header) |
1352 * |------------------------------------------+ block 1
1353 * | sub header (struct kdump_sub_header) |
1354 * |------------------------------------------+ block 2
1355 * | 1st-dump_bitmap |
1356 * |------------------------------------------+ block 2 + X blocks
1357 * | 2nd-dump_bitmap | (aligned by block)
1358 * |------------------------------------------+ block 2 + 2 * X blocks
1359 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1360 * | page desc for pfn 1 (struct page_desc) |
1361 * | : |
1362 * |------------------------------------------| (not aligned by block)
1363 * | page data (pfn 0) |
1364 * | page data (pfn 1) |
1365 * | : |
1366 * +------------------------------------------+
1369 ret = write_start_flat_header(s->fd);
1370 if (ret < 0) {
1371 dump_error(s, "dump: failed to write start flat header", errp);
1372 return;
1375 write_dump_header(s, &local_err);
1376 if (local_err) {
1377 error_propagate(errp, local_err);
1378 return;
1381 write_dump_bitmap(s, &local_err);
1382 if (local_err) {
1383 error_propagate(errp, local_err);
1384 return;
1387 write_dump_pages(s, &local_err);
1388 if (local_err) {
1389 error_propagate(errp, local_err);
1390 return;
1393 ret = write_end_flat_header(s->fd);
1394 if (ret < 0) {
1395 dump_error(s, "dump: failed to write end flat header", errp);
1396 return;
1399 dump_completed(s);
1402 static ram_addr_t get_start_block(DumpState *s)
1404 GuestPhysBlock *block;
1406 if (!s->has_filter) {
1407 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1408 return 0;
1411 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1412 if (block->target_start >= s->begin + s->length ||
1413 block->target_end <= s->begin) {
1414 /* This block is out of the range */
1415 continue;
1418 s->next_block = block;
1419 if (s->begin > block->target_start) {
1420 s->start = s->begin - block->target_start;
1421 } else {
1422 s->start = 0;
1424 return s->start;
1427 return -1;
1430 static void get_max_mapnr(DumpState *s)
1432 GuestPhysBlock *last_block;
1434 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead);
1435 s->max_mapnr = paddr_to_pfn(last_block->target_end);
1438 static void dump_init(DumpState *s, int fd, bool has_format,
1439 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1440 int64_t begin, int64_t length, Error **errp)
1442 CPUState *cpu;
1443 int nr_cpus;
1444 Error *err = NULL;
1445 int ret;
1447 /* kdump-compressed is conflict with paging and filter */
1448 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1449 assert(!paging && !has_filter);
1452 if (runstate_is_running()) {
1453 vm_stop(RUN_STATE_SAVE_VM);
1454 s->resume = true;
1455 } else {
1456 s->resume = false;
1459 /* If we use KVM, we should synchronize the registers before we get dump
1460 * info or physmap info.
1462 cpu_synchronize_all_states();
1463 nr_cpus = 0;
1464 CPU_FOREACH(cpu) {
1465 nr_cpus++;
1468 s->fd = fd;
1469 s->has_filter = has_filter;
1470 s->begin = begin;
1471 s->length = length;
1473 memory_mapping_list_init(&s->list);
1475 guest_phys_blocks_init(&s->guest_phys_blocks);
1476 guest_phys_blocks_append(&s->guest_phys_blocks);
1478 s->start = get_start_block(s);
1479 if (s->start == -1) {
1480 error_set(errp, QERR_INVALID_PARAMETER, "begin");
1481 goto cleanup;
1484 /* get dump info: endian, class and architecture.
1485 * If the target architecture is not supported, cpu_get_dump_info() will
1486 * return -1.
1488 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
1489 if (ret < 0) {
1490 error_set(errp, QERR_UNSUPPORTED);
1491 goto cleanup;
1494 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1495 s->dump_info.d_machine, nr_cpus);
1496 if (s->note_size < 0) {
1497 error_set(errp, QERR_UNSUPPORTED);
1498 goto cleanup;
1501 /* get memory mapping */
1502 if (paging) {
1503 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
1504 if (err != NULL) {
1505 error_propagate(errp, err);
1506 goto cleanup;
1508 } else {
1509 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
1512 s->nr_cpus = nr_cpus;
1514 get_max_mapnr(s);
1516 uint64_t tmp;
1517 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), TARGET_PAGE_SIZE);
1518 s->len_dump_bitmap = tmp * TARGET_PAGE_SIZE;
1520 /* init for kdump-compressed format */
1521 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1522 switch (format) {
1523 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1524 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1525 break;
1527 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
1528 #ifdef CONFIG_LZO
1529 if (lzo_init() != LZO_E_OK) {
1530 error_setg(errp, "failed to initialize the LZO library");
1531 goto cleanup;
1533 #endif
1534 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1535 break;
1537 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1538 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1539 break;
1541 default:
1542 s->flag_compress = 0;
1545 return;
1548 if (s->has_filter) {
1549 memory_mapping_filter(&s->list, s->begin, s->length);
1553 * calculate phdr_num
1555 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1557 s->phdr_num = 1; /* PT_NOTE */
1558 if (s->list.num < UINT16_MAX - 2) {
1559 s->phdr_num += s->list.num;
1560 s->have_section = false;
1561 } else {
1562 s->have_section = true;
1563 s->phdr_num = PN_XNUM;
1564 s->sh_info = 1; /* PT_NOTE */
1566 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1567 if (s->list.num <= UINT32_MAX - 1) {
1568 s->sh_info += s->list.num;
1569 } else {
1570 s->sh_info = UINT32_MAX;
1574 if (s->dump_info.d_class == ELFCLASS64) {
1575 if (s->have_section) {
1576 s->memory_offset = sizeof(Elf64_Ehdr) +
1577 sizeof(Elf64_Phdr) * s->sh_info +
1578 sizeof(Elf64_Shdr) + s->note_size;
1579 } else {
1580 s->memory_offset = sizeof(Elf64_Ehdr) +
1581 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1583 } else {
1584 if (s->have_section) {
1585 s->memory_offset = sizeof(Elf32_Ehdr) +
1586 sizeof(Elf32_Phdr) * s->sh_info +
1587 sizeof(Elf32_Shdr) + s->note_size;
1588 } else {
1589 s->memory_offset = sizeof(Elf32_Ehdr) +
1590 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1594 return;
1596 cleanup:
1597 dump_cleanup(s);
1600 void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin,
1601 int64_t begin, bool has_length,
1602 int64_t length, bool has_format,
1603 DumpGuestMemoryFormat format, Error **errp)
1605 const char *p;
1606 int fd = -1;
1607 DumpState *s;
1608 Error *local_err = NULL;
1611 * kdump-compressed format need the whole memory dumped, so paging or
1612 * filter is not supported here.
1614 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1615 (paging || has_begin || has_length)) {
1616 error_setg(errp, "kdump-compressed format doesn't support paging or "
1617 "filter");
1618 return;
1620 if (has_begin && !has_length) {
1621 error_set(errp, QERR_MISSING_PARAMETER, "length");
1622 return;
1624 if (!has_begin && has_length) {
1625 error_set(errp, QERR_MISSING_PARAMETER, "begin");
1626 return;
1629 /* check whether lzo/snappy is supported */
1630 #ifndef CONFIG_LZO
1631 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1632 error_setg(errp, "kdump-lzo is not available now");
1633 return;
1635 #endif
1637 #ifndef CONFIG_SNAPPY
1638 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1639 error_setg(errp, "kdump-snappy is not available now");
1640 return;
1642 #endif
1644 #if !defined(WIN32)
1645 if (strstart(file, "fd:", &p)) {
1646 fd = monitor_get_fd(cur_mon, p, errp);
1647 if (fd == -1) {
1648 return;
1651 #endif
1653 if (strstart(file, "file:", &p)) {
1654 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1655 if (fd < 0) {
1656 error_setg_file_open(errp, errno, p);
1657 return;
1661 if (fd == -1) {
1662 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
1663 return;
1666 s = g_malloc0(sizeof(DumpState));
1668 dump_init(s, fd, has_format, format, paging, has_begin,
1669 begin, length, &local_err);
1670 if (local_err) {
1671 g_free(s);
1672 error_propagate(errp, local_err);
1673 return;
1676 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1677 create_kdump_vmcore(s, errp);
1678 } else {
1679 create_vmcore(s, errp);
1682 g_free(s);
1685 DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
1687 DumpGuestMemoryFormatList *item;
1688 DumpGuestMemoryCapability *cap =
1689 g_malloc0(sizeof(DumpGuestMemoryCapability));
1691 /* elf is always available */
1692 item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1693 cap->formats = item;
1694 item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
1696 /* kdump-zlib is always available */
1697 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1698 item = item->next;
1699 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1701 /* add new item if kdump-lzo is available */
1702 #ifdef CONFIG_LZO
1703 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1704 item = item->next;
1705 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1706 #endif
1708 /* add new item if kdump-snappy is available */
1709 #ifdef CONFIG_SNAPPY
1710 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1711 item = item->next;
1712 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1713 #endif
1715 return cap;