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/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/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"
38 #include <lzo/lzo1x.h>
43 #ifndef ELF_MACHINE_UNAME
44 #define ELF_MACHINE_UNAME "Unknown"
47 #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
49 #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
50 ((DIV_ROUND_UP((hdr_size), 4) + \
51 DIV_ROUND_UP((name_size), 4) + \
52 DIV_ROUND_UP((desc_size), 4)) * 4)
54 uint16_t cpu_to_dump16(DumpState
*s
, uint16_t val
)
56 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
57 val
= cpu_to_le16(val
);
59 val
= cpu_to_be16(val
);
65 uint32_t cpu_to_dump32(DumpState
*s
, uint32_t val
)
67 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
68 val
= cpu_to_le32(val
);
70 val
= cpu_to_be32(val
);
76 uint64_t cpu_to_dump64(DumpState
*s
, uint64_t val
)
78 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
79 val
= cpu_to_le64(val
);
81 val
= cpu_to_be64(val
);
87 static int dump_cleanup(DumpState
*s
)
89 guest_phys_blocks_free(&s
->guest_phys_blocks
);
90 memory_mapping_list_free(&s
->list
);
92 g_free(s
->guest_note
);
96 qemu_mutex_lock_iothread();
100 qemu_mutex_unlock_iothread();
107 static int fd_write_vmcore(const void *buf
, size_t size
, void *opaque
)
109 DumpState
*s
= opaque
;
112 written_size
= qemu_write_full(s
->fd
, buf
, size
);
113 if (written_size
!= size
) {
120 static void write_elf64_header(DumpState
*s
, Error
**errp
)
122 Elf64_Ehdr elf_header
;
125 memset(&elf_header
, 0, sizeof(Elf64_Ehdr
));
126 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
127 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS64
;
128 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
129 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
130 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
131 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
132 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
133 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
134 elf_header
.e_phoff
= cpu_to_dump64(s
, sizeof(Elf64_Ehdr
));
135 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf64_Phdr
));
136 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
137 if (s
->have_section
) {
138 uint64_t shoff
= sizeof(Elf64_Ehdr
) + sizeof(Elf64_Phdr
) * s
->sh_info
;
140 elf_header
.e_shoff
= cpu_to_dump64(s
, shoff
);
141 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf64_Shdr
));
142 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
145 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
147 error_setg_errno(errp
, -ret
, "dump: failed to write elf header");
151 static void write_elf32_header(DumpState
*s
, Error
**errp
)
153 Elf32_Ehdr elf_header
;
156 memset(&elf_header
, 0, sizeof(Elf32_Ehdr
));
157 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
158 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS32
;
159 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
160 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
161 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
162 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
163 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
164 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
165 elf_header
.e_phoff
= cpu_to_dump32(s
, sizeof(Elf32_Ehdr
));
166 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf32_Phdr
));
167 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
168 if (s
->have_section
) {
169 uint32_t shoff
= sizeof(Elf32_Ehdr
) + sizeof(Elf32_Phdr
) * s
->sh_info
;
171 elf_header
.e_shoff
= cpu_to_dump32(s
, shoff
);
172 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf32_Shdr
));
173 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
176 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
178 error_setg_errno(errp
, -ret
, "dump: failed to write elf header");
182 static void write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
183 int phdr_index
, hwaddr offset
,
184 hwaddr filesz
, Error
**errp
)
189 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
190 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
191 phdr
.p_offset
= cpu_to_dump64(s
, offset
);
192 phdr
.p_paddr
= cpu_to_dump64(s
, memory_mapping
->phys_addr
);
193 phdr
.p_filesz
= cpu_to_dump64(s
, filesz
);
194 phdr
.p_memsz
= cpu_to_dump64(s
, memory_mapping
->length
);
195 phdr
.p_vaddr
= cpu_to_dump64(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
197 assert(memory_mapping
->length
>= filesz
);
199 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
201 error_setg_errno(errp
, -ret
,
202 "dump: failed to write program header table");
206 static void write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
207 int phdr_index
, hwaddr offset
,
208 hwaddr filesz
, Error
**errp
)
213 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
214 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
215 phdr
.p_offset
= cpu_to_dump32(s
, offset
);
216 phdr
.p_paddr
= cpu_to_dump32(s
, memory_mapping
->phys_addr
);
217 phdr
.p_filesz
= cpu_to_dump32(s
, filesz
);
218 phdr
.p_memsz
= cpu_to_dump32(s
, memory_mapping
->length
);
220 cpu_to_dump32(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
222 assert(memory_mapping
->length
>= filesz
);
224 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
226 error_setg_errno(errp
, -ret
,
227 "dump: failed to write program header table");
231 static void write_elf64_note(DumpState
*s
, Error
**errp
)
234 hwaddr begin
= s
->memory_offset
- s
->note_size
;
237 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
238 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
239 phdr
.p_offset
= cpu_to_dump64(s
, begin
);
241 phdr
.p_filesz
= cpu_to_dump64(s
, s
->note_size
);
242 phdr
.p_memsz
= cpu_to_dump64(s
, s
->note_size
);
245 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
247 error_setg_errno(errp
, -ret
,
248 "dump: failed to write program header table");
252 static inline int cpu_index(CPUState
*cpu
)
254 return cpu
->cpu_index
+ 1;
257 static void write_guest_note(WriteCoreDumpFunction f
, DumpState
*s
,
263 ret
= f(s
->guest_note
, s
->guest_note_size
, s
);
265 error_setg(errp
, "dump: failed to write guest note");
270 static void write_elf64_notes(WriteCoreDumpFunction f
, DumpState
*s
,
279 ret
= cpu_write_elf64_note(f
, cpu
, id
, s
);
281 error_setg(errp
, "dump: failed to write elf notes");
287 ret
= cpu_write_elf64_qemunote(f
, cpu
, s
);
289 error_setg(errp
, "dump: failed to write CPU status");
294 write_guest_note(f
, s
, errp
);
297 static void write_elf32_note(DumpState
*s
, Error
**errp
)
299 hwaddr begin
= s
->memory_offset
- s
->note_size
;
303 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
304 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
305 phdr
.p_offset
= cpu_to_dump32(s
, begin
);
307 phdr
.p_filesz
= cpu_to_dump32(s
, s
->note_size
);
308 phdr
.p_memsz
= cpu_to_dump32(s
, s
->note_size
);
311 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
313 error_setg_errno(errp
, -ret
,
314 "dump: failed to write program header table");
318 static void write_elf32_notes(WriteCoreDumpFunction f
, DumpState
*s
,
327 ret
= cpu_write_elf32_note(f
, cpu
, id
, s
);
329 error_setg(errp
, "dump: failed to write elf notes");
335 ret
= cpu_write_elf32_qemunote(f
, cpu
, s
);
337 error_setg(errp
, "dump: failed to write CPU status");
342 write_guest_note(f
, s
, errp
);
345 static void write_elf_section(DumpState
*s
, int type
, Error
**errp
)
354 shdr_size
= sizeof(Elf32_Shdr
);
355 memset(&shdr32
, 0, shdr_size
);
356 shdr32
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
359 shdr_size
= sizeof(Elf64_Shdr
);
360 memset(&shdr64
, 0, shdr_size
);
361 shdr64
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
365 ret
= fd_write_vmcore(&shdr
, shdr_size
, s
);
367 error_setg_errno(errp
, -ret
,
368 "dump: failed to write section header table");
372 static void write_data(DumpState
*s
, void *buf
, int length
, Error
**errp
)
376 ret
= fd_write_vmcore(buf
, length
, s
);
378 error_setg_errno(errp
, -ret
, "dump: failed to save memory");
380 s
->written_size
+= length
;
384 /* write the memory to vmcore. 1 page per I/O. */
385 static void write_memory(DumpState
*s
, GuestPhysBlock
*block
, ram_addr_t start
,
386 int64_t size
, Error
**errp
)
389 Error
*local_err
= NULL
;
391 for (i
= 0; i
< size
/ s
->dump_info
.page_size
; i
++) {
392 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
393 s
->dump_info
.page_size
, &local_err
);
395 error_propagate(errp
, local_err
);
400 if ((size
% s
->dump_info
.page_size
) != 0) {
401 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
402 size
% s
->dump_info
.page_size
, &local_err
);
404 error_propagate(errp
, local_err
);
410 /* get the memory's offset and size in the vmcore */
411 static void get_offset_range(hwaddr phys_addr
,
412 ram_addr_t mapping_length
,
417 GuestPhysBlock
*block
;
418 hwaddr offset
= s
->memory_offset
;
419 int64_t size_in_block
, start
;
421 /* When the memory is not stored into vmcore, offset will be -1 */
426 if (phys_addr
< s
->begin
|| phys_addr
>= s
->begin
+ s
->length
) {
431 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
433 if (block
->target_start
>= s
->begin
+ s
->length
||
434 block
->target_end
<= s
->begin
) {
435 /* This block is out of the range */
439 if (s
->begin
<= block
->target_start
) {
440 start
= block
->target_start
;
445 size_in_block
= block
->target_end
- start
;
446 if (s
->begin
+ s
->length
< block
->target_end
) {
447 size_in_block
-= block
->target_end
- (s
->begin
+ s
->length
);
450 start
= block
->target_start
;
451 size_in_block
= block
->target_end
- block
->target_start
;
454 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
455 *p_offset
= phys_addr
- start
+ offset
;
457 /* The offset range mapped from the vmcore file must not spill over
458 * the GuestPhysBlock, clamp it. The rest of the mapping will be
459 * zero-filled in memory at load time; see
460 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
462 *p_filesz
= phys_addr
+ mapping_length
<= start
+ size_in_block
?
464 size_in_block
- (phys_addr
- start
);
468 offset
+= size_in_block
;
472 static void write_elf_loads(DumpState
*s
, Error
**errp
)
474 hwaddr offset
, filesz
;
475 MemoryMapping
*memory_mapping
;
476 uint32_t phdr_index
= 1;
478 Error
*local_err
= NULL
;
480 if (s
->have_section
) {
481 max_index
= s
->sh_info
;
483 max_index
= s
->phdr_num
;
486 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
487 get_offset_range(memory_mapping
->phys_addr
,
488 memory_mapping
->length
,
489 s
, &offset
, &filesz
);
490 if (s
->dump_info
.d_class
== ELFCLASS64
) {
491 write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
,
494 write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
,
499 error_propagate(errp
, local_err
);
503 if (phdr_index
>= max_index
) {
509 /* write elf header, PT_NOTE and elf note to vmcore. */
510 static void dump_begin(DumpState
*s
, Error
**errp
)
512 Error
*local_err
= NULL
;
515 * the vmcore's format is:
534 * we only know where the memory is saved after we write elf note into
538 /* write elf header to vmcore */
539 if (s
->dump_info
.d_class
== ELFCLASS64
) {
540 write_elf64_header(s
, &local_err
);
542 write_elf32_header(s
, &local_err
);
545 error_propagate(errp
, local_err
);
549 if (s
->dump_info
.d_class
== ELFCLASS64
) {
550 /* write PT_NOTE to vmcore */
551 write_elf64_note(s
, &local_err
);
553 error_propagate(errp
, local_err
);
557 /* write all PT_LOAD to vmcore */
558 write_elf_loads(s
, &local_err
);
560 error_propagate(errp
, local_err
);
564 /* write section to vmcore */
565 if (s
->have_section
) {
566 write_elf_section(s
, 1, &local_err
);
568 error_propagate(errp
, local_err
);
573 /* write notes to vmcore */
574 write_elf64_notes(fd_write_vmcore
, s
, &local_err
);
576 error_propagate(errp
, local_err
);
580 /* write PT_NOTE to vmcore */
581 write_elf32_note(s
, &local_err
);
583 error_propagate(errp
, local_err
);
587 /* write all PT_LOAD to vmcore */
588 write_elf_loads(s
, &local_err
);
590 error_propagate(errp
, local_err
);
594 /* write section to vmcore */
595 if (s
->have_section
) {
596 write_elf_section(s
, 0, &local_err
);
598 error_propagate(errp
, local_err
);
603 /* write notes to vmcore */
604 write_elf32_notes(fd_write_vmcore
, s
, &local_err
);
606 error_propagate(errp
, local_err
);
612 static int get_next_block(DumpState
*s
, GuestPhysBlock
*block
)
615 block
= QTAILQ_NEXT(block
, next
);
622 s
->next_block
= block
;
624 if (block
->target_start
>= s
->begin
+ s
->length
||
625 block
->target_end
<= s
->begin
) {
626 /* This block is out of the range */
630 if (s
->begin
> block
->target_start
) {
631 s
->start
= s
->begin
- block
->target_start
;
639 /* write all memory to vmcore */
640 static void dump_iterate(DumpState
*s
, Error
**errp
)
642 GuestPhysBlock
*block
;
644 Error
*local_err
= NULL
;
647 block
= s
->next_block
;
649 size
= block
->target_end
- block
->target_start
;
652 if (s
->begin
+ s
->length
< block
->target_end
) {
653 size
-= block
->target_end
- (s
->begin
+ s
->length
);
656 write_memory(s
, block
, s
->start
, size
, &local_err
);
658 error_propagate(errp
, local_err
);
662 } while (!get_next_block(s
, block
));
665 static void create_vmcore(DumpState
*s
, Error
**errp
)
667 Error
*local_err
= NULL
;
669 dump_begin(s
, &local_err
);
671 error_propagate(errp
, local_err
);
675 dump_iterate(s
, errp
);
678 static int write_start_flat_header(int fd
)
680 MakedumpfileHeader
*mh
;
683 QEMU_BUILD_BUG_ON(sizeof *mh
> MAX_SIZE_MDF_HEADER
);
684 mh
= g_malloc0(MAX_SIZE_MDF_HEADER
);
686 memcpy(mh
->signature
, MAKEDUMPFILE_SIGNATURE
,
687 MIN(sizeof mh
->signature
, sizeof MAKEDUMPFILE_SIGNATURE
));
689 mh
->type
= cpu_to_be64(TYPE_FLAT_HEADER
);
690 mh
->version
= cpu_to_be64(VERSION_FLAT_HEADER
);
693 written_size
= qemu_write_full(fd
, mh
, MAX_SIZE_MDF_HEADER
);
694 if (written_size
!= MAX_SIZE_MDF_HEADER
) {
702 static int write_end_flat_header(int fd
)
704 MakedumpfileDataHeader mdh
;
706 mdh
.offset
= END_FLAG_FLAT_HEADER
;
707 mdh
.buf_size
= END_FLAG_FLAT_HEADER
;
710 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
711 if (written_size
!= sizeof(mdh
)) {
718 static int write_buffer(int fd
, off_t offset
, const void *buf
, size_t size
)
721 MakedumpfileDataHeader mdh
;
723 mdh
.offset
= cpu_to_be64(offset
);
724 mdh
.buf_size
= cpu_to_be64(size
);
726 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
727 if (written_size
!= sizeof(mdh
)) {
731 written_size
= qemu_write_full(fd
, buf
, size
);
732 if (written_size
!= size
) {
739 static int buf_write_note(const void *buf
, size_t size
, void *opaque
)
741 DumpState
*s
= opaque
;
743 /* note_buf is not enough */
744 if (s
->note_buf_offset
+ size
> s
->note_size
) {
748 memcpy(s
->note_buf
+ s
->note_buf_offset
, buf
, size
);
750 s
->note_buf_offset
+= size
;
756 * This function retrieves various sizes from an elf header.
758 * @note has to be a valid ELF note. The return sizes are unmodified
759 * (not padded or rounded up to be multiple of 4).
761 static void get_note_sizes(DumpState
*s
, const void *note
,
762 uint64_t *note_head_size
,
766 uint64_t note_head_sz
;
770 if (s
->dump_info
.d_class
== ELFCLASS64
) {
771 const Elf64_Nhdr
*hdr
= note
;
772 note_head_sz
= sizeof(Elf64_Nhdr
);
773 name_sz
= tswap64(hdr
->n_namesz
);
774 desc_sz
= tswap64(hdr
->n_descsz
);
776 const Elf32_Nhdr
*hdr
= note
;
777 note_head_sz
= sizeof(Elf32_Nhdr
);
778 name_sz
= tswap32(hdr
->n_namesz
);
779 desc_sz
= tswap32(hdr
->n_descsz
);
782 if (note_head_size
) {
783 *note_head_size
= note_head_sz
;
786 *name_size
= name_sz
;
789 *desc_size
= desc_sz
;
793 static bool note_name_equal(DumpState
*s
,
794 const uint8_t *note
, const char *name
)
796 int len
= strlen(name
) + 1;
797 uint64_t head_size
, name_size
;
799 get_note_sizes(s
, note
, &head_size
, &name_size
, NULL
);
800 head_size
= ROUND_UP(head_size
, 4);
802 return name_size
== len
&& memcmp(note
+ head_size
, name
, len
) == 0;
805 /* write common header, sub header and elf note to vmcore */
806 static void create_header32(DumpState
*s
, Error
**errp
)
808 DiskDumpHeader32
*dh
= NULL
;
809 KdumpSubHeader32
*kh
= NULL
;
812 uint32_t sub_hdr_size
;
813 uint32_t bitmap_blocks
;
815 uint64_t offset_note
;
816 Error
*local_err
= NULL
;
818 /* write common header, the version of kdump-compressed format is 6th */
819 size
= sizeof(DiskDumpHeader32
);
820 dh
= g_malloc0(size
);
822 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
823 dh
->header_version
= cpu_to_dump32(s
, 6);
824 block_size
= s
->dump_info
.page_size
;
825 dh
->block_size
= cpu_to_dump32(s
, block_size
);
826 sub_hdr_size
= sizeof(struct KdumpSubHeader32
) + s
->note_size
;
827 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
828 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
829 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
830 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
831 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
832 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
833 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
834 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
836 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
837 status
|= DUMP_DH_COMPRESSED_ZLIB
;
840 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
841 status
|= DUMP_DH_COMPRESSED_LZO
;
845 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
846 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
849 dh
->status
= cpu_to_dump32(s
, status
);
851 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
852 error_setg(errp
, "dump: failed to write disk dump header");
856 /* write sub header */
857 size
= sizeof(KdumpSubHeader32
);
858 kh
= g_malloc0(size
);
860 /* 64bit max_mapnr_64 */
861 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
862 kh
->phys_base
= cpu_to_dump32(s
, s
->dump_info
.phys_base
);
863 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
865 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
867 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
868 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
870 get_note_sizes(s
, s
->guest_note
,
871 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
872 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
873 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
874 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
875 kh
->size_vmcoreinfo
= cpu_to_dump32(s
, size_vmcoreinfo_desc
);
878 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
879 kh
->note_size
= cpu_to_dump32(s
, s
->note_size
);
881 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
882 block_size
, kh
, size
) < 0) {
883 error_setg(errp
, "dump: failed to write kdump sub header");
888 s
->note_buf
= g_malloc0(s
->note_size
);
889 s
->note_buf_offset
= 0;
891 /* use s->note_buf to store notes temporarily */
892 write_elf32_notes(buf_write_note
, s
, &local_err
);
894 error_propagate(errp
, local_err
);
897 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
899 error_setg(errp
, "dump: failed to write notes");
903 /* get offset of dump_bitmap */
904 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
907 /* get offset of page */
908 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
917 /* write common header, sub header and elf note to vmcore */
918 static void create_header64(DumpState
*s
, Error
**errp
)
920 DiskDumpHeader64
*dh
= NULL
;
921 KdumpSubHeader64
*kh
= NULL
;
924 uint32_t sub_hdr_size
;
925 uint32_t bitmap_blocks
;
927 uint64_t offset_note
;
928 Error
*local_err
= NULL
;
930 /* write common header, the version of kdump-compressed format is 6th */
931 size
= sizeof(DiskDumpHeader64
);
932 dh
= g_malloc0(size
);
934 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
935 dh
->header_version
= cpu_to_dump32(s
, 6);
936 block_size
= s
->dump_info
.page_size
;
937 dh
->block_size
= cpu_to_dump32(s
, block_size
);
938 sub_hdr_size
= sizeof(struct KdumpSubHeader64
) + s
->note_size
;
939 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
940 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
941 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
942 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
943 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
944 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
945 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
946 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
948 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
949 status
|= DUMP_DH_COMPRESSED_ZLIB
;
952 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
953 status
|= DUMP_DH_COMPRESSED_LZO
;
957 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
958 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
961 dh
->status
= cpu_to_dump32(s
, status
);
963 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
964 error_setg(errp
, "dump: failed to write disk dump header");
968 /* write sub header */
969 size
= sizeof(KdumpSubHeader64
);
970 kh
= g_malloc0(size
);
972 /* 64bit max_mapnr_64 */
973 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
974 kh
->phys_base
= cpu_to_dump64(s
, s
->dump_info
.phys_base
);
975 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
977 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
979 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
980 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
982 get_note_sizes(s
, s
->guest_note
,
983 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
984 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
985 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
986 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
987 kh
->size_vmcoreinfo
= cpu_to_dump64(s
, size_vmcoreinfo_desc
);
990 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
991 kh
->note_size
= cpu_to_dump64(s
, s
->note_size
);
993 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
994 block_size
, kh
, size
) < 0) {
995 error_setg(errp
, "dump: failed to write kdump sub header");
1000 s
->note_buf
= g_malloc0(s
->note_size
);
1001 s
->note_buf_offset
= 0;
1003 /* use s->note_buf to store notes temporarily */
1004 write_elf64_notes(buf_write_note
, s
, &local_err
);
1006 error_propagate(errp
, local_err
);
1010 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
1011 s
->note_size
) < 0) {
1012 error_setg(errp
, "dump: failed to write notes");
1016 /* get offset of dump_bitmap */
1017 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
1020 /* get offset of page */
1021 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
1027 g_free(s
->note_buf
);
1030 static void write_dump_header(DumpState
*s
, Error
**errp
)
1032 Error
*local_err
= NULL
;
1034 if (s
->dump_info
.d_class
== ELFCLASS32
) {
1035 create_header32(s
, &local_err
);
1037 create_header64(s
, &local_err
);
1039 error_propagate(errp
, local_err
);
1042 static size_t dump_bitmap_get_bufsize(DumpState
*s
)
1044 return s
->dump_info
.page_size
;
1048 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1049 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1050 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1051 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1052 * vmcore, ie. synchronizing un-sync bit into vmcore.
1054 static int set_dump_bitmap(uint64_t last_pfn
, uint64_t pfn
, bool value
,
1055 uint8_t *buf
, DumpState
*s
)
1057 off_t old_offset
, new_offset
;
1058 off_t offset_bitmap1
, offset_bitmap2
;
1060 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1061 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1063 /* should not set the previous place */
1064 assert(last_pfn
<= pfn
);
1067 * if the bit needed to be set is not cached in buf, flush the data in buf
1068 * to vmcore firstly.
1069 * making new_offset be bigger than old_offset can also sync remained data
1072 old_offset
= bitmap_bufsize
* (last_pfn
/ bits_per_buf
);
1073 new_offset
= bitmap_bufsize
* (pfn
/ bits_per_buf
);
1075 while (old_offset
< new_offset
) {
1076 /* calculate the offset and write dump_bitmap */
1077 offset_bitmap1
= s
->offset_dump_bitmap
+ old_offset
;
1078 if (write_buffer(s
->fd
, offset_bitmap1
, buf
,
1079 bitmap_bufsize
) < 0) {
1083 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1084 offset_bitmap2
= s
->offset_dump_bitmap
+ s
->len_dump_bitmap
+
1086 if (write_buffer(s
->fd
, offset_bitmap2
, buf
,
1087 bitmap_bufsize
) < 0) {
1091 memset(buf
, 0, bitmap_bufsize
);
1092 old_offset
+= bitmap_bufsize
;
1095 /* get the exact place of the bit in the buf, and set it */
1096 byte
= (pfn
% bits_per_buf
) / CHAR_BIT
;
1097 bit
= (pfn
% bits_per_buf
) % CHAR_BIT
;
1099 buf
[byte
] |= 1u << bit
;
1101 buf
[byte
] &= ~(1u << bit
);
1107 static uint64_t dump_paddr_to_pfn(DumpState
*s
, uint64_t addr
)
1109 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1111 return (addr
>> target_page_shift
) - ARCH_PFN_OFFSET
;
1114 static uint64_t dump_pfn_to_paddr(DumpState
*s
, uint64_t pfn
)
1116 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1118 return (pfn
+ ARCH_PFN_OFFSET
) << target_page_shift
;
1122 * exam every page and return the page frame number and the address of the page.
1123 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1124 * blocks, so block->target_start and block->target_end should be interal
1125 * multiples of the target page size.
1127 static bool get_next_page(GuestPhysBlock
**blockptr
, uint64_t *pfnptr
,
1128 uint8_t **bufptr
, DumpState
*s
)
1130 GuestPhysBlock
*block
= *blockptr
;
1131 hwaddr addr
, target_page_mask
= ~((hwaddr
)s
->dump_info
.page_size
- 1);
1134 /* block == NULL means the start of the iteration */
1136 block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1138 assert((block
->target_start
& ~target_page_mask
) == 0);
1139 assert((block
->target_end
& ~target_page_mask
) == 0);
1140 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1142 *bufptr
= block
->host_addr
;
1147 *pfnptr
= *pfnptr
+ 1;
1148 addr
= dump_pfn_to_paddr(s
, *pfnptr
);
1150 if ((addr
>= block
->target_start
) &&
1151 (addr
+ s
->dump_info
.page_size
<= block
->target_end
)) {
1152 buf
= block
->host_addr
+ (addr
- block
->target_start
);
1154 /* the next page is in the next block */
1155 block
= QTAILQ_NEXT(block
, next
);
1160 assert((block
->target_start
& ~target_page_mask
) == 0);
1161 assert((block
->target_end
& ~target_page_mask
) == 0);
1162 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1163 buf
= block
->host_addr
;
1173 static void write_dump_bitmap(DumpState
*s
, Error
**errp
)
1176 uint64_t last_pfn
, pfn
;
1177 void *dump_bitmap_buf
;
1178 size_t num_dumpable
;
1179 GuestPhysBlock
*block_iter
= NULL
;
1180 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1181 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1183 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1184 dump_bitmap_buf
= g_malloc0(bitmap_bufsize
);
1190 * exam memory page by page, and set the bit in dump_bitmap corresponded
1191 * to the existing page.
1193 while (get_next_page(&block_iter
, &pfn
, NULL
, s
)) {
1194 ret
= set_dump_bitmap(last_pfn
, pfn
, true, dump_bitmap_buf
, s
);
1196 error_setg(errp
, "dump: failed to set dump_bitmap");
1205 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1206 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1207 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1209 if (num_dumpable
> 0) {
1210 ret
= set_dump_bitmap(last_pfn
, last_pfn
+ bits_per_buf
, false,
1211 dump_bitmap_buf
, s
);
1213 error_setg(errp
, "dump: failed to sync dump_bitmap");
1218 /* number of dumpable pages that will be dumped later */
1219 s
->num_dumpable
= num_dumpable
;
1222 g_free(dump_bitmap_buf
);
1225 static void prepare_data_cache(DataCache
*data_cache
, DumpState
*s
,
1228 data_cache
->fd
= s
->fd
;
1229 data_cache
->data_size
= 0;
1230 data_cache
->buf_size
= 4 * dump_bitmap_get_bufsize(s
);
1231 data_cache
->buf
= g_malloc0(data_cache
->buf_size
);
1232 data_cache
->offset
= offset
;
1235 static int write_cache(DataCache
*dc
, const void *buf
, size_t size
,
1239 * dc->buf_size should not be less than size, otherwise dc will never be
1242 assert(size
<= dc
->buf_size
);
1245 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1246 * otherwise check if the space is enough for caching data in buf, if not,
1247 * write the data in dc->buf to dc->fd and reset dc->buf
1249 if ((!flag_sync
&& dc
->data_size
+ size
> dc
->buf_size
) ||
1250 (flag_sync
&& dc
->data_size
> 0)) {
1251 if (write_buffer(dc
->fd
, dc
->offset
, dc
->buf
, dc
->data_size
) < 0) {
1255 dc
->offset
+= dc
->data_size
;
1260 memcpy(dc
->buf
+ dc
->data_size
, buf
, size
);
1261 dc
->data_size
+= size
;
1267 static void free_data_cache(DataCache
*data_cache
)
1269 g_free(data_cache
->buf
);
1272 static size_t get_len_buf_out(size_t page_size
, uint32_t flag_compress
)
1274 switch (flag_compress
) {
1275 case DUMP_DH_COMPRESSED_ZLIB
:
1276 return compressBound(page_size
);
1278 case DUMP_DH_COMPRESSED_LZO
:
1280 * LZO will expand incompressible data by a little amount. Please check
1281 * the following URL to see the expansion calculation:
1282 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1284 return page_size
+ page_size
/ 16 + 64 + 3;
1286 #ifdef CONFIG_SNAPPY
1287 case DUMP_DH_COMPRESSED_SNAPPY
:
1288 return snappy_max_compressed_length(page_size
);
1295 * check if the page is all 0
1297 static inline bool is_zero_page(const uint8_t *buf
, size_t page_size
)
1299 return buffer_is_zero(buf
, page_size
);
1302 static void write_dump_pages(DumpState
*s
, Error
**errp
)
1305 DataCache page_desc
, page_data
;
1306 size_t len_buf_out
, size_out
;
1308 lzo_bytep wrkmem
= NULL
;
1310 uint8_t *buf_out
= NULL
;
1311 off_t offset_desc
, offset_data
;
1312 PageDescriptor pd
, pd_zero
;
1314 GuestPhysBlock
*block_iter
= NULL
;
1317 /* get offset of page_desc and page_data in dump file */
1318 offset_desc
= s
->offset_page
;
1319 offset_data
= offset_desc
+ sizeof(PageDescriptor
) * s
->num_dumpable
;
1321 prepare_data_cache(&page_desc
, s
, offset_desc
);
1322 prepare_data_cache(&page_data
, s
, offset_data
);
1324 /* prepare buffer to store compressed data */
1325 len_buf_out
= get_len_buf_out(s
->dump_info
.page_size
, s
->flag_compress
);
1326 assert(len_buf_out
!= 0);
1329 wrkmem
= g_malloc(LZO1X_1_MEM_COMPRESS
);
1332 buf_out
= g_malloc(len_buf_out
);
1335 * init zero page's page_desc and page_data, because every zero page
1336 * uses the same page_data
1338 pd_zero
.size
= cpu_to_dump32(s
, s
->dump_info
.page_size
);
1339 pd_zero
.flags
= cpu_to_dump32(s
, 0);
1340 pd_zero
.offset
= cpu_to_dump64(s
, offset_data
);
1341 pd_zero
.page_flags
= cpu_to_dump64(s
, 0);
1342 buf
= g_malloc0(s
->dump_info
.page_size
);
1343 ret
= write_cache(&page_data
, buf
, s
->dump_info
.page_size
, false);
1346 error_setg(errp
, "dump: failed to write page data (zero page)");
1350 offset_data
+= s
->dump_info
.page_size
;
1353 * dump memory to vmcore page by page. zero page will all be resided in the
1354 * first page of page section
1356 while (get_next_page(&block_iter
, &pfn_iter
, &buf
, s
)) {
1357 /* check zero page */
1358 if (is_zero_page(buf
, s
->dump_info
.page_size
)) {
1359 ret
= write_cache(&page_desc
, &pd_zero
, sizeof(PageDescriptor
),
1362 error_setg(errp
, "dump: failed to write page desc");
1367 * not zero page, then:
1368 * 1. compress the page
1369 * 2. write the compressed page into the cache of page_data
1370 * 3. get page desc of the compressed page and write it into the
1371 * cache of page_desc
1373 * only one compression format will be used here, for
1374 * s->flag_compress is set. But when compression fails to work,
1375 * we fall back to save in plaintext.
1377 size_out
= len_buf_out
;
1378 if ((s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) &&
1379 (compress2(buf_out
, (uLongf
*)&size_out
, buf
,
1380 s
->dump_info
.page_size
, Z_BEST_SPEED
) == Z_OK
) &&
1381 (size_out
< s
->dump_info
.page_size
)) {
1382 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_ZLIB
);
1383 pd
.size
= cpu_to_dump32(s
, size_out
);
1385 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1387 error_setg(errp
, "dump: failed to write page data");
1391 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) &&
1392 (lzo1x_1_compress(buf
, s
->dump_info
.page_size
, buf_out
,
1393 (lzo_uint
*)&size_out
, wrkmem
) == LZO_E_OK
) &&
1394 (size_out
< s
->dump_info
.page_size
)) {
1395 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_LZO
);
1396 pd
.size
= cpu_to_dump32(s
, size_out
);
1398 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1400 error_setg(errp
, "dump: failed to write page data");
1404 #ifdef CONFIG_SNAPPY
1405 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) &&
1406 (snappy_compress((char *)buf
, s
->dump_info
.page_size
,
1407 (char *)buf_out
, &size_out
) == SNAPPY_OK
) &&
1408 (size_out
< s
->dump_info
.page_size
)) {
1409 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_SNAPPY
);
1410 pd
.size
= cpu_to_dump32(s
, size_out
);
1412 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1414 error_setg(errp
, "dump: failed to write page data");
1420 * fall back to save in plaintext, size_out should be
1421 * assigned the target's page size
1423 pd
.flags
= cpu_to_dump32(s
, 0);
1424 size_out
= s
->dump_info
.page_size
;
1425 pd
.size
= cpu_to_dump32(s
, size_out
);
1427 ret
= write_cache(&page_data
, buf
,
1428 s
->dump_info
.page_size
, false);
1430 error_setg(errp
, "dump: failed to write page data");
1435 /* get and write page desc here */
1436 pd
.page_flags
= cpu_to_dump64(s
, 0);
1437 pd
.offset
= cpu_to_dump64(s
, offset_data
);
1438 offset_data
+= size_out
;
1440 ret
= write_cache(&page_desc
, &pd
, sizeof(PageDescriptor
), false);
1442 error_setg(errp
, "dump: failed to write page desc");
1446 s
->written_size
+= s
->dump_info
.page_size
;
1449 ret
= write_cache(&page_desc
, NULL
, 0, true);
1451 error_setg(errp
, "dump: failed to sync cache for page_desc");
1454 ret
= write_cache(&page_data
, NULL
, 0, true);
1456 error_setg(errp
, "dump: failed to sync cache for page_data");
1461 free_data_cache(&page_desc
);
1462 free_data_cache(&page_data
);
1471 static void create_kdump_vmcore(DumpState
*s
, Error
**errp
)
1474 Error
*local_err
= NULL
;
1477 * the kdump-compressed format is:
1479 * +------------------------------------------+ 0x0
1480 * | main header (struct disk_dump_header) |
1481 * |------------------------------------------+ block 1
1482 * | sub header (struct kdump_sub_header) |
1483 * |------------------------------------------+ block 2
1484 * | 1st-dump_bitmap |
1485 * |------------------------------------------+ block 2 + X blocks
1486 * | 2nd-dump_bitmap | (aligned by block)
1487 * |------------------------------------------+ block 2 + 2 * X blocks
1488 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1489 * | page desc for pfn 1 (struct page_desc) |
1491 * |------------------------------------------| (not aligned by block)
1492 * | page data (pfn 0) |
1493 * | page data (pfn 1) |
1495 * +------------------------------------------+
1498 ret
= write_start_flat_header(s
->fd
);
1500 error_setg(errp
, "dump: failed to write start flat header");
1504 write_dump_header(s
, &local_err
);
1506 error_propagate(errp
, local_err
);
1510 write_dump_bitmap(s
, &local_err
);
1512 error_propagate(errp
, local_err
);
1516 write_dump_pages(s
, &local_err
);
1518 error_propagate(errp
, local_err
);
1522 ret
= write_end_flat_header(s
->fd
);
1524 error_setg(errp
, "dump: failed to write end flat header");
1529 static ram_addr_t
get_start_block(DumpState
*s
)
1531 GuestPhysBlock
*block
;
1533 if (!s
->has_filter
) {
1534 s
->next_block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1538 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1539 if (block
->target_start
>= s
->begin
+ s
->length
||
1540 block
->target_end
<= s
->begin
) {
1541 /* This block is out of the range */
1545 s
->next_block
= block
;
1546 if (s
->begin
> block
->target_start
) {
1547 s
->start
= s
->begin
- block
->target_start
;
1557 static void get_max_mapnr(DumpState
*s
)
1559 GuestPhysBlock
*last_block
;
1561 last_block
= QTAILQ_LAST(&s
->guest_phys_blocks
.head
);
1562 s
->max_mapnr
= dump_paddr_to_pfn(s
, last_block
->target_end
);
1565 static DumpState dump_state_global
= { .status
= DUMP_STATUS_NONE
};
1567 static void dump_state_prepare(DumpState
*s
)
1569 /* zero the struct, setting status to active */
1570 *s
= (DumpState
) { .status
= DUMP_STATUS_ACTIVE
};
1573 bool dump_in_progress(void)
1575 DumpState
*state
= &dump_state_global
;
1576 return (atomic_read(&state
->status
) == DUMP_STATUS_ACTIVE
);
1579 /* calculate total size of memory to be dumped (taking filter into
1581 static int64_t dump_calculate_size(DumpState
*s
)
1583 GuestPhysBlock
*block
;
1584 int64_t size
= 0, total
= 0, left
= 0, right
= 0;
1586 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1587 if (s
->has_filter
) {
1588 /* calculate the overlapped region. */
1589 left
= MAX(s
->begin
, block
->target_start
);
1590 right
= MIN(s
->begin
+ s
->length
, block
->target_end
);
1591 size
= right
- left
;
1592 size
= size
> 0 ? size
: 0;
1594 /* count the whole region in */
1595 size
= (block
->target_end
- block
->target_start
);
1603 static void vmcoreinfo_update_phys_base(DumpState
*s
)
1605 uint64_t size
, note_head_size
, name_size
, phys_base
;
1610 if (!note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1614 get_note_sizes(s
, s
->guest_note
, ¬e_head_size
, &name_size
, &size
);
1615 note_head_size
= ROUND_UP(note_head_size
, 4);
1617 vmci
= s
->guest_note
+ note_head_size
+ ROUND_UP(name_size
, 4);
1618 *(vmci
+ size
) = '\0';
1620 lines
= g_strsplit((char *)vmci
, "\n", -1);
1621 for (i
= 0; lines
[i
]; i
++) {
1622 const char *prefix
= NULL
;
1624 if (s
->dump_info
.d_machine
== EM_X86_64
) {
1625 prefix
= "NUMBER(phys_base)=";
1626 } else if (s
->dump_info
.d_machine
== EM_AARCH64
) {
1627 prefix
= "NUMBER(PHYS_OFFSET)=";
1630 if (prefix
&& g_str_has_prefix(lines
[i
], prefix
)) {
1631 if (qemu_strtou64(lines
[i
] + strlen(prefix
), NULL
, 16,
1633 warn_report("Failed to read %s", prefix
);
1635 s
->dump_info
.phys_base
= phys_base
;
1644 static void dump_init(DumpState
*s
, int fd
, bool has_format
,
1645 DumpGuestMemoryFormat format
, bool paging
, bool has_filter
,
1646 int64_t begin
, int64_t length
, Error
**errp
)
1648 VMCoreInfoState
*vmci
= vmcoreinfo_find();
1654 s
->has_format
= has_format
;
1656 s
->written_size
= 0;
1658 /* kdump-compressed is conflict with paging and filter */
1659 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1660 assert(!paging
&& !has_filter
);
1663 if (runstate_is_running()) {
1664 vm_stop(RUN_STATE_SAVE_VM
);
1670 /* If we use KVM, we should synchronize the registers before we get dump
1671 * info or physmap info.
1673 cpu_synchronize_all_states();
1680 s
->has_filter
= has_filter
;
1684 memory_mapping_list_init(&s
->list
);
1686 guest_phys_blocks_init(&s
->guest_phys_blocks
);
1687 guest_phys_blocks_append(&s
->guest_phys_blocks
);
1688 s
->total_size
= dump_calculate_size(s
);
1689 #ifdef DEBUG_DUMP_GUEST_MEMORY
1690 fprintf(stderr
, "DUMP: total memory to dump: %lu\n", s
->total_size
);
1693 /* it does not make sense to dump non-existent memory */
1694 if (!s
->total_size
) {
1695 error_setg(errp
, "dump: no guest memory to dump");
1699 s
->start
= get_start_block(s
);
1700 if (s
->start
== -1) {
1701 error_setg(errp
, QERR_INVALID_PARAMETER
, "begin");
1705 /* get dump info: endian, class and architecture.
1706 * If the target architecture is not supported, cpu_get_dump_info() will
1709 ret
= cpu_get_dump_info(&s
->dump_info
, &s
->guest_phys_blocks
);
1711 error_setg(errp
, QERR_UNSUPPORTED
);
1715 if (!s
->dump_info
.page_size
) {
1716 s
->dump_info
.page_size
= TARGET_PAGE_SIZE
;
1719 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
1720 s
->dump_info
.d_machine
, nr_cpus
);
1721 if (s
->note_size
< 0) {
1722 error_setg(errp
, QERR_UNSUPPORTED
);
1727 * The goal of this block is to (a) update the previously guessed
1728 * phys_base, (b) copy the guest note out of the guest.
1729 * Failure to do so is not fatal for dumping.
1732 uint64_t addr
, note_head_size
, name_size
, desc_size
;
1736 note_head_size
= s
->dump_info
.d_class
== ELFCLASS32
?
1737 sizeof(Elf32_Nhdr
) : sizeof(Elf64_Nhdr
);
1739 format
= le16_to_cpu(vmci
->vmcoreinfo
.guest_format
);
1740 size
= le32_to_cpu(vmci
->vmcoreinfo
.size
);
1741 addr
= le64_to_cpu(vmci
->vmcoreinfo
.paddr
);
1742 if (!vmci
->has_vmcoreinfo
) {
1743 warn_report("guest note is not present");
1744 } else if (size
< note_head_size
|| size
> MAX_GUEST_NOTE_SIZE
) {
1745 warn_report("guest note size is invalid: %" PRIu32
, size
);
1746 } else if (format
!= FW_CFG_VMCOREINFO_FORMAT_ELF
) {
1747 warn_report("guest note format is unsupported: %" PRIu16
, format
);
1749 s
->guest_note
= g_malloc(size
+ 1); /* +1 for adding \0 */
1750 cpu_physical_memory_read(addr
, s
->guest_note
, size
);
1752 get_note_sizes(s
, s
->guest_note
, NULL
, &name_size
, &desc_size
);
1753 s
->guest_note_size
= ELF_NOTE_SIZE(note_head_size
, name_size
,
1755 if (name_size
> MAX_GUEST_NOTE_SIZE
||
1756 desc_size
> MAX_GUEST_NOTE_SIZE
||
1757 s
->guest_note_size
> size
) {
1758 warn_report("Invalid guest note header");
1759 g_free(s
->guest_note
);
1760 s
->guest_note
= NULL
;
1762 vmcoreinfo_update_phys_base(s
);
1763 s
->note_size
+= s
->guest_note_size
;
1768 /* get memory mapping */
1770 qemu_get_guest_memory_mapping(&s
->list
, &s
->guest_phys_blocks
, &err
);
1772 error_propagate(errp
, err
);
1776 qemu_get_guest_simple_memory_mapping(&s
->list
, &s
->guest_phys_blocks
);
1779 s
->nr_cpus
= nr_cpus
;
1784 tmp
= DIV_ROUND_UP(DIV_ROUND_UP(s
->max_mapnr
, CHAR_BIT
),
1785 s
->dump_info
.page_size
);
1786 s
->len_dump_bitmap
= tmp
* s
->dump_info
.page_size
;
1788 /* init for kdump-compressed format */
1789 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1791 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
:
1792 s
->flag_compress
= DUMP_DH_COMPRESSED_ZLIB
;
1795 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
:
1797 if (lzo_init() != LZO_E_OK
) {
1798 error_setg(errp
, "failed to initialize the LZO library");
1802 s
->flag_compress
= DUMP_DH_COMPRESSED_LZO
;
1805 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
:
1806 s
->flag_compress
= DUMP_DH_COMPRESSED_SNAPPY
;
1810 s
->flag_compress
= 0;
1816 if (s
->has_filter
) {
1817 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
1821 * calculate phdr_num
1823 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1825 s
->phdr_num
= 1; /* PT_NOTE */
1826 if (s
->list
.num
< UINT16_MAX
- 2) {
1827 s
->phdr_num
+= s
->list
.num
;
1828 s
->have_section
= false;
1830 s
->have_section
= true;
1831 s
->phdr_num
= PN_XNUM
;
1832 s
->sh_info
= 1; /* PT_NOTE */
1834 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1835 if (s
->list
.num
<= UINT32_MAX
- 1) {
1836 s
->sh_info
+= s
->list
.num
;
1838 s
->sh_info
= UINT32_MAX
;
1842 if (s
->dump_info
.d_class
== ELFCLASS64
) {
1843 if (s
->have_section
) {
1844 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1845 sizeof(Elf64_Phdr
) * s
->sh_info
+
1846 sizeof(Elf64_Shdr
) + s
->note_size
;
1848 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1849 sizeof(Elf64_Phdr
) * s
->phdr_num
+ s
->note_size
;
1852 if (s
->have_section
) {
1853 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1854 sizeof(Elf32_Phdr
) * s
->sh_info
+
1855 sizeof(Elf32_Shdr
) + s
->note_size
;
1857 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1858 sizeof(Elf32_Phdr
) * s
->phdr_num
+ s
->note_size
;
1868 /* this operation might be time consuming. */
1869 static void dump_process(DumpState
*s
, Error
**errp
)
1871 Error
*local_err
= NULL
;
1872 DumpQueryResult
*result
= NULL
;
1874 if (s
->has_format
&& s
->format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
) {
1875 #ifdef TARGET_X86_64
1876 create_win_dump(s
, &local_err
);
1878 } else if (s
->has_format
&& s
->format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1879 create_kdump_vmcore(s
, &local_err
);
1881 create_vmcore(s
, &local_err
);
1884 /* make sure status is written after written_size updates */
1886 atomic_set(&s
->status
,
1887 (local_err
? DUMP_STATUS_FAILED
: DUMP_STATUS_COMPLETED
));
1889 /* send DUMP_COMPLETED message (unconditionally) */
1890 result
= qmp_query_dump(NULL
);
1891 /* should never fail */
1893 qapi_event_send_dump_completed(result
, !!local_err
, (local_err
? \
1894 error_get_pretty(local_err
) : NULL
));
1895 qapi_free_DumpQueryResult(result
);
1897 error_propagate(errp
, local_err
);
1901 static void *dump_thread(void *data
)
1903 DumpState
*s
= (DumpState
*)data
;
1904 dump_process(s
, NULL
);
1908 DumpQueryResult
*qmp_query_dump(Error
**errp
)
1910 DumpQueryResult
*result
= g_new(DumpQueryResult
, 1);
1911 DumpState
*state
= &dump_state_global
;
1912 result
->status
= atomic_read(&state
->status
);
1913 /* make sure we are reading status and written_size in order */
1915 result
->completed
= state
->written_size
;
1916 result
->total
= state
->total_size
;
1920 void qmp_dump_guest_memory(bool paging
, const char *file
,
1921 bool has_detach
, bool detach
,
1922 bool has_begin
, int64_t begin
, bool has_length
,
1923 int64_t length
, bool has_format
,
1924 DumpGuestMemoryFormat format
, Error
**errp
)
1929 Error
*local_err
= NULL
;
1930 bool detach_p
= false;
1932 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1933 error_setg(errp
, "Dump not allowed during incoming migration.");
1937 /* if there is a dump in background, we should wait until the dump
1939 if (dump_in_progress()) {
1940 error_setg(errp
, "There is a dump in process, please wait.");
1945 * kdump-compressed format need the whole memory dumped, so paging or
1946 * filter is not supported here.
1948 if ((has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) &&
1949 (paging
|| has_begin
|| has_length
)) {
1950 error_setg(errp
, "kdump-compressed format doesn't support paging or "
1954 if (has_begin
&& !has_length
) {
1955 error_setg(errp
, QERR_MISSING_PARAMETER
, "length");
1958 if (!has_begin
&& has_length
) {
1959 error_setg(errp
, QERR_MISSING_PARAMETER
, "begin");
1966 /* check whether lzo/snappy is supported */
1968 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
) {
1969 error_setg(errp
, "kdump-lzo is not available now");
1974 #ifndef CONFIG_SNAPPY
1975 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
) {
1976 error_setg(errp
, "kdump-snappy is not available now");
1981 #ifndef TARGET_X86_64
1982 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
) {
1983 error_setg(errp
, "Windows dump is only available for x86-64");
1989 if (strstart(file
, "fd:", &p
)) {
1990 fd
= monitor_get_fd(cur_mon
, p
, errp
);
1997 if (strstart(file
, "file:", &p
)) {
1998 fd
= qemu_open(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
2000 error_setg_file_open(errp
, errno
, p
);
2006 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
2010 s
= &dump_state_global
;
2011 dump_state_prepare(s
);
2013 dump_init(s
, fd
, has_format
, format
, paging
, has_begin
,
2014 begin
, length
, &local_err
);
2016 error_propagate(errp
, local_err
);
2017 atomic_set(&s
->status
, DUMP_STATUS_FAILED
);
2024 qemu_thread_create(&s
->dump_thread
, "dump_thread", dump_thread
,
2025 s
, QEMU_THREAD_DETACHED
);
2028 dump_process(s
, errp
);
2032 DumpGuestMemoryCapability
*qmp_query_dump_guest_memory_capability(Error
**errp
)
2034 DumpGuestMemoryFormatList
*item
;
2035 DumpGuestMemoryCapability
*cap
=
2036 g_malloc0(sizeof(DumpGuestMemoryCapability
));
2038 /* elf is always available */
2039 item
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
2040 cap
->formats
= item
;
2041 item
->value
= DUMP_GUEST_MEMORY_FORMAT_ELF
;
2043 /* kdump-zlib is always available */
2044 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
2046 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
;
2048 /* add new item if kdump-lzo is available */
2050 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
2052 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
;
2055 /* add new item if kdump-snappy is available */
2056 #ifdef CONFIG_SNAPPY
2057 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
2059 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
;
2062 /* Windows dump is available only if target is x86_64 */
2063 #ifdef TARGET_X86_64
2064 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
2066 item
->value
= DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
;