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
;
607 Error
*local_err
= NULL
;
610 block
= s
->next_block
;
612 size
= block
->target_end
- block
->target_start
;
615 if (s
->begin
+ s
->length
< block
->target_end
) {
616 size
-= block
->target_end
- (s
->begin
+ s
->length
);
619 write_memory(s
, block
, s
->start
, size
, &local_err
);
621 error_propagate(errp
, local_err
);
625 } while (!get_next_block(s
, block
));
630 static void create_vmcore(DumpState
*s
, Error
**errp
)
632 Error
*local_err
= NULL
;
634 dump_begin(s
, &local_err
);
636 error_propagate(errp
, local_err
);
640 dump_iterate(s
, errp
);
643 static int write_start_flat_header(int fd
)
645 MakedumpfileHeader
*mh
;
648 QEMU_BUILD_BUG_ON(sizeof *mh
> MAX_SIZE_MDF_HEADER
);
649 mh
= g_malloc0(MAX_SIZE_MDF_HEADER
);
651 memcpy(mh
->signature
, MAKEDUMPFILE_SIGNATURE
,
652 MIN(sizeof mh
->signature
, sizeof MAKEDUMPFILE_SIGNATURE
));
654 mh
->type
= cpu_to_be64(TYPE_FLAT_HEADER
);
655 mh
->version
= cpu_to_be64(VERSION_FLAT_HEADER
);
658 written_size
= qemu_write_full(fd
, mh
, MAX_SIZE_MDF_HEADER
);
659 if (written_size
!= MAX_SIZE_MDF_HEADER
) {
667 static int write_end_flat_header(int fd
)
669 MakedumpfileDataHeader mdh
;
671 mdh
.offset
= END_FLAG_FLAT_HEADER
;
672 mdh
.buf_size
= END_FLAG_FLAT_HEADER
;
675 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
676 if (written_size
!= sizeof(mdh
)) {
683 static int write_buffer(int fd
, off_t offset
, const void *buf
, size_t size
)
686 MakedumpfileDataHeader mdh
;
688 mdh
.offset
= cpu_to_be64(offset
);
689 mdh
.buf_size
= cpu_to_be64(size
);
691 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
692 if (written_size
!= sizeof(mdh
)) {
696 written_size
= qemu_write_full(fd
, buf
, size
);
697 if (written_size
!= size
) {
704 static int buf_write_note(const void *buf
, size_t size
, void *opaque
)
706 DumpState
*s
= opaque
;
708 /* note_buf is not enough */
709 if (s
->note_buf_offset
+ size
> s
->note_size
) {
713 memcpy(s
->note_buf
+ s
->note_buf_offset
, buf
, size
);
715 s
->note_buf_offset
+= size
;
720 /* write common header, sub header and elf note to vmcore */
721 static void create_header32(DumpState
*s
, Error
**errp
)
723 DiskDumpHeader32
*dh
= NULL
;
724 KdumpSubHeader32
*kh
= NULL
;
727 uint32_t sub_hdr_size
;
728 uint32_t bitmap_blocks
;
730 uint64_t offset_note
;
731 Error
*local_err
= NULL
;
733 /* write common header, the version of kdump-compressed format is 6th */
734 size
= sizeof(DiskDumpHeader32
);
735 dh
= g_malloc0(size
);
737 strncpy(dh
->signature
, KDUMP_SIGNATURE
, strlen(KDUMP_SIGNATURE
));
738 dh
->header_version
= cpu_to_dump32(s
, 6);
739 block_size
= TARGET_PAGE_SIZE
;
740 dh
->block_size
= cpu_to_dump32(s
, block_size
);
741 sub_hdr_size
= sizeof(struct KdumpSubHeader32
) + s
->note_size
;
742 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
743 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
744 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
745 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
746 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
747 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
748 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
749 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
751 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
752 status
|= DUMP_DH_COMPRESSED_ZLIB
;
755 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
756 status
|= DUMP_DH_COMPRESSED_LZO
;
760 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
761 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
764 dh
->status
= cpu_to_dump32(s
, status
);
766 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
767 dump_error(s
, "dump: failed to write disk dump header", errp
);
771 /* write sub header */
772 size
= sizeof(KdumpSubHeader32
);
773 kh
= g_malloc0(size
);
775 /* 64bit max_mapnr_64 */
776 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
777 kh
->phys_base
= cpu_to_dump32(s
, PHYS_BASE
);
778 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
780 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
781 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
782 kh
->note_size
= cpu_to_dump32(s
, s
->note_size
);
784 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
785 block_size
, kh
, size
) < 0) {
786 dump_error(s
, "dump: failed to write kdump sub header", errp
);
791 s
->note_buf
= g_malloc0(s
->note_size
);
792 s
->note_buf_offset
= 0;
794 /* use s->note_buf to store notes temporarily */
795 write_elf32_notes(buf_write_note
, s
, &local_err
);
797 error_propagate(errp
, local_err
);
800 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
802 dump_error(s
, "dump: failed to write notes", errp
);
806 /* get offset of dump_bitmap */
807 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
810 /* get offset of page */
811 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
820 /* write common header, sub header and elf note to vmcore */
821 static void create_header64(DumpState
*s
, Error
**errp
)
823 DiskDumpHeader64
*dh
= NULL
;
824 KdumpSubHeader64
*kh
= NULL
;
827 uint32_t sub_hdr_size
;
828 uint32_t bitmap_blocks
;
830 uint64_t offset_note
;
831 Error
*local_err
= NULL
;
833 /* write common header, the version of kdump-compressed format is 6th */
834 size
= sizeof(DiskDumpHeader64
);
835 dh
= g_malloc0(size
);
837 strncpy(dh
->signature
, KDUMP_SIGNATURE
, strlen(KDUMP_SIGNATURE
));
838 dh
->header_version
= cpu_to_dump32(s
, 6);
839 block_size
= TARGET_PAGE_SIZE
;
840 dh
->block_size
= cpu_to_dump32(s
, block_size
);
841 sub_hdr_size
= sizeof(struct KdumpSubHeader64
) + s
->note_size
;
842 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
843 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
844 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
845 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
846 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
847 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
848 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
849 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
851 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
852 status
|= DUMP_DH_COMPRESSED_ZLIB
;
855 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
856 status
|= DUMP_DH_COMPRESSED_LZO
;
860 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
861 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
864 dh
->status
= cpu_to_dump32(s
, status
);
866 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
867 dump_error(s
, "dump: failed to write disk dump header", errp
);
871 /* write sub header */
872 size
= sizeof(KdumpSubHeader64
);
873 kh
= g_malloc0(size
);
875 /* 64bit max_mapnr_64 */
876 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
877 kh
->phys_base
= cpu_to_dump64(s
, PHYS_BASE
);
878 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
880 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
881 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
882 kh
->note_size
= cpu_to_dump64(s
, s
->note_size
);
884 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
885 block_size
, kh
, size
) < 0) {
886 dump_error(s
, "dump: failed to write kdump sub header", errp
);
891 s
->note_buf
= g_malloc0(s
->note_size
);
892 s
->note_buf_offset
= 0;
894 /* use s->note_buf to store notes temporarily */
895 write_elf64_notes(buf_write_note
, s
, &local_err
);
897 error_propagate(errp
, local_err
);
901 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
903 dump_error(s
, "dump: failed to write notes", errp
);
907 /* get offset of dump_bitmap */
908 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
911 /* get offset of page */
912 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
921 static void write_dump_header(DumpState
*s
, Error
**errp
)
923 Error
*local_err
= NULL
;
925 if (s
->dump_info
.d_class
== ELFCLASS32
) {
926 create_header32(s
, &local_err
);
928 create_header64(s
, &local_err
);
931 error_propagate(errp
, local_err
);
936 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
937 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
938 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
939 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
940 * vmcore, ie. synchronizing un-sync bit into vmcore.
942 static int set_dump_bitmap(uint64_t last_pfn
, uint64_t pfn
, bool value
,
943 uint8_t *buf
, DumpState
*s
)
945 off_t old_offset
, new_offset
;
946 off_t offset_bitmap1
, offset_bitmap2
;
949 /* should not set the previous place */
950 assert(last_pfn
<= pfn
);
953 * if the bit needed to be set is not cached in buf, flush the data in buf
955 * making new_offset be bigger than old_offset can also sync remained data
958 old_offset
= BUFSIZE_BITMAP
* (last_pfn
/ PFN_BUFBITMAP
);
959 new_offset
= BUFSIZE_BITMAP
* (pfn
/ PFN_BUFBITMAP
);
961 while (old_offset
< new_offset
) {
962 /* calculate the offset and write dump_bitmap */
963 offset_bitmap1
= s
->offset_dump_bitmap
+ old_offset
;
964 if (write_buffer(s
->fd
, offset_bitmap1
, buf
,
965 BUFSIZE_BITMAP
) < 0) {
969 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
970 offset_bitmap2
= s
->offset_dump_bitmap
+ s
->len_dump_bitmap
+
972 if (write_buffer(s
->fd
, offset_bitmap2
, buf
,
973 BUFSIZE_BITMAP
) < 0) {
977 memset(buf
, 0, BUFSIZE_BITMAP
);
978 old_offset
+= BUFSIZE_BITMAP
;
981 /* get the exact place of the bit in the buf, and set it */
982 byte
= (pfn
% PFN_BUFBITMAP
) / CHAR_BIT
;
983 bit
= (pfn
% PFN_BUFBITMAP
) % CHAR_BIT
;
985 buf
[byte
] |= 1u << bit
;
987 buf
[byte
] &= ~(1u << bit
);
994 * exam every page and return the page frame number and the address of the page.
995 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
996 * blocks, so block->target_start and block->target_end should be interal
997 * multiples of the target page size.
999 static bool get_next_page(GuestPhysBlock
**blockptr
, uint64_t *pfnptr
,
1000 uint8_t **bufptr
, DumpState
*s
)
1002 GuestPhysBlock
*block
= *blockptr
;
1006 /* block == NULL means the start of the iteration */
1008 block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1010 assert((block
->target_start
& ~TARGET_PAGE_MASK
) == 0);
1011 assert((block
->target_end
& ~TARGET_PAGE_MASK
) == 0);
1012 *pfnptr
= paddr_to_pfn(block
->target_start
);
1014 *bufptr
= block
->host_addr
;
1019 *pfnptr
= *pfnptr
+ 1;
1020 addr
= pfn_to_paddr(*pfnptr
);
1022 if ((addr
>= block
->target_start
) &&
1023 (addr
+ TARGET_PAGE_SIZE
<= block
->target_end
)) {
1024 buf
= block
->host_addr
+ (addr
- block
->target_start
);
1026 /* the next page is in the next block */
1027 block
= QTAILQ_NEXT(block
, next
);
1032 assert((block
->target_start
& ~TARGET_PAGE_MASK
) == 0);
1033 assert((block
->target_end
& ~TARGET_PAGE_MASK
) == 0);
1034 *pfnptr
= paddr_to_pfn(block
->target_start
);
1035 buf
= block
->host_addr
;
1045 static void write_dump_bitmap(DumpState
*s
, Error
**errp
)
1048 uint64_t last_pfn
, pfn
;
1049 void *dump_bitmap_buf
;
1050 size_t num_dumpable
;
1051 GuestPhysBlock
*block_iter
= NULL
;
1053 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1054 dump_bitmap_buf
= g_malloc0(BUFSIZE_BITMAP
);
1060 * exam memory page by page, and set the bit in dump_bitmap corresponded
1061 * to the existing page.
1063 while (get_next_page(&block_iter
, &pfn
, NULL
, s
)) {
1064 ret
= set_dump_bitmap(last_pfn
, pfn
, true, dump_bitmap_buf
, s
);
1066 dump_error(s
, "dump: failed to set dump_bitmap", errp
);
1075 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1076 * set last_pfn + PFN_BUFBITMAP to 0 and those set but un-sync bit will be
1077 * synchronized into vmcore.
1079 if (num_dumpable
> 0) {
1080 ret
= set_dump_bitmap(last_pfn
, last_pfn
+ PFN_BUFBITMAP
, false,
1081 dump_bitmap_buf
, s
);
1083 dump_error(s
, "dump: failed to sync dump_bitmap", errp
);
1088 /* number of dumpable pages that will be dumped later */
1089 s
->num_dumpable
= num_dumpable
;
1092 g_free(dump_bitmap_buf
);
1095 static void prepare_data_cache(DataCache
*data_cache
, DumpState
*s
,
1098 data_cache
->fd
= s
->fd
;
1099 data_cache
->data_size
= 0;
1100 data_cache
->buf_size
= BUFSIZE_DATA_CACHE
;
1101 data_cache
->buf
= g_malloc0(BUFSIZE_DATA_CACHE
);
1102 data_cache
->offset
= offset
;
1105 static int write_cache(DataCache
*dc
, const void *buf
, size_t size
,
1109 * dc->buf_size should not be less than size, otherwise dc will never be
1112 assert(size
<= dc
->buf_size
);
1115 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1116 * otherwise check if the space is enough for caching data in buf, if not,
1117 * write the data in dc->buf to dc->fd and reset dc->buf
1119 if ((!flag_sync
&& dc
->data_size
+ size
> dc
->buf_size
) ||
1120 (flag_sync
&& dc
->data_size
> 0)) {
1121 if (write_buffer(dc
->fd
, dc
->offset
, dc
->buf
, dc
->data_size
) < 0) {
1125 dc
->offset
+= dc
->data_size
;
1130 memcpy(dc
->buf
+ dc
->data_size
, buf
, size
);
1131 dc
->data_size
+= size
;
1137 static void free_data_cache(DataCache
*data_cache
)
1139 g_free(data_cache
->buf
);
1142 static size_t get_len_buf_out(size_t page_size
, uint32_t flag_compress
)
1144 switch (flag_compress
) {
1145 case DUMP_DH_COMPRESSED_ZLIB
:
1146 return compressBound(page_size
);
1148 case DUMP_DH_COMPRESSED_LZO
:
1150 * LZO will expand incompressible data by a little amount. Please check
1151 * the following URL to see the expansion calculation:
1152 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1154 return page_size
+ page_size
/ 16 + 64 + 3;
1156 #ifdef CONFIG_SNAPPY
1157 case DUMP_DH_COMPRESSED_SNAPPY
:
1158 return snappy_max_compressed_length(page_size
);
1165 * check if the page is all 0
1167 static inline bool is_zero_page(const uint8_t *buf
, size_t page_size
)
1169 return buffer_is_zero(buf
, page_size
);
1172 static void write_dump_pages(DumpState
*s
, Error
**errp
)
1175 DataCache page_desc
, page_data
;
1176 size_t len_buf_out
, size_out
;
1178 lzo_bytep wrkmem
= NULL
;
1180 uint8_t *buf_out
= NULL
;
1181 off_t offset_desc
, offset_data
;
1182 PageDescriptor pd
, pd_zero
;
1184 GuestPhysBlock
*block_iter
= NULL
;
1187 /* get offset of page_desc and page_data in dump file */
1188 offset_desc
= s
->offset_page
;
1189 offset_data
= offset_desc
+ sizeof(PageDescriptor
) * s
->num_dumpable
;
1191 prepare_data_cache(&page_desc
, s
, offset_desc
);
1192 prepare_data_cache(&page_data
, s
, offset_data
);
1194 /* prepare buffer to store compressed data */
1195 len_buf_out
= get_len_buf_out(TARGET_PAGE_SIZE
, s
->flag_compress
);
1196 assert(len_buf_out
!= 0);
1199 wrkmem
= g_malloc(LZO1X_1_MEM_COMPRESS
);
1202 buf_out
= g_malloc(len_buf_out
);
1205 * init zero page's page_desc and page_data, because every zero page
1206 * uses the same page_data
1208 pd_zero
.size
= cpu_to_dump32(s
, TARGET_PAGE_SIZE
);
1209 pd_zero
.flags
= cpu_to_dump32(s
, 0);
1210 pd_zero
.offset
= cpu_to_dump64(s
, offset_data
);
1211 pd_zero
.page_flags
= cpu_to_dump64(s
, 0);
1212 buf
= g_malloc0(TARGET_PAGE_SIZE
);
1213 ret
= write_cache(&page_data
, buf
, TARGET_PAGE_SIZE
, false);
1216 dump_error(s
, "dump: failed to write page data (zero page)", errp
);
1220 offset_data
+= TARGET_PAGE_SIZE
;
1223 * dump memory to vmcore page by page. zero page will all be resided in the
1224 * first page of page section
1226 while (get_next_page(&block_iter
, &pfn_iter
, &buf
, s
)) {
1227 /* check zero page */
1228 if (is_zero_page(buf
, TARGET_PAGE_SIZE
)) {
1229 ret
= write_cache(&page_desc
, &pd_zero
, sizeof(PageDescriptor
),
1232 dump_error(s
, "dump: failed to write page desc", errp
);
1237 * not zero page, then:
1238 * 1. compress the page
1239 * 2. write the compressed page into the cache of page_data
1240 * 3. get page desc of the compressed page and write it into the
1241 * cache of page_desc
1243 * only one compression format will be used here, for
1244 * s->flag_compress is set. But when compression fails to work,
1245 * we fall back to save in plaintext.
1247 size_out
= len_buf_out
;
1248 if ((s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) &&
1249 (compress2(buf_out
, (uLongf
*)&size_out
, buf
,
1250 TARGET_PAGE_SIZE
, Z_BEST_SPEED
) == Z_OK
) &&
1251 (size_out
< TARGET_PAGE_SIZE
)) {
1252 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_ZLIB
);
1253 pd
.size
= cpu_to_dump32(s
, size_out
);
1255 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1257 dump_error(s
, "dump: failed to write page data", errp
);
1261 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) &&
1262 (lzo1x_1_compress(buf
, TARGET_PAGE_SIZE
, buf_out
,
1263 (lzo_uint
*)&size_out
, wrkmem
) == LZO_E_OK
) &&
1264 (size_out
< TARGET_PAGE_SIZE
)) {
1265 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_LZO
);
1266 pd
.size
= cpu_to_dump32(s
, size_out
);
1268 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1270 dump_error(s
, "dump: failed to write page data", errp
);
1274 #ifdef CONFIG_SNAPPY
1275 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) &&
1276 (snappy_compress((char *)buf
, TARGET_PAGE_SIZE
,
1277 (char *)buf_out
, &size_out
) == SNAPPY_OK
) &&
1278 (size_out
< TARGET_PAGE_SIZE
)) {
1279 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_SNAPPY
);
1280 pd
.size
= cpu_to_dump32(s
, size_out
);
1282 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1284 dump_error(s
, "dump: failed to write page data", errp
);
1290 * fall back to save in plaintext, size_out should be
1291 * assigned TARGET_PAGE_SIZE
1293 pd
.flags
= cpu_to_dump32(s
, 0);
1294 size_out
= TARGET_PAGE_SIZE
;
1295 pd
.size
= cpu_to_dump32(s
, size_out
);
1297 ret
= write_cache(&page_data
, buf
, TARGET_PAGE_SIZE
, false);
1299 dump_error(s
, "dump: failed to write page data", errp
);
1304 /* get and write page desc here */
1305 pd
.page_flags
= cpu_to_dump64(s
, 0);
1306 pd
.offset
= cpu_to_dump64(s
, offset_data
);
1307 offset_data
+= size_out
;
1309 ret
= write_cache(&page_desc
, &pd
, sizeof(PageDescriptor
), false);
1311 dump_error(s
, "dump: failed to write page desc", errp
);
1317 ret
= write_cache(&page_desc
, NULL
, 0, true);
1319 dump_error(s
, "dump: failed to sync cache for page_desc", errp
);
1322 ret
= write_cache(&page_data
, NULL
, 0, true);
1324 dump_error(s
, "dump: failed to sync cache for page_data", errp
);
1329 free_data_cache(&page_desc
);
1330 free_data_cache(&page_data
);
1339 static void create_kdump_vmcore(DumpState
*s
, Error
**errp
)
1342 Error
*local_err
= NULL
;
1345 * the kdump-compressed format is:
1347 * +------------------------------------------+ 0x0
1348 * | main header (struct disk_dump_header) |
1349 * |------------------------------------------+ block 1
1350 * | sub header (struct kdump_sub_header) |
1351 * |------------------------------------------+ block 2
1352 * | 1st-dump_bitmap |
1353 * |------------------------------------------+ block 2 + X blocks
1354 * | 2nd-dump_bitmap | (aligned by block)
1355 * |------------------------------------------+ block 2 + 2 * X blocks
1356 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1357 * | page desc for pfn 1 (struct page_desc) |
1359 * |------------------------------------------| (not aligned by block)
1360 * | page data (pfn 0) |
1361 * | page data (pfn 1) |
1363 * +------------------------------------------+
1366 ret
= write_start_flat_header(s
->fd
);
1368 dump_error(s
, "dump: failed to write start flat header", errp
);
1372 write_dump_header(s
, &local_err
);
1374 error_propagate(errp
, local_err
);
1378 write_dump_bitmap(s
, &local_err
);
1380 error_propagate(errp
, local_err
);
1384 write_dump_pages(s
, &local_err
);
1386 error_propagate(errp
, local_err
);
1390 ret
= write_end_flat_header(s
->fd
);
1392 dump_error(s
, "dump: failed to write end flat header", errp
);
1399 static ram_addr_t
get_start_block(DumpState
*s
)
1401 GuestPhysBlock
*block
;
1403 if (!s
->has_filter
) {
1404 s
->next_block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1408 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1409 if (block
->target_start
>= s
->begin
+ s
->length
||
1410 block
->target_end
<= s
->begin
) {
1411 /* This block is out of the range */
1415 s
->next_block
= block
;
1416 if (s
->begin
> block
->target_start
) {
1417 s
->start
= s
->begin
- block
->target_start
;
1427 static void get_max_mapnr(DumpState
*s
)
1429 GuestPhysBlock
*last_block
;
1431 last_block
= QTAILQ_LAST(&s
->guest_phys_blocks
.head
, GuestPhysBlockHead
);
1432 s
->max_mapnr
= paddr_to_pfn(last_block
->target_end
);
1435 static void dump_init(DumpState
*s
, int fd
, bool has_format
,
1436 DumpGuestMemoryFormat format
, bool paging
, bool has_filter
,
1437 int64_t begin
, int64_t length
, Error
**errp
)
1444 /* kdump-compressed is conflict with paging and filter */
1445 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1446 assert(!paging
&& !has_filter
);
1449 if (runstate_is_running()) {
1450 vm_stop(RUN_STATE_SAVE_VM
);
1456 /* If we use KVM, we should synchronize the registers before we get dump
1457 * info or physmap info.
1459 cpu_synchronize_all_states();
1466 s
->has_filter
= has_filter
;
1470 memory_mapping_list_init(&s
->list
);
1472 guest_phys_blocks_init(&s
->guest_phys_blocks
);
1473 guest_phys_blocks_append(&s
->guest_phys_blocks
);
1475 s
->start
= get_start_block(s
);
1476 if (s
->start
== -1) {
1477 error_set(errp
, QERR_INVALID_PARAMETER
, "begin");
1481 /* get dump info: endian, class and architecture.
1482 * If the target architecture is not supported, cpu_get_dump_info() will
1485 ret
= cpu_get_dump_info(&s
->dump_info
, &s
->guest_phys_blocks
);
1487 error_set(errp
, QERR_UNSUPPORTED
);
1491 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
1492 s
->dump_info
.d_machine
, nr_cpus
);
1493 if (s
->note_size
< 0) {
1494 error_set(errp
, QERR_UNSUPPORTED
);
1498 /* get memory mapping */
1500 qemu_get_guest_memory_mapping(&s
->list
, &s
->guest_phys_blocks
, &err
);
1502 error_propagate(errp
, err
);
1506 qemu_get_guest_simple_memory_mapping(&s
->list
, &s
->guest_phys_blocks
);
1509 s
->nr_cpus
= nr_cpus
;
1514 tmp
= DIV_ROUND_UP(DIV_ROUND_UP(s
->max_mapnr
, CHAR_BIT
), TARGET_PAGE_SIZE
);
1515 s
->len_dump_bitmap
= tmp
* TARGET_PAGE_SIZE
;
1517 /* init for kdump-compressed format */
1518 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1520 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
:
1521 s
->flag_compress
= DUMP_DH_COMPRESSED_ZLIB
;
1524 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
:
1526 if (lzo_init() != LZO_E_OK
) {
1527 error_setg(errp
, "failed to initialize the LZO library");
1531 s
->flag_compress
= DUMP_DH_COMPRESSED_LZO
;
1534 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
:
1535 s
->flag_compress
= DUMP_DH_COMPRESSED_SNAPPY
;
1539 s
->flag_compress
= 0;
1545 if (s
->has_filter
) {
1546 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
1550 * calculate phdr_num
1552 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1554 s
->phdr_num
= 1; /* PT_NOTE */
1555 if (s
->list
.num
< UINT16_MAX
- 2) {
1556 s
->phdr_num
+= s
->list
.num
;
1557 s
->have_section
= false;
1559 s
->have_section
= true;
1560 s
->phdr_num
= PN_XNUM
;
1561 s
->sh_info
= 1; /* PT_NOTE */
1563 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1564 if (s
->list
.num
<= UINT32_MAX
- 1) {
1565 s
->sh_info
+= s
->list
.num
;
1567 s
->sh_info
= UINT32_MAX
;
1571 if (s
->dump_info
.d_class
== ELFCLASS64
) {
1572 if (s
->have_section
) {
1573 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1574 sizeof(Elf64_Phdr
) * s
->sh_info
+
1575 sizeof(Elf64_Shdr
) + s
->note_size
;
1577 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1578 sizeof(Elf64_Phdr
) * s
->phdr_num
+ s
->note_size
;
1581 if (s
->have_section
) {
1582 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1583 sizeof(Elf32_Phdr
) * s
->sh_info
+
1584 sizeof(Elf32_Shdr
) + s
->note_size
;
1586 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1587 sizeof(Elf32_Phdr
) * s
->phdr_num
+ s
->note_size
;
1597 void qmp_dump_guest_memory(bool paging
, const char *file
, bool has_begin
,
1598 int64_t begin
, bool has_length
,
1599 int64_t length
, bool has_format
,
1600 DumpGuestMemoryFormat format
, Error
**errp
)
1605 Error
*local_err
= NULL
;
1608 * kdump-compressed format need the whole memory dumped, so paging or
1609 * filter is not supported here.
1611 if ((has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) &&
1612 (paging
|| has_begin
|| has_length
)) {
1613 error_setg(errp
, "kdump-compressed format doesn't support paging or "
1617 if (has_begin
&& !has_length
) {
1618 error_set(errp
, QERR_MISSING_PARAMETER
, "length");
1621 if (!has_begin
&& has_length
) {
1622 error_set(errp
, QERR_MISSING_PARAMETER
, "begin");
1626 /* check whether lzo/snappy is supported */
1628 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
) {
1629 error_setg(errp
, "kdump-lzo is not available now");
1634 #ifndef CONFIG_SNAPPY
1635 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
) {
1636 error_setg(errp
, "kdump-snappy is not available now");
1642 if (strstart(file
, "fd:", &p
)) {
1643 fd
= monitor_get_fd(cur_mon
, p
, errp
);
1650 if (strstart(file
, "file:", &p
)) {
1651 fd
= qemu_open(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
1653 error_setg_file_open(errp
, errno
, p
);
1659 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
1663 s
= g_malloc0(sizeof(DumpState
));
1665 dump_init(s
, fd
, has_format
, format
, paging
, has_begin
,
1666 begin
, length
, &local_err
);
1669 error_propagate(errp
, local_err
);
1673 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1674 create_kdump_vmcore(s
, errp
);
1676 create_vmcore(s
, errp
);
1682 DumpGuestMemoryCapability
*qmp_query_dump_guest_memory_capability(Error
**errp
)
1684 DumpGuestMemoryFormatList
*item
;
1685 DumpGuestMemoryCapability
*cap
=
1686 g_malloc0(sizeof(DumpGuestMemoryCapability
));
1688 /* elf is always available */
1689 item
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1690 cap
->formats
= item
;
1691 item
->value
= DUMP_GUEST_MEMORY_FORMAT_ELF
;
1693 /* kdump-zlib is always available */
1694 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1696 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
;
1698 /* add new item if kdump-lzo is available */
1700 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1702 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
;
1705 /* add new item if kdump-snappy is available */
1706 #ifdef CONFIG_SNAPPY
1707 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1709 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
;