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-common.h"
18 #include "exec/cpu-all.h"
19 #include "exec/hwaddr.h"
20 #include "monitor/monitor.h"
21 #include "sysemu/kvm.h"
22 #include "sysemu/dump.h"
23 #include "sysemu/sysemu.h"
24 #include "sysemu/memory_mapping.h"
25 #include "sysemu/cpus.h"
26 #include "qapi/qmp/qerror.h"
27 #include "qmp-commands.h"
31 #include <lzo/lzo1x.h>
36 #ifndef ELF_MACHINE_UNAME
37 #define ELF_MACHINE_UNAME "Unknown"
40 uint16_t cpu_to_dump16(DumpState
*s
, uint16_t val
)
42 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
43 val
= cpu_to_le16(val
);
45 val
= cpu_to_be16(val
);
51 uint32_t cpu_to_dump32(DumpState
*s
, uint32_t val
)
53 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
54 val
= cpu_to_le32(val
);
56 val
= cpu_to_be32(val
);
62 uint64_t cpu_to_dump64(DumpState
*s
, uint64_t val
)
64 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
65 val
= cpu_to_le64(val
);
67 val
= cpu_to_be64(val
);
73 static int dump_cleanup(DumpState
*s
)
75 guest_phys_blocks_free(&s
->guest_phys_blocks
);
76 memory_mapping_list_free(&s
->list
);
85 static int fd_write_vmcore(const void *buf
, size_t size
, void *opaque
)
87 DumpState
*s
= opaque
;
90 written_size
= qemu_write_full(s
->fd
, buf
, size
);
91 if (written_size
!= size
) {
98 static void write_elf64_header(DumpState
*s
, Error
**errp
)
100 Elf64_Ehdr elf_header
;
103 memset(&elf_header
, 0, sizeof(Elf64_Ehdr
));
104 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
105 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS64
;
106 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
107 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
108 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
109 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
110 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
111 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
112 elf_header
.e_phoff
= cpu_to_dump64(s
, sizeof(Elf64_Ehdr
));
113 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf64_Phdr
));
114 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
115 if (s
->have_section
) {
116 uint64_t shoff
= sizeof(Elf64_Ehdr
) + sizeof(Elf64_Phdr
) * s
->sh_info
;
118 elf_header
.e_shoff
= cpu_to_dump64(s
, shoff
);
119 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf64_Shdr
));
120 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
123 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
125 error_setg(errp
, "dump: failed to write elf header");
129 static void write_elf32_header(DumpState
*s
, Error
**errp
)
131 Elf32_Ehdr elf_header
;
134 memset(&elf_header
, 0, sizeof(Elf32_Ehdr
));
135 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
136 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS32
;
137 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
138 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
139 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
140 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
141 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
142 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
143 elf_header
.e_phoff
= cpu_to_dump32(s
, sizeof(Elf32_Ehdr
));
144 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf32_Phdr
));
145 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
146 if (s
->have_section
) {
147 uint32_t shoff
= sizeof(Elf32_Ehdr
) + sizeof(Elf32_Phdr
) * s
->sh_info
;
149 elf_header
.e_shoff
= cpu_to_dump32(s
, shoff
);
150 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf32_Shdr
));
151 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
154 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
156 error_setg(errp
, "dump: failed to write elf header");
160 static void write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
161 int phdr_index
, hwaddr offset
,
162 hwaddr filesz
, Error
**errp
)
167 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
168 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
169 phdr
.p_offset
= cpu_to_dump64(s
, offset
);
170 phdr
.p_paddr
= cpu_to_dump64(s
, memory_mapping
->phys_addr
);
171 phdr
.p_filesz
= cpu_to_dump64(s
, filesz
);
172 phdr
.p_memsz
= cpu_to_dump64(s
, memory_mapping
->length
);
173 phdr
.p_vaddr
= cpu_to_dump64(s
, memory_mapping
->virt_addr
);
175 assert(memory_mapping
->length
>= filesz
);
177 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
179 error_setg(errp
, "dump: failed to write program header table");
183 static void write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
184 int phdr_index
, hwaddr offset
,
185 hwaddr filesz
, Error
**errp
)
190 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
191 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
192 phdr
.p_offset
= cpu_to_dump32(s
, offset
);
193 phdr
.p_paddr
= cpu_to_dump32(s
, memory_mapping
->phys_addr
);
194 phdr
.p_filesz
= cpu_to_dump32(s
, filesz
);
195 phdr
.p_memsz
= cpu_to_dump32(s
, memory_mapping
->length
);
196 phdr
.p_vaddr
= cpu_to_dump32(s
, memory_mapping
->virt_addr
);
198 assert(memory_mapping
->length
>= filesz
);
200 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
202 error_setg(errp
, "dump: failed to write program header table");
206 static void write_elf64_note(DumpState
*s
, Error
**errp
)
209 hwaddr begin
= s
->memory_offset
- s
->note_size
;
212 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
213 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
214 phdr
.p_offset
= cpu_to_dump64(s
, begin
);
216 phdr
.p_filesz
= cpu_to_dump64(s
, s
->note_size
);
217 phdr
.p_memsz
= cpu_to_dump64(s
, s
->note_size
);
220 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
222 error_setg(errp
, "dump: failed to write program header table");
226 static inline int cpu_index(CPUState
*cpu
)
228 return cpu
->cpu_index
+ 1;
231 static void write_elf64_notes(WriteCoreDumpFunction f
, DumpState
*s
,
240 ret
= cpu_write_elf64_note(f
, cpu
, id
, s
);
242 error_setg(errp
, "dump: failed to write elf notes");
248 ret
= cpu_write_elf64_qemunote(f
, cpu
, s
);
250 error_setg(errp
, "dump: failed to write CPU status");
256 static void write_elf32_note(DumpState
*s
, Error
**errp
)
258 hwaddr begin
= s
->memory_offset
- s
->note_size
;
262 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
263 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
264 phdr
.p_offset
= cpu_to_dump32(s
, begin
);
266 phdr
.p_filesz
= cpu_to_dump32(s
, s
->note_size
);
267 phdr
.p_memsz
= cpu_to_dump32(s
, s
->note_size
);
270 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
272 error_setg(errp
, "dump: failed to write program header table");
276 static void write_elf32_notes(WriteCoreDumpFunction f
, DumpState
*s
,
285 ret
= cpu_write_elf32_note(f
, cpu
, id
, s
);
287 error_setg(errp
, "dump: failed to write elf notes");
293 ret
= cpu_write_elf32_qemunote(f
, cpu
, s
);
295 error_setg(errp
, "dump: failed to write CPU status");
301 static void write_elf_section(DumpState
*s
, int type
, Error
**errp
)
310 shdr_size
= sizeof(Elf32_Shdr
);
311 memset(&shdr32
, 0, shdr_size
);
312 shdr32
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
315 shdr_size
= sizeof(Elf64_Shdr
);
316 memset(&shdr64
, 0, shdr_size
);
317 shdr64
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
321 ret
= fd_write_vmcore(&shdr
, shdr_size
, s
);
323 error_setg(errp
, "dump: failed to write section header table");
327 static void write_data(DumpState
*s
, void *buf
, int length
, Error
**errp
)
331 ret
= fd_write_vmcore(buf
, length
, s
);
333 error_setg(errp
, "dump: failed to save memory");
337 /* write the memory to vmcore. 1 page per I/O. */
338 static void write_memory(DumpState
*s
, GuestPhysBlock
*block
, ram_addr_t start
,
339 int64_t size
, Error
**errp
)
342 Error
*local_err
= NULL
;
344 for (i
= 0; i
< size
/ s
->dump_info
.page_size
; i
++) {
345 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
346 s
->dump_info
.page_size
, &local_err
);
348 error_propagate(errp
, local_err
);
353 if ((size
% s
->dump_info
.page_size
) != 0) {
354 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
355 size
% s
->dump_info
.page_size
, &local_err
);
357 error_propagate(errp
, local_err
);
363 /* get the memory's offset and size in the vmcore */
364 static void get_offset_range(hwaddr phys_addr
,
365 ram_addr_t mapping_length
,
370 GuestPhysBlock
*block
;
371 hwaddr offset
= s
->memory_offset
;
372 int64_t size_in_block
, start
;
374 /* When the memory is not stored into vmcore, offset will be -1 */
379 if (phys_addr
< s
->begin
|| phys_addr
>= s
->begin
+ s
->length
) {
384 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
386 if (block
->target_start
>= s
->begin
+ s
->length
||
387 block
->target_end
<= s
->begin
) {
388 /* This block is out of the range */
392 if (s
->begin
<= block
->target_start
) {
393 start
= block
->target_start
;
398 size_in_block
= block
->target_end
- start
;
399 if (s
->begin
+ s
->length
< block
->target_end
) {
400 size_in_block
-= block
->target_end
- (s
->begin
+ s
->length
);
403 start
= block
->target_start
;
404 size_in_block
= block
->target_end
- block
->target_start
;
407 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
408 *p_offset
= phys_addr
- start
+ offset
;
410 /* The offset range mapped from the vmcore file must not spill over
411 * the GuestPhysBlock, clamp it. The rest of the mapping will be
412 * zero-filled in memory at load time; see
413 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
415 *p_filesz
= phys_addr
+ mapping_length
<= start
+ size_in_block
?
417 size_in_block
- (phys_addr
- start
);
421 offset
+= size_in_block
;
425 static void write_elf_loads(DumpState
*s
, Error
**errp
)
427 hwaddr offset
, filesz
;
428 MemoryMapping
*memory_mapping
;
429 uint32_t phdr_index
= 1;
431 Error
*local_err
= NULL
;
433 if (s
->have_section
) {
434 max_index
= s
->sh_info
;
436 max_index
= s
->phdr_num
;
439 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
440 get_offset_range(memory_mapping
->phys_addr
,
441 memory_mapping
->length
,
442 s
, &offset
, &filesz
);
443 if (s
->dump_info
.d_class
== ELFCLASS64
) {
444 write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
,
447 write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
,
452 error_propagate(errp
, local_err
);
456 if (phdr_index
>= max_index
) {
462 /* write elf header, PT_NOTE and elf note to vmcore. */
463 static void dump_begin(DumpState
*s
, Error
**errp
)
465 Error
*local_err
= NULL
;
468 * the vmcore's format is:
487 * we only know where the memory is saved after we write elf note into
491 /* write elf header to vmcore */
492 if (s
->dump_info
.d_class
== ELFCLASS64
) {
493 write_elf64_header(s
, &local_err
);
495 write_elf32_header(s
, &local_err
);
498 error_propagate(errp
, local_err
);
502 if (s
->dump_info
.d_class
== ELFCLASS64
) {
503 /* write PT_NOTE to vmcore */
504 write_elf64_note(s
, &local_err
);
506 error_propagate(errp
, local_err
);
510 /* write all PT_LOAD to vmcore */
511 write_elf_loads(s
, &local_err
);
513 error_propagate(errp
, local_err
);
517 /* write section to vmcore */
518 if (s
->have_section
) {
519 write_elf_section(s
, 1, &local_err
);
521 error_propagate(errp
, local_err
);
526 /* write notes to vmcore */
527 write_elf64_notes(fd_write_vmcore
, s
, &local_err
);
529 error_propagate(errp
, local_err
);
533 /* write PT_NOTE to vmcore */
534 write_elf32_note(s
, &local_err
);
536 error_propagate(errp
, local_err
);
540 /* write all PT_LOAD to vmcore */
541 write_elf_loads(s
, &local_err
);
543 error_propagate(errp
, local_err
);
547 /* write section to vmcore */
548 if (s
->have_section
) {
549 write_elf_section(s
, 0, &local_err
);
551 error_propagate(errp
, local_err
);
556 /* write notes to vmcore */
557 write_elf32_notes(fd_write_vmcore
, s
, &local_err
);
559 error_propagate(errp
, local_err
);
565 static int get_next_block(DumpState
*s
, GuestPhysBlock
*block
)
568 block
= QTAILQ_NEXT(block
, next
);
575 s
->next_block
= block
;
577 if (block
->target_start
>= s
->begin
+ s
->length
||
578 block
->target_end
<= s
->begin
) {
579 /* This block is out of the range */
583 if (s
->begin
> block
->target_start
) {
584 s
->start
= s
->begin
- block
->target_start
;
592 /* write all memory to vmcore */
593 static void dump_iterate(DumpState
*s
, Error
**errp
)
595 GuestPhysBlock
*block
;
597 Error
*local_err
= NULL
;
600 block
= s
->next_block
;
602 size
= block
->target_end
- block
->target_start
;
605 if (s
->begin
+ s
->length
< block
->target_end
) {
606 size
-= block
->target_end
- (s
->begin
+ s
->length
);
609 write_memory(s
, block
, s
->start
, size
, &local_err
);
611 error_propagate(errp
, local_err
);
615 } while (!get_next_block(s
, block
));
618 static void create_vmcore(DumpState
*s
, Error
**errp
)
620 Error
*local_err
= NULL
;
622 dump_begin(s
, &local_err
);
624 error_propagate(errp
, local_err
);
628 dump_iterate(s
, errp
);
631 static int write_start_flat_header(int fd
)
633 MakedumpfileHeader
*mh
;
636 QEMU_BUILD_BUG_ON(sizeof *mh
> MAX_SIZE_MDF_HEADER
);
637 mh
= g_malloc0(MAX_SIZE_MDF_HEADER
);
639 memcpy(mh
->signature
, MAKEDUMPFILE_SIGNATURE
,
640 MIN(sizeof mh
->signature
, sizeof MAKEDUMPFILE_SIGNATURE
));
642 mh
->type
= cpu_to_be64(TYPE_FLAT_HEADER
);
643 mh
->version
= cpu_to_be64(VERSION_FLAT_HEADER
);
646 written_size
= qemu_write_full(fd
, mh
, MAX_SIZE_MDF_HEADER
);
647 if (written_size
!= MAX_SIZE_MDF_HEADER
) {
655 static int write_end_flat_header(int fd
)
657 MakedumpfileDataHeader mdh
;
659 mdh
.offset
= END_FLAG_FLAT_HEADER
;
660 mdh
.buf_size
= END_FLAG_FLAT_HEADER
;
663 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
664 if (written_size
!= sizeof(mdh
)) {
671 static int write_buffer(int fd
, off_t offset
, const void *buf
, size_t size
)
674 MakedumpfileDataHeader mdh
;
676 mdh
.offset
= cpu_to_be64(offset
);
677 mdh
.buf_size
= cpu_to_be64(size
);
679 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
680 if (written_size
!= sizeof(mdh
)) {
684 written_size
= qemu_write_full(fd
, buf
, size
);
685 if (written_size
!= size
) {
692 static int buf_write_note(const void *buf
, size_t size
, void *opaque
)
694 DumpState
*s
= opaque
;
696 /* note_buf is not enough */
697 if (s
->note_buf_offset
+ size
> s
->note_size
) {
701 memcpy(s
->note_buf
+ s
->note_buf_offset
, buf
, size
);
703 s
->note_buf_offset
+= size
;
708 /* write common header, sub header and elf note to vmcore */
709 static void create_header32(DumpState
*s
, Error
**errp
)
711 DiskDumpHeader32
*dh
= NULL
;
712 KdumpSubHeader32
*kh
= NULL
;
715 uint32_t sub_hdr_size
;
716 uint32_t bitmap_blocks
;
718 uint64_t offset_note
;
719 Error
*local_err
= NULL
;
721 /* write common header, the version of kdump-compressed format is 6th */
722 size
= sizeof(DiskDumpHeader32
);
723 dh
= g_malloc0(size
);
725 strncpy(dh
->signature
, KDUMP_SIGNATURE
, strlen(KDUMP_SIGNATURE
));
726 dh
->header_version
= cpu_to_dump32(s
, 6);
727 block_size
= s
->dump_info
.page_size
;
728 dh
->block_size
= cpu_to_dump32(s
, block_size
);
729 sub_hdr_size
= sizeof(struct KdumpSubHeader32
) + s
->note_size
;
730 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
731 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
732 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
733 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
734 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
735 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
736 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
737 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
739 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
740 status
|= DUMP_DH_COMPRESSED_ZLIB
;
743 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
744 status
|= DUMP_DH_COMPRESSED_LZO
;
748 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
749 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
752 dh
->status
= cpu_to_dump32(s
, status
);
754 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
755 error_setg(errp
, "dump: failed to write disk dump header");
759 /* write sub header */
760 size
= sizeof(KdumpSubHeader32
);
761 kh
= g_malloc0(size
);
763 /* 64bit max_mapnr_64 */
764 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
765 kh
->phys_base
= cpu_to_dump32(s
, s
->dump_info
.phys_base
);
766 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
768 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
769 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
770 kh
->note_size
= cpu_to_dump32(s
, s
->note_size
);
772 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
773 block_size
, kh
, size
) < 0) {
774 error_setg(errp
, "dump: failed to write kdump sub header");
779 s
->note_buf
= g_malloc0(s
->note_size
);
780 s
->note_buf_offset
= 0;
782 /* use s->note_buf to store notes temporarily */
783 write_elf32_notes(buf_write_note
, s
, &local_err
);
785 error_propagate(errp
, local_err
);
788 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
790 error_setg(errp
, "dump: failed to write notes");
794 /* get offset of dump_bitmap */
795 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
798 /* get offset of page */
799 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
808 /* write common header, sub header and elf note to vmcore */
809 static void create_header64(DumpState
*s
, Error
**errp
)
811 DiskDumpHeader64
*dh
= NULL
;
812 KdumpSubHeader64
*kh
= NULL
;
815 uint32_t sub_hdr_size
;
816 uint32_t bitmap_blocks
;
818 uint64_t offset_note
;
819 Error
*local_err
= NULL
;
821 /* write common header, the version of kdump-compressed format is 6th */
822 size
= sizeof(DiskDumpHeader64
);
823 dh
= g_malloc0(size
);
825 strncpy(dh
->signature
, KDUMP_SIGNATURE
, strlen(KDUMP_SIGNATURE
));
826 dh
->header_version
= cpu_to_dump32(s
, 6);
827 block_size
= s
->dump_info
.page_size
;
828 dh
->block_size
= cpu_to_dump32(s
, block_size
);
829 sub_hdr_size
= sizeof(struct KdumpSubHeader64
) + s
->note_size
;
830 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
831 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
832 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
833 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
834 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
835 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
836 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
837 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
839 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
840 status
|= DUMP_DH_COMPRESSED_ZLIB
;
843 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
844 status
|= DUMP_DH_COMPRESSED_LZO
;
848 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
849 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
852 dh
->status
= cpu_to_dump32(s
, status
);
854 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
855 error_setg(errp
, "dump: failed to write disk dump header");
859 /* write sub header */
860 size
= sizeof(KdumpSubHeader64
);
861 kh
= g_malloc0(size
);
863 /* 64bit max_mapnr_64 */
864 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
865 kh
->phys_base
= cpu_to_dump64(s
, s
->dump_info
.phys_base
);
866 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
868 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
869 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
870 kh
->note_size
= cpu_to_dump64(s
, s
->note_size
);
872 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
873 block_size
, kh
, size
) < 0) {
874 error_setg(errp
, "dump: failed to write kdump sub header");
879 s
->note_buf
= g_malloc0(s
->note_size
);
880 s
->note_buf_offset
= 0;
882 /* use s->note_buf to store notes temporarily */
883 write_elf64_notes(buf_write_note
, s
, &local_err
);
885 error_propagate(errp
, local_err
);
889 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
891 error_setg(errp
, "dump: failed to write notes");
895 /* get offset of dump_bitmap */
896 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
899 /* get offset of page */
900 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
909 static void write_dump_header(DumpState
*s
, Error
**errp
)
911 Error
*local_err
= NULL
;
913 if (s
->dump_info
.d_class
== ELFCLASS32
) {
914 create_header32(s
, &local_err
);
916 create_header64(s
, &local_err
);
919 error_propagate(errp
, local_err
);
923 static size_t dump_bitmap_get_bufsize(DumpState
*s
)
925 return s
->dump_info
.page_size
;
929 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
930 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
931 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
932 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
933 * vmcore, ie. synchronizing un-sync bit into vmcore.
935 static int set_dump_bitmap(uint64_t last_pfn
, uint64_t pfn
, bool value
,
936 uint8_t *buf
, DumpState
*s
)
938 off_t old_offset
, new_offset
;
939 off_t offset_bitmap1
, offset_bitmap2
;
941 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
942 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
944 /* should not set the previous place */
945 assert(last_pfn
<= pfn
);
948 * if the bit needed to be set is not cached in buf, flush the data in buf
950 * making new_offset be bigger than old_offset can also sync remained data
953 old_offset
= bitmap_bufsize
* (last_pfn
/ bits_per_buf
);
954 new_offset
= bitmap_bufsize
* (pfn
/ bits_per_buf
);
956 while (old_offset
< new_offset
) {
957 /* calculate the offset and write dump_bitmap */
958 offset_bitmap1
= s
->offset_dump_bitmap
+ old_offset
;
959 if (write_buffer(s
->fd
, offset_bitmap1
, buf
,
960 bitmap_bufsize
) < 0) {
964 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
965 offset_bitmap2
= s
->offset_dump_bitmap
+ s
->len_dump_bitmap
+
967 if (write_buffer(s
->fd
, offset_bitmap2
, buf
,
968 bitmap_bufsize
) < 0) {
972 memset(buf
, 0, bitmap_bufsize
);
973 old_offset
+= bitmap_bufsize
;
976 /* get the exact place of the bit in the buf, and set it */
977 byte
= (pfn
% bits_per_buf
) / CHAR_BIT
;
978 bit
= (pfn
% bits_per_buf
) % CHAR_BIT
;
980 buf
[byte
] |= 1u << bit
;
982 buf
[byte
] &= ~(1u << bit
);
988 static uint64_t dump_paddr_to_pfn(DumpState
*s
, uint64_t addr
)
990 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
992 return (addr
>> target_page_shift
) - ARCH_PFN_OFFSET
;
995 static uint64_t dump_pfn_to_paddr(DumpState
*s
, uint64_t pfn
)
997 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
999 return (pfn
+ ARCH_PFN_OFFSET
) << target_page_shift
;
1003 * exam every page and return the page frame number and the address of the page.
1004 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1005 * blocks, so block->target_start and block->target_end should be interal
1006 * multiples of the target page size.
1008 static bool get_next_page(GuestPhysBlock
**blockptr
, uint64_t *pfnptr
,
1009 uint8_t **bufptr
, DumpState
*s
)
1011 GuestPhysBlock
*block
= *blockptr
;
1012 hwaddr addr
, target_page_mask
= ~((hwaddr
)s
->dump_info
.page_size
- 1);
1015 /* block == NULL means the start of the iteration */
1017 block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1019 assert((block
->target_start
& ~target_page_mask
) == 0);
1020 assert((block
->target_end
& ~target_page_mask
) == 0);
1021 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1023 *bufptr
= block
->host_addr
;
1028 *pfnptr
= *pfnptr
+ 1;
1029 addr
= dump_pfn_to_paddr(s
, *pfnptr
);
1031 if ((addr
>= block
->target_start
) &&
1032 (addr
+ s
->dump_info
.page_size
<= block
->target_end
)) {
1033 buf
= block
->host_addr
+ (addr
- block
->target_start
);
1035 /* the next page is in the next block */
1036 block
= QTAILQ_NEXT(block
, next
);
1041 assert((block
->target_start
& ~target_page_mask
) == 0);
1042 assert((block
->target_end
& ~target_page_mask
) == 0);
1043 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1044 buf
= block
->host_addr
;
1054 static void write_dump_bitmap(DumpState
*s
, Error
**errp
)
1057 uint64_t last_pfn
, pfn
;
1058 void *dump_bitmap_buf
;
1059 size_t num_dumpable
;
1060 GuestPhysBlock
*block_iter
= NULL
;
1061 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1062 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1064 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1065 dump_bitmap_buf
= g_malloc0(bitmap_bufsize
);
1071 * exam memory page by page, and set the bit in dump_bitmap corresponded
1072 * to the existing page.
1074 while (get_next_page(&block_iter
, &pfn
, NULL
, s
)) {
1075 ret
= set_dump_bitmap(last_pfn
, pfn
, true, dump_bitmap_buf
, s
);
1077 error_setg(errp
, "dump: failed to set dump_bitmap");
1086 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1087 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1088 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1090 if (num_dumpable
> 0) {
1091 ret
= set_dump_bitmap(last_pfn
, last_pfn
+ bits_per_buf
, false,
1092 dump_bitmap_buf
, s
);
1094 error_setg(errp
, "dump: failed to sync dump_bitmap");
1099 /* number of dumpable pages that will be dumped later */
1100 s
->num_dumpable
= num_dumpable
;
1103 g_free(dump_bitmap_buf
);
1106 static void prepare_data_cache(DataCache
*data_cache
, DumpState
*s
,
1109 data_cache
->fd
= s
->fd
;
1110 data_cache
->data_size
= 0;
1111 data_cache
->buf_size
= 4 * dump_bitmap_get_bufsize(s
);
1112 data_cache
->buf
= g_malloc0(data_cache
->buf_size
);
1113 data_cache
->offset
= offset
;
1116 static int write_cache(DataCache
*dc
, const void *buf
, size_t size
,
1120 * dc->buf_size should not be less than size, otherwise dc will never be
1123 assert(size
<= dc
->buf_size
);
1126 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1127 * otherwise check if the space is enough for caching data in buf, if not,
1128 * write the data in dc->buf to dc->fd and reset dc->buf
1130 if ((!flag_sync
&& dc
->data_size
+ size
> dc
->buf_size
) ||
1131 (flag_sync
&& dc
->data_size
> 0)) {
1132 if (write_buffer(dc
->fd
, dc
->offset
, dc
->buf
, dc
->data_size
) < 0) {
1136 dc
->offset
+= dc
->data_size
;
1141 memcpy(dc
->buf
+ dc
->data_size
, buf
, size
);
1142 dc
->data_size
+= size
;
1148 static void free_data_cache(DataCache
*data_cache
)
1150 g_free(data_cache
->buf
);
1153 static size_t get_len_buf_out(size_t page_size
, uint32_t flag_compress
)
1155 switch (flag_compress
) {
1156 case DUMP_DH_COMPRESSED_ZLIB
:
1157 return compressBound(page_size
);
1159 case DUMP_DH_COMPRESSED_LZO
:
1161 * LZO will expand incompressible data by a little amount. Please check
1162 * the following URL to see the expansion calculation:
1163 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1165 return page_size
+ page_size
/ 16 + 64 + 3;
1167 #ifdef CONFIG_SNAPPY
1168 case DUMP_DH_COMPRESSED_SNAPPY
:
1169 return snappy_max_compressed_length(page_size
);
1176 * check if the page is all 0
1178 static inline bool is_zero_page(const uint8_t *buf
, size_t page_size
)
1180 return buffer_is_zero(buf
, page_size
);
1183 static void write_dump_pages(DumpState
*s
, Error
**errp
)
1186 DataCache page_desc
, page_data
;
1187 size_t len_buf_out
, size_out
;
1189 lzo_bytep wrkmem
= NULL
;
1191 uint8_t *buf_out
= NULL
;
1192 off_t offset_desc
, offset_data
;
1193 PageDescriptor pd
, pd_zero
;
1195 GuestPhysBlock
*block_iter
= NULL
;
1198 /* get offset of page_desc and page_data in dump file */
1199 offset_desc
= s
->offset_page
;
1200 offset_data
= offset_desc
+ sizeof(PageDescriptor
) * s
->num_dumpable
;
1202 prepare_data_cache(&page_desc
, s
, offset_desc
);
1203 prepare_data_cache(&page_data
, s
, offset_data
);
1205 /* prepare buffer to store compressed data */
1206 len_buf_out
= get_len_buf_out(s
->dump_info
.page_size
, s
->flag_compress
);
1207 assert(len_buf_out
!= 0);
1210 wrkmem
= g_malloc(LZO1X_1_MEM_COMPRESS
);
1213 buf_out
= g_malloc(len_buf_out
);
1216 * init zero page's page_desc and page_data, because every zero page
1217 * uses the same page_data
1219 pd_zero
.size
= cpu_to_dump32(s
, s
->dump_info
.page_size
);
1220 pd_zero
.flags
= cpu_to_dump32(s
, 0);
1221 pd_zero
.offset
= cpu_to_dump64(s
, offset_data
);
1222 pd_zero
.page_flags
= cpu_to_dump64(s
, 0);
1223 buf
= g_malloc0(s
->dump_info
.page_size
);
1224 ret
= write_cache(&page_data
, buf
, s
->dump_info
.page_size
, false);
1227 error_setg(errp
, "dump: failed to write page data (zero page)");
1231 offset_data
+= s
->dump_info
.page_size
;
1234 * dump memory to vmcore page by page. zero page will all be resided in the
1235 * first page of page section
1237 while (get_next_page(&block_iter
, &pfn_iter
, &buf
, s
)) {
1238 /* check zero page */
1239 if (is_zero_page(buf
, s
->dump_info
.page_size
)) {
1240 ret
= write_cache(&page_desc
, &pd_zero
, sizeof(PageDescriptor
),
1243 error_setg(errp
, "dump: failed to write page desc");
1248 * not zero page, then:
1249 * 1. compress the page
1250 * 2. write the compressed page into the cache of page_data
1251 * 3. get page desc of the compressed page and write it into the
1252 * cache of page_desc
1254 * only one compression format will be used here, for
1255 * s->flag_compress is set. But when compression fails to work,
1256 * we fall back to save in plaintext.
1258 size_out
= len_buf_out
;
1259 if ((s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) &&
1260 (compress2(buf_out
, (uLongf
*)&size_out
, buf
,
1261 s
->dump_info
.page_size
, Z_BEST_SPEED
) == Z_OK
) &&
1262 (size_out
< s
->dump_info
.page_size
)) {
1263 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_ZLIB
);
1264 pd
.size
= cpu_to_dump32(s
, size_out
);
1266 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1268 error_setg(errp
, "dump: failed to write page data");
1272 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) &&
1273 (lzo1x_1_compress(buf
, s
->dump_info
.page_size
, buf_out
,
1274 (lzo_uint
*)&size_out
, wrkmem
) == LZO_E_OK
) &&
1275 (size_out
< s
->dump_info
.page_size
)) {
1276 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_LZO
);
1277 pd
.size
= cpu_to_dump32(s
, size_out
);
1279 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1281 error_setg(errp
, "dump: failed to write page data");
1285 #ifdef CONFIG_SNAPPY
1286 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) &&
1287 (snappy_compress((char *)buf
, s
->dump_info
.page_size
,
1288 (char *)buf_out
, &size_out
) == SNAPPY_OK
) &&
1289 (size_out
< s
->dump_info
.page_size
)) {
1290 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_SNAPPY
);
1291 pd
.size
= cpu_to_dump32(s
, size_out
);
1293 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1295 error_setg(errp
, "dump: failed to write page data");
1301 * fall back to save in plaintext, size_out should be
1302 * assigned the target's page size
1304 pd
.flags
= cpu_to_dump32(s
, 0);
1305 size_out
= s
->dump_info
.page_size
;
1306 pd
.size
= cpu_to_dump32(s
, size_out
);
1308 ret
= write_cache(&page_data
, buf
,
1309 s
->dump_info
.page_size
, false);
1311 error_setg(errp
, "dump: failed to write page data");
1316 /* get and write page desc here */
1317 pd
.page_flags
= cpu_to_dump64(s
, 0);
1318 pd
.offset
= cpu_to_dump64(s
, offset_data
);
1319 offset_data
+= size_out
;
1321 ret
= write_cache(&page_desc
, &pd
, sizeof(PageDescriptor
), false);
1323 error_setg(errp
, "dump: failed to write page desc");
1329 ret
= write_cache(&page_desc
, NULL
, 0, true);
1331 error_setg(errp
, "dump: failed to sync cache for page_desc");
1334 ret
= write_cache(&page_data
, NULL
, 0, true);
1336 error_setg(errp
, "dump: failed to sync cache for page_data");
1341 free_data_cache(&page_desc
);
1342 free_data_cache(&page_data
);
1351 static void create_kdump_vmcore(DumpState
*s
, Error
**errp
)
1354 Error
*local_err
= NULL
;
1357 * the kdump-compressed format is:
1359 * +------------------------------------------+ 0x0
1360 * | main header (struct disk_dump_header) |
1361 * |------------------------------------------+ block 1
1362 * | sub header (struct kdump_sub_header) |
1363 * |------------------------------------------+ block 2
1364 * | 1st-dump_bitmap |
1365 * |------------------------------------------+ block 2 + X blocks
1366 * | 2nd-dump_bitmap | (aligned by block)
1367 * |------------------------------------------+ block 2 + 2 * X blocks
1368 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1369 * | page desc for pfn 1 (struct page_desc) |
1371 * |------------------------------------------| (not aligned by block)
1372 * | page data (pfn 0) |
1373 * | page data (pfn 1) |
1375 * +------------------------------------------+
1378 ret
= write_start_flat_header(s
->fd
);
1380 error_setg(errp
, "dump: failed to write start flat header");
1384 write_dump_header(s
, &local_err
);
1386 error_propagate(errp
, local_err
);
1390 write_dump_bitmap(s
, &local_err
);
1392 error_propagate(errp
, local_err
);
1396 write_dump_pages(s
, &local_err
);
1398 error_propagate(errp
, local_err
);
1402 ret
= write_end_flat_header(s
->fd
);
1404 error_setg(errp
, "dump: failed to write end flat header");
1409 static ram_addr_t
get_start_block(DumpState
*s
)
1411 GuestPhysBlock
*block
;
1413 if (!s
->has_filter
) {
1414 s
->next_block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1418 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1419 if (block
->target_start
>= s
->begin
+ s
->length
||
1420 block
->target_end
<= s
->begin
) {
1421 /* This block is out of the range */
1425 s
->next_block
= block
;
1426 if (s
->begin
> block
->target_start
) {
1427 s
->start
= s
->begin
- block
->target_start
;
1437 static void get_max_mapnr(DumpState
*s
)
1439 GuestPhysBlock
*last_block
;
1441 last_block
= QTAILQ_LAST(&s
->guest_phys_blocks
.head
, GuestPhysBlockHead
);
1442 s
->max_mapnr
= dump_paddr_to_pfn(s
, last_block
->target_end
);
1445 static DumpState dump_state_global
= { .status
= DUMP_STATUS_NONE
};
1447 static void dump_state_prepare(DumpState
*s
)
1449 /* zero the struct, setting status to active */
1450 *s
= (DumpState
) { .status
= DUMP_STATUS_ACTIVE
};
1453 bool dump_in_progress(void)
1455 DumpState
*state
= &dump_state_global
;
1456 return (state
->status
== DUMP_STATUS_ACTIVE
);
1459 static void dump_init(DumpState
*s
, int fd
, bool has_format
,
1460 DumpGuestMemoryFormat format
, bool paging
, bool has_filter
,
1461 int64_t begin
, int64_t length
, Error
**errp
)
1468 s
->has_format
= has_format
;
1471 /* kdump-compressed is conflict with paging and filter */
1472 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1473 assert(!paging
&& !has_filter
);
1476 if (runstate_is_running()) {
1477 vm_stop(RUN_STATE_SAVE_VM
);
1483 /* If we use KVM, we should synchronize the registers before we get dump
1484 * info or physmap info.
1486 cpu_synchronize_all_states();
1493 s
->has_filter
= has_filter
;
1497 memory_mapping_list_init(&s
->list
);
1499 guest_phys_blocks_init(&s
->guest_phys_blocks
);
1500 guest_phys_blocks_append(&s
->guest_phys_blocks
);
1502 s
->start
= get_start_block(s
);
1503 if (s
->start
== -1) {
1504 error_setg(errp
, QERR_INVALID_PARAMETER
, "begin");
1508 /* get dump info: endian, class and architecture.
1509 * If the target architecture is not supported, cpu_get_dump_info() will
1512 ret
= cpu_get_dump_info(&s
->dump_info
, &s
->guest_phys_blocks
);
1514 error_setg(errp
, QERR_UNSUPPORTED
);
1518 if (!s
->dump_info
.page_size
) {
1519 s
->dump_info
.page_size
= TARGET_PAGE_SIZE
;
1522 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
1523 s
->dump_info
.d_machine
, nr_cpus
);
1524 if (s
->note_size
< 0) {
1525 error_setg(errp
, QERR_UNSUPPORTED
);
1529 /* get memory mapping */
1531 qemu_get_guest_memory_mapping(&s
->list
, &s
->guest_phys_blocks
, &err
);
1533 error_propagate(errp
, err
);
1537 qemu_get_guest_simple_memory_mapping(&s
->list
, &s
->guest_phys_blocks
);
1540 s
->nr_cpus
= nr_cpus
;
1545 tmp
= DIV_ROUND_UP(DIV_ROUND_UP(s
->max_mapnr
, CHAR_BIT
),
1546 s
->dump_info
.page_size
);
1547 s
->len_dump_bitmap
= tmp
* s
->dump_info
.page_size
;
1549 /* init for kdump-compressed format */
1550 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1552 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
:
1553 s
->flag_compress
= DUMP_DH_COMPRESSED_ZLIB
;
1556 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
:
1558 if (lzo_init() != LZO_E_OK
) {
1559 error_setg(errp
, "failed to initialize the LZO library");
1563 s
->flag_compress
= DUMP_DH_COMPRESSED_LZO
;
1566 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
:
1567 s
->flag_compress
= DUMP_DH_COMPRESSED_SNAPPY
;
1571 s
->flag_compress
= 0;
1577 if (s
->has_filter
) {
1578 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
1582 * calculate phdr_num
1584 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1586 s
->phdr_num
= 1; /* PT_NOTE */
1587 if (s
->list
.num
< UINT16_MAX
- 2) {
1588 s
->phdr_num
+= s
->list
.num
;
1589 s
->have_section
= false;
1591 s
->have_section
= true;
1592 s
->phdr_num
= PN_XNUM
;
1593 s
->sh_info
= 1; /* PT_NOTE */
1595 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1596 if (s
->list
.num
<= UINT32_MAX
- 1) {
1597 s
->sh_info
+= s
->list
.num
;
1599 s
->sh_info
= UINT32_MAX
;
1603 if (s
->dump_info
.d_class
== ELFCLASS64
) {
1604 if (s
->have_section
) {
1605 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1606 sizeof(Elf64_Phdr
) * s
->sh_info
+
1607 sizeof(Elf64_Shdr
) + s
->note_size
;
1609 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1610 sizeof(Elf64_Phdr
) * s
->phdr_num
+ s
->note_size
;
1613 if (s
->have_section
) {
1614 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1615 sizeof(Elf32_Phdr
) * s
->sh_info
+
1616 sizeof(Elf32_Shdr
) + s
->note_size
;
1618 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1619 sizeof(Elf32_Phdr
) * s
->phdr_num
+ s
->note_size
;
1629 /* this operation might be time consuming. */
1630 static void dump_process(DumpState
*s
, Error
**errp
)
1632 Error
*local_err
= NULL
;
1634 if (s
->has_format
&& s
->format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1635 create_kdump_vmcore(s
, &local_err
);
1637 create_vmcore(s
, &local_err
);
1640 s
->status
= (local_err
? DUMP_STATUS_FAILED
: DUMP_STATUS_COMPLETED
);
1641 error_propagate(errp
, local_err
);
1646 void qmp_dump_guest_memory(bool paging
, const char *file
,
1647 bool has_detach
, bool detach
,
1648 bool has_begin
, int64_t begin
, bool has_length
,
1649 int64_t length
, bool has_format
,
1650 DumpGuestMemoryFormat format
, Error
**errp
)
1655 Error
*local_err
= NULL
;
1657 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1658 error_setg(errp
, "Dump not allowed during incoming migration.");
1662 /* if there is a dump in background, we should wait until the dump
1664 if (dump_in_progress()) {
1665 error_setg(errp
, "There is a dump in process, please wait.");
1670 * kdump-compressed format need the whole memory dumped, so paging or
1671 * filter is not supported here.
1673 if ((has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) &&
1674 (paging
|| has_begin
|| has_length
)) {
1675 error_setg(errp
, "kdump-compressed format doesn't support paging or "
1679 if (has_begin
&& !has_length
) {
1680 error_setg(errp
, QERR_MISSING_PARAMETER
, "length");
1683 if (!has_begin
&& has_length
) {
1684 error_setg(errp
, QERR_MISSING_PARAMETER
, "begin");
1688 /* check whether lzo/snappy is supported */
1690 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
) {
1691 error_setg(errp
, "kdump-lzo is not available now");
1696 #ifndef CONFIG_SNAPPY
1697 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
) {
1698 error_setg(errp
, "kdump-snappy is not available now");
1704 if (strstart(file
, "fd:", &p
)) {
1705 fd
= monitor_get_fd(cur_mon
, p
, errp
);
1712 if (strstart(file
, "file:", &p
)) {
1713 fd
= qemu_open(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
1715 error_setg_file_open(errp
, errno
, p
);
1721 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
1725 s
= &dump_state_global
;
1726 dump_state_prepare(s
);
1728 dump_init(s
, fd
, has_format
, format
, paging
, has_begin
,
1729 begin
, length
, &local_err
);
1731 error_propagate(errp
, local_err
);
1732 s
->status
= DUMP_STATUS_FAILED
;
1736 dump_process(s
, errp
);
1739 DumpGuestMemoryCapability
*qmp_query_dump_guest_memory_capability(Error
**errp
)
1741 DumpGuestMemoryFormatList
*item
;
1742 DumpGuestMemoryCapability
*cap
=
1743 g_malloc0(sizeof(DumpGuestMemoryCapability
));
1745 /* elf is always available */
1746 item
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1747 cap
->formats
= item
;
1748 item
->value
= DUMP_GUEST_MEMORY_FORMAT_ELF
;
1750 /* kdump-zlib is always available */
1751 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1753 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
;
1755 /* add new item if kdump-lzo is available */
1757 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1759 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
;
1762 /* add new item if kdump-snappy is available */
1763 #ifdef CONFIG_SNAPPY
1764 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1766 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
;