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-common.h"
17 #include "exec/cpu-all.h"
18 #include "exec/hwaddr.h"
19 #include "monitor/monitor.h"
20 #include "sysemu/kvm.h"
21 #include "sysemu/dump.h"
22 #include "sysemu/sysemu.h"
23 #include "sysemu/memory_mapping.h"
24 #include "sysemu/cpus.h"
25 #include "qapi/error.h"
26 #include "qmp-commands.h"
30 #include <lzo/lzo1x.h>
35 #ifndef ELF_MACHINE_UNAME
36 #define ELF_MACHINE_UNAME "Unknown"
39 uint16_t cpu_to_dump16(DumpState
*s
, uint16_t val
)
41 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
42 val
= cpu_to_le16(val
);
44 val
= cpu_to_be16(val
);
50 uint32_t cpu_to_dump32(DumpState
*s
, uint32_t val
)
52 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
53 val
= cpu_to_le32(val
);
55 val
= cpu_to_be32(val
);
61 uint64_t cpu_to_dump64(DumpState
*s
, uint64_t val
)
63 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
64 val
= cpu_to_le64(val
);
66 val
= cpu_to_be64(val
);
72 static int dump_cleanup(DumpState
*s
)
74 guest_phys_blocks_free(&s
->guest_phys_blocks
);
75 memory_mapping_list_free(&s
->list
);
84 static void dump_error(DumpState
*s
, const char *reason
, Error
**errp
)
87 error_setg(errp
, "%s", reason
);
90 static int fd_write_vmcore(const void *buf
, size_t size
, void *opaque
)
92 DumpState
*s
= opaque
;
95 written_size
= qemu_write_full(s
->fd
, buf
, size
);
96 if (written_size
!= size
) {
103 static void write_elf64_header(DumpState
*s
, Error
**errp
)
105 Elf64_Ehdr elf_header
;
108 memset(&elf_header
, 0, sizeof(Elf64_Ehdr
));
109 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
110 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS64
;
111 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
112 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
113 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
114 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
115 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
116 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
117 elf_header
.e_phoff
= cpu_to_dump64(s
, sizeof(Elf64_Ehdr
));
118 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf64_Phdr
));
119 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
120 if (s
->have_section
) {
121 uint64_t shoff
= sizeof(Elf64_Ehdr
) + sizeof(Elf64_Phdr
) * s
->sh_info
;
123 elf_header
.e_shoff
= cpu_to_dump64(s
, shoff
);
124 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf64_Shdr
));
125 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
128 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
130 dump_error(s
, "dump: failed to write elf header", errp
);
134 static void write_elf32_header(DumpState
*s
, Error
**errp
)
136 Elf32_Ehdr elf_header
;
139 memset(&elf_header
, 0, sizeof(Elf32_Ehdr
));
140 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
141 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS32
;
142 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
143 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
144 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
145 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
146 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
147 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
148 elf_header
.e_phoff
= cpu_to_dump32(s
, sizeof(Elf32_Ehdr
));
149 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf32_Phdr
));
150 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
151 if (s
->have_section
) {
152 uint32_t shoff
= sizeof(Elf32_Ehdr
) + sizeof(Elf32_Phdr
) * s
->sh_info
;
154 elf_header
.e_shoff
= cpu_to_dump32(s
, shoff
);
155 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf32_Shdr
));
156 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
159 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
161 dump_error(s
, "dump: failed to write elf header", errp
);
165 static void write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
166 int phdr_index
, hwaddr offset
,
167 hwaddr filesz
, Error
**errp
)
172 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
173 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
174 phdr
.p_offset
= cpu_to_dump64(s
, offset
);
175 phdr
.p_paddr
= cpu_to_dump64(s
, memory_mapping
->phys_addr
);
176 phdr
.p_filesz
= cpu_to_dump64(s
, filesz
);
177 phdr
.p_memsz
= cpu_to_dump64(s
, memory_mapping
->length
);
178 phdr
.p_vaddr
= cpu_to_dump64(s
, memory_mapping
->virt_addr
);
180 assert(memory_mapping
->length
>= filesz
);
182 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
184 dump_error(s
, "dump: failed to write program header table", errp
);
188 static void write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
189 int phdr_index
, hwaddr offset
,
190 hwaddr filesz
, Error
**errp
)
195 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
196 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
197 phdr
.p_offset
= cpu_to_dump32(s
, offset
);
198 phdr
.p_paddr
= cpu_to_dump32(s
, memory_mapping
->phys_addr
);
199 phdr
.p_filesz
= cpu_to_dump32(s
, filesz
);
200 phdr
.p_memsz
= cpu_to_dump32(s
, memory_mapping
->length
);
201 phdr
.p_vaddr
= cpu_to_dump32(s
, memory_mapping
->virt_addr
);
203 assert(memory_mapping
->length
>= filesz
);
205 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
207 dump_error(s
, "dump: failed to write program header table", errp
);
211 static void write_elf64_note(DumpState
*s
, Error
**errp
)
214 hwaddr begin
= s
->memory_offset
- s
->note_size
;
217 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
218 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
219 phdr
.p_offset
= cpu_to_dump64(s
, begin
);
221 phdr
.p_filesz
= cpu_to_dump64(s
, s
->note_size
);
222 phdr
.p_memsz
= cpu_to_dump64(s
, s
->note_size
);
225 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
227 dump_error(s
, "dump: failed to write program header table", errp
);
231 static inline int cpu_index(CPUState
*cpu
)
233 return cpu
->cpu_index
+ 1;
236 static void write_elf64_notes(WriteCoreDumpFunction f
, DumpState
*s
,
245 ret
= cpu_write_elf64_note(f
, cpu
, id
, s
);
247 dump_error(s
, "dump: failed to write elf notes", errp
);
253 ret
= cpu_write_elf64_qemunote(f
, cpu
, s
);
255 dump_error(s
, "dump: failed to write CPU status", errp
);
261 static void write_elf32_note(DumpState
*s
, Error
**errp
)
263 hwaddr begin
= s
->memory_offset
- s
->note_size
;
267 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
268 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
269 phdr
.p_offset
= cpu_to_dump32(s
, begin
);
271 phdr
.p_filesz
= cpu_to_dump32(s
, s
->note_size
);
272 phdr
.p_memsz
= cpu_to_dump32(s
, s
->note_size
);
275 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
277 dump_error(s
, "dump: failed to write program header table", errp
);
281 static void write_elf32_notes(WriteCoreDumpFunction f
, DumpState
*s
,
290 ret
= cpu_write_elf32_note(f
, cpu
, id
, s
);
292 dump_error(s
, "dump: failed to write elf notes", errp
);
298 ret
= cpu_write_elf32_qemunote(f
, cpu
, s
);
300 dump_error(s
, "dump: failed to write CPU status", errp
);
306 static void write_elf_section(DumpState
*s
, int type
, Error
**errp
)
315 shdr_size
= sizeof(Elf32_Shdr
);
316 memset(&shdr32
, 0, shdr_size
);
317 shdr32
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
320 shdr_size
= sizeof(Elf64_Shdr
);
321 memset(&shdr64
, 0, shdr_size
);
322 shdr64
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
326 ret
= fd_write_vmcore(&shdr
, shdr_size
, s
);
328 dump_error(s
, "dump: failed to write section header table", errp
);
332 static void write_data(DumpState
*s
, void *buf
, int length
, Error
**errp
)
336 ret
= fd_write_vmcore(buf
, length
, s
);
338 dump_error(s
, "dump: failed to save memory", errp
);
342 /* write the memory to vmcore. 1 page per I/O. */
343 static void write_memory(DumpState
*s
, GuestPhysBlock
*block
, ram_addr_t start
,
344 int64_t size
, Error
**errp
)
347 Error
*local_err
= NULL
;
349 for (i
= 0; i
< size
/ TARGET_PAGE_SIZE
; i
++) {
350 write_data(s
, block
->host_addr
+ start
+ i
* TARGET_PAGE_SIZE
,
351 TARGET_PAGE_SIZE
, &local_err
);
353 error_propagate(errp
, local_err
);
358 if ((size
% TARGET_PAGE_SIZE
) != 0) {
359 write_data(s
, block
->host_addr
+ start
+ i
* TARGET_PAGE_SIZE
,
360 size
% TARGET_PAGE_SIZE
, &local_err
);
362 error_propagate(errp
, local_err
);
368 /* get the memory's offset and size in the vmcore */
369 static void get_offset_range(hwaddr phys_addr
,
370 ram_addr_t mapping_length
,
375 GuestPhysBlock
*block
;
376 hwaddr offset
= s
->memory_offset
;
377 int64_t size_in_block
, start
;
379 /* When the memory is not stored into vmcore, offset will be -1 */
384 if (phys_addr
< s
->begin
|| phys_addr
>= s
->begin
+ s
->length
) {
389 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
391 if (block
->target_start
>= s
->begin
+ s
->length
||
392 block
->target_end
<= s
->begin
) {
393 /* This block is out of the range */
397 if (s
->begin
<= block
->target_start
) {
398 start
= block
->target_start
;
403 size_in_block
= block
->target_end
- start
;
404 if (s
->begin
+ s
->length
< block
->target_end
) {
405 size_in_block
-= block
->target_end
- (s
->begin
+ s
->length
);
408 start
= block
->target_start
;
409 size_in_block
= block
->target_end
- block
->target_start
;
412 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
413 *p_offset
= phys_addr
- start
+ offset
;
415 /* The offset range mapped from the vmcore file must not spill over
416 * the GuestPhysBlock, clamp it. The rest of the mapping will be
417 * zero-filled in memory at load time; see
418 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
420 *p_filesz
= phys_addr
+ mapping_length
<= start
+ size_in_block
?
422 size_in_block
- (phys_addr
- start
);
426 offset
+= size_in_block
;
430 static void write_elf_loads(DumpState
*s
, Error
**errp
)
432 hwaddr offset
, filesz
;
433 MemoryMapping
*memory_mapping
;
434 uint32_t phdr_index
= 1;
436 Error
*local_err
= NULL
;
438 if (s
->have_section
) {
439 max_index
= s
->sh_info
;
441 max_index
= s
->phdr_num
;
444 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
445 get_offset_range(memory_mapping
->phys_addr
,
446 memory_mapping
->length
,
447 s
, &offset
, &filesz
);
448 if (s
->dump_info
.d_class
== ELFCLASS64
) {
449 write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
,
452 write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
,
457 error_propagate(errp
, local_err
);
461 if (phdr_index
>= max_index
) {
467 /* write elf header, PT_NOTE and elf note to vmcore. */
468 static void dump_begin(DumpState
*s
, Error
**errp
)
470 Error
*local_err
= NULL
;
473 * the vmcore's format is:
492 * we only know where the memory is saved after we write elf note into
496 /* write elf header to vmcore */
497 if (s
->dump_info
.d_class
== ELFCLASS64
) {
498 write_elf64_header(s
, &local_err
);
500 write_elf32_header(s
, &local_err
);
503 error_propagate(errp
, local_err
);
507 if (s
->dump_info
.d_class
== ELFCLASS64
) {
508 /* write PT_NOTE to vmcore */
509 write_elf64_note(s
, &local_err
);
511 error_propagate(errp
, local_err
);
515 /* write all PT_LOAD to vmcore */
516 write_elf_loads(s
, &local_err
);
518 error_propagate(errp
, local_err
);
522 /* write section to vmcore */
523 if (s
->have_section
) {
524 write_elf_section(s
, 1, &local_err
);
526 error_propagate(errp
, local_err
);
531 /* write notes to vmcore */
532 write_elf64_notes(fd_write_vmcore
, s
, &local_err
);
534 error_propagate(errp
, local_err
);
538 /* write PT_NOTE to vmcore */
539 write_elf32_note(s
, &local_err
);
541 error_propagate(errp
, local_err
);
545 /* write all PT_LOAD to vmcore */
546 write_elf_loads(s
, &local_err
);
548 error_propagate(errp
, local_err
);
552 /* write section to vmcore */
553 if (s
->have_section
) {
554 write_elf_section(s
, 0, &local_err
);
556 error_propagate(errp
, local_err
);
561 /* write notes to vmcore */
562 write_elf32_notes(fd_write_vmcore
, s
, &local_err
);
564 error_propagate(errp
, local_err
);
570 static void dump_completed(DumpState
*s
)
575 static int get_next_block(DumpState
*s
, GuestPhysBlock
*block
)
578 block
= QTAILQ_NEXT(block
, next
);
585 s
->next_block
= block
;
587 if (block
->target_start
>= s
->begin
+ s
->length
||
588 block
->target_end
<= s
->begin
) {
589 /* This block is out of the range */
593 if (s
->begin
> block
->target_start
) {
594 s
->start
= s
->begin
- block
->target_start
;
602 /* write all memory to vmcore */
603 static void dump_iterate(DumpState
*s
, Error
**errp
)
605 GuestPhysBlock
*block
;
608 Error
*local_err
= NULL
;
611 block
= s
->next_block
;
613 size
= block
->target_end
- block
->target_start
;
616 if (s
->begin
+ s
->length
< block
->target_end
) {
617 size
-= block
->target_end
- (s
->begin
+ s
->length
);
620 write_memory(s
, block
, s
->start
, size
, &local_err
);
622 error_propagate(errp
, local_err
);
626 ret
= get_next_block(s
, block
);
633 static void create_vmcore(DumpState
*s
, Error
**errp
)
635 Error
*local_err
= NULL
;
637 dump_begin(s
, &local_err
);
639 error_propagate(errp
, local_err
);
643 dump_iterate(s
, errp
);
646 static int write_start_flat_header(int fd
)
648 MakedumpfileHeader
*mh
;
651 QEMU_BUILD_BUG_ON(sizeof *mh
> MAX_SIZE_MDF_HEADER
);
652 mh
= g_malloc0(MAX_SIZE_MDF_HEADER
);
654 memcpy(mh
->signature
, MAKEDUMPFILE_SIGNATURE
,
655 MIN(sizeof mh
->signature
, sizeof MAKEDUMPFILE_SIGNATURE
));
657 mh
->type
= cpu_to_be64(TYPE_FLAT_HEADER
);
658 mh
->version
= cpu_to_be64(VERSION_FLAT_HEADER
);
661 written_size
= qemu_write_full(fd
, mh
, MAX_SIZE_MDF_HEADER
);
662 if (written_size
!= MAX_SIZE_MDF_HEADER
) {
670 static int write_end_flat_header(int fd
)
672 MakedumpfileDataHeader mdh
;
674 mdh
.offset
= END_FLAG_FLAT_HEADER
;
675 mdh
.buf_size
= END_FLAG_FLAT_HEADER
;
678 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
679 if (written_size
!= sizeof(mdh
)) {
686 static int write_buffer(int fd
, off_t offset
, const void *buf
, size_t size
)
689 MakedumpfileDataHeader mdh
;
691 mdh
.offset
= cpu_to_be64(offset
);
692 mdh
.buf_size
= cpu_to_be64(size
);
694 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
695 if (written_size
!= sizeof(mdh
)) {
699 written_size
= qemu_write_full(fd
, buf
, size
);
700 if (written_size
!= size
) {
707 static int buf_write_note(const void *buf
, size_t size
, void *opaque
)
709 DumpState
*s
= opaque
;
711 /* note_buf is not enough */
712 if (s
->note_buf_offset
+ size
> s
->note_size
) {
716 memcpy(s
->note_buf
+ s
->note_buf_offset
, buf
, size
);
718 s
->note_buf_offset
+= size
;
723 /* write common header, sub header and elf note to vmcore */
724 static void create_header32(DumpState
*s
, Error
**errp
)
726 DiskDumpHeader32
*dh
= NULL
;
727 KdumpSubHeader32
*kh
= NULL
;
730 uint32_t sub_hdr_size
;
731 uint32_t bitmap_blocks
;
733 uint64_t offset_note
;
734 Error
*local_err
= NULL
;
736 /* write common header, the version of kdump-compressed format is 6th */
737 size
= sizeof(DiskDumpHeader32
);
738 dh
= g_malloc0(size
);
740 strncpy(dh
->signature
, KDUMP_SIGNATURE
, strlen(KDUMP_SIGNATURE
));
741 dh
->header_version
= cpu_to_dump32(s
, 6);
742 block_size
= TARGET_PAGE_SIZE
;
743 dh
->block_size
= cpu_to_dump32(s
, block_size
);
744 sub_hdr_size
= sizeof(struct KdumpSubHeader32
) + s
->note_size
;
745 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
746 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
747 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
748 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
749 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
750 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
751 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
752 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
754 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
755 status
|= DUMP_DH_COMPRESSED_ZLIB
;
758 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
759 status
|= DUMP_DH_COMPRESSED_LZO
;
763 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
764 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
767 dh
->status
= cpu_to_dump32(s
, status
);
769 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
770 dump_error(s
, "dump: failed to write disk dump header", errp
);
774 /* write sub header */
775 size
= sizeof(KdumpSubHeader32
);
776 kh
= g_malloc0(size
);
778 /* 64bit max_mapnr_64 */
779 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
780 kh
->phys_base
= cpu_to_dump32(s
, PHYS_BASE
);
781 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
783 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
784 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
785 kh
->note_size
= cpu_to_dump32(s
, s
->note_size
);
787 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
788 block_size
, kh
, size
) < 0) {
789 dump_error(s
, "dump: failed to write kdump sub header", errp
);
794 s
->note_buf
= g_malloc0(s
->note_size
);
795 s
->note_buf_offset
= 0;
797 /* use s->note_buf to store notes temporarily */
798 write_elf32_notes(buf_write_note
, s
, &local_err
);
800 error_propagate(errp
, local_err
);
803 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
805 dump_error(s
, "dump: failed to write notes", errp
);
809 /* get offset of dump_bitmap */
810 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
813 /* get offset of page */
814 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
823 /* write common header, sub header and elf note to vmcore */
824 static void create_header64(DumpState
*s
, Error
**errp
)
826 DiskDumpHeader64
*dh
= NULL
;
827 KdumpSubHeader64
*kh
= NULL
;
830 uint32_t sub_hdr_size
;
831 uint32_t bitmap_blocks
;
833 uint64_t offset_note
;
834 Error
*local_err
= NULL
;
836 /* write common header, the version of kdump-compressed format is 6th */
837 size
= sizeof(DiskDumpHeader64
);
838 dh
= g_malloc0(size
);
840 strncpy(dh
->signature
, KDUMP_SIGNATURE
, strlen(KDUMP_SIGNATURE
));
841 dh
->header_version
= cpu_to_dump32(s
, 6);
842 block_size
= TARGET_PAGE_SIZE
;
843 dh
->block_size
= cpu_to_dump32(s
, block_size
);
844 sub_hdr_size
= sizeof(struct KdumpSubHeader64
) + s
->note_size
;
845 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
846 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
847 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
848 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
849 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
850 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
851 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
852 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
854 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
855 status
|= DUMP_DH_COMPRESSED_ZLIB
;
858 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
859 status
|= DUMP_DH_COMPRESSED_LZO
;
863 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
864 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
867 dh
->status
= cpu_to_dump32(s
, status
);
869 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
870 dump_error(s
, "dump: failed to write disk dump header", errp
);
874 /* write sub header */
875 size
= sizeof(KdumpSubHeader64
);
876 kh
= g_malloc0(size
);
878 /* 64bit max_mapnr_64 */
879 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
880 kh
->phys_base
= cpu_to_dump64(s
, PHYS_BASE
);
881 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
883 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
884 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
885 kh
->note_size
= cpu_to_dump64(s
, s
->note_size
);
887 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
888 block_size
, kh
, size
) < 0) {
889 dump_error(s
, "dump: failed to write kdump sub header", errp
);
894 s
->note_buf
= g_malloc0(s
->note_size
);
895 s
->note_buf_offset
= 0;
897 /* use s->note_buf to store notes temporarily */
898 write_elf64_notes(buf_write_note
, s
, &local_err
);
900 error_propagate(errp
, local_err
);
904 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
906 dump_error(s
, "dump: failed to write notes", errp
);
910 /* get offset of dump_bitmap */
911 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
914 /* get offset of page */
915 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
924 static void write_dump_header(DumpState
*s
, Error
**errp
)
926 Error
*local_err
= NULL
;
928 if (s
->dump_info
.d_class
== ELFCLASS32
) {
929 create_header32(s
, &local_err
);
931 create_header64(s
, &local_err
);
934 error_propagate(errp
, local_err
);
939 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
940 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
941 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
942 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
943 * vmcore, ie. synchronizing un-sync bit into vmcore.
945 static int set_dump_bitmap(uint64_t last_pfn
, uint64_t pfn
, bool value
,
946 uint8_t *buf
, DumpState
*s
)
948 off_t old_offset
, new_offset
;
949 off_t offset_bitmap1
, offset_bitmap2
;
952 /* should not set the previous place */
953 assert(last_pfn
<= pfn
);
956 * if the bit needed to be set is not cached in buf, flush the data in buf
958 * making new_offset be bigger than old_offset can also sync remained data
961 old_offset
= BUFSIZE_BITMAP
* (last_pfn
/ PFN_BUFBITMAP
);
962 new_offset
= BUFSIZE_BITMAP
* (pfn
/ PFN_BUFBITMAP
);
964 while (old_offset
< new_offset
) {
965 /* calculate the offset and write dump_bitmap */
966 offset_bitmap1
= s
->offset_dump_bitmap
+ old_offset
;
967 if (write_buffer(s
->fd
, offset_bitmap1
, buf
,
968 BUFSIZE_BITMAP
) < 0) {
972 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
973 offset_bitmap2
= s
->offset_dump_bitmap
+ s
->len_dump_bitmap
+
975 if (write_buffer(s
->fd
, offset_bitmap2
, buf
,
976 BUFSIZE_BITMAP
) < 0) {
980 memset(buf
, 0, BUFSIZE_BITMAP
);
981 old_offset
+= BUFSIZE_BITMAP
;
984 /* get the exact place of the bit in the buf, and set it */
985 byte
= (pfn
% PFN_BUFBITMAP
) / CHAR_BIT
;
986 bit
= (pfn
% PFN_BUFBITMAP
) % CHAR_BIT
;
988 buf
[byte
] |= 1u << bit
;
990 buf
[byte
] &= ~(1u << bit
);
997 * exam every page and return the page frame number and the address of the page.
998 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
999 * blocks, so block->target_start and block->target_end should be interal
1000 * multiples of the target page size.
1002 static bool get_next_page(GuestPhysBlock
**blockptr
, uint64_t *pfnptr
,
1003 uint8_t **bufptr
, DumpState
*s
)
1005 GuestPhysBlock
*block
= *blockptr
;
1009 /* block == NULL means the start of the iteration */
1011 block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1013 assert((block
->target_start
& ~TARGET_PAGE_MASK
) == 0);
1014 assert((block
->target_end
& ~TARGET_PAGE_MASK
) == 0);
1015 *pfnptr
= paddr_to_pfn(block
->target_start
);
1017 *bufptr
= block
->host_addr
;
1022 *pfnptr
= *pfnptr
+ 1;
1023 addr
= pfn_to_paddr(*pfnptr
);
1025 if ((addr
>= block
->target_start
) &&
1026 (addr
+ TARGET_PAGE_SIZE
<= block
->target_end
)) {
1027 buf
= block
->host_addr
+ (addr
- block
->target_start
);
1029 /* the next page is in the next block */
1030 block
= QTAILQ_NEXT(block
, next
);
1035 assert((block
->target_start
& ~TARGET_PAGE_MASK
) == 0);
1036 assert((block
->target_end
& ~TARGET_PAGE_MASK
) == 0);
1037 *pfnptr
= paddr_to_pfn(block
->target_start
);
1038 buf
= block
->host_addr
;
1048 static void write_dump_bitmap(DumpState
*s
, Error
**errp
)
1051 uint64_t last_pfn
, pfn
;
1052 void *dump_bitmap_buf
;
1053 size_t num_dumpable
;
1054 GuestPhysBlock
*block_iter
= NULL
;
1056 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1057 dump_bitmap_buf
= g_malloc0(BUFSIZE_BITMAP
);
1063 * exam memory page by page, and set the bit in dump_bitmap corresponded
1064 * to the existing page.
1066 while (get_next_page(&block_iter
, &pfn
, NULL
, s
)) {
1067 ret
= set_dump_bitmap(last_pfn
, pfn
, true, dump_bitmap_buf
, s
);
1069 dump_error(s
, "dump: failed to set dump_bitmap", errp
);
1078 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1079 * set last_pfn + PFN_BUFBITMAP to 0 and those set but un-sync bit will be
1080 * synchronized into vmcore.
1082 if (num_dumpable
> 0) {
1083 ret
= set_dump_bitmap(last_pfn
, last_pfn
+ PFN_BUFBITMAP
, false,
1084 dump_bitmap_buf
, s
);
1086 dump_error(s
, "dump: failed to sync dump_bitmap", errp
);
1091 /* number of dumpable pages that will be dumped later */
1092 s
->num_dumpable
= num_dumpable
;
1095 g_free(dump_bitmap_buf
);
1098 static void prepare_data_cache(DataCache
*data_cache
, DumpState
*s
,
1101 data_cache
->fd
= s
->fd
;
1102 data_cache
->data_size
= 0;
1103 data_cache
->buf_size
= BUFSIZE_DATA_CACHE
;
1104 data_cache
->buf
= g_malloc0(BUFSIZE_DATA_CACHE
);
1105 data_cache
->offset
= offset
;
1108 static int write_cache(DataCache
*dc
, const void *buf
, size_t size
,
1112 * dc->buf_size should not be less than size, otherwise dc will never be
1115 assert(size
<= dc
->buf_size
);
1118 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1119 * otherwise check if the space is enough for caching data in buf, if not,
1120 * write the data in dc->buf to dc->fd and reset dc->buf
1122 if ((!flag_sync
&& dc
->data_size
+ size
> dc
->buf_size
) ||
1123 (flag_sync
&& dc
->data_size
> 0)) {
1124 if (write_buffer(dc
->fd
, dc
->offset
, dc
->buf
, dc
->data_size
) < 0) {
1128 dc
->offset
+= dc
->data_size
;
1133 memcpy(dc
->buf
+ dc
->data_size
, buf
, size
);
1134 dc
->data_size
+= size
;
1140 static void free_data_cache(DataCache
*data_cache
)
1142 g_free(data_cache
->buf
);
1145 static size_t get_len_buf_out(size_t page_size
, uint32_t flag_compress
)
1147 switch (flag_compress
) {
1148 case DUMP_DH_COMPRESSED_ZLIB
:
1149 return compressBound(page_size
);
1151 case DUMP_DH_COMPRESSED_LZO
:
1153 * LZO will expand incompressible data by a little amount. Please check
1154 * the following URL to see the expansion calculation:
1155 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1157 return page_size
+ page_size
/ 16 + 64 + 3;
1159 #ifdef CONFIG_SNAPPY
1160 case DUMP_DH_COMPRESSED_SNAPPY
:
1161 return snappy_max_compressed_length(page_size
);
1168 * check if the page is all 0
1170 static inline bool is_zero_page(const uint8_t *buf
, size_t page_size
)
1172 return buffer_is_zero(buf
, page_size
);
1175 static void write_dump_pages(DumpState
*s
, Error
**errp
)
1178 DataCache page_desc
, page_data
;
1179 size_t len_buf_out
, size_out
;
1181 lzo_bytep wrkmem
= NULL
;
1183 uint8_t *buf_out
= NULL
;
1184 off_t offset_desc
, offset_data
;
1185 PageDescriptor pd
, pd_zero
;
1187 GuestPhysBlock
*block_iter
= NULL
;
1190 /* get offset of page_desc and page_data in dump file */
1191 offset_desc
= s
->offset_page
;
1192 offset_data
= offset_desc
+ sizeof(PageDescriptor
) * s
->num_dumpable
;
1194 prepare_data_cache(&page_desc
, s
, offset_desc
);
1195 prepare_data_cache(&page_data
, s
, offset_data
);
1197 /* prepare buffer to store compressed data */
1198 len_buf_out
= get_len_buf_out(TARGET_PAGE_SIZE
, s
->flag_compress
);
1199 assert(len_buf_out
!= 0);
1202 wrkmem
= g_malloc(LZO1X_1_MEM_COMPRESS
);
1205 buf_out
= g_malloc(len_buf_out
);
1208 * init zero page's page_desc and page_data, because every zero page
1209 * uses the same page_data
1211 pd_zero
.size
= cpu_to_dump32(s
, TARGET_PAGE_SIZE
);
1212 pd_zero
.flags
= cpu_to_dump32(s
, 0);
1213 pd_zero
.offset
= cpu_to_dump64(s
, offset_data
);
1214 pd_zero
.page_flags
= cpu_to_dump64(s
, 0);
1215 buf
= g_malloc0(TARGET_PAGE_SIZE
);
1216 ret
= write_cache(&page_data
, buf
, TARGET_PAGE_SIZE
, false);
1219 dump_error(s
, "dump: failed to write page data (zero page)", errp
);
1223 offset_data
+= TARGET_PAGE_SIZE
;
1226 * dump memory to vmcore page by page. zero page will all be resided in the
1227 * first page of page section
1229 while (get_next_page(&block_iter
, &pfn_iter
, &buf
, s
)) {
1230 /* check zero page */
1231 if (is_zero_page(buf
, TARGET_PAGE_SIZE
)) {
1232 ret
= write_cache(&page_desc
, &pd_zero
, sizeof(PageDescriptor
),
1235 dump_error(s
, "dump: failed to write page desc", errp
);
1240 * not zero page, then:
1241 * 1. compress the page
1242 * 2. write the compressed page into the cache of page_data
1243 * 3. get page desc of the compressed page and write it into the
1244 * cache of page_desc
1246 * only one compression format will be used here, for
1247 * s->flag_compress is set. But when compression fails to work,
1248 * we fall back to save in plaintext.
1250 size_out
= len_buf_out
;
1251 if ((s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) &&
1252 (compress2(buf_out
, (uLongf
*)&size_out
, buf
,
1253 TARGET_PAGE_SIZE
, Z_BEST_SPEED
) == Z_OK
) &&
1254 (size_out
< TARGET_PAGE_SIZE
)) {
1255 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_ZLIB
);
1256 pd
.size
= cpu_to_dump32(s
, size_out
);
1258 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1260 dump_error(s
, "dump: failed to write page data", errp
);
1264 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) &&
1265 (lzo1x_1_compress(buf
, TARGET_PAGE_SIZE
, buf_out
,
1266 (lzo_uint
*)&size_out
, wrkmem
) == LZO_E_OK
) &&
1267 (size_out
< TARGET_PAGE_SIZE
)) {
1268 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_LZO
);
1269 pd
.size
= cpu_to_dump32(s
, size_out
);
1271 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1273 dump_error(s
, "dump: failed to write page data", errp
);
1277 #ifdef CONFIG_SNAPPY
1278 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) &&
1279 (snappy_compress((char *)buf
, TARGET_PAGE_SIZE
,
1280 (char *)buf_out
, &size_out
) == SNAPPY_OK
) &&
1281 (size_out
< TARGET_PAGE_SIZE
)) {
1282 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_SNAPPY
);
1283 pd
.size
= cpu_to_dump32(s
, size_out
);
1285 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1287 dump_error(s
, "dump: failed to write page data", errp
);
1293 * fall back to save in plaintext, size_out should be
1294 * assigned TARGET_PAGE_SIZE
1296 pd
.flags
= cpu_to_dump32(s
, 0);
1297 size_out
= TARGET_PAGE_SIZE
;
1298 pd
.size
= cpu_to_dump32(s
, size_out
);
1300 ret
= write_cache(&page_data
, buf
, TARGET_PAGE_SIZE
, false);
1302 dump_error(s
, "dump: failed to write page data", errp
);
1307 /* get and write page desc here */
1308 pd
.page_flags
= cpu_to_dump64(s
, 0);
1309 pd
.offset
= cpu_to_dump64(s
, offset_data
);
1310 offset_data
+= size_out
;
1312 ret
= write_cache(&page_desc
, &pd
, sizeof(PageDescriptor
), false);
1314 dump_error(s
, "dump: failed to write page desc", errp
);
1320 ret
= write_cache(&page_desc
, NULL
, 0, true);
1322 dump_error(s
, "dump: failed to sync cache for page_desc", errp
);
1325 ret
= write_cache(&page_data
, NULL
, 0, true);
1327 dump_error(s
, "dump: failed to sync cache for page_data", errp
);
1332 free_data_cache(&page_desc
);
1333 free_data_cache(&page_data
);
1342 static void create_kdump_vmcore(DumpState
*s
, Error
**errp
)
1345 Error
*local_err
= NULL
;
1348 * the kdump-compressed format is:
1350 * +------------------------------------------+ 0x0
1351 * | main header (struct disk_dump_header) |
1352 * |------------------------------------------+ block 1
1353 * | sub header (struct kdump_sub_header) |
1354 * |------------------------------------------+ block 2
1355 * | 1st-dump_bitmap |
1356 * |------------------------------------------+ block 2 + X blocks
1357 * | 2nd-dump_bitmap | (aligned by block)
1358 * |------------------------------------------+ block 2 + 2 * X blocks
1359 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1360 * | page desc for pfn 1 (struct page_desc) |
1362 * |------------------------------------------| (not aligned by block)
1363 * | page data (pfn 0) |
1364 * | page data (pfn 1) |
1366 * +------------------------------------------+
1369 ret
= write_start_flat_header(s
->fd
);
1371 dump_error(s
, "dump: failed to write start flat header", errp
);
1375 write_dump_header(s
, &local_err
);
1377 error_propagate(errp
, local_err
);
1381 write_dump_bitmap(s
, &local_err
);
1383 error_propagate(errp
, local_err
);
1387 write_dump_pages(s
, &local_err
);
1389 error_propagate(errp
, local_err
);
1393 ret
= write_end_flat_header(s
->fd
);
1395 dump_error(s
, "dump: failed to write end flat header", errp
);
1402 static ram_addr_t
get_start_block(DumpState
*s
)
1404 GuestPhysBlock
*block
;
1406 if (!s
->has_filter
) {
1407 s
->next_block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1411 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1412 if (block
->target_start
>= s
->begin
+ s
->length
||
1413 block
->target_end
<= s
->begin
) {
1414 /* This block is out of the range */
1418 s
->next_block
= block
;
1419 if (s
->begin
> block
->target_start
) {
1420 s
->start
= s
->begin
- block
->target_start
;
1430 static void get_max_mapnr(DumpState
*s
)
1432 GuestPhysBlock
*last_block
;
1434 last_block
= QTAILQ_LAST(&s
->guest_phys_blocks
.head
, GuestPhysBlockHead
);
1435 s
->max_mapnr
= paddr_to_pfn(last_block
->target_end
);
1438 static void dump_init(DumpState
*s
, int fd
, bool has_format
,
1439 DumpGuestMemoryFormat format
, bool paging
, bool has_filter
,
1440 int64_t begin
, int64_t length
, Error
**errp
)
1447 /* kdump-compressed is conflict with paging and filter */
1448 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1449 assert(!paging
&& !has_filter
);
1452 if (runstate_is_running()) {
1453 vm_stop(RUN_STATE_SAVE_VM
);
1459 /* If we use KVM, we should synchronize the registers before we get dump
1460 * info or physmap info.
1462 cpu_synchronize_all_states();
1469 s
->has_filter
= has_filter
;
1473 memory_mapping_list_init(&s
->list
);
1475 guest_phys_blocks_init(&s
->guest_phys_blocks
);
1476 guest_phys_blocks_append(&s
->guest_phys_blocks
);
1478 s
->start
= get_start_block(s
);
1479 if (s
->start
== -1) {
1480 error_set(errp
, QERR_INVALID_PARAMETER
, "begin");
1484 /* get dump info: endian, class and architecture.
1485 * If the target architecture is not supported, cpu_get_dump_info() will
1488 ret
= cpu_get_dump_info(&s
->dump_info
, &s
->guest_phys_blocks
);
1490 error_set(errp
, QERR_UNSUPPORTED
);
1494 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
1495 s
->dump_info
.d_machine
, nr_cpus
);
1496 if (s
->note_size
< 0) {
1497 error_set(errp
, QERR_UNSUPPORTED
);
1501 /* get memory mapping */
1503 qemu_get_guest_memory_mapping(&s
->list
, &s
->guest_phys_blocks
, &err
);
1505 error_propagate(errp
, err
);
1509 qemu_get_guest_simple_memory_mapping(&s
->list
, &s
->guest_phys_blocks
);
1512 s
->nr_cpus
= nr_cpus
;
1517 tmp
= DIV_ROUND_UP(DIV_ROUND_UP(s
->max_mapnr
, CHAR_BIT
), TARGET_PAGE_SIZE
);
1518 s
->len_dump_bitmap
= tmp
* TARGET_PAGE_SIZE
;
1520 /* init for kdump-compressed format */
1521 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1523 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
:
1524 s
->flag_compress
= DUMP_DH_COMPRESSED_ZLIB
;
1527 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
:
1529 if (lzo_init() != LZO_E_OK
) {
1530 error_setg(errp
, "failed to initialize the LZO library");
1534 s
->flag_compress
= DUMP_DH_COMPRESSED_LZO
;
1537 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
:
1538 s
->flag_compress
= DUMP_DH_COMPRESSED_SNAPPY
;
1542 s
->flag_compress
= 0;
1548 if (s
->has_filter
) {
1549 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
1553 * calculate phdr_num
1555 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1557 s
->phdr_num
= 1; /* PT_NOTE */
1558 if (s
->list
.num
< UINT16_MAX
- 2) {
1559 s
->phdr_num
+= s
->list
.num
;
1560 s
->have_section
= false;
1562 s
->have_section
= true;
1563 s
->phdr_num
= PN_XNUM
;
1564 s
->sh_info
= 1; /* PT_NOTE */
1566 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1567 if (s
->list
.num
<= UINT32_MAX
- 1) {
1568 s
->sh_info
+= s
->list
.num
;
1570 s
->sh_info
= UINT32_MAX
;
1574 if (s
->dump_info
.d_class
== ELFCLASS64
) {
1575 if (s
->have_section
) {
1576 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1577 sizeof(Elf64_Phdr
) * s
->sh_info
+
1578 sizeof(Elf64_Shdr
) + s
->note_size
;
1580 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1581 sizeof(Elf64_Phdr
) * s
->phdr_num
+ s
->note_size
;
1584 if (s
->have_section
) {
1585 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1586 sizeof(Elf32_Phdr
) * s
->sh_info
+
1587 sizeof(Elf32_Shdr
) + s
->note_size
;
1589 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1590 sizeof(Elf32_Phdr
) * s
->phdr_num
+ s
->note_size
;
1600 void qmp_dump_guest_memory(bool paging
, const char *file
, bool has_begin
,
1601 int64_t begin
, bool has_length
,
1602 int64_t length
, bool has_format
,
1603 DumpGuestMemoryFormat format
, Error
**errp
)
1608 Error
*local_err
= NULL
;
1611 * kdump-compressed format need the whole memory dumped, so paging or
1612 * filter is not supported here.
1614 if ((has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) &&
1615 (paging
|| has_begin
|| has_length
)) {
1616 error_setg(errp
, "kdump-compressed format doesn't support paging or "
1620 if (has_begin
&& !has_length
) {
1621 error_set(errp
, QERR_MISSING_PARAMETER
, "length");
1624 if (!has_begin
&& has_length
) {
1625 error_set(errp
, QERR_MISSING_PARAMETER
, "begin");
1629 /* check whether lzo/snappy is supported */
1631 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
) {
1632 error_setg(errp
, "kdump-lzo is not available now");
1637 #ifndef CONFIG_SNAPPY
1638 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
) {
1639 error_setg(errp
, "kdump-snappy is not available now");
1645 if (strstart(file
, "fd:", &p
)) {
1646 fd
= monitor_get_fd(cur_mon
, p
, errp
);
1653 if (strstart(file
, "file:", &p
)) {
1654 fd
= qemu_open(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
1656 error_setg_file_open(errp
, errno
, p
);
1662 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
1666 s
= g_malloc0(sizeof(DumpState
));
1668 dump_init(s
, fd
, has_format
, format
, paging
, has_begin
,
1669 begin
, length
, &local_err
);
1672 error_propagate(errp
, local_err
);
1676 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1677 create_kdump_vmcore(s
, errp
);
1679 create_vmcore(s
, errp
);
1685 DumpGuestMemoryCapability
*qmp_query_dump_guest_memory_capability(Error
**errp
)
1687 DumpGuestMemoryFormatList
*item
;
1688 DumpGuestMemoryCapability
*cap
=
1689 g_malloc0(sizeof(DumpGuestMemoryCapability
));
1691 /* elf is always available */
1692 item
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1693 cap
->formats
= item
;
1694 item
->value
= DUMP_GUEST_MEMORY_FORMAT_ELF
;
1696 /* kdump-zlib is always available */
1697 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1699 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
;
1701 /* add new item if kdump-lzo is available */
1703 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1705 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
;
1708 /* add new item if kdump-snappy is available */
1709 #ifdef CONFIG_SNAPPY
1710 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1712 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
;