4 * Copyright Fujitsu, Corp. 2011, 2012
7 * Wen Congyang <wency@cn.fujitsu.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qemu-common.h"
16 #include "qemu/cutils.h"
18 #include "exec/hwaddr.h"
19 #include "monitor/monitor.h"
20 #include "sysemu/kvm.h"
21 #include "sysemu/dump.h"
22 #include "sysemu/memory_mapping.h"
23 #include "sysemu/runstate.h"
24 #include "sysemu/cpus.h"
25 #include "qapi/error.h"
26 #include "qapi/qapi-commands-dump.h"
27 #include "qapi/qapi-events-dump.h"
28 #include "qapi/qmp/qerror.h"
29 #include "qemu/error-report.h"
30 #include "qemu/main-loop.h"
31 #include "hw/misc/vmcoreinfo.h"
32 #include "migration/blocker.h"
40 #include <lzo/lzo1x.h>
45 #ifndef ELF_MACHINE_UNAME
46 #define ELF_MACHINE_UNAME "Unknown"
49 #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
51 static Error
*dump_migration_blocker
;
53 #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
54 ((DIV_ROUND_UP((hdr_size), 4) + \
55 DIV_ROUND_UP((name_size), 4) + \
56 DIV_ROUND_UP((desc_size), 4)) * 4)
58 uint16_t cpu_to_dump16(DumpState
*s
, uint16_t val
)
60 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
61 val
= cpu_to_le16(val
);
63 val
= cpu_to_be16(val
);
69 uint32_t cpu_to_dump32(DumpState
*s
, uint32_t val
)
71 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
72 val
= cpu_to_le32(val
);
74 val
= cpu_to_be32(val
);
80 uint64_t cpu_to_dump64(DumpState
*s
, uint64_t val
)
82 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
83 val
= cpu_to_le64(val
);
85 val
= cpu_to_be64(val
);
91 static int dump_cleanup(DumpState
*s
)
93 guest_phys_blocks_free(&s
->guest_phys_blocks
);
94 memory_mapping_list_free(&s
->list
);
96 g_free(s
->guest_note
);
100 qemu_mutex_lock_iothread();
104 qemu_mutex_unlock_iothread();
107 migrate_del_blocker(dump_migration_blocker
);
112 static int fd_write_vmcore(const void *buf
, size_t size
, void *opaque
)
114 DumpState
*s
= opaque
;
117 written_size
= qemu_write_full(s
->fd
, buf
, size
);
118 if (written_size
!= size
) {
125 static void write_elf64_header(DumpState
*s
, Error
**errp
)
127 Elf64_Ehdr elf_header
;
130 memset(&elf_header
, 0, sizeof(Elf64_Ehdr
));
131 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
132 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS64
;
133 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
134 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
135 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
136 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
137 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
138 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
139 elf_header
.e_phoff
= cpu_to_dump64(s
, sizeof(Elf64_Ehdr
));
140 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf64_Phdr
));
141 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
142 if (s
->have_section
) {
143 uint64_t shoff
= sizeof(Elf64_Ehdr
) + sizeof(Elf64_Phdr
) * s
->sh_info
;
145 elf_header
.e_shoff
= cpu_to_dump64(s
, shoff
);
146 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf64_Shdr
));
147 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
150 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
152 error_setg_errno(errp
, -ret
, "dump: failed to write elf header");
156 static void write_elf32_header(DumpState
*s
, Error
**errp
)
158 Elf32_Ehdr elf_header
;
161 memset(&elf_header
, 0, sizeof(Elf32_Ehdr
));
162 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
163 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS32
;
164 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
165 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
166 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
167 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
168 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
169 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
170 elf_header
.e_phoff
= cpu_to_dump32(s
, sizeof(Elf32_Ehdr
));
171 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf32_Phdr
));
172 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
173 if (s
->have_section
) {
174 uint32_t shoff
= sizeof(Elf32_Ehdr
) + sizeof(Elf32_Phdr
) * s
->sh_info
;
176 elf_header
.e_shoff
= cpu_to_dump32(s
, shoff
);
177 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf32_Shdr
));
178 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
181 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
183 error_setg_errno(errp
, -ret
, "dump: failed to write elf header");
187 static void write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
188 int phdr_index
, hwaddr offset
,
189 hwaddr filesz
, Error
**errp
)
194 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
195 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
196 phdr
.p_offset
= cpu_to_dump64(s
, offset
);
197 phdr
.p_paddr
= cpu_to_dump64(s
, memory_mapping
->phys_addr
);
198 phdr
.p_filesz
= cpu_to_dump64(s
, filesz
);
199 phdr
.p_memsz
= cpu_to_dump64(s
, memory_mapping
->length
);
200 phdr
.p_vaddr
= cpu_to_dump64(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
202 assert(memory_mapping
->length
>= filesz
);
204 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
206 error_setg_errno(errp
, -ret
,
207 "dump: failed to write program header table");
211 static void write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
212 int phdr_index
, hwaddr offset
,
213 hwaddr filesz
, Error
**errp
)
218 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
219 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
220 phdr
.p_offset
= cpu_to_dump32(s
, offset
);
221 phdr
.p_paddr
= cpu_to_dump32(s
, memory_mapping
->phys_addr
);
222 phdr
.p_filesz
= cpu_to_dump32(s
, filesz
);
223 phdr
.p_memsz
= cpu_to_dump32(s
, memory_mapping
->length
);
225 cpu_to_dump32(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
227 assert(memory_mapping
->length
>= filesz
);
229 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
231 error_setg_errno(errp
, -ret
,
232 "dump: failed to write program header table");
236 static void write_elf64_note(DumpState
*s
, Error
**errp
)
239 hwaddr begin
= s
->memory_offset
- s
->note_size
;
242 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
243 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
244 phdr
.p_offset
= cpu_to_dump64(s
, begin
);
246 phdr
.p_filesz
= cpu_to_dump64(s
, s
->note_size
);
247 phdr
.p_memsz
= cpu_to_dump64(s
, s
->note_size
);
250 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
252 error_setg_errno(errp
, -ret
,
253 "dump: failed to write program header table");
257 static inline int cpu_index(CPUState
*cpu
)
259 return cpu
->cpu_index
+ 1;
262 static void write_guest_note(WriteCoreDumpFunction f
, DumpState
*s
,
268 ret
= f(s
->guest_note
, s
->guest_note_size
, s
);
270 error_setg(errp
, "dump: failed to write guest note");
275 static void write_elf64_notes(WriteCoreDumpFunction f
, DumpState
*s
,
284 ret
= cpu_write_elf64_note(f
, cpu
, id
, s
);
286 error_setg(errp
, "dump: failed to write elf notes");
292 ret
= cpu_write_elf64_qemunote(f
, cpu
, s
);
294 error_setg(errp
, "dump: failed to write CPU status");
299 write_guest_note(f
, s
, errp
);
302 static void write_elf32_note(DumpState
*s
, Error
**errp
)
304 hwaddr begin
= s
->memory_offset
- s
->note_size
;
308 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
309 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
310 phdr
.p_offset
= cpu_to_dump32(s
, begin
);
312 phdr
.p_filesz
= cpu_to_dump32(s
, s
->note_size
);
313 phdr
.p_memsz
= cpu_to_dump32(s
, s
->note_size
);
316 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
318 error_setg_errno(errp
, -ret
,
319 "dump: failed to write program header table");
323 static void write_elf32_notes(WriteCoreDumpFunction f
, DumpState
*s
,
332 ret
= cpu_write_elf32_note(f
, cpu
, id
, s
);
334 error_setg(errp
, "dump: failed to write elf notes");
340 ret
= cpu_write_elf32_qemunote(f
, cpu
, s
);
342 error_setg(errp
, "dump: failed to write CPU status");
347 write_guest_note(f
, s
, errp
);
350 static void write_elf_section(DumpState
*s
, int type
, Error
**errp
)
359 shdr_size
= sizeof(Elf32_Shdr
);
360 memset(&shdr32
, 0, shdr_size
);
361 shdr32
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
364 shdr_size
= sizeof(Elf64_Shdr
);
365 memset(&shdr64
, 0, shdr_size
);
366 shdr64
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
370 ret
= fd_write_vmcore(shdr
, shdr_size
, s
);
372 error_setg_errno(errp
, -ret
,
373 "dump: failed to write section header table");
377 static void write_data(DumpState
*s
, void *buf
, int length
, Error
**errp
)
381 ret
= fd_write_vmcore(buf
, length
, s
);
383 error_setg_errno(errp
, -ret
, "dump: failed to save memory");
385 s
->written_size
+= length
;
389 /* write the memory to vmcore. 1 page per I/O. */
390 static void write_memory(DumpState
*s
, GuestPhysBlock
*block
, ram_addr_t start
,
391 int64_t size
, Error
**errp
)
394 Error
*local_err
= NULL
;
396 for (i
= 0; i
< size
/ s
->dump_info
.page_size
; i
++) {
397 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
398 s
->dump_info
.page_size
, &local_err
);
400 error_propagate(errp
, local_err
);
405 if ((size
% s
->dump_info
.page_size
) != 0) {
406 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
407 size
% s
->dump_info
.page_size
, &local_err
);
409 error_propagate(errp
, local_err
);
415 /* get the memory's offset and size in the vmcore */
416 static void get_offset_range(hwaddr phys_addr
,
417 ram_addr_t mapping_length
,
422 GuestPhysBlock
*block
;
423 hwaddr offset
= s
->memory_offset
;
424 int64_t size_in_block
, start
;
426 /* When the memory is not stored into vmcore, offset will be -1 */
431 if (phys_addr
< s
->begin
|| phys_addr
>= s
->begin
+ s
->length
) {
436 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
438 if (block
->target_start
>= s
->begin
+ s
->length
||
439 block
->target_end
<= s
->begin
) {
440 /* This block is out of the range */
444 if (s
->begin
<= block
->target_start
) {
445 start
= block
->target_start
;
450 size_in_block
= block
->target_end
- start
;
451 if (s
->begin
+ s
->length
< block
->target_end
) {
452 size_in_block
-= block
->target_end
- (s
->begin
+ s
->length
);
455 start
= block
->target_start
;
456 size_in_block
= block
->target_end
- block
->target_start
;
459 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
460 *p_offset
= phys_addr
- start
+ offset
;
462 /* The offset range mapped from the vmcore file must not spill over
463 * the GuestPhysBlock, clamp it. The rest of the mapping will be
464 * zero-filled in memory at load time; see
465 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
467 *p_filesz
= phys_addr
+ mapping_length
<= start
+ size_in_block
?
469 size_in_block
- (phys_addr
- start
);
473 offset
+= size_in_block
;
477 static void write_elf_loads(DumpState
*s
, Error
**errp
)
479 hwaddr offset
, filesz
;
480 MemoryMapping
*memory_mapping
;
481 uint32_t phdr_index
= 1;
483 Error
*local_err
= NULL
;
485 if (s
->have_section
) {
486 max_index
= s
->sh_info
;
488 max_index
= s
->phdr_num
;
491 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
492 get_offset_range(memory_mapping
->phys_addr
,
493 memory_mapping
->length
,
494 s
, &offset
, &filesz
);
495 if (s
->dump_info
.d_class
== ELFCLASS64
) {
496 write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
,
499 write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
,
504 error_propagate(errp
, local_err
);
508 if (phdr_index
>= max_index
) {
514 /* write elf header, PT_NOTE and elf note to vmcore. */
515 static void dump_begin(DumpState
*s
, Error
**errp
)
517 Error
*local_err
= NULL
;
520 * the vmcore's format is:
539 * we only know where the memory is saved after we write elf note into
543 /* write elf header to vmcore */
544 if (s
->dump_info
.d_class
== ELFCLASS64
) {
545 write_elf64_header(s
, &local_err
);
547 write_elf32_header(s
, &local_err
);
550 error_propagate(errp
, local_err
);
554 if (s
->dump_info
.d_class
== ELFCLASS64
) {
555 /* write PT_NOTE to vmcore */
556 write_elf64_note(s
, &local_err
);
558 error_propagate(errp
, local_err
);
562 /* write all PT_LOAD to vmcore */
563 write_elf_loads(s
, &local_err
);
565 error_propagate(errp
, local_err
);
569 /* write section to vmcore */
570 if (s
->have_section
) {
571 write_elf_section(s
, 1, &local_err
);
573 error_propagate(errp
, local_err
);
578 /* write notes to vmcore */
579 write_elf64_notes(fd_write_vmcore
, s
, &local_err
);
581 error_propagate(errp
, local_err
);
585 /* write PT_NOTE to vmcore */
586 write_elf32_note(s
, &local_err
);
588 error_propagate(errp
, local_err
);
592 /* write all PT_LOAD to vmcore */
593 write_elf_loads(s
, &local_err
);
595 error_propagate(errp
, local_err
);
599 /* write section to vmcore */
600 if (s
->have_section
) {
601 write_elf_section(s
, 0, &local_err
);
603 error_propagate(errp
, local_err
);
608 /* write notes to vmcore */
609 write_elf32_notes(fd_write_vmcore
, s
, &local_err
);
611 error_propagate(errp
, local_err
);
617 static int get_next_block(DumpState
*s
, GuestPhysBlock
*block
)
620 block
= QTAILQ_NEXT(block
, next
);
627 s
->next_block
= block
;
629 if (block
->target_start
>= s
->begin
+ s
->length
||
630 block
->target_end
<= s
->begin
) {
631 /* This block is out of the range */
635 if (s
->begin
> block
->target_start
) {
636 s
->start
= s
->begin
- block
->target_start
;
644 /* write all memory to vmcore */
645 static void dump_iterate(DumpState
*s
, Error
**errp
)
647 GuestPhysBlock
*block
;
649 Error
*local_err
= NULL
;
652 block
= s
->next_block
;
654 size
= block
->target_end
- block
->target_start
;
657 if (s
->begin
+ s
->length
< block
->target_end
) {
658 size
-= block
->target_end
- (s
->begin
+ s
->length
);
661 write_memory(s
, block
, s
->start
, size
, &local_err
);
663 error_propagate(errp
, local_err
);
667 } while (!get_next_block(s
, block
));
670 static void create_vmcore(DumpState
*s
, Error
**errp
)
672 Error
*local_err
= NULL
;
674 dump_begin(s
, &local_err
);
676 error_propagate(errp
, local_err
);
680 dump_iterate(s
, errp
);
683 static int write_start_flat_header(int fd
)
685 MakedumpfileHeader
*mh
;
688 QEMU_BUILD_BUG_ON(sizeof *mh
> MAX_SIZE_MDF_HEADER
);
689 mh
= g_malloc0(MAX_SIZE_MDF_HEADER
);
691 memcpy(mh
->signature
, MAKEDUMPFILE_SIGNATURE
,
692 MIN(sizeof mh
->signature
, sizeof MAKEDUMPFILE_SIGNATURE
));
694 mh
->type
= cpu_to_be64(TYPE_FLAT_HEADER
);
695 mh
->version
= cpu_to_be64(VERSION_FLAT_HEADER
);
698 written_size
= qemu_write_full(fd
, mh
, MAX_SIZE_MDF_HEADER
);
699 if (written_size
!= MAX_SIZE_MDF_HEADER
) {
707 static int write_end_flat_header(int fd
)
709 MakedumpfileDataHeader mdh
;
711 mdh
.offset
= END_FLAG_FLAT_HEADER
;
712 mdh
.buf_size
= END_FLAG_FLAT_HEADER
;
715 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
716 if (written_size
!= sizeof(mdh
)) {
723 static int write_buffer(int fd
, off_t offset
, const void *buf
, size_t size
)
726 MakedumpfileDataHeader mdh
;
728 mdh
.offset
= cpu_to_be64(offset
);
729 mdh
.buf_size
= cpu_to_be64(size
);
731 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
732 if (written_size
!= sizeof(mdh
)) {
736 written_size
= qemu_write_full(fd
, buf
, size
);
737 if (written_size
!= size
) {
744 static int buf_write_note(const void *buf
, size_t size
, void *opaque
)
746 DumpState
*s
= opaque
;
748 /* note_buf is not enough */
749 if (s
->note_buf_offset
+ size
> s
->note_size
) {
753 memcpy(s
->note_buf
+ s
->note_buf_offset
, buf
, size
);
755 s
->note_buf_offset
+= size
;
761 * This function retrieves various sizes from an elf header.
763 * @note has to be a valid ELF note. The return sizes are unmodified
764 * (not padded or rounded up to be multiple of 4).
766 static void get_note_sizes(DumpState
*s
, const void *note
,
767 uint64_t *note_head_size
,
771 uint64_t note_head_sz
;
775 if (s
->dump_info
.d_class
== ELFCLASS64
) {
776 const Elf64_Nhdr
*hdr
= note
;
777 note_head_sz
= sizeof(Elf64_Nhdr
);
778 name_sz
= tswap64(hdr
->n_namesz
);
779 desc_sz
= tswap64(hdr
->n_descsz
);
781 const Elf32_Nhdr
*hdr
= note
;
782 note_head_sz
= sizeof(Elf32_Nhdr
);
783 name_sz
= tswap32(hdr
->n_namesz
);
784 desc_sz
= tswap32(hdr
->n_descsz
);
787 if (note_head_size
) {
788 *note_head_size
= note_head_sz
;
791 *name_size
= name_sz
;
794 *desc_size
= desc_sz
;
798 static bool note_name_equal(DumpState
*s
,
799 const uint8_t *note
, const char *name
)
801 int len
= strlen(name
) + 1;
802 uint64_t head_size
, name_size
;
804 get_note_sizes(s
, note
, &head_size
, &name_size
, NULL
);
805 head_size
= ROUND_UP(head_size
, 4);
807 return name_size
== len
&& memcmp(note
+ head_size
, name
, len
) == 0;
810 /* write common header, sub header and elf note to vmcore */
811 static void create_header32(DumpState
*s
, Error
**errp
)
813 DiskDumpHeader32
*dh
= NULL
;
814 KdumpSubHeader32
*kh
= NULL
;
817 uint32_t sub_hdr_size
;
818 uint32_t bitmap_blocks
;
820 uint64_t offset_note
;
821 Error
*local_err
= NULL
;
823 /* write common header, the version of kdump-compressed format is 6th */
824 size
= sizeof(DiskDumpHeader32
);
825 dh
= g_malloc0(size
);
827 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
828 dh
->header_version
= cpu_to_dump32(s
, 6);
829 block_size
= s
->dump_info
.page_size
;
830 dh
->block_size
= cpu_to_dump32(s
, block_size
);
831 sub_hdr_size
= sizeof(struct KdumpSubHeader32
) + s
->note_size
;
832 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
833 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
834 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
835 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
836 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
837 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
838 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
839 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
841 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
842 status
|= DUMP_DH_COMPRESSED_ZLIB
;
845 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
846 status
|= DUMP_DH_COMPRESSED_LZO
;
850 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
851 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
854 dh
->status
= cpu_to_dump32(s
, status
);
856 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
857 error_setg(errp
, "dump: failed to write disk dump header");
861 /* write sub header */
862 size
= sizeof(KdumpSubHeader32
);
863 kh
= g_malloc0(size
);
865 /* 64bit max_mapnr_64 */
866 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
867 kh
->phys_base
= cpu_to_dump32(s
, s
->dump_info
.phys_base
);
868 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
870 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
872 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
873 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
875 get_note_sizes(s
, s
->guest_note
,
876 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
877 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
878 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
879 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
880 kh
->size_vmcoreinfo
= cpu_to_dump32(s
, size_vmcoreinfo_desc
);
883 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
884 kh
->note_size
= cpu_to_dump32(s
, s
->note_size
);
886 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
887 block_size
, kh
, size
) < 0) {
888 error_setg(errp
, "dump: failed to write kdump sub header");
893 s
->note_buf
= g_malloc0(s
->note_size
);
894 s
->note_buf_offset
= 0;
896 /* use s->note_buf to store notes temporarily */
897 write_elf32_notes(buf_write_note
, s
, &local_err
);
899 error_propagate(errp
, local_err
);
902 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
904 error_setg(errp
, "dump: failed to write notes");
908 /* get offset of dump_bitmap */
909 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
912 /* get offset of page */
913 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
922 /* write common header, sub header and elf note to vmcore */
923 static void create_header64(DumpState
*s
, Error
**errp
)
925 DiskDumpHeader64
*dh
= NULL
;
926 KdumpSubHeader64
*kh
= NULL
;
929 uint32_t sub_hdr_size
;
930 uint32_t bitmap_blocks
;
932 uint64_t offset_note
;
933 Error
*local_err
= NULL
;
935 /* write common header, the version of kdump-compressed format is 6th */
936 size
= sizeof(DiskDumpHeader64
);
937 dh
= g_malloc0(size
);
939 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
940 dh
->header_version
= cpu_to_dump32(s
, 6);
941 block_size
= s
->dump_info
.page_size
;
942 dh
->block_size
= cpu_to_dump32(s
, block_size
);
943 sub_hdr_size
= sizeof(struct KdumpSubHeader64
) + s
->note_size
;
944 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
945 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
946 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
947 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
948 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
949 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
950 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
951 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
953 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
954 status
|= DUMP_DH_COMPRESSED_ZLIB
;
957 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
958 status
|= DUMP_DH_COMPRESSED_LZO
;
962 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
963 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
966 dh
->status
= cpu_to_dump32(s
, status
);
968 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
969 error_setg(errp
, "dump: failed to write disk dump header");
973 /* write sub header */
974 size
= sizeof(KdumpSubHeader64
);
975 kh
= g_malloc0(size
);
977 /* 64bit max_mapnr_64 */
978 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
979 kh
->phys_base
= cpu_to_dump64(s
, s
->dump_info
.phys_base
);
980 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
982 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
984 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
985 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
987 get_note_sizes(s
, s
->guest_note
,
988 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
989 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
990 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
991 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
992 kh
->size_vmcoreinfo
= cpu_to_dump64(s
, size_vmcoreinfo_desc
);
995 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
996 kh
->note_size
= cpu_to_dump64(s
, s
->note_size
);
998 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
999 block_size
, kh
, size
) < 0) {
1000 error_setg(errp
, "dump: failed to write kdump sub header");
1005 s
->note_buf
= g_malloc0(s
->note_size
);
1006 s
->note_buf_offset
= 0;
1008 /* use s->note_buf to store notes temporarily */
1009 write_elf64_notes(buf_write_note
, s
, &local_err
);
1011 error_propagate(errp
, local_err
);
1015 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
1016 s
->note_size
) < 0) {
1017 error_setg(errp
, "dump: failed to write notes");
1021 /* get offset of dump_bitmap */
1022 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
1025 /* get offset of page */
1026 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
1032 g_free(s
->note_buf
);
1035 static void write_dump_header(DumpState
*s
, Error
**errp
)
1037 if (s
->dump_info
.d_class
== ELFCLASS32
) {
1038 create_header32(s
, errp
);
1040 create_header64(s
, errp
);
1044 static size_t dump_bitmap_get_bufsize(DumpState
*s
)
1046 return s
->dump_info
.page_size
;
1050 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1051 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1052 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1053 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1054 * vmcore, ie. synchronizing un-sync bit into vmcore.
1056 static int set_dump_bitmap(uint64_t last_pfn
, uint64_t pfn
, bool value
,
1057 uint8_t *buf
, DumpState
*s
)
1059 off_t old_offset
, new_offset
;
1060 off_t offset_bitmap1
, offset_bitmap2
;
1062 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1063 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1065 /* should not set the previous place */
1066 assert(last_pfn
<= pfn
);
1069 * if the bit needed to be set is not cached in buf, flush the data in buf
1070 * to vmcore firstly.
1071 * making new_offset be bigger than old_offset can also sync remained data
1074 old_offset
= bitmap_bufsize
* (last_pfn
/ bits_per_buf
);
1075 new_offset
= bitmap_bufsize
* (pfn
/ bits_per_buf
);
1077 while (old_offset
< new_offset
) {
1078 /* calculate the offset and write dump_bitmap */
1079 offset_bitmap1
= s
->offset_dump_bitmap
+ old_offset
;
1080 if (write_buffer(s
->fd
, offset_bitmap1
, buf
,
1081 bitmap_bufsize
) < 0) {
1085 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1086 offset_bitmap2
= s
->offset_dump_bitmap
+ s
->len_dump_bitmap
+
1088 if (write_buffer(s
->fd
, offset_bitmap2
, buf
,
1089 bitmap_bufsize
) < 0) {
1093 memset(buf
, 0, bitmap_bufsize
);
1094 old_offset
+= bitmap_bufsize
;
1097 /* get the exact place of the bit in the buf, and set it */
1098 byte
= (pfn
% bits_per_buf
) / CHAR_BIT
;
1099 bit
= (pfn
% bits_per_buf
) % CHAR_BIT
;
1101 buf
[byte
] |= 1u << bit
;
1103 buf
[byte
] &= ~(1u << bit
);
1109 static uint64_t dump_paddr_to_pfn(DumpState
*s
, uint64_t addr
)
1111 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1113 return (addr
>> target_page_shift
) - ARCH_PFN_OFFSET
;
1116 static uint64_t dump_pfn_to_paddr(DumpState
*s
, uint64_t pfn
)
1118 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1120 return (pfn
+ ARCH_PFN_OFFSET
) << target_page_shift
;
1124 * exam every page and return the page frame number and the address of the page.
1125 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1126 * blocks, so block->target_start and block->target_end should be interal
1127 * multiples of the target page size.
1129 static bool get_next_page(GuestPhysBlock
**blockptr
, uint64_t *pfnptr
,
1130 uint8_t **bufptr
, DumpState
*s
)
1132 GuestPhysBlock
*block
= *blockptr
;
1133 hwaddr addr
, target_page_mask
= ~((hwaddr
)s
->dump_info
.page_size
- 1);
1136 /* block == NULL means the start of the iteration */
1138 block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1140 assert((block
->target_start
& ~target_page_mask
) == 0);
1141 assert((block
->target_end
& ~target_page_mask
) == 0);
1142 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1144 *bufptr
= block
->host_addr
;
1149 *pfnptr
= *pfnptr
+ 1;
1150 addr
= dump_pfn_to_paddr(s
, *pfnptr
);
1152 if ((addr
>= block
->target_start
) &&
1153 (addr
+ s
->dump_info
.page_size
<= block
->target_end
)) {
1154 buf
= block
->host_addr
+ (addr
- block
->target_start
);
1156 /* the next page is in the next block */
1157 block
= QTAILQ_NEXT(block
, next
);
1162 assert((block
->target_start
& ~target_page_mask
) == 0);
1163 assert((block
->target_end
& ~target_page_mask
) == 0);
1164 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1165 buf
= block
->host_addr
;
1175 static void write_dump_bitmap(DumpState
*s
, Error
**errp
)
1178 uint64_t last_pfn
, pfn
;
1179 void *dump_bitmap_buf
;
1180 size_t num_dumpable
;
1181 GuestPhysBlock
*block_iter
= NULL
;
1182 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1183 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1185 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1186 dump_bitmap_buf
= g_malloc0(bitmap_bufsize
);
1192 * exam memory page by page, and set the bit in dump_bitmap corresponded
1193 * to the existing page.
1195 while (get_next_page(&block_iter
, &pfn
, NULL
, s
)) {
1196 ret
= set_dump_bitmap(last_pfn
, pfn
, true, dump_bitmap_buf
, s
);
1198 error_setg(errp
, "dump: failed to set dump_bitmap");
1207 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1208 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1209 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1211 if (num_dumpable
> 0) {
1212 ret
= set_dump_bitmap(last_pfn
, last_pfn
+ bits_per_buf
, false,
1213 dump_bitmap_buf
, s
);
1215 error_setg(errp
, "dump: failed to sync dump_bitmap");
1220 /* number of dumpable pages that will be dumped later */
1221 s
->num_dumpable
= num_dumpable
;
1224 g_free(dump_bitmap_buf
);
1227 static void prepare_data_cache(DataCache
*data_cache
, DumpState
*s
,
1230 data_cache
->fd
= s
->fd
;
1231 data_cache
->data_size
= 0;
1232 data_cache
->buf_size
= 4 * dump_bitmap_get_bufsize(s
);
1233 data_cache
->buf
= g_malloc0(data_cache
->buf_size
);
1234 data_cache
->offset
= offset
;
1237 static int write_cache(DataCache
*dc
, const void *buf
, size_t size
,
1241 * dc->buf_size should not be less than size, otherwise dc will never be
1244 assert(size
<= dc
->buf_size
);
1247 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1248 * otherwise check if the space is enough for caching data in buf, if not,
1249 * write the data in dc->buf to dc->fd and reset dc->buf
1251 if ((!flag_sync
&& dc
->data_size
+ size
> dc
->buf_size
) ||
1252 (flag_sync
&& dc
->data_size
> 0)) {
1253 if (write_buffer(dc
->fd
, dc
->offset
, dc
->buf
, dc
->data_size
) < 0) {
1257 dc
->offset
+= dc
->data_size
;
1262 memcpy(dc
->buf
+ dc
->data_size
, buf
, size
);
1263 dc
->data_size
+= size
;
1269 static void free_data_cache(DataCache
*data_cache
)
1271 g_free(data_cache
->buf
);
1274 static size_t get_len_buf_out(size_t page_size
, uint32_t flag_compress
)
1276 switch (flag_compress
) {
1277 case DUMP_DH_COMPRESSED_ZLIB
:
1278 return compressBound(page_size
);
1280 case DUMP_DH_COMPRESSED_LZO
:
1282 * LZO will expand incompressible data by a little amount. Please check
1283 * the following URL to see the expansion calculation:
1284 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1286 return page_size
+ page_size
/ 16 + 64 + 3;
1288 #ifdef CONFIG_SNAPPY
1289 case DUMP_DH_COMPRESSED_SNAPPY
:
1290 return snappy_max_compressed_length(page_size
);
1297 * check if the page is all 0
1299 static inline bool is_zero_page(const uint8_t *buf
, size_t page_size
)
1301 return buffer_is_zero(buf
, page_size
);
1304 static void write_dump_pages(DumpState
*s
, Error
**errp
)
1307 DataCache page_desc
, page_data
;
1308 size_t len_buf_out
, size_out
;
1310 lzo_bytep wrkmem
= NULL
;
1312 uint8_t *buf_out
= NULL
;
1313 off_t offset_desc
, offset_data
;
1314 PageDescriptor pd
, pd_zero
;
1316 GuestPhysBlock
*block_iter
= NULL
;
1319 /* get offset of page_desc and page_data in dump file */
1320 offset_desc
= s
->offset_page
;
1321 offset_data
= offset_desc
+ sizeof(PageDescriptor
) * s
->num_dumpable
;
1323 prepare_data_cache(&page_desc
, s
, offset_desc
);
1324 prepare_data_cache(&page_data
, s
, offset_data
);
1326 /* prepare buffer to store compressed data */
1327 len_buf_out
= get_len_buf_out(s
->dump_info
.page_size
, s
->flag_compress
);
1328 assert(len_buf_out
!= 0);
1331 wrkmem
= g_malloc(LZO1X_1_MEM_COMPRESS
);
1334 buf_out
= g_malloc(len_buf_out
);
1337 * init zero page's page_desc and page_data, because every zero page
1338 * uses the same page_data
1340 pd_zero
.size
= cpu_to_dump32(s
, s
->dump_info
.page_size
);
1341 pd_zero
.flags
= cpu_to_dump32(s
, 0);
1342 pd_zero
.offset
= cpu_to_dump64(s
, offset_data
);
1343 pd_zero
.page_flags
= cpu_to_dump64(s
, 0);
1344 buf
= g_malloc0(s
->dump_info
.page_size
);
1345 ret
= write_cache(&page_data
, buf
, s
->dump_info
.page_size
, false);
1348 error_setg(errp
, "dump: failed to write page data (zero page)");
1352 offset_data
+= s
->dump_info
.page_size
;
1355 * dump memory to vmcore page by page. zero page will all be resided in the
1356 * first page of page section
1358 while (get_next_page(&block_iter
, &pfn_iter
, &buf
, s
)) {
1359 /* check zero page */
1360 if (is_zero_page(buf
, s
->dump_info
.page_size
)) {
1361 ret
= write_cache(&page_desc
, &pd_zero
, sizeof(PageDescriptor
),
1364 error_setg(errp
, "dump: failed to write page desc");
1369 * not zero page, then:
1370 * 1. compress the page
1371 * 2. write the compressed page into the cache of page_data
1372 * 3. get page desc of the compressed page and write it into the
1373 * cache of page_desc
1375 * only one compression format will be used here, for
1376 * s->flag_compress is set. But when compression fails to work,
1377 * we fall back to save in plaintext.
1379 size_out
= len_buf_out
;
1380 if ((s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) &&
1381 (compress2(buf_out
, (uLongf
*)&size_out
, buf
,
1382 s
->dump_info
.page_size
, Z_BEST_SPEED
) == Z_OK
) &&
1383 (size_out
< s
->dump_info
.page_size
)) {
1384 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_ZLIB
);
1385 pd
.size
= cpu_to_dump32(s
, size_out
);
1387 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1389 error_setg(errp
, "dump: failed to write page data");
1393 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) &&
1394 (lzo1x_1_compress(buf
, s
->dump_info
.page_size
, buf_out
,
1395 (lzo_uint
*)&size_out
, wrkmem
) == LZO_E_OK
) &&
1396 (size_out
< s
->dump_info
.page_size
)) {
1397 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_LZO
);
1398 pd
.size
= cpu_to_dump32(s
, size_out
);
1400 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1402 error_setg(errp
, "dump: failed to write page data");
1406 #ifdef CONFIG_SNAPPY
1407 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) &&
1408 (snappy_compress((char *)buf
, s
->dump_info
.page_size
,
1409 (char *)buf_out
, &size_out
) == SNAPPY_OK
) &&
1410 (size_out
< s
->dump_info
.page_size
)) {
1411 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_SNAPPY
);
1412 pd
.size
= cpu_to_dump32(s
, size_out
);
1414 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1416 error_setg(errp
, "dump: failed to write page data");
1422 * fall back to save in plaintext, size_out should be
1423 * assigned the target's page size
1425 pd
.flags
= cpu_to_dump32(s
, 0);
1426 size_out
= s
->dump_info
.page_size
;
1427 pd
.size
= cpu_to_dump32(s
, size_out
);
1429 ret
= write_cache(&page_data
, buf
,
1430 s
->dump_info
.page_size
, false);
1432 error_setg(errp
, "dump: failed to write page data");
1437 /* get and write page desc here */
1438 pd
.page_flags
= cpu_to_dump64(s
, 0);
1439 pd
.offset
= cpu_to_dump64(s
, offset_data
);
1440 offset_data
+= size_out
;
1442 ret
= write_cache(&page_desc
, &pd
, sizeof(PageDescriptor
), false);
1444 error_setg(errp
, "dump: failed to write page desc");
1448 s
->written_size
+= s
->dump_info
.page_size
;
1451 ret
= write_cache(&page_desc
, NULL
, 0, true);
1453 error_setg(errp
, "dump: failed to sync cache for page_desc");
1456 ret
= write_cache(&page_data
, NULL
, 0, true);
1458 error_setg(errp
, "dump: failed to sync cache for page_data");
1463 free_data_cache(&page_desc
);
1464 free_data_cache(&page_data
);
1473 static void create_kdump_vmcore(DumpState
*s
, Error
**errp
)
1476 Error
*local_err
= NULL
;
1479 * the kdump-compressed format is:
1481 * +------------------------------------------+ 0x0
1482 * | main header (struct disk_dump_header) |
1483 * |------------------------------------------+ block 1
1484 * | sub header (struct kdump_sub_header) |
1485 * |------------------------------------------+ block 2
1486 * | 1st-dump_bitmap |
1487 * |------------------------------------------+ block 2 + X blocks
1488 * | 2nd-dump_bitmap | (aligned by block)
1489 * |------------------------------------------+ block 2 + 2 * X blocks
1490 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1491 * | page desc for pfn 1 (struct page_desc) |
1493 * |------------------------------------------| (not aligned by block)
1494 * | page data (pfn 0) |
1495 * | page data (pfn 1) |
1497 * +------------------------------------------+
1500 ret
= write_start_flat_header(s
->fd
);
1502 error_setg(errp
, "dump: failed to write start flat header");
1506 write_dump_header(s
, &local_err
);
1508 error_propagate(errp
, local_err
);
1512 write_dump_bitmap(s
, &local_err
);
1514 error_propagate(errp
, local_err
);
1518 write_dump_pages(s
, &local_err
);
1520 error_propagate(errp
, local_err
);
1524 ret
= write_end_flat_header(s
->fd
);
1526 error_setg(errp
, "dump: failed to write end flat header");
1531 static ram_addr_t
get_start_block(DumpState
*s
)
1533 GuestPhysBlock
*block
;
1535 if (!s
->has_filter
) {
1536 s
->next_block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1540 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1541 if (block
->target_start
>= s
->begin
+ s
->length
||
1542 block
->target_end
<= s
->begin
) {
1543 /* This block is out of the range */
1547 s
->next_block
= block
;
1548 if (s
->begin
> block
->target_start
) {
1549 s
->start
= s
->begin
- block
->target_start
;
1559 static void get_max_mapnr(DumpState
*s
)
1561 GuestPhysBlock
*last_block
;
1563 last_block
= QTAILQ_LAST(&s
->guest_phys_blocks
.head
);
1564 s
->max_mapnr
= dump_paddr_to_pfn(s
, last_block
->target_end
);
1567 static DumpState dump_state_global
= { .status
= DUMP_STATUS_NONE
};
1569 static void dump_state_prepare(DumpState
*s
)
1571 /* zero the struct, setting status to active */
1572 *s
= (DumpState
) { .status
= DUMP_STATUS_ACTIVE
};
1575 bool dump_in_progress(void)
1577 DumpState
*state
= &dump_state_global
;
1578 return (qatomic_read(&state
->status
) == DUMP_STATUS_ACTIVE
);
1581 /* calculate total size of memory to be dumped (taking filter into
1583 static int64_t dump_calculate_size(DumpState
*s
)
1585 GuestPhysBlock
*block
;
1586 int64_t size
= 0, total
= 0, left
= 0, right
= 0;
1588 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1589 if (s
->has_filter
) {
1590 /* calculate the overlapped region. */
1591 left
= MAX(s
->begin
, block
->target_start
);
1592 right
= MIN(s
->begin
+ s
->length
, block
->target_end
);
1593 size
= right
- left
;
1594 size
= size
> 0 ? size
: 0;
1596 /* count the whole region in */
1597 size
= (block
->target_end
- block
->target_start
);
1605 static void vmcoreinfo_update_phys_base(DumpState
*s
)
1607 uint64_t size
, note_head_size
, name_size
, phys_base
;
1612 if (!note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1616 get_note_sizes(s
, s
->guest_note
, ¬e_head_size
, &name_size
, &size
);
1617 note_head_size
= ROUND_UP(note_head_size
, 4);
1619 vmci
= s
->guest_note
+ note_head_size
+ ROUND_UP(name_size
, 4);
1620 *(vmci
+ size
) = '\0';
1622 lines
= g_strsplit((char *)vmci
, "\n", -1);
1623 for (i
= 0; lines
[i
]; i
++) {
1624 const char *prefix
= NULL
;
1626 if (s
->dump_info
.d_machine
== EM_X86_64
) {
1627 prefix
= "NUMBER(phys_base)=";
1628 } else if (s
->dump_info
.d_machine
== EM_AARCH64
) {
1629 prefix
= "NUMBER(PHYS_OFFSET)=";
1632 if (prefix
&& g_str_has_prefix(lines
[i
], prefix
)) {
1633 if (qemu_strtou64(lines
[i
] + strlen(prefix
), NULL
, 16,
1635 warn_report("Failed to read %s", prefix
);
1637 s
->dump_info
.phys_base
= phys_base
;
1646 static void dump_init(DumpState
*s
, int fd
, bool has_format
,
1647 DumpGuestMemoryFormat format
, bool paging
, bool has_filter
,
1648 int64_t begin
, int64_t length
, Error
**errp
)
1650 VMCoreInfoState
*vmci
= vmcoreinfo_find();
1656 s
->has_format
= has_format
;
1658 s
->written_size
= 0;
1660 /* kdump-compressed is conflict with paging and filter */
1661 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1662 assert(!paging
&& !has_filter
);
1665 if (runstate_is_running()) {
1666 vm_stop(RUN_STATE_SAVE_VM
);
1672 /* If we use KVM, we should synchronize the registers before we get dump
1673 * info or physmap info.
1675 cpu_synchronize_all_states();
1682 s
->has_filter
= has_filter
;
1686 memory_mapping_list_init(&s
->list
);
1688 guest_phys_blocks_init(&s
->guest_phys_blocks
);
1689 guest_phys_blocks_append(&s
->guest_phys_blocks
);
1690 s
->total_size
= dump_calculate_size(s
);
1691 #ifdef DEBUG_DUMP_GUEST_MEMORY
1692 fprintf(stderr
, "DUMP: total memory to dump: %lu\n", s
->total_size
);
1695 /* it does not make sense to dump non-existent memory */
1696 if (!s
->total_size
) {
1697 error_setg(errp
, "dump: no guest memory to dump");
1701 s
->start
= get_start_block(s
);
1702 if (s
->start
== -1) {
1703 error_setg(errp
, QERR_INVALID_PARAMETER
, "begin");
1707 /* get dump info: endian, class and architecture.
1708 * If the target architecture is not supported, cpu_get_dump_info() will
1711 ret
= cpu_get_dump_info(&s
->dump_info
, &s
->guest_phys_blocks
);
1713 error_setg(errp
, QERR_UNSUPPORTED
);
1717 if (!s
->dump_info
.page_size
) {
1718 s
->dump_info
.page_size
= TARGET_PAGE_SIZE
;
1721 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
1722 s
->dump_info
.d_machine
, nr_cpus
);
1723 if (s
->note_size
< 0) {
1724 error_setg(errp
, QERR_UNSUPPORTED
);
1729 * The goal of this block is to (a) update the previously guessed
1730 * phys_base, (b) copy the guest note out of the guest.
1731 * Failure to do so is not fatal for dumping.
1734 uint64_t addr
, note_head_size
, name_size
, desc_size
;
1738 note_head_size
= s
->dump_info
.d_class
== ELFCLASS32
?
1739 sizeof(Elf32_Nhdr
) : sizeof(Elf64_Nhdr
);
1741 format
= le16_to_cpu(vmci
->vmcoreinfo
.guest_format
);
1742 size
= le32_to_cpu(vmci
->vmcoreinfo
.size
);
1743 addr
= le64_to_cpu(vmci
->vmcoreinfo
.paddr
);
1744 if (!vmci
->has_vmcoreinfo
) {
1745 warn_report("guest note is not present");
1746 } else if (size
< note_head_size
|| size
> MAX_GUEST_NOTE_SIZE
) {
1747 warn_report("guest note size is invalid: %" PRIu32
, size
);
1748 } else if (format
!= FW_CFG_VMCOREINFO_FORMAT_ELF
) {
1749 warn_report("guest note format is unsupported: %" PRIu16
, format
);
1751 s
->guest_note
= g_malloc(size
+ 1); /* +1 for adding \0 */
1752 cpu_physical_memory_read(addr
, s
->guest_note
, size
);
1754 get_note_sizes(s
, s
->guest_note
, NULL
, &name_size
, &desc_size
);
1755 s
->guest_note_size
= ELF_NOTE_SIZE(note_head_size
, name_size
,
1757 if (name_size
> MAX_GUEST_NOTE_SIZE
||
1758 desc_size
> MAX_GUEST_NOTE_SIZE
||
1759 s
->guest_note_size
> size
) {
1760 warn_report("Invalid guest note header");
1761 g_free(s
->guest_note
);
1762 s
->guest_note
= NULL
;
1764 vmcoreinfo_update_phys_base(s
);
1765 s
->note_size
+= s
->guest_note_size
;
1770 /* get memory mapping */
1772 qemu_get_guest_memory_mapping(&s
->list
, &s
->guest_phys_blocks
, &err
);
1774 error_propagate(errp
, err
);
1778 qemu_get_guest_simple_memory_mapping(&s
->list
, &s
->guest_phys_blocks
);
1781 s
->nr_cpus
= nr_cpus
;
1786 tmp
= DIV_ROUND_UP(DIV_ROUND_UP(s
->max_mapnr
, CHAR_BIT
),
1787 s
->dump_info
.page_size
);
1788 s
->len_dump_bitmap
= tmp
* s
->dump_info
.page_size
;
1790 /* init for kdump-compressed format */
1791 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1793 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
:
1794 s
->flag_compress
= DUMP_DH_COMPRESSED_ZLIB
;
1797 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
:
1799 if (lzo_init() != LZO_E_OK
) {
1800 error_setg(errp
, "failed to initialize the LZO library");
1804 s
->flag_compress
= DUMP_DH_COMPRESSED_LZO
;
1807 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
:
1808 s
->flag_compress
= DUMP_DH_COMPRESSED_SNAPPY
;
1812 s
->flag_compress
= 0;
1818 if (s
->has_filter
) {
1819 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
1823 * calculate phdr_num
1825 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1827 s
->phdr_num
= 1; /* PT_NOTE */
1828 if (s
->list
.num
< UINT16_MAX
- 2) {
1829 s
->phdr_num
+= s
->list
.num
;
1830 s
->have_section
= false;
1832 s
->have_section
= true;
1833 s
->phdr_num
= PN_XNUM
;
1834 s
->sh_info
= 1; /* PT_NOTE */
1836 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1837 if (s
->list
.num
<= UINT32_MAX
- 1) {
1838 s
->sh_info
+= s
->list
.num
;
1840 s
->sh_info
= UINT32_MAX
;
1844 if (s
->dump_info
.d_class
== ELFCLASS64
) {
1845 if (s
->have_section
) {
1846 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1847 sizeof(Elf64_Phdr
) * s
->sh_info
+
1848 sizeof(Elf64_Shdr
) + s
->note_size
;
1850 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1851 sizeof(Elf64_Phdr
) * s
->phdr_num
+ s
->note_size
;
1854 if (s
->have_section
) {
1855 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1856 sizeof(Elf32_Phdr
) * s
->sh_info
+
1857 sizeof(Elf32_Shdr
) + s
->note_size
;
1859 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1860 sizeof(Elf32_Phdr
) * s
->phdr_num
+ s
->note_size
;
1870 /* this operation might be time consuming. */
1871 static void dump_process(DumpState
*s
, Error
**errp
)
1873 Error
*local_err
= NULL
;
1874 DumpQueryResult
*result
= NULL
;
1876 if (s
->has_format
&& s
->format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
) {
1877 #ifdef TARGET_X86_64
1878 create_win_dump(s
, &local_err
);
1880 } else if (s
->has_format
&& s
->format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1881 create_kdump_vmcore(s
, &local_err
);
1883 create_vmcore(s
, &local_err
);
1886 /* make sure status is written after written_size updates */
1888 qatomic_set(&s
->status
,
1889 (local_err
? DUMP_STATUS_FAILED
: DUMP_STATUS_COMPLETED
));
1891 /* send DUMP_COMPLETED message (unconditionally) */
1892 result
= qmp_query_dump(NULL
);
1893 /* should never fail */
1895 qapi_event_send_dump_completed(result
, !!local_err
, (local_err
?
1896 error_get_pretty(local_err
) : NULL
));
1897 qapi_free_DumpQueryResult(result
);
1899 error_propagate(errp
, local_err
);
1903 static void *dump_thread(void *data
)
1905 DumpState
*s
= (DumpState
*)data
;
1906 dump_process(s
, NULL
);
1910 DumpQueryResult
*qmp_query_dump(Error
**errp
)
1912 DumpQueryResult
*result
= g_new(DumpQueryResult
, 1);
1913 DumpState
*state
= &dump_state_global
;
1914 result
->status
= qatomic_read(&state
->status
);
1915 /* make sure we are reading status and written_size in order */
1917 result
->completed
= state
->written_size
;
1918 result
->total
= state
->total_size
;
1922 void qmp_dump_guest_memory(bool paging
, const char *file
,
1923 bool has_detach
, bool detach
,
1924 bool has_begin
, int64_t begin
, bool has_length
,
1925 int64_t length
, bool has_format
,
1926 DumpGuestMemoryFormat format
, Error
**errp
)
1931 Error
*local_err
= NULL
;
1932 bool detach_p
= false;
1934 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1935 error_setg(errp
, "Dump not allowed during incoming migration.");
1939 /* if there is a dump in background, we should wait until the dump
1941 if (dump_in_progress()) {
1942 error_setg(errp
, "There is a dump in process, please wait.");
1947 * kdump-compressed format need the whole memory dumped, so paging or
1948 * filter is not supported here.
1950 if ((has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) &&
1951 (paging
|| has_begin
|| has_length
)) {
1952 error_setg(errp
, "kdump-compressed format doesn't support paging or "
1956 if (has_begin
&& !has_length
) {
1957 error_setg(errp
, QERR_MISSING_PARAMETER
, "length");
1960 if (!has_begin
&& has_length
) {
1961 error_setg(errp
, QERR_MISSING_PARAMETER
, "begin");
1968 /* check whether lzo/snappy is supported */
1970 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
) {
1971 error_setg(errp
, "kdump-lzo is not available now");
1976 #ifndef CONFIG_SNAPPY
1977 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
) {
1978 error_setg(errp
, "kdump-snappy is not available now");
1983 #ifndef TARGET_X86_64
1984 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
) {
1985 error_setg(errp
, "Windows dump is only available for x86-64");
1991 if (strstart(file
, "fd:", &p
)) {
1992 fd
= monitor_get_fd(monitor_cur(), p
, errp
);
1999 if (strstart(file
, "file:", &p
)) {
2000 fd
= qemu_open_old(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
2002 error_setg_file_open(errp
, errno
, p
);
2008 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
2012 if (!dump_migration_blocker
) {
2013 error_setg(&dump_migration_blocker
,
2014 "Live migration disabled: dump-guest-memory in progress");
2018 * Allows even for -only-migratable, but forbid migration during the
2019 * process of dump guest memory.
2021 if (migrate_add_blocker_internal(dump_migration_blocker
, errp
)) {
2022 /* Remember to release the fd before passing it over to dump state */
2027 s
= &dump_state_global
;
2028 dump_state_prepare(s
);
2030 dump_init(s
, fd
, has_format
, format
, paging
, has_begin
,
2031 begin
, length
, &local_err
);
2033 error_propagate(errp
, local_err
);
2034 qatomic_set(&s
->status
, DUMP_STATUS_FAILED
);
2041 qemu_thread_create(&s
->dump_thread
, "dump_thread", dump_thread
,
2042 s
, QEMU_THREAD_DETACHED
);
2045 dump_process(s
, errp
);
2049 DumpGuestMemoryCapability
*qmp_query_dump_guest_memory_capability(Error
**errp
)
2051 DumpGuestMemoryCapability
*cap
=
2052 g_malloc0(sizeof(DumpGuestMemoryCapability
));
2053 DumpGuestMemoryFormatList
**tail
= &cap
->formats
;
2055 /* elf is always available */
2056 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_ELF
);
2058 /* kdump-zlib is always available */
2059 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
);
2061 /* add new item if kdump-lzo is available */
2063 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
);
2066 /* add new item if kdump-snappy is available */
2067 #ifdef CONFIG_SNAPPY
2068 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
);
2071 /* Windows dump is available only if target is x86_64 */
2072 #ifdef TARGET_X86_64
2073 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
);