tpm: CRB: query backend for TPM established flag
[qemu/ar7.git] / dump.c
blob6bdb0dbe235fd13a2e3fdf0e9554d9f66f8501cb
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/error.h"
26 #include "qapi/qapi-commands-misc.h"
27 #include "qapi/qapi-events-misc.h"
28 #include "qapi/qmp/qerror.h"
29 #include "qemu/error-report.h"
30 #include "hw/misc/vmcoreinfo.h"
32 #include <zlib.h>
33 #ifdef CONFIG_LZO
34 #include <lzo/lzo1x.h>
35 #endif
36 #ifdef CONFIG_SNAPPY
37 #include <snappy-c.h>
38 #endif
39 #ifndef ELF_MACHINE_UNAME
40 #define ELF_MACHINE_UNAME "Unknown"
41 #endif
43 #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
45 #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
46 ((DIV_ROUND_UP((hdr_size), 4) + \
47 DIV_ROUND_UP((name_size), 4) + \
48 DIV_ROUND_UP((desc_size), 4)) * 4)
50 uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
52 if (s->dump_info.d_endian == ELFDATA2LSB) {
53 val = cpu_to_le16(val);
54 } else {
55 val = cpu_to_be16(val);
58 return val;
61 uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
63 if (s->dump_info.d_endian == ELFDATA2LSB) {
64 val = cpu_to_le32(val);
65 } else {
66 val = cpu_to_be32(val);
69 return val;
72 uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
74 if (s->dump_info.d_endian == ELFDATA2LSB) {
75 val = cpu_to_le64(val);
76 } else {
77 val = cpu_to_be64(val);
80 return val;
83 static int dump_cleanup(DumpState *s)
85 guest_phys_blocks_free(&s->guest_phys_blocks);
86 memory_mapping_list_free(&s->list);
87 close(s->fd);
88 g_free(s->guest_note);
89 s->guest_note = NULL;
90 if (s->resume) {
91 if (s->detached) {
92 qemu_mutex_lock_iothread();
94 vm_start();
95 if (s->detached) {
96 qemu_mutex_unlock_iothread();
100 return 0;
103 static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
105 DumpState *s = opaque;
106 size_t written_size;
108 written_size = qemu_write_full(s->fd, buf, size);
109 if (written_size != size) {
110 return -1;
113 return 0;
116 static void write_elf64_header(DumpState *s, Error **errp)
118 Elf64_Ehdr elf_header;
119 int ret;
121 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
122 memcpy(&elf_header, ELFMAG, SELFMAG);
123 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
124 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
125 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
126 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
127 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
128 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
129 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
130 elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
131 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
132 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
133 if (s->have_section) {
134 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
136 elf_header.e_shoff = cpu_to_dump64(s, shoff);
137 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
138 elf_header.e_shnum = cpu_to_dump16(s, 1);
141 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
142 if (ret < 0) {
143 error_setg(errp, "dump: failed to write elf header");
147 static void write_elf32_header(DumpState *s, Error **errp)
149 Elf32_Ehdr elf_header;
150 int ret;
152 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
153 memcpy(&elf_header, ELFMAG, SELFMAG);
154 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
155 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
156 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
157 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
158 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
159 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
160 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
161 elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
162 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
163 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
164 if (s->have_section) {
165 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
167 elf_header.e_shoff = cpu_to_dump32(s, shoff);
168 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
169 elf_header.e_shnum = cpu_to_dump16(s, 1);
172 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
173 if (ret < 0) {
174 error_setg(errp, "dump: failed to write elf header");
178 static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
179 int phdr_index, hwaddr offset,
180 hwaddr filesz, Error **errp)
182 Elf64_Phdr phdr;
183 int ret;
185 memset(&phdr, 0, sizeof(Elf64_Phdr));
186 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
187 phdr.p_offset = cpu_to_dump64(s, offset);
188 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
189 phdr.p_filesz = cpu_to_dump64(s, filesz);
190 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
191 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr);
193 assert(memory_mapping->length >= filesz);
195 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
196 if (ret < 0) {
197 error_setg(errp, "dump: failed to write program header table");
201 static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
202 int phdr_index, hwaddr offset,
203 hwaddr filesz, Error **errp)
205 Elf32_Phdr phdr;
206 int ret;
208 memset(&phdr, 0, sizeof(Elf32_Phdr));
209 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
210 phdr.p_offset = cpu_to_dump32(s, offset);
211 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
212 phdr.p_filesz = cpu_to_dump32(s, filesz);
213 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
214 phdr.p_vaddr = cpu_to_dump32(s, memory_mapping->virt_addr);
216 assert(memory_mapping->length >= filesz);
218 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
219 if (ret < 0) {
220 error_setg(errp, "dump: failed to write program header table");
224 static void write_elf64_note(DumpState *s, Error **errp)
226 Elf64_Phdr phdr;
227 hwaddr begin = s->memory_offset - s->note_size;
228 int ret;
230 memset(&phdr, 0, sizeof(Elf64_Phdr));
231 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
232 phdr.p_offset = cpu_to_dump64(s, begin);
233 phdr.p_paddr = 0;
234 phdr.p_filesz = cpu_to_dump64(s, s->note_size);
235 phdr.p_memsz = cpu_to_dump64(s, s->note_size);
236 phdr.p_vaddr = 0;
238 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
239 if (ret < 0) {
240 error_setg(errp, "dump: failed to write program header table");
244 static inline int cpu_index(CPUState *cpu)
246 return cpu->cpu_index + 1;
249 static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
250 Error **errp)
252 int ret;
254 if (s->guest_note) {
255 ret = f(s->guest_note, s->guest_note_size, s);
256 if (ret < 0) {
257 error_setg(errp, "dump: failed to write guest note");
262 static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
263 Error **errp)
265 CPUState *cpu;
266 int ret;
267 int id;
269 CPU_FOREACH(cpu) {
270 id = cpu_index(cpu);
271 ret = cpu_write_elf64_note(f, cpu, id, s);
272 if (ret < 0) {
273 error_setg(errp, "dump: failed to write elf notes");
274 return;
278 CPU_FOREACH(cpu) {
279 ret = cpu_write_elf64_qemunote(f, cpu, s);
280 if (ret < 0) {
281 error_setg(errp, "dump: failed to write CPU status");
282 return;
286 write_guest_note(f, s, errp);
289 static void write_elf32_note(DumpState *s, Error **errp)
291 hwaddr begin = s->memory_offset - s->note_size;
292 Elf32_Phdr phdr;
293 int ret;
295 memset(&phdr, 0, sizeof(Elf32_Phdr));
296 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
297 phdr.p_offset = cpu_to_dump32(s, begin);
298 phdr.p_paddr = 0;
299 phdr.p_filesz = cpu_to_dump32(s, s->note_size);
300 phdr.p_memsz = cpu_to_dump32(s, s->note_size);
301 phdr.p_vaddr = 0;
303 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
304 if (ret < 0) {
305 error_setg(errp, "dump: failed to write program header table");
309 static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
310 Error **errp)
312 CPUState *cpu;
313 int ret;
314 int id;
316 CPU_FOREACH(cpu) {
317 id = cpu_index(cpu);
318 ret = cpu_write_elf32_note(f, cpu, id, s);
319 if (ret < 0) {
320 error_setg(errp, "dump: failed to write elf notes");
321 return;
325 CPU_FOREACH(cpu) {
326 ret = cpu_write_elf32_qemunote(f, cpu, s);
327 if (ret < 0) {
328 error_setg(errp, "dump: failed to write CPU status");
329 return;
333 write_guest_note(f, s, errp);
336 static void write_elf_section(DumpState *s, int type, Error **errp)
338 Elf32_Shdr shdr32;
339 Elf64_Shdr shdr64;
340 int shdr_size;
341 void *shdr;
342 int ret;
344 if (type == 0) {
345 shdr_size = sizeof(Elf32_Shdr);
346 memset(&shdr32, 0, shdr_size);
347 shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
348 shdr = &shdr32;
349 } else {
350 shdr_size = sizeof(Elf64_Shdr);
351 memset(&shdr64, 0, shdr_size);
352 shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
353 shdr = &shdr64;
356 ret = fd_write_vmcore(&shdr, shdr_size, s);
357 if (ret < 0) {
358 error_setg(errp, "dump: failed to write section header table");
362 static void write_data(DumpState *s, void *buf, int length, Error **errp)
364 int ret;
366 ret = fd_write_vmcore(buf, length, s);
367 if (ret < 0) {
368 error_setg(errp, "dump: failed to save memory");
369 } else {
370 s->written_size += length;
374 /* write the memory to vmcore. 1 page per I/O. */
375 static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
376 int64_t size, Error **errp)
378 int64_t i;
379 Error *local_err = NULL;
381 for (i = 0; i < size / s->dump_info.page_size; i++) {
382 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
383 s->dump_info.page_size, &local_err);
384 if (local_err) {
385 error_propagate(errp, local_err);
386 return;
390 if ((size % s->dump_info.page_size) != 0) {
391 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
392 size % s->dump_info.page_size, &local_err);
393 if (local_err) {
394 error_propagate(errp, local_err);
395 return;
400 /* get the memory's offset and size in the vmcore */
401 static void get_offset_range(hwaddr phys_addr,
402 ram_addr_t mapping_length,
403 DumpState *s,
404 hwaddr *p_offset,
405 hwaddr *p_filesz)
407 GuestPhysBlock *block;
408 hwaddr offset = s->memory_offset;
409 int64_t size_in_block, start;
411 /* When the memory is not stored into vmcore, offset will be -1 */
412 *p_offset = -1;
413 *p_filesz = 0;
415 if (s->has_filter) {
416 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
417 return;
421 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
422 if (s->has_filter) {
423 if (block->target_start >= s->begin + s->length ||
424 block->target_end <= s->begin) {
425 /* This block is out of the range */
426 continue;
429 if (s->begin <= block->target_start) {
430 start = block->target_start;
431 } else {
432 start = s->begin;
435 size_in_block = block->target_end - start;
436 if (s->begin + s->length < block->target_end) {
437 size_in_block -= block->target_end - (s->begin + s->length);
439 } else {
440 start = block->target_start;
441 size_in_block = block->target_end - block->target_start;
444 if (phys_addr >= start && phys_addr < start + size_in_block) {
445 *p_offset = phys_addr - start + offset;
447 /* The offset range mapped from the vmcore file must not spill over
448 * the GuestPhysBlock, clamp it. The rest of the mapping will be
449 * zero-filled in memory at load time; see
450 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
452 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
453 mapping_length :
454 size_in_block - (phys_addr - start);
455 return;
458 offset += size_in_block;
462 static void write_elf_loads(DumpState *s, Error **errp)
464 hwaddr offset, filesz;
465 MemoryMapping *memory_mapping;
466 uint32_t phdr_index = 1;
467 uint32_t max_index;
468 Error *local_err = NULL;
470 if (s->have_section) {
471 max_index = s->sh_info;
472 } else {
473 max_index = s->phdr_num;
476 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
477 get_offset_range(memory_mapping->phys_addr,
478 memory_mapping->length,
479 s, &offset, &filesz);
480 if (s->dump_info.d_class == ELFCLASS64) {
481 write_elf64_load(s, memory_mapping, phdr_index++, offset,
482 filesz, &local_err);
483 } else {
484 write_elf32_load(s, memory_mapping, phdr_index++, offset,
485 filesz, &local_err);
488 if (local_err) {
489 error_propagate(errp, local_err);
490 return;
493 if (phdr_index >= max_index) {
494 break;
499 /* write elf header, PT_NOTE and elf note to vmcore. */
500 static void dump_begin(DumpState *s, Error **errp)
502 Error *local_err = NULL;
505 * the vmcore's format is:
506 * --------------
507 * | elf header |
508 * --------------
509 * | PT_NOTE |
510 * --------------
511 * | PT_LOAD |
512 * --------------
513 * | ...... |
514 * --------------
515 * | PT_LOAD |
516 * --------------
517 * | sec_hdr |
518 * --------------
519 * | elf note |
520 * --------------
521 * | memory |
522 * --------------
524 * we only know where the memory is saved after we write elf note into
525 * vmcore.
528 /* write elf header to vmcore */
529 if (s->dump_info.d_class == ELFCLASS64) {
530 write_elf64_header(s, &local_err);
531 } else {
532 write_elf32_header(s, &local_err);
534 if (local_err) {
535 error_propagate(errp, local_err);
536 return;
539 if (s->dump_info.d_class == ELFCLASS64) {
540 /* write PT_NOTE to vmcore */
541 write_elf64_note(s, &local_err);
542 if (local_err) {
543 error_propagate(errp, local_err);
544 return;
547 /* write all PT_LOAD to vmcore */
548 write_elf_loads(s, &local_err);
549 if (local_err) {
550 error_propagate(errp, local_err);
551 return;
554 /* write section to vmcore */
555 if (s->have_section) {
556 write_elf_section(s, 1, &local_err);
557 if (local_err) {
558 error_propagate(errp, local_err);
559 return;
563 /* write notes to vmcore */
564 write_elf64_notes(fd_write_vmcore, s, &local_err);
565 if (local_err) {
566 error_propagate(errp, local_err);
567 return;
569 } else {
570 /* write PT_NOTE to vmcore */
571 write_elf32_note(s, &local_err);
572 if (local_err) {
573 error_propagate(errp, local_err);
574 return;
577 /* write all PT_LOAD to vmcore */
578 write_elf_loads(s, &local_err);
579 if (local_err) {
580 error_propagate(errp, local_err);
581 return;
584 /* write section to vmcore */
585 if (s->have_section) {
586 write_elf_section(s, 0, &local_err);
587 if (local_err) {
588 error_propagate(errp, local_err);
589 return;
593 /* write notes to vmcore */
594 write_elf32_notes(fd_write_vmcore, s, &local_err);
595 if (local_err) {
596 error_propagate(errp, local_err);
597 return;
602 static int get_next_block(DumpState *s, GuestPhysBlock *block)
604 while (1) {
605 block = QTAILQ_NEXT(block, next);
606 if (!block) {
607 /* no more block */
608 return 1;
611 s->start = 0;
612 s->next_block = block;
613 if (s->has_filter) {
614 if (block->target_start >= s->begin + s->length ||
615 block->target_end <= s->begin) {
616 /* This block is out of the range */
617 continue;
620 if (s->begin > block->target_start) {
621 s->start = s->begin - block->target_start;
625 return 0;
629 /* write all memory to vmcore */
630 static void dump_iterate(DumpState *s, Error **errp)
632 GuestPhysBlock *block;
633 int64_t size;
634 Error *local_err = NULL;
636 do {
637 block = s->next_block;
639 size = block->target_end - block->target_start;
640 if (s->has_filter) {
641 size -= s->start;
642 if (s->begin + s->length < block->target_end) {
643 size -= block->target_end - (s->begin + s->length);
646 write_memory(s, block, s->start, size, &local_err);
647 if (local_err) {
648 error_propagate(errp, local_err);
649 return;
652 } while (!get_next_block(s, block));
655 static void create_vmcore(DumpState *s, Error **errp)
657 Error *local_err = NULL;
659 dump_begin(s, &local_err);
660 if (local_err) {
661 error_propagate(errp, local_err);
662 return;
665 dump_iterate(s, errp);
668 static int write_start_flat_header(int fd)
670 MakedumpfileHeader *mh;
671 int ret = 0;
673 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
674 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
676 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
677 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
679 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
680 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
682 size_t written_size;
683 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
684 if (written_size != MAX_SIZE_MDF_HEADER) {
685 ret = -1;
688 g_free(mh);
689 return ret;
692 static int write_end_flat_header(int fd)
694 MakedumpfileDataHeader mdh;
696 mdh.offset = END_FLAG_FLAT_HEADER;
697 mdh.buf_size = END_FLAG_FLAT_HEADER;
699 size_t written_size;
700 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
701 if (written_size != sizeof(mdh)) {
702 return -1;
705 return 0;
708 static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
710 size_t written_size;
711 MakedumpfileDataHeader mdh;
713 mdh.offset = cpu_to_be64(offset);
714 mdh.buf_size = cpu_to_be64(size);
716 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
717 if (written_size != sizeof(mdh)) {
718 return -1;
721 written_size = qemu_write_full(fd, buf, size);
722 if (written_size != size) {
723 return -1;
726 return 0;
729 static int buf_write_note(const void *buf, size_t size, void *opaque)
731 DumpState *s = opaque;
733 /* note_buf is not enough */
734 if (s->note_buf_offset + size > s->note_size) {
735 return -1;
738 memcpy(s->note_buf + s->note_buf_offset, buf, size);
740 s->note_buf_offset += size;
742 return 0;
746 * This function retrieves various sizes from an elf header.
748 * @note has to be a valid ELF note. The return sizes are unmodified
749 * (not padded or rounded up to be multiple of 4).
751 static void get_note_sizes(DumpState *s, const void *note,
752 uint64_t *note_head_size,
753 uint64_t *name_size,
754 uint64_t *desc_size)
756 uint64_t note_head_sz;
757 uint64_t name_sz;
758 uint64_t desc_sz;
760 if (s->dump_info.d_class == ELFCLASS64) {
761 const Elf64_Nhdr *hdr = note;
762 note_head_sz = sizeof(Elf64_Nhdr);
763 name_sz = tswap64(hdr->n_namesz);
764 desc_sz = tswap64(hdr->n_descsz);
765 } else {
766 const Elf32_Nhdr *hdr = note;
767 note_head_sz = sizeof(Elf32_Nhdr);
768 name_sz = tswap32(hdr->n_namesz);
769 desc_sz = tswap32(hdr->n_descsz);
772 if (note_head_size) {
773 *note_head_size = note_head_sz;
775 if (name_size) {
776 *name_size = name_sz;
778 if (desc_size) {
779 *desc_size = desc_sz;
783 static bool note_name_equal(DumpState *s,
784 const uint8_t *note, const char *name)
786 int len = strlen(name) + 1;
787 uint64_t head_size, name_size;
789 get_note_sizes(s, note, &head_size, &name_size, NULL);
790 head_size = ROUND_UP(head_size, 4);
792 return name_size == len && memcmp(note + head_size, name, len) == 0;
795 /* write common header, sub header and elf note to vmcore */
796 static void create_header32(DumpState *s, Error **errp)
798 DiskDumpHeader32 *dh = NULL;
799 KdumpSubHeader32 *kh = NULL;
800 size_t size;
801 uint32_t block_size;
802 uint32_t sub_hdr_size;
803 uint32_t bitmap_blocks;
804 uint32_t status = 0;
805 uint64_t offset_note;
806 Error *local_err = NULL;
808 /* write common header, the version of kdump-compressed format is 6th */
809 size = sizeof(DiskDumpHeader32);
810 dh = g_malloc0(size);
812 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
813 dh->header_version = cpu_to_dump32(s, 6);
814 block_size = s->dump_info.page_size;
815 dh->block_size = cpu_to_dump32(s, block_size);
816 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
817 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
818 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
819 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
820 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
821 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
822 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
823 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
824 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
826 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
827 status |= DUMP_DH_COMPRESSED_ZLIB;
829 #ifdef CONFIG_LZO
830 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
831 status |= DUMP_DH_COMPRESSED_LZO;
833 #endif
834 #ifdef CONFIG_SNAPPY
835 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
836 status |= DUMP_DH_COMPRESSED_SNAPPY;
838 #endif
839 dh->status = cpu_to_dump32(s, status);
841 if (write_buffer(s->fd, 0, dh, size) < 0) {
842 error_setg(errp, "dump: failed to write disk dump header");
843 goto out;
846 /* write sub header */
847 size = sizeof(KdumpSubHeader32);
848 kh = g_malloc0(size);
850 /* 64bit max_mapnr_64 */
851 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
852 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
853 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
855 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
856 if (s->guest_note &&
857 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
858 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
860 get_note_sizes(s, s->guest_note,
861 &hsize, &name_size, &size_vmcoreinfo_desc);
862 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
863 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
864 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
865 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
868 kh->offset_note = cpu_to_dump64(s, offset_note);
869 kh->note_size = cpu_to_dump32(s, s->note_size);
871 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
872 block_size, kh, size) < 0) {
873 error_setg(errp, "dump: failed to write kdump sub header");
874 goto out;
877 /* write note */
878 s->note_buf = g_malloc0(s->note_size);
879 s->note_buf_offset = 0;
881 /* use s->note_buf to store notes temporarily */
882 write_elf32_notes(buf_write_note, s, &local_err);
883 if (local_err) {
884 error_propagate(errp, local_err);
885 goto out;
887 if (write_buffer(s->fd, offset_note, s->note_buf,
888 s->note_size) < 0) {
889 error_setg(errp, "dump: failed to write notes");
890 goto out;
893 /* get offset of dump_bitmap */
894 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
895 block_size;
897 /* get offset of page */
898 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
899 block_size;
901 out:
902 g_free(dh);
903 g_free(kh);
904 g_free(s->note_buf);
907 /* write common header, sub header and elf note to vmcore */
908 static void create_header64(DumpState *s, Error **errp)
910 DiskDumpHeader64 *dh = NULL;
911 KdumpSubHeader64 *kh = NULL;
912 size_t size;
913 uint32_t block_size;
914 uint32_t sub_hdr_size;
915 uint32_t bitmap_blocks;
916 uint32_t status = 0;
917 uint64_t offset_note;
918 Error *local_err = NULL;
920 /* write common header, the version of kdump-compressed format is 6th */
921 size = sizeof(DiskDumpHeader64);
922 dh = g_malloc0(size);
924 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
925 dh->header_version = cpu_to_dump32(s, 6);
926 block_size = s->dump_info.page_size;
927 dh->block_size = cpu_to_dump32(s, block_size);
928 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
929 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
930 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
931 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
932 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
933 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
934 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
935 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
936 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
938 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
939 status |= DUMP_DH_COMPRESSED_ZLIB;
941 #ifdef CONFIG_LZO
942 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
943 status |= DUMP_DH_COMPRESSED_LZO;
945 #endif
946 #ifdef CONFIG_SNAPPY
947 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
948 status |= DUMP_DH_COMPRESSED_SNAPPY;
950 #endif
951 dh->status = cpu_to_dump32(s, status);
953 if (write_buffer(s->fd, 0, dh, size) < 0) {
954 error_setg(errp, "dump: failed to write disk dump header");
955 goto out;
958 /* write sub header */
959 size = sizeof(KdumpSubHeader64);
960 kh = g_malloc0(size);
962 /* 64bit max_mapnr_64 */
963 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
964 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
965 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
967 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
968 if (s->guest_note &&
969 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
970 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
972 get_note_sizes(s, s->guest_note,
973 &hsize, &name_size, &size_vmcoreinfo_desc);
974 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
975 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
976 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
977 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
980 kh->offset_note = cpu_to_dump64(s, offset_note);
981 kh->note_size = cpu_to_dump64(s, s->note_size);
983 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
984 block_size, kh, size) < 0) {
985 error_setg(errp, "dump: failed to write kdump sub header");
986 goto out;
989 /* write note */
990 s->note_buf = g_malloc0(s->note_size);
991 s->note_buf_offset = 0;
993 /* use s->note_buf to store notes temporarily */
994 write_elf64_notes(buf_write_note, s, &local_err);
995 if (local_err) {
996 error_propagate(errp, local_err);
997 goto out;
1000 if (write_buffer(s->fd, offset_note, s->note_buf,
1001 s->note_size) < 0) {
1002 error_setg(errp, "dump: failed to write notes");
1003 goto out;
1006 /* get offset of dump_bitmap */
1007 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1008 block_size;
1010 /* get offset of page */
1011 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1012 block_size;
1014 out:
1015 g_free(dh);
1016 g_free(kh);
1017 g_free(s->note_buf);
1020 static void write_dump_header(DumpState *s, Error **errp)
1022 Error *local_err = NULL;
1024 if (s->dump_info.d_class == ELFCLASS32) {
1025 create_header32(s, &local_err);
1026 } else {
1027 create_header64(s, &local_err);
1029 error_propagate(errp, local_err);
1032 static size_t dump_bitmap_get_bufsize(DumpState *s)
1034 return s->dump_info.page_size;
1038 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1039 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1040 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1041 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1042 * vmcore, ie. synchronizing un-sync bit into vmcore.
1044 static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1045 uint8_t *buf, DumpState *s)
1047 off_t old_offset, new_offset;
1048 off_t offset_bitmap1, offset_bitmap2;
1049 uint32_t byte, bit;
1050 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1051 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1053 /* should not set the previous place */
1054 assert(last_pfn <= pfn);
1057 * if the bit needed to be set is not cached in buf, flush the data in buf
1058 * to vmcore firstly.
1059 * making new_offset be bigger than old_offset can also sync remained data
1060 * into vmcore.
1062 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1063 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
1065 while (old_offset < new_offset) {
1066 /* calculate the offset and write dump_bitmap */
1067 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1068 if (write_buffer(s->fd, offset_bitmap1, buf,
1069 bitmap_bufsize) < 0) {
1070 return -1;
1073 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1074 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1075 old_offset;
1076 if (write_buffer(s->fd, offset_bitmap2, buf,
1077 bitmap_bufsize) < 0) {
1078 return -1;
1081 memset(buf, 0, bitmap_bufsize);
1082 old_offset += bitmap_bufsize;
1085 /* get the exact place of the bit in the buf, and set it */
1086 byte = (pfn % bits_per_buf) / CHAR_BIT;
1087 bit = (pfn % bits_per_buf) % CHAR_BIT;
1088 if (value) {
1089 buf[byte] |= 1u << bit;
1090 } else {
1091 buf[byte] &= ~(1u << bit);
1094 return 0;
1097 static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1099 int target_page_shift = ctz32(s->dump_info.page_size);
1101 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1104 static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1106 int target_page_shift = ctz32(s->dump_info.page_size);
1108 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1112 * exam every page and return the page frame number and the address of the page.
1113 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1114 * blocks, so block->target_start and block->target_end should be interal
1115 * multiples of the target page size.
1117 static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1118 uint8_t **bufptr, DumpState *s)
1120 GuestPhysBlock *block = *blockptr;
1121 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
1122 uint8_t *buf;
1124 /* block == NULL means the start of the iteration */
1125 if (!block) {
1126 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1127 *blockptr = block;
1128 assert((block->target_start & ~target_page_mask) == 0);
1129 assert((block->target_end & ~target_page_mask) == 0);
1130 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
1131 if (bufptr) {
1132 *bufptr = block->host_addr;
1134 return true;
1137 *pfnptr = *pfnptr + 1;
1138 addr = dump_pfn_to_paddr(s, *pfnptr);
1140 if ((addr >= block->target_start) &&
1141 (addr + s->dump_info.page_size <= block->target_end)) {
1142 buf = block->host_addr + (addr - block->target_start);
1143 } else {
1144 /* the next page is in the next block */
1145 block = QTAILQ_NEXT(block, next);
1146 *blockptr = block;
1147 if (!block) {
1148 return false;
1150 assert((block->target_start & ~target_page_mask) == 0);
1151 assert((block->target_end & ~target_page_mask) == 0);
1152 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
1153 buf = block->host_addr;
1156 if (bufptr) {
1157 *bufptr = buf;
1160 return true;
1163 static void write_dump_bitmap(DumpState *s, Error **errp)
1165 int ret = 0;
1166 uint64_t last_pfn, pfn;
1167 void *dump_bitmap_buf;
1168 size_t num_dumpable;
1169 GuestPhysBlock *block_iter = NULL;
1170 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1171 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1173 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1174 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
1176 num_dumpable = 0;
1177 last_pfn = 0;
1180 * exam memory page by page, and set the bit in dump_bitmap corresponded
1181 * to the existing page.
1183 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1184 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1185 if (ret < 0) {
1186 error_setg(errp, "dump: failed to set dump_bitmap");
1187 goto out;
1190 last_pfn = pfn;
1191 num_dumpable++;
1195 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1196 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1197 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1199 if (num_dumpable > 0) {
1200 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
1201 dump_bitmap_buf, s);
1202 if (ret < 0) {
1203 error_setg(errp, "dump: failed to sync dump_bitmap");
1204 goto out;
1208 /* number of dumpable pages that will be dumped later */
1209 s->num_dumpable = num_dumpable;
1211 out:
1212 g_free(dump_bitmap_buf);
1215 static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1216 off_t offset)
1218 data_cache->fd = s->fd;
1219 data_cache->data_size = 0;
1220 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1221 data_cache->buf = g_malloc0(data_cache->buf_size);
1222 data_cache->offset = offset;
1225 static int write_cache(DataCache *dc, const void *buf, size_t size,
1226 bool flag_sync)
1229 * dc->buf_size should not be less than size, otherwise dc will never be
1230 * enough
1232 assert(size <= dc->buf_size);
1235 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1236 * otherwise check if the space is enough for caching data in buf, if not,
1237 * write the data in dc->buf to dc->fd and reset dc->buf
1239 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1240 (flag_sync && dc->data_size > 0)) {
1241 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1242 return -1;
1245 dc->offset += dc->data_size;
1246 dc->data_size = 0;
1249 if (!flag_sync) {
1250 memcpy(dc->buf + dc->data_size, buf, size);
1251 dc->data_size += size;
1254 return 0;
1257 static void free_data_cache(DataCache *data_cache)
1259 g_free(data_cache->buf);
1262 static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1264 switch (flag_compress) {
1265 case DUMP_DH_COMPRESSED_ZLIB:
1266 return compressBound(page_size);
1268 case DUMP_DH_COMPRESSED_LZO:
1270 * LZO will expand incompressible data by a little amount. Please check
1271 * the following URL to see the expansion calculation:
1272 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1274 return page_size + page_size / 16 + 64 + 3;
1276 #ifdef CONFIG_SNAPPY
1277 case DUMP_DH_COMPRESSED_SNAPPY:
1278 return snappy_max_compressed_length(page_size);
1279 #endif
1281 return 0;
1285 * check if the page is all 0
1287 static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1289 return buffer_is_zero(buf, page_size);
1292 static void write_dump_pages(DumpState *s, Error **errp)
1294 int ret = 0;
1295 DataCache page_desc, page_data;
1296 size_t len_buf_out, size_out;
1297 #ifdef CONFIG_LZO
1298 lzo_bytep wrkmem = NULL;
1299 #endif
1300 uint8_t *buf_out = NULL;
1301 off_t offset_desc, offset_data;
1302 PageDescriptor pd, pd_zero;
1303 uint8_t *buf;
1304 GuestPhysBlock *block_iter = NULL;
1305 uint64_t pfn_iter;
1307 /* get offset of page_desc and page_data in dump file */
1308 offset_desc = s->offset_page;
1309 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1311 prepare_data_cache(&page_desc, s, offset_desc);
1312 prepare_data_cache(&page_data, s, offset_data);
1314 /* prepare buffer to store compressed data */
1315 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
1316 assert(len_buf_out != 0);
1318 #ifdef CONFIG_LZO
1319 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1320 #endif
1322 buf_out = g_malloc(len_buf_out);
1325 * init zero page's page_desc and page_data, because every zero page
1326 * uses the same page_data
1328 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
1329 pd_zero.flags = cpu_to_dump32(s, 0);
1330 pd_zero.offset = cpu_to_dump64(s, offset_data);
1331 pd_zero.page_flags = cpu_to_dump64(s, 0);
1332 buf = g_malloc0(s->dump_info.page_size);
1333 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
1334 g_free(buf);
1335 if (ret < 0) {
1336 error_setg(errp, "dump: failed to write page data (zero page)");
1337 goto out;
1340 offset_data += s->dump_info.page_size;
1343 * dump memory to vmcore page by page. zero page will all be resided in the
1344 * first page of page section
1346 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1347 /* check zero page */
1348 if (is_zero_page(buf, s->dump_info.page_size)) {
1349 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1350 false);
1351 if (ret < 0) {
1352 error_setg(errp, "dump: failed to write page desc");
1353 goto out;
1355 } else {
1357 * not zero page, then:
1358 * 1. compress the page
1359 * 2. write the compressed page into the cache of page_data
1360 * 3. get page desc of the compressed page and write it into the
1361 * cache of page_desc
1363 * only one compression format will be used here, for
1364 * s->flag_compress is set. But when compression fails to work,
1365 * we fall back to save in plaintext.
1367 size_out = len_buf_out;
1368 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
1369 (compress2(buf_out, (uLongf *)&size_out, buf,
1370 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1371 (size_out < s->dump_info.page_size)) {
1372 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1373 pd.size = cpu_to_dump32(s, size_out);
1375 ret = write_cache(&page_data, buf_out, size_out, false);
1376 if (ret < 0) {
1377 error_setg(errp, "dump: failed to write page data");
1378 goto out;
1380 #ifdef CONFIG_LZO
1381 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
1382 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
1383 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
1384 (size_out < s->dump_info.page_size)) {
1385 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1386 pd.size = cpu_to_dump32(s, size_out);
1388 ret = write_cache(&page_data, buf_out, size_out, false);
1389 if (ret < 0) {
1390 error_setg(errp, "dump: failed to write page data");
1391 goto out;
1393 #endif
1394 #ifdef CONFIG_SNAPPY
1395 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
1396 (snappy_compress((char *)buf, s->dump_info.page_size,
1397 (char *)buf_out, &size_out) == SNAPPY_OK) &&
1398 (size_out < s->dump_info.page_size)) {
1399 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1400 pd.size = cpu_to_dump32(s, size_out);
1402 ret = write_cache(&page_data, buf_out, size_out, false);
1403 if (ret < 0) {
1404 error_setg(errp, "dump: failed to write page data");
1405 goto out;
1407 #endif
1408 } else {
1410 * fall back to save in plaintext, size_out should be
1411 * assigned the target's page size
1413 pd.flags = cpu_to_dump32(s, 0);
1414 size_out = s->dump_info.page_size;
1415 pd.size = cpu_to_dump32(s, size_out);
1417 ret = write_cache(&page_data, buf,
1418 s->dump_info.page_size, false);
1419 if (ret < 0) {
1420 error_setg(errp, "dump: failed to write page data");
1421 goto out;
1425 /* get and write page desc here */
1426 pd.page_flags = cpu_to_dump64(s, 0);
1427 pd.offset = cpu_to_dump64(s, offset_data);
1428 offset_data += size_out;
1430 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1431 if (ret < 0) {
1432 error_setg(errp, "dump: failed to write page desc");
1433 goto out;
1436 s->written_size += s->dump_info.page_size;
1439 ret = write_cache(&page_desc, NULL, 0, true);
1440 if (ret < 0) {
1441 error_setg(errp, "dump: failed to sync cache for page_desc");
1442 goto out;
1444 ret = write_cache(&page_data, NULL, 0, true);
1445 if (ret < 0) {
1446 error_setg(errp, "dump: failed to sync cache for page_data");
1447 goto out;
1450 out:
1451 free_data_cache(&page_desc);
1452 free_data_cache(&page_data);
1454 #ifdef CONFIG_LZO
1455 g_free(wrkmem);
1456 #endif
1458 g_free(buf_out);
1461 static void create_kdump_vmcore(DumpState *s, Error **errp)
1463 int ret;
1464 Error *local_err = NULL;
1467 * the kdump-compressed format is:
1468 * File offset
1469 * +------------------------------------------+ 0x0
1470 * | main header (struct disk_dump_header) |
1471 * |------------------------------------------+ block 1
1472 * | sub header (struct kdump_sub_header) |
1473 * |------------------------------------------+ block 2
1474 * | 1st-dump_bitmap |
1475 * |------------------------------------------+ block 2 + X blocks
1476 * | 2nd-dump_bitmap | (aligned by block)
1477 * |------------------------------------------+ block 2 + 2 * X blocks
1478 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1479 * | page desc for pfn 1 (struct page_desc) |
1480 * | : |
1481 * |------------------------------------------| (not aligned by block)
1482 * | page data (pfn 0) |
1483 * | page data (pfn 1) |
1484 * | : |
1485 * +------------------------------------------+
1488 ret = write_start_flat_header(s->fd);
1489 if (ret < 0) {
1490 error_setg(errp, "dump: failed to write start flat header");
1491 return;
1494 write_dump_header(s, &local_err);
1495 if (local_err) {
1496 error_propagate(errp, local_err);
1497 return;
1500 write_dump_bitmap(s, &local_err);
1501 if (local_err) {
1502 error_propagate(errp, local_err);
1503 return;
1506 write_dump_pages(s, &local_err);
1507 if (local_err) {
1508 error_propagate(errp, local_err);
1509 return;
1512 ret = write_end_flat_header(s->fd);
1513 if (ret < 0) {
1514 error_setg(errp, "dump: failed to write end flat header");
1515 return;
1519 static ram_addr_t get_start_block(DumpState *s)
1521 GuestPhysBlock *block;
1523 if (!s->has_filter) {
1524 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1525 return 0;
1528 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1529 if (block->target_start >= s->begin + s->length ||
1530 block->target_end <= s->begin) {
1531 /* This block is out of the range */
1532 continue;
1535 s->next_block = block;
1536 if (s->begin > block->target_start) {
1537 s->start = s->begin - block->target_start;
1538 } else {
1539 s->start = 0;
1541 return s->start;
1544 return -1;
1547 static void get_max_mapnr(DumpState *s)
1549 GuestPhysBlock *last_block;
1551 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead);
1552 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
1555 static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1557 static void dump_state_prepare(DumpState *s)
1559 /* zero the struct, setting status to active */
1560 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1563 bool dump_in_progress(void)
1565 DumpState *state = &dump_state_global;
1566 return (atomic_read(&state->status) == DUMP_STATUS_ACTIVE);
1569 /* calculate total size of memory to be dumped (taking filter into
1570 * acoount.) */
1571 static int64_t dump_calculate_size(DumpState *s)
1573 GuestPhysBlock *block;
1574 int64_t size = 0, total = 0, left = 0, right = 0;
1576 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1577 if (s->has_filter) {
1578 /* calculate the overlapped region. */
1579 left = MAX(s->begin, block->target_start);
1580 right = MIN(s->begin + s->length, block->target_end);
1581 size = right - left;
1582 size = size > 0 ? size : 0;
1583 } else {
1584 /* count the whole region in */
1585 size = (block->target_end - block->target_start);
1587 total += size;
1590 return total;
1593 static void vmcoreinfo_update_phys_base(DumpState *s)
1595 uint64_t size, note_head_size, name_size, phys_base;
1596 char **lines;
1597 uint8_t *vmci;
1598 size_t i;
1600 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1601 return;
1604 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1605 note_head_size = ROUND_UP(note_head_size, 4);
1607 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1608 *(vmci + size) = '\0';
1610 lines = g_strsplit((char *)vmci, "\n", -1);
1611 for (i = 0; lines[i]; i++) {
1612 const char *prefix = NULL;
1614 if (s->dump_info.d_machine == EM_X86_64) {
1615 prefix = "NUMBER(phys_base)=";
1616 } else if (s->dump_info.d_machine == EM_AARCH64) {
1617 prefix = "NUMBER(PHYS_OFFSET)=";
1620 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1621 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
1622 &phys_base) < 0) {
1623 warn_report("Failed to read %s", prefix);
1624 } else {
1625 s->dump_info.phys_base = phys_base;
1627 break;
1631 g_strfreev(lines);
1634 static void dump_init(DumpState *s, int fd, bool has_format,
1635 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1636 int64_t begin, int64_t length, Error **errp)
1638 VMCoreInfoState *vmci = vmcoreinfo_find();
1639 CPUState *cpu;
1640 int nr_cpus;
1641 Error *err = NULL;
1642 int ret;
1644 s->has_format = has_format;
1645 s->format = format;
1646 s->written_size = 0;
1648 /* kdump-compressed is conflict with paging and filter */
1649 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1650 assert(!paging && !has_filter);
1653 if (runstate_is_running()) {
1654 vm_stop(RUN_STATE_SAVE_VM);
1655 s->resume = true;
1656 } else {
1657 s->resume = false;
1660 /* If we use KVM, we should synchronize the registers before we get dump
1661 * info or physmap info.
1663 cpu_synchronize_all_states();
1664 nr_cpus = 0;
1665 CPU_FOREACH(cpu) {
1666 nr_cpus++;
1669 s->fd = fd;
1670 s->has_filter = has_filter;
1671 s->begin = begin;
1672 s->length = length;
1674 memory_mapping_list_init(&s->list);
1676 guest_phys_blocks_init(&s->guest_phys_blocks);
1677 guest_phys_blocks_append(&s->guest_phys_blocks);
1678 s->total_size = dump_calculate_size(s);
1679 #ifdef DEBUG_DUMP_GUEST_MEMORY
1680 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1681 #endif
1683 /* it does not make sense to dump non-existent memory */
1684 if (!s->total_size) {
1685 error_setg(errp, "dump: no guest memory to dump");
1686 goto cleanup;
1689 s->start = get_start_block(s);
1690 if (s->start == -1) {
1691 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
1692 goto cleanup;
1695 /* get dump info: endian, class and architecture.
1696 * If the target architecture is not supported, cpu_get_dump_info() will
1697 * return -1.
1699 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
1700 if (ret < 0) {
1701 error_setg(errp, QERR_UNSUPPORTED);
1702 goto cleanup;
1705 if (!s->dump_info.page_size) {
1706 s->dump_info.page_size = TARGET_PAGE_SIZE;
1709 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1710 s->dump_info.d_machine, nr_cpus);
1711 if (s->note_size < 0) {
1712 error_setg(errp, QERR_UNSUPPORTED);
1713 goto cleanup;
1717 * The goal of this block is to (a) update the previously guessed
1718 * phys_base, (b) copy the guest note out of the guest.
1719 * Failure to do so is not fatal for dumping.
1721 if (vmci) {
1722 uint64_t addr, note_head_size, name_size, desc_size;
1723 uint32_t size;
1724 uint16_t format;
1726 note_head_size = s->dump_info.d_class == ELFCLASS32 ?
1727 sizeof(Elf32_Nhdr) : sizeof(Elf64_Nhdr);
1729 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1730 size = le32_to_cpu(vmci->vmcoreinfo.size);
1731 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1732 if (!vmci->has_vmcoreinfo) {
1733 warn_report("guest note is not present");
1734 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1735 warn_report("guest note size is invalid: %" PRIu32, size);
1736 } else if (format != VMCOREINFO_FORMAT_ELF) {
1737 warn_report("guest note format is unsupported: %" PRIu16, format);
1738 } else {
1739 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1740 cpu_physical_memory_read(addr, s->guest_note, size);
1742 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1743 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1744 desc_size);
1745 if (name_size > MAX_GUEST_NOTE_SIZE ||
1746 desc_size > MAX_GUEST_NOTE_SIZE ||
1747 s->guest_note_size > size) {
1748 warn_report("Invalid guest note header");
1749 g_free(s->guest_note);
1750 s->guest_note = NULL;
1751 } else {
1752 vmcoreinfo_update_phys_base(s);
1753 s->note_size += s->guest_note_size;
1758 /* get memory mapping */
1759 if (paging) {
1760 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
1761 if (err != NULL) {
1762 error_propagate(errp, err);
1763 goto cleanup;
1765 } else {
1766 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
1769 s->nr_cpus = nr_cpus;
1771 get_max_mapnr(s);
1773 uint64_t tmp;
1774 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1775 s->dump_info.page_size);
1776 s->len_dump_bitmap = tmp * s->dump_info.page_size;
1778 /* init for kdump-compressed format */
1779 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1780 switch (format) {
1781 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1782 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1783 break;
1785 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
1786 #ifdef CONFIG_LZO
1787 if (lzo_init() != LZO_E_OK) {
1788 error_setg(errp, "failed to initialize the LZO library");
1789 goto cleanup;
1791 #endif
1792 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1793 break;
1795 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1796 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1797 break;
1799 default:
1800 s->flag_compress = 0;
1803 return;
1806 if (s->has_filter) {
1807 memory_mapping_filter(&s->list, s->begin, s->length);
1811 * calculate phdr_num
1813 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1815 s->phdr_num = 1; /* PT_NOTE */
1816 if (s->list.num < UINT16_MAX - 2) {
1817 s->phdr_num += s->list.num;
1818 s->have_section = false;
1819 } else {
1820 s->have_section = true;
1821 s->phdr_num = PN_XNUM;
1822 s->sh_info = 1; /* PT_NOTE */
1824 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1825 if (s->list.num <= UINT32_MAX - 1) {
1826 s->sh_info += s->list.num;
1827 } else {
1828 s->sh_info = UINT32_MAX;
1832 if (s->dump_info.d_class == ELFCLASS64) {
1833 if (s->have_section) {
1834 s->memory_offset = sizeof(Elf64_Ehdr) +
1835 sizeof(Elf64_Phdr) * s->sh_info +
1836 sizeof(Elf64_Shdr) + s->note_size;
1837 } else {
1838 s->memory_offset = sizeof(Elf64_Ehdr) +
1839 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1841 } else {
1842 if (s->have_section) {
1843 s->memory_offset = sizeof(Elf32_Ehdr) +
1844 sizeof(Elf32_Phdr) * s->sh_info +
1845 sizeof(Elf32_Shdr) + s->note_size;
1846 } else {
1847 s->memory_offset = sizeof(Elf32_Ehdr) +
1848 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1852 return;
1854 cleanup:
1855 dump_cleanup(s);
1858 /* this operation might be time consuming. */
1859 static void dump_process(DumpState *s, Error **errp)
1861 Error *local_err = NULL;
1862 DumpQueryResult *result = NULL;
1864 if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1865 create_kdump_vmcore(s, &local_err);
1866 } else {
1867 create_vmcore(s, &local_err);
1870 /* make sure status is written after written_size updates */
1871 smp_wmb();
1872 atomic_set(&s->status,
1873 (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
1875 /* send DUMP_COMPLETED message (unconditionally) */
1876 result = qmp_query_dump(NULL);
1877 /* should never fail */
1878 assert(result);
1879 qapi_event_send_dump_completed(result, !!local_err, (local_err ? \
1880 error_get_pretty(local_err) : NULL),
1881 &error_abort);
1882 qapi_free_DumpQueryResult(result);
1884 error_propagate(errp, local_err);
1885 dump_cleanup(s);
1888 static void *dump_thread(void *data)
1890 DumpState *s = (DumpState *)data;
1891 dump_process(s, NULL);
1892 return NULL;
1895 DumpQueryResult *qmp_query_dump(Error **errp)
1897 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1898 DumpState *state = &dump_state_global;
1899 result->status = atomic_read(&state->status);
1900 /* make sure we are reading status and written_size in order */
1901 smp_rmb();
1902 result->completed = state->written_size;
1903 result->total = state->total_size;
1904 return result;
1907 void qmp_dump_guest_memory(bool paging, const char *file,
1908 bool has_detach, bool detach,
1909 bool has_begin, int64_t begin, bool has_length,
1910 int64_t length, bool has_format,
1911 DumpGuestMemoryFormat format, Error **errp)
1913 const char *p;
1914 int fd = -1;
1915 DumpState *s;
1916 Error *local_err = NULL;
1917 bool detach_p = false;
1919 if (runstate_check(RUN_STATE_INMIGRATE)) {
1920 error_setg(errp, "Dump not allowed during incoming migration.");
1921 return;
1924 /* if there is a dump in background, we should wait until the dump
1925 * finished */
1926 if (dump_in_progress()) {
1927 error_setg(errp, "There is a dump in process, please wait.");
1928 return;
1932 * kdump-compressed format need the whole memory dumped, so paging or
1933 * filter is not supported here.
1935 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1936 (paging || has_begin || has_length)) {
1937 error_setg(errp, "kdump-compressed format doesn't support paging or "
1938 "filter");
1939 return;
1941 if (has_begin && !has_length) {
1942 error_setg(errp, QERR_MISSING_PARAMETER, "length");
1943 return;
1945 if (!has_begin && has_length) {
1946 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
1947 return;
1949 if (has_detach) {
1950 detach_p = detach;
1953 /* check whether lzo/snappy is supported */
1954 #ifndef CONFIG_LZO
1955 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1956 error_setg(errp, "kdump-lzo is not available now");
1957 return;
1959 #endif
1961 #ifndef CONFIG_SNAPPY
1962 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1963 error_setg(errp, "kdump-snappy is not available now");
1964 return;
1966 #endif
1968 #if !defined(WIN32)
1969 if (strstart(file, "fd:", &p)) {
1970 fd = monitor_get_fd(cur_mon, p, errp);
1971 if (fd == -1) {
1972 return;
1975 #endif
1977 if (strstart(file, "file:", &p)) {
1978 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1979 if (fd < 0) {
1980 error_setg_file_open(errp, errno, p);
1981 return;
1985 if (fd == -1) {
1986 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
1987 return;
1990 s = &dump_state_global;
1991 dump_state_prepare(s);
1993 dump_init(s, fd, has_format, format, paging, has_begin,
1994 begin, length, &local_err);
1995 if (local_err) {
1996 error_propagate(errp, local_err);
1997 atomic_set(&s->status, DUMP_STATUS_FAILED);
1998 return;
2001 if (detach_p) {
2002 /* detached dump */
2003 s->detached = true;
2004 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2005 s, QEMU_THREAD_DETACHED);
2006 } else {
2007 /* sync dump */
2008 dump_process(s, errp);
2012 DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2014 DumpGuestMemoryFormatList *item;
2015 DumpGuestMemoryCapability *cap =
2016 g_malloc0(sizeof(DumpGuestMemoryCapability));
2018 /* elf is always available */
2019 item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2020 cap->formats = item;
2021 item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
2023 /* kdump-zlib is always available */
2024 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2025 item = item->next;
2026 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2028 /* add new item if kdump-lzo is available */
2029 #ifdef CONFIG_LZO
2030 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2031 item = item->next;
2032 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2033 #endif
2035 /* add new item if kdump-snappy is available */
2036 #ifdef CONFIG_SNAPPY
2037 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2038 item = item->next;
2039 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2040 #endif
2042 return cap;