4 * Copyright Fujitsu, Corp. 2011, 2012
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"
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"
30 #include <lzo/lzo1x.h>
35 #ifndef ELF_MACHINE_UNAME
36 #define ELF_MACHINE_UNAME "Unknown"
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);
44 val = cpu_to_be16(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);
55 val = cpu_to_be32(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);
66 val = cpu_to_be64(val);
72 static int dump_cleanup(DumpState *s)
74 guest_phys_blocks_free(&s->guest_phys_blocks);
75 memory_mapping_list_free(&s->list);
84 static void dump_error(DumpState *s, const char *reason)
89 static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
91 DumpState *s = opaque;
94 written_size = qemu_write_full(s->fd, buf, size);
95 if (written_size != size) {
102 static int write_elf64_header(DumpState *s)
104 Elf64_Ehdr elf_header;
107 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
108 memcpy(&elf_header, ELFMAG, SELFMAG);
109 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
110 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
111 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
112 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
113 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
114 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
115 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
116 elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
117 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
118 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
119 if (s->have_section) {
120 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
122 elf_header.e_shoff = cpu_to_dump64(s, shoff);
123 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
124 elf_header.e_shnum = cpu_to_dump16(s, 1);
127 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
129 dump_error(s, "dump: failed to write elf header.\n");
136 static int write_elf32_header(DumpState *s)
138 Elf32_Ehdr elf_header;
141 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
142 memcpy(&elf_header, ELFMAG, SELFMAG);
143 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
144 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
145 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
146 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
147 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
148 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
149 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
150 elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
151 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
152 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
153 if (s->have_section) {
154 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
156 elf_header.e_shoff = cpu_to_dump32(s, shoff);
157 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
158 elf_header.e_shnum = cpu_to_dump16(s, 1);
161 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
163 dump_error(s, "dump: failed to write elf header.\n");
170 static int write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
171 int phdr_index, hwaddr offset,
177 memset(&phdr, 0, sizeof(Elf64_Phdr));
178 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
179 phdr.p_offset = cpu_to_dump64(s, offset);
180 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
181 phdr.p_filesz = cpu_to_dump64(s, filesz);
182 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
183 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr);
185 assert(memory_mapping->length >= filesz);
187 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
189 dump_error(s, "dump: failed to write program header table.\n");
196 static int write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
197 int phdr_index, hwaddr offset,
203 memset(&phdr, 0, sizeof(Elf32_Phdr));
204 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
205 phdr.p_offset = cpu_to_dump32(s, offset);
206 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
207 phdr.p_filesz = cpu_to_dump32(s, filesz);
208 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
209 phdr.p_vaddr = cpu_to_dump32(s, memory_mapping->virt_addr);
211 assert(memory_mapping->length >= filesz);
213 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
215 dump_error(s, "dump: failed to write program header table.\n");
222 static int write_elf64_note(DumpState *s)
225 hwaddr begin = s->memory_offset - s->note_size;
228 memset(&phdr, 0, sizeof(Elf64_Phdr));
229 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
230 phdr.p_offset = cpu_to_dump64(s, begin);
232 phdr.p_filesz = cpu_to_dump64(s, s->note_size);
233 phdr.p_memsz = cpu_to_dump64(s, s->note_size);
236 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
238 dump_error(s, "dump: failed to write program header table.\n");
245 static inline int cpu_index(CPUState *cpu)
247 return cpu->cpu_index + 1;
250 static int write_elf64_notes(WriteCoreDumpFunction f, DumpState *s)
258 ret = cpu_write_elf64_note(f, cpu, id, s);
260 dump_error(s, "dump: failed to write elf notes.\n");
266 ret = cpu_write_elf64_qemunote(f, cpu, s);
268 dump_error(s, "dump: failed to write CPU status.\n");
276 static int write_elf32_note(DumpState *s)
278 hwaddr begin = s->memory_offset - s->note_size;
282 memset(&phdr, 0, sizeof(Elf32_Phdr));
283 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
284 phdr.p_offset = cpu_to_dump32(s, begin);
286 phdr.p_filesz = cpu_to_dump32(s, s->note_size);
287 phdr.p_memsz = cpu_to_dump32(s, s->note_size);
290 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
292 dump_error(s, "dump: failed to write program header table.\n");
299 static int write_elf32_notes(WriteCoreDumpFunction f, DumpState *s)
307 ret = cpu_write_elf32_note(f, cpu, id, s);
309 dump_error(s, "dump: failed to write elf notes.\n");
315 ret = cpu_write_elf32_qemunote(f, cpu, s);
317 dump_error(s, "dump: failed to write CPU status.\n");
325 static int write_elf_section(DumpState *s, int type)
334 shdr_size = sizeof(Elf32_Shdr);
335 memset(&shdr32, 0, shdr_size);
336 shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
339 shdr_size = sizeof(Elf64_Shdr);
340 memset(&shdr64, 0, shdr_size);
341 shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
345 ret = fd_write_vmcore(&shdr, shdr_size, s);
347 dump_error(s, "dump: failed to write section header table.\n");
354 static int write_data(DumpState *s, void *buf, int length)
358 ret = fd_write_vmcore(buf, length, s);
360 dump_error(s, "dump: failed to save memory.\n");
367 /* write the memroy to vmcore. 1 page per I/O. */
368 static int write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
374 for (i = 0; i < size / TARGET_PAGE_SIZE; i++) {
375 ret = write_data(s, block->host_addr + start + i * TARGET_PAGE_SIZE,
382 if ((size % TARGET_PAGE_SIZE) != 0) {
383 ret = write_data(s, block->host_addr + start + i * TARGET_PAGE_SIZE,
384 size % TARGET_PAGE_SIZE);
393 /* get the memory's offset and size in the vmcore */
394 static void get_offset_range(hwaddr phys_addr,
395 ram_addr_t mapping_length,
400 GuestPhysBlock *block;
401 hwaddr offset = s->memory_offset;
402 int64_t size_in_block, start;
404 /* When the memory is not stored into vmcore, offset will be -1 */
409 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
414 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
416 if (block->target_start >= s->begin + s->length ||
417 block->target_end <= s->begin) {
418 /* This block is out of the range */
422 if (s->begin <= block->target_start) {
423 start = block->target_start;
428 size_in_block = block->target_end - start;
429 if (s->begin + s->length < block->target_end) {
430 size_in_block -= block->target_end - (s->begin + s->length);
433 start = block->target_start;
434 size_in_block = block->target_end - block->target_start;
437 if (phys_addr >= start && phys_addr < start + size_in_block) {
438 *p_offset = phys_addr - start + offset;
440 /* The offset range mapped from the vmcore file must not spill over
441 * the GuestPhysBlock, clamp it. The rest of the mapping will be
442 * zero-filled in memory at load time; see
443 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
445 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
447 size_in_block - (phys_addr - start);
451 offset += size_in_block;
455 static int write_elf_loads(DumpState *s)
457 hwaddr offset, filesz;
458 MemoryMapping *memory_mapping;
459 uint32_t phdr_index = 1;
463 if (s->have_section) {
464 max_index = s->sh_info;
466 max_index = s->phdr_num;
469 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
470 get_offset_range(memory_mapping->phys_addr,
471 memory_mapping->length,
472 s, &offset, &filesz);
473 if (s->dump_info.d_class == ELFCLASS64) {
474 ret = write_elf64_load(s, memory_mapping, phdr_index++, offset,
477 ret = write_elf32_load(s, memory_mapping, phdr_index++, offset,
485 if (phdr_index >= max_index) {
493 /* write elf header, PT_NOTE and elf note to vmcore. */
494 static int dump_begin(DumpState *s)
499 * the vmcore's format is:
518 * we only know where the memory is saved after we write elf note into
522 /* write elf header to vmcore */
523 if (s->dump_info.d_class == ELFCLASS64) {
524 ret = write_elf64_header(s);
526 ret = write_elf32_header(s);
532 if (s->dump_info.d_class == ELFCLASS64) {
533 /* write PT_NOTE to vmcore */
534 if (write_elf64_note(s) < 0) {
538 /* write all PT_LOAD to vmcore */
539 if (write_elf_loads(s) < 0) {
543 /* write section to vmcore */
544 if (s->have_section) {
545 if (write_elf_section(s, 1) < 0) {
550 /* write notes to vmcore */
551 if (write_elf64_notes(fd_write_vmcore, s) < 0) {
556 /* write PT_NOTE to vmcore */
557 if (write_elf32_note(s) < 0) {
561 /* write all PT_LOAD to vmcore */
562 if (write_elf_loads(s) < 0) {
566 /* write section to vmcore */
567 if (s->have_section) {
568 if (write_elf_section(s, 0) < 0) {
573 /* write notes to vmcore */
574 if (write_elf32_notes(fd_write_vmcore, s) < 0) {
582 /* write PT_LOAD to vmcore */
583 static int dump_completed(DumpState *s)
589 static int get_next_block(DumpState *s, GuestPhysBlock *block)
592 block = QTAILQ_NEXT(block, next);
599 s->next_block = block;
601 if (block->target_start >= s->begin + s->length ||
602 block->target_end <= s->begin) {
603 /* This block is out of the range */
607 if (s->begin > block->target_start) {
608 s->start = s->begin - block->target_start;
616 /* write all memory to vmcore */
617 static int dump_iterate(DumpState *s)
619 GuestPhysBlock *block;
624 block = s->next_block;
626 size = block->target_end - block->target_start;
629 if (s->begin + s->length < block->target_end) {
630 size -= block->target_end - (s->begin + s->length);
633 ret = write_memory(s, block, s->start, size);
638 ret = get_next_block(s, block);
646 static int create_vmcore(DumpState *s)
655 ret = dump_iterate(s);
663 static int write_start_flat_header(int fd)
665 MakedumpfileHeader *mh;
668 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
669 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
671 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
672 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
674 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
675 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
678 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
679 if (written_size != MAX_SIZE_MDF_HEADER) {
687 static int write_end_flat_header(int fd)
689 MakedumpfileDataHeader mdh;
691 mdh.offset = END_FLAG_FLAT_HEADER;
692 mdh.buf_size = END_FLAG_FLAT_HEADER;
695 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
696 if (written_size != sizeof(mdh)) {
703 static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
706 MakedumpfileDataHeader mdh;
708 mdh.offset = cpu_to_be64(offset);
709 mdh.buf_size = cpu_to_be64(size);
711 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
712 if (written_size != sizeof(mdh)) {
716 written_size = qemu_write_full(fd, buf, size);
717 if (written_size != size) {
724 static int buf_write_note(const void *buf, size_t size, void *opaque)
726 DumpState *s = opaque;
728 /* note_buf is not enough */
729 if (s->note_buf_offset + size > s->note_size) {
733 memcpy(s->note_buf + s->note_buf_offset, buf, size);
735 s->note_buf_offset += size;
740 /* write common header, sub header and elf note to vmcore */
741 static int create_header32(DumpState *s)
744 DiskDumpHeader32 *dh = NULL;
745 KdumpSubHeader32 *kh = NULL;
748 uint32_t sub_hdr_size;
749 uint32_t bitmap_blocks;
751 uint64_t offset_note;
753 /* write common header, the version of kdump-compressed format is 6th */
754 size = sizeof(DiskDumpHeader32);
755 dh = g_malloc0(size);
757 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
758 dh->header_version = cpu_to_dump32(s, 6);
759 block_size = TARGET_PAGE_SIZE;
760 dh->block_size = cpu_to_dump32(s, block_size);
761 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
762 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
763 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
764 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
765 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
766 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
767 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
768 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
769 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
771 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
772 status |= DUMP_DH_COMPRESSED_ZLIB;
775 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
776 status |= DUMP_DH_COMPRESSED_LZO;
780 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
781 status |= DUMP_DH_COMPRESSED_SNAPPY;
784 dh->status = cpu_to_dump32(s, status);
786 if (write_buffer(s->fd, 0, dh, size) < 0) {
787 dump_error(s, "dump: failed to write disk dump header.\n");
792 /* write sub header */
793 size = sizeof(KdumpSubHeader32);
794 kh = g_malloc0(size);
796 /* 64bit max_mapnr_64 */
797 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
798 kh->phys_base = cpu_to_dump32(s, PHYS_BASE);
799 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
801 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
802 kh->offset_note = cpu_to_dump64(s, offset_note);
803 kh->note_size = cpu_to_dump32(s, s->note_size);
805 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
806 block_size, kh, size) < 0) {
807 dump_error(s, "dump: failed to write kdump sub header.\n");
813 s->note_buf = g_malloc0(s->note_size);
814 s->note_buf_offset = 0;
816 /* use s->note_buf to store notes temporarily */
817 if (write_elf32_notes(buf_write_note, s) < 0) {
822 if (write_buffer(s->fd, offset_note, s->note_buf,
824 dump_error(s, "dump: failed to write notes");
829 /* get offset of dump_bitmap */
830 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
833 /* get offset of page */
834 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
845 /* write common header, sub header and elf note to vmcore */
846 static int create_header64(DumpState *s)
849 DiskDumpHeader64 *dh = NULL;
850 KdumpSubHeader64 *kh = NULL;
853 uint32_t sub_hdr_size;
854 uint32_t bitmap_blocks;
856 uint64_t offset_note;
858 /* write common header, the version of kdump-compressed format is 6th */
859 size = sizeof(DiskDumpHeader64);
860 dh = g_malloc0(size);
862 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
863 dh->header_version = cpu_to_dump32(s, 6);
864 block_size = TARGET_PAGE_SIZE;
865 dh->block_size = cpu_to_dump32(s, block_size);
866 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
867 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
868 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
869 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
870 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
871 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
872 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
873 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
874 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
876 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
877 status |= DUMP_DH_COMPRESSED_ZLIB;
880 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
881 status |= DUMP_DH_COMPRESSED_LZO;
885 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
886 status |= DUMP_DH_COMPRESSED_SNAPPY;
889 dh->status = cpu_to_dump32(s, status);
891 if (write_buffer(s->fd, 0, dh, size) < 0) {
892 dump_error(s, "dump: failed to write disk dump header.\n");
897 /* write sub header */
898 size = sizeof(KdumpSubHeader64);
899 kh = g_malloc0(size);
901 /* 64bit max_mapnr_64 */
902 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
903 kh->phys_base = cpu_to_dump64(s, PHYS_BASE);
904 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
906 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
907 kh->offset_note = cpu_to_dump64(s, offset_note);
908 kh->note_size = cpu_to_dump64(s, s->note_size);
910 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
911 block_size, kh, size) < 0) {
912 dump_error(s, "dump: failed to write kdump sub header.\n");
918 s->note_buf = g_malloc0(s->note_size);
919 s->note_buf_offset = 0;
921 /* use s->note_buf to store notes temporarily */
922 if (write_elf64_notes(buf_write_note, s) < 0) {
927 if (write_buffer(s->fd, offset_note, s->note_buf,
929 dump_error(s, "dump: failed to write notes");
934 /* get offset of dump_bitmap */
935 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
938 /* get offset of page */
939 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
950 static int write_dump_header(DumpState *s)
952 if (s->dump_info.d_class == ELFCLASS32) {
953 return create_header32(s);
955 return create_header64(s);
960 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
961 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
962 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
963 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
964 * vmcore, ie. synchronizing un-sync bit into vmcore.
966 static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
967 uint8_t *buf, DumpState *s)
969 off_t old_offset, new_offset;
970 off_t offset_bitmap1, offset_bitmap2;
973 /* should not set the previous place */
974 assert(last_pfn <= pfn);
977 * if the bit needed to be set is not cached in buf, flush the data in buf
979 * making new_offset be bigger than old_offset can also sync remained data
982 old_offset = BUFSIZE_BITMAP * (last_pfn / PFN_BUFBITMAP);
983 new_offset = BUFSIZE_BITMAP * (pfn / PFN_BUFBITMAP);
985 while (old_offset < new_offset) {
986 /* calculate the offset and write dump_bitmap */
987 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
988 if (write_buffer(s->fd, offset_bitmap1, buf,
989 BUFSIZE_BITMAP) < 0) {
993 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
994 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
996 if (write_buffer(s->fd, offset_bitmap2, buf,
997 BUFSIZE_BITMAP) < 0) {
1001 memset(buf, 0, BUFSIZE_BITMAP);
1002 old_offset += BUFSIZE_BITMAP;
1005 /* get the exact place of the bit in the buf, and set it */
1006 byte = (pfn % PFN_BUFBITMAP) / CHAR_BIT;
1007 bit = (pfn % PFN_BUFBITMAP) % CHAR_BIT;
1009 buf[byte] |= 1u << bit;
1011 buf[byte] &= ~(1u << bit);
1018 * exam every page and return the page frame number and the address of the page.
1019 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1020 * blocks, so block->target_start and block->target_end should be interal
1021 * multiples of the target page size.
1023 static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1024 uint8_t **bufptr, DumpState *s)
1026 GuestPhysBlock *block = *blockptr;
1030 /* block == NULL means the start of the iteration */
1032 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1034 assert((block->target_start & ~TARGET_PAGE_MASK) == 0);
1035 assert((block->target_end & ~TARGET_PAGE_MASK) == 0);
1036 *pfnptr = paddr_to_pfn(block->target_start);
1038 *bufptr = block->host_addr;
1043 *pfnptr = *pfnptr + 1;
1044 addr = pfn_to_paddr(*pfnptr);
1046 if ((addr >= block->target_start) &&
1047 (addr + TARGET_PAGE_SIZE <= block->target_end)) {
1048 buf = block->host_addr + (addr - block->target_start);
1050 /* the next page is in the next block */
1051 block = QTAILQ_NEXT(block, next);
1056 assert((block->target_start & ~TARGET_PAGE_MASK) == 0);
1057 assert((block->target_end & ~TARGET_PAGE_MASK) == 0);
1058 *pfnptr = paddr_to_pfn(block->target_start);
1059 buf = block->host_addr;
1069 static int write_dump_bitmap(DumpState *s)
1072 uint64_t last_pfn, pfn;
1073 void *dump_bitmap_buf;
1074 size_t num_dumpable;
1075 GuestPhysBlock *block_iter = NULL;
1077 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1078 dump_bitmap_buf = g_malloc0(BUFSIZE_BITMAP);
1084 * exam memory page by page, and set the bit in dump_bitmap corresponded
1085 * to the existing page.
1087 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1088 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1090 dump_error(s, "dump: failed to set dump_bitmap.\n");
1100 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1101 * set last_pfn + PFN_BUFBITMAP to 0 and those set but un-sync bit will be
1102 * synchronized into vmcore.
1104 if (num_dumpable > 0) {
1105 ret = set_dump_bitmap(last_pfn, last_pfn + PFN_BUFBITMAP, false,
1106 dump_bitmap_buf, s);
1108 dump_error(s, "dump: failed to sync dump_bitmap.\n");
1114 /* number of dumpable pages that will be dumped later */
1115 s->num_dumpable = num_dumpable;
1118 g_free(dump_bitmap_buf);
1123 static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1126 data_cache->fd = s->fd;
1127 data_cache->data_size = 0;
1128 data_cache->buf_size = BUFSIZE_DATA_CACHE;
1129 data_cache->buf = g_malloc0(BUFSIZE_DATA_CACHE);
1130 data_cache->offset = offset;
1133 static int write_cache(DataCache *dc, const void *buf, size_t size,
1137 * dc->buf_size should not be less than size, otherwise dc will never be
1140 assert(size <= dc->buf_size);
1143 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1144 * otherwise check if the space is enough for caching data in buf, if not,
1145 * write the data in dc->buf to dc->fd and reset dc->buf
1147 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1148 (flag_sync && dc->data_size > 0)) {
1149 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1153 dc->offset += dc->data_size;
1158 memcpy(dc->buf + dc->data_size, buf, size);
1159 dc->data_size += size;
1165 static void free_data_cache(DataCache *data_cache)
1167 g_free(data_cache->buf);
1170 static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1172 switch (flag_compress) {
1173 case DUMP_DH_COMPRESSED_ZLIB:
1174 return compressBound(page_size);
1176 case DUMP_DH_COMPRESSED_LZO:
1178 * LZO will expand incompressible data by a little amount. Please check
1179 * the following URL to see the expansion calculation:
1180 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1182 return page_size + page_size / 16 + 64 + 3;
1184 #ifdef CONFIG_SNAPPY
1185 case DUMP_DH_COMPRESSED_SNAPPY:
1186 return snappy_max_compressed_length(page_size);
1193 * check if the page is all 0
1195 static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1197 return buffer_is_zero(buf, page_size);
1200 static int write_dump_pages(DumpState *s)
1203 DataCache page_desc, page_data;
1204 size_t len_buf_out, size_out;
1206 lzo_bytep wrkmem = NULL;
1208 uint8_t *buf_out = NULL;
1209 off_t offset_desc, offset_data;
1210 PageDescriptor pd, pd_zero;
1212 GuestPhysBlock *block_iter = NULL;
1215 /* get offset of page_desc and page_data in dump file */
1216 offset_desc = s->offset_page;
1217 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1219 prepare_data_cache(&page_desc, s, offset_desc);
1220 prepare_data_cache(&page_data, s, offset_data);
1222 /* prepare buffer to store compressed data */
1223 len_buf_out = get_len_buf_out(TARGET_PAGE_SIZE, s->flag_compress);
1224 assert(len_buf_out != 0);
1227 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1230 buf_out = g_malloc(len_buf_out);
1233 * init zero page's page_desc and page_data, because every zero page
1234 * uses the same page_data
1236 pd_zero.size = cpu_to_dump32(s, TARGET_PAGE_SIZE);
1237 pd_zero.flags = cpu_to_dump32(s, 0);
1238 pd_zero.offset = cpu_to_dump64(s, offset_data);
1239 pd_zero.page_flags = cpu_to_dump64(s, 0);
1240 buf = g_malloc0(TARGET_PAGE_SIZE);
1241 ret = write_cache(&page_data, buf, TARGET_PAGE_SIZE, false);
1244 dump_error(s, "dump: failed to write page data(zero page).\n");
1248 offset_data += TARGET_PAGE_SIZE;
1251 * dump memory to vmcore page by page. zero page will all be resided in the
1252 * first page of page section
1254 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1255 /* check zero page */
1256 if (is_zero_page(buf, TARGET_PAGE_SIZE)) {
1257 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1260 dump_error(s, "dump: failed to write page desc.\n");
1265 * not zero page, then:
1266 * 1. compress the page
1267 * 2. write the compressed page into the cache of page_data
1268 * 3. get page desc of the compressed page and write it into the
1269 * cache of page_desc
1271 * only one compression format will be used here, for
1272 * s->flag_compress is set. But when compression fails to work,
1273 * we fall back to save in plaintext.
1275 size_out = len_buf_out;
1276 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
1277 (compress2(buf_out, (uLongf *)&size_out, buf,
1278 TARGET_PAGE_SIZE, Z_BEST_SPEED) == Z_OK) &&
1279 (size_out < TARGET_PAGE_SIZE)) {
1280 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1281 pd.size = cpu_to_dump32(s, size_out);
1283 ret = write_cache(&page_data, buf_out, size_out, false);
1285 dump_error(s, "dump: failed to write page data.\n");
1289 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
1290 (lzo1x_1_compress(buf, TARGET_PAGE_SIZE, buf_out,
1291 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
1292 (size_out < TARGET_PAGE_SIZE)) {
1293 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1294 pd.size = cpu_to_dump32(s, size_out);
1296 ret = write_cache(&page_data, buf_out, size_out, false);
1298 dump_error(s, "dump: failed to write page data.\n");
1302 #ifdef CONFIG_SNAPPY
1303 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
1304 (snappy_compress((char *)buf, TARGET_PAGE_SIZE,
1305 (char *)buf_out, &size_out) == SNAPPY_OK) &&
1306 (size_out < TARGET_PAGE_SIZE)) {
1307 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1308 pd.size = cpu_to_dump32(s, size_out);
1310 ret = write_cache(&page_data, buf_out, size_out, false);
1312 dump_error(s, "dump: failed to write page data.\n");
1318 * fall back to save in plaintext, size_out should be
1319 * assigned TARGET_PAGE_SIZE
1321 pd.flags = cpu_to_dump32(s, 0);
1322 size_out = TARGET_PAGE_SIZE;
1323 pd.size = cpu_to_dump32(s, size_out);
1325 ret = write_cache(&page_data, buf, TARGET_PAGE_SIZE, false);
1327 dump_error(s, "dump: failed to write page data.\n");
1332 /* get and write page desc here */
1333 pd.page_flags = cpu_to_dump64(s, 0);
1334 pd.offset = cpu_to_dump64(s, offset_data);
1335 offset_data += size_out;
1337 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1339 dump_error(s, "dump: failed to write page desc.\n");
1345 ret = write_cache(&page_desc, NULL, 0, true);
1347 dump_error(s, "dump: failed to sync cache for page_desc.\n");
1350 ret = write_cache(&page_data, NULL, 0, true);
1352 dump_error(s, "dump: failed to sync cache for page_data.\n");
1357 free_data_cache(&page_desc);
1358 free_data_cache(&page_data);
1369 static int create_kdump_vmcore(DumpState *s)
1374 * the kdump-compressed format is:
1376 * +------------------------------------------+ 0x0
1377 * | main header (struct disk_dump_header) |
1378 * |------------------------------------------+ block 1
1379 * | sub header (struct kdump_sub_header) |
1380 * |------------------------------------------+ block 2
1381 * | 1st-dump_bitmap |
1382 * |------------------------------------------+ block 2 + X blocks
1383 * | 2nd-dump_bitmap | (aligned by block)
1384 * |------------------------------------------+ block 2 + 2 * X blocks
1385 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1386 * | page desc for pfn 1 (struct page_desc) |
1388 * |------------------------------------------| (not aligned by block)
1389 * | page data (pfn 0) |
1390 * | page data (pfn 1) |
1392 * +------------------------------------------+
1395 ret = write_start_flat_header(s->fd);
1397 dump_error(s, "dump: failed to write start flat header.\n");
1401 ret = write_dump_header(s);
1406 ret = write_dump_bitmap(s);
1411 ret = write_dump_pages(s);
1416 ret = write_end_flat_header(s->fd);
1418 dump_error(s, "dump: failed to write end flat header.\n");
1427 static ram_addr_t get_start_block(DumpState *s)
1429 GuestPhysBlock *block;
1431 if (!s->has_filter) {
1432 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1436 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1437 if (block->target_start >= s->begin + s->length ||
1438 block->target_end <= s->begin) {
1439 /* This block is out of the range */
1443 s->next_block = block;
1444 if (s->begin > block->target_start) {
1445 s->start = s->begin - block->target_start;
1455 static void get_max_mapnr(DumpState *s)
1457 GuestPhysBlock *last_block;
1459 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead);
1460 s->max_mapnr = paddr_to_pfn(last_block->target_end);
1463 static int dump_init(DumpState *s, int fd, bool has_format,
1464 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1465 int64_t begin, int64_t length, Error **errp)
1472 /* kdump-compressed is conflict with paging and filter */
1473 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1474 assert(!paging && !has_filter);
1477 if (runstate_is_running()) {
1478 vm_stop(RUN_STATE_SAVE_VM);
1484 /* If we use KVM, we should synchronize the registers before we get dump
1485 * info or physmap info.
1487 cpu_synchronize_all_states();
1494 s->has_filter = has_filter;
1498 memory_mapping_list_init(&s->list);
1500 guest_phys_blocks_init(&s->guest_phys_blocks);
1501 guest_phys_blocks_append(&s->guest_phys_blocks);
1503 s->start = get_start_block(s);
1504 if (s->start == -1) {
1505 error_set(errp, QERR_INVALID_PARAMETER, "begin");
1509 /* get dump info: endian, class and architecture.
1510 * If the target architecture is not supported, cpu_get_dump_info() will
1513 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
1515 error_set(errp, QERR_UNSUPPORTED);
1519 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1520 s->dump_info.d_machine, nr_cpus);
1521 if (s->note_size < 0) {
1522 error_set(errp, QERR_UNSUPPORTED);
1526 /* get memory mapping */
1528 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
1530 error_propagate(errp, err);
1534 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
1537 s->nr_cpus = nr_cpus;
1542 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), TARGET_PAGE_SIZE);
1543 s->len_dump_bitmap = tmp * TARGET_PAGE_SIZE;
1545 /* init for kdump-compressed format */
1546 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1548 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1549 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1552 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
1554 if (lzo_init() != LZO_E_OK) {
1555 error_setg(errp, "failed to initialize the LZO library");
1559 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1562 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1563 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1567 s->flag_compress = 0;
1573 if (s->has_filter) {
1574 memory_mapping_filter(&s->list, s->begin, s->length);
1578 * calculate phdr_num
1580 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1582 s->phdr_num = 1; /* PT_NOTE */
1583 if (s->list.num < UINT16_MAX - 2) {
1584 s->phdr_num += s->list.num;
1585 s->have_section = false;
1587 s->have_section = true;
1588 s->phdr_num = PN_XNUM;
1589 s->sh_info = 1; /* PT_NOTE */
1591 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1592 if (s->list.num <= UINT32_MAX - 1) {
1593 s->sh_info += s->list.num;
1595 s->sh_info = UINT32_MAX;
1599 if (s->dump_info.d_class == ELFCLASS64) {
1600 if (s->have_section) {
1601 s->memory_offset = sizeof(Elf64_Ehdr) +
1602 sizeof(Elf64_Phdr) * s->sh_info +
1603 sizeof(Elf64_Shdr) + s->note_size;
1605 s->memory_offset = sizeof(Elf64_Ehdr) +
1606 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1609 if (s->have_section) {
1610 s->memory_offset = sizeof(Elf32_Ehdr) +
1611 sizeof(Elf32_Phdr) * s->sh_info +
1612 sizeof(Elf32_Shdr) + s->note_size;
1614 s->memory_offset = sizeof(Elf32_Ehdr) +
1615 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1626 void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin,
1627 int64_t begin, bool has_length,
1628 int64_t length, bool has_format,
1629 DumpGuestMemoryFormat format, Error **errp)
1637 * kdump-compressed format need the whole memory dumped, so paging or
1638 * filter is not supported here.
1640 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1641 (paging || has_begin || has_length)) {
1642 error_setg(errp, "kdump-compressed format doesn't support paging or "
1646 if (has_begin && !has_length) {
1647 error_set(errp, QERR_MISSING_PARAMETER, "length");
1650 if (!has_begin && has_length) {
1651 error_set(errp, QERR_MISSING_PARAMETER, "begin");
1655 /* check whether lzo/snappy is supported */
1657 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1658 error_setg(errp, "kdump-lzo is not available now");
1663 #ifndef CONFIG_SNAPPY
1664 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1665 error_setg(errp, "kdump-snappy is not available now");
1671 if (strstart(file, "fd:", &p)) {
1672 fd = monitor_get_fd(cur_mon, p, errp);
1679 if (strstart(file, "file:", &p)) {
1680 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1682 error_setg_file_open(errp, errno, p);
1688 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
1692 s = g_malloc0(sizeof(DumpState));
1694 ret = dump_init(s, fd, has_format, format, paging, has_begin,
1695 begin, length, errp);
1701 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1702 if (create_kdump_vmcore(s) < 0) {
1703 error_set(errp, QERR_IO_ERROR);
1706 if (create_vmcore(s) < 0) {
1707 error_set(errp, QERR_IO_ERROR);
1714 DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
1716 DumpGuestMemoryFormatList *item;
1717 DumpGuestMemoryCapability *cap =
1718 g_malloc0(sizeof(DumpGuestMemoryCapability));
1720 /* elf is always available */
1721 item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1722 cap->formats = item;
1723 item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
1725 /* kdump-zlib is always available */
1726 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1728 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1730 /* add new item if kdump-lzo is available */
1732 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1734 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1737 /* add new item if kdump-snappy is available */
1738 #ifdef CONFIG_SNAPPY
1739 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1741 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;