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/main-loop.h"
28 #include "hw/misc/vmcoreinfo.h"
29 #include "migration/blocker.h"
30 #include "hw/core/cpu.h"
35 #include <lzo/lzo1x.h>
40 #ifndef ELF_MACHINE_UNAME
41 #define ELF_MACHINE_UNAME "Unknown"
44 #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
46 static Error
*dump_migration_blocker
;
48 #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
49 ((DIV_ROUND_UP((hdr_size), 4) + \
50 DIV_ROUND_UP((name_size), 4) + \
51 DIV_ROUND_UP((desc_size), 4)) * 4)
53 static inline bool dump_is_64bit(DumpState
*s
)
55 return s
->dump_info
.d_class
== ELFCLASS64
;
58 static inline bool dump_has_filter(DumpState
*s
)
60 return s
->filter_area_length
> 0;
63 uint16_t cpu_to_dump16(DumpState
*s
, uint16_t val
)
65 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
66 val
= cpu_to_le16(val
);
68 val
= cpu_to_be16(val
);
74 uint32_t cpu_to_dump32(DumpState
*s
, uint32_t val
)
76 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
77 val
= cpu_to_le32(val
);
79 val
= cpu_to_be32(val
);
85 uint64_t cpu_to_dump64(DumpState
*s
, uint64_t val
)
87 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
88 val
= cpu_to_le64(val
);
90 val
= cpu_to_be64(val
);
96 static int dump_cleanup(DumpState
*s
)
98 guest_phys_blocks_free(&s
->guest_phys_blocks
);
99 memory_mapping_list_free(&s
->list
);
101 g_free(s
->guest_note
);
102 g_array_unref(s
->string_table_buf
);
103 s
->guest_note
= NULL
;
106 qemu_mutex_lock_iothread();
110 qemu_mutex_unlock_iothread();
113 migrate_del_blocker(dump_migration_blocker
);
118 static int fd_write_vmcore(const void *buf
, size_t size
, void *opaque
)
120 DumpState
*s
= opaque
;
123 written_size
= qemu_write_full(s
->fd
, buf
, size
);
124 if (written_size
!= size
) {
131 static void prepare_elf64_header(DumpState
*s
, Elf64_Ehdr
*elf_header
)
134 * phnum in the elf header is 16 bit, if we have more segments we
135 * set phnum to PN_XNUM and write the real number of segments to a
138 uint16_t phnum
= MIN(s
->phdr_num
, PN_XNUM
);
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
);
152 elf_header
->e_shoff
= cpu_to_dump64(s
, s
->shdr_offset
);
153 elf_header
->e_shentsize
= cpu_to_dump16(s
, sizeof(Elf64_Shdr
));
154 elf_header
->e_shnum
= cpu_to_dump16(s
, s
->shdr_num
);
155 elf_header
->e_shstrndx
= cpu_to_dump16(s
, s
->shdr_num
- 1);
158 static void prepare_elf32_header(DumpState
*s
, Elf32_Ehdr
*elf_header
)
161 * phnum in the elf header is 16 bit, if we have more segments we
162 * set phnum to PN_XNUM and write the real number of segments to a
165 uint16_t phnum
= MIN(s
->phdr_num
, PN_XNUM
);
167 memset(elf_header
, 0, sizeof(Elf32_Ehdr
));
168 memcpy(elf_header
, ELFMAG
, SELFMAG
);
169 elf_header
->e_ident
[EI_CLASS
] = ELFCLASS32
;
170 elf_header
->e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
171 elf_header
->e_ident
[EI_VERSION
] = EV_CURRENT
;
172 elf_header
->e_type
= cpu_to_dump16(s
, ET_CORE
);
173 elf_header
->e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
174 elf_header
->e_version
= cpu_to_dump32(s
, EV_CURRENT
);
175 elf_header
->e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
176 elf_header
->e_phoff
= cpu_to_dump32(s
, s
->phdr_offset
);
177 elf_header
->e_phentsize
= cpu_to_dump16(s
, sizeof(Elf32_Phdr
));
178 elf_header
->e_phnum
= cpu_to_dump16(s
, phnum
);
179 elf_header
->e_shoff
= cpu_to_dump32(s
, s
->shdr_offset
);
180 elf_header
->e_shentsize
= cpu_to_dump16(s
, sizeof(Elf32_Shdr
));
181 elf_header
->e_shnum
= cpu_to_dump16(s
, s
->shdr_num
);
182 elf_header
->e_shstrndx
= cpu_to_dump16(s
, s
->shdr_num
- 1);
185 static void write_elf_header(DumpState
*s
, Error
**errp
)
187 Elf32_Ehdr elf32_header
;
188 Elf64_Ehdr elf64_header
;
193 /* The NULL header and the shstrtab are always defined */
194 assert(s
->shdr_num
>= 2);
195 if (dump_is_64bit(s
)) {
196 prepare_elf64_header(s
, &elf64_header
);
197 header_size
= sizeof(elf64_header
);
198 header_ptr
= &elf64_header
;
200 prepare_elf32_header(s
, &elf32_header
);
201 header_size
= sizeof(elf32_header
);
202 header_ptr
= &elf32_header
;
205 ret
= fd_write_vmcore(header_ptr
, header_size
, s
);
207 error_setg_errno(errp
, -ret
, "dump: failed to write elf header");
211 static void write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
212 int phdr_index
, hwaddr offset
,
213 hwaddr filesz
, Error
**errp
)
218 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
219 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
220 phdr
.p_offset
= cpu_to_dump64(s
, offset
);
221 phdr
.p_paddr
= cpu_to_dump64(s
, memory_mapping
->phys_addr
);
222 phdr
.p_filesz
= cpu_to_dump64(s
, filesz
);
223 phdr
.p_memsz
= cpu_to_dump64(s
, memory_mapping
->length
);
224 phdr
.p_vaddr
= cpu_to_dump64(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
226 assert(memory_mapping
->length
>= filesz
);
228 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
230 error_setg_errno(errp
, -ret
,
231 "dump: failed to write program header table");
235 static void write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
236 int phdr_index
, hwaddr offset
,
237 hwaddr filesz
, Error
**errp
)
242 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
243 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
244 phdr
.p_offset
= cpu_to_dump32(s
, offset
);
245 phdr
.p_paddr
= cpu_to_dump32(s
, memory_mapping
->phys_addr
);
246 phdr
.p_filesz
= cpu_to_dump32(s
, filesz
);
247 phdr
.p_memsz
= cpu_to_dump32(s
, memory_mapping
->length
);
249 cpu_to_dump32(s
, memory_mapping
->virt_addr
) ?: phdr
.p_paddr
;
251 assert(memory_mapping
->length
>= filesz
);
253 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
255 error_setg_errno(errp
, -ret
,
256 "dump: failed to write program header table");
260 static void prepare_elf64_phdr_note(DumpState
*s
, Elf64_Phdr
*phdr
)
262 memset(phdr
, 0, sizeof(*phdr
));
263 phdr
->p_type
= cpu_to_dump32(s
, PT_NOTE
);
264 phdr
->p_offset
= cpu_to_dump64(s
, s
->note_offset
);
266 phdr
->p_filesz
= cpu_to_dump64(s
, s
->note_size
);
267 phdr
->p_memsz
= cpu_to_dump64(s
, s
->note_size
);
271 static inline int cpu_index(CPUState
*cpu
)
273 return cpu
->cpu_index
+ 1;
276 static void write_guest_note(WriteCoreDumpFunction f
, DumpState
*s
,
282 ret
= f(s
->guest_note
, s
->guest_note_size
, s
);
284 error_setg(errp
, "dump: failed to write guest note");
289 static void write_elf64_notes(WriteCoreDumpFunction f
, DumpState
*s
,
298 ret
= cpu_write_elf64_note(f
, cpu
, id
, s
);
300 error_setg(errp
, "dump: failed to write elf notes");
306 ret
= cpu_write_elf64_qemunote(f
, cpu
, s
);
308 error_setg(errp
, "dump: failed to write CPU status");
313 write_guest_note(f
, s
, errp
);
316 static void prepare_elf32_phdr_note(DumpState
*s
, Elf32_Phdr
*phdr
)
318 memset(phdr
, 0, sizeof(*phdr
));
319 phdr
->p_type
= cpu_to_dump32(s
, PT_NOTE
);
320 phdr
->p_offset
= cpu_to_dump32(s
, s
->note_offset
);
322 phdr
->p_filesz
= cpu_to_dump32(s
, s
->note_size
);
323 phdr
->p_memsz
= cpu_to_dump32(s
, s
->note_size
);
327 static void write_elf32_notes(WriteCoreDumpFunction f
, DumpState
*s
,
336 ret
= cpu_write_elf32_note(f
, cpu
, id
, s
);
338 error_setg(errp
, "dump: failed to write elf notes");
344 ret
= cpu_write_elf32_qemunote(f
, cpu
, s
);
346 error_setg(errp
, "dump: failed to write CPU status");
351 write_guest_note(f
, s
, errp
);
354 static void write_elf_phdr_note(DumpState
*s
, Error
**errp
)
362 if (dump_is_64bit(s
)) {
363 prepare_elf64_phdr_note(s
, &phdr64
);
364 size
= sizeof(phdr64
);
367 prepare_elf32_phdr_note(s
, &phdr32
);
368 size
= sizeof(phdr32
);
372 ret
= fd_write_vmcore(phdr
, size
, s
);
374 error_setg_errno(errp
, -ret
,
375 "dump: failed to write program header table");
379 static void prepare_elf_section_hdr_zero(DumpState
*s
)
381 if (dump_is_64bit(s
)) {
382 Elf64_Shdr
*shdr64
= s
->elf_section_hdrs
;
384 shdr64
->sh_info
= cpu_to_dump32(s
, s
->phdr_num
);
386 Elf32_Shdr
*shdr32
= s
->elf_section_hdrs
;
388 shdr32
->sh_info
= cpu_to_dump32(s
, s
->phdr_num
);
392 static void prepare_elf_section_hdr_string(DumpState
*s
, void *buff
)
394 uint64_t index
= s
->string_table_buf
->len
;
395 const char strtab
[] = ".shstrtab";
396 Elf32_Shdr shdr32
= {};
397 Elf64_Shdr shdr64
= {};
401 g_array_append_vals(s
->string_table_buf
, strtab
, sizeof(strtab
));
402 if (dump_is_64bit(s
)) {
403 shdr_size
= sizeof(Elf64_Shdr
);
404 shdr64
.sh_type
= SHT_STRTAB
;
405 shdr64
.sh_offset
= s
->section_offset
+ s
->elf_section_data_size
;
406 shdr64
.sh_name
= index
;
407 shdr64
.sh_size
= s
->string_table_buf
->len
;
410 shdr_size
= sizeof(Elf32_Shdr
);
411 shdr32
.sh_type
= SHT_STRTAB
;
412 shdr32
.sh_offset
= s
->section_offset
+ s
->elf_section_data_size
;
413 shdr32
.sh_name
= index
;
414 shdr32
.sh_size
= s
->string_table_buf
->len
;
417 memcpy(buff
, shdr
, shdr_size
);
420 static bool prepare_elf_section_hdrs(DumpState
*s
, Error
**errp
)
422 size_t len
, sizeof_shdr
;
428 * - Arch section hdrs
431 sizeof_shdr
= dump_is_64bit(s
) ? sizeof(Elf64_Shdr
) : sizeof(Elf32_Shdr
);
432 len
= sizeof_shdr
* s
->shdr_num
;
433 s
->elf_section_hdrs
= g_malloc0(len
);
434 buff_hdr
= s
->elf_section_hdrs
;
437 * The first section header is ALWAYS a special initial section
440 * The header should be 0 with one exception being that if
441 * phdr_num is PN_XNUM then the sh_info field contains the real
442 * number of segment entries.
444 * As we zero allocate the buffer we will only need to modify
445 * sh_info for the PN_XNUM case.
447 if (s
->phdr_num
>= PN_XNUM
) {
448 prepare_elf_section_hdr_zero(s
);
450 buff_hdr
+= sizeof_shdr
;
452 /* Add architecture defined section headers */
453 if (s
->dump_info
.arch_sections_write_hdr_fn
454 && s
->shdr_num
> 2) {
455 buff_hdr
+= s
->dump_info
.arch_sections_write_hdr_fn(s
, buff_hdr
);
457 if (s
->shdr_num
>= SHN_LORESERVE
) {
458 error_setg_errno(errp
, EINVAL
,
459 "dump: too many architecture defined sections");
465 * String table is the last section since strings are added via
466 * arch_sections_write_hdr().
468 prepare_elf_section_hdr_string(s
, buff_hdr
);
472 static void write_elf_section_headers(DumpState
*s
, Error
**errp
)
474 size_t sizeof_shdr
= dump_is_64bit(s
) ? sizeof(Elf64_Shdr
) : sizeof(Elf32_Shdr
);
477 if (!prepare_elf_section_hdrs(s
, errp
)) {
481 ret
= fd_write_vmcore(s
->elf_section_hdrs
, s
->shdr_num
* sizeof_shdr
, s
);
483 error_setg_errno(errp
, -ret
, "dump: failed to write section headers");
486 g_free(s
->elf_section_hdrs
);
489 static void write_elf_sections(DumpState
*s
, Error
**errp
)
493 if (s
->elf_section_data_size
) {
494 /* Write architecture section data */
495 ret
= fd_write_vmcore(s
->elf_section_data
,
496 s
->elf_section_data_size
, s
);
498 error_setg_errno(errp
, -ret
,
499 "dump: failed to write architecture section data");
504 /* Write string table */
505 ret
= fd_write_vmcore(s
->string_table_buf
->data
,
506 s
->string_table_buf
->len
, s
);
508 error_setg_errno(errp
, -ret
, "dump: failed to write string table data");
512 static void write_data(DumpState
*s
, void *buf
, int length
, Error
**errp
)
516 ret
= fd_write_vmcore(buf
, length
, s
);
518 error_setg_errno(errp
, -ret
, "dump: failed to save memory");
520 s
->written_size
+= length
;
524 /* write the memory to vmcore. 1 page per I/O. */
525 static void write_memory(DumpState
*s
, GuestPhysBlock
*block
, ram_addr_t start
,
526 int64_t size
, Error
**errp
)
531 for (i
= 0; i
< size
/ s
->dump_info
.page_size
; i
++) {
532 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
533 s
->dump_info
.page_size
, errp
);
539 if ((size
% s
->dump_info
.page_size
) != 0) {
540 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
541 size
% s
->dump_info
.page_size
, errp
);
548 /* get the memory's offset and size in the vmcore */
549 static void get_offset_range(hwaddr phys_addr
,
550 ram_addr_t mapping_length
,
555 GuestPhysBlock
*block
;
556 hwaddr offset
= s
->memory_offset
;
557 int64_t size_in_block
, start
;
559 /* When the memory is not stored into vmcore, offset will be -1 */
563 if (dump_has_filter(s
)) {
564 if (phys_addr
< s
->filter_area_begin
||
565 phys_addr
>= s
->filter_area_begin
+ s
->filter_area_length
) {
570 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
571 if (dump_has_filter(s
)) {
572 if (block
->target_start
>= s
->filter_area_begin
+ s
->filter_area_length
||
573 block
->target_end
<= s
->filter_area_begin
) {
574 /* This block is out of the range */
578 if (s
->filter_area_begin
<= block
->target_start
) {
579 start
= block
->target_start
;
581 start
= s
->filter_area_begin
;
584 size_in_block
= block
->target_end
- start
;
585 if (s
->filter_area_begin
+ s
->filter_area_length
< block
->target_end
) {
586 size_in_block
-= block
->target_end
- (s
->filter_area_begin
+ s
->filter_area_length
);
589 start
= block
->target_start
;
590 size_in_block
= block
->target_end
- block
->target_start
;
593 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
594 *p_offset
= phys_addr
- start
+ offset
;
596 /* The offset range mapped from the vmcore file must not spill over
597 * the GuestPhysBlock, clamp it. The rest of the mapping will be
598 * zero-filled in memory at load time; see
599 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
601 *p_filesz
= phys_addr
+ mapping_length
<= start
+ size_in_block
?
603 size_in_block
- (phys_addr
- start
);
607 offset
+= size_in_block
;
611 static void write_elf_phdr_loads(DumpState
*s
, Error
**errp
)
614 hwaddr offset
, filesz
;
615 MemoryMapping
*memory_mapping
;
616 uint32_t phdr_index
= 1;
618 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
619 get_offset_range(memory_mapping
->phys_addr
,
620 memory_mapping
->length
,
621 s
, &offset
, &filesz
);
622 if (dump_is_64bit(s
)) {
623 write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
,
626 write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
,
634 if (phdr_index
>= s
->phdr_num
) {
640 static void write_elf_notes(DumpState
*s
, Error
**errp
)
642 if (dump_is_64bit(s
)) {
643 write_elf64_notes(fd_write_vmcore
, s
, errp
);
645 write_elf32_notes(fd_write_vmcore
, s
, errp
);
649 /* write elf header, PT_NOTE and elf note to vmcore. */
650 static void dump_begin(DumpState
*s
, Error
**errp
)
655 * the vmcore's format is:
674 * we only know where the memory is saved after we write elf note into
678 /* write elf header to vmcore */
679 write_elf_header(s
, errp
);
684 /* write section headers to vmcore */
685 write_elf_section_headers(s
, errp
);
690 /* write PT_NOTE to vmcore */
691 write_elf_phdr_note(s
, errp
);
696 /* write all PT_LOADs to vmcore */
697 write_elf_phdr_loads(s
, errp
);
702 /* write notes to vmcore */
703 write_elf_notes(s
, errp
);
706 int64_t dump_filtered_memblock_size(GuestPhysBlock
*block
,
707 int64_t filter_area_start
,
708 int64_t filter_area_length
)
710 int64_t size
, left
, right
;
712 /* No filter, return full size */
713 if (!filter_area_length
) {
714 return block
->target_end
- block
->target_start
;
717 /* calculate the overlapped region. */
718 left
= MAX(filter_area_start
, block
->target_start
);
719 right
= MIN(filter_area_start
+ filter_area_length
, block
->target_end
);
721 size
= size
> 0 ? size
: 0;
726 int64_t dump_filtered_memblock_start(GuestPhysBlock
*block
,
727 int64_t filter_area_start
,
728 int64_t filter_area_length
)
730 if (filter_area_length
) {
731 /* return -1 if the block is not within filter area */
732 if (block
->target_start
>= filter_area_start
+ filter_area_length
||
733 block
->target_end
<= filter_area_start
) {
737 if (filter_area_start
> block
->target_start
) {
738 return filter_area_start
- block
->target_start
;
745 /* write all memory to vmcore */
746 static void dump_iterate(DumpState
*s
, Error
**errp
)
749 GuestPhysBlock
*block
;
750 int64_t memblock_size
, memblock_start
;
752 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
753 memblock_start
= dump_filtered_memblock_start(block
, s
->filter_area_begin
, s
->filter_area_length
);
754 if (memblock_start
== -1) {
758 memblock_size
= dump_filtered_memblock_size(block
, s
->filter_area_begin
, s
->filter_area_length
);
760 /* Write the memory to file */
761 write_memory(s
, block
, memblock_start
, memblock_size
, errp
);
768 static void dump_end(DumpState
*s
, Error
**errp
)
772 if (s
->elf_section_data_size
) {
773 s
->elf_section_data
= g_malloc0(s
->elf_section_data_size
);
776 /* Adds the architecture defined section data to s->elf_section_data */
777 if (s
->dump_info
.arch_sections_write_fn
&&
778 s
->elf_section_data_size
) {
779 rc
= s
->dump_info
.arch_sections_write_fn(s
, s
->elf_section_data
);
781 error_setg_errno(errp
, rc
,
782 "dump: failed to get arch section data");
783 g_free(s
->elf_section_data
);
788 /* write sections to vmcore */
789 write_elf_sections(s
, errp
);
792 static void create_vmcore(DumpState
*s
, Error
**errp
)
801 /* Iterate over memory and dump it to file */
802 dump_iterate(s
, errp
);
807 /* Write the section data */
811 static int write_start_flat_header(int fd
)
813 MakedumpfileHeader
*mh
;
816 QEMU_BUILD_BUG_ON(sizeof *mh
> MAX_SIZE_MDF_HEADER
);
817 mh
= g_malloc0(MAX_SIZE_MDF_HEADER
);
819 memcpy(mh
->signature
, MAKEDUMPFILE_SIGNATURE
,
820 MIN(sizeof mh
->signature
, sizeof MAKEDUMPFILE_SIGNATURE
));
822 mh
->type
= cpu_to_be64(TYPE_FLAT_HEADER
);
823 mh
->version
= cpu_to_be64(VERSION_FLAT_HEADER
);
826 written_size
= qemu_write_full(fd
, mh
, MAX_SIZE_MDF_HEADER
);
827 if (written_size
!= MAX_SIZE_MDF_HEADER
) {
835 static int write_end_flat_header(int fd
)
837 MakedumpfileDataHeader mdh
;
839 mdh
.offset
= END_FLAG_FLAT_HEADER
;
840 mdh
.buf_size
= END_FLAG_FLAT_HEADER
;
843 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
844 if (written_size
!= sizeof(mdh
)) {
851 static int write_buffer(int fd
, off_t offset
, const void *buf
, size_t size
)
854 MakedumpfileDataHeader mdh
;
856 mdh
.offset
= cpu_to_be64(offset
);
857 mdh
.buf_size
= cpu_to_be64(size
);
859 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
860 if (written_size
!= sizeof(mdh
)) {
864 written_size
= qemu_write_full(fd
, buf
, size
);
865 if (written_size
!= size
) {
872 static int buf_write_note(const void *buf
, size_t size
, void *opaque
)
874 DumpState
*s
= opaque
;
876 /* note_buf is not enough */
877 if (s
->note_buf_offset
+ size
> s
->note_size
) {
881 memcpy(s
->note_buf
+ s
->note_buf_offset
, buf
, size
);
883 s
->note_buf_offset
+= size
;
889 * This function retrieves various sizes from an elf header.
891 * @note has to be a valid ELF note. The return sizes are unmodified
892 * (not padded or rounded up to be multiple of 4).
894 static void get_note_sizes(DumpState
*s
, const void *note
,
895 uint64_t *note_head_size
,
899 uint64_t note_head_sz
;
903 if (dump_is_64bit(s
)) {
904 const Elf64_Nhdr
*hdr
= note
;
905 note_head_sz
= sizeof(Elf64_Nhdr
);
906 name_sz
= cpu_to_dump64(s
, hdr
->n_namesz
);
907 desc_sz
= cpu_to_dump64(s
, hdr
->n_descsz
);
909 const Elf32_Nhdr
*hdr
= note
;
910 note_head_sz
= sizeof(Elf32_Nhdr
);
911 name_sz
= cpu_to_dump32(s
, hdr
->n_namesz
);
912 desc_sz
= cpu_to_dump32(s
, hdr
->n_descsz
);
915 if (note_head_size
) {
916 *note_head_size
= note_head_sz
;
919 *name_size
= name_sz
;
922 *desc_size
= desc_sz
;
926 static bool note_name_equal(DumpState
*s
,
927 const uint8_t *note
, const char *name
)
929 int len
= strlen(name
) + 1;
930 uint64_t head_size
, name_size
;
932 get_note_sizes(s
, note
, &head_size
, &name_size
, NULL
);
933 head_size
= ROUND_UP(head_size
, 4);
935 return name_size
== len
&& memcmp(note
+ head_size
, name
, len
) == 0;
938 /* write common header, sub header and elf note to vmcore */
939 static void create_header32(DumpState
*s
, Error
**errp
)
942 DiskDumpHeader32
*dh
= NULL
;
943 KdumpSubHeader32
*kh
= NULL
;
946 uint32_t sub_hdr_size
;
947 uint32_t bitmap_blocks
;
949 uint64_t offset_note
;
951 /* write common header, the version of kdump-compressed format is 6th */
952 size
= sizeof(DiskDumpHeader32
);
953 dh
= g_malloc0(size
);
955 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
956 dh
->header_version
= cpu_to_dump32(s
, 6);
957 block_size
= s
->dump_info
.page_size
;
958 dh
->block_size
= cpu_to_dump32(s
, block_size
);
959 sub_hdr_size
= sizeof(struct KdumpSubHeader32
) + s
->note_size
;
960 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
961 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
962 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
963 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
964 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
965 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
966 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
967 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
969 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
970 status
|= DUMP_DH_COMPRESSED_ZLIB
;
973 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
974 status
|= DUMP_DH_COMPRESSED_LZO
;
978 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
979 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
982 dh
->status
= cpu_to_dump32(s
, status
);
984 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
985 error_setg(errp
, "dump: failed to write disk dump header");
989 /* write sub header */
990 size
= sizeof(KdumpSubHeader32
);
991 kh
= g_malloc0(size
);
993 /* 64bit max_mapnr_64 */
994 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
995 kh
->phys_base
= cpu_to_dump32(s
, s
->dump_info
.phys_base
);
996 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
998 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
1000 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1001 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
1003 get_note_sizes(s
, s
->guest_note
,
1004 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
1005 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
1006 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
1007 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
1008 kh
->size_vmcoreinfo
= cpu_to_dump32(s
, size_vmcoreinfo_desc
);
1011 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
1012 kh
->note_size
= cpu_to_dump32(s
, s
->note_size
);
1014 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
1015 block_size
, kh
, size
) < 0) {
1016 error_setg(errp
, "dump: failed to write kdump sub header");
1021 s
->note_buf
= g_malloc0(s
->note_size
);
1022 s
->note_buf_offset
= 0;
1024 /* use s->note_buf to store notes temporarily */
1025 write_elf32_notes(buf_write_note
, s
, errp
);
1029 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
1030 s
->note_size
) < 0) {
1031 error_setg(errp
, "dump: failed to write notes");
1035 /* get offset of dump_bitmap */
1036 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
1039 /* get offset of page */
1040 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
1046 g_free(s
->note_buf
);
1049 /* write common header, sub header and elf note to vmcore */
1050 static void create_header64(DumpState
*s
, Error
**errp
)
1053 DiskDumpHeader64
*dh
= NULL
;
1054 KdumpSubHeader64
*kh
= NULL
;
1056 uint32_t block_size
;
1057 uint32_t sub_hdr_size
;
1058 uint32_t bitmap_blocks
;
1059 uint32_t status
= 0;
1060 uint64_t offset_note
;
1062 /* write common header, the version of kdump-compressed format is 6th */
1063 size
= sizeof(DiskDumpHeader64
);
1064 dh
= g_malloc0(size
);
1066 memcpy(dh
->signature
, KDUMP_SIGNATURE
, SIG_LEN
);
1067 dh
->header_version
= cpu_to_dump32(s
, 6);
1068 block_size
= s
->dump_info
.page_size
;
1069 dh
->block_size
= cpu_to_dump32(s
, block_size
);
1070 sub_hdr_size
= sizeof(struct KdumpSubHeader64
) + s
->note_size
;
1071 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
1072 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
1073 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
1074 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
1075 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
1076 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
1077 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
1078 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
1080 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
1081 status
|= DUMP_DH_COMPRESSED_ZLIB
;
1084 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
1085 status
|= DUMP_DH_COMPRESSED_LZO
;
1088 #ifdef CONFIG_SNAPPY
1089 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
1090 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
1093 dh
->status
= cpu_to_dump32(s
, status
);
1095 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
1096 error_setg(errp
, "dump: failed to write disk dump header");
1100 /* write sub header */
1101 size
= sizeof(KdumpSubHeader64
);
1102 kh
= g_malloc0(size
);
1104 /* 64bit max_mapnr_64 */
1105 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
1106 kh
->phys_base
= cpu_to_dump64(s
, s
->dump_info
.phys_base
);
1107 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
1109 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
1110 if (s
->guest_note
&&
1111 note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1112 uint64_t hsize
, name_size
, size_vmcoreinfo_desc
, offset_vmcoreinfo
;
1114 get_note_sizes(s
, s
->guest_note
,
1115 &hsize
, &name_size
, &size_vmcoreinfo_desc
);
1116 offset_vmcoreinfo
= offset_note
+ s
->note_size
- s
->guest_note_size
+
1117 (DIV_ROUND_UP(hsize
, 4) + DIV_ROUND_UP(name_size
, 4)) * 4;
1118 kh
->offset_vmcoreinfo
= cpu_to_dump64(s
, offset_vmcoreinfo
);
1119 kh
->size_vmcoreinfo
= cpu_to_dump64(s
, size_vmcoreinfo_desc
);
1122 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
1123 kh
->note_size
= cpu_to_dump64(s
, s
->note_size
);
1125 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
1126 block_size
, kh
, size
) < 0) {
1127 error_setg(errp
, "dump: failed to write kdump sub header");
1132 s
->note_buf
= g_malloc0(s
->note_size
);
1133 s
->note_buf_offset
= 0;
1135 /* use s->note_buf to store notes temporarily */
1136 write_elf64_notes(buf_write_note
, s
, errp
);
1141 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
1142 s
->note_size
) < 0) {
1143 error_setg(errp
, "dump: failed to write notes");
1147 /* get offset of dump_bitmap */
1148 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
1151 /* get offset of page */
1152 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
1158 g_free(s
->note_buf
);
1161 static void write_dump_header(DumpState
*s
, Error
**errp
)
1163 if (dump_is_64bit(s
)) {
1164 create_header64(s
, errp
);
1166 create_header32(s
, errp
);
1170 static size_t dump_bitmap_get_bufsize(DumpState
*s
)
1172 return s
->dump_info
.page_size
;
1176 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1177 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1178 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1179 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1180 * vmcore, ie. synchronizing un-sync bit into vmcore.
1182 static int set_dump_bitmap(uint64_t last_pfn
, uint64_t pfn
, bool value
,
1183 uint8_t *buf
, DumpState
*s
)
1185 off_t old_offset
, new_offset
;
1186 off_t offset_bitmap1
, offset_bitmap2
;
1188 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1189 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1191 /* should not set the previous place */
1192 assert(last_pfn
<= pfn
);
1195 * if the bit needed to be set is not cached in buf, flush the data in buf
1196 * to vmcore firstly.
1197 * making new_offset be bigger than old_offset can also sync remained data
1200 old_offset
= bitmap_bufsize
* (last_pfn
/ bits_per_buf
);
1201 new_offset
= bitmap_bufsize
* (pfn
/ bits_per_buf
);
1203 while (old_offset
< new_offset
) {
1204 /* calculate the offset and write dump_bitmap */
1205 offset_bitmap1
= s
->offset_dump_bitmap
+ old_offset
;
1206 if (write_buffer(s
->fd
, offset_bitmap1
, buf
,
1207 bitmap_bufsize
) < 0) {
1211 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1212 offset_bitmap2
= s
->offset_dump_bitmap
+ s
->len_dump_bitmap
+
1214 if (write_buffer(s
->fd
, offset_bitmap2
, buf
,
1215 bitmap_bufsize
) < 0) {
1219 memset(buf
, 0, bitmap_bufsize
);
1220 old_offset
+= bitmap_bufsize
;
1223 /* get the exact place of the bit in the buf, and set it */
1224 byte
= (pfn
% bits_per_buf
) / CHAR_BIT
;
1225 bit
= (pfn
% bits_per_buf
) % CHAR_BIT
;
1227 buf
[byte
] |= 1u << bit
;
1229 buf
[byte
] &= ~(1u << bit
);
1235 static uint64_t dump_paddr_to_pfn(DumpState
*s
, uint64_t addr
)
1237 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1239 return (addr
>> target_page_shift
) - ARCH_PFN_OFFSET
;
1242 static uint64_t dump_pfn_to_paddr(DumpState
*s
, uint64_t pfn
)
1244 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1246 return (pfn
+ ARCH_PFN_OFFSET
) << target_page_shift
;
1250 * Return the page frame number and the page content in *bufptr. bufptr can be
1251 * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
1252 * memory. This is not necessarily the memory returned.
1254 static bool get_next_page(GuestPhysBlock
**blockptr
, uint64_t *pfnptr
,
1255 uint8_t **bufptr
, DumpState
*s
)
1257 GuestPhysBlock
*block
= *blockptr
;
1258 uint32_t page_size
= s
->dump_info
.page_size
;
1259 uint8_t *buf
= NULL
, *hbuf
;
1262 /* block == NULL means the start of the iteration */
1264 block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1266 addr
= block
->target_start
;
1267 *pfnptr
= dump_paddr_to_pfn(s
, addr
);
1270 addr
= dump_pfn_to_paddr(s
, *pfnptr
);
1272 assert(block
!= NULL
);
1275 if (addr
>= block
->target_start
&& addr
< block
->target_end
) {
1276 size_t n
= MIN(block
->target_end
- addr
, page_size
- addr
% page_size
);
1277 hbuf
= block
->host_addr
+ (addr
- block
->target_start
);
1279 if (n
== page_size
) {
1280 /* this is a whole target page, go for it */
1281 assert(addr
% page_size
== 0);
1284 } else if (bufptr
) {
1287 memset(buf
, 0, page_size
);
1293 memcpy(buf
+ addr
% page_size
, hbuf
, n
);
1295 if (addr
% page_size
== 0) {
1296 /* we filled up the page */
1300 /* the next page is in the next block */
1301 *blockptr
= block
= QTAILQ_NEXT(block
, next
);
1306 addr
= block
->target_start
;
1307 /* are we still in the same page? */
1308 if (dump_paddr_to_pfn(s
, addr
) != *pfnptr
) {
1310 /* no, but we already filled something earlier, return it */
1313 /* else continue from there */
1314 *pfnptr
= dump_paddr_to_pfn(s
, addr
);
1327 static void write_dump_bitmap(DumpState
*s
, Error
**errp
)
1330 uint64_t last_pfn
, pfn
;
1331 void *dump_bitmap_buf
;
1332 size_t num_dumpable
;
1333 GuestPhysBlock
*block_iter
= NULL
;
1334 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1335 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1337 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1338 dump_bitmap_buf
= g_malloc0(bitmap_bufsize
);
1344 * exam memory page by page, and set the bit in dump_bitmap corresponded
1345 * to the existing page.
1347 while (get_next_page(&block_iter
, &pfn
, NULL
, s
)) {
1348 ret
= set_dump_bitmap(last_pfn
, pfn
, true, dump_bitmap_buf
, s
);
1350 error_setg(errp
, "dump: failed to set dump_bitmap");
1359 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1360 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1361 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1363 if (num_dumpable
> 0) {
1364 ret
= set_dump_bitmap(last_pfn
, last_pfn
+ bits_per_buf
, false,
1365 dump_bitmap_buf
, s
);
1367 error_setg(errp
, "dump: failed to sync dump_bitmap");
1372 /* number of dumpable pages that will be dumped later */
1373 s
->num_dumpable
= num_dumpable
;
1376 g_free(dump_bitmap_buf
);
1379 static void prepare_data_cache(DataCache
*data_cache
, DumpState
*s
,
1382 data_cache
->fd
= s
->fd
;
1383 data_cache
->data_size
= 0;
1384 data_cache
->buf_size
= 4 * dump_bitmap_get_bufsize(s
);
1385 data_cache
->buf
= g_malloc0(data_cache
->buf_size
);
1386 data_cache
->offset
= offset
;
1389 static int write_cache(DataCache
*dc
, const void *buf
, size_t size
,
1393 * dc->buf_size should not be less than size, otherwise dc will never be
1396 assert(size
<= dc
->buf_size
);
1399 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1400 * otherwise check if the space is enough for caching data in buf, if not,
1401 * write the data in dc->buf to dc->fd and reset dc->buf
1403 if ((!flag_sync
&& dc
->data_size
+ size
> dc
->buf_size
) ||
1404 (flag_sync
&& dc
->data_size
> 0)) {
1405 if (write_buffer(dc
->fd
, dc
->offset
, dc
->buf
, dc
->data_size
) < 0) {
1409 dc
->offset
+= dc
->data_size
;
1414 memcpy(dc
->buf
+ dc
->data_size
, buf
, size
);
1415 dc
->data_size
+= size
;
1421 static void free_data_cache(DataCache
*data_cache
)
1423 g_free(data_cache
->buf
);
1426 static size_t get_len_buf_out(size_t page_size
, uint32_t flag_compress
)
1428 switch (flag_compress
) {
1429 case DUMP_DH_COMPRESSED_ZLIB
:
1430 return compressBound(page_size
);
1432 case DUMP_DH_COMPRESSED_LZO
:
1434 * LZO will expand incompressible data by a little amount. Please check
1435 * the following URL to see the expansion calculation:
1436 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1438 return page_size
+ page_size
/ 16 + 64 + 3;
1440 #ifdef CONFIG_SNAPPY
1441 case DUMP_DH_COMPRESSED_SNAPPY
:
1442 return snappy_max_compressed_length(page_size
);
1448 static void write_dump_pages(DumpState
*s
, Error
**errp
)
1451 DataCache page_desc
, page_data
;
1452 size_t len_buf_out
, size_out
;
1454 lzo_bytep wrkmem
= NULL
;
1456 uint8_t *buf_out
= NULL
;
1457 off_t offset_desc
, offset_data
;
1458 PageDescriptor pd
, pd_zero
;
1460 GuestPhysBlock
*block_iter
= NULL
;
1462 g_autofree
uint8_t *page
= NULL
;
1464 /* get offset of page_desc and page_data in dump file */
1465 offset_desc
= s
->offset_page
;
1466 offset_data
= offset_desc
+ sizeof(PageDescriptor
) * s
->num_dumpable
;
1468 prepare_data_cache(&page_desc
, s
, offset_desc
);
1469 prepare_data_cache(&page_data
, s
, offset_data
);
1471 /* prepare buffer to store compressed data */
1472 len_buf_out
= get_len_buf_out(s
->dump_info
.page_size
, s
->flag_compress
);
1473 assert(len_buf_out
!= 0);
1476 wrkmem
= g_malloc(LZO1X_1_MEM_COMPRESS
);
1479 buf_out
= g_malloc(len_buf_out
);
1482 * init zero page's page_desc and page_data, because every zero page
1483 * uses the same page_data
1485 pd_zero
.size
= cpu_to_dump32(s
, s
->dump_info
.page_size
);
1486 pd_zero
.flags
= cpu_to_dump32(s
, 0);
1487 pd_zero
.offset
= cpu_to_dump64(s
, offset_data
);
1488 pd_zero
.page_flags
= cpu_to_dump64(s
, 0);
1489 buf
= g_malloc0(s
->dump_info
.page_size
);
1490 ret
= write_cache(&page_data
, buf
, s
->dump_info
.page_size
, false);
1493 error_setg(errp
, "dump: failed to write page data (zero page)");
1497 offset_data
+= s
->dump_info
.page_size
;
1498 page
= g_malloc(s
->dump_info
.page_size
);
1501 * dump memory to vmcore page by page. zero page will all be resided in the
1502 * first page of page section
1504 for (buf
= page
; get_next_page(&block_iter
, &pfn_iter
, &buf
, s
); buf
= page
) {
1505 /* check zero page */
1506 if (buffer_is_zero(buf
, s
->dump_info
.page_size
)) {
1507 ret
= write_cache(&page_desc
, &pd_zero
, sizeof(PageDescriptor
),
1510 error_setg(errp
, "dump: failed to write page desc");
1515 * not zero page, then:
1516 * 1. compress the page
1517 * 2. write the compressed page into the cache of page_data
1518 * 3. get page desc of the compressed page and write it into the
1519 * cache of page_desc
1521 * only one compression format will be used here, for
1522 * s->flag_compress is set. But when compression fails to work,
1523 * we fall back to save in plaintext.
1525 size_out
= len_buf_out
;
1526 if ((s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) &&
1527 (compress2(buf_out
, (uLongf
*)&size_out
, buf
,
1528 s
->dump_info
.page_size
, Z_BEST_SPEED
) == Z_OK
) &&
1529 (size_out
< s
->dump_info
.page_size
)) {
1530 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_ZLIB
);
1531 pd
.size
= cpu_to_dump32(s
, size_out
);
1533 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1535 error_setg(errp
, "dump: failed to write page data");
1539 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) &&
1540 (lzo1x_1_compress(buf
, s
->dump_info
.page_size
, buf_out
,
1541 (lzo_uint
*)&size_out
, wrkmem
) == LZO_E_OK
) &&
1542 (size_out
< s
->dump_info
.page_size
)) {
1543 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_LZO
);
1544 pd
.size
= cpu_to_dump32(s
, size_out
);
1546 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1548 error_setg(errp
, "dump: failed to write page data");
1552 #ifdef CONFIG_SNAPPY
1553 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) &&
1554 (snappy_compress((char *)buf
, s
->dump_info
.page_size
,
1555 (char *)buf_out
, &size_out
) == SNAPPY_OK
) &&
1556 (size_out
< s
->dump_info
.page_size
)) {
1557 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_SNAPPY
);
1558 pd
.size
= cpu_to_dump32(s
, size_out
);
1560 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1562 error_setg(errp
, "dump: failed to write page data");
1568 * fall back to save in plaintext, size_out should be
1569 * assigned the target's page size
1571 pd
.flags
= cpu_to_dump32(s
, 0);
1572 size_out
= s
->dump_info
.page_size
;
1573 pd
.size
= cpu_to_dump32(s
, size_out
);
1575 ret
= write_cache(&page_data
, buf
,
1576 s
->dump_info
.page_size
, false);
1578 error_setg(errp
, "dump: failed to write page data");
1583 /* get and write page desc here */
1584 pd
.page_flags
= cpu_to_dump64(s
, 0);
1585 pd
.offset
= cpu_to_dump64(s
, offset_data
);
1586 offset_data
+= size_out
;
1588 ret
= write_cache(&page_desc
, &pd
, sizeof(PageDescriptor
), false);
1590 error_setg(errp
, "dump: failed to write page desc");
1594 s
->written_size
+= s
->dump_info
.page_size
;
1597 ret
= write_cache(&page_desc
, NULL
, 0, true);
1599 error_setg(errp
, "dump: failed to sync cache for page_desc");
1602 ret
= write_cache(&page_data
, NULL
, 0, true);
1604 error_setg(errp
, "dump: failed to sync cache for page_data");
1609 free_data_cache(&page_desc
);
1610 free_data_cache(&page_data
);
1619 static void create_kdump_vmcore(DumpState
*s
, Error
**errp
)
1625 * the kdump-compressed format is:
1627 * +------------------------------------------+ 0x0
1628 * | main header (struct disk_dump_header) |
1629 * |------------------------------------------+ block 1
1630 * | sub header (struct kdump_sub_header) |
1631 * |------------------------------------------+ block 2
1632 * | 1st-dump_bitmap |
1633 * |------------------------------------------+ block 2 + X blocks
1634 * | 2nd-dump_bitmap | (aligned by block)
1635 * |------------------------------------------+ block 2 + 2 * X blocks
1636 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1637 * | page desc for pfn 1 (struct page_desc) |
1639 * |------------------------------------------| (not aligned by block)
1640 * | page data (pfn 0) |
1641 * | page data (pfn 1) |
1643 * +------------------------------------------+
1646 ret
= write_start_flat_header(s
->fd
);
1648 error_setg(errp
, "dump: failed to write start flat header");
1652 write_dump_header(s
, errp
);
1657 write_dump_bitmap(s
, errp
);
1662 write_dump_pages(s
, errp
);
1667 ret
= write_end_flat_header(s
->fd
);
1669 error_setg(errp
, "dump: failed to write end flat header");
1674 static int validate_start_block(DumpState
*s
)
1676 GuestPhysBlock
*block
;
1678 if (!dump_has_filter(s
)) {
1682 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1683 /* This block is out of the range */
1684 if (block
->target_start
>= s
->filter_area_begin
+ s
->filter_area_length
||
1685 block
->target_end
<= s
->filter_area_begin
) {
1694 static void get_max_mapnr(DumpState
*s
)
1696 GuestPhysBlock
*last_block
;
1698 last_block
= QTAILQ_LAST(&s
->guest_phys_blocks
.head
);
1699 s
->max_mapnr
= dump_paddr_to_pfn(s
, last_block
->target_end
);
1702 static DumpState dump_state_global
= { .status
= DUMP_STATUS_NONE
};
1704 static void dump_state_prepare(DumpState
*s
)
1706 /* zero the struct, setting status to active */
1707 *s
= (DumpState
) { .status
= DUMP_STATUS_ACTIVE
};
1710 bool qemu_system_dump_in_progress(void)
1712 DumpState
*state
= &dump_state_global
;
1713 return (qatomic_read(&state
->status
) == DUMP_STATUS_ACTIVE
);
1717 * calculate total size of memory to be dumped (taking filter into
1720 static int64_t dump_calculate_size(DumpState
*s
)
1722 GuestPhysBlock
*block
;
1725 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1726 total
+= dump_filtered_memblock_size(block
,
1727 s
->filter_area_begin
,
1728 s
->filter_area_length
);
1734 static void vmcoreinfo_update_phys_base(DumpState
*s
)
1736 uint64_t size
, note_head_size
, name_size
, phys_base
;
1741 if (!note_name_equal(s
, s
->guest_note
, "VMCOREINFO")) {
1745 get_note_sizes(s
, s
->guest_note
, ¬e_head_size
, &name_size
, &size
);
1746 note_head_size
= ROUND_UP(note_head_size
, 4);
1748 vmci
= s
->guest_note
+ note_head_size
+ ROUND_UP(name_size
, 4);
1749 *(vmci
+ size
) = '\0';
1751 lines
= g_strsplit((char *)vmci
, "\n", -1);
1752 for (i
= 0; lines
[i
]; i
++) {
1753 const char *prefix
= NULL
;
1755 if (s
->dump_info
.d_machine
== EM_X86_64
) {
1756 prefix
= "NUMBER(phys_base)=";
1757 } else if (s
->dump_info
.d_machine
== EM_AARCH64
) {
1758 prefix
= "NUMBER(PHYS_OFFSET)=";
1761 if (prefix
&& g_str_has_prefix(lines
[i
], prefix
)) {
1762 if (qemu_strtou64(lines
[i
] + strlen(prefix
), NULL
, 16,
1764 warn_report("Failed to read %s", prefix
);
1766 s
->dump_info
.phys_base
= phys_base
;
1775 static void dump_init(DumpState
*s
, int fd
, bool has_format
,
1776 DumpGuestMemoryFormat format
, bool paging
, bool has_filter
,
1777 int64_t begin
, int64_t length
, Error
**errp
)
1780 VMCoreInfoState
*vmci
= vmcoreinfo_find();
1785 s
->has_format
= has_format
;
1787 s
->written_size
= 0;
1789 /* kdump-compressed is conflict with paging and filter */
1790 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1791 assert(!paging
&& !has_filter
);
1794 if (runstate_is_running()) {
1795 vm_stop(RUN_STATE_SAVE_VM
);
1801 /* If we use KVM, we should synchronize the registers before we get dump
1802 * info or physmap info.
1804 cpu_synchronize_all_states();
1811 if (has_filter
&& !length
) {
1812 error_setg(errp
, QERR_INVALID_PARAMETER
, "length");
1815 s
->filter_area_begin
= begin
;
1816 s
->filter_area_length
= length
;
1818 /* First index is 0, it's the special null name */
1819 s
->string_table_buf
= g_array_new(FALSE
, TRUE
, 1);
1821 * Allocate the null name, due to the clearing option set to true
1824 g_array_set_size(s
->string_table_buf
, 1);
1826 memory_mapping_list_init(&s
->list
);
1828 guest_phys_blocks_init(&s
->guest_phys_blocks
);
1829 guest_phys_blocks_append(&s
->guest_phys_blocks
);
1830 s
->total_size
= dump_calculate_size(s
);
1831 #ifdef DEBUG_DUMP_GUEST_MEMORY
1832 fprintf(stderr
, "DUMP: total memory to dump: %lu\n", s
->total_size
);
1835 /* it does not make sense to dump non-existent memory */
1836 if (!s
->total_size
) {
1837 error_setg(errp
, "dump: no guest memory to dump");
1841 /* Is the filter filtering everything? */
1842 if (validate_start_block(s
) == -1) {
1843 error_setg(errp
, QERR_INVALID_PARAMETER
, "begin");
1847 /* get dump info: endian, class and architecture.
1848 * If the target architecture is not supported, cpu_get_dump_info() will
1851 ret
= cpu_get_dump_info(&s
->dump_info
, &s
->guest_phys_blocks
);
1854 "dumping guest memory is not supported on this target");
1858 if (!s
->dump_info
.page_size
) {
1859 s
->dump_info
.page_size
= qemu_target_page_size();
1862 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
1863 s
->dump_info
.d_machine
, nr_cpus
);
1864 assert(s
->note_size
>= 0);
1867 * The goal of this block is to (a) update the previously guessed
1868 * phys_base, (b) copy the guest note out of the guest.
1869 * Failure to do so is not fatal for dumping.
1872 uint64_t addr
, note_head_size
, name_size
, desc_size
;
1876 note_head_size
= dump_is_64bit(s
) ?
1877 sizeof(Elf64_Nhdr
) : sizeof(Elf32_Nhdr
);
1879 format
= le16_to_cpu(vmci
->vmcoreinfo
.guest_format
);
1880 size
= le32_to_cpu(vmci
->vmcoreinfo
.size
);
1881 addr
= le64_to_cpu(vmci
->vmcoreinfo
.paddr
);
1882 if (!vmci
->has_vmcoreinfo
) {
1883 warn_report("guest note is not present");
1884 } else if (size
< note_head_size
|| size
> MAX_GUEST_NOTE_SIZE
) {
1885 warn_report("guest note size is invalid: %" PRIu32
, size
);
1886 } else if (format
!= FW_CFG_VMCOREINFO_FORMAT_ELF
) {
1887 warn_report("guest note format is unsupported: %" PRIu16
, format
);
1889 s
->guest_note
= g_malloc(size
+ 1); /* +1 for adding \0 */
1890 cpu_physical_memory_read(addr
, s
->guest_note
, size
);
1892 get_note_sizes(s
, s
->guest_note
, NULL
, &name_size
, &desc_size
);
1893 s
->guest_note_size
= ELF_NOTE_SIZE(note_head_size
, name_size
,
1895 if (name_size
> MAX_GUEST_NOTE_SIZE
||
1896 desc_size
> MAX_GUEST_NOTE_SIZE
||
1897 s
->guest_note_size
> size
) {
1898 warn_report("Invalid guest note header");
1899 g_free(s
->guest_note
);
1900 s
->guest_note
= NULL
;
1902 vmcoreinfo_update_phys_base(s
);
1903 s
->note_size
+= s
->guest_note_size
;
1908 /* get memory mapping */
1910 qemu_get_guest_memory_mapping(&s
->list
, &s
->guest_phys_blocks
, errp
);
1915 qemu_get_guest_simple_memory_mapping(&s
->list
, &s
->guest_phys_blocks
);
1918 s
->nr_cpus
= nr_cpus
;
1923 tmp
= DIV_ROUND_UP(DIV_ROUND_UP(s
->max_mapnr
, CHAR_BIT
),
1924 s
->dump_info
.page_size
);
1925 s
->len_dump_bitmap
= tmp
* s
->dump_info
.page_size
;
1927 /* init for kdump-compressed format */
1928 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1930 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
:
1931 s
->flag_compress
= DUMP_DH_COMPRESSED_ZLIB
;
1934 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
:
1936 if (lzo_init() != LZO_E_OK
) {
1937 error_setg(errp
, "failed to initialize the LZO library");
1941 s
->flag_compress
= DUMP_DH_COMPRESSED_LZO
;
1944 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
:
1945 s
->flag_compress
= DUMP_DH_COMPRESSED_SNAPPY
;
1949 s
->flag_compress
= 0;
1955 if (dump_has_filter(s
)) {
1956 memory_mapping_filter(&s
->list
, s
->filter_area_begin
, s
->filter_area_length
);
1960 * The first section header is always a special one in which most
1961 * fields are 0. The section header string table is also always
1967 * Adds the number of architecture sections to shdr_num and sets
1968 * elf_section_data_size so we know the offsets and sizes of all
1971 if (s
->dump_info
.arch_sections_add_fn
) {
1972 s
->dump_info
.arch_sections_add_fn(s
);
1976 * calculate shdr_num so we know the offsets and sizes of all
1978 * Calculate phdr_num
1980 * The absolute maximum amount of phdrs is UINT32_MAX - 1 as
1981 * sh_info is 32 bit. There's special handling once we go over
1982 * UINT16_MAX - 1 but that is handled in the ehdr and section
1985 s
->phdr_num
= 1; /* Reserve PT_NOTE */
1986 if (s
->list
.num
<= UINT32_MAX
- 1) {
1987 s
->phdr_num
+= s
->list
.num
;
1989 s
->phdr_num
= UINT32_MAX
;
1993 * Now that the number of section and program headers is known we
1994 * can calculate the offsets of the headers and data.
1996 if (dump_is_64bit(s
)) {
1997 s
->shdr_offset
= sizeof(Elf64_Ehdr
);
1998 s
->phdr_offset
= s
->shdr_offset
+ sizeof(Elf64_Shdr
) * s
->shdr_num
;
1999 s
->note_offset
= s
->phdr_offset
+ sizeof(Elf64_Phdr
) * s
->phdr_num
;
2001 s
->shdr_offset
= sizeof(Elf32_Ehdr
);
2002 s
->phdr_offset
= s
->shdr_offset
+ sizeof(Elf32_Shdr
) * s
->shdr_num
;
2003 s
->note_offset
= s
->phdr_offset
+ sizeof(Elf32_Phdr
) * s
->phdr_num
;
2005 s
->memory_offset
= s
->note_offset
+ s
->note_size
;
2006 s
->section_offset
= s
->memory_offset
+ s
->total_size
;
2014 /* this operation might be time consuming. */
2015 static void dump_process(DumpState
*s
, Error
**errp
)
2018 DumpQueryResult
*result
= NULL
;
2020 if (s
->has_format
&& s
->format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
) {
2021 create_win_dump(s
, errp
);
2022 } else if (s
->has_format
&& s
->format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
2023 create_kdump_vmcore(s
, errp
);
2025 create_vmcore(s
, errp
);
2028 /* make sure status is written after written_size updates */
2030 qatomic_set(&s
->status
,
2031 (*errp
? DUMP_STATUS_FAILED
: DUMP_STATUS_COMPLETED
));
2033 /* send DUMP_COMPLETED message (unconditionally) */
2034 result
= qmp_query_dump(NULL
);
2035 /* should never fail */
2037 qapi_event_send_dump_completed(result
,
2038 *errp
? error_get_pretty(*errp
) : NULL
);
2039 qapi_free_DumpQueryResult(result
);
2044 static void *dump_thread(void *data
)
2046 DumpState
*s
= (DumpState
*)data
;
2047 dump_process(s
, NULL
);
2051 DumpQueryResult
*qmp_query_dump(Error
**errp
)
2053 DumpQueryResult
*result
= g_new(DumpQueryResult
, 1);
2054 DumpState
*state
= &dump_state_global
;
2055 result
->status
= qatomic_read(&state
->status
);
2056 /* make sure we are reading status and written_size in order */
2058 result
->completed
= state
->written_size
;
2059 result
->total
= state
->total_size
;
2063 void qmp_dump_guest_memory(bool paging
, const char *file
,
2064 bool has_detach
, bool detach
,
2065 bool has_begin
, int64_t begin
, bool has_length
,
2066 int64_t length
, bool has_format
,
2067 DumpGuestMemoryFormat format
, Error
**errp
)
2073 bool detach_p
= false;
2075 if (runstate_check(RUN_STATE_INMIGRATE
)) {
2076 error_setg(errp
, "Dump not allowed during incoming migration.");
2080 /* if there is a dump in background, we should wait until the dump
2082 if (qemu_system_dump_in_progress()) {
2083 error_setg(errp
, "There is a dump in process, please wait.");
2088 * kdump-compressed format need the whole memory dumped, so paging or
2089 * filter is not supported here.
2091 if ((has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) &&
2092 (paging
|| has_begin
|| has_length
)) {
2093 error_setg(errp
, "kdump-compressed format doesn't support paging or "
2097 if (has_begin
&& !has_length
) {
2098 error_setg(errp
, QERR_MISSING_PARAMETER
, "length");
2101 if (!has_begin
&& has_length
) {
2102 error_setg(errp
, QERR_MISSING_PARAMETER
, "begin");
2109 /* check whether lzo/snappy is supported */
2111 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
) {
2112 error_setg(errp
, "kdump-lzo is not available now");
2117 #ifndef CONFIG_SNAPPY
2118 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
) {
2119 error_setg(errp
, "kdump-snappy is not available now");
2124 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
2125 && !win_dump_available(errp
)) {
2130 if (strstart(file
, "fd:", &p
)) {
2131 fd
= monitor_get_fd(monitor_cur(), p
, errp
);
2138 if (strstart(file
, "file:", &p
)) {
2139 fd
= qemu_open_old(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
2141 error_setg_file_open(errp
, errno
, p
);
2147 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
2151 if (!dump_migration_blocker
) {
2152 error_setg(&dump_migration_blocker
,
2153 "Live migration disabled: dump-guest-memory in progress");
2157 * Allows even for -only-migratable, but forbid migration during the
2158 * process of dump guest memory.
2160 if (migrate_add_blocker_internal(dump_migration_blocker
, errp
)) {
2161 /* Remember to release the fd before passing it over to dump state */
2166 s
= &dump_state_global
;
2167 dump_state_prepare(s
);
2169 dump_init(s
, fd
, has_format
, format
, paging
, has_begin
,
2170 begin
, length
, errp
);
2172 qatomic_set(&s
->status
, DUMP_STATUS_FAILED
);
2179 qemu_thread_create(&s
->dump_thread
, "dump_thread", dump_thread
,
2180 s
, QEMU_THREAD_DETACHED
);
2183 dump_process(s
, errp
);
2187 DumpGuestMemoryCapability
*qmp_query_dump_guest_memory_capability(Error
**errp
)
2189 DumpGuestMemoryCapability
*cap
=
2190 g_new0(DumpGuestMemoryCapability
, 1);
2191 DumpGuestMemoryFormatList
**tail
= &cap
->formats
;
2193 /* elf is always available */
2194 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_ELF
);
2196 /* kdump-zlib is always available */
2197 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
);
2199 /* add new item if kdump-lzo is available */
2201 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
);
2204 /* add new item if kdump-snappy is available */
2205 #ifdef CONFIG_SNAPPY
2206 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
);
2209 if (win_dump_available(NULL
)) {
2210 QAPI_LIST_APPEND(tail
, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
);