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/qmp/qerror.h"
27 #include "qmp-commands.h"
28 #include "qapi-event.h"
29 #include "qemu/error-report.h"
30 #include "hw/misc/vmcoreinfo.h"
34 #include <lzo/lzo1x.h>
39 #ifndef ELF_MACHINE_UNAME
40 #define ELF_MACHINE_UNAME "Unknown"
43 #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
45 #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
46 ((DIV_ROUND_UP((hdr_size), 4) + \
47 DIV_ROUND_UP((name_size), 4) + \
48 DIV_ROUND_UP((desc_size), 4)) * 4)
50 uint16_t cpu_to_dump16(DumpState
*s
, uint16_t val
)
52 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
53 val
= cpu_to_le16(val
);
55 val
= cpu_to_be16(val
);
61 uint32_t cpu_to_dump32(DumpState
*s
, uint32_t val
)
63 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
64 val
= cpu_to_le32(val
);
66 val
= cpu_to_be32(val
);
72 uint64_t cpu_to_dump64(DumpState
*s
, uint64_t val
)
74 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
75 val
= cpu_to_le64(val
);
77 val
= cpu_to_be64(val
);
83 static int dump_cleanup(DumpState
*s
)
85 guest_phys_blocks_free(&s
->guest_phys_blocks
);
86 memory_mapping_list_free(&s
->list
);
88 g_free(s
->guest_note
);
92 qemu_mutex_lock_iothread();
96 qemu_mutex_unlock_iothread();
103 static int fd_write_vmcore(const void *buf
, size_t size
, void *opaque
)
105 DumpState
*s
= opaque
;
108 written_size
= qemu_write_full(s
->fd
, buf
, size
);
109 if (written_size
!= size
) {
116 static void write_elf64_header(DumpState
*s
, Error
**errp
)
118 Elf64_Ehdr elf_header
;
121 memset(&elf_header
, 0, sizeof(Elf64_Ehdr
));
122 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
123 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS64
;
124 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
125 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
126 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
127 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
128 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
129 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
130 elf_header
.e_phoff
= cpu_to_dump64(s
, sizeof(Elf64_Ehdr
));
131 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf64_Phdr
));
132 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
133 if (s
->have_section
) {
134 uint64_t shoff
= sizeof(Elf64_Ehdr
) + sizeof(Elf64_Phdr
) * s
->sh_info
;
136 elf_header
.e_shoff
= cpu_to_dump64(s
, shoff
);
137 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf64_Shdr
));
138 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
141 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
143 error_setg(errp
, "dump: failed to write elf header");
147 static void write_elf32_header(DumpState
*s
, Error
**errp
)
149 Elf32_Ehdr elf_header
;
152 memset(&elf_header
, 0, sizeof(Elf32_Ehdr
));
153 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
154 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS32
;
155 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
156 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
157 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
158 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
159 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
160 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
161 elf_header
.e_phoff
= cpu_to_dump32(s
, sizeof(Elf32_Ehdr
));
162 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf32_Phdr
));
163 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
164 if (s
->have_section
) {
165 uint32_t shoff
= sizeof(Elf32_Ehdr
) + sizeof(Elf32_Phdr
) * s
->sh_info
;
167 elf_header
.e_shoff
= cpu_to_dump32(s
, shoff
);
168 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf32_Shdr
));
169 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
172 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
174 error_setg(errp
, "dump: failed to write elf header");
178 static void write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
179 int phdr_index
, hwaddr offset
,
180 hwaddr filesz
, Error
**errp
)
185 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
186 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
187 phdr
.p_offset
= cpu_to_dump64(s
, offset
);
188 phdr
.p_paddr
= cpu_to_dump64(s
, memory_mapping
->phys_addr
);
189 phdr
.p_filesz
= cpu_to_dump64(s
, filesz
);
190 phdr
.p_memsz
= cpu_to_dump64(s
, memory_mapping
->length
);
191 phdr
.p_vaddr
= cpu_to_dump64(s
, memory_mapping
->virt_addr
);
193 assert(memory_mapping
->length
>= filesz
);
195 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
197 error_setg(errp
, "dump: failed to write program header table");
201 static void write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
202 int phdr_index
, hwaddr offset
,
203 hwaddr filesz
, Error
**errp
)
208 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
209 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
210 phdr
.p_offset
= cpu_to_dump32(s
, offset
);
211 phdr
.p_paddr
= cpu_to_dump32(s
, memory_mapping
->phys_addr
);
212 phdr
.p_filesz
= cpu_to_dump32(s
, filesz
);
213 phdr
.p_memsz
= cpu_to_dump32(s
, memory_mapping
->length
);
214 phdr
.p_vaddr
= cpu_to_dump32(s
, memory_mapping
->virt_addr
);
216 assert(memory_mapping
->length
>= filesz
);
218 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
220 error_setg(errp
, "dump: failed to write program header table");
224 static void write_elf64_note(DumpState
*s
, Error
**errp
)
227 hwaddr begin
= s
->memory_offset
- s
->note_size
;
230 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
231 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
232 phdr
.p_offset
= cpu_to_dump64(s
, begin
);
234 phdr
.p_filesz
= cpu_to_dump64(s
, s
->note_size
);
235 phdr
.p_memsz
= cpu_to_dump64(s
, s
->note_size
);
238 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
240 error_setg(errp
, "dump: failed to write program header table");
244 static inline int cpu_index(CPUState
*cpu
)
246 return cpu
->cpu_index
+ 1;
249 static void write_guest_note(WriteCoreDumpFunction f
, DumpState
*s
,
255 ret
= f(s
->guest_note
, s
->guest_note_size
, s
);
257 error_setg(errp
, "dump: failed to write guest note");
262 static void write_elf64_notes(WriteCoreDumpFunction f
, DumpState
*s
,
271 ret
= cpu_write_elf64_note(f
, cpu
, id
, s
);
273 error_setg(errp
, "dump: failed to write elf notes");
279 ret
= cpu_write_elf64_qemunote(f
, cpu
, s
);
281 error_setg(errp
, "dump: failed to write CPU status");
286 write_guest_note(f
, s
, errp
);
289 static void write_elf32_note(DumpState
*s
, Error
**errp
)
291 hwaddr begin
= s
->memory_offset
- s
->note_size
;
295 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
296 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
297 phdr
.p_offset
= cpu_to_dump32(s
, begin
);
299 phdr
.p_filesz
= cpu_to_dump32(s
, s
->note_size
);
300 phdr
.p_memsz
= cpu_to_dump32(s
, s
->note_size
);
303 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
305 error_setg(errp
, "dump: failed to write program header table");
309 static void write_elf32_notes(WriteCoreDumpFunction f
, DumpState
*s
,
318 ret
= cpu_write_elf32_note(f
, cpu
, id
, s
);
320 error_setg(errp
, "dump: failed to write elf notes");
326 ret
= cpu_write_elf32_qemunote(f
, cpu
, s
);
328 error_setg(errp
, "dump: failed to write CPU status");
333 write_guest_note(f
, s
, errp
);
336 static void write_elf_section(DumpState
*s
, int type
, Error
**errp
)
345 shdr_size
= sizeof(Elf32_Shdr
);
346 memset(&shdr32
, 0, shdr_size
);
347 shdr32
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
350 shdr_size
= sizeof(Elf64_Shdr
);
351 memset(&shdr64
, 0, shdr_size
);
352 shdr64
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
356 ret
= fd_write_vmcore(&shdr
, shdr_size
, s
);
358 error_setg(errp
, "dump: failed to write section header table");
362 static void write_data(DumpState
*s
, void *buf
, int length
, Error
**errp
)
366 ret
= fd_write_vmcore(buf
, length
, s
);
368 error_setg(errp
, "dump: failed to save memory");
370 s
->written_size
+= length
;
374 /* write the memory to vmcore. 1 page per I/O. */
375 static void write_memory(DumpState
*s
, GuestPhysBlock
*block
, ram_addr_t start
,
376 int64_t size
, Error
**errp
)
379 Error
*local_err
= NULL
;
381 for (i
= 0; i
< size
/ s
->dump_info
.page_size
; i
++) {
382 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
383 s
->dump_info
.page_size
, &local_err
);
385 error_propagate(errp
, local_err
);
390 if ((size
% s
->dump_info
.page_size
) != 0) {
391 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
392 size
% s
->dump_info
.page_size
, &local_err
);
394 error_propagate(errp
, local_err
);
400 /* get the memory's offset and size in the vmcore */
401 static void get_offset_range(hwaddr phys_addr
,
402 ram_addr_t mapping_length
,
407 GuestPhysBlock
*block
;
408 hwaddr offset
= s
->memory_offset
;
409 int64_t size_in_block
, start
;
411 /* When the memory is not stored into vmcore, offset will be -1 */
416 if (phys_addr
< s
->begin
|| phys_addr
>= s
->begin
+ s
->length
) {
421 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
423 if (block
->target_start
>= s
->begin
+ s
->length
||
424 block
->target_end
<= s
->begin
) {
425 /* This block is out of the range */
429 if (s
->begin
<= block
->target_start
) {
430 start
= block
->target_start
;
435 size_in_block
= block
->target_end
- start
;
436 if (s
->begin
+ s
->length
< block
->target_end
) {
437 size_in_block
-= block
->target_end
- (s
->begin
+ s
->length
);
440 start
= block
->target_start
;
441 size_in_block
= block
->target_end
- block
->target_start
;
444 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
445 *p_offset
= phys_addr
- start
+ offset
;
447 /* The offset range mapped from the vmcore file must not spill over
448 * the GuestPhysBlock, clamp it. The rest of the mapping will be
449 * zero-filled in memory at load time; see
450 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
452 *p_filesz
= phys_addr
+ mapping_length
<= start
+ size_in_block
?
454 size_in_block
- (phys_addr
- start
);
458 offset
+= size_in_block
;
462 static void write_elf_loads(DumpState
*s
, Error
**errp
)
464 hwaddr offset
, filesz
;
465 MemoryMapping
*memory_mapping
;
466 uint32_t phdr_index
= 1;
468 Error
*local_err
= NULL
;
470 if (s
->have_section
) {
471 max_index
= s
->sh_info
;
473 max_index
= s
->phdr_num
;
476 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
477 get_offset_range(memory_mapping
->phys_addr
,
478 memory_mapping
->length
,
479 s
, &offset
, &filesz
);
480 if (s
->dump_info
.d_class
== ELFCLASS64
) {
481 write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
,
484 write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
,
489 error_propagate(errp
, local_err
);
493 if (phdr_index
>= max_index
) {
499 /* write elf header, PT_NOTE and elf note to vmcore. */
500 static void dump_begin(DumpState
*s
, Error
**errp
)
502 Error
*local_err
= NULL
;
505 * the vmcore's format is:
524 * we only know where the memory is saved after we write elf note into
528 /* write elf header to vmcore */
529 if (s
->dump_info
.d_class
== ELFCLASS64
) {
530 write_elf64_header(s
, &local_err
);
532 write_elf32_header(s
, &local_err
);
535 error_propagate(errp
, local_err
);
539 if (s
->dump_info
.d_class
== ELFCLASS64
) {
540 /* write PT_NOTE to vmcore */
541 write_elf64_note(s
, &local_err
);
543 error_propagate(errp
, local_err
);
547 /* write all PT_LOAD to vmcore */
548 write_elf_loads(s
, &local_err
);
550 error_propagate(errp
, local_err
);
554 /* write section to vmcore */
555 if (s
->have_section
) {
556 write_elf_section(s
, 1, &local_err
);
558 error_propagate(errp
, local_err
);
563 /* write notes to vmcore */
564 write_elf64_notes(fd_write_vmcore
, s
, &local_err
);
566 error_propagate(errp
, local_err
);
570 /* write PT_NOTE to vmcore */
571 write_elf32_note(s
, &local_err
);
573 error_propagate(errp
, local_err
);
577 /* write all PT_LOAD to vmcore */
578 write_elf_loads(s
, &local_err
);
580 error_propagate(errp
, local_err
);
584 /* write section to vmcore */
585 if (s
->have_section
) {
586 write_elf_section(s
, 0, &local_err
);
588 error_propagate(errp
, local_err
);
593 /* write notes to vmcore */
594 write_elf32_notes(fd_write_vmcore
, s
, &local_err
);
596 error_propagate(errp
, local_err
);
602 static int get_next_block(DumpState
*s
, GuestPhysBlock
*block
)
605 block
= QTAILQ_NEXT(block
, next
);
612 s
->next_block
= block
;
614 if (block
->target_start
>= s
->begin
+ s
->length
||
615 block
->target_end
<= s
->begin
) {
616 /* This block is out of the range */
620 if (s
->begin
> block
->target_start
) {
621 s
->start
= s
->begin
- block
->target_start
;
629 /* write all memory to vmcore */
630 static void dump_iterate(DumpState
*s
, Error
**errp
)
632 GuestPhysBlock
*block
;
634 Error
*local_err
= NULL
;
637 block
= s
->next_block
;
639 size
= block
->target_end
- block
->target_start
;
642 if (s
->begin
+ s
->length
< block
->target_end
) {
643 size
-= block
->target_end
- (s
->begin
+ s
->length
);
646 write_memory(s
, block
, s
->start
, size
, &local_err
);
648 error_propagate(errp
, local_err
);
652 } while (!get_next_block(s
, block
));
655 static void create_vmcore(DumpState
*s
, Error
**errp
)
657 Error
*local_err
= NULL
;
659 dump_begin(s
, &local_err
);
661 error_propagate(errp
, local_err
);
665 dump_iterate(s
, errp
);
668 static int write_start_flat_header(int fd
)
670 MakedumpfileHeader
*mh
;
673 QEMU_BUILD_BUG_ON(sizeof *mh
> MAX_SIZE_MDF_HEADER
);
674 mh
= g_malloc0(MAX_SIZE_MDF_HEADER
);
676 memcpy(mh
->signature
, MAKEDUMPFILE_SIGNATURE
,
677 MIN(sizeof mh
->signature
, sizeof MAKEDUMPFILE_SIGNATURE
));
679 mh
->type
= cpu_to_be64(TYPE_FLAT_HEADER
);
680 mh
->version
= cpu_to_be64(VERSION_FLAT_HEADER
);
683 written_size
= qemu_write_full(fd
, mh
, MAX_SIZE_MDF_HEADER
);
684 if (written_size
!= MAX_SIZE_MDF_HEADER
) {
692 static int write_end_flat_header(int fd
)
694 MakedumpfileDataHeader mdh
;
696 mdh
.offset
= END_FLAG_FLAT_HEADER
;
697 mdh
.buf_size
= END_FLAG_FLAT_HEADER
;
700 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
701 if (written_size
!= sizeof(mdh
)) {
708 static int write_buffer(int fd
, off_t offset
, const void *buf
, size_t size
)
711 MakedumpfileDataHeader mdh
;
713 mdh
.offset
= cpu_to_be64(offset
);
714 mdh
.buf_size
= cpu_to_be64(size
);
716 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
717 if (written_size
!= sizeof(mdh
)) {
721 written_size
= qemu_write_full(fd
, buf
, size
);
722 if (written_size
!= size
) {
729 static int buf_write_note(const void *buf
, size_t size
, void *opaque
)
731 DumpState
*s
= opaque
;
733 /* note_buf is not enough */
734 if (s
->note_buf_offset
+ size
> s
->note_size
) {
738 memcpy(s
->note_buf
+ s
->note_buf_offset
, buf
, size
);
740 s
->note_buf_offset
+= size
;
746 * This function retrieves various sizes from an elf header.
748 * @note has to be a valid ELF note. The return sizes are unmodified
749 * (not padded or rounded up to be multiple of 4).
751 static void get_note_sizes(DumpState
*s
, const void *note
,
752 uint64_t *note_head_size
,
756 uint64_t note_head_sz
;
760 if (s
->dump_info
.d_class
== ELFCLASS64
) {
761 const Elf64_Nhdr
*hdr
= note
;
762 note_head_sz
= sizeof(Elf64_Nhdr
);
763 name_sz
= tswap64(hdr
->n_namesz
);
764 desc_sz
= tswap64(hdr
->n_descsz
);
766 const Elf32_Nhdr
*hdr
= note
;
767 note_head_sz
= sizeof(Elf32_Nhdr
);
768 name_sz
= tswap32(hdr
->n_namesz
);
769 desc_sz
= tswap32(hdr
->n_descsz
);
772 if (note_head_size
) {
773 *note_head_size
= note_head_sz
;
776 *name_size
= name_sz
;
779 *desc_size
= desc_sz
;
783 static bool note_name_equal(DumpState
*s
,
784 const uint8_t *note
, const char *name
)
786 int len
= strlen(name
) + 1;
787 uint64_t head_size
, name_size
;
789 get_note_sizes(s
, note
, &head_size
, &name_size
, NULL
);
790 head_size
= ROUND_UP(head_size
, 4);
792 return name_size
== len
&& memcmp(note
+ head_size
, name
, len
) == 0;
795 /* write common header, sub header and elf note to vmcore */
796 static void create_header32(DumpState
*s
, Error
**errp
)
798 DiskDumpHeader32
*dh
= NULL
;
799 KdumpSubHeader32
*kh
= NULL
;
802 uint32_t sub_hdr_size
;
803 uint32_t bitmap_blocks
;
805 uint64_t offset_note
;
806 Error
*local_err
= NULL
;
808 /* write common header, the version of kdump-compressed format is 6th */
809 size
= sizeof(DiskDumpHeader32
);
810 dh
= g_malloc0(size
);
812 strncpy(dh
->signature
, KDUMP_SIGNATURE
, strlen(KDUMP_SIGNATURE
));
813 dh
->header_version
= cpu_to_dump32(s
, 6);
814 block_size
= s
->dump_info
.page_size
;
815 dh
->block_size
= cpu_to_dump32(s
, block_size
);
816 sub_hdr_size
= sizeof(struct KdumpSubHeader32
) + s
->note_size
;
817 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
818 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
819 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
820 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
821 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
822 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
823 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
824 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
826 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
827 status
|= DUMP_DH_COMPRESSED_ZLIB
;
830 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
831 status
|= DUMP_DH_COMPRESSED_LZO
;
835 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
836 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
839 dh
->status
= cpu_to_dump32(s
, status
);
841 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
842 error_setg(errp
, "dump: failed to write disk dump header");
846 /* write sub header */
847 size
= sizeof(KdumpSubHeader32
);
848 kh
= g_malloc0(size
);
850 /* 64bit max_mapnr_64 */
851 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
852 kh
->phys_base
= cpu_to_dump32(s
, s
->dump_info
.phys_base
);
853 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
855 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
857 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
858 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
860 get_note_sizes(s
, s
->guest_note
,
861 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
862 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
863 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
864 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
865 kh
->size_vmcoreinfo
= cpu_to_dump32(s
, size_vmcoreinfo_desc
);
868 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
869 kh
->note_size
= cpu_to_dump32(s
, s
->note_size
);
871 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
872 block_size
, kh
, size
) < 0) {
873 error_setg(errp
, "dump: failed to write kdump sub header");
878 s
->note_buf
= g_malloc0(s
->note_size
);
879 s
->note_buf_offset
= 0;
881 /* use s->note_buf to store notes temporarily */
882 write_elf32_notes(buf_write_note
, s
, &local_err
);
884 error_propagate(errp
, local_err
);
887 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
889 error_setg(errp
, "dump: failed to write notes");
893 /* get offset of dump_bitmap */
894 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
897 /* get offset of page */
898 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
907 /* write common header, sub header and elf note to vmcore */
908 static void create_header64(DumpState
*s
, Error
**errp
)
910 DiskDumpHeader64
*dh
= NULL
;
911 KdumpSubHeader64
*kh
= NULL
;
914 uint32_t sub_hdr_size
;
915 uint32_t bitmap_blocks
;
917 uint64_t offset_note
;
918 Error
*local_err
= NULL
;
920 /* write common header, the version of kdump-compressed format is 6th */
921 size
= sizeof(DiskDumpHeader64
);
922 dh
= g_malloc0(size
);
924 strncpy(dh
->signature
, KDUMP_SIGNATURE
, strlen(KDUMP_SIGNATURE
));
925 dh
->header_version
= cpu_to_dump32(s
, 6);
926 block_size
= s
->dump_info
.page_size
;
927 dh
->block_size
= cpu_to_dump32(s
, block_size
);
928 sub_hdr_size
= sizeof(struct KdumpSubHeader64
) + s
->note_size
;
929 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
930 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
931 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
932 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
933 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
934 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
935 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
936 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
938 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
939 status
|= DUMP_DH_COMPRESSED_ZLIB
;
942 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
943 status
|= DUMP_DH_COMPRESSED_LZO
;
947 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
948 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
951 dh
->status
= cpu_to_dump32(s
, status
);
953 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
954 error_setg(errp
, "dump: failed to write disk dump header");
958 /* write sub header */
959 size
= sizeof(KdumpSubHeader64
);
960 kh
= g_malloc0(size
);
962 /* 64bit max_mapnr_64 */
963 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
964 kh
->phys_base
= cpu_to_dump64(s
, s
->dump_info
.phys_base
);
965 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
967 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
969 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
970 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
972 get_note_sizes(s
, s
->guest_note
,
973 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
974 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
975 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
976 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
977 kh
->size_vmcoreinfo
= cpu_to_dump64(s
, size_vmcoreinfo_desc
);
980 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
981 kh
->note_size
= cpu_to_dump64(s
, s
->note_size
);
983 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
984 block_size
, kh
, size
) < 0) {
985 error_setg(errp
, "dump: failed to write kdump sub header");
990 s
->note_buf
= g_malloc0(s
->note_size
);
991 s
->note_buf_offset
= 0;
993 /* use s->note_buf to store notes temporarily */
994 write_elf64_notes(buf_write_note
, s
, &local_err
);
996 error_propagate(errp
, local_err
);
1000 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
1001 s
->note_size
) < 0) {
1002 error_setg(errp
, "dump: failed to write notes");
1006 /* get offset of dump_bitmap */
1007 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
1010 /* get offset of page */
1011 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
1017 g_free(s
->note_buf
);
1020 static void write_dump_header(DumpState
*s
, Error
**errp
)
1022 Error
*local_err
= NULL
;
1024 if (s
->dump_info
.d_class
== ELFCLASS32
) {
1025 create_header32(s
, &local_err
);
1027 create_header64(s
, &local_err
);
1029 error_propagate(errp
, local_err
);
1032 static size_t dump_bitmap_get_bufsize(DumpState
*s
)
1034 return s
->dump_info
.page_size
;
1038 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1039 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1040 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1041 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1042 * vmcore, ie. synchronizing un-sync bit into vmcore.
1044 static int set_dump_bitmap(uint64_t last_pfn
, uint64_t pfn
, bool value
,
1045 uint8_t *buf
, DumpState
*s
)
1047 off_t old_offset
, new_offset
;
1048 off_t offset_bitmap1
, offset_bitmap2
;
1050 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1051 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1053 /* should not set the previous place */
1054 assert(last_pfn
<= pfn
);
1057 * if the bit needed to be set is not cached in buf, flush the data in buf
1058 * to vmcore firstly.
1059 * making new_offset be bigger than old_offset can also sync remained data
1062 old_offset
= bitmap_bufsize
* (last_pfn
/ bits_per_buf
);
1063 new_offset
= bitmap_bufsize
* (pfn
/ bits_per_buf
);
1065 while (old_offset
< new_offset
) {
1066 /* calculate the offset and write dump_bitmap */
1067 offset_bitmap1
= s
->offset_dump_bitmap
+ old_offset
;
1068 if (write_buffer(s
->fd
, offset_bitmap1
, buf
,
1069 bitmap_bufsize
) < 0) {
1073 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1074 offset_bitmap2
= s
->offset_dump_bitmap
+ s
->len_dump_bitmap
+
1076 if (write_buffer(s
->fd
, offset_bitmap2
, buf
,
1077 bitmap_bufsize
) < 0) {
1081 memset(buf
, 0, bitmap_bufsize
);
1082 old_offset
+= bitmap_bufsize
;
1085 /* get the exact place of the bit in the buf, and set it */
1086 byte
= (pfn
% bits_per_buf
) / CHAR_BIT
;
1087 bit
= (pfn
% bits_per_buf
) % CHAR_BIT
;
1089 buf
[byte
] |= 1u << bit
;
1091 buf
[byte
] &= ~(1u << bit
);
1097 static uint64_t dump_paddr_to_pfn(DumpState
*s
, uint64_t addr
)
1099 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1101 return (addr
>> target_page_shift
) - ARCH_PFN_OFFSET
;
1104 static uint64_t dump_pfn_to_paddr(DumpState
*s
, uint64_t pfn
)
1106 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1108 return (pfn
+ ARCH_PFN_OFFSET
) << target_page_shift
;
1112 * exam every page and return the page frame number and the address of the page.
1113 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1114 * blocks, so block->target_start and block->target_end should be interal
1115 * multiples of the target page size.
1117 static bool get_next_page(GuestPhysBlock
**blockptr
, uint64_t *pfnptr
,
1118 uint8_t **bufptr
, DumpState
*s
)
1120 GuestPhysBlock
*block
= *blockptr
;
1121 hwaddr addr
, target_page_mask
= ~((hwaddr
)s
->dump_info
.page_size
- 1);
1124 /* block == NULL means the start of the iteration */
1126 block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1128 assert((block
->target_start
& ~target_page_mask
) == 0);
1129 assert((block
->target_end
& ~target_page_mask
) == 0);
1130 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1132 *bufptr
= block
->host_addr
;
1137 *pfnptr
= *pfnptr
+ 1;
1138 addr
= dump_pfn_to_paddr(s
, *pfnptr
);
1140 if ((addr
>= block
->target_start
) &&
1141 (addr
+ s
->dump_info
.page_size
<= block
->target_end
)) {
1142 buf
= block
->host_addr
+ (addr
- block
->target_start
);
1144 /* the next page is in the next block */
1145 block
= QTAILQ_NEXT(block
, next
);
1150 assert((block
->target_start
& ~target_page_mask
) == 0);
1151 assert((block
->target_end
& ~target_page_mask
) == 0);
1152 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1153 buf
= block
->host_addr
;
1163 static void write_dump_bitmap(DumpState
*s
, Error
**errp
)
1166 uint64_t last_pfn
, pfn
;
1167 void *dump_bitmap_buf
;
1168 size_t num_dumpable
;
1169 GuestPhysBlock
*block_iter
= NULL
;
1170 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1171 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1173 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1174 dump_bitmap_buf
= g_malloc0(bitmap_bufsize
);
1180 * exam memory page by page, and set the bit in dump_bitmap corresponded
1181 * to the existing page.
1183 while (get_next_page(&block_iter
, &pfn
, NULL
, s
)) {
1184 ret
= set_dump_bitmap(last_pfn
, pfn
, true, dump_bitmap_buf
, s
);
1186 error_setg(errp
, "dump: failed to set dump_bitmap");
1195 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1196 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1197 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1199 if (num_dumpable
> 0) {
1200 ret
= set_dump_bitmap(last_pfn
, last_pfn
+ bits_per_buf
, false,
1201 dump_bitmap_buf
, s
);
1203 error_setg(errp
, "dump: failed to sync dump_bitmap");
1208 /* number of dumpable pages that will be dumped later */
1209 s
->num_dumpable
= num_dumpable
;
1212 g_free(dump_bitmap_buf
);
1215 static void prepare_data_cache(DataCache
*data_cache
, DumpState
*s
,
1218 data_cache
->fd
= s
->fd
;
1219 data_cache
->data_size
= 0;
1220 data_cache
->buf_size
= 4 * dump_bitmap_get_bufsize(s
);
1221 data_cache
->buf
= g_malloc0(data_cache
->buf_size
);
1222 data_cache
->offset
= offset
;
1225 static int write_cache(DataCache
*dc
, const void *buf
, size_t size
,
1229 * dc->buf_size should not be less than size, otherwise dc will never be
1232 assert(size
<= dc
->buf_size
);
1235 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1236 * otherwise check if the space is enough for caching data in buf, if not,
1237 * write the data in dc->buf to dc->fd and reset dc->buf
1239 if ((!flag_sync
&& dc
->data_size
+ size
> dc
->buf_size
) ||
1240 (flag_sync
&& dc
->data_size
> 0)) {
1241 if (write_buffer(dc
->fd
, dc
->offset
, dc
->buf
, dc
->data_size
) < 0) {
1245 dc
->offset
+= dc
->data_size
;
1250 memcpy(dc
->buf
+ dc
->data_size
, buf
, size
);
1251 dc
->data_size
+= size
;
1257 static void free_data_cache(DataCache
*data_cache
)
1259 g_free(data_cache
->buf
);
1262 static size_t get_len_buf_out(size_t page_size
, uint32_t flag_compress
)
1264 switch (flag_compress
) {
1265 case DUMP_DH_COMPRESSED_ZLIB
:
1266 return compressBound(page_size
);
1268 case DUMP_DH_COMPRESSED_LZO
:
1270 * LZO will expand incompressible data by a little amount. Please check
1271 * the following URL to see the expansion calculation:
1272 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1274 return page_size
+ page_size
/ 16 + 64 + 3;
1276 #ifdef CONFIG_SNAPPY
1277 case DUMP_DH_COMPRESSED_SNAPPY
:
1278 return snappy_max_compressed_length(page_size
);
1285 * check if the page is all 0
1287 static inline bool is_zero_page(const uint8_t *buf
, size_t page_size
)
1289 return buffer_is_zero(buf
, page_size
);
1292 static void write_dump_pages(DumpState
*s
, Error
**errp
)
1295 DataCache page_desc
, page_data
;
1296 size_t len_buf_out
, size_out
;
1298 lzo_bytep wrkmem
= NULL
;
1300 uint8_t *buf_out
= NULL
;
1301 off_t offset_desc
, offset_data
;
1302 PageDescriptor pd
, pd_zero
;
1304 GuestPhysBlock
*block_iter
= NULL
;
1307 /* get offset of page_desc and page_data in dump file */
1308 offset_desc
= s
->offset_page
;
1309 offset_data
= offset_desc
+ sizeof(PageDescriptor
) * s
->num_dumpable
;
1311 prepare_data_cache(&page_desc
, s
, offset_desc
);
1312 prepare_data_cache(&page_data
, s
, offset_data
);
1314 /* prepare buffer to store compressed data */
1315 len_buf_out
= get_len_buf_out(s
->dump_info
.page_size
, s
->flag_compress
);
1316 assert(len_buf_out
!= 0);
1319 wrkmem
= g_malloc(LZO1X_1_MEM_COMPRESS
);
1322 buf_out
= g_malloc(len_buf_out
);
1325 * init zero page's page_desc and page_data, because every zero page
1326 * uses the same page_data
1328 pd_zero
.size
= cpu_to_dump32(s
, s
->dump_info
.page_size
);
1329 pd_zero
.flags
= cpu_to_dump32(s
, 0);
1330 pd_zero
.offset
= cpu_to_dump64(s
, offset_data
);
1331 pd_zero
.page_flags
= cpu_to_dump64(s
, 0);
1332 buf
= g_malloc0(s
->dump_info
.page_size
);
1333 ret
= write_cache(&page_data
, buf
, s
->dump_info
.page_size
, false);
1336 error_setg(errp
, "dump: failed to write page data (zero page)");
1340 offset_data
+= s
->dump_info
.page_size
;
1343 * dump memory to vmcore page by page. zero page will all be resided in the
1344 * first page of page section
1346 while (get_next_page(&block_iter
, &pfn_iter
, &buf
, s
)) {
1347 /* check zero page */
1348 if (is_zero_page(buf
, s
->dump_info
.page_size
)) {
1349 ret
= write_cache(&page_desc
, &pd_zero
, sizeof(PageDescriptor
),
1352 error_setg(errp
, "dump: failed to write page desc");
1357 * not zero page, then:
1358 * 1. compress the page
1359 * 2. write the compressed page into the cache of page_data
1360 * 3. get page desc of the compressed page and write it into the
1361 * cache of page_desc
1363 * only one compression format will be used here, for
1364 * s->flag_compress is set. But when compression fails to work,
1365 * we fall back to save in plaintext.
1367 size_out
= len_buf_out
;
1368 if ((s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) &&
1369 (compress2(buf_out
, (uLongf
*)&size_out
, buf
,
1370 s
->dump_info
.page_size
, Z_BEST_SPEED
) == Z_OK
) &&
1371 (size_out
< s
->dump_info
.page_size
)) {
1372 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_ZLIB
);
1373 pd
.size
= cpu_to_dump32(s
, size_out
);
1375 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1377 error_setg(errp
, "dump: failed to write page data");
1381 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) &&
1382 (lzo1x_1_compress(buf
, s
->dump_info
.page_size
, buf_out
,
1383 (lzo_uint
*)&size_out
, wrkmem
) == LZO_E_OK
) &&
1384 (size_out
< s
->dump_info
.page_size
)) {
1385 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_LZO
);
1386 pd
.size
= cpu_to_dump32(s
, size_out
);
1388 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1390 error_setg(errp
, "dump: failed to write page data");
1394 #ifdef CONFIG_SNAPPY
1395 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) &&
1396 (snappy_compress((char *)buf
, s
->dump_info
.page_size
,
1397 (char *)buf_out
, &size_out
) == SNAPPY_OK
) &&
1398 (size_out
< s
->dump_info
.page_size
)) {
1399 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_SNAPPY
);
1400 pd
.size
= cpu_to_dump32(s
, size_out
);
1402 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1404 error_setg(errp
, "dump: failed to write page data");
1410 * fall back to save in plaintext, size_out should be
1411 * assigned the target's page size
1413 pd
.flags
= cpu_to_dump32(s
, 0);
1414 size_out
= s
->dump_info
.page_size
;
1415 pd
.size
= cpu_to_dump32(s
, size_out
);
1417 ret
= write_cache(&page_data
, buf
,
1418 s
->dump_info
.page_size
, false);
1420 error_setg(errp
, "dump: failed to write page data");
1425 /* get and write page desc here */
1426 pd
.page_flags
= cpu_to_dump64(s
, 0);
1427 pd
.offset
= cpu_to_dump64(s
, offset_data
);
1428 offset_data
+= size_out
;
1430 ret
= write_cache(&page_desc
, &pd
, sizeof(PageDescriptor
), false);
1432 error_setg(errp
, "dump: failed to write page desc");
1436 s
->written_size
+= s
->dump_info
.page_size
;
1439 ret
= write_cache(&page_desc
, NULL
, 0, true);
1441 error_setg(errp
, "dump: failed to sync cache for page_desc");
1444 ret
= write_cache(&page_data
, NULL
, 0, true);
1446 error_setg(errp
, "dump: failed to sync cache for page_data");
1451 free_data_cache(&page_desc
);
1452 free_data_cache(&page_data
);
1461 static void create_kdump_vmcore(DumpState
*s
, Error
**errp
)
1464 Error
*local_err
= NULL
;
1467 * the kdump-compressed format is:
1469 * +------------------------------------------+ 0x0
1470 * | main header (struct disk_dump_header) |
1471 * |------------------------------------------+ block 1
1472 * | sub header (struct kdump_sub_header) |
1473 * |------------------------------------------+ block 2
1474 * | 1st-dump_bitmap |
1475 * |------------------------------------------+ block 2 + X blocks
1476 * | 2nd-dump_bitmap | (aligned by block)
1477 * |------------------------------------------+ block 2 + 2 * X blocks
1478 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1479 * | page desc for pfn 1 (struct page_desc) |
1481 * |------------------------------------------| (not aligned by block)
1482 * | page data (pfn 0) |
1483 * | page data (pfn 1) |
1485 * +------------------------------------------+
1488 ret
= write_start_flat_header(s
->fd
);
1490 error_setg(errp
, "dump: failed to write start flat header");
1494 write_dump_header(s
, &local_err
);
1496 error_propagate(errp
, local_err
);
1500 write_dump_bitmap(s
, &local_err
);
1502 error_propagate(errp
, local_err
);
1506 write_dump_pages(s
, &local_err
);
1508 error_propagate(errp
, local_err
);
1512 ret
= write_end_flat_header(s
->fd
);
1514 error_setg(errp
, "dump: failed to write end flat header");
1519 static ram_addr_t
get_start_block(DumpState
*s
)
1521 GuestPhysBlock
*block
;
1523 if (!s
->has_filter
) {
1524 s
->next_block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1528 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1529 if (block
->target_start
>= s
->begin
+ s
->length
||
1530 block
->target_end
<= s
->begin
) {
1531 /* This block is out of the range */
1535 s
->next_block
= block
;
1536 if (s
->begin
> block
->target_start
) {
1537 s
->start
= s
->begin
- block
->target_start
;
1547 static void get_max_mapnr(DumpState
*s
)
1549 GuestPhysBlock
*last_block
;
1551 last_block
= QTAILQ_LAST(&s
->guest_phys_blocks
.head
, GuestPhysBlockHead
);
1552 s
->max_mapnr
= dump_paddr_to_pfn(s
, last_block
->target_end
);
1555 static DumpState dump_state_global
= { .status
= DUMP_STATUS_NONE
};
1557 static void dump_state_prepare(DumpState
*s
)
1559 /* zero the struct, setting status to active */
1560 *s
= (DumpState
) { .status
= DUMP_STATUS_ACTIVE
};
1563 bool dump_in_progress(void)
1565 DumpState
*state
= &dump_state_global
;
1566 return (atomic_read(&state
->status
) == DUMP_STATUS_ACTIVE
);
1569 /* calculate total size of memory to be dumped (taking filter into
1571 static int64_t dump_calculate_size(DumpState
*s
)
1573 GuestPhysBlock
*block
;
1574 int64_t size
= 0, total
= 0, left
= 0, right
= 0;
1576 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1577 if (s
->has_filter
) {
1578 /* calculate the overlapped region. */
1579 left
= MAX(s
->begin
, block
->target_start
);
1580 right
= MIN(s
->begin
+ s
->length
, block
->target_end
);
1581 size
= right
- left
;
1582 size
= size
> 0 ? size
: 0;
1584 /* count the whole region in */
1585 size
= (block
->target_end
- block
->target_start
);
1593 static void vmcoreinfo_update_phys_base(DumpState
*s
)
1595 uint64_t size
, note_head_size
, name_size
, phys_base
;
1600 if (!note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1604 get_note_sizes(s
, s
->guest_note
, ¬e_head_size
, &name_size
, &size
);
1605 note_head_size
= ROUND_UP(note_head_size
, 4);
1607 vmci
= s
->guest_note
+ note_head_size
+ ROUND_UP(name_size
, 4);
1608 *(vmci
+ size
) = '\0';
1610 lines
= g_strsplit((char *)vmci
, "\n", -1);
1611 for (i
= 0; lines
[i
]; i
++) {
1612 if (g_str_has_prefix(lines
[i
], "NUMBER(phys_base)=")) {
1613 if (qemu_strtou64(lines
[i
] + 18, NULL
, 16,
1615 warn_report("Failed to read NUMBER(phys_base)=");
1617 s
->dump_info
.phys_base
= phys_base
;
1626 static void dump_init(DumpState
*s
, int fd
, bool has_format
,
1627 DumpGuestMemoryFormat format
, bool paging
, bool has_filter
,
1628 int64_t begin
, int64_t length
, Error
**errp
)
1630 VMCoreInfoState
*vmci
= vmcoreinfo_find();
1636 s
->has_format
= has_format
;
1638 s
->written_size
= 0;
1640 /* kdump-compressed is conflict with paging and filter */
1641 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1642 assert(!paging
&& !has_filter
);
1645 if (runstate_is_running()) {
1646 vm_stop(RUN_STATE_SAVE_VM
);
1652 /* If we use KVM, we should synchronize the registers before we get dump
1653 * info or physmap info.
1655 cpu_synchronize_all_states();
1662 s
->has_filter
= has_filter
;
1666 memory_mapping_list_init(&s
->list
);
1668 guest_phys_blocks_init(&s
->guest_phys_blocks
);
1669 guest_phys_blocks_append(&s
->guest_phys_blocks
);
1670 s
->total_size
= dump_calculate_size(s
);
1671 #ifdef DEBUG_DUMP_GUEST_MEMORY
1672 fprintf(stderr
, "DUMP: total memory to dump: %lu\n", s
->total_size
);
1675 /* it does not make sense to dump non-existent memory */
1676 if (!s
->total_size
) {
1677 error_setg(errp
, "dump: no guest memory to dump");
1681 s
->start
= get_start_block(s
);
1682 if (s
->start
== -1) {
1683 error_setg(errp
, QERR_INVALID_PARAMETER
, "begin");
1687 /* get dump info: endian, class and architecture.
1688 * If the target architecture is not supported, cpu_get_dump_info() will
1691 ret
= cpu_get_dump_info(&s
->dump_info
, &s
->guest_phys_blocks
);
1693 error_setg(errp
, QERR_UNSUPPORTED
);
1697 if (!s
->dump_info
.page_size
) {
1698 s
->dump_info
.page_size
= TARGET_PAGE_SIZE
;
1701 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
1702 s
->dump_info
.d_machine
, nr_cpus
);
1703 if (s
->note_size
< 0) {
1704 error_setg(errp
, QERR_UNSUPPORTED
);
1709 * The goal of this block is to (a) update the previously guessed
1710 * phys_base, (b) copy the guest note out of the guest.
1711 * Failure to do so is not fatal for dumping.
1714 uint64_t addr
, note_head_size
, name_size
, desc_size
;
1718 note_head_size
= s
->dump_info
.d_class
== ELFCLASS32
?
1719 sizeof(Elf32_Nhdr
) : sizeof(Elf64_Nhdr
);
1721 format
= le16_to_cpu(vmci
->vmcoreinfo
.guest_format
);
1722 size
= le32_to_cpu(vmci
->vmcoreinfo
.size
);
1723 addr
= le64_to_cpu(vmci
->vmcoreinfo
.paddr
);
1724 if (!vmci
->has_vmcoreinfo
) {
1725 warn_report("guest note is not present");
1726 } else if (size
< note_head_size
|| size
> MAX_GUEST_NOTE_SIZE
) {
1727 warn_report("guest note size is invalid: %" PRIu32
, size
);
1728 } else if (format
!= VMCOREINFO_FORMAT_ELF
) {
1729 warn_report("guest note format is unsupported: %" PRIu16
, format
);
1731 s
->guest_note
= g_malloc(size
+ 1); /* +1 for adding \0 */
1732 cpu_physical_memory_read(addr
, s
->guest_note
, size
);
1734 get_note_sizes(s
, s
->guest_note
, NULL
, &name_size
, &desc_size
);
1735 s
->guest_note_size
= ELF_NOTE_SIZE(note_head_size
, name_size
,
1737 if (name_size
> MAX_GUEST_NOTE_SIZE
||
1738 desc_size
> MAX_GUEST_NOTE_SIZE
||
1739 s
->guest_note_size
> size
) {
1740 warn_report("Invalid guest note header");
1741 g_free(s
->guest_note
);
1742 s
->guest_note
= NULL
;
1744 vmcoreinfo_update_phys_base(s
);
1745 s
->note_size
+= s
->guest_note_size
;
1750 /* get memory mapping */
1752 qemu_get_guest_memory_mapping(&s
->list
, &s
->guest_phys_blocks
, &err
);
1754 error_propagate(errp
, err
);
1758 qemu_get_guest_simple_memory_mapping(&s
->list
, &s
->guest_phys_blocks
);
1761 s
->nr_cpus
= nr_cpus
;
1766 tmp
= DIV_ROUND_UP(DIV_ROUND_UP(s
->max_mapnr
, CHAR_BIT
),
1767 s
->dump_info
.page_size
);
1768 s
->len_dump_bitmap
= tmp
* s
->dump_info
.page_size
;
1770 /* init for kdump-compressed format */
1771 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1773 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
:
1774 s
->flag_compress
= DUMP_DH_COMPRESSED_ZLIB
;
1777 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
:
1779 if (lzo_init() != LZO_E_OK
) {
1780 error_setg(errp
, "failed to initialize the LZO library");
1784 s
->flag_compress
= DUMP_DH_COMPRESSED_LZO
;
1787 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
:
1788 s
->flag_compress
= DUMP_DH_COMPRESSED_SNAPPY
;
1792 s
->flag_compress
= 0;
1798 if (s
->has_filter
) {
1799 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
1803 * calculate phdr_num
1805 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1807 s
->phdr_num
= 1; /* PT_NOTE */
1808 if (s
->list
.num
< UINT16_MAX
- 2) {
1809 s
->phdr_num
+= s
->list
.num
;
1810 s
->have_section
= false;
1812 s
->have_section
= true;
1813 s
->phdr_num
= PN_XNUM
;
1814 s
->sh_info
= 1; /* PT_NOTE */
1816 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1817 if (s
->list
.num
<= UINT32_MAX
- 1) {
1818 s
->sh_info
+= s
->list
.num
;
1820 s
->sh_info
= UINT32_MAX
;
1824 if (s
->dump_info
.d_class
== ELFCLASS64
) {
1825 if (s
->have_section
) {
1826 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1827 sizeof(Elf64_Phdr
) * s
->sh_info
+
1828 sizeof(Elf64_Shdr
) + s
->note_size
;
1830 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1831 sizeof(Elf64_Phdr
) * s
->phdr_num
+ s
->note_size
;
1834 if (s
->have_section
) {
1835 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1836 sizeof(Elf32_Phdr
) * s
->sh_info
+
1837 sizeof(Elf32_Shdr
) + s
->note_size
;
1839 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1840 sizeof(Elf32_Phdr
) * s
->phdr_num
+ s
->note_size
;
1850 /* this operation might be time consuming. */
1851 static void dump_process(DumpState
*s
, Error
**errp
)
1853 Error
*local_err
= NULL
;
1854 DumpQueryResult
*result
= NULL
;
1856 if (s
->has_format
&& s
->format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1857 create_kdump_vmcore(s
, &local_err
);
1859 create_vmcore(s
, &local_err
);
1862 /* make sure status is written after written_size updates */
1864 atomic_set(&s
->status
,
1865 (local_err
? DUMP_STATUS_FAILED
: DUMP_STATUS_COMPLETED
));
1867 /* send DUMP_COMPLETED message (unconditionally) */
1868 result
= qmp_query_dump(NULL
);
1869 /* should never fail */
1871 qapi_event_send_dump_completed(result
, !!local_err
, (local_err
? \
1872 error_get_pretty(local_err
) : NULL
),
1874 qapi_free_DumpQueryResult(result
);
1876 error_propagate(errp
, local_err
);
1880 static void *dump_thread(void *data
)
1882 DumpState
*s
= (DumpState
*)data
;
1883 dump_process(s
, NULL
);
1887 DumpQueryResult
*qmp_query_dump(Error
**errp
)
1889 DumpQueryResult
*result
= g_new(DumpQueryResult
, 1);
1890 DumpState
*state
= &dump_state_global
;
1891 result
->status
= atomic_read(&state
->status
);
1892 /* make sure we are reading status and written_size in order */
1894 result
->completed
= state
->written_size
;
1895 result
->total
= state
->total_size
;
1899 void qmp_dump_guest_memory(bool paging
, const char *file
,
1900 bool has_detach
, bool detach
,
1901 bool has_begin
, int64_t begin
, bool has_length
,
1902 int64_t length
, bool has_format
,
1903 DumpGuestMemoryFormat format
, Error
**errp
)
1908 Error
*local_err
= NULL
;
1909 bool detach_p
= false;
1911 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1912 error_setg(errp
, "Dump not allowed during incoming migration.");
1916 /* if there is a dump in background, we should wait until the dump
1918 if (dump_in_progress()) {
1919 error_setg(errp
, "There is a dump in process, please wait.");
1924 * kdump-compressed format need the whole memory dumped, so paging or
1925 * filter is not supported here.
1927 if ((has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) &&
1928 (paging
|| has_begin
|| has_length
)) {
1929 error_setg(errp
, "kdump-compressed format doesn't support paging or "
1933 if (has_begin
&& !has_length
) {
1934 error_setg(errp
, QERR_MISSING_PARAMETER
, "length");
1937 if (!has_begin
&& has_length
) {
1938 error_setg(errp
, QERR_MISSING_PARAMETER
, "begin");
1945 /* check whether lzo/snappy is supported */
1947 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
) {
1948 error_setg(errp
, "kdump-lzo is not available now");
1953 #ifndef CONFIG_SNAPPY
1954 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
) {
1955 error_setg(errp
, "kdump-snappy is not available now");
1961 if (strstart(file
, "fd:", &p
)) {
1962 fd
= monitor_get_fd(cur_mon
, p
, errp
);
1969 if (strstart(file
, "file:", &p
)) {
1970 fd
= qemu_open(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
1972 error_setg_file_open(errp
, errno
, p
);
1978 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
1982 s
= &dump_state_global
;
1983 dump_state_prepare(s
);
1985 dump_init(s
, fd
, has_format
, format
, paging
, has_begin
,
1986 begin
, length
, &local_err
);
1988 error_propagate(errp
, local_err
);
1989 atomic_set(&s
->status
, DUMP_STATUS_FAILED
);
1996 qemu_thread_create(&s
->dump_thread
, "dump_thread", dump_thread
,
1997 s
, QEMU_THREAD_DETACHED
);
2000 dump_process(s
, errp
);
2004 DumpGuestMemoryCapability
*qmp_query_dump_guest_memory_capability(Error
**errp
)
2006 DumpGuestMemoryFormatList
*item
;
2007 DumpGuestMemoryCapability
*cap
=
2008 g_malloc0(sizeof(DumpGuestMemoryCapability
));
2010 /* elf is always available */
2011 item
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
2012 cap
->formats
= item
;
2013 item
->value
= DUMP_GUEST_MEMORY_FORMAT_ELF
;
2015 /* kdump-zlib is always available */
2016 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
2018 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
;
2020 /* add new item if kdump-lzo is available */
2022 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
2024 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
;
2027 /* add new item if kdump-snappy is available */
2028 #ifdef CONFIG_SNAPPY
2029 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
2031 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
;