dump: add guest ELF note
[qemu/kevin.git] / dump.c
blob1479f1f04e82267eac5ec2c168aa6d7416c1ed20
1 /*
2 * QEMU dump
4 * Copyright Fujitsu, Corp. 2011, 2012
6 * Authors:
7 * Wen Congyang <wency@cn.fujitsu.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qemu/cutils.h"
16 #include "elf.h"
17 #include "cpu.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/qmp/qerror.h"
26 #include "qmp-commands.h"
27 #include "qapi-event.h"
28 #include "qemu/error-report.h"
29 #include "hw/misc/vmcoreinfo.h"
31 #include <zlib.h>
32 #ifdef CONFIG_LZO
33 #include <lzo/lzo1x.h>
34 #endif
35 #ifdef CONFIG_SNAPPY
36 #include <snappy-c.h>
37 #endif
38 #ifndef ELF_MACHINE_UNAME
39 #define ELF_MACHINE_UNAME "Unknown"
40 #endif
42 #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
44 #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
45 ((DIV_ROUND_UP((hdr_size), 4) + \
46 DIV_ROUND_UP((name_size), 4) + \
47 DIV_ROUND_UP((desc_size), 4)) * 4)
49 uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
51 if (s->dump_info.d_endian == ELFDATA2LSB) {
52 val = cpu_to_le16(val);
53 } else {
54 val = cpu_to_be16(val);
57 return val;
60 uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
62 if (s->dump_info.d_endian == ELFDATA2LSB) {
63 val = cpu_to_le32(val);
64 } else {
65 val = cpu_to_be32(val);
68 return val;
71 uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
73 if (s->dump_info.d_endian == ELFDATA2LSB) {
74 val = cpu_to_le64(val);
75 } else {
76 val = cpu_to_be64(val);
79 return val;
82 static int dump_cleanup(DumpState *s)
84 guest_phys_blocks_free(&s->guest_phys_blocks);
85 memory_mapping_list_free(&s->list);
86 close(s->fd);
87 g_free(s->guest_note);
88 s->guest_note = NULL;
89 if (s->resume) {
90 if (s->detached) {
91 qemu_mutex_lock_iothread();
93 vm_start();
94 if (s->detached) {
95 qemu_mutex_unlock_iothread();
99 return 0;
102 static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
104 DumpState *s = opaque;
105 size_t written_size;
107 written_size = qemu_write_full(s->fd, buf, size);
108 if (written_size != size) {
109 return -1;
112 return 0;
115 static void write_elf64_header(DumpState *s, Error **errp)
117 Elf64_Ehdr elf_header;
118 int ret;
120 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
121 memcpy(&elf_header, ELFMAG, SELFMAG);
122 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
123 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
124 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
125 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
126 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
127 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
128 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
129 elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
130 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
131 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
132 if (s->have_section) {
133 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
135 elf_header.e_shoff = cpu_to_dump64(s, shoff);
136 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
137 elf_header.e_shnum = cpu_to_dump16(s, 1);
140 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
141 if (ret < 0) {
142 error_setg(errp, "dump: failed to write elf header");
146 static void write_elf32_header(DumpState *s, Error **errp)
148 Elf32_Ehdr elf_header;
149 int ret;
151 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
152 memcpy(&elf_header, ELFMAG, SELFMAG);
153 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
154 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
155 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
156 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
157 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
158 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
159 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
160 elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
161 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
162 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
163 if (s->have_section) {
164 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
166 elf_header.e_shoff = cpu_to_dump32(s, shoff);
167 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
168 elf_header.e_shnum = cpu_to_dump16(s, 1);
171 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
172 if (ret < 0) {
173 error_setg(errp, "dump: failed to write elf header");
177 static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
178 int phdr_index, hwaddr offset,
179 hwaddr filesz, Error **errp)
181 Elf64_Phdr phdr;
182 int ret;
184 memset(&phdr, 0, sizeof(Elf64_Phdr));
185 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
186 phdr.p_offset = cpu_to_dump64(s, offset);
187 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
188 phdr.p_filesz = cpu_to_dump64(s, filesz);
189 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
190 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr);
192 assert(memory_mapping->length >= filesz);
194 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
195 if (ret < 0) {
196 error_setg(errp, "dump: failed to write program header table");
200 static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
201 int phdr_index, hwaddr offset,
202 hwaddr filesz, Error **errp)
204 Elf32_Phdr phdr;
205 int ret;
207 memset(&phdr, 0, sizeof(Elf32_Phdr));
208 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
209 phdr.p_offset = cpu_to_dump32(s, offset);
210 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
211 phdr.p_filesz = cpu_to_dump32(s, filesz);
212 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
213 phdr.p_vaddr = cpu_to_dump32(s, memory_mapping->virt_addr);
215 assert(memory_mapping->length >= filesz);
217 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
218 if (ret < 0) {
219 error_setg(errp, "dump: failed to write program header table");
223 static void write_elf64_note(DumpState *s, Error **errp)
225 Elf64_Phdr phdr;
226 hwaddr begin = s->memory_offset - s->note_size;
227 int ret;
229 memset(&phdr, 0, sizeof(Elf64_Phdr));
230 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
231 phdr.p_offset = cpu_to_dump64(s, begin);
232 phdr.p_paddr = 0;
233 phdr.p_filesz = cpu_to_dump64(s, s->note_size);
234 phdr.p_memsz = cpu_to_dump64(s, s->note_size);
235 phdr.p_vaddr = 0;
237 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
238 if (ret < 0) {
239 error_setg(errp, "dump: failed to write program header table");
243 static inline int cpu_index(CPUState *cpu)
245 return cpu->cpu_index + 1;
248 static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
249 Error **errp)
251 int ret;
253 if (s->guest_note) {
254 ret = f(s->guest_note, s->guest_note_size, s);
255 if (ret < 0) {
256 error_setg(errp, "dump: failed to write guest note");
261 static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
262 Error **errp)
264 CPUState *cpu;
265 int ret;
266 int id;
268 CPU_FOREACH(cpu) {
269 id = cpu_index(cpu);
270 ret = cpu_write_elf64_note(f, cpu, id, s);
271 if (ret < 0) {
272 error_setg(errp, "dump: failed to write elf notes");
273 return;
277 CPU_FOREACH(cpu) {
278 ret = cpu_write_elf64_qemunote(f, cpu, s);
279 if (ret < 0) {
280 error_setg(errp, "dump: failed to write CPU status");
281 return;
285 write_guest_note(f, s, errp);
288 static void write_elf32_note(DumpState *s, Error **errp)
290 hwaddr begin = s->memory_offset - s->note_size;
291 Elf32_Phdr phdr;
292 int ret;
294 memset(&phdr, 0, sizeof(Elf32_Phdr));
295 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
296 phdr.p_offset = cpu_to_dump32(s, begin);
297 phdr.p_paddr = 0;
298 phdr.p_filesz = cpu_to_dump32(s, s->note_size);
299 phdr.p_memsz = cpu_to_dump32(s, s->note_size);
300 phdr.p_vaddr = 0;
302 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
303 if (ret < 0) {
304 error_setg(errp, "dump: failed to write program header table");
308 static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
309 Error **errp)
311 CPUState *cpu;
312 int ret;
313 int id;
315 CPU_FOREACH(cpu) {
316 id = cpu_index(cpu);
317 ret = cpu_write_elf32_note(f, cpu, id, s);
318 if (ret < 0) {
319 error_setg(errp, "dump: failed to write elf notes");
320 return;
324 CPU_FOREACH(cpu) {
325 ret = cpu_write_elf32_qemunote(f, cpu, s);
326 if (ret < 0) {
327 error_setg(errp, "dump: failed to write CPU status");
328 return;
332 write_guest_note(f, s, errp);
335 static void write_elf_section(DumpState *s, int type, Error **errp)
337 Elf32_Shdr shdr32;
338 Elf64_Shdr shdr64;
339 int shdr_size;
340 void *shdr;
341 int ret;
343 if (type == 0) {
344 shdr_size = sizeof(Elf32_Shdr);
345 memset(&shdr32, 0, shdr_size);
346 shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
347 shdr = &shdr32;
348 } else {
349 shdr_size = sizeof(Elf64_Shdr);
350 memset(&shdr64, 0, shdr_size);
351 shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
352 shdr = &shdr64;
355 ret = fd_write_vmcore(&shdr, shdr_size, s);
356 if (ret < 0) {
357 error_setg(errp, "dump: failed to write section header table");
361 static void write_data(DumpState *s, void *buf, int length, Error **errp)
363 int ret;
365 ret = fd_write_vmcore(buf, length, s);
366 if (ret < 0) {
367 error_setg(errp, "dump: failed to save memory");
368 } else {
369 s->written_size += length;
373 /* write the memory to vmcore. 1 page per I/O. */
374 static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
375 int64_t size, Error **errp)
377 int64_t i;
378 Error *local_err = NULL;
380 for (i = 0; i < size / s->dump_info.page_size; i++) {
381 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
382 s->dump_info.page_size, &local_err);
383 if (local_err) {
384 error_propagate(errp, local_err);
385 return;
389 if ((size % s->dump_info.page_size) != 0) {
390 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
391 size % s->dump_info.page_size, &local_err);
392 if (local_err) {
393 error_propagate(errp, local_err);
394 return;
399 /* get the memory's offset and size in the vmcore */
400 static void get_offset_range(hwaddr phys_addr,
401 ram_addr_t mapping_length,
402 DumpState *s,
403 hwaddr *p_offset,
404 hwaddr *p_filesz)
406 GuestPhysBlock *block;
407 hwaddr offset = s->memory_offset;
408 int64_t size_in_block, start;
410 /* When the memory is not stored into vmcore, offset will be -1 */
411 *p_offset = -1;
412 *p_filesz = 0;
414 if (s->has_filter) {
415 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
416 return;
420 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
421 if (s->has_filter) {
422 if (block->target_start >= s->begin + s->length ||
423 block->target_end <= s->begin) {
424 /* This block is out of the range */
425 continue;
428 if (s->begin <= block->target_start) {
429 start = block->target_start;
430 } else {
431 start = s->begin;
434 size_in_block = block->target_end - start;
435 if (s->begin + s->length < block->target_end) {
436 size_in_block -= block->target_end - (s->begin + s->length);
438 } else {
439 start = block->target_start;
440 size_in_block = block->target_end - block->target_start;
443 if (phys_addr >= start && phys_addr < start + size_in_block) {
444 *p_offset = phys_addr - start + offset;
446 /* The offset range mapped from the vmcore file must not spill over
447 * the GuestPhysBlock, clamp it. The rest of the mapping will be
448 * zero-filled in memory at load time; see
449 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
451 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
452 mapping_length :
453 size_in_block - (phys_addr - start);
454 return;
457 offset += size_in_block;
461 static void write_elf_loads(DumpState *s, Error **errp)
463 hwaddr offset, filesz;
464 MemoryMapping *memory_mapping;
465 uint32_t phdr_index = 1;
466 uint32_t max_index;
467 Error *local_err = NULL;
469 if (s->have_section) {
470 max_index = s->sh_info;
471 } else {
472 max_index = s->phdr_num;
475 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
476 get_offset_range(memory_mapping->phys_addr,
477 memory_mapping->length,
478 s, &offset, &filesz);
479 if (s->dump_info.d_class == ELFCLASS64) {
480 write_elf64_load(s, memory_mapping, phdr_index++, offset,
481 filesz, &local_err);
482 } else {
483 write_elf32_load(s, memory_mapping, phdr_index++, offset,
484 filesz, &local_err);
487 if (local_err) {
488 error_propagate(errp, local_err);
489 return;
492 if (phdr_index >= max_index) {
493 break;
498 /* write elf header, PT_NOTE and elf note to vmcore. */
499 static void dump_begin(DumpState *s, Error **errp)
501 Error *local_err = NULL;
504 * the vmcore's format is:
505 * --------------
506 * | elf header |
507 * --------------
508 * | PT_NOTE |
509 * --------------
510 * | PT_LOAD |
511 * --------------
512 * | ...... |
513 * --------------
514 * | PT_LOAD |
515 * --------------
516 * | sec_hdr |
517 * --------------
518 * | elf note |
519 * --------------
520 * | memory |
521 * --------------
523 * we only know where the memory is saved after we write elf note into
524 * vmcore.
527 /* write elf header to vmcore */
528 if (s->dump_info.d_class == ELFCLASS64) {
529 write_elf64_header(s, &local_err);
530 } else {
531 write_elf32_header(s, &local_err);
533 if (local_err) {
534 error_propagate(errp, local_err);
535 return;
538 if (s->dump_info.d_class == ELFCLASS64) {
539 /* write PT_NOTE to vmcore */
540 write_elf64_note(s, &local_err);
541 if (local_err) {
542 error_propagate(errp, local_err);
543 return;
546 /* write all PT_LOAD to vmcore */
547 write_elf_loads(s, &local_err);
548 if (local_err) {
549 error_propagate(errp, local_err);
550 return;
553 /* write section to vmcore */
554 if (s->have_section) {
555 write_elf_section(s, 1, &local_err);
556 if (local_err) {
557 error_propagate(errp, local_err);
558 return;
562 /* write notes to vmcore */
563 write_elf64_notes(fd_write_vmcore, s, &local_err);
564 if (local_err) {
565 error_propagate(errp, local_err);
566 return;
568 } else {
569 /* write PT_NOTE to vmcore */
570 write_elf32_note(s, &local_err);
571 if (local_err) {
572 error_propagate(errp, local_err);
573 return;
576 /* write all PT_LOAD to vmcore */
577 write_elf_loads(s, &local_err);
578 if (local_err) {
579 error_propagate(errp, local_err);
580 return;
583 /* write section to vmcore */
584 if (s->have_section) {
585 write_elf_section(s, 0, &local_err);
586 if (local_err) {
587 error_propagate(errp, local_err);
588 return;
592 /* write notes to vmcore */
593 write_elf32_notes(fd_write_vmcore, s, &local_err);
594 if (local_err) {
595 error_propagate(errp, local_err);
596 return;
601 static int get_next_block(DumpState *s, GuestPhysBlock *block)
603 while (1) {
604 block = QTAILQ_NEXT(block, next);
605 if (!block) {
606 /* no more block */
607 return 1;
610 s->start = 0;
611 s->next_block = block;
612 if (s->has_filter) {
613 if (block->target_start >= s->begin + s->length ||
614 block->target_end <= s->begin) {
615 /* This block is out of the range */
616 continue;
619 if (s->begin > block->target_start) {
620 s->start = s->begin - block->target_start;
624 return 0;
628 /* write all memory to vmcore */
629 static void dump_iterate(DumpState *s, Error **errp)
631 GuestPhysBlock *block;
632 int64_t size;
633 Error *local_err = NULL;
635 do {
636 block = s->next_block;
638 size = block->target_end - block->target_start;
639 if (s->has_filter) {
640 size -= s->start;
641 if (s->begin + s->length < block->target_end) {
642 size -= block->target_end - (s->begin + s->length);
645 write_memory(s, block, s->start, size, &local_err);
646 if (local_err) {
647 error_propagate(errp, local_err);
648 return;
651 } while (!get_next_block(s, block));
654 static void create_vmcore(DumpState *s, Error **errp)
656 Error *local_err = NULL;
658 dump_begin(s, &local_err);
659 if (local_err) {
660 error_propagate(errp, local_err);
661 return;
664 dump_iterate(s, errp);
667 static int write_start_flat_header(int fd)
669 MakedumpfileHeader *mh;
670 int ret = 0;
672 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
673 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
675 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
676 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
678 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
679 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
681 size_t written_size;
682 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
683 if (written_size != MAX_SIZE_MDF_HEADER) {
684 ret = -1;
687 g_free(mh);
688 return ret;
691 static int write_end_flat_header(int fd)
693 MakedumpfileDataHeader mdh;
695 mdh.offset = END_FLAG_FLAT_HEADER;
696 mdh.buf_size = END_FLAG_FLAT_HEADER;
698 size_t written_size;
699 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
700 if (written_size != sizeof(mdh)) {
701 return -1;
704 return 0;
707 static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
709 size_t written_size;
710 MakedumpfileDataHeader mdh;
712 mdh.offset = cpu_to_be64(offset);
713 mdh.buf_size = cpu_to_be64(size);
715 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
716 if (written_size != sizeof(mdh)) {
717 return -1;
720 written_size = qemu_write_full(fd, buf, size);
721 if (written_size != size) {
722 return -1;
725 return 0;
728 static int buf_write_note(const void *buf, size_t size, void *opaque)
730 DumpState *s = opaque;
732 /* note_buf is not enough */
733 if (s->note_buf_offset + size > s->note_size) {
734 return -1;
737 memcpy(s->note_buf + s->note_buf_offset, buf, size);
739 s->note_buf_offset += size;
741 return 0;
745 * This function retrieves various sizes from an elf header.
747 * @note has to be a valid ELF note. The return sizes are unmodified
748 * (not padded or rounded up to be multiple of 4).
750 static void get_note_sizes(DumpState *s, const void *note,
751 uint64_t *note_head_size,
752 uint64_t *name_size,
753 uint64_t *desc_size)
755 uint64_t note_head_sz;
756 uint64_t name_sz;
757 uint64_t desc_sz;
759 if (s->dump_info.d_class == ELFCLASS64) {
760 const Elf64_Nhdr *hdr = note;
761 note_head_sz = sizeof(Elf64_Nhdr);
762 name_sz = tswap64(hdr->n_namesz);
763 desc_sz = tswap64(hdr->n_descsz);
764 } else {
765 const Elf32_Nhdr *hdr = note;
766 note_head_sz = sizeof(Elf32_Nhdr);
767 name_sz = tswap32(hdr->n_namesz);
768 desc_sz = tswap32(hdr->n_descsz);
771 if (note_head_size) {
772 *note_head_size = note_head_sz;
774 if (name_size) {
775 *name_size = name_sz;
777 if (desc_size) {
778 *desc_size = desc_sz;
782 /* write common header, sub header and elf note to vmcore */
783 static void create_header32(DumpState *s, Error **errp)
785 DiskDumpHeader32 *dh = NULL;
786 KdumpSubHeader32 *kh = NULL;
787 size_t size;
788 uint32_t block_size;
789 uint32_t sub_hdr_size;
790 uint32_t bitmap_blocks;
791 uint32_t status = 0;
792 uint64_t offset_note;
793 Error *local_err = NULL;
795 /* write common header, the version of kdump-compressed format is 6th */
796 size = sizeof(DiskDumpHeader32);
797 dh = g_malloc0(size);
799 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
800 dh->header_version = cpu_to_dump32(s, 6);
801 block_size = s->dump_info.page_size;
802 dh->block_size = cpu_to_dump32(s, block_size);
803 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
804 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
805 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
806 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
807 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
808 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
809 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
810 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
811 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
813 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
814 status |= DUMP_DH_COMPRESSED_ZLIB;
816 #ifdef CONFIG_LZO
817 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
818 status |= DUMP_DH_COMPRESSED_LZO;
820 #endif
821 #ifdef CONFIG_SNAPPY
822 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
823 status |= DUMP_DH_COMPRESSED_SNAPPY;
825 #endif
826 dh->status = cpu_to_dump32(s, status);
828 if (write_buffer(s->fd, 0, dh, size) < 0) {
829 error_setg(errp, "dump: failed to write disk dump header");
830 goto out;
833 /* write sub header */
834 size = sizeof(KdumpSubHeader32);
835 kh = g_malloc0(size);
837 /* 64bit max_mapnr_64 */
838 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
839 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
840 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
842 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
843 kh->offset_note = cpu_to_dump64(s, offset_note);
844 kh->note_size = cpu_to_dump32(s, s->note_size);
846 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
847 block_size, kh, size) < 0) {
848 error_setg(errp, "dump: failed to write kdump sub header");
849 goto out;
852 /* write note */
853 s->note_buf = g_malloc0(s->note_size);
854 s->note_buf_offset = 0;
856 /* use s->note_buf to store notes temporarily */
857 write_elf32_notes(buf_write_note, s, &local_err);
858 if (local_err) {
859 error_propagate(errp, local_err);
860 goto out;
862 if (write_buffer(s->fd, offset_note, s->note_buf,
863 s->note_size) < 0) {
864 error_setg(errp, "dump: failed to write notes");
865 goto out;
868 /* get offset of dump_bitmap */
869 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
870 block_size;
872 /* get offset of page */
873 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
874 block_size;
876 out:
877 g_free(dh);
878 g_free(kh);
879 g_free(s->note_buf);
882 /* write common header, sub header and elf note to vmcore */
883 static void create_header64(DumpState *s, Error **errp)
885 DiskDumpHeader64 *dh = NULL;
886 KdumpSubHeader64 *kh = NULL;
887 size_t size;
888 uint32_t block_size;
889 uint32_t sub_hdr_size;
890 uint32_t bitmap_blocks;
891 uint32_t status = 0;
892 uint64_t offset_note;
893 Error *local_err = NULL;
895 /* write common header, the version of kdump-compressed format is 6th */
896 size = sizeof(DiskDumpHeader64);
897 dh = g_malloc0(size);
899 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
900 dh->header_version = cpu_to_dump32(s, 6);
901 block_size = s->dump_info.page_size;
902 dh->block_size = cpu_to_dump32(s, block_size);
903 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
904 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
905 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
906 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
907 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
908 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
909 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
910 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
911 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
913 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
914 status |= DUMP_DH_COMPRESSED_ZLIB;
916 #ifdef CONFIG_LZO
917 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
918 status |= DUMP_DH_COMPRESSED_LZO;
920 #endif
921 #ifdef CONFIG_SNAPPY
922 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
923 status |= DUMP_DH_COMPRESSED_SNAPPY;
925 #endif
926 dh->status = cpu_to_dump32(s, status);
928 if (write_buffer(s->fd, 0, dh, size) < 0) {
929 error_setg(errp, "dump: failed to write disk dump header");
930 goto out;
933 /* write sub header */
934 size = sizeof(KdumpSubHeader64);
935 kh = g_malloc0(size);
937 /* 64bit max_mapnr_64 */
938 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
939 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
940 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
942 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
943 kh->offset_note = cpu_to_dump64(s, offset_note);
944 kh->note_size = cpu_to_dump64(s, s->note_size);
946 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
947 block_size, kh, size) < 0) {
948 error_setg(errp, "dump: failed to write kdump sub header");
949 goto out;
952 /* write note */
953 s->note_buf = g_malloc0(s->note_size);
954 s->note_buf_offset = 0;
956 /* use s->note_buf to store notes temporarily */
957 write_elf64_notes(buf_write_note, s, &local_err);
958 if (local_err) {
959 error_propagate(errp, local_err);
960 goto out;
963 if (write_buffer(s->fd, offset_note, s->note_buf,
964 s->note_size) < 0) {
965 error_setg(errp, "dump: failed to write notes");
966 goto out;
969 /* get offset of dump_bitmap */
970 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
971 block_size;
973 /* get offset of page */
974 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
975 block_size;
977 out:
978 g_free(dh);
979 g_free(kh);
980 g_free(s->note_buf);
983 static void write_dump_header(DumpState *s, Error **errp)
985 Error *local_err = NULL;
987 if (s->dump_info.d_class == ELFCLASS32) {
988 create_header32(s, &local_err);
989 } else {
990 create_header64(s, &local_err);
992 error_propagate(errp, local_err);
995 static size_t dump_bitmap_get_bufsize(DumpState *s)
997 return s->dump_info.page_size;
1001 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1002 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1003 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1004 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1005 * vmcore, ie. synchronizing un-sync bit into vmcore.
1007 static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1008 uint8_t *buf, DumpState *s)
1010 off_t old_offset, new_offset;
1011 off_t offset_bitmap1, offset_bitmap2;
1012 uint32_t byte, bit;
1013 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1014 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1016 /* should not set the previous place */
1017 assert(last_pfn <= pfn);
1020 * if the bit needed to be set is not cached in buf, flush the data in buf
1021 * to vmcore firstly.
1022 * making new_offset be bigger than old_offset can also sync remained data
1023 * into vmcore.
1025 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1026 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
1028 while (old_offset < new_offset) {
1029 /* calculate the offset and write dump_bitmap */
1030 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1031 if (write_buffer(s->fd, offset_bitmap1, buf,
1032 bitmap_bufsize) < 0) {
1033 return -1;
1036 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1037 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1038 old_offset;
1039 if (write_buffer(s->fd, offset_bitmap2, buf,
1040 bitmap_bufsize) < 0) {
1041 return -1;
1044 memset(buf, 0, bitmap_bufsize);
1045 old_offset += bitmap_bufsize;
1048 /* get the exact place of the bit in the buf, and set it */
1049 byte = (pfn % bits_per_buf) / CHAR_BIT;
1050 bit = (pfn % bits_per_buf) % CHAR_BIT;
1051 if (value) {
1052 buf[byte] |= 1u << bit;
1053 } else {
1054 buf[byte] &= ~(1u << bit);
1057 return 0;
1060 static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1062 int target_page_shift = ctz32(s->dump_info.page_size);
1064 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1067 static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1069 int target_page_shift = ctz32(s->dump_info.page_size);
1071 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1075 * exam every page and return the page frame number and the address of the page.
1076 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1077 * blocks, so block->target_start and block->target_end should be interal
1078 * multiples of the target page size.
1080 static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1081 uint8_t **bufptr, DumpState *s)
1083 GuestPhysBlock *block = *blockptr;
1084 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
1085 uint8_t *buf;
1087 /* block == NULL means the start of the iteration */
1088 if (!block) {
1089 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1090 *blockptr = block;
1091 assert((block->target_start & ~target_page_mask) == 0);
1092 assert((block->target_end & ~target_page_mask) == 0);
1093 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
1094 if (bufptr) {
1095 *bufptr = block->host_addr;
1097 return true;
1100 *pfnptr = *pfnptr + 1;
1101 addr = dump_pfn_to_paddr(s, *pfnptr);
1103 if ((addr >= block->target_start) &&
1104 (addr + s->dump_info.page_size <= block->target_end)) {
1105 buf = block->host_addr + (addr - block->target_start);
1106 } else {
1107 /* the next page is in the next block */
1108 block = QTAILQ_NEXT(block, next);
1109 *blockptr = block;
1110 if (!block) {
1111 return false;
1113 assert((block->target_start & ~target_page_mask) == 0);
1114 assert((block->target_end & ~target_page_mask) == 0);
1115 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
1116 buf = block->host_addr;
1119 if (bufptr) {
1120 *bufptr = buf;
1123 return true;
1126 static void write_dump_bitmap(DumpState *s, Error **errp)
1128 int ret = 0;
1129 uint64_t last_pfn, pfn;
1130 void *dump_bitmap_buf;
1131 size_t num_dumpable;
1132 GuestPhysBlock *block_iter = NULL;
1133 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1134 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1136 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1137 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
1139 num_dumpable = 0;
1140 last_pfn = 0;
1143 * exam memory page by page, and set the bit in dump_bitmap corresponded
1144 * to the existing page.
1146 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1147 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1148 if (ret < 0) {
1149 error_setg(errp, "dump: failed to set dump_bitmap");
1150 goto out;
1153 last_pfn = pfn;
1154 num_dumpable++;
1158 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1159 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1160 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1162 if (num_dumpable > 0) {
1163 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
1164 dump_bitmap_buf, s);
1165 if (ret < 0) {
1166 error_setg(errp, "dump: failed to sync dump_bitmap");
1167 goto out;
1171 /* number of dumpable pages that will be dumped later */
1172 s->num_dumpable = num_dumpable;
1174 out:
1175 g_free(dump_bitmap_buf);
1178 static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1179 off_t offset)
1181 data_cache->fd = s->fd;
1182 data_cache->data_size = 0;
1183 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1184 data_cache->buf = g_malloc0(data_cache->buf_size);
1185 data_cache->offset = offset;
1188 static int write_cache(DataCache *dc, const void *buf, size_t size,
1189 bool flag_sync)
1192 * dc->buf_size should not be less than size, otherwise dc will never be
1193 * enough
1195 assert(size <= dc->buf_size);
1198 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1199 * otherwise check if the space is enough for caching data in buf, if not,
1200 * write the data in dc->buf to dc->fd and reset dc->buf
1202 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1203 (flag_sync && dc->data_size > 0)) {
1204 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1205 return -1;
1208 dc->offset += dc->data_size;
1209 dc->data_size = 0;
1212 if (!flag_sync) {
1213 memcpy(dc->buf + dc->data_size, buf, size);
1214 dc->data_size += size;
1217 return 0;
1220 static void free_data_cache(DataCache *data_cache)
1222 g_free(data_cache->buf);
1225 static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1227 switch (flag_compress) {
1228 case DUMP_DH_COMPRESSED_ZLIB:
1229 return compressBound(page_size);
1231 case DUMP_DH_COMPRESSED_LZO:
1233 * LZO will expand incompressible data by a little amount. Please check
1234 * the following URL to see the expansion calculation:
1235 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1237 return page_size + page_size / 16 + 64 + 3;
1239 #ifdef CONFIG_SNAPPY
1240 case DUMP_DH_COMPRESSED_SNAPPY:
1241 return snappy_max_compressed_length(page_size);
1242 #endif
1244 return 0;
1248 * check if the page is all 0
1250 static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1252 return buffer_is_zero(buf, page_size);
1255 static void write_dump_pages(DumpState *s, Error **errp)
1257 int ret = 0;
1258 DataCache page_desc, page_data;
1259 size_t len_buf_out, size_out;
1260 #ifdef CONFIG_LZO
1261 lzo_bytep wrkmem = NULL;
1262 #endif
1263 uint8_t *buf_out = NULL;
1264 off_t offset_desc, offset_data;
1265 PageDescriptor pd, pd_zero;
1266 uint8_t *buf;
1267 GuestPhysBlock *block_iter = NULL;
1268 uint64_t pfn_iter;
1270 /* get offset of page_desc and page_data in dump file */
1271 offset_desc = s->offset_page;
1272 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1274 prepare_data_cache(&page_desc, s, offset_desc);
1275 prepare_data_cache(&page_data, s, offset_data);
1277 /* prepare buffer to store compressed data */
1278 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
1279 assert(len_buf_out != 0);
1281 #ifdef CONFIG_LZO
1282 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1283 #endif
1285 buf_out = g_malloc(len_buf_out);
1288 * init zero page's page_desc and page_data, because every zero page
1289 * uses the same page_data
1291 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
1292 pd_zero.flags = cpu_to_dump32(s, 0);
1293 pd_zero.offset = cpu_to_dump64(s, offset_data);
1294 pd_zero.page_flags = cpu_to_dump64(s, 0);
1295 buf = g_malloc0(s->dump_info.page_size);
1296 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
1297 g_free(buf);
1298 if (ret < 0) {
1299 error_setg(errp, "dump: failed to write page data (zero page)");
1300 goto out;
1303 offset_data += s->dump_info.page_size;
1306 * dump memory to vmcore page by page. zero page will all be resided in the
1307 * first page of page section
1309 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1310 /* check zero page */
1311 if (is_zero_page(buf, s->dump_info.page_size)) {
1312 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1313 false);
1314 if (ret < 0) {
1315 error_setg(errp, "dump: failed to write page desc");
1316 goto out;
1318 } else {
1320 * not zero page, then:
1321 * 1. compress the page
1322 * 2. write the compressed page into the cache of page_data
1323 * 3. get page desc of the compressed page and write it into the
1324 * cache of page_desc
1326 * only one compression format will be used here, for
1327 * s->flag_compress is set. But when compression fails to work,
1328 * we fall back to save in plaintext.
1330 size_out = len_buf_out;
1331 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
1332 (compress2(buf_out, (uLongf *)&size_out, buf,
1333 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1334 (size_out < s->dump_info.page_size)) {
1335 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1336 pd.size = cpu_to_dump32(s, size_out);
1338 ret = write_cache(&page_data, buf_out, size_out, false);
1339 if (ret < 0) {
1340 error_setg(errp, "dump: failed to write page data");
1341 goto out;
1343 #ifdef CONFIG_LZO
1344 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
1345 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
1346 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
1347 (size_out < s->dump_info.page_size)) {
1348 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1349 pd.size = cpu_to_dump32(s, size_out);
1351 ret = write_cache(&page_data, buf_out, size_out, false);
1352 if (ret < 0) {
1353 error_setg(errp, "dump: failed to write page data");
1354 goto out;
1356 #endif
1357 #ifdef CONFIG_SNAPPY
1358 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
1359 (snappy_compress((char *)buf, s->dump_info.page_size,
1360 (char *)buf_out, &size_out) == SNAPPY_OK) &&
1361 (size_out < s->dump_info.page_size)) {
1362 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1363 pd.size = cpu_to_dump32(s, size_out);
1365 ret = write_cache(&page_data, buf_out, size_out, false);
1366 if (ret < 0) {
1367 error_setg(errp, "dump: failed to write page data");
1368 goto out;
1370 #endif
1371 } else {
1373 * fall back to save in plaintext, size_out should be
1374 * assigned the target's page size
1376 pd.flags = cpu_to_dump32(s, 0);
1377 size_out = s->dump_info.page_size;
1378 pd.size = cpu_to_dump32(s, size_out);
1380 ret = write_cache(&page_data, buf,
1381 s->dump_info.page_size, false);
1382 if (ret < 0) {
1383 error_setg(errp, "dump: failed to write page data");
1384 goto out;
1388 /* get and write page desc here */
1389 pd.page_flags = cpu_to_dump64(s, 0);
1390 pd.offset = cpu_to_dump64(s, offset_data);
1391 offset_data += size_out;
1393 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1394 if (ret < 0) {
1395 error_setg(errp, "dump: failed to write page desc");
1396 goto out;
1399 s->written_size += s->dump_info.page_size;
1402 ret = write_cache(&page_desc, NULL, 0, true);
1403 if (ret < 0) {
1404 error_setg(errp, "dump: failed to sync cache for page_desc");
1405 goto out;
1407 ret = write_cache(&page_data, NULL, 0, true);
1408 if (ret < 0) {
1409 error_setg(errp, "dump: failed to sync cache for page_data");
1410 goto out;
1413 out:
1414 free_data_cache(&page_desc);
1415 free_data_cache(&page_data);
1417 #ifdef CONFIG_LZO
1418 g_free(wrkmem);
1419 #endif
1421 g_free(buf_out);
1424 static void create_kdump_vmcore(DumpState *s, Error **errp)
1426 int ret;
1427 Error *local_err = NULL;
1430 * the kdump-compressed format is:
1431 * File offset
1432 * +------------------------------------------+ 0x0
1433 * | main header (struct disk_dump_header) |
1434 * |------------------------------------------+ block 1
1435 * | sub header (struct kdump_sub_header) |
1436 * |------------------------------------------+ block 2
1437 * | 1st-dump_bitmap |
1438 * |------------------------------------------+ block 2 + X blocks
1439 * | 2nd-dump_bitmap | (aligned by block)
1440 * |------------------------------------------+ block 2 + 2 * X blocks
1441 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1442 * | page desc for pfn 1 (struct page_desc) |
1443 * | : |
1444 * |------------------------------------------| (not aligned by block)
1445 * | page data (pfn 0) |
1446 * | page data (pfn 1) |
1447 * | : |
1448 * +------------------------------------------+
1451 ret = write_start_flat_header(s->fd);
1452 if (ret < 0) {
1453 error_setg(errp, "dump: failed to write start flat header");
1454 return;
1457 write_dump_header(s, &local_err);
1458 if (local_err) {
1459 error_propagate(errp, local_err);
1460 return;
1463 write_dump_bitmap(s, &local_err);
1464 if (local_err) {
1465 error_propagate(errp, local_err);
1466 return;
1469 write_dump_pages(s, &local_err);
1470 if (local_err) {
1471 error_propagate(errp, local_err);
1472 return;
1475 ret = write_end_flat_header(s->fd);
1476 if (ret < 0) {
1477 error_setg(errp, "dump: failed to write end flat header");
1478 return;
1482 static ram_addr_t get_start_block(DumpState *s)
1484 GuestPhysBlock *block;
1486 if (!s->has_filter) {
1487 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1488 return 0;
1491 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1492 if (block->target_start >= s->begin + s->length ||
1493 block->target_end <= s->begin) {
1494 /* This block is out of the range */
1495 continue;
1498 s->next_block = block;
1499 if (s->begin > block->target_start) {
1500 s->start = s->begin - block->target_start;
1501 } else {
1502 s->start = 0;
1504 return s->start;
1507 return -1;
1510 static void get_max_mapnr(DumpState *s)
1512 GuestPhysBlock *last_block;
1514 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead);
1515 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
1518 static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1520 static void dump_state_prepare(DumpState *s)
1522 /* zero the struct, setting status to active */
1523 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1526 bool dump_in_progress(void)
1528 DumpState *state = &dump_state_global;
1529 return (atomic_read(&state->status) == DUMP_STATUS_ACTIVE);
1532 /* calculate total size of memory to be dumped (taking filter into
1533 * acoount.) */
1534 static int64_t dump_calculate_size(DumpState *s)
1536 GuestPhysBlock *block;
1537 int64_t size = 0, total = 0, left = 0, right = 0;
1539 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1540 if (s->has_filter) {
1541 /* calculate the overlapped region. */
1542 left = MAX(s->begin, block->target_start);
1543 right = MIN(s->begin + s->length, block->target_end);
1544 size = right - left;
1545 size = size > 0 ? size : 0;
1546 } else {
1547 /* count the whole region in */
1548 size = (block->target_end - block->target_start);
1550 total += size;
1553 return total;
1556 static void dump_init(DumpState *s, int fd, bool has_format,
1557 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1558 int64_t begin, int64_t length, Error **errp)
1560 VMCoreInfoState *vmci = vmcoreinfo_find();
1561 CPUState *cpu;
1562 int nr_cpus;
1563 Error *err = NULL;
1564 int ret;
1566 s->has_format = has_format;
1567 s->format = format;
1568 s->written_size = 0;
1570 /* kdump-compressed is conflict with paging and filter */
1571 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1572 assert(!paging && !has_filter);
1575 if (runstate_is_running()) {
1576 vm_stop(RUN_STATE_SAVE_VM);
1577 s->resume = true;
1578 } else {
1579 s->resume = false;
1582 /* If we use KVM, we should synchronize the registers before we get dump
1583 * info or physmap info.
1585 cpu_synchronize_all_states();
1586 nr_cpus = 0;
1587 CPU_FOREACH(cpu) {
1588 nr_cpus++;
1591 s->fd = fd;
1592 s->has_filter = has_filter;
1593 s->begin = begin;
1594 s->length = length;
1596 memory_mapping_list_init(&s->list);
1598 guest_phys_blocks_init(&s->guest_phys_blocks);
1599 guest_phys_blocks_append(&s->guest_phys_blocks);
1600 s->total_size = dump_calculate_size(s);
1601 #ifdef DEBUG_DUMP_GUEST_MEMORY
1602 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1603 #endif
1605 /* it does not make sense to dump non-existent memory */
1606 if (!s->total_size) {
1607 error_setg(errp, "dump: no guest memory to dump");
1608 goto cleanup;
1611 s->start = get_start_block(s);
1612 if (s->start == -1) {
1613 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
1614 goto cleanup;
1617 /* get dump info: endian, class and architecture.
1618 * If the target architecture is not supported, cpu_get_dump_info() will
1619 * return -1.
1621 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
1622 if (ret < 0) {
1623 error_setg(errp, QERR_UNSUPPORTED);
1624 goto cleanup;
1627 if (!s->dump_info.page_size) {
1628 s->dump_info.page_size = TARGET_PAGE_SIZE;
1631 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1632 s->dump_info.d_machine, nr_cpus);
1633 if (s->note_size < 0) {
1634 error_setg(errp, QERR_UNSUPPORTED);
1635 goto cleanup;
1639 * The goal of this block is to copy the guest note out of
1640 * the guest. Failure to do so is not fatal for dumping.
1642 if (vmci) {
1643 uint64_t addr, note_head_size, name_size, desc_size;
1644 uint32_t size;
1645 uint16_t format;
1647 note_head_size = s->dump_info.d_class == ELFCLASS32 ?
1648 sizeof(Elf32_Nhdr) : sizeof(Elf64_Nhdr);
1650 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1651 size = le32_to_cpu(vmci->vmcoreinfo.size);
1652 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1653 if (!vmci->has_vmcoreinfo) {
1654 warn_report("guest note is not present");
1655 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1656 warn_report("guest note size is invalid: %" PRIu32, size);
1657 } else if (format != VMCOREINFO_FORMAT_ELF) {
1658 warn_report("guest note format is unsupported: %" PRIu16, format);
1659 } else {
1660 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1661 cpu_physical_memory_read(addr, s->guest_note, size);
1663 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1664 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1665 desc_size);
1666 if (name_size > MAX_GUEST_NOTE_SIZE ||
1667 desc_size > MAX_GUEST_NOTE_SIZE ||
1668 s->guest_note_size > size) {
1669 warn_report("Invalid guest note header");
1670 g_free(s->guest_note);
1671 s->guest_note = NULL;
1672 } else {
1673 s->note_size += s->guest_note_size;
1678 /* get memory mapping */
1679 if (paging) {
1680 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
1681 if (err != NULL) {
1682 error_propagate(errp, err);
1683 goto cleanup;
1685 } else {
1686 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
1689 s->nr_cpus = nr_cpus;
1691 get_max_mapnr(s);
1693 uint64_t tmp;
1694 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1695 s->dump_info.page_size);
1696 s->len_dump_bitmap = tmp * s->dump_info.page_size;
1698 /* init for kdump-compressed format */
1699 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1700 switch (format) {
1701 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1702 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1703 break;
1705 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
1706 #ifdef CONFIG_LZO
1707 if (lzo_init() != LZO_E_OK) {
1708 error_setg(errp, "failed to initialize the LZO library");
1709 goto cleanup;
1711 #endif
1712 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1713 break;
1715 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1716 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1717 break;
1719 default:
1720 s->flag_compress = 0;
1723 return;
1726 if (s->has_filter) {
1727 memory_mapping_filter(&s->list, s->begin, s->length);
1731 * calculate phdr_num
1733 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1735 s->phdr_num = 1; /* PT_NOTE */
1736 if (s->list.num < UINT16_MAX - 2) {
1737 s->phdr_num += s->list.num;
1738 s->have_section = false;
1739 } else {
1740 s->have_section = true;
1741 s->phdr_num = PN_XNUM;
1742 s->sh_info = 1; /* PT_NOTE */
1744 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1745 if (s->list.num <= UINT32_MAX - 1) {
1746 s->sh_info += s->list.num;
1747 } else {
1748 s->sh_info = UINT32_MAX;
1752 if (s->dump_info.d_class == ELFCLASS64) {
1753 if (s->have_section) {
1754 s->memory_offset = sizeof(Elf64_Ehdr) +
1755 sizeof(Elf64_Phdr) * s->sh_info +
1756 sizeof(Elf64_Shdr) + s->note_size;
1757 } else {
1758 s->memory_offset = sizeof(Elf64_Ehdr) +
1759 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1761 } else {
1762 if (s->have_section) {
1763 s->memory_offset = sizeof(Elf32_Ehdr) +
1764 sizeof(Elf32_Phdr) * s->sh_info +
1765 sizeof(Elf32_Shdr) + s->note_size;
1766 } else {
1767 s->memory_offset = sizeof(Elf32_Ehdr) +
1768 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1772 return;
1774 cleanup:
1775 dump_cleanup(s);
1778 /* this operation might be time consuming. */
1779 static void dump_process(DumpState *s, Error **errp)
1781 Error *local_err = NULL;
1782 DumpQueryResult *result = NULL;
1784 if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1785 create_kdump_vmcore(s, &local_err);
1786 } else {
1787 create_vmcore(s, &local_err);
1790 /* make sure status is written after written_size updates */
1791 smp_wmb();
1792 atomic_set(&s->status,
1793 (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
1795 /* send DUMP_COMPLETED message (unconditionally) */
1796 result = qmp_query_dump(NULL);
1797 /* should never fail */
1798 assert(result);
1799 qapi_event_send_dump_completed(result, !!local_err, (local_err ? \
1800 error_get_pretty(local_err) : NULL),
1801 &error_abort);
1802 qapi_free_DumpQueryResult(result);
1804 error_propagate(errp, local_err);
1805 dump_cleanup(s);
1808 static void *dump_thread(void *data)
1810 DumpState *s = (DumpState *)data;
1811 dump_process(s, NULL);
1812 return NULL;
1815 DumpQueryResult *qmp_query_dump(Error **errp)
1817 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1818 DumpState *state = &dump_state_global;
1819 result->status = atomic_read(&state->status);
1820 /* make sure we are reading status and written_size in order */
1821 smp_rmb();
1822 result->completed = state->written_size;
1823 result->total = state->total_size;
1824 return result;
1827 void qmp_dump_guest_memory(bool paging, const char *file,
1828 bool has_detach, bool detach,
1829 bool has_begin, int64_t begin, bool has_length,
1830 int64_t length, bool has_format,
1831 DumpGuestMemoryFormat format, Error **errp)
1833 const char *p;
1834 int fd = -1;
1835 DumpState *s;
1836 Error *local_err = NULL;
1837 bool detach_p = false;
1839 if (runstate_check(RUN_STATE_INMIGRATE)) {
1840 error_setg(errp, "Dump not allowed during incoming migration.");
1841 return;
1844 /* if there is a dump in background, we should wait until the dump
1845 * finished */
1846 if (dump_in_progress()) {
1847 error_setg(errp, "There is a dump in process, please wait.");
1848 return;
1852 * kdump-compressed format need the whole memory dumped, so paging or
1853 * filter is not supported here.
1855 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1856 (paging || has_begin || has_length)) {
1857 error_setg(errp, "kdump-compressed format doesn't support paging or "
1858 "filter");
1859 return;
1861 if (has_begin && !has_length) {
1862 error_setg(errp, QERR_MISSING_PARAMETER, "length");
1863 return;
1865 if (!has_begin && has_length) {
1866 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
1867 return;
1869 if (has_detach) {
1870 detach_p = detach;
1873 /* check whether lzo/snappy is supported */
1874 #ifndef CONFIG_LZO
1875 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1876 error_setg(errp, "kdump-lzo is not available now");
1877 return;
1879 #endif
1881 #ifndef CONFIG_SNAPPY
1882 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1883 error_setg(errp, "kdump-snappy is not available now");
1884 return;
1886 #endif
1888 #if !defined(WIN32)
1889 if (strstart(file, "fd:", &p)) {
1890 fd = monitor_get_fd(cur_mon, p, errp);
1891 if (fd == -1) {
1892 return;
1895 #endif
1897 if (strstart(file, "file:", &p)) {
1898 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1899 if (fd < 0) {
1900 error_setg_file_open(errp, errno, p);
1901 return;
1905 if (fd == -1) {
1906 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
1907 return;
1910 s = &dump_state_global;
1911 dump_state_prepare(s);
1913 dump_init(s, fd, has_format, format, paging, has_begin,
1914 begin, length, &local_err);
1915 if (local_err) {
1916 error_propagate(errp, local_err);
1917 atomic_set(&s->status, DUMP_STATUS_FAILED);
1918 return;
1921 if (detach_p) {
1922 /* detached dump */
1923 s->detached = true;
1924 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
1925 s, QEMU_THREAD_DETACHED);
1926 } else {
1927 /* sync dump */
1928 dump_process(s, errp);
1932 DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
1934 DumpGuestMemoryFormatList *item;
1935 DumpGuestMemoryCapability *cap =
1936 g_malloc0(sizeof(DumpGuestMemoryCapability));
1938 /* elf is always available */
1939 item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1940 cap->formats = item;
1941 item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
1943 /* kdump-zlib is always available */
1944 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1945 item = item->next;
1946 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1948 /* add new item if kdump-lzo is available */
1949 #ifdef CONFIG_LZO
1950 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1951 item = item->next;
1952 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1953 #endif
1955 /* add new item if kdump-snappy is available */
1956 #ifdef CONFIG_SNAPPY
1957 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1958 item = item->next;
1959 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1960 #endif
1962 return cap;