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 "qemu/bswap.h"
18 #include "exec/target_page.h"
19 #include "monitor/monitor.h"
20 #include "sysemu/dump.h"
21 #include "sysemu/runstate.h"
22 #include "sysemu/cpus.h"
23 #include "qapi/error.h"
24 #include "qapi/qapi-commands-dump.h"
25 #include "qapi/qapi-events-dump.h"
26 #include "qapi/qmp/qerror.h"
27 #include "qemu/error-report.h"
28 #include "qemu/main-loop.h"
29 #include "hw/misc/vmcoreinfo.h"
30 #include "migration/blocker.h"
31 #include "hw/core/cpu.h"
36 #include <lzo/lzo1x.h>
41 #ifndef ELF_MACHINE_UNAME
42 #define ELF_MACHINE_UNAME "Unknown"
45 #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
47 static Error
*dump_migration_blocker
;
49 #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
50 ((DIV_ROUND_UP((hdr_size), 4) + \
51 DIV_ROUND_UP((name_size), 4) + \
52 DIV_ROUND_UP((desc_size), 4)) * 4)
54 static inline bool dump_is_64bit(DumpState
*s
)
56 return s
->dump_info
.d_class
== ELFCLASS64
;
59 static inline bool dump_has_filter(DumpState
*s
)
61 return s
->filter_area_length
> 0;
64 uint16_t cpu_to_dump16(DumpState
*s
, uint16_t val
)
66 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
67 val
= cpu_to_le16(val
);
69 val
= cpu_to_be16(val
);
75 uint32_t cpu_to_dump32(DumpState
*s
, uint32_t val
)
77 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
78 val
= cpu_to_le32(val
);
80 val
= cpu_to_be32(val
);
86 uint64_t cpu_to_dump64(DumpState
*s
, uint64_t val
)
88 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
89 val
= cpu_to_le64(val
);
91 val
= cpu_to_be64(val
);
97 static int dump_cleanup(DumpState
*s
)
99 guest_phys_blocks_free(&s
->guest_phys_blocks
);
100 memory_mapping_list_free(&s
->list
);
102 g_free(s
->guest_note
);
103 g_array_unref(s
->string_table_buf
);
104 s
->guest_note
= NULL
;
107 qemu_mutex_lock_iothread();
111 qemu_mutex_unlock_iothread();
114 migrate_del_blocker(&dump_migration_blocker
);
119 static int fd_write_vmcore(const void *buf
, size_t size
, void *opaque
)
121 DumpState
*s
= opaque
;
124 written_size
= qemu_write_full(s
->fd
, buf
, size
);
125 if (written_size
!= size
) {
132 static void prepare_elf64_header(DumpState
*s
, Elf64_Ehdr
*elf_header
)
135 * phnum in the elf header is 16 bit, if we have more segments we
136 * set phnum to PN_XNUM and write the real number of segments to a
139 uint16_t phnum
= MIN(s
->phdr_num
, PN_XNUM
);
141 memset(elf_header
, 0, sizeof(Elf64_Ehdr
));
142 memcpy(elf_header
, ELFMAG
, SELFMAG
);
143 elf_header
->e_ident
[EI_CLASS
] = ELFCLASS64
;
144 elf_header
->e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
145 elf_header
->e_ident
[EI_VERSION
] = EV_CURRENT
;
146 elf_header
->e_type
= cpu_to_dump16(s
, ET_CORE
);
147 elf_header
->e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
148 elf_header
->e_version
= cpu_to_dump32(s
, EV_CURRENT
);
149 elf_header
->e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
150 elf_header
->e_phoff
= cpu_to_dump64(s
, s
->phdr_offset
);
151 elf_header
->e_phentsize
= cpu_to_dump16(s
, sizeof(Elf64_Phdr
));
152 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
);
156 elf_header
->e_shstrndx
= cpu_to_dump16(s
, s
->shdr_num
- 1);
159 static void prepare_elf32_header(DumpState
*s
, Elf32_Ehdr
*elf_header
)
162 * phnum in the elf header is 16 bit, if we have more segments we
163 * set phnum to PN_XNUM and write the real number of segments to a
166 uint16_t phnum
= MIN(s
->phdr_num
, PN_XNUM
);
168 memset(elf_header
, 0, sizeof(Elf32_Ehdr
));
169 memcpy(elf_header
, ELFMAG
, SELFMAG
);
170 elf_header
->e_ident
[EI_CLASS
] = ELFCLASS32
;
171 elf_header
->e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
172 elf_header
->e_ident
[EI_VERSION
] = EV_CURRENT
;
173 elf_header
->e_type
= cpu_to_dump16(s
, ET_CORE
);
174 elf_header
->e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
175 elf_header
->e_version
= cpu_to_dump32(s
, EV_CURRENT
);
176 elf_header
->e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
177 elf_header
->e_phoff
= cpu_to_dump32(s
, s
->phdr_offset
);
178 elf_header
->e_phentsize
= cpu_to_dump16(s
, sizeof(Elf32_Phdr
));
179 elf_header
->e_phnum
= cpu_to_dump16(s
, phnum
);
180 elf_header
->e_shoff
= cpu_to_dump32(s
, s
->shdr_offset
);
181 elf_header
->e_shentsize
= cpu_to_dump16(s
, sizeof(Elf32_Shdr
));
182 elf_header
->e_shnum
= cpu_to_dump16(s
, s
->shdr_num
);
183 elf_header
->e_shstrndx
= cpu_to_dump16(s
, s
->shdr_num
- 1);
186 static void write_elf_header(DumpState
*s
, Error
**errp
)
188 Elf32_Ehdr elf32_header
;
189 Elf64_Ehdr elf64_header
;
194 /* The NULL header and the shstrtab are always defined */
195 assert(s
->shdr_num
>= 2);
196 if (dump_is_64bit(s
)) {
197 prepare_elf64_header(s
, &elf64_header
);
198 header_size
= sizeof(elf64_header
);
199 header_ptr
= &elf64_header
;
201 prepare_elf32_header(s
, &elf32_header
);
202 header_size
= sizeof(elf32_header
);
203 header_ptr
= &elf32_header
;
206 ret
= fd_write_vmcore(header_ptr
, header_size
, s
);
208 error_setg_errno(errp
, -ret
, "dump: failed to write elf header");
212 static void write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
213 int phdr_index
, hwaddr offset
,
214 hwaddr filesz
, Error
**errp
)
219 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
220 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
221 phdr
.p_offset
= cpu_to_dump64(s
, offset
);
222 phdr
.p_paddr
= cpu_to_dump64(s
, memory_mapping
->phys_addr
);
223 phdr
.p_filesz
= cpu_to_dump64(s
, filesz
);
224 phdr
.p_memsz
= cpu_to_dump64(s
, memory_mapping
->length
);
225 phdr
.p_vaddr
= cpu_to_dump64(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
227 assert(memory_mapping
->length
>= filesz
);
229 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
231 error_setg_errno(errp
, -ret
,
232 "dump: failed to write program header table");
236 static void write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
237 int phdr_index
, hwaddr offset
,
238 hwaddr filesz
, Error
**errp
)
243 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
244 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
245 phdr
.p_offset
= cpu_to_dump32(s
, offset
);
246 phdr
.p_paddr
= cpu_to_dump32(s
, memory_mapping
->phys_addr
);
247 phdr
.p_filesz
= cpu_to_dump32(s
, filesz
);
248 phdr
.p_memsz
= cpu_to_dump32(s
, memory_mapping
->length
);
250 cpu_to_dump32(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
252 assert(memory_mapping
->length
>= filesz
);
254 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
256 error_setg_errno(errp
, -ret
,
257 "dump: failed to write program header table");
261 static void prepare_elf64_phdr_note(DumpState
*s
, Elf64_Phdr
*phdr
)
263 memset(phdr
, 0, sizeof(*phdr
));
264 phdr
->p_type
= cpu_to_dump32(s
, PT_NOTE
);
265 phdr
->p_offset
= cpu_to_dump64(s
, s
->note_offset
);
267 phdr
->p_filesz
= cpu_to_dump64(s
, s
->note_size
);
268 phdr
->p_memsz
= cpu_to_dump64(s
, s
->note_size
);
272 static inline int cpu_index(CPUState
*cpu
)
274 return cpu
->cpu_index
+ 1;
277 static void write_guest_note(WriteCoreDumpFunction f
, DumpState
*s
,
283 ret
= f(s
->guest_note
, s
->guest_note_size
, s
);
285 error_setg(errp
, "dump: failed to write guest note");
290 static void write_elf64_notes(WriteCoreDumpFunction f
, DumpState
*s
,
299 ret
= cpu_write_elf64_note(f
, cpu
, id
, s
);
301 error_setg(errp
, "dump: failed to write elf notes");
307 ret
= cpu_write_elf64_qemunote(f
, cpu
, s
);
309 error_setg(errp
, "dump: failed to write CPU status");
314 write_guest_note(f
, s
, errp
);
317 static void prepare_elf32_phdr_note(DumpState
*s
, Elf32_Phdr
*phdr
)
319 memset(phdr
, 0, sizeof(*phdr
));
320 phdr
->p_type
= cpu_to_dump32(s
, PT_NOTE
);
321 phdr
->p_offset
= cpu_to_dump32(s
, s
->note_offset
);
323 phdr
->p_filesz
= cpu_to_dump32(s
, s
->note_size
);
324 phdr
->p_memsz
= cpu_to_dump32(s
, s
->note_size
);
328 static void write_elf32_notes(WriteCoreDumpFunction f
, DumpState
*s
,
337 ret
= cpu_write_elf32_note(f
, cpu
, id
, s
);
339 error_setg(errp
, "dump: failed to write elf notes");
345 ret
= cpu_write_elf32_qemunote(f
, cpu
, s
);
347 error_setg(errp
, "dump: failed to write CPU status");
352 write_guest_note(f
, s
, errp
);
355 static void write_elf_phdr_note(DumpState
*s
, Error
**errp
)
363 if (dump_is_64bit(s
)) {
364 prepare_elf64_phdr_note(s
, &phdr64
);
365 size
= sizeof(phdr64
);
368 prepare_elf32_phdr_note(s
, &phdr32
);
369 size
= sizeof(phdr32
);
373 ret
= fd_write_vmcore(phdr
, size
, s
);
375 error_setg_errno(errp
, -ret
,
376 "dump: failed to write program header table");
380 static void prepare_elf_section_hdr_zero(DumpState
*s
)
382 if (dump_is_64bit(s
)) {
383 Elf64_Shdr
*shdr64
= s
->elf_section_hdrs
;
385 shdr64
->sh_info
= cpu_to_dump32(s
, s
->phdr_num
);
387 Elf32_Shdr
*shdr32
= s
->elf_section_hdrs
;
389 shdr32
->sh_info
= cpu_to_dump32(s
, s
->phdr_num
);
393 static void prepare_elf_section_hdr_string(DumpState
*s
, void *buff
)
395 uint64_t index
= s
->string_table_buf
->len
;
396 const char strtab
[] = ".shstrtab";
397 Elf32_Shdr shdr32
= {};
398 Elf64_Shdr shdr64
= {};
402 g_array_append_vals(s
->string_table_buf
, strtab
, sizeof(strtab
));
403 if (dump_is_64bit(s
)) {
404 shdr_size
= sizeof(Elf64_Shdr
);
405 shdr64
.sh_type
= SHT_STRTAB
;
406 shdr64
.sh_offset
= s
->section_offset
+ s
->elf_section_data_size
;
407 shdr64
.sh_name
= index
;
408 shdr64
.sh_size
= s
->string_table_buf
->len
;
411 shdr_size
= sizeof(Elf32_Shdr
);
412 shdr32
.sh_type
= SHT_STRTAB
;
413 shdr32
.sh_offset
= s
->section_offset
+ s
->elf_section_data_size
;
414 shdr32
.sh_name
= index
;
415 shdr32
.sh_size
= s
->string_table_buf
->len
;
418 memcpy(buff
, shdr
, shdr_size
);
421 static bool prepare_elf_section_hdrs(DumpState
*s
, Error
**errp
)
423 size_t len
, sizeof_shdr
;
429 * - Arch section hdrs
432 sizeof_shdr
= dump_is_64bit(s
) ? sizeof(Elf64_Shdr
) : sizeof(Elf32_Shdr
);
433 len
= sizeof_shdr
* s
->shdr_num
;
434 s
->elf_section_hdrs
= g_malloc0(len
);
435 buff_hdr
= s
->elf_section_hdrs
;
438 * The first section header is ALWAYS a special initial section
441 * The header should be 0 with one exception being that if
442 * phdr_num is PN_XNUM then the sh_info field contains the real
443 * number of segment entries.
445 * As we zero allocate the buffer we will only need to modify
446 * sh_info for the PN_XNUM case.
448 if (s
->phdr_num
>= PN_XNUM
) {
449 prepare_elf_section_hdr_zero(s
);
451 buff_hdr
+= sizeof_shdr
;
453 /* Add architecture defined section headers */
454 if (s
->dump_info
.arch_sections_write_hdr_fn
455 && s
->shdr_num
> 2) {
456 buff_hdr
+= s
->dump_info
.arch_sections_write_hdr_fn(s
, buff_hdr
);
458 if (s
->shdr_num
>= SHN_LORESERVE
) {
459 error_setg_errno(errp
, EINVAL
,
460 "dump: too many architecture defined sections");
466 * String table is the last section since strings are added via
467 * arch_sections_write_hdr().
469 prepare_elf_section_hdr_string(s
, buff_hdr
);
473 static void write_elf_section_headers(DumpState
*s
, Error
**errp
)
475 size_t sizeof_shdr
= dump_is_64bit(s
) ? sizeof(Elf64_Shdr
) : sizeof(Elf32_Shdr
);
478 if (!prepare_elf_section_hdrs(s
, errp
)) {
482 ret
= fd_write_vmcore(s
->elf_section_hdrs
, s
->shdr_num
* sizeof_shdr
, s
);
484 error_setg_errno(errp
, -ret
, "dump: failed to write section headers");
487 g_free(s
->elf_section_hdrs
);
490 static void write_elf_sections(DumpState
*s
, Error
**errp
)
494 if (s
->elf_section_data_size
) {
495 /* Write architecture section data */
496 ret
= fd_write_vmcore(s
->elf_section_data
,
497 s
->elf_section_data_size
, s
);
499 error_setg_errno(errp
, -ret
,
500 "dump: failed to write architecture section data");
505 /* Write string table */
506 ret
= fd_write_vmcore(s
->string_table_buf
->data
,
507 s
->string_table_buf
->len
, s
);
509 error_setg_errno(errp
, -ret
, "dump: failed to write string table data");
513 static void write_data(DumpState
*s
, void *buf
, int length
, Error
**errp
)
517 ret
= fd_write_vmcore(buf
, length
, s
);
519 error_setg_errno(errp
, -ret
, "dump: failed to save memory");
521 s
->written_size
+= length
;
525 /* write the memory to vmcore. 1 page per I/O. */
526 static void write_memory(DumpState
*s
, GuestPhysBlock
*block
, ram_addr_t start
,
527 int64_t size
, Error
**errp
)
532 for (i
= 0; i
< size
/ s
->dump_info
.page_size
; i
++) {
533 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
534 s
->dump_info
.page_size
, errp
);
540 if ((size
% s
->dump_info
.page_size
) != 0) {
541 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
542 size
% s
->dump_info
.page_size
, errp
);
549 /* get the memory's offset and size in the vmcore */
550 static void get_offset_range(hwaddr phys_addr
,
551 ram_addr_t mapping_length
,
556 GuestPhysBlock
*block
;
557 hwaddr offset
= s
->memory_offset
;
558 int64_t size_in_block
, start
;
560 /* When the memory is not stored into vmcore, offset will be -1 */
564 if (dump_has_filter(s
)) {
565 if (phys_addr
< s
->filter_area_begin
||
566 phys_addr
>= s
->filter_area_begin
+ s
->filter_area_length
) {
571 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
572 if (dump_has_filter(s
)) {
573 if (block
->target_start
>= s
->filter_area_begin
+ s
->filter_area_length
||
574 block
->target_end
<= s
->filter_area_begin
) {
575 /* This block is out of the range */
579 if (s
->filter_area_begin
<= block
->target_start
) {
580 start
= block
->target_start
;
582 start
= s
->filter_area_begin
;
585 size_in_block
= block
->target_end
- start
;
586 if (s
->filter_area_begin
+ s
->filter_area_length
< block
->target_end
) {
587 size_in_block
-= block
->target_end
- (s
->filter_area_begin
+ s
->filter_area_length
);
590 start
= block
->target_start
;
591 size_in_block
= block
->target_end
- block
->target_start
;
594 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
595 *p_offset
= phys_addr
- start
+ offset
;
597 /* The offset range mapped from the vmcore file must not spill over
598 * the GuestPhysBlock, clamp it. The rest of the mapping will be
599 * zero-filled in memory at load time; see
600 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
602 *p_filesz
= phys_addr
+ mapping_length
<= start
+ size_in_block
?
604 size_in_block
- (phys_addr
- start
);
608 offset
+= size_in_block
;
612 static void write_elf_phdr_loads(DumpState
*s
, Error
**errp
)
615 hwaddr offset
, filesz
;
616 MemoryMapping
*memory_mapping
;
617 uint32_t phdr_index
= 1;
619 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
620 get_offset_range(memory_mapping
->phys_addr
,
621 memory_mapping
->length
,
622 s
, &offset
, &filesz
);
623 if (dump_is_64bit(s
)) {
624 write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
,
627 write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
,
635 if (phdr_index
>= s
->phdr_num
) {
641 static void write_elf_notes(DumpState
*s
, Error
**errp
)
643 if (dump_is_64bit(s
)) {
644 write_elf64_notes(fd_write_vmcore
, s
, errp
);
646 write_elf32_notes(fd_write_vmcore
, s
, errp
);
650 /* write elf header, PT_NOTE and elf note to vmcore. */
651 static void dump_begin(DumpState
*s
, Error
**errp
)
656 * the vmcore's format is:
675 * we only know where the memory is saved after we write elf note into
679 /* write elf header to vmcore */
680 write_elf_header(s
, errp
);
685 /* write section headers to vmcore */
686 write_elf_section_headers(s
, errp
);
691 /* write PT_NOTE to vmcore */
692 write_elf_phdr_note(s
, errp
);
697 /* write all PT_LOADs to vmcore */
698 write_elf_phdr_loads(s
, errp
);
703 /* write notes to vmcore */
704 write_elf_notes(s
, errp
);
707 int64_t dump_filtered_memblock_size(GuestPhysBlock
*block
,
708 int64_t filter_area_start
,
709 int64_t filter_area_length
)
711 int64_t size
, left
, right
;
713 /* No filter, return full size */
714 if (!filter_area_length
) {
715 return block
->target_end
- block
->target_start
;
718 /* calculate the overlapped region. */
719 left
= MAX(filter_area_start
, block
->target_start
);
720 right
= MIN(filter_area_start
+ filter_area_length
, block
->target_end
);
722 size
= size
> 0 ? size
: 0;
727 int64_t dump_filtered_memblock_start(GuestPhysBlock
*block
,
728 int64_t filter_area_start
,
729 int64_t filter_area_length
)
731 if (filter_area_length
) {
732 /* return -1 if the block is not within filter area */
733 if (block
->target_start
>= filter_area_start
+ filter_area_length
||
734 block
->target_end
<= filter_area_start
) {
738 if (filter_area_start
> block
->target_start
) {
739 return filter_area_start
- block
->target_start
;
746 /* write all memory to vmcore */
747 static void dump_iterate(DumpState
*s
, Error
**errp
)
750 GuestPhysBlock
*block
;
751 int64_t memblock_size
, memblock_start
;
753 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
754 memblock_start
= dump_filtered_memblock_start(block
, s
->filter_area_begin
, s
->filter_area_length
);
755 if (memblock_start
== -1) {
759 memblock_size
= dump_filtered_memblock_size(block
, s
->filter_area_begin
, s
->filter_area_length
);
761 /* Write the memory to file */
762 write_memory(s
, block
, memblock_start
, memblock_size
, errp
);
769 static void dump_end(DumpState
*s
, Error
**errp
)
773 if (s
->elf_section_data_size
) {
774 s
->elf_section_data
= g_malloc0(s
->elf_section_data_size
);
777 /* Adds the architecture defined section data to s->elf_section_data */
778 if (s
->dump_info
.arch_sections_write_fn
&&
779 s
->elf_section_data_size
) {
780 rc
= s
->dump_info
.arch_sections_write_fn(s
, s
->elf_section_data
);
782 error_setg_errno(errp
, rc
,
783 "dump: failed to get arch section data");
784 g_free(s
->elf_section_data
);
789 /* write sections to vmcore */
790 write_elf_sections(s
, errp
);
793 static void create_vmcore(DumpState
*s
, Error
**errp
)
802 /* Iterate over memory and dump it to file */
803 dump_iterate(s
, errp
);
808 /* Write the section data */
812 static int write_start_flat_header(DumpState
*s
)
814 MakedumpfileHeader
*mh
;
821 QEMU_BUILD_BUG_ON(sizeof *mh
> MAX_SIZE_MDF_HEADER
);
822 mh
= g_malloc0(MAX_SIZE_MDF_HEADER
);
824 memcpy(mh
->signature
, MAKEDUMPFILE_SIGNATURE
,
825 MIN(sizeof mh
->signature
, sizeof MAKEDUMPFILE_SIGNATURE
));
827 mh
->type
= cpu_to_be64(TYPE_FLAT_HEADER
);
828 mh
->version
= cpu_to_be64(VERSION_FLAT_HEADER
);
831 written_size
= qemu_write_full(s
->fd
, mh
, MAX_SIZE_MDF_HEADER
);
832 if (written_size
!= MAX_SIZE_MDF_HEADER
) {
840 static int write_end_flat_header(DumpState
*s
)
842 MakedumpfileDataHeader mdh
;
848 mdh
.offset
= END_FLAG_FLAT_HEADER
;
849 mdh
.buf_size
= END_FLAG_FLAT_HEADER
;
852 written_size
= qemu_write_full(s
->fd
, &mdh
, sizeof(mdh
));
853 if (written_size
!= sizeof(mdh
)) {
860 static int write_buffer(DumpState
*s
, off_t offset
, const void *buf
, size_t size
)
863 MakedumpfileDataHeader mdh
;
867 seek_loc
= lseek(s
->fd
, offset
, SEEK_SET
);
868 if (seek_loc
== (off_t
) -1) {
872 mdh
.offset
= cpu_to_be64(offset
);
873 mdh
.buf_size
= cpu_to_be64(size
);
875 written_size
= qemu_write_full(s
->fd
, &mdh
, sizeof(mdh
));
876 if (written_size
!= sizeof(mdh
)) {
881 written_size
= qemu_write_full(s
->fd
, buf
, size
);
882 if (written_size
!= size
) {
889 static int buf_write_note(const void *buf
, size_t size
, void *opaque
)
891 DumpState
*s
= opaque
;
893 /* note_buf is not enough */
894 if (s
->note_buf_offset
+ size
> s
->note_size
) {
898 memcpy(s
->note_buf
+ s
->note_buf_offset
, buf
, size
);
900 s
->note_buf_offset
+= size
;
906 * This function retrieves various sizes from an elf header.
908 * @note has to be a valid ELF note. The return sizes are unmodified
909 * (not padded or rounded up to be multiple of 4).
911 static void get_note_sizes(DumpState
*s
, const void *note
,
912 uint64_t *note_head_size
,
916 uint64_t note_head_sz
;
920 if (dump_is_64bit(s
)) {
921 const Elf64_Nhdr
*hdr
= note
;
922 note_head_sz
= sizeof(Elf64_Nhdr
);
923 name_sz
= cpu_to_dump64(s
, hdr
->n_namesz
);
924 desc_sz
= cpu_to_dump64(s
, hdr
->n_descsz
);
926 const Elf32_Nhdr
*hdr
= note
;
927 note_head_sz
= sizeof(Elf32_Nhdr
);
928 name_sz
= cpu_to_dump32(s
, hdr
->n_namesz
);
929 desc_sz
= cpu_to_dump32(s
, hdr
->n_descsz
);
932 if (note_head_size
) {
933 *note_head_size
= note_head_sz
;
936 *name_size
= name_sz
;
939 *desc_size
= desc_sz
;
943 static bool note_name_equal(DumpState
*s
,
944 const uint8_t *note
, const char *name
)
946 int len
= strlen(name
) + 1;
947 uint64_t head_size
, name_size
;
949 get_note_sizes(s
, note
, &head_size
, &name_size
, NULL
);
950 head_size
= ROUND_UP(head_size
, 4);
952 return name_size
== len
&& memcmp(note
+ head_size
, name
, len
) == 0;
955 /* write common header, sub header and elf note to vmcore */
956 static void create_header32(DumpState
*s
, Error
**errp
)
959 DiskDumpHeader32
*dh
= NULL
;
960 KdumpSubHeader32
*kh
= NULL
;
963 uint32_t sub_hdr_size
;
964 uint32_t bitmap_blocks
;
966 uint64_t offset_note
;
968 /* write common header, the version of kdump-compressed format is 6th */
969 size
= sizeof(DiskDumpHeader32
);
970 dh
= g_malloc0(size
);
972 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
973 dh
->header_version
= cpu_to_dump32(s
, 6);
974 block_size
= s
->dump_info
.page_size
;
975 dh
->block_size
= cpu_to_dump32(s
, block_size
);
976 sub_hdr_size
= sizeof(struct KdumpSubHeader32
) + s
->note_size
;
977 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
978 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
979 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
980 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
981 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
982 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
983 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
984 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
986 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
987 status
|= DUMP_DH_COMPRESSED_ZLIB
;
990 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
991 status
|= DUMP_DH_COMPRESSED_LZO
;
995 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
996 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
999 dh
->status
= cpu_to_dump32(s
, status
);
1001 if (write_buffer(s
, 0, dh
, size
) < 0) {
1002 error_setg(errp
, "dump: failed to write disk dump header");
1006 /* write sub header */
1007 size
= sizeof(KdumpSubHeader32
);
1008 kh
= g_malloc0(size
);
1010 /* 64bit max_mapnr_64 */
1011 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
1012 kh
->phys_base
= cpu_to_dump32(s
, s
->dump_info
.phys_base
);
1013 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
1015 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
1016 if (s
->guest_note
&&
1017 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1018 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
1020 get_note_sizes(s
, s
->guest_note
,
1021 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
1022 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
1023 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
1024 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
1025 kh
->size_vmcoreinfo
= cpu_to_dump32(s
, size_vmcoreinfo_desc
);
1028 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
1029 kh
->note_size
= cpu_to_dump32(s
, s
->note_size
);
1031 if (write_buffer(s
, DISKDUMP_HEADER_BLOCKS
*
1032 block_size
, kh
, size
) < 0) {
1033 error_setg(errp
, "dump: failed to write kdump sub header");
1038 s
->note_buf
= g_malloc0(s
->note_size
);
1039 s
->note_buf_offset
= 0;
1041 /* use s->note_buf to store notes temporarily */
1042 write_elf32_notes(buf_write_note
, s
, errp
);
1046 if (write_buffer(s
, offset_note
, s
->note_buf
,
1047 s
->note_size
) < 0) {
1048 error_setg(errp
, "dump: failed to write notes");
1052 /* get offset of dump_bitmap */
1053 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
1056 /* get offset of page */
1057 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
1063 g_free(s
->note_buf
);
1066 /* write common header, sub header and elf note to vmcore */
1067 static void create_header64(DumpState
*s
, Error
**errp
)
1070 DiskDumpHeader64
*dh
= NULL
;
1071 KdumpSubHeader64
*kh
= NULL
;
1073 uint32_t block_size
;
1074 uint32_t sub_hdr_size
;
1075 uint32_t bitmap_blocks
;
1076 uint32_t status
= 0;
1077 uint64_t offset_note
;
1079 /* write common header, the version of kdump-compressed format is 6th */
1080 size
= sizeof(DiskDumpHeader64
);
1081 dh
= g_malloc0(size
);
1083 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
1084 dh
->header_version
= cpu_to_dump32(s
, 6);
1085 block_size
= s
->dump_info
.page_size
;
1086 dh
->block_size
= cpu_to_dump32(s
, block_size
);
1087 sub_hdr_size
= sizeof(struct KdumpSubHeader64
) + s
->note_size
;
1088 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
1089 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
1090 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
1091 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
1092 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
1093 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
1094 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
1095 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
1097 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
1098 status
|= DUMP_DH_COMPRESSED_ZLIB
;
1101 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
1102 status
|= DUMP_DH_COMPRESSED_LZO
;
1105 #ifdef CONFIG_SNAPPY
1106 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
1107 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
1110 dh
->status
= cpu_to_dump32(s
, status
);
1112 if (write_buffer(s
, 0, dh
, size
) < 0) {
1113 error_setg(errp
, "dump: failed to write disk dump header");
1117 /* write sub header */
1118 size
= sizeof(KdumpSubHeader64
);
1119 kh
= g_malloc0(size
);
1121 /* 64bit max_mapnr_64 */
1122 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
1123 kh
->phys_base
= cpu_to_dump64(s
, s
->dump_info
.phys_base
);
1124 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
1126 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
1127 if (s
->guest_note
&&
1128 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1129 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
1131 get_note_sizes(s
, s
->guest_note
,
1132 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
1133 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
1134 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
1135 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
1136 kh
->size_vmcoreinfo
= cpu_to_dump64(s
, size_vmcoreinfo_desc
);
1139 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
1140 kh
->note_size
= cpu_to_dump64(s
, s
->note_size
);
1142 if (write_buffer(s
, DISKDUMP_HEADER_BLOCKS
*
1143 block_size
, kh
, size
) < 0) {
1144 error_setg(errp
, "dump: failed to write kdump sub header");
1149 s
->note_buf
= g_malloc0(s
->note_size
);
1150 s
->note_buf_offset
= 0;
1152 /* use s->note_buf to store notes temporarily */
1153 write_elf64_notes(buf_write_note
, s
, errp
);
1158 if (write_buffer(s
, offset_note
, s
->note_buf
,
1159 s
->note_size
) < 0) {
1160 error_setg(errp
, "dump: failed to write notes");
1164 /* get offset of dump_bitmap */
1165 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
1168 /* get offset of page */
1169 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
1175 g_free(s
->note_buf
);
1178 static void write_dump_header(DumpState
*s
, Error
**errp
)
1180 if (dump_is_64bit(s
)) {
1181 create_header64(s
, errp
);
1183 create_header32(s
, errp
);
1187 static size_t dump_bitmap_get_bufsize(DumpState
*s
)
1189 return s
->dump_info
.page_size
;
1193 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1194 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1195 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1196 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1197 * vmcore, ie. synchronizing un-sync bit into vmcore.
1199 static int set_dump_bitmap(uint64_t last_pfn
, uint64_t pfn
, bool value
,
1200 uint8_t *buf
, DumpState
*s
)
1202 off_t old_offset
, new_offset
;
1203 off_t offset_bitmap1
, offset_bitmap2
;
1205 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1206 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1208 /* should not set the previous place */
1209 assert(last_pfn
<= pfn
);
1212 * if the bit needed to be set is not cached in buf, flush the data in buf
1213 * to vmcore firstly.
1214 * making new_offset be bigger than old_offset can also sync remained data
1217 old_offset
= bitmap_bufsize
* (last_pfn
/ bits_per_buf
);
1218 new_offset
= bitmap_bufsize
* (pfn
/ bits_per_buf
);
1220 while (old_offset
< new_offset
) {
1221 /* calculate the offset and write dump_bitmap */
1222 offset_bitmap1
= s
->offset_dump_bitmap
+ old_offset
;
1223 if (write_buffer(s
, offset_bitmap1
, buf
,
1224 bitmap_bufsize
) < 0) {
1228 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1229 offset_bitmap2
= s
->offset_dump_bitmap
+ s
->len_dump_bitmap
+
1231 if (write_buffer(s
, offset_bitmap2
, buf
,
1232 bitmap_bufsize
) < 0) {
1236 memset(buf
, 0, bitmap_bufsize
);
1237 old_offset
+= bitmap_bufsize
;
1240 /* get the exact place of the bit in the buf, and set it */
1241 byte
= (pfn
% bits_per_buf
) / CHAR_BIT
;
1242 bit
= (pfn
% bits_per_buf
) % CHAR_BIT
;
1244 buf
[byte
] |= 1u << bit
;
1246 buf
[byte
] &= ~(1u << bit
);
1252 static uint64_t dump_paddr_to_pfn(DumpState
*s
, uint64_t addr
)
1254 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1256 return (addr
>> target_page_shift
) - ARCH_PFN_OFFSET
;
1259 static uint64_t dump_pfn_to_paddr(DumpState
*s
, uint64_t pfn
)
1261 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1263 return (pfn
+ ARCH_PFN_OFFSET
) << target_page_shift
;
1267 * Return the page frame number and the page content in *bufptr. bufptr can be
1268 * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
1269 * memory. This is not necessarily the memory returned.
1271 static bool get_next_page(GuestPhysBlock
**blockptr
, uint64_t *pfnptr
,
1272 uint8_t **bufptr
, DumpState
*s
)
1274 GuestPhysBlock
*block
= *blockptr
;
1275 uint32_t page_size
= s
->dump_info
.page_size
;
1276 uint8_t *buf
= NULL
, *hbuf
;
1279 /* block == NULL means the start of the iteration */
1281 block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1283 addr
= block
->target_start
;
1284 *pfnptr
= dump_paddr_to_pfn(s
, addr
);
1287 addr
= dump_pfn_to_paddr(s
, *pfnptr
);
1289 assert(block
!= NULL
);
1292 if (addr
>= block
->target_start
&& addr
< block
->target_end
) {
1293 size_t n
= MIN(block
->target_end
- addr
, page_size
- addr
% page_size
);
1294 hbuf
= block
->host_addr
+ (addr
- block
->target_start
);
1296 if (n
== page_size
) {
1297 /* this is a whole target page, go for it */
1298 assert(addr
% page_size
== 0);
1301 } else if (bufptr
) {
1304 memset(buf
, 0, page_size
);
1310 memcpy(buf
+ addr
% page_size
, hbuf
, n
);
1312 if (addr
% page_size
== 0 || addr
>= block
->target_end
) {
1313 /* we filled up the page or the current block is finished */
1317 /* the next page is in the next block */
1318 *blockptr
= block
= QTAILQ_NEXT(block
, next
);
1323 addr
= block
->target_start
;
1324 /* are we still in the same page? */
1325 if (dump_paddr_to_pfn(s
, addr
) != *pfnptr
) {
1327 /* no, but we already filled something earlier, return it */
1330 /* else continue from there */
1331 *pfnptr
= dump_paddr_to_pfn(s
, addr
);
1344 static void write_dump_bitmap(DumpState
*s
, Error
**errp
)
1347 uint64_t last_pfn
, pfn
;
1348 void *dump_bitmap_buf
;
1349 size_t num_dumpable
;
1350 GuestPhysBlock
*block_iter
= NULL
;
1351 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1352 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1354 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1355 dump_bitmap_buf
= g_malloc0(bitmap_bufsize
);
1361 * exam memory page by page, and set the bit in dump_bitmap corresponded
1362 * to the existing page.
1364 while (get_next_page(&block_iter
, &pfn
, NULL
, s
)) {
1365 ret
= set_dump_bitmap(last_pfn
, pfn
, true, dump_bitmap_buf
, s
);
1367 error_setg(errp
, "dump: failed to set dump_bitmap");
1376 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1377 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1378 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1380 if (num_dumpable
> 0) {
1381 ret
= set_dump_bitmap(last_pfn
, last_pfn
+ bits_per_buf
, false,
1382 dump_bitmap_buf
, s
);
1384 error_setg(errp
, "dump: failed to sync dump_bitmap");
1389 /* number of dumpable pages that will be dumped later */
1390 s
->num_dumpable
= num_dumpable
;
1393 g_free(dump_bitmap_buf
);
1396 static void prepare_data_cache(DataCache
*data_cache
, DumpState
*s
,
1399 data_cache
->state
= s
;
1400 data_cache
->data_size
= 0;
1401 data_cache
->buf_size
= 4 * dump_bitmap_get_bufsize(s
);
1402 data_cache
->buf
= g_malloc0(data_cache
->buf_size
);
1403 data_cache
->offset
= offset
;
1406 static int write_cache(DataCache
*dc
, const void *buf
, size_t size
,
1410 * dc->buf_size should not be less than size, otherwise dc will never be
1413 assert(size
<= dc
->buf_size
);
1416 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1417 * otherwise check if the space is enough for caching data in buf, if not,
1418 * write the data in dc->buf to dc->state->fd and reset dc->buf
1420 if ((!flag_sync
&& dc
->data_size
+ size
> dc
->buf_size
) ||
1421 (flag_sync
&& dc
->data_size
> 0)) {
1422 if (write_buffer(dc
->state
, dc
->offset
, dc
->buf
, dc
->data_size
) < 0) {
1426 dc
->offset
+= dc
->data_size
;
1431 memcpy(dc
->buf
+ dc
->data_size
, buf
, size
);
1432 dc
->data_size
+= size
;
1438 static void free_data_cache(DataCache
*data_cache
)
1440 g_free(data_cache
->buf
);
1443 static size_t get_len_buf_out(size_t page_size
, uint32_t flag_compress
)
1445 switch (flag_compress
) {
1446 case DUMP_DH_COMPRESSED_ZLIB
:
1447 return compressBound(page_size
);
1449 case DUMP_DH_COMPRESSED_LZO
:
1451 * LZO will expand incompressible data by a little amount. Please check
1452 * the following URL to see the expansion calculation:
1453 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1455 return page_size
+ page_size
/ 16 + 64 + 3;
1457 #ifdef CONFIG_SNAPPY
1458 case DUMP_DH_COMPRESSED_SNAPPY
:
1459 return snappy_max_compressed_length(page_size
);
1465 static void write_dump_pages(DumpState
*s
, Error
**errp
)
1468 DataCache page_desc
, page_data
;
1469 size_t len_buf_out
, size_out
;
1471 lzo_bytep wrkmem
= NULL
;
1473 uint8_t *buf_out
= NULL
;
1474 off_t offset_desc
, offset_data
;
1475 PageDescriptor pd
, pd_zero
;
1477 GuestPhysBlock
*block_iter
= NULL
;
1479 g_autofree
uint8_t *page
= NULL
;
1481 /* get offset of page_desc and page_data in dump file */
1482 offset_desc
= s
->offset_page
;
1483 offset_data
= offset_desc
+ sizeof(PageDescriptor
) * s
->num_dumpable
;
1485 prepare_data_cache(&page_desc
, s
, offset_desc
);
1486 prepare_data_cache(&page_data
, s
, offset_data
);
1488 /* prepare buffer to store compressed data */
1489 len_buf_out
= get_len_buf_out(s
->dump_info
.page_size
, s
->flag_compress
);
1490 assert(len_buf_out
!= 0);
1493 wrkmem
= g_malloc(LZO1X_1_MEM_COMPRESS
);
1496 buf_out
= g_malloc(len_buf_out
);
1499 * init zero page's page_desc and page_data, because every zero page
1500 * uses the same page_data
1502 pd_zero
.size
= cpu_to_dump32(s
, s
->dump_info
.page_size
);
1503 pd_zero
.flags
= cpu_to_dump32(s
, 0);
1504 pd_zero
.offset
= cpu_to_dump64(s
, offset_data
);
1505 pd_zero
.page_flags
= cpu_to_dump64(s
, 0);
1506 buf
= g_malloc0(s
->dump_info
.page_size
);
1507 ret
= write_cache(&page_data
, buf
, s
->dump_info
.page_size
, false);
1510 error_setg(errp
, "dump: failed to write page data (zero page)");
1514 offset_data
+= s
->dump_info
.page_size
;
1515 page
= g_malloc(s
->dump_info
.page_size
);
1518 * dump memory to vmcore page by page. zero page will all be resided in the
1519 * first page of page section
1521 for (buf
= page
; get_next_page(&block_iter
, &pfn_iter
, &buf
, s
); buf
= page
) {
1522 /* check zero page */
1523 if (buffer_is_zero(buf
, s
->dump_info
.page_size
)) {
1524 ret
= write_cache(&page_desc
, &pd_zero
, sizeof(PageDescriptor
),
1527 error_setg(errp
, "dump: failed to write page desc");
1532 * not zero page, then:
1533 * 1. compress the page
1534 * 2. write the compressed page into the cache of page_data
1535 * 3. get page desc of the compressed page and write it into the
1536 * cache of page_desc
1538 * only one compression format will be used here, for
1539 * s->flag_compress is set. But when compression fails to work,
1540 * we fall back to save in plaintext.
1542 size_out
= len_buf_out
;
1543 if ((s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) &&
1544 (compress2(buf_out
, (uLongf
*)&size_out
, buf
,
1545 s
->dump_info
.page_size
, Z_BEST_SPEED
) == Z_OK
) &&
1546 (size_out
< s
->dump_info
.page_size
)) {
1547 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_ZLIB
);
1548 pd
.size
= cpu_to_dump32(s
, size_out
);
1550 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1552 error_setg(errp
, "dump: failed to write page data");
1556 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) &&
1557 (lzo1x_1_compress(buf
, s
->dump_info
.page_size
, buf_out
,
1558 (lzo_uint
*)&size_out
, wrkmem
) == LZO_E_OK
) &&
1559 (size_out
< s
->dump_info
.page_size
)) {
1560 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_LZO
);
1561 pd
.size
= cpu_to_dump32(s
, size_out
);
1563 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1565 error_setg(errp
, "dump: failed to write page data");
1569 #ifdef CONFIG_SNAPPY
1570 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) &&
1571 (snappy_compress((char *)buf
, s
->dump_info
.page_size
,
1572 (char *)buf_out
, &size_out
) == SNAPPY_OK
) &&
1573 (size_out
< s
->dump_info
.page_size
)) {
1574 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_SNAPPY
);
1575 pd
.size
= cpu_to_dump32(s
, size_out
);
1577 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1579 error_setg(errp
, "dump: failed to write page data");
1585 * fall back to save in plaintext, size_out should be
1586 * assigned the target's page size
1588 pd
.flags
= cpu_to_dump32(s
, 0);
1589 size_out
= s
->dump_info
.page_size
;
1590 pd
.size
= cpu_to_dump32(s
, size_out
);
1592 ret
= write_cache(&page_data
, buf
,
1593 s
->dump_info
.page_size
, false);
1595 error_setg(errp
, "dump: failed to write page data");
1600 /* get and write page desc here */
1601 pd
.page_flags
= cpu_to_dump64(s
, 0);
1602 pd
.offset
= cpu_to_dump64(s
, offset_data
);
1603 offset_data
+= size_out
;
1605 ret
= write_cache(&page_desc
, &pd
, sizeof(PageDescriptor
), false);
1607 error_setg(errp
, "dump: failed to write page desc");
1611 s
->written_size
+= s
->dump_info
.page_size
;
1614 ret
= write_cache(&page_desc
, NULL
, 0, true);
1616 error_setg(errp
, "dump: failed to sync cache for page_desc");
1619 ret
= write_cache(&page_data
, NULL
, 0, true);
1621 error_setg(errp
, "dump: failed to sync cache for page_data");
1626 free_data_cache(&page_desc
);
1627 free_data_cache(&page_data
);
1636 static void create_kdump_vmcore(DumpState
*s
, Error
**errp
)
1642 * the kdump-compressed format is:
1644 * +------------------------------------------+ 0x0
1645 * | main header (struct disk_dump_header) |
1646 * |------------------------------------------+ block 1
1647 * | sub header (struct kdump_sub_header) |
1648 * |------------------------------------------+ block 2
1649 * | 1st-dump_bitmap |
1650 * |------------------------------------------+ block 2 + X blocks
1651 * | 2nd-dump_bitmap | (aligned by block)
1652 * |------------------------------------------+ block 2 + 2 * X blocks
1653 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1654 * | page desc for pfn 1 (struct page_desc) |
1656 * |------------------------------------------| (not aligned by block)
1657 * | page data (pfn 0) |
1658 * | page data (pfn 1) |
1660 * +------------------------------------------+
1663 ret
= write_start_flat_header(s
);
1665 error_setg(errp
, "dump: failed to write start flat header");
1669 write_dump_header(s
, errp
);
1674 write_dump_bitmap(s
, errp
);
1679 write_dump_pages(s
, errp
);
1684 ret
= write_end_flat_header(s
);
1686 error_setg(errp
, "dump: failed to write end flat header");
1691 static int validate_start_block(DumpState
*s
)
1693 GuestPhysBlock
*block
;
1695 if (!dump_has_filter(s
)) {
1699 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1700 /* This block is out of the range */
1701 if (block
->target_start
>= s
->filter_area_begin
+ s
->filter_area_length
||
1702 block
->target_end
<= s
->filter_area_begin
) {
1711 static void get_max_mapnr(DumpState
*s
)
1713 GuestPhysBlock
*last_block
;
1715 last_block
= QTAILQ_LAST(&s
->guest_phys_blocks
.head
);
1716 s
->max_mapnr
= dump_paddr_to_pfn(s
, last_block
->target_end
);
1719 static DumpState dump_state_global
= { .status
= DUMP_STATUS_NONE
};
1721 static void dump_state_prepare(DumpState
*s
)
1723 /* zero the struct, setting status to active */
1724 *s
= (DumpState
) { .status
= DUMP_STATUS_ACTIVE
};
1727 bool qemu_system_dump_in_progress(void)
1729 DumpState
*state
= &dump_state_global
;
1730 return (qatomic_read(&state
->status
) == DUMP_STATUS_ACTIVE
);
1734 * calculate total size of memory to be dumped (taking filter into
1737 static int64_t dump_calculate_size(DumpState
*s
)
1739 GuestPhysBlock
*block
;
1742 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1743 total
+= dump_filtered_memblock_size(block
,
1744 s
->filter_area_begin
,
1745 s
->filter_area_length
);
1751 static void vmcoreinfo_update_phys_base(DumpState
*s
)
1753 uint64_t size
, note_head_size
, name_size
, phys_base
;
1758 if (!note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1762 get_note_sizes(s
, s
->guest_note
, ¬e_head_size
, &name_size
, &size
);
1763 note_head_size
= ROUND_UP(note_head_size
, 4);
1765 vmci
= s
->guest_note
+ note_head_size
+ ROUND_UP(name_size
, 4);
1766 *(vmci
+ size
) = '\0';
1768 lines
= g_strsplit((char *)vmci
, "\n", -1);
1769 for (i
= 0; lines
[i
]; i
++) {
1770 const char *prefix
= NULL
;
1772 if (s
->dump_info
.d_machine
== EM_X86_64
) {
1773 prefix
= "NUMBER(phys_base)=";
1774 } else if (s
->dump_info
.d_machine
== EM_AARCH64
) {
1775 prefix
= "NUMBER(PHYS_OFFSET)=";
1778 if (prefix
&& g_str_has_prefix(lines
[i
], prefix
)) {
1779 if (qemu_strtou64(lines
[i
] + strlen(prefix
), NULL
, 16,
1781 warn_report("Failed to read %s", prefix
);
1783 s
->dump_info
.phys_base
= phys_base
;
1792 static void dump_init(DumpState
*s
, int fd
, bool has_format
,
1793 DumpGuestMemoryFormat format
, bool paging
, bool has_filter
,
1794 int64_t begin
, int64_t length
, bool kdump_raw
,
1798 VMCoreInfoState
*vmci
= vmcoreinfo_find();
1803 s
->has_format
= has_format
;
1805 s
->written_size
= 0;
1806 s
->kdump_raw
= kdump_raw
;
1808 /* kdump-compressed is conflict with paging and filter */
1809 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1810 assert(!paging
&& !has_filter
);
1813 if (runstate_is_running()) {
1814 vm_stop(RUN_STATE_SAVE_VM
);
1820 /* If we use KVM, we should synchronize the registers before we get dump
1821 * info or physmap info.
1823 cpu_synchronize_all_states();
1830 if (has_filter
&& !length
) {
1831 error_setg(errp
, QERR_INVALID_PARAMETER
, "length");
1834 s
->filter_area_begin
= begin
;
1835 s
->filter_area_length
= length
;
1837 /* First index is 0, it's the special null name */
1838 s
->string_table_buf
= g_array_new(FALSE
, TRUE
, 1);
1840 * Allocate the null name, due to the clearing option set to true
1843 g_array_set_size(s
->string_table_buf
, 1);
1845 memory_mapping_list_init(&s
->list
);
1847 guest_phys_blocks_init(&s
->guest_phys_blocks
);
1848 guest_phys_blocks_append(&s
->guest_phys_blocks
);
1849 s
->total_size
= dump_calculate_size(s
);
1850 #ifdef DEBUG_DUMP_GUEST_MEMORY
1851 fprintf(stderr
, "DUMP: total memory to dump: %lu\n", s
->total_size
);
1854 /* it does not make sense to dump non-existent memory */
1855 if (!s
->total_size
) {
1856 error_setg(errp
, "dump: no guest memory to dump");
1860 /* Is the filter filtering everything? */
1861 if (validate_start_block(s
) == -1) {
1862 error_setg(errp
, QERR_INVALID_PARAMETER
, "begin");
1866 /* get dump info: endian, class and architecture.
1867 * If the target architecture is not supported, cpu_get_dump_info() will
1870 ret
= cpu_get_dump_info(&s
->dump_info
, &s
->guest_phys_blocks
);
1873 "dumping guest memory is not supported on this target");
1877 if (!s
->dump_info
.page_size
) {
1878 s
->dump_info
.page_size
= qemu_target_page_size();
1881 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
1882 s
->dump_info
.d_machine
, nr_cpus
);
1883 assert(s
->note_size
>= 0);
1886 * The goal of this block is to (a) update the previously guessed
1887 * phys_base, (b) copy the guest note out of the guest.
1888 * Failure to do so is not fatal for dumping.
1891 uint64_t addr
, note_head_size
, name_size
, desc_size
;
1893 uint16_t guest_format
;
1895 note_head_size
= dump_is_64bit(s
) ?
1896 sizeof(Elf64_Nhdr
) : sizeof(Elf32_Nhdr
);
1898 guest_format
= le16_to_cpu(vmci
->vmcoreinfo
.guest_format
);
1899 size
= le32_to_cpu(vmci
->vmcoreinfo
.size
);
1900 addr
= le64_to_cpu(vmci
->vmcoreinfo
.paddr
);
1901 if (!vmci
->has_vmcoreinfo
) {
1902 warn_report("guest note is not present");
1903 } else if (size
< note_head_size
|| size
> MAX_GUEST_NOTE_SIZE
) {
1904 warn_report("guest note size is invalid: %" PRIu32
, size
);
1905 } else if (guest_format
!= FW_CFG_VMCOREINFO_FORMAT_ELF
) {
1906 warn_report("guest note format is unsupported: %" PRIu16
, guest_format
);
1908 s
->guest_note
= g_malloc(size
+ 1); /* +1 for adding \0 */
1909 cpu_physical_memory_read(addr
, s
->guest_note
, size
);
1911 get_note_sizes(s
, s
->guest_note
, NULL
, &name_size
, &desc_size
);
1912 s
->guest_note_size
= ELF_NOTE_SIZE(note_head_size
, name_size
,
1914 if (name_size
> MAX_GUEST_NOTE_SIZE
||
1915 desc_size
> MAX_GUEST_NOTE_SIZE
||
1916 s
->guest_note_size
> size
) {
1917 warn_report("Invalid guest note header");
1918 g_free(s
->guest_note
);
1919 s
->guest_note
= NULL
;
1921 vmcoreinfo_update_phys_base(s
);
1922 s
->note_size
+= s
->guest_note_size
;
1927 /* get memory mapping */
1929 qemu_get_guest_memory_mapping(&s
->list
, &s
->guest_phys_blocks
, errp
);
1934 qemu_get_guest_simple_memory_mapping(&s
->list
, &s
->guest_phys_blocks
);
1937 s
->nr_cpus
= nr_cpus
;
1942 tmp
= DIV_ROUND_UP(DIV_ROUND_UP(s
->max_mapnr
, CHAR_BIT
),
1943 s
->dump_info
.page_size
);
1944 s
->len_dump_bitmap
= tmp
* s
->dump_info
.page_size
;
1946 /* init for kdump-compressed format */
1947 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1949 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
:
1950 s
->flag_compress
= DUMP_DH_COMPRESSED_ZLIB
;
1953 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
:
1955 if (lzo_init() != LZO_E_OK
) {
1956 error_setg(errp
, "failed to initialize the LZO library");
1960 s
->flag_compress
= DUMP_DH_COMPRESSED_LZO
;
1963 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
:
1964 s
->flag_compress
= DUMP_DH_COMPRESSED_SNAPPY
;
1968 s
->flag_compress
= 0;
1974 if (dump_has_filter(s
)) {
1975 memory_mapping_filter(&s
->list
, s
->filter_area_begin
, s
->filter_area_length
);
1979 * The first section header is always a special one in which most
1980 * fields are 0. The section header string table is also always
1986 * Adds the number of architecture sections to shdr_num and sets
1987 * elf_section_data_size so we know the offsets and sizes of all
1990 if (s
->dump_info
.arch_sections_add_fn
) {
1991 s
->dump_info
.arch_sections_add_fn(s
);
1995 * calculate shdr_num so we know the offsets and sizes of all
1997 * Calculate phdr_num
1999 * The absolute maximum amount of phdrs is UINT32_MAX - 1 as
2000 * sh_info is 32 bit. There's special handling once we go over
2001 * UINT16_MAX - 1 but that is handled in the ehdr and section
2004 s
->phdr_num
= 1; /* Reserve PT_NOTE */
2005 if (s
->list
.num
<= UINT32_MAX
- 1) {
2006 s
->phdr_num
+= s
->list
.num
;
2008 s
->phdr_num
= UINT32_MAX
;
2012 * Now that the number of section and program headers is known we
2013 * can calculate the offsets of the headers and data.
2015 if (dump_is_64bit(s
)) {
2016 s
->shdr_offset
= sizeof(Elf64_Ehdr
);
2017 s
->phdr_offset
= s
->shdr_offset
+ sizeof(Elf64_Shdr
) * s
->shdr_num
;
2018 s
->note_offset
= s
->phdr_offset
+ sizeof(Elf64_Phdr
) * s
->phdr_num
;
2020 s
->shdr_offset
= sizeof(Elf32_Ehdr
);
2021 s
->phdr_offset
= s
->shdr_offset
+ sizeof(Elf32_Shdr
) * s
->shdr_num
;
2022 s
->note_offset
= s
->phdr_offset
+ sizeof(Elf32_Phdr
) * s
->phdr_num
;
2024 s
->memory_offset
= s
->note_offset
+ s
->note_size
;
2025 s
->section_offset
= s
->memory_offset
+ s
->total_size
;
2033 /* this operation might be time consuming. */
2034 static void dump_process(DumpState
*s
, Error
**errp
)
2037 DumpQueryResult
*result
= NULL
;
2039 if (s
->has_format
&& s
->format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
) {
2040 create_win_dump(s
, errp
);
2041 } else if (s
->has_format
&& s
->format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
2042 create_kdump_vmcore(s
, errp
);
2044 create_vmcore(s
, errp
);
2047 /* make sure status is written after written_size updates */
2049 qatomic_set(&s
->status
,
2050 (*errp
? DUMP_STATUS_FAILED
: DUMP_STATUS_COMPLETED
));
2052 /* send DUMP_COMPLETED message (unconditionally) */
2053 result
= qmp_query_dump(NULL
);
2054 /* should never fail */
2056 qapi_event_send_dump_completed(result
,
2057 *errp
? error_get_pretty(*errp
) : NULL
);
2058 qapi_free_DumpQueryResult(result
);
2063 static void *dump_thread(void *data
)
2065 DumpState
*s
= (DumpState
*)data
;
2066 dump_process(s
, NULL
);
2070 DumpQueryResult
*qmp_query_dump(Error
**errp
)
2072 DumpQueryResult
*result
= g_new(DumpQueryResult
, 1);
2073 DumpState
*state
= &dump_state_global
;
2074 result
->status
= qatomic_read(&state
->status
);
2075 /* make sure we are reading status and written_size in order */
2077 result
->completed
= state
->written_size
;
2078 result
->total
= state
->total_size
;
2082 void qmp_dump_guest_memory(bool paging
, const char *protocol
,
2083 bool has_detach
, bool detach
,
2084 bool has_begin
, int64_t begin
,
2085 bool has_length
, int64_t length
,
2086 bool has_format
, DumpGuestMemoryFormat format
,
2093 bool detach_p
= false;
2094 bool kdump_raw
= false;
2096 if (runstate_check(RUN_STATE_INMIGRATE
)) {
2097 error_setg(errp
, "Dump not allowed during incoming migration.");
2101 /* if there is a dump in background, we should wait until the dump
2103 if (qemu_system_dump_in_progress()) {
2104 error_setg(errp
, "There is a dump in process, please wait.");
2109 * externally, we represent kdump-raw-* as separate formats, but internally
2110 * they are handled the same, except for the "raw" flag
2114 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_ZLIB
:
2115 format
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
;
2118 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_LZO
:
2119 format
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
;
2122 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_SNAPPY
:
2123 format
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
;
2132 * kdump-compressed format need the whole memory dumped, so paging or
2133 * filter is not supported here.
2135 if ((has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) &&
2136 (paging
|| has_begin
|| has_length
)) {
2137 error_setg(errp
, "kdump-compressed format doesn't support paging or "
2141 if (has_begin
&& !has_length
) {
2142 error_setg(errp
, QERR_MISSING_PARAMETER
, "length");
2145 if (!has_begin
&& has_length
) {
2146 error_setg(errp
, QERR_MISSING_PARAMETER
, "begin");
2153 /* check whether lzo/snappy is supported */
2155 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
) {
2156 error_setg(errp
, "kdump-lzo is not available now");
2161 #ifndef CONFIG_SNAPPY
2162 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
) {
2163 error_setg(errp
, "kdump-snappy is not available now");
2168 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
2169 && !win_dump_available(errp
)) {
2174 if (strstart(protocol
, "fd:", &p
)) {
2175 fd
= monitor_get_fd(monitor_cur(), p
, errp
);
2182 if (strstart(protocol
, "file:", &p
)) {
2183 fd
= qemu_open_old(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
2185 error_setg_file_open(errp
, errno
, p
);
2191 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
2194 if (kdump_raw
&& lseek(fd
, 0, SEEK_CUR
) == (off_t
) -1) {
2195 error_setg(errp
, "kdump-raw formats require a seekable file");
2199 if (!dump_migration_blocker
) {
2200 error_setg(&dump_migration_blocker
,
2201 "Live migration disabled: dump-guest-memory in progress");
2205 * Allows even for -only-migratable, but forbid migration during the
2206 * process of dump guest memory.
2208 if (migrate_add_blocker_internal(&dump_migration_blocker
, errp
)) {
2209 /* Remember to release the fd before passing it over to dump state */
2214 s
= &dump_state_global
;
2215 dump_state_prepare(s
);
2217 dump_init(s
, fd
, has_format
, format
, paging
, has_begin
,
2218 begin
, length
, kdump_raw
, errp
);
2220 qatomic_set(&s
->status
, DUMP_STATUS_FAILED
);
2227 qemu_thread_create(&s
->dump_thread
, "dump_thread", dump_thread
,
2228 s
, QEMU_THREAD_DETACHED
);
2231 dump_process(s
, errp
);
2235 DumpGuestMemoryCapability
*qmp_query_dump_guest_memory_capability(Error
**errp
)
2237 DumpGuestMemoryCapability
*cap
=
2238 g_new0(DumpGuestMemoryCapability
, 1);
2239 DumpGuestMemoryFormatList
**tail
= &cap
->formats
;
2241 /* elf is always available */
2242 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_ELF
);
2244 /* kdump-zlib is always available */
2245 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
);
2246 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_ZLIB
);
2248 /* add new item if kdump-lzo is available */
2250 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
);
2251 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_LZO
);
2254 /* add new item if kdump-snappy is available */
2255 #ifdef CONFIG_SNAPPY
2256 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
);
2257 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_RAW_SNAPPY
);
2260 if (win_dump_available(NULL
)) {
2261 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
);