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 uint16_t cpu_to_dump16(DumpState
*s
, uint16_t val
)
64 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
65 val
= cpu_to_le16(val
);
67 val
= cpu_to_be16(val
);
73 uint32_t cpu_to_dump32(DumpState
*s
, uint32_t val
)
75 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
76 val
= cpu_to_le32(val
);
78 val
= cpu_to_be32(val
);
84 uint64_t cpu_to_dump64(DumpState
*s
, uint64_t val
)
86 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
87 val
= cpu_to_le64(val
);
89 val
= cpu_to_be64(val
);
95 static int dump_cleanup(DumpState
*s
)
97 guest_phys_blocks_free(&s
->guest_phys_blocks
);
98 memory_mapping_list_free(&s
->list
);
100 g_free(s
->guest_note
);
101 s
->guest_note
= NULL
;
104 qemu_mutex_lock_iothread();
108 qemu_mutex_unlock_iothread();
111 migrate_del_blocker(dump_migration_blocker
);
116 static int fd_write_vmcore(const void *buf
, size_t size
, void *opaque
)
118 DumpState
*s
= opaque
;
121 written_size
= qemu_write_full(s
->fd
, buf
, size
);
122 if (written_size
!= size
) {
129 static void write_elf64_header(DumpState
*s
, Error
**errp
)
132 * phnum in the elf header is 16 bit, if we have more segments we
133 * set phnum to PN_XNUM and write the real number of segments to a
136 uint16_t phnum
= MIN(s
->phdr_num
, PN_XNUM
);
137 Elf64_Ehdr elf_header
;
140 memset(&elf_header
, 0, sizeof(Elf64_Ehdr
));
141 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
142 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS64
;
143 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
144 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
145 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
146 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
147 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
148 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
149 elf_header
.e_phoff
= cpu_to_dump64(s
, s
->phdr_offset
);
150 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf64_Phdr
));
151 elf_header
.e_phnum
= cpu_to_dump16(s
, phnum
);
153 elf_header
.e_shoff
= cpu_to_dump64(s
, s
->shdr_offset
);
154 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf64_Shdr
));
155 elf_header
.e_shnum
= cpu_to_dump16(s
, s
->shdr_num
);
158 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
160 error_setg_errno(errp
, -ret
, "dump: failed to write elf header");
164 static void write_elf32_header(DumpState
*s
, Error
**errp
)
167 * phnum in the elf header is 16 bit, if we have more segments we
168 * set phnum to PN_XNUM and write the real number of segments to a
171 uint16_t phnum
= MIN(s
->phdr_num
, PN_XNUM
);
172 Elf32_Ehdr elf_header
;
175 memset(&elf_header
, 0, sizeof(Elf32_Ehdr
));
176 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
177 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS32
;
178 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
179 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
180 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
181 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
182 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
183 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
184 elf_header
.e_phoff
= cpu_to_dump32(s
, s
->phdr_offset
);
185 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf32_Phdr
));
186 elf_header
.e_phnum
= cpu_to_dump16(s
, phnum
);
188 elf_header
.e_shoff
= cpu_to_dump32(s
, s
->shdr_offset
);
189 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf32_Shdr
));
190 elf_header
.e_shnum
= cpu_to_dump16(s
, s
->shdr_num
);
193 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
195 error_setg_errno(errp
, -ret
, "dump: failed to write elf header");
199 static void write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
200 int phdr_index
, hwaddr offset
,
201 hwaddr filesz
, Error
**errp
)
206 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
207 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
208 phdr
.p_offset
= cpu_to_dump64(s
, offset
);
209 phdr
.p_paddr
= cpu_to_dump64(s
, memory_mapping
->phys_addr
);
210 phdr
.p_filesz
= cpu_to_dump64(s
, filesz
);
211 phdr
.p_memsz
= cpu_to_dump64(s
, memory_mapping
->length
);
212 phdr
.p_vaddr
= cpu_to_dump64(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
214 assert(memory_mapping
->length
>= filesz
);
216 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
218 error_setg_errno(errp
, -ret
,
219 "dump: failed to write program header table");
223 static void write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
224 int phdr_index
, hwaddr offset
,
225 hwaddr filesz
, Error
**errp
)
230 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
231 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
232 phdr
.p_offset
= cpu_to_dump32(s
, offset
);
233 phdr
.p_paddr
= cpu_to_dump32(s
, memory_mapping
->phys_addr
);
234 phdr
.p_filesz
= cpu_to_dump32(s
, filesz
);
235 phdr
.p_memsz
= cpu_to_dump32(s
, memory_mapping
->length
);
237 cpu_to_dump32(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
239 assert(memory_mapping
->length
>= filesz
);
241 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
243 error_setg_errno(errp
, -ret
,
244 "dump: failed to write program header table");
248 static void write_elf64_phdr_note(DumpState
*s
, Elf64_Phdr
*phdr
)
250 memset(phdr
, 0, sizeof(*phdr
));
251 phdr
->p_type
= cpu_to_dump32(s
, PT_NOTE
);
252 phdr
->p_offset
= cpu_to_dump64(s
, s
->note_offset
);
254 phdr
->p_filesz
= cpu_to_dump64(s
, s
->note_size
);
255 phdr
->p_memsz
= cpu_to_dump64(s
, s
->note_size
);
259 static inline int cpu_index(CPUState
*cpu
)
261 return cpu
->cpu_index
+ 1;
264 static void write_guest_note(WriteCoreDumpFunction f
, DumpState
*s
,
270 ret
= f(s
->guest_note
, s
->guest_note_size
, s
);
272 error_setg(errp
, "dump: failed to write guest note");
277 static void write_elf64_notes(WriteCoreDumpFunction f
, DumpState
*s
,
286 ret
= cpu_write_elf64_note(f
, cpu
, id
, s
);
288 error_setg(errp
, "dump: failed to write elf notes");
294 ret
= cpu_write_elf64_qemunote(f
, cpu
, s
);
296 error_setg(errp
, "dump: failed to write CPU status");
301 write_guest_note(f
, s
, errp
);
304 static void write_elf32_phdr_note(DumpState
*s
, Elf32_Phdr
*phdr
)
306 memset(phdr
, 0, sizeof(*phdr
));
307 phdr
->p_type
= cpu_to_dump32(s
, PT_NOTE
);
308 phdr
->p_offset
= cpu_to_dump32(s
, s
->note_offset
);
310 phdr
->p_filesz
= cpu_to_dump32(s
, s
->note_size
);
311 phdr
->p_memsz
= cpu_to_dump32(s
, s
->note_size
);
315 static void write_elf32_notes(WriteCoreDumpFunction f
, DumpState
*s
,
324 ret
= cpu_write_elf32_note(f
, cpu
, id
, s
);
326 error_setg(errp
, "dump: failed to write elf notes");
332 ret
= cpu_write_elf32_qemunote(f
, cpu
, s
);
334 error_setg(errp
, "dump: failed to write CPU status");
339 write_guest_note(f
, s
, errp
);
342 static void write_elf_phdr_note(DumpState
*s
, Error
**errp
)
351 if (dump_is_64bit(s
)) {
352 write_elf64_phdr_note(s
, &phdr64
);
353 size
= sizeof(phdr64
);
356 write_elf32_phdr_note(s
, &phdr32
);
357 size
= sizeof(phdr32
);
361 ret
= fd_write_vmcore(phdr
, size
, s
);
363 error_setg_errno(errp
, -ret
,
364 "dump: failed to write program header table");
368 static void write_elf_section(DumpState
*s
, int type
, Error
**errp
)
377 shdr_size
= sizeof(Elf32_Shdr
);
378 memset(&shdr32
, 0, shdr_size
);
379 shdr32
.sh_info
= cpu_to_dump32(s
, s
->phdr_num
);
382 shdr_size
= sizeof(Elf64_Shdr
);
383 memset(&shdr64
, 0, shdr_size
);
384 shdr64
.sh_info
= cpu_to_dump32(s
, s
->phdr_num
);
388 ret
= fd_write_vmcore(shdr
, shdr_size
, s
);
390 error_setg_errno(errp
, -ret
,
391 "dump: failed to write section header table");
395 static void write_data(DumpState
*s
, void *buf
, int length
, Error
**errp
)
399 ret
= fd_write_vmcore(buf
, length
, s
);
401 error_setg_errno(errp
, -ret
, "dump: failed to save memory");
403 s
->written_size
+= length
;
407 /* write the memory to vmcore. 1 page per I/O. */
408 static void write_memory(DumpState
*s
, GuestPhysBlock
*block
, ram_addr_t start
,
409 int64_t size
, Error
**errp
)
414 for (i
= 0; i
< size
/ s
->dump_info
.page_size
; i
++) {
415 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
416 s
->dump_info
.page_size
, errp
);
422 if ((size
% s
->dump_info
.page_size
) != 0) {
423 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
424 size
% s
->dump_info
.page_size
, errp
);
431 /* get the memory's offset and size in the vmcore */
432 static void get_offset_range(hwaddr phys_addr
,
433 ram_addr_t mapping_length
,
438 GuestPhysBlock
*block
;
439 hwaddr offset
= s
->memory_offset
;
440 int64_t size_in_block
, start
;
442 /* When the memory is not stored into vmcore, offset will be -1 */
447 if (phys_addr
< s
->begin
|| phys_addr
>= s
->begin
+ s
->length
) {
452 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
454 if (block
->target_start
>= s
->begin
+ s
->length
||
455 block
->target_end
<= s
->begin
) {
456 /* This block is out of the range */
460 if (s
->begin
<= block
->target_start
) {
461 start
= block
->target_start
;
466 size_in_block
= block
->target_end
- start
;
467 if (s
->begin
+ s
->length
< block
->target_end
) {
468 size_in_block
-= block
->target_end
- (s
->begin
+ s
->length
);
471 start
= block
->target_start
;
472 size_in_block
= block
->target_end
- block
->target_start
;
475 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
476 *p_offset
= phys_addr
- start
+ offset
;
478 /* The offset range mapped from the vmcore file must not spill over
479 * the GuestPhysBlock, clamp it. The rest of the mapping will be
480 * zero-filled in memory at load time; see
481 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
483 *p_filesz
= phys_addr
+ mapping_length
<= start
+ size_in_block
?
485 size_in_block
- (phys_addr
- start
);
489 offset
+= size_in_block
;
493 static void write_elf_loads(DumpState
*s
, Error
**errp
)
496 hwaddr offset
, filesz
;
497 MemoryMapping
*memory_mapping
;
498 uint32_t phdr_index
= 1;
500 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
501 get_offset_range(memory_mapping
->phys_addr
,
502 memory_mapping
->length
,
503 s
, &offset
, &filesz
);
504 if (dump_is_64bit(s
)) {
505 write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
,
508 write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
,
516 if (phdr_index
>= s
->phdr_num
) {
522 /* write elf header, PT_NOTE and elf note to vmcore. */
523 static void dump_begin(DumpState
*s
, Error
**errp
)
528 * the vmcore's format is:
547 * we only know where the memory is saved after we write elf note into
551 /* write elf header to vmcore */
552 if (dump_is_64bit(s
)) {
553 write_elf64_header(s
, errp
);
555 write_elf32_header(s
, errp
);
561 /* write PT_NOTE to vmcore */
562 write_elf_phdr_note(s
, errp
);
567 /* write all PT_LOAD to vmcore */
568 write_elf_loads(s
, errp
);
573 /* write section to vmcore */
575 write_elf_section(s
, 1, errp
);
581 if (dump_is_64bit(s
)) {
582 /* write notes to vmcore */
583 write_elf64_notes(fd_write_vmcore
, s
, errp
);
585 /* write notes to vmcore */
586 write_elf32_notes(fd_write_vmcore
, s
, errp
);
590 static int get_next_block(DumpState
*s
, GuestPhysBlock
*block
)
593 block
= QTAILQ_NEXT(block
, next
);
600 s
->next_block
= block
;
602 if (block
->target_start
>= s
->begin
+ s
->length
||
603 block
->target_end
<= s
->begin
) {
604 /* This block is out of the range */
608 if (s
->begin
> block
->target_start
) {
609 s
->start
= s
->begin
- block
->target_start
;
617 /* write all memory to vmcore */
618 static void dump_iterate(DumpState
*s
, Error
**errp
)
621 GuestPhysBlock
*block
;
625 block
= s
->next_block
;
627 size
= block
->target_end
- block
->target_start
;
630 if (s
->begin
+ s
->length
< block
->target_end
) {
631 size
-= block
->target_end
- (s
->begin
+ s
->length
);
634 write_memory(s
, block
, s
->start
, size
, errp
);
639 } while (!get_next_block(s
, block
));
642 static void create_vmcore(DumpState
*s
, Error
**errp
)
651 dump_iterate(s
, errp
);
654 static int write_start_flat_header(int fd
)
656 MakedumpfileHeader
*mh
;
659 QEMU_BUILD_BUG_ON(sizeof *mh
> MAX_SIZE_MDF_HEADER
);
660 mh
= g_malloc0(MAX_SIZE_MDF_HEADER
);
662 memcpy(mh
->signature
, MAKEDUMPFILE_SIGNATURE
,
663 MIN(sizeof mh
->signature
, sizeof MAKEDUMPFILE_SIGNATURE
));
665 mh
->type
= cpu_to_be64(TYPE_FLAT_HEADER
);
666 mh
->version
= cpu_to_be64(VERSION_FLAT_HEADER
);
669 written_size
= qemu_write_full(fd
, mh
, MAX_SIZE_MDF_HEADER
);
670 if (written_size
!= MAX_SIZE_MDF_HEADER
) {
678 static int write_end_flat_header(int fd
)
680 MakedumpfileDataHeader mdh
;
682 mdh
.offset
= END_FLAG_FLAT_HEADER
;
683 mdh
.buf_size
= END_FLAG_FLAT_HEADER
;
686 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
687 if (written_size
!= sizeof(mdh
)) {
694 static int write_buffer(int fd
, off_t offset
, const void *buf
, size_t size
)
697 MakedumpfileDataHeader mdh
;
699 mdh
.offset
= cpu_to_be64(offset
);
700 mdh
.buf_size
= cpu_to_be64(size
);
702 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
703 if (written_size
!= sizeof(mdh
)) {
707 written_size
= qemu_write_full(fd
, buf
, size
);
708 if (written_size
!= size
) {
715 static int buf_write_note(const void *buf
, size_t size
, void *opaque
)
717 DumpState
*s
= opaque
;
719 /* note_buf is not enough */
720 if (s
->note_buf_offset
+ size
> s
->note_size
) {
724 memcpy(s
->note_buf
+ s
->note_buf_offset
, buf
, size
);
726 s
->note_buf_offset
+= size
;
732 * This function retrieves various sizes from an elf header.
734 * @note has to be a valid ELF note. The return sizes are unmodified
735 * (not padded or rounded up to be multiple of 4).
737 static void get_note_sizes(DumpState
*s
, const void *note
,
738 uint64_t *note_head_size
,
742 uint64_t note_head_sz
;
746 if (dump_is_64bit(s
)) {
747 const Elf64_Nhdr
*hdr
= note
;
748 note_head_sz
= sizeof(Elf64_Nhdr
);
749 name_sz
= tswap64(hdr
->n_namesz
);
750 desc_sz
= tswap64(hdr
->n_descsz
);
752 const Elf32_Nhdr
*hdr
= note
;
753 note_head_sz
= sizeof(Elf32_Nhdr
);
754 name_sz
= tswap32(hdr
->n_namesz
);
755 desc_sz
= tswap32(hdr
->n_descsz
);
758 if (note_head_size
) {
759 *note_head_size
= note_head_sz
;
762 *name_size
= name_sz
;
765 *desc_size
= desc_sz
;
769 static bool note_name_equal(DumpState
*s
,
770 const uint8_t *note
, const char *name
)
772 int len
= strlen(name
) + 1;
773 uint64_t head_size
, name_size
;
775 get_note_sizes(s
, note
, &head_size
, &name_size
, NULL
);
776 head_size
= ROUND_UP(head_size
, 4);
778 return name_size
== len
&& memcmp(note
+ head_size
, name
, len
) == 0;
781 /* write common header, sub header and elf note to vmcore */
782 static void create_header32(DumpState
*s
, Error
**errp
)
785 DiskDumpHeader32
*dh
= NULL
;
786 KdumpSubHeader32
*kh
= NULL
;
789 uint32_t sub_hdr_size
;
790 uint32_t bitmap_blocks
;
792 uint64_t offset_note
;
794 /* write common header, the version of kdump-compressed format is 6th */
795 size
= sizeof(DiskDumpHeader32
);
796 dh
= g_malloc0(size
);
798 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
799 dh
->header_version
= cpu_to_dump32(s
, 6);
800 block_size
= s
->dump_info
.page_size
;
801 dh
->block_size
= cpu_to_dump32(s
, block_size
);
802 sub_hdr_size
= sizeof(struct KdumpSubHeader32
) + s
->note_size
;
803 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
804 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
805 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
806 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
807 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
808 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
809 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
810 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
812 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
813 status
|= DUMP_DH_COMPRESSED_ZLIB
;
816 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
817 status
|= DUMP_DH_COMPRESSED_LZO
;
821 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
822 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
825 dh
->status
= cpu_to_dump32(s
, status
);
827 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
828 error_setg(errp
, "dump: failed to write disk dump header");
832 /* write sub header */
833 size
= sizeof(KdumpSubHeader32
);
834 kh
= g_malloc0(size
);
836 /* 64bit max_mapnr_64 */
837 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
838 kh
->phys_base
= cpu_to_dump32(s
, s
->dump_info
.phys_base
);
839 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
841 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
843 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
844 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
846 get_note_sizes(s
, s
->guest_note
,
847 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
848 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
849 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
850 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
851 kh
->size_vmcoreinfo
= cpu_to_dump32(s
, size_vmcoreinfo_desc
);
854 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
855 kh
->note_size
= cpu_to_dump32(s
, s
->note_size
);
857 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
858 block_size
, kh
, size
) < 0) {
859 error_setg(errp
, "dump: failed to write kdump sub header");
864 s
->note_buf
= g_malloc0(s
->note_size
);
865 s
->note_buf_offset
= 0;
867 /* use s->note_buf to store notes temporarily */
868 write_elf32_notes(buf_write_note
, s
, errp
);
872 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
874 error_setg(errp
, "dump: failed to write notes");
878 /* get offset of dump_bitmap */
879 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
882 /* get offset of page */
883 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
892 /* write common header, sub header and elf note to vmcore */
893 static void create_header64(DumpState
*s
, Error
**errp
)
896 DiskDumpHeader64
*dh
= NULL
;
897 KdumpSubHeader64
*kh
= NULL
;
900 uint32_t sub_hdr_size
;
901 uint32_t bitmap_blocks
;
903 uint64_t offset_note
;
905 /* write common header, the version of kdump-compressed format is 6th */
906 size
= sizeof(DiskDumpHeader64
);
907 dh
= g_malloc0(size
);
909 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
910 dh
->header_version
= cpu_to_dump32(s
, 6);
911 block_size
= s
->dump_info
.page_size
;
912 dh
->block_size
= cpu_to_dump32(s
, block_size
);
913 sub_hdr_size
= sizeof(struct KdumpSubHeader64
) + s
->note_size
;
914 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
915 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
916 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
917 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
918 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
919 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
920 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
921 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
923 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
924 status
|= DUMP_DH_COMPRESSED_ZLIB
;
927 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
928 status
|= DUMP_DH_COMPRESSED_LZO
;
932 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
933 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
936 dh
->status
= cpu_to_dump32(s
, status
);
938 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
939 error_setg(errp
, "dump: failed to write disk dump header");
943 /* write sub header */
944 size
= sizeof(KdumpSubHeader64
);
945 kh
= g_malloc0(size
);
947 /* 64bit max_mapnr_64 */
948 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
949 kh
->phys_base
= cpu_to_dump64(s
, s
->dump_info
.phys_base
);
950 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
952 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
954 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
955 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
957 get_note_sizes(s
, s
->guest_note
,
958 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
959 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
960 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
961 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
962 kh
->size_vmcoreinfo
= cpu_to_dump64(s
, size_vmcoreinfo_desc
);
965 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
966 kh
->note_size
= cpu_to_dump64(s
, s
->note_size
);
968 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
969 block_size
, kh
, size
) < 0) {
970 error_setg(errp
, "dump: failed to write kdump sub header");
975 s
->note_buf
= g_malloc0(s
->note_size
);
976 s
->note_buf_offset
= 0;
978 /* use s->note_buf to store notes temporarily */
979 write_elf64_notes(buf_write_note
, s
, errp
);
984 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
986 error_setg(errp
, "dump: failed to write notes");
990 /* get offset of dump_bitmap */
991 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
994 /* get offset of page */
995 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
1001 g_free(s
->note_buf
);
1004 static void write_dump_header(DumpState
*s
, Error
**errp
)
1006 if (dump_is_64bit(s
)) {
1007 create_header64(s
, errp
);
1009 create_header32(s
, errp
);
1013 static size_t dump_bitmap_get_bufsize(DumpState
*s
)
1015 return s
->dump_info
.page_size
;
1019 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1020 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1021 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1022 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1023 * vmcore, ie. synchronizing un-sync bit into vmcore.
1025 static int set_dump_bitmap(uint64_t last_pfn
, uint64_t pfn
, bool value
,
1026 uint8_t *buf
, DumpState
*s
)
1028 off_t old_offset
, new_offset
;
1029 off_t offset_bitmap1
, offset_bitmap2
;
1031 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1032 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1034 /* should not set the previous place */
1035 assert(last_pfn
<= pfn
);
1038 * if the bit needed to be set is not cached in buf, flush the data in buf
1039 * to vmcore firstly.
1040 * making new_offset be bigger than old_offset can also sync remained data
1043 old_offset
= bitmap_bufsize
* (last_pfn
/ bits_per_buf
);
1044 new_offset
= bitmap_bufsize
* (pfn
/ bits_per_buf
);
1046 while (old_offset
< new_offset
) {
1047 /* calculate the offset and write dump_bitmap */
1048 offset_bitmap1
= s
->offset_dump_bitmap
+ old_offset
;
1049 if (write_buffer(s
->fd
, offset_bitmap1
, buf
,
1050 bitmap_bufsize
) < 0) {
1054 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1055 offset_bitmap2
= s
->offset_dump_bitmap
+ s
->len_dump_bitmap
+
1057 if (write_buffer(s
->fd
, offset_bitmap2
, buf
,
1058 bitmap_bufsize
) < 0) {
1062 memset(buf
, 0, bitmap_bufsize
);
1063 old_offset
+= bitmap_bufsize
;
1066 /* get the exact place of the bit in the buf, and set it */
1067 byte
= (pfn
% bits_per_buf
) / CHAR_BIT
;
1068 bit
= (pfn
% bits_per_buf
) % CHAR_BIT
;
1070 buf
[byte
] |= 1u << bit
;
1072 buf
[byte
] &= ~(1u << bit
);
1078 static uint64_t dump_paddr_to_pfn(DumpState
*s
, uint64_t addr
)
1080 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1082 return (addr
>> target_page_shift
) - ARCH_PFN_OFFSET
;
1085 static uint64_t dump_pfn_to_paddr(DumpState
*s
, uint64_t pfn
)
1087 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1089 return (pfn
+ ARCH_PFN_OFFSET
) << target_page_shift
;
1093 * exam every page and return the page frame number and the address of the page.
1094 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1095 * blocks, so block->target_start and block->target_end should be interal
1096 * multiples of the target page size.
1098 static bool get_next_page(GuestPhysBlock
**blockptr
, uint64_t *pfnptr
,
1099 uint8_t **bufptr
, DumpState
*s
)
1101 GuestPhysBlock
*block
= *blockptr
;
1102 hwaddr addr
, target_page_mask
= ~((hwaddr
)s
->dump_info
.page_size
- 1);
1105 /* block == NULL means the start of the iteration */
1107 block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1109 assert((block
->target_start
& ~target_page_mask
) == 0);
1110 assert((block
->target_end
& ~target_page_mask
) == 0);
1111 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1113 *bufptr
= block
->host_addr
;
1118 *pfnptr
= *pfnptr
+ 1;
1119 addr
= dump_pfn_to_paddr(s
, *pfnptr
);
1121 if ((addr
>= block
->target_start
) &&
1122 (addr
+ s
->dump_info
.page_size
<= block
->target_end
)) {
1123 buf
= block
->host_addr
+ (addr
- block
->target_start
);
1125 /* the next page is in the next block */
1126 block
= QTAILQ_NEXT(block
, next
);
1131 assert((block
->target_start
& ~target_page_mask
) == 0);
1132 assert((block
->target_end
& ~target_page_mask
) == 0);
1133 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1134 buf
= block
->host_addr
;
1144 static void write_dump_bitmap(DumpState
*s
, Error
**errp
)
1147 uint64_t last_pfn
, pfn
;
1148 void *dump_bitmap_buf
;
1149 size_t num_dumpable
;
1150 GuestPhysBlock
*block_iter
= NULL
;
1151 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1152 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1154 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1155 dump_bitmap_buf
= g_malloc0(bitmap_bufsize
);
1161 * exam memory page by page, and set the bit in dump_bitmap corresponded
1162 * to the existing page.
1164 while (get_next_page(&block_iter
, &pfn
, NULL
, s
)) {
1165 ret
= set_dump_bitmap(last_pfn
, pfn
, true, dump_bitmap_buf
, s
);
1167 error_setg(errp
, "dump: failed to set dump_bitmap");
1176 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1177 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1178 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1180 if (num_dumpable
> 0) {
1181 ret
= set_dump_bitmap(last_pfn
, last_pfn
+ bits_per_buf
, false,
1182 dump_bitmap_buf
, s
);
1184 error_setg(errp
, "dump: failed to sync dump_bitmap");
1189 /* number of dumpable pages that will be dumped later */
1190 s
->num_dumpable
= num_dumpable
;
1193 g_free(dump_bitmap_buf
);
1196 static void prepare_data_cache(DataCache
*data_cache
, DumpState
*s
,
1199 data_cache
->fd
= s
->fd
;
1200 data_cache
->data_size
= 0;
1201 data_cache
->buf_size
= 4 * dump_bitmap_get_bufsize(s
);
1202 data_cache
->buf
= g_malloc0(data_cache
->buf_size
);
1203 data_cache
->offset
= offset
;
1206 static int write_cache(DataCache
*dc
, const void *buf
, size_t size
,
1210 * dc->buf_size should not be less than size, otherwise dc will never be
1213 assert(size
<= dc
->buf_size
);
1216 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1217 * otherwise check if the space is enough for caching data in buf, if not,
1218 * write the data in dc->buf to dc->fd and reset dc->buf
1220 if ((!flag_sync
&& dc
->data_size
+ size
> dc
->buf_size
) ||
1221 (flag_sync
&& dc
->data_size
> 0)) {
1222 if (write_buffer(dc
->fd
, dc
->offset
, dc
->buf
, dc
->data_size
) < 0) {
1226 dc
->offset
+= dc
->data_size
;
1231 memcpy(dc
->buf
+ dc
->data_size
, buf
, size
);
1232 dc
->data_size
+= size
;
1238 static void free_data_cache(DataCache
*data_cache
)
1240 g_free(data_cache
->buf
);
1243 static size_t get_len_buf_out(size_t page_size
, uint32_t flag_compress
)
1245 switch (flag_compress
) {
1246 case DUMP_DH_COMPRESSED_ZLIB
:
1247 return compressBound(page_size
);
1249 case DUMP_DH_COMPRESSED_LZO
:
1251 * LZO will expand incompressible data by a little amount. Please check
1252 * the following URL to see the expansion calculation:
1253 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1255 return page_size
+ page_size
/ 16 + 64 + 3;
1257 #ifdef CONFIG_SNAPPY
1258 case DUMP_DH_COMPRESSED_SNAPPY
:
1259 return snappy_max_compressed_length(page_size
);
1265 static void write_dump_pages(DumpState
*s
, Error
**errp
)
1268 DataCache page_desc
, page_data
;
1269 size_t len_buf_out
, size_out
;
1271 lzo_bytep wrkmem
= NULL
;
1273 uint8_t *buf_out
= NULL
;
1274 off_t offset_desc
, offset_data
;
1275 PageDescriptor pd
, pd_zero
;
1277 GuestPhysBlock
*block_iter
= NULL
;
1280 /* get offset of page_desc and page_data in dump file */
1281 offset_desc
= s
->offset_page
;
1282 offset_data
= offset_desc
+ sizeof(PageDescriptor
) * s
->num_dumpable
;
1284 prepare_data_cache(&page_desc
, s
, offset_desc
);
1285 prepare_data_cache(&page_data
, s
, offset_data
);
1287 /* prepare buffer to store compressed data */
1288 len_buf_out
= get_len_buf_out(s
->dump_info
.page_size
, s
->flag_compress
);
1289 assert(len_buf_out
!= 0);
1292 wrkmem
= g_malloc(LZO1X_1_MEM_COMPRESS
);
1295 buf_out
= g_malloc(len_buf_out
);
1298 * init zero page's page_desc and page_data, because every zero page
1299 * uses the same page_data
1301 pd_zero
.size
= cpu_to_dump32(s
, s
->dump_info
.page_size
);
1302 pd_zero
.flags
= cpu_to_dump32(s
, 0);
1303 pd_zero
.offset
= cpu_to_dump64(s
, offset_data
);
1304 pd_zero
.page_flags
= cpu_to_dump64(s
, 0);
1305 buf
= g_malloc0(s
->dump_info
.page_size
);
1306 ret
= write_cache(&page_data
, buf
, s
->dump_info
.page_size
, false);
1309 error_setg(errp
, "dump: failed to write page data (zero page)");
1313 offset_data
+= s
->dump_info
.page_size
;
1316 * dump memory to vmcore page by page. zero page will all be resided in the
1317 * first page of page section
1319 while (get_next_page(&block_iter
, &pfn_iter
, &buf
, s
)) {
1320 /* check zero page */
1321 if (buffer_is_zero(buf
, s
->dump_info
.page_size
)) {
1322 ret
= write_cache(&page_desc
, &pd_zero
, sizeof(PageDescriptor
),
1325 error_setg(errp
, "dump: failed to write page desc");
1330 * not zero page, then:
1331 * 1. compress the page
1332 * 2. write the compressed page into the cache of page_data
1333 * 3. get page desc of the compressed page and write it into the
1334 * cache of page_desc
1336 * only one compression format will be used here, for
1337 * s->flag_compress is set. But when compression fails to work,
1338 * we fall back to save in plaintext.
1340 size_out
= len_buf_out
;
1341 if ((s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) &&
1342 (compress2(buf_out
, (uLongf
*)&size_out
, buf
,
1343 s
->dump_info
.page_size
, Z_BEST_SPEED
) == Z_OK
) &&
1344 (size_out
< s
->dump_info
.page_size
)) {
1345 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_ZLIB
);
1346 pd
.size
= cpu_to_dump32(s
, size_out
);
1348 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1350 error_setg(errp
, "dump: failed to write page data");
1354 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) &&
1355 (lzo1x_1_compress(buf
, s
->dump_info
.page_size
, buf_out
,
1356 (lzo_uint
*)&size_out
, wrkmem
) == LZO_E_OK
) &&
1357 (size_out
< s
->dump_info
.page_size
)) {
1358 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_LZO
);
1359 pd
.size
= cpu_to_dump32(s
, size_out
);
1361 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1363 error_setg(errp
, "dump: failed to write page data");
1367 #ifdef CONFIG_SNAPPY
1368 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) &&
1369 (snappy_compress((char *)buf
, s
->dump_info
.page_size
,
1370 (char *)buf_out
, &size_out
) == SNAPPY_OK
) &&
1371 (size_out
< s
->dump_info
.page_size
)) {
1372 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_SNAPPY
);
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");
1383 * fall back to save in plaintext, size_out should be
1384 * assigned the target's page size
1386 pd
.flags
= cpu_to_dump32(s
, 0);
1387 size_out
= s
->dump_info
.page_size
;
1388 pd
.size
= cpu_to_dump32(s
, size_out
);
1390 ret
= write_cache(&page_data
, buf
,
1391 s
->dump_info
.page_size
, false);
1393 error_setg(errp
, "dump: failed to write page data");
1398 /* get and write page desc here */
1399 pd
.page_flags
= cpu_to_dump64(s
, 0);
1400 pd
.offset
= cpu_to_dump64(s
, offset_data
);
1401 offset_data
+= size_out
;
1403 ret
= write_cache(&page_desc
, &pd
, sizeof(PageDescriptor
), false);
1405 error_setg(errp
, "dump: failed to write page desc");
1409 s
->written_size
+= s
->dump_info
.page_size
;
1412 ret
= write_cache(&page_desc
, NULL
, 0, true);
1414 error_setg(errp
, "dump: failed to sync cache for page_desc");
1417 ret
= write_cache(&page_data
, NULL
, 0, true);
1419 error_setg(errp
, "dump: failed to sync cache for page_data");
1424 free_data_cache(&page_desc
);
1425 free_data_cache(&page_data
);
1434 static void create_kdump_vmcore(DumpState
*s
, Error
**errp
)
1440 * the kdump-compressed format is:
1442 * +------------------------------------------+ 0x0
1443 * | main header (struct disk_dump_header) |
1444 * |------------------------------------------+ block 1
1445 * | sub header (struct kdump_sub_header) |
1446 * |------------------------------------------+ block 2
1447 * | 1st-dump_bitmap |
1448 * |------------------------------------------+ block 2 + X blocks
1449 * | 2nd-dump_bitmap | (aligned by block)
1450 * |------------------------------------------+ block 2 + 2 * X blocks
1451 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1452 * | page desc for pfn 1 (struct page_desc) |
1454 * |------------------------------------------| (not aligned by block)
1455 * | page data (pfn 0) |
1456 * | page data (pfn 1) |
1458 * +------------------------------------------+
1461 ret
= write_start_flat_header(s
->fd
);
1463 error_setg(errp
, "dump: failed to write start flat header");
1467 write_dump_header(s
, errp
);
1472 write_dump_bitmap(s
, errp
);
1477 write_dump_pages(s
, errp
);
1482 ret
= write_end_flat_header(s
->fd
);
1484 error_setg(errp
, "dump: failed to write end flat header");
1489 static ram_addr_t
get_start_block(DumpState
*s
)
1491 GuestPhysBlock
*block
;
1493 if (!s
->has_filter
) {
1494 s
->next_block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1498 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1499 if (block
->target_start
>= s
->begin
+ s
->length
||
1500 block
->target_end
<= s
->begin
) {
1501 /* This block is out of the range */
1505 s
->next_block
= block
;
1506 if (s
->begin
> block
->target_start
) {
1507 s
->start
= s
->begin
- block
->target_start
;
1517 static void get_max_mapnr(DumpState
*s
)
1519 GuestPhysBlock
*last_block
;
1521 last_block
= QTAILQ_LAST(&s
->guest_phys_blocks
.head
);
1522 s
->max_mapnr
= dump_paddr_to_pfn(s
, last_block
->target_end
);
1525 static DumpState dump_state_global
= { .status
= DUMP_STATUS_NONE
};
1527 static void dump_state_prepare(DumpState
*s
)
1529 /* zero the struct, setting status to active */
1530 *s
= (DumpState
) { .status
= DUMP_STATUS_ACTIVE
};
1533 bool qemu_system_dump_in_progress(void)
1535 DumpState
*state
= &dump_state_global
;
1536 return (qatomic_read(&state
->status
) == DUMP_STATUS_ACTIVE
);
1539 /* calculate total size of memory to be dumped (taking filter into
1541 static int64_t dump_calculate_size(DumpState
*s
)
1543 GuestPhysBlock
*block
;
1544 int64_t size
= 0, total
= 0, left
= 0, right
= 0;
1546 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1547 if (s
->has_filter
) {
1548 /* calculate the overlapped region. */
1549 left
= MAX(s
->begin
, block
->target_start
);
1550 right
= MIN(s
->begin
+ s
->length
, block
->target_end
);
1551 size
= right
- left
;
1552 size
= size
> 0 ? size
: 0;
1554 /* count the whole region in */
1555 size
= (block
->target_end
- block
->target_start
);
1563 static void vmcoreinfo_update_phys_base(DumpState
*s
)
1565 uint64_t size
, note_head_size
, name_size
, phys_base
;
1570 if (!note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1574 get_note_sizes(s
, s
->guest_note
, ¬e_head_size
, &name_size
, &size
);
1575 note_head_size
= ROUND_UP(note_head_size
, 4);
1577 vmci
= s
->guest_note
+ note_head_size
+ ROUND_UP(name_size
, 4);
1578 *(vmci
+ size
) = '\0';
1580 lines
= g_strsplit((char *)vmci
, "\n", -1);
1581 for (i
= 0; lines
[i
]; i
++) {
1582 const char *prefix
= NULL
;
1584 if (s
->dump_info
.d_machine
== EM_X86_64
) {
1585 prefix
= "NUMBER(phys_base)=";
1586 } else if (s
->dump_info
.d_machine
== EM_AARCH64
) {
1587 prefix
= "NUMBER(PHYS_OFFSET)=";
1590 if (prefix
&& g_str_has_prefix(lines
[i
], prefix
)) {
1591 if (qemu_strtou64(lines
[i
] + strlen(prefix
), NULL
, 16,
1593 warn_report("Failed to read %s", prefix
);
1595 s
->dump_info
.phys_base
= phys_base
;
1604 static void dump_init(DumpState
*s
, int fd
, bool has_format
,
1605 DumpGuestMemoryFormat format
, bool paging
, bool has_filter
,
1606 int64_t begin
, int64_t length
, Error
**errp
)
1609 VMCoreInfoState
*vmci
= vmcoreinfo_find();
1614 s
->has_format
= has_format
;
1616 s
->written_size
= 0;
1618 /* kdump-compressed is conflict with paging and filter */
1619 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1620 assert(!paging
&& !has_filter
);
1623 if (runstate_is_running()) {
1624 vm_stop(RUN_STATE_SAVE_VM
);
1630 /* If we use KVM, we should synchronize the registers before we get dump
1631 * info or physmap info.
1633 cpu_synchronize_all_states();
1640 s
->has_filter
= has_filter
;
1644 memory_mapping_list_init(&s
->list
);
1646 guest_phys_blocks_init(&s
->guest_phys_blocks
);
1647 guest_phys_blocks_append(&s
->guest_phys_blocks
);
1648 s
->total_size
= dump_calculate_size(s
);
1649 #ifdef DEBUG_DUMP_GUEST_MEMORY
1650 fprintf(stderr
, "DUMP: total memory to dump: %lu\n", s
->total_size
);
1653 /* it does not make sense to dump non-existent memory */
1654 if (!s
->total_size
) {
1655 error_setg(errp
, "dump: no guest memory to dump");
1659 s
->start
= get_start_block(s
);
1660 if (s
->start
== -1) {
1661 error_setg(errp
, QERR_INVALID_PARAMETER
, "begin");
1665 /* get dump info: endian, class and architecture.
1666 * If the target architecture is not supported, cpu_get_dump_info() will
1669 ret
= cpu_get_dump_info(&s
->dump_info
, &s
->guest_phys_blocks
);
1671 error_setg(errp
, QERR_UNSUPPORTED
);
1675 if (!s
->dump_info
.page_size
) {
1676 s
->dump_info
.page_size
= TARGET_PAGE_SIZE
;
1679 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
1680 s
->dump_info
.d_machine
, nr_cpus
);
1681 if (s
->note_size
< 0) {
1682 error_setg(errp
, QERR_UNSUPPORTED
);
1687 * The goal of this block is to (a) update the previously guessed
1688 * phys_base, (b) copy the guest note out of the guest.
1689 * Failure to do so is not fatal for dumping.
1692 uint64_t addr
, note_head_size
, name_size
, desc_size
;
1696 note_head_size
= dump_is_64bit(s
) ?
1697 sizeof(Elf64_Nhdr
) : sizeof(Elf32_Nhdr
);
1699 format
= le16_to_cpu(vmci
->vmcoreinfo
.guest_format
);
1700 size
= le32_to_cpu(vmci
->vmcoreinfo
.size
);
1701 addr
= le64_to_cpu(vmci
->vmcoreinfo
.paddr
);
1702 if (!vmci
->has_vmcoreinfo
) {
1703 warn_report("guest note is not present");
1704 } else if (size
< note_head_size
|| size
> MAX_GUEST_NOTE_SIZE
) {
1705 warn_report("guest note size is invalid: %" PRIu32
, size
);
1706 } else if (format
!= FW_CFG_VMCOREINFO_FORMAT_ELF
) {
1707 warn_report("guest note format is unsupported: %" PRIu16
, format
);
1709 s
->guest_note
= g_malloc(size
+ 1); /* +1 for adding \0 */
1710 cpu_physical_memory_read(addr
, s
->guest_note
, size
);
1712 get_note_sizes(s
, s
->guest_note
, NULL
, &name_size
, &desc_size
);
1713 s
->guest_note_size
= ELF_NOTE_SIZE(note_head_size
, name_size
,
1715 if (name_size
> MAX_GUEST_NOTE_SIZE
||
1716 desc_size
> MAX_GUEST_NOTE_SIZE
||
1717 s
->guest_note_size
> size
) {
1718 warn_report("Invalid guest note header");
1719 g_free(s
->guest_note
);
1720 s
->guest_note
= NULL
;
1722 vmcoreinfo_update_phys_base(s
);
1723 s
->note_size
+= s
->guest_note_size
;
1728 /* get memory mapping */
1730 qemu_get_guest_memory_mapping(&s
->list
, &s
->guest_phys_blocks
, errp
);
1735 qemu_get_guest_simple_memory_mapping(&s
->list
, &s
->guest_phys_blocks
);
1738 s
->nr_cpus
= nr_cpus
;
1743 tmp
= DIV_ROUND_UP(DIV_ROUND_UP(s
->max_mapnr
, CHAR_BIT
),
1744 s
->dump_info
.page_size
);
1745 s
->len_dump_bitmap
= tmp
* s
->dump_info
.page_size
;
1747 /* init for kdump-compressed format */
1748 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1750 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
:
1751 s
->flag_compress
= DUMP_DH_COMPRESSED_ZLIB
;
1754 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
:
1756 if (lzo_init() != LZO_E_OK
) {
1757 error_setg(errp
, "failed to initialize the LZO library");
1761 s
->flag_compress
= DUMP_DH_COMPRESSED_LZO
;
1764 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
:
1765 s
->flag_compress
= DUMP_DH_COMPRESSED_SNAPPY
;
1769 s
->flag_compress
= 0;
1775 if (s
->has_filter
) {
1776 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
1780 * calculate phdr_num
1782 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1784 s
->phdr_num
= 1; /* PT_NOTE */
1785 if (s
->list
.num
< UINT16_MAX
- 2) {
1787 s
->phdr_num
+= s
->list
.num
;
1789 /* sh_info of section 0 holds the real number of phdrs */
1792 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1793 if (s
->list
.num
<= UINT32_MAX
- 1) {
1794 s
->phdr_num
+= s
->list
.num
;
1796 s
->phdr_num
= UINT32_MAX
;
1800 if (dump_is_64bit(s
)) {
1801 s
->phdr_offset
= sizeof(Elf64_Ehdr
);
1802 s
->shdr_offset
= s
->phdr_offset
+ sizeof(Elf64_Phdr
) * s
->phdr_num
;
1803 s
->note_offset
= s
->shdr_offset
+ sizeof(Elf64_Shdr
) * s
->shdr_num
;
1804 s
->memory_offset
= s
->note_offset
+ s
->note_size
;
1807 s
->phdr_offset
= sizeof(Elf32_Ehdr
);
1808 s
->shdr_offset
= s
->phdr_offset
+ sizeof(Elf32_Phdr
) * s
->phdr_num
;
1809 s
->note_offset
= s
->shdr_offset
+ sizeof(Elf32_Shdr
) * s
->shdr_num
;
1810 s
->memory_offset
= s
->note_offset
+ s
->note_size
;
1819 /* this operation might be time consuming. */
1820 static void dump_process(DumpState
*s
, Error
**errp
)
1823 DumpQueryResult
*result
= NULL
;
1825 if (s
->has_format
&& s
->format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
) {
1826 #ifdef TARGET_X86_64
1827 create_win_dump(s
, errp
);
1829 } else if (s
->has_format
&& s
->format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1830 create_kdump_vmcore(s
, errp
);
1832 create_vmcore(s
, errp
);
1835 /* make sure status is written after written_size updates */
1837 qatomic_set(&s
->status
,
1838 (*errp
? DUMP_STATUS_FAILED
: DUMP_STATUS_COMPLETED
));
1840 /* send DUMP_COMPLETED message (unconditionally) */
1841 result
= qmp_query_dump(NULL
);
1842 /* should never fail */
1844 qapi_event_send_dump_completed(result
, !!*errp
, (*errp
?
1845 error_get_pretty(*errp
) : NULL
));
1846 qapi_free_DumpQueryResult(result
);
1851 static void *dump_thread(void *data
)
1853 DumpState
*s
= (DumpState
*)data
;
1854 dump_process(s
, NULL
);
1858 DumpQueryResult
*qmp_query_dump(Error
**errp
)
1860 DumpQueryResult
*result
= g_new(DumpQueryResult
, 1);
1861 DumpState
*state
= &dump_state_global
;
1862 result
->status
= qatomic_read(&state
->status
);
1863 /* make sure we are reading status and written_size in order */
1865 result
->completed
= state
->written_size
;
1866 result
->total
= state
->total_size
;
1870 void qmp_dump_guest_memory(bool paging
, const char *file
,
1871 bool has_detach
, bool detach
,
1872 bool has_begin
, int64_t begin
, bool has_length
,
1873 int64_t length
, bool has_format
,
1874 DumpGuestMemoryFormat format
, Error
**errp
)
1880 bool detach_p
= false;
1882 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1883 error_setg(errp
, "Dump not allowed during incoming migration.");
1887 /* if there is a dump in background, we should wait until the dump
1889 if (qemu_system_dump_in_progress()) {
1890 error_setg(errp
, "There is a dump in process, please wait.");
1895 * kdump-compressed format need the whole memory dumped, so paging or
1896 * filter is not supported here.
1898 if ((has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) &&
1899 (paging
|| has_begin
|| has_length
)) {
1900 error_setg(errp
, "kdump-compressed format doesn't support paging or "
1904 if (has_begin
&& !has_length
) {
1905 error_setg(errp
, QERR_MISSING_PARAMETER
, "length");
1908 if (!has_begin
&& has_length
) {
1909 error_setg(errp
, QERR_MISSING_PARAMETER
, "begin");
1916 /* check whether lzo/snappy is supported */
1918 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
) {
1919 error_setg(errp
, "kdump-lzo is not available now");
1924 #ifndef CONFIG_SNAPPY
1925 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
) {
1926 error_setg(errp
, "kdump-snappy is not available now");
1931 #ifndef TARGET_X86_64
1932 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
) {
1933 error_setg(errp
, "Windows dump is only available for x86-64");
1939 if (strstart(file
, "fd:", &p
)) {
1940 fd
= monitor_get_fd(monitor_cur(), p
, errp
);
1947 if (strstart(file
, "file:", &p
)) {
1948 fd
= qemu_open_old(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
1950 error_setg_file_open(errp
, errno
, p
);
1956 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
1960 if (!dump_migration_blocker
) {
1961 error_setg(&dump_migration_blocker
,
1962 "Live migration disabled: dump-guest-memory in progress");
1966 * Allows even for -only-migratable, but forbid migration during the
1967 * process of dump guest memory.
1969 if (migrate_add_blocker_internal(dump_migration_blocker
, errp
)) {
1970 /* Remember to release the fd before passing it over to dump state */
1975 s
= &dump_state_global
;
1976 dump_state_prepare(s
);
1978 dump_init(s
, fd
, has_format
, format
, paging
, has_begin
,
1979 begin
, length
, errp
);
1981 qatomic_set(&s
->status
, DUMP_STATUS_FAILED
);
1988 qemu_thread_create(&s
->dump_thread
, "dump_thread", dump_thread
,
1989 s
, QEMU_THREAD_DETACHED
);
1992 dump_process(s
, errp
);
1996 DumpGuestMemoryCapability
*qmp_query_dump_guest_memory_capability(Error
**errp
)
1998 DumpGuestMemoryCapability
*cap
=
1999 g_new0(DumpGuestMemoryCapability
, 1);
2000 DumpGuestMemoryFormatList
**tail
= &cap
->formats
;
2002 /* elf is always available */
2003 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_ELF
);
2005 /* kdump-zlib is always available */
2006 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
);
2008 /* add new item if kdump-lzo is available */
2010 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
);
2013 /* add new item if kdump-snappy is available */
2014 #ifdef CONFIG_SNAPPY
2015 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
);
2018 /* Windows dump is available only if target is x86_64 */
2019 #ifdef TARGET_X86_64
2020 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
);