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"
17 #include "exec/hwaddr.h"
18 #include "monitor/monitor.h"
19 #include "sysemu/kvm.h"
20 #include "sysemu/dump.h"
21 #include "sysemu/memory_mapping.h"
22 #include "sysemu/runstate.h"
23 #include "sysemu/cpus.h"
24 #include "qapi/error.h"
25 #include "qapi/qapi-commands-dump.h"
26 #include "qapi/qapi-events-dump.h"
27 #include "qapi/qmp/qerror.h"
28 #include "qemu/error-report.h"
29 #include "qemu/main-loop.h"
30 #include "hw/misc/vmcoreinfo.h"
31 #include "migration/blocker.h"
39 #include <lzo/lzo1x.h>
44 #ifndef ELF_MACHINE_UNAME
45 #define ELF_MACHINE_UNAME "Unknown"
48 #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
50 static Error
*dump_migration_blocker
;
52 #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
53 ((DIV_ROUND_UP((hdr_size), 4) + \
54 DIV_ROUND_UP((name_size), 4) + \
55 DIV_ROUND_UP((desc_size), 4)) * 4)
57 static inline bool dump_is_64bit(DumpState
*s
)
59 return s
->dump_info
.d_class
== ELFCLASS64
;
62 static inline bool dump_has_filter(DumpState
*s
)
64 return s
->filter_area_length
> 0;
67 uint16_t cpu_to_dump16(DumpState
*s
, uint16_t val
)
69 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
70 val
= cpu_to_le16(val
);
72 val
= cpu_to_be16(val
);
78 uint32_t cpu_to_dump32(DumpState
*s
, uint32_t val
)
80 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
81 val
= cpu_to_le32(val
);
83 val
= cpu_to_be32(val
);
89 uint64_t cpu_to_dump64(DumpState
*s
, uint64_t val
)
91 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
92 val
= cpu_to_le64(val
);
94 val
= cpu_to_be64(val
);
100 static int dump_cleanup(DumpState
*s
)
102 guest_phys_blocks_free(&s
->guest_phys_blocks
);
103 memory_mapping_list_free(&s
->list
);
105 g_free(s
->guest_note
);
106 s
->guest_note
= NULL
;
109 qemu_mutex_lock_iothread();
113 qemu_mutex_unlock_iothread();
116 migrate_del_blocker(dump_migration_blocker
);
121 static int fd_write_vmcore(const void *buf
, size_t size
, void *opaque
)
123 DumpState
*s
= opaque
;
126 written_size
= qemu_write_full(s
->fd
, buf
, size
);
127 if (written_size
!= size
) {
134 static void prepare_elf64_header(DumpState
*s
, Elf64_Ehdr
*elf_header
)
137 * phnum in the elf header is 16 bit, if we have more segments we
138 * set phnum to PN_XNUM and write the real number of segments to a
141 uint16_t phnum
= MIN(s
->phdr_num
, PN_XNUM
);
143 memset(elf_header
, 0, sizeof(Elf64_Ehdr
));
144 memcpy(elf_header
, ELFMAG
, SELFMAG
);
145 elf_header
->e_ident
[EI_CLASS
] = ELFCLASS64
;
146 elf_header
->e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
147 elf_header
->e_ident
[EI_VERSION
] = EV_CURRENT
;
148 elf_header
->e_type
= cpu_to_dump16(s
, ET_CORE
);
149 elf_header
->e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
150 elf_header
->e_version
= cpu_to_dump32(s
, EV_CURRENT
);
151 elf_header
->e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
152 elf_header
->e_phoff
= cpu_to_dump64(s
, s
->phdr_offset
);
153 elf_header
->e_phentsize
= cpu_to_dump16(s
, sizeof(Elf64_Phdr
));
154 elf_header
->e_phnum
= cpu_to_dump16(s
, phnum
);
156 elf_header
->e_shoff
= cpu_to_dump64(s
, s
->shdr_offset
);
157 elf_header
->e_shentsize
= cpu_to_dump16(s
, sizeof(Elf64_Shdr
));
158 elf_header
->e_shnum
= cpu_to_dump16(s
, s
->shdr_num
);
162 static void prepare_elf32_header(DumpState
*s
, Elf32_Ehdr
*elf_header
)
165 * phnum in the elf header is 16 bit, if we have more segments we
166 * set phnum to PN_XNUM and write the real number of segments to a
169 uint16_t phnum
= MIN(s
->phdr_num
, PN_XNUM
);
171 memset(elf_header
, 0, sizeof(Elf32_Ehdr
));
172 memcpy(elf_header
, ELFMAG
, SELFMAG
);
173 elf_header
->e_ident
[EI_CLASS
] = ELFCLASS32
;
174 elf_header
->e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
175 elf_header
->e_ident
[EI_VERSION
] = EV_CURRENT
;
176 elf_header
->e_type
= cpu_to_dump16(s
, ET_CORE
);
177 elf_header
->e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
178 elf_header
->e_version
= cpu_to_dump32(s
, EV_CURRENT
);
179 elf_header
->e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
180 elf_header
->e_phoff
= cpu_to_dump32(s
, s
->phdr_offset
);
181 elf_header
->e_phentsize
= cpu_to_dump16(s
, sizeof(Elf32_Phdr
));
182 elf_header
->e_phnum
= cpu_to_dump16(s
, phnum
);
184 elf_header
->e_shoff
= cpu_to_dump32(s
, s
->shdr_offset
);
185 elf_header
->e_shentsize
= cpu_to_dump16(s
, sizeof(Elf32_Shdr
));
186 elf_header
->e_shnum
= cpu_to_dump16(s
, s
->shdr_num
);
190 static void write_elf_header(DumpState
*s
, Error
**errp
)
192 Elf32_Ehdr elf32_header
;
193 Elf64_Ehdr elf64_header
;
198 if (dump_is_64bit(s
)) {
199 prepare_elf64_header(s
, &elf64_header
);
200 header_size
= sizeof(elf64_header
);
201 header_ptr
= &elf64_header
;
203 prepare_elf32_header(s
, &elf32_header
);
204 header_size
= sizeof(elf32_header
);
205 header_ptr
= &elf32_header
;
208 ret
= fd_write_vmcore(header_ptr
, header_size
, s
);
210 error_setg_errno(errp
, -ret
, "dump: failed to write elf header");
214 static void write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
215 int phdr_index
, hwaddr offset
,
216 hwaddr filesz
, Error
**errp
)
221 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
222 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
223 phdr
.p_offset
= cpu_to_dump64(s
, offset
);
224 phdr
.p_paddr
= cpu_to_dump64(s
, memory_mapping
->phys_addr
);
225 phdr
.p_filesz
= cpu_to_dump64(s
, filesz
);
226 phdr
.p_memsz
= cpu_to_dump64(s
, memory_mapping
->length
);
227 phdr
.p_vaddr
= cpu_to_dump64(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
229 assert(memory_mapping
->length
>= filesz
);
231 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
233 error_setg_errno(errp
, -ret
,
234 "dump: failed to write program header table");
238 static void write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
239 int phdr_index
, hwaddr offset
,
240 hwaddr filesz
, Error
**errp
)
245 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
246 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
247 phdr
.p_offset
= cpu_to_dump32(s
, offset
);
248 phdr
.p_paddr
= cpu_to_dump32(s
, memory_mapping
->phys_addr
);
249 phdr
.p_filesz
= cpu_to_dump32(s
, filesz
);
250 phdr
.p_memsz
= cpu_to_dump32(s
, memory_mapping
->length
);
252 cpu_to_dump32(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
254 assert(memory_mapping
->length
>= filesz
);
256 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
258 error_setg_errno(errp
, -ret
,
259 "dump: failed to write program header table");
263 static void prepare_elf64_phdr_note(DumpState
*s
, Elf64_Phdr
*phdr
)
265 memset(phdr
, 0, sizeof(*phdr
));
266 phdr
->p_type
= cpu_to_dump32(s
, PT_NOTE
);
267 phdr
->p_offset
= cpu_to_dump64(s
, s
->note_offset
);
269 phdr
->p_filesz
= cpu_to_dump64(s
, s
->note_size
);
270 phdr
->p_memsz
= cpu_to_dump64(s
, s
->note_size
);
274 static inline int cpu_index(CPUState
*cpu
)
276 return cpu
->cpu_index
+ 1;
279 static void write_guest_note(WriteCoreDumpFunction f
, DumpState
*s
,
285 ret
= f(s
->guest_note
, s
->guest_note_size
, s
);
287 error_setg(errp
, "dump: failed to write guest note");
292 static void write_elf64_notes(WriteCoreDumpFunction f
, DumpState
*s
,
301 ret
= cpu_write_elf64_note(f
, cpu
, id
, s
);
303 error_setg(errp
, "dump: failed to write elf notes");
309 ret
= cpu_write_elf64_qemunote(f
, cpu
, s
);
311 error_setg(errp
, "dump: failed to write CPU status");
316 write_guest_note(f
, s
, errp
);
319 static void prepare_elf32_phdr_note(DumpState
*s
, Elf32_Phdr
*phdr
)
321 memset(phdr
, 0, sizeof(*phdr
));
322 phdr
->p_type
= cpu_to_dump32(s
, PT_NOTE
);
323 phdr
->p_offset
= cpu_to_dump32(s
, s
->note_offset
);
325 phdr
->p_filesz
= cpu_to_dump32(s
, s
->note_size
);
326 phdr
->p_memsz
= cpu_to_dump32(s
, s
->note_size
);
330 static void write_elf32_notes(WriteCoreDumpFunction f
, DumpState
*s
,
339 ret
= cpu_write_elf32_note(f
, cpu
, id
, s
);
341 error_setg(errp
, "dump: failed to write elf notes");
347 ret
= cpu_write_elf32_qemunote(f
, cpu
, s
);
349 error_setg(errp
, "dump: failed to write CPU status");
354 write_guest_note(f
, s
, errp
);
357 static void write_elf_phdr_note(DumpState
*s
, Error
**errp
)
366 if (dump_is_64bit(s
)) {
367 prepare_elf64_phdr_note(s
, &phdr64
);
368 size
= sizeof(phdr64
);
371 prepare_elf32_phdr_note(s
, &phdr32
);
372 size
= sizeof(phdr32
);
376 ret
= fd_write_vmcore(phdr
, size
, s
);
378 error_setg_errno(errp
, -ret
,
379 "dump: failed to write program header table");
383 static void prepare_elf_section_hdr_zero(DumpState
*s
)
385 if (dump_is_64bit(s
)) {
386 Elf64_Shdr
*shdr64
= s
->elf_section_hdrs
;
388 shdr64
->sh_info
= cpu_to_dump32(s
, s
->phdr_num
);
390 Elf32_Shdr
*shdr32
= s
->elf_section_hdrs
;
392 shdr32
->sh_info
= cpu_to_dump32(s
, s
->phdr_num
);
396 static void prepare_elf_section_hdrs(DumpState
*s
)
398 size_t len
, sizeof_shdr
;
404 sizeof_shdr
= dump_is_64bit(s
) ? sizeof(Elf64_Shdr
) : sizeof(Elf32_Shdr
);
405 len
= sizeof_shdr
* s
->shdr_num
;
406 s
->elf_section_hdrs
= g_malloc0(len
);
409 * The first section header is ALWAYS a special initial section
412 * The header should be 0 with one exception being that if
413 * phdr_num is PN_XNUM then the sh_info field contains the real
414 * number of segment entries.
416 * As we zero allocate the buffer we will only need to modify
417 * sh_info for the PN_XNUM case.
419 if (s
->phdr_num
>= PN_XNUM
) {
420 prepare_elf_section_hdr_zero(s
);
424 static void write_elf_section_headers(DumpState
*s
, Error
**errp
)
426 size_t sizeof_shdr
= dump_is_64bit(s
) ? sizeof(Elf64_Shdr
) : sizeof(Elf32_Shdr
);
429 prepare_elf_section_hdrs(s
);
431 ret
= fd_write_vmcore(s
->elf_section_hdrs
, s
->shdr_num
* sizeof_shdr
, s
);
433 error_setg_errno(errp
, -ret
, "dump: failed to write section headers");
436 g_free(s
->elf_section_hdrs
);
439 static void write_data(DumpState
*s
, void *buf
, int length
, Error
**errp
)
443 ret
= fd_write_vmcore(buf
, length
, s
);
445 error_setg_errno(errp
, -ret
, "dump: failed to save memory");
447 s
->written_size
+= length
;
451 /* write the memory to vmcore. 1 page per I/O. */
452 static void write_memory(DumpState
*s
, GuestPhysBlock
*block
, ram_addr_t start
,
453 int64_t size
, Error
**errp
)
458 for (i
= 0; i
< size
/ s
->dump_info
.page_size
; i
++) {
459 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
460 s
->dump_info
.page_size
, errp
);
466 if ((size
% s
->dump_info
.page_size
) != 0) {
467 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
468 size
% s
->dump_info
.page_size
, errp
);
475 /* get the memory's offset and size in the vmcore */
476 static void get_offset_range(hwaddr phys_addr
,
477 ram_addr_t mapping_length
,
482 GuestPhysBlock
*block
;
483 hwaddr offset
= s
->memory_offset
;
484 int64_t size_in_block
, start
;
486 /* When the memory is not stored into vmcore, offset will be -1 */
490 if (dump_has_filter(s
)) {
491 if (phys_addr
< s
->filter_area_begin
||
492 phys_addr
>= s
->filter_area_begin
+ s
->filter_area_length
) {
497 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
498 if (dump_has_filter(s
)) {
499 if (block
->target_start
>= s
->filter_area_begin
+ s
->filter_area_length
||
500 block
->target_end
<= s
->filter_area_begin
) {
501 /* This block is out of the range */
505 if (s
->filter_area_begin
<= block
->target_start
) {
506 start
= block
->target_start
;
508 start
= s
->filter_area_begin
;
511 size_in_block
= block
->target_end
- start
;
512 if (s
->filter_area_begin
+ s
->filter_area_length
< block
->target_end
) {
513 size_in_block
-= block
->target_end
- (s
->filter_area_begin
+ s
->filter_area_length
);
516 start
= block
->target_start
;
517 size_in_block
= block
->target_end
- block
->target_start
;
520 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
521 *p_offset
= phys_addr
- start
+ offset
;
523 /* The offset range mapped from the vmcore file must not spill over
524 * the GuestPhysBlock, clamp it. The rest of the mapping will be
525 * zero-filled in memory at load time; see
526 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
528 *p_filesz
= phys_addr
+ mapping_length
<= start
+ size_in_block
?
530 size_in_block
- (phys_addr
- start
);
534 offset
+= size_in_block
;
538 static void write_elf_phdr_loads(DumpState
*s
, Error
**errp
)
541 hwaddr offset
, filesz
;
542 MemoryMapping
*memory_mapping
;
543 uint32_t phdr_index
= 1;
545 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
546 get_offset_range(memory_mapping
->phys_addr
,
547 memory_mapping
->length
,
548 s
, &offset
, &filesz
);
549 if (dump_is_64bit(s
)) {
550 write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
,
553 write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
,
561 if (phdr_index
>= s
->phdr_num
) {
567 static void write_elf_notes(DumpState
*s
, Error
**errp
)
569 if (dump_is_64bit(s
)) {
570 write_elf64_notes(fd_write_vmcore
, s
, errp
);
572 write_elf32_notes(fd_write_vmcore
, s
, errp
);
576 /* write elf header, PT_NOTE and elf note to vmcore. */
577 static void dump_begin(DumpState
*s
, Error
**errp
)
582 * the vmcore's format is:
601 * we only know where the memory is saved after we write elf note into
605 /* write elf header to vmcore */
606 write_elf_header(s
, errp
);
611 /* write section headers to vmcore */
612 write_elf_section_headers(s
, errp
);
617 /* write PT_NOTE to vmcore */
618 write_elf_phdr_note(s
, errp
);
623 /* write all PT_LOADs to vmcore */
624 write_elf_phdr_loads(s
, errp
);
629 /* write notes to vmcore */
630 write_elf_notes(s
, errp
);
633 static int64_t dump_filtered_memblock_size(GuestPhysBlock
*block
,
634 int64_t filter_area_start
,
635 int64_t filter_area_length
)
637 int64_t size
, left
, right
;
639 /* No filter, return full size */
640 if (!filter_area_length
) {
641 return block
->target_end
- block
->target_start
;
644 /* calculate the overlapped region. */
645 left
= MAX(filter_area_start
, block
->target_start
);
646 right
= MIN(filter_area_start
+ filter_area_length
, block
->target_end
);
648 size
= size
> 0 ? size
: 0;
653 static int64_t dump_filtered_memblock_start(GuestPhysBlock
*block
,
654 int64_t filter_area_start
,
655 int64_t filter_area_length
)
657 if (filter_area_length
) {
658 /* return -1 if the block is not within filter area */
659 if (block
->target_start
>= filter_area_start
+ filter_area_length
||
660 block
->target_end
<= filter_area_start
) {
664 if (filter_area_start
> block
->target_start
) {
665 return filter_area_start
- block
->target_start
;
672 /* write all memory to vmcore */
673 static void dump_iterate(DumpState
*s
, Error
**errp
)
676 GuestPhysBlock
*block
;
677 int64_t memblock_size
, memblock_start
;
679 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
680 memblock_start
= dump_filtered_memblock_start(block
, s
->filter_area_begin
, s
->filter_area_length
);
681 if (memblock_start
== -1) {
685 memblock_size
= dump_filtered_memblock_size(block
, s
->filter_area_begin
, s
->filter_area_length
);
687 /* Write the memory to file */
688 write_memory(s
, block
, memblock_start
, memblock_size
, errp
);
695 static void create_vmcore(DumpState
*s
, Error
**errp
)
704 dump_iterate(s
, errp
);
707 static int write_start_flat_header(int fd
)
709 MakedumpfileHeader
*mh
;
712 QEMU_BUILD_BUG_ON(sizeof *mh
> MAX_SIZE_MDF_HEADER
);
713 mh
= g_malloc0(MAX_SIZE_MDF_HEADER
);
715 memcpy(mh
->signature
, MAKEDUMPFILE_SIGNATURE
,
716 MIN(sizeof mh
->signature
, sizeof MAKEDUMPFILE_SIGNATURE
));
718 mh
->type
= cpu_to_be64(TYPE_FLAT_HEADER
);
719 mh
->version
= cpu_to_be64(VERSION_FLAT_HEADER
);
722 written_size
= qemu_write_full(fd
, mh
, MAX_SIZE_MDF_HEADER
);
723 if (written_size
!= MAX_SIZE_MDF_HEADER
) {
731 static int write_end_flat_header(int fd
)
733 MakedumpfileDataHeader mdh
;
735 mdh
.offset
= END_FLAG_FLAT_HEADER
;
736 mdh
.buf_size
= END_FLAG_FLAT_HEADER
;
739 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
740 if (written_size
!= sizeof(mdh
)) {
747 static int write_buffer(int fd
, off_t offset
, const void *buf
, size_t size
)
750 MakedumpfileDataHeader mdh
;
752 mdh
.offset
= cpu_to_be64(offset
);
753 mdh
.buf_size
= cpu_to_be64(size
);
755 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
756 if (written_size
!= sizeof(mdh
)) {
760 written_size
= qemu_write_full(fd
, buf
, size
);
761 if (written_size
!= size
) {
768 static int buf_write_note(const void *buf
, size_t size
, void *opaque
)
770 DumpState
*s
= opaque
;
772 /* note_buf is not enough */
773 if (s
->note_buf_offset
+ size
> s
->note_size
) {
777 memcpy(s
->note_buf
+ s
->note_buf_offset
, buf
, size
);
779 s
->note_buf_offset
+= size
;
785 * This function retrieves various sizes from an elf header.
787 * @note has to be a valid ELF note. The return sizes are unmodified
788 * (not padded or rounded up to be multiple of 4).
790 static void get_note_sizes(DumpState
*s
, const void *note
,
791 uint64_t *note_head_size
,
795 uint64_t note_head_sz
;
799 if (dump_is_64bit(s
)) {
800 const Elf64_Nhdr
*hdr
= note
;
801 note_head_sz
= sizeof(Elf64_Nhdr
);
802 name_sz
= tswap64(hdr
->n_namesz
);
803 desc_sz
= tswap64(hdr
->n_descsz
);
805 const Elf32_Nhdr
*hdr
= note
;
806 note_head_sz
= sizeof(Elf32_Nhdr
);
807 name_sz
= tswap32(hdr
->n_namesz
);
808 desc_sz
= tswap32(hdr
->n_descsz
);
811 if (note_head_size
) {
812 *note_head_size
= note_head_sz
;
815 *name_size
= name_sz
;
818 *desc_size
= desc_sz
;
822 static bool note_name_equal(DumpState
*s
,
823 const uint8_t *note
, const char *name
)
825 int len
= strlen(name
) + 1;
826 uint64_t head_size
, name_size
;
828 get_note_sizes(s
, note
, &head_size
, &name_size
, NULL
);
829 head_size
= ROUND_UP(head_size
, 4);
831 return name_size
== len
&& memcmp(note
+ head_size
, name
, len
) == 0;
834 /* write common header, sub header and elf note to vmcore */
835 static void create_header32(DumpState
*s
, Error
**errp
)
838 DiskDumpHeader32
*dh
= NULL
;
839 KdumpSubHeader32
*kh
= NULL
;
842 uint32_t sub_hdr_size
;
843 uint32_t bitmap_blocks
;
845 uint64_t offset_note
;
847 /* write common header, the version of kdump-compressed format is 6th */
848 size
= sizeof(DiskDumpHeader32
);
849 dh
= g_malloc0(size
);
851 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
852 dh
->header_version
= cpu_to_dump32(s
, 6);
853 block_size
= s
->dump_info
.page_size
;
854 dh
->block_size
= cpu_to_dump32(s
, block_size
);
855 sub_hdr_size
= sizeof(struct KdumpSubHeader32
) + s
->note_size
;
856 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
857 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
858 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
859 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
860 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
861 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
862 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
863 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
865 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
866 status
|= DUMP_DH_COMPRESSED_ZLIB
;
869 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
870 status
|= DUMP_DH_COMPRESSED_LZO
;
874 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
875 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
878 dh
->status
= cpu_to_dump32(s
, status
);
880 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
881 error_setg(errp
, "dump: failed to write disk dump header");
885 /* write sub header */
886 size
= sizeof(KdumpSubHeader32
);
887 kh
= g_malloc0(size
);
889 /* 64bit max_mapnr_64 */
890 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
891 kh
->phys_base
= cpu_to_dump32(s
, s
->dump_info
.phys_base
);
892 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
894 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
896 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
897 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
899 get_note_sizes(s
, s
->guest_note
,
900 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
901 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
902 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
903 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
904 kh
->size_vmcoreinfo
= cpu_to_dump32(s
, size_vmcoreinfo_desc
);
907 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
908 kh
->note_size
= cpu_to_dump32(s
, s
->note_size
);
910 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
911 block_size
, kh
, size
) < 0) {
912 error_setg(errp
, "dump: failed to write kdump sub header");
917 s
->note_buf
= g_malloc0(s
->note_size
);
918 s
->note_buf_offset
= 0;
920 /* use s->note_buf to store notes temporarily */
921 write_elf32_notes(buf_write_note
, s
, errp
);
925 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
927 error_setg(errp
, "dump: failed to write notes");
931 /* get offset of dump_bitmap */
932 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
935 /* get offset of page */
936 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
945 /* write common header, sub header and elf note to vmcore */
946 static void create_header64(DumpState
*s
, Error
**errp
)
949 DiskDumpHeader64
*dh
= NULL
;
950 KdumpSubHeader64
*kh
= NULL
;
953 uint32_t sub_hdr_size
;
954 uint32_t bitmap_blocks
;
956 uint64_t offset_note
;
958 /* write common header, the version of kdump-compressed format is 6th */
959 size
= sizeof(DiskDumpHeader64
);
960 dh
= g_malloc0(size
);
962 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
963 dh
->header_version
= cpu_to_dump32(s
, 6);
964 block_size
= s
->dump_info
.page_size
;
965 dh
->block_size
= cpu_to_dump32(s
, block_size
);
966 sub_hdr_size
= sizeof(struct KdumpSubHeader64
) + s
->note_size
;
967 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
968 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
969 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
970 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
971 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
972 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
973 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
974 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
976 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
977 status
|= DUMP_DH_COMPRESSED_ZLIB
;
980 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
981 status
|= DUMP_DH_COMPRESSED_LZO
;
985 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
986 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
989 dh
->status
= cpu_to_dump32(s
, status
);
991 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
992 error_setg(errp
, "dump: failed to write disk dump header");
996 /* write sub header */
997 size
= sizeof(KdumpSubHeader64
);
998 kh
= g_malloc0(size
);
1000 /* 64bit max_mapnr_64 */
1001 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
1002 kh
->phys_base
= cpu_to_dump64(s
, s
->dump_info
.phys_base
);
1003 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
1005 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
1006 if (s
->guest_note
&&
1007 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1008 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
1010 get_note_sizes(s
, s
->guest_note
,
1011 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
1012 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
1013 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
1014 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
1015 kh
->size_vmcoreinfo
= cpu_to_dump64(s
, size_vmcoreinfo_desc
);
1018 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
1019 kh
->note_size
= cpu_to_dump64(s
, s
->note_size
);
1021 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
1022 block_size
, kh
, size
) < 0) {
1023 error_setg(errp
, "dump: failed to write kdump sub header");
1028 s
->note_buf
= g_malloc0(s
->note_size
);
1029 s
->note_buf_offset
= 0;
1031 /* use s->note_buf to store notes temporarily */
1032 write_elf64_notes(buf_write_note
, s
, errp
);
1037 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
1038 s
->note_size
) < 0) {
1039 error_setg(errp
, "dump: failed to write notes");
1043 /* get offset of dump_bitmap */
1044 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
1047 /* get offset of page */
1048 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
1054 g_free(s
->note_buf
);
1057 static void write_dump_header(DumpState
*s
, Error
**errp
)
1059 if (dump_is_64bit(s
)) {
1060 create_header64(s
, errp
);
1062 create_header32(s
, errp
);
1066 static size_t dump_bitmap_get_bufsize(DumpState
*s
)
1068 return s
->dump_info
.page_size
;
1072 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1073 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1074 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1075 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1076 * vmcore, ie. synchronizing un-sync bit into vmcore.
1078 static int set_dump_bitmap(uint64_t last_pfn
, uint64_t pfn
, bool value
,
1079 uint8_t *buf
, DumpState
*s
)
1081 off_t old_offset
, new_offset
;
1082 off_t offset_bitmap1
, offset_bitmap2
;
1084 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1085 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1087 /* should not set the previous place */
1088 assert(last_pfn
<= pfn
);
1091 * if the bit needed to be set is not cached in buf, flush the data in buf
1092 * to vmcore firstly.
1093 * making new_offset be bigger than old_offset can also sync remained data
1096 old_offset
= bitmap_bufsize
* (last_pfn
/ bits_per_buf
);
1097 new_offset
= bitmap_bufsize
* (pfn
/ bits_per_buf
);
1099 while (old_offset
< new_offset
) {
1100 /* calculate the offset and write dump_bitmap */
1101 offset_bitmap1
= s
->offset_dump_bitmap
+ old_offset
;
1102 if (write_buffer(s
->fd
, offset_bitmap1
, buf
,
1103 bitmap_bufsize
) < 0) {
1107 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1108 offset_bitmap2
= s
->offset_dump_bitmap
+ s
->len_dump_bitmap
+
1110 if (write_buffer(s
->fd
, offset_bitmap2
, buf
,
1111 bitmap_bufsize
) < 0) {
1115 memset(buf
, 0, bitmap_bufsize
);
1116 old_offset
+= bitmap_bufsize
;
1119 /* get the exact place of the bit in the buf, and set it */
1120 byte
= (pfn
% bits_per_buf
) / CHAR_BIT
;
1121 bit
= (pfn
% bits_per_buf
) % CHAR_BIT
;
1123 buf
[byte
] |= 1u << bit
;
1125 buf
[byte
] &= ~(1u << bit
);
1131 static uint64_t dump_paddr_to_pfn(DumpState
*s
, uint64_t addr
)
1133 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1135 return (addr
>> target_page_shift
) - ARCH_PFN_OFFSET
;
1138 static uint64_t dump_pfn_to_paddr(DumpState
*s
, uint64_t pfn
)
1140 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1142 return (pfn
+ ARCH_PFN_OFFSET
) << target_page_shift
;
1146 * Return the page frame number and the page content in *bufptr. bufptr can be
1147 * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
1148 * memory. This is not necessarily the memory returned.
1150 static bool get_next_page(GuestPhysBlock
**blockptr
, uint64_t *pfnptr
,
1151 uint8_t **bufptr
, DumpState
*s
)
1153 GuestPhysBlock
*block
= *blockptr
;
1154 uint32_t page_size
= s
->dump_info
.page_size
;
1155 uint8_t *buf
= NULL
, *hbuf
;
1158 /* block == NULL means the start of the iteration */
1160 block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1162 addr
= block
->target_start
;
1163 *pfnptr
= dump_paddr_to_pfn(s
, addr
);
1166 addr
= dump_pfn_to_paddr(s
, *pfnptr
);
1168 assert(block
!= NULL
);
1171 if (addr
>= block
->target_start
&& addr
< block
->target_end
) {
1172 size_t n
= MIN(block
->target_end
- addr
, page_size
- addr
% page_size
);
1173 hbuf
= block
->host_addr
+ (addr
- block
->target_start
);
1175 if (n
== page_size
) {
1176 /* this is a whole target page, go for it */
1177 assert(addr
% page_size
== 0);
1180 } else if (bufptr
) {
1183 memset(buf
, 0, page_size
);
1189 memcpy(buf
+ addr
% page_size
, hbuf
, n
);
1191 if (addr
% page_size
== 0) {
1192 /* we filled up the page */
1196 /* the next page is in the next block */
1197 *blockptr
= block
= QTAILQ_NEXT(block
, next
);
1202 addr
= block
->target_start
;
1203 /* are we still in the same page? */
1204 if (dump_paddr_to_pfn(s
, addr
) != *pfnptr
) {
1206 /* no, but we already filled something earlier, return it */
1209 /* else continue from there */
1210 *pfnptr
= dump_paddr_to_pfn(s
, addr
);
1223 static void write_dump_bitmap(DumpState
*s
, Error
**errp
)
1226 uint64_t last_pfn
, pfn
;
1227 void *dump_bitmap_buf
;
1228 size_t num_dumpable
;
1229 GuestPhysBlock
*block_iter
= NULL
;
1230 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1231 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1233 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1234 dump_bitmap_buf
= g_malloc0(bitmap_bufsize
);
1240 * exam memory page by page, and set the bit in dump_bitmap corresponded
1241 * to the existing page.
1243 while (get_next_page(&block_iter
, &pfn
, NULL
, s
)) {
1244 ret
= set_dump_bitmap(last_pfn
, pfn
, true, dump_bitmap_buf
, s
);
1246 error_setg(errp
, "dump: failed to set dump_bitmap");
1255 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1256 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1257 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1259 if (num_dumpable
> 0) {
1260 ret
= set_dump_bitmap(last_pfn
, last_pfn
+ bits_per_buf
, false,
1261 dump_bitmap_buf
, s
);
1263 error_setg(errp
, "dump: failed to sync dump_bitmap");
1268 /* number of dumpable pages that will be dumped later */
1269 s
->num_dumpable
= num_dumpable
;
1272 g_free(dump_bitmap_buf
);
1275 static void prepare_data_cache(DataCache
*data_cache
, DumpState
*s
,
1278 data_cache
->fd
= s
->fd
;
1279 data_cache
->data_size
= 0;
1280 data_cache
->buf_size
= 4 * dump_bitmap_get_bufsize(s
);
1281 data_cache
->buf
= g_malloc0(data_cache
->buf_size
);
1282 data_cache
->offset
= offset
;
1285 static int write_cache(DataCache
*dc
, const void *buf
, size_t size
,
1289 * dc->buf_size should not be less than size, otherwise dc will never be
1292 assert(size
<= dc
->buf_size
);
1295 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1296 * otherwise check if the space is enough for caching data in buf, if not,
1297 * write the data in dc->buf to dc->fd and reset dc->buf
1299 if ((!flag_sync
&& dc
->data_size
+ size
> dc
->buf_size
) ||
1300 (flag_sync
&& dc
->data_size
> 0)) {
1301 if (write_buffer(dc
->fd
, dc
->offset
, dc
->buf
, dc
->data_size
) < 0) {
1305 dc
->offset
+= dc
->data_size
;
1310 memcpy(dc
->buf
+ dc
->data_size
, buf
, size
);
1311 dc
->data_size
+= size
;
1317 static void free_data_cache(DataCache
*data_cache
)
1319 g_free(data_cache
->buf
);
1322 static size_t get_len_buf_out(size_t page_size
, uint32_t flag_compress
)
1324 switch (flag_compress
) {
1325 case DUMP_DH_COMPRESSED_ZLIB
:
1326 return compressBound(page_size
);
1328 case DUMP_DH_COMPRESSED_LZO
:
1330 * LZO will expand incompressible data by a little amount. Please check
1331 * the following URL to see the expansion calculation:
1332 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1334 return page_size
+ page_size
/ 16 + 64 + 3;
1336 #ifdef CONFIG_SNAPPY
1337 case DUMP_DH_COMPRESSED_SNAPPY
:
1338 return snappy_max_compressed_length(page_size
);
1344 static void write_dump_pages(DumpState
*s
, Error
**errp
)
1347 DataCache page_desc
, page_data
;
1348 size_t len_buf_out
, size_out
;
1350 lzo_bytep wrkmem
= NULL
;
1352 uint8_t *buf_out
= NULL
;
1353 off_t offset_desc
, offset_data
;
1354 PageDescriptor pd
, pd_zero
;
1356 GuestPhysBlock
*block_iter
= NULL
;
1358 g_autofree
uint8_t *page
= NULL
;
1360 /* get offset of page_desc and page_data in dump file */
1361 offset_desc
= s
->offset_page
;
1362 offset_data
= offset_desc
+ sizeof(PageDescriptor
) * s
->num_dumpable
;
1364 prepare_data_cache(&page_desc
, s
, offset_desc
);
1365 prepare_data_cache(&page_data
, s
, offset_data
);
1367 /* prepare buffer to store compressed data */
1368 len_buf_out
= get_len_buf_out(s
->dump_info
.page_size
, s
->flag_compress
);
1369 assert(len_buf_out
!= 0);
1372 wrkmem
= g_malloc(LZO1X_1_MEM_COMPRESS
);
1375 buf_out
= g_malloc(len_buf_out
);
1378 * init zero page's page_desc and page_data, because every zero page
1379 * uses the same page_data
1381 pd_zero
.size
= cpu_to_dump32(s
, s
->dump_info
.page_size
);
1382 pd_zero
.flags
= cpu_to_dump32(s
, 0);
1383 pd_zero
.offset
= cpu_to_dump64(s
, offset_data
);
1384 pd_zero
.page_flags
= cpu_to_dump64(s
, 0);
1385 buf
= g_malloc0(s
->dump_info
.page_size
);
1386 ret
= write_cache(&page_data
, buf
, s
->dump_info
.page_size
, false);
1389 error_setg(errp
, "dump: failed to write page data (zero page)");
1393 offset_data
+= s
->dump_info
.page_size
;
1394 page
= g_malloc(s
->dump_info
.page_size
);
1397 * dump memory to vmcore page by page. zero page will all be resided in the
1398 * first page of page section
1400 for (buf
= page
; get_next_page(&block_iter
, &pfn_iter
, &buf
, s
); buf
= page
) {
1401 /* check zero page */
1402 if (buffer_is_zero(buf
, s
->dump_info
.page_size
)) {
1403 ret
= write_cache(&page_desc
, &pd_zero
, sizeof(PageDescriptor
),
1406 error_setg(errp
, "dump: failed to write page desc");
1411 * not zero page, then:
1412 * 1. compress the page
1413 * 2. write the compressed page into the cache of page_data
1414 * 3. get page desc of the compressed page and write it into the
1415 * cache of page_desc
1417 * only one compression format will be used here, for
1418 * s->flag_compress is set. But when compression fails to work,
1419 * we fall back to save in plaintext.
1421 size_out
= len_buf_out
;
1422 if ((s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) &&
1423 (compress2(buf_out
, (uLongf
*)&size_out
, buf
,
1424 s
->dump_info
.page_size
, Z_BEST_SPEED
) == Z_OK
) &&
1425 (size_out
< s
->dump_info
.page_size
)) {
1426 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_ZLIB
);
1427 pd
.size
= cpu_to_dump32(s
, size_out
);
1429 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1431 error_setg(errp
, "dump: failed to write page data");
1435 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) &&
1436 (lzo1x_1_compress(buf
, s
->dump_info
.page_size
, buf_out
,
1437 (lzo_uint
*)&size_out
, wrkmem
) == LZO_E_OK
) &&
1438 (size_out
< s
->dump_info
.page_size
)) {
1439 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_LZO
);
1440 pd
.size
= cpu_to_dump32(s
, size_out
);
1442 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1444 error_setg(errp
, "dump: failed to write page data");
1448 #ifdef CONFIG_SNAPPY
1449 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) &&
1450 (snappy_compress((char *)buf
, s
->dump_info
.page_size
,
1451 (char *)buf_out
, &size_out
) == SNAPPY_OK
) &&
1452 (size_out
< s
->dump_info
.page_size
)) {
1453 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_SNAPPY
);
1454 pd
.size
= cpu_to_dump32(s
, size_out
);
1456 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1458 error_setg(errp
, "dump: failed to write page data");
1464 * fall back to save in plaintext, size_out should be
1465 * assigned the target's page size
1467 pd
.flags
= cpu_to_dump32(s
, 0);
1468 size_out
= s
->dump_info
.page_size
;
1469 pd
.size
= cpu_to_dump32(s
, size_out
);
1471 ret
= write_cache(&page_data
, buf
,
1472 s
->dump_info
.page_size
, false);
1474 error_setg(errp
, "dump: failed to write page data");
1479 /* get and write page desc here */
1480 pd
.page_flags
= cpu_to_dump64(s
, 0);
1481 pd
.offset
= cpu_to_dump64(s
, offset_data
);
1482 offset_data
+= size_out
;
1484 ret
= write_cache(&page_desc
, &pd
, sizeof(PageDescriptor
), false);
1486 error_setg(errp
, "dump: failed to write page desc");
1490 s
->written_size
+= s
->dump_info
.page_size
;
1493 ret
= write_cache(&page_desc
, NULL
, 0, true);
1495 error_setg(errp
, "dump: failed to sync cache for page_desc");
1498 ret
= write_cache(&page_data
, NULL
, 0, true);
1500 error_setg(errp
, "dump: failed to sync cache for page_data");
1505 free_data_cache(&page_desc
);
1506 free_data_cache(&page_data
);
1515 static void create_kdump_vmcore(DumpState
*s
, Error
**errp
)
1521 * the kdump-compressed format is:
1523 * +------------------------------------------+ 0x0
1524 * | main header (struct disk_dump_header) |
1525 * |------------------------------------------+ block 1
1526 * | sub header (struct kdump_sub_header) |
1527 * |------------------------------------------+ block 2
1528 * | 1st-dump_bitmap |
1529 * |------------------------------------------+ block 2 + X blocks
1530 * | 2nd-dump_bitmap | (aligned by block)
1531 * |------------------------------------------+ block 2 + 2 * X blocks
1532 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1533 * | page desc for pfn 1 (struct page_desc) |
1535 * |------------------------------------------| (not aligned by block)
1536 * | page data (pfn 0) |
1537 * | page data (pfn 1) |
1539 * +------------------------------------------+
1542 ret
= write_start_flat_header(s
->fd
);
1544 error_setg(errp
, "dump: failed to write start flat header");
1548 write_dump_header(s
, errp
);
1553 write_dump_bitmap(s
, errp
);
1558 write_dump_pages(s
, errp
);
1563 ret
= write_end_flat_header(s
->fd
);
1565 error_setg(errp
, "dump: failed to write end flat header");
1570 static int validate_start_block(DumpState
*s
)
1572 GuestPhysBlock
*block
;
1574 if (!dump_has_filter(s
)) {
1578 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1579 /* This block is out of the range */
1580 if (block
->target_start
>= s
->filter_area_begin
+ s
->filter_area_length
||
1581 block
->target_end
<= s
->filter_area_begin
) {
1590 static void get_max_mapnr(DumpState
*s
)
1592 GuestPhysBlock
*last_block
;
1594 last_block
= QTAILQ_LAST(&s
->guest_phys_blocks
.head
);
1595 s
->max_mapnr
= dump_paddr_to_pfn(s
, last_block
->target_end
);
1598 static DumpState dump_state_global
= { .status
= DUMP_STATUS_NONE
};
1600 static void dump_state_prepare(DumpState
*s
)
1602 /* zero the struct, setting status to active */
1603 *s
= (DumpState
) { .status
= DUMP_STATUS_ACTIVE
};
1606 bool qemu_system_dump_in_progress(void)
1608 DumpState
*state
= &dump_state_global
;
1609 return (qatomic_read(&state
->status
) == DUMP_STATUS_ACTIVE
);
1613 * calculate total size of memory to be dumped (taking filter into
1616 static int64_t dump_calculate_size(DumpState
*s
)
1618 GuestPhysBlock
*block
;
1621 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1622 total
+= dump_filtered_memblock_size(block
,
1623 s
->filter_area_begin
,
1624 s
->filter_area_length
);
1630 static void vmcoreinfo_update_phys_base(DumpState
*s
)
1632 uint64_t size
, note_head_size
, name_size
, phys_base
;
1637 if (!note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1641 get_note_sizes(s
, s
->guest_note
, ¬e_head_size
, &name_size
, &size
);
1642 note_head_size
= ROUND_UP(note_head_size
, 4);
1644 vmci
= s
->guest_note
+ note_head_size
+ ROUND_UP(name_size
, 4);
1645 *(vmci
+ size
) = '\0';
1647 lines
= g_strsplit((char *)vmci
, "\n", -1);
1648 for (i
= 0; lines
[i
]; i
++) {
1649 const char *prefix
= NULL
;
1651 if (s
->dump_info
.d_machine
== EM_X86_64
) {
1652 prefix
= "NUMBER(phys_base)=";
1653 } else if (s
->dump_info
.d_machine
== EM_AARCH64
) {
1654 prefix
= "NUMBER(PHYS_OFFSET)=";
1657 if (prefix
&& g_str_has_prefix(lines
[i
], prefix
)) {
1658 if (qemu_strtou64(lines
[i
] + strlen(prefix
), NULL
, 16,
1660 warn_report("Failed to read %s", prefix
);
1662 s
->dump_info
.phys_base
= phys_base
;
1671 static void dump_init(DumpState
*s
, int fd
, bool has_format
,
1672 DumpGuestMemoryFormat format
, bool paging
, bool has_filter
,
1673 int64_t begin
, int64_t length
, Error
**errp
)
1676 VMCoreInfoState
*vmci
= vmcoreinfo_find();
1681 s
->has_format
= has_format
;
1683 s
->written_size
= 0;
1685 /* kdump-compressed is conflict with paging and filter */
1686 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1687 assert(!paging
&& !has_filter
);
1690 if (runstate_is_running()) {
1691 vm_stop(RUN_STATE_SAVE_VM
);
1697 /* If we use KVM, we should synchronize the registers before we get dump
1698 * info or physmap info.
1700 cpu_synchronize_all_states();
1707 if (has_filter
&& !length
) {
1708 error_setg(errp
, QERR_INVALID_PARAMETER
, "length");
1711 s
->filter_area_begin
= begin
;
1712 s
->filter_area_length
= length
;
1714 memory_mapping_list_init(&s
->list
);
1716 guest_phys_blocks_init(&s
->guest_phys_blocks
);
1717 guest_phys_blocks_append(&s
->guest_phys_blocks
);
1718 s
->total_size
= dump_calculate_size(s
);
1719 #ifdef DEBUG_DUMP_GUEST_MEMORY
1720 fprintf(stderr
, "DUMP: total memory to dump: %lu\n", s
->total_size
);
1723 /* it does not make sense to dump non-existent memory */
1724 if (!s
->total_size
) {
1725 error_setg(errp
, "dump: no guest memory to dump");
1729 /* Is the filter filtering everything? */
1730 if (validate_start_block(s
) == -1) {
1731 error_setg(errp
, QERR_INVALID_PARAMETER
, "begin");
1735 /* get dump info: endian, class and architecture.
1736 * If the target architecture is not supported, cpu_get_dump_info() will
1739 ret
= cpu_get_dump_info(&s
->dump_info
, &s
->guest_phys_blocks
);
1741 error_setg(errp
, QERR_UNSUPPORTED
);
1745 if (!s
->dump_info
.page_size
) {
1746 s
->dump_info
.page_size
= TARGET_PAGE_SIZE
;
1749 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
1750 s
->dump_info
.d_machine
, nr_cpus
);
1751 if (s
->note_size
< 0) {
1752 error_setg(errp
, QERR_UNSUPPORTED
);
1757 * The goal of this block is to (a) update the previously guessed
1758 * phys_base, (b) copy the guest note out of the guest.
1759 * Failure to do so is not fatal for dumping.
1762 uint64_t addr
, note_head_size
, name_size
, desc_size
;
1766 note_head_size
= dump_is_64bit(s
) ?
1767 sizeof(Elf64_Nhdr
) : sizeof(Elf32_Nhdr
);
1769 format
= le16_to_cpu(vmci
->vmcoreinfo
.guest_format
);
1770 size
= le32_to_cpu(vmci
->vmcoreinfo
.size
);
1771 addr
= le64_to_cpu(vmci
->vmcoreinfo
.paddr
);
1772 if (!vmci
->has_vmcoreinfo
) {
1773 warn_report("guest note is not present");
1774 } else if (size
< note_head_size
|| size
> MAX_GUEST_NOTE_SIZE
) {
1775 warn_report("guest note size is invalid: %" PRIu32
, size
);
1776 } else if (format
!= FW_CFG_VMCOREINFO_FORMAT_ELF
) {
1777 warn_report("guest note format is unsupported: %" PRIu16
, format
);
1779 s
->guest_note
= g_malloc(size
+ 1); /* +1 for adding \0 */
1780 cpu_physical_memory_read(addr
, s
->guest_note
, size
);
1782 get_note_sizes(s
, s
->guest_note
, NULL
, &name_size
, &desc_size
);
1783 s
->guest_note_size
= ELF_NOTE_SIZE(note_head_size
, name_size
,
1785 if (name_size
> MAX_GUEST_NOTE_SIZE
||
1786 desc_size
> MAX_GUEST_NOTE_SIZE
||
1787 s
->guest_note_size
> size
) {
1788 warn_report("Invalid guest note header");
1789 g_free(s
->guest_note
);
1790 s
->guest_note
= NULL
;
1792 vmcoreinfo_update_phys_base(s
);
1793 s
->note_size
+= s
->guest_note_size
;
1798 /* get memory mapping */
1800 qemu_get_guest_memory_mapping(&s
->list
, &s
->guest_phys_blocks
, errp
);
1805 qemu_get_guest_simple_memory_mapping(&s
->list
, &s
->guest_phys_blocks
);
1808 s
->nr_cpus
= nr_cpus
;
1813 tmp
= DIV_ROUND_UP(DIV_ROUND_UP(s
->max_mapnr
, CHAR_BIT
),
1814 s
->dump_info
.page_size
);
1815 s
->len_dump_bitmap
= tmp
* s
->dump_info
.page_size
;
1817 /* init for kdump-compressed format */
1818 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1820 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
:
1821 s
->flag_compress
= DUMP_DH_COMPRESSED_ZLIB
;
1824 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
:
1826 if (lzo_init() != LZO_E_OK
) {
1827 error_setg(errp
, "failed to initialize the LZO library");
1831 s
->flag_compress
= DUMP_DH_COMPRESSED_LZO
;
1834 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
:
1835 s
->flag_compress
= DUMP_DH_COMPRESSED_SNAPPY
;
1839 s
->flag_compress
= 0;
1845 if (dump_has_filter(s
)) {
1846 memory_mapping_filter(&s
->list
, s
->filter_area_begin
, s
->filter_area_length
);
1850 * calculate phdr_num
1852 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1854 s
->phdr_num
= 1; /* PT_NOTE */
1855 if (s
->list
.num
< UINT16_MAX
- 2) {
1857 s
->phdr_num
+= s
->list
.num
;
1859 /* sh_info of section 0 holds the real number of phdrs */
1862 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1863 if (s
->list
.num
<= UINT32_MAX
- 1) {
1864 s
->phdr_num
+= s
->list
.num
;
1866 s
->phdr_num
= UINT32_MAX
;
1870 if (dump_is_64bit(s
)) {
1871 s
->shdr_offset
= sizeof(Elf64_Ehdr
);
1872 s
->phdr_offset
= s
->shdr_offset
+ sizeof(Elf64_Shdr
) * s
->shdr_num
;
1873 s
->note_offset
= s
->phdr_offset
+ sizeof(Elf64_Phdr
) * s
->phdr_num
;
1875 s
->shdr_offset
= sizeof(Elf32_Ehdr
);
1876 s
->phdr_offset
= s
->shdr_offset
+ sizeof(Elf32_Shdr
) * s
->shdr_num
;
1877 s
->note_offset
= s
->phdr_offset
+ sizeof(Elf32_Phdr
) * s
->phdr_num
;
1879 s
->memory_offset
= s
->note_offset
+ s
->note_size
;
1880 s
->section_offset
= s
->memory_offset
+ s
->total_size
;
1888 /* this operation might be time consuming. */
1889 static void dump_process(DumpState
*s
, Error
**errp
)
1892 DumpQueryResult
*result
= NULL
;
1894 if (s
->has_format
&& s
->format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
) {
1895 #ifdef TARGET_X86_64
1896 create_win_dump(s
, errp
);
1898 } else if (s
->has_format
&& s
->format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1899 create_kdump_vmcore(s
, errp
);
1901 create_vmcore(s
, errp
);
1904 /* make sure status is written after written_size updates */
1906 qatomic_set(&s
->status
,
1907 (*errp
? DUMP_STATUS_FAILED
: DUMP_STATUS_COMPLETED
));
1909 /* send DUMP_COMPLETED message (unconditionally) */
1910 result
= qmp_query_dump(NULL
);
1911 /* should never fail */
1913 qapi_event_send_dump_completed(result
, !!*errp
, (*errp
?
1914 error_get_pretty(*errp
) : NULL
));
1915 qapi_free_DumpQueryResult(result
);
1920 static void *dump_thread(void *data
)
1922 DumpState
*s
= (DumpState
*)data
;
1923 dump_process(s
, NULL
);
1927 DumpQueryResult
*qmp_query_dump(Error
**errp
)
1929 DumpQueryResult
*result
= g_new(DumpQueryResult
, 1);
1930 DumpState
*state
= &dump_state_global
;
1931 result
->status
= qatomic_read(&state
->status
);
1932 /* make sure we are reading status and written_size in order */
1934 result
->completed
= state
->written_size
;
1935 result
->total
= state
->total_size
;
1939 void qmp_dump_guest_memory(bool paging
, const char *file
,
1940 bool has_detach
, bool detach
,
1941 bool has_begin
, int64_t begin
, bool has_length
,
1942 int64_t length
, bool has_format
,
1943 DumpGuestMemoryFormat format
, Error
**errp
)
1949 bool detach_p
= false;
1951 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1952 error_setg(errp
, "Dump not allowed during incoming migration.");
1956 /* if there is a dump in background, we should wait until the dump
1958 if (qemu_system_dump_in_progress()) {
1959 error_setg(errp
, "There is a dump in process, please wait.");
1964 * kdump-compressed format need the whole memory dumped, so paging or
1965 * filter is not supported here.
1967 if ((has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) &&
1968 (paging
|| has_begin
|| has_length
)) {
1969 error_setg(errp
, "kdump-compressed format doesn't support paging or "
1973 if (has_begin
&& !has_length
) {
1974 error_setg(errp
, QERR_MISSING_PARAMETER
, "length");
1977 if (!has_begin
&& has_length
) {
1978 error_setg(errp
, QERR_MISSING_PARAMETER
, "begin");
1985 /* check whether lzo/snappy is supported */
1987 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
) {
1988 error_setg(errp
, "kdump-lzo is not available now");
1993 #ifndef CONFIG_SNAPPY
1994 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
) {
1995 error_setg(errp
, "kdump-snappy is not available now");
2000 #ifndef TARGET_X86_64
2001 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
) {
2002 error_setg(errp
, "Windows dump is only available for x86-64");
2008 if (strstart(file
, "fd:", &p
)) {
2009 fd
= monitor_get_fd(monitor_cur(), p
, errp
);
2016 if (strstart(file
, "file:", &p
)) {
2017 fd
= qemu_open_old(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
2019 error_setg_file_open(errp
, errno
, p
);
2025 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
2029 if (!dump_migration_blocker
) {
2030 error_setg(&dump_migration_blocker
,
2031 "Live migration disabled: dump-guest-memory in progress");
2035 * Allows even for -only-migratable, but forbid migration during the
2036 * process of dump guest memory.
2038 if (migrate_add_blocker_internal(dump_migration_blocker
, errp
)) {
2039 /* Remember to release the fd before passing it over to dump state */
2044 s
= &dump_state_global
;
2045 dump_state_prepare(s
);
2047 dump_init(s
, fd
, has_format
, format
, paging
, has_begin
,
2048 begin
, length
, errp
);
2050 qatomic_set(&s
->status
, DUMP_STATUS_FAILED
);
2057 qemu_thread_create(&s
->dump_thread
, "dump_thread", dump_thread
,
2058 s
, QEMU_THREAD_DETACHED
);
2061 dump_process(s
, errp
);
2065 DumpGuestMemoryCapability
*qmp_query_dump_guest_memory_capability(Error
**errp
)
2067 DumpGuestMemoryCapability
*cap
=
2068 g_new0(DumpGuestMemoryCapability
, 1);
2069 DumpGuestMemoryFormatList
**tail
= &cap
->formats
;
2071 /* elf is always available */
2072 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_ELF
);
2074 /* kdump-zlib is always available */
2075 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
);
2077 /* add new item if kdump-lzo is available */
2079 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
);
2082 /* add new item if kdump-snappy is available */
2083 #ifdef CONFIG_SNAPPY
2084 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
);
2087 /* Windows dump is available only if target is x86_64 */
2088 #ifdef TARGET_X86_64
2089 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
);