4 * Copyright Fujitsu, Corp. 2011, 2012
7 * Wen Congyang <wency@cn.fujitsu.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qemu/cutils.h"
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"
28 #include "qapi-event.h"
32 #include <lzo/lzo1x.h>
37 #ifndef ELF_MACHINE_UNAME
38 #define ELF_MACHINE_UNAME "Unknown"
41 uint16_t cpu_to_dump16(DumpState
*s
, uint16_t val
)
43 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
44 val
= cpu_to_le16(val
);
46 val
= cpu_to_be16(val
);
52 uint32_t cpu_to_dump32(DumpState
*s
, uint32_t val
)
54 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
55 val
= cpu_to_le32(val
);
57 val
= cpu_to_be32(val
);
63 uint64_t cpu_to_dump64(DumpState
*s
, uint64_t val
)
65 if (s
->dump_info
.d_endian
== ELFDATA2LSB
) {
66 val
= cpu_to_le64(val
);
68 val
= cpu_to_be64(val
);
74 static int dump_cleanup(DumpState
*s
)
76 guest_phys_blocks_free(&s
->guest_phys_blocks
);
77 memory_mapping_list_free(&s
->list
);
81 qemu_mutex_lock_iothread();
85 qemu_mutex_unlock_iothread();
92 static int fd_write_vmcore(const void *buf
, size_t size
, void *opaque
)
94 DumpState
*s
= opaque
;
97 written_size
= qemu_write_full(s
->fd
, buf
, size
);
98 if (written_size
!= size
) {
105 static void write_elf64_header(DumpState
*s
, Error
**errp
)
107 Elf64_Ehdr elf_header
;
110 memset(&elf_header
, 0, sizeof(Elf64_Ehdr
));
111 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
112 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS64
;
113 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
114 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
115 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
116 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
117 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
118 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
119 elf_header
.e_phoff
= cpu_to_dump64(s
, sizeof(Elf64_Ehdr
));
120 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf64_Phdr
));
121 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
122 if (s
->have_section
) {
123 uint64_t shoff
= sizeof(Elf64_Ehdr
) + sizeof(Elf64_Phdr
) * s
->sh_info
;
125 elf_header
.e_shoff
= cpu_to_dump64(s
, shoff
);
126 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf64_Shdr
));
127 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
130 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
132 error_setg(errp
, "dump: failed to write elf header");
136 static void write_elf32_header(DumpState
*s
, Error
**errp
)
138 Elf32_Ehdr elf_header
;
141 memset(&elf_header
, 0, sizeof(Elf32_Ehdr
));
142 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
143 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS32
;
144 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
145 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
146 elf_header
.e_type
= cpu_to_dump16(s
, ET_CORE
);
147 elf_header
.e_machine
= cpu_to_dump16(s
, s
->dump_info
.d_machine
);
148 elf_header
.e_version
= cpu_to_dump32(s
, EV_CURRENT
);
149 elf_header
.e_ehsize
= cpu_to_dump16(s
, sizeof(elf_header
));
150 elf_header
.e_phoff
= cpu_to_dump32(s
, sizeof(Elf32_Ehdr
));
151 elf_header
.e_phentsize
= cpu_to_dump16(s
, sizeof(Elf32_Phdr
));
152 elf_header
.e_phnum
= cpu_to_dump16(s
, s
->phdr_num
);
153 if (s
->have_section
) {
154 uint32_t shoff
= sizeof(Elf32_Ehdr
) + sizeof(Elf32_Phdr
) * s
->sh_info
;
156 elf_header
.e_shoff
= cpu_to_dump32(s
, shoff
);
157 elf_header
.e_shentsize
= cpu_to_dump16(s
, sizeof(Elf32_Shdr
));
158 elf_header
.e_shnum
= cpu_to_dump16(s
, 1);
161 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
163 error_setg(errp
, "dump: failed to write elf header");
167 static void write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
168 int phdr_index
, hwaddr offset
,
169 hwaddr filesz
, Error
**errp
)
174 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
175 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
176 phdr
.p_offset
= cpu_to_dump64(s
, offset
);
177 phdr
.p_paddr
= cpu_to_dump64(s
, memory_mapping
->phys_addr
);
178 phdr
.p_filesz
= cpu_to_dump64(s
, filesz
);
179 phdr
.p_memsz
= cpu_to_dump64(s
, memory_mapping
->length
);
180 phdr
.p_vaddr
= cpu_to_dump64(s
, memory_mapping
->virt_addr
);
182 assert(memory_mapping
->length
>= filesz
);
184 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
186 error_setg(errp
, "dump: failed to write program header table");
190 static void write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
191 int phdr_index
, hwaddr offset
,
192 hwaddr filesz
, Error
**errp
)
197 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
198 phdr
.p_type
= cpu_to_dump32(s
, PT_LOAD
);
199 phdr
.p_offset
= cpu_to_dump32(s
, offset
);
200 phdr
.p_paddr
= cpu_to_dump32(s
, memory_mapping
->phys_addr
);
201 phdr
.p_filesz
= cpu_to_dump32(s
, filesz
);
202 phdr
.p_memsz
= cpu_to_dump32(s
, memory_mapping
->length
);
203 phdr
.p_vaddr
= cpu_to_dump32(s
, memory_mapping
->virt_addr
);
205 assert(memory_mapping
->length
>= filesz
);
207 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
209 error_setg(errp
, "dump: failed to write program header table");
213 static void write_elf64_note(DumpState
*s
, Error
**errp
)
216 hwaddr begin
= s
->memory_offset
- s
->note_size
;
219 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
220 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
221 phdr
.p_offset
= cpu_to_dump64(s
, begin
);
223 phdr
.p_filesz
= cpu_to_dump64(s
, s
->note_size
);
224 phdr
.p_memsz
= cpu_to_dump64(s
, s
->note_size
);
227 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
229 error_setg(errp
, "dump: failed to write program header table");
233 static inline int cpu_index(CPUState
*cpu
)
235 return cpu
->cpu_index
+ 1;
238 static void write_elf64_notes(WriteCoreDumpFunction f
, DumpState
*s
,
247 ret
= cpu_write_elf64_note(f
, cpu
, id
, s
);
249 error_setg(errp
, "dump: failed to write elf notes");
255 ret
= cpu_write_elf64_qemunote(f
, cpu
, s
);
257 error_setg(errp
, "dump: failed to write CPU status");
263 static void write_elf32_note(DumpState
*s
, Error
**errp
)
265 hwaddr begin
= s
->memory_offset
- s
->note_size
;
269 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
270 phdr
.p_type
= cpu_to_dump32(s
, PT_NOTE
);
271 phdr
.p_offset
= cpu_to_dump32(s
, begin
);
273 phdr
.p_filesz
= cpu_to_dump32(s
, s
->note_size
);
274 phdr
.p_memsz
= cpu_to_dump32(s
, s
->note_size
);
277 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
279 error_setg(errp
, "dump: failed to write program header table");
283 static void write_elf32_notes(WriteCoreDumpFunction f
, DumpState
*s
,
292 ret
= cpu_write_elf32_note(f
, cpu
, id
, s
);
294 error_setg(errp
, "dump: failed to write elf notes");
300 ret
= cpu_write_elf32_qemunote(f
, cpu
, s
);
302 error_setg(errp
, "dump: failed to write CPU status");
308 static void write_elf_section(DumpState
*s
, int type
, Error
**errp
)
317 shdr_size
= sizeof(Elf32_Shdr
);
318 memset(&shdr32
, 0, shdr_size
);
319 shdr32
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
322 shdr_size
= sizeof(Elf64_Shdr
);
323 memset(&shdr64
, 0, shdr_size
);
324 shdr64
.sh_info
= cpu_to_dump32(s
, s
->sh_info
);
328 ret
= fd_write_vmcore(&shdr
, shdr_size
, s
);
330 error_setg(errp
, "dump: failed to write section header table");
334 static void write_data(DumpState
*s
, void *buf
, int length
, Error
**errp
)
338 ret
= fd_write_vmcore(buf
, length
, s
);
340 error_setg(errp
, "dump: failed to save memory");
342 s
->written_size
+= length
;
346 /* write the memory to vmcore. 1 page per I/O. */
347 static void write_memory(DumpState
*s
, GuestPhysBlock
*block
, ram_addr_t start
,
348 int64_t size
, Error
**errp
)
351 Error
*local_err
= NULL
;
353 for (i
= 0; i
< size
/ s
->dump_info
.page_size
; i
++) {
354 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
355 s
->dump_info
.page_size
, &local_err
);
357 error_propagate(errp
, local_err
);
362 if ((size
% s
->dump_info
.page_size
) != 0) {
363 write_data(s
, block
->host_addr
+ start
+ i
* s
->dump_info
.page_size
,
364 size
% s
->dump_info
.page_size
, &local_err
);
366 error_propagate(errp
, local_err
);
372 /* get the memory's offset and size in the vmcore */
373 static void get_offset_range(hwaddr phys_addr
,
374 ram_addr_t mapping_length
,
379 GuestPhysBlock
*block
;
380 hwaddr offset
= s
->memory_offset
;
381 int64_t size_in_block
, start
;
383 /* When the memory is not stored into vmcore, offset will be -1 */
388 if (phys_addr
< s
->begin
|| phys_addr
>= s
->begin
+ s
->length
) {
393 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
395 if (block
->target_start
>= s
->begin
+ s
->length
||
396 block
->target_end
<= s
->begin
) {
397 /* This block is out of the range */
401 if (s
->begin
<= block
->target_start
) {
402 start
= block
->target_start
;
407 size_in_block
= block
->target_end
- start
;
408 if (s
->begin
+ s
->length
< block
->target_end
) {
409 size_in_block
-= block
->target_end
- (s
->begin
+ s
->length
);
412 start
= block
->target_start
;
413 size_in_block
= block
->target_end
- block
->target_start
;
416 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
417 *p_offset
= phys_addr
- start
+ offset
;
419 /* The offset range mapped from the vmcore file must not spill over
420 * the GuestPhysBlock, clamp it. The rest of the mapping will be
421 * zero-filled in memory at load time; see
422 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
424 *p_filesz
= phys_addr
+ mapping_length
<= start
+ size_in_block
?
426 size_in_block
- (phys_addr
- start
);
430 offset
+= size_in_block
;
434 static void write_elf_loads(DumpState
*s
, Error
**errp
)
436 hwaddr offset
, filesz
;
437 MemoryMapping
*memory_mapping
;
438 uint32_t phdr_index
= 1;
440 Error
*local_err
= NULL
;
442 if (s
->have_section
) {
443 max_index
= s
->sh_info
;
445 max_index
= s
->phdr_num
;
448 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
449 get_offset_range(memory_mapping
->phys_addr
,
450 memory_mapping
->length
,
451 s
, &offset
, &filesz
);
452 if (s
->dump_info
.d_class
== ELFCLASS64
) {
453 write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
,
456 write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
,
461 error_propagate(errp
, local_err
);
465 if (phdr_index
>= max_index
) {
471 /* write elf header, PT_NOTE and elf note to vmcore. */
472 static void dump_begin(DumpState
*s
, Error
**errp
)
474 Error
*local_err
= NULL
;
477 * the vmcore's format is:
496 * we only know where the memory is saved after we write elf note into
500 /* write elf header to vmcore */
501 if (s
->dump_info
.d_class
== ELFCLASS64
) {
502 write_elf64_header(s
, &local_err
);
504 write_elf32_header(s
, &local_err
);
507 error_propagate(errp
, local_err
);
511 if (s
->dump_info
.d_class
== ELFCLASS64
) {
512 /* write PT_NOTE to vmcore */
513 write_elf64_note(s
, &local_err
);
515 error_propagate(errp
, local_err
);
519 /* write all PT_LOAD to vmcore */
520 write_elf_loads(s
, &local_err
);
522 error_propagate(errp
, local_err
);
526 /* write section to vmcore */
527 if (s
->have_section
) {
528 write_elf_section(s
, 1, &local_err
);
530 error_propagate(errp
, local_err
);
535 /* write notes to vmcore */
536 write_elf64_notes(fd_write_vmcore
, s
, &local_err
);
538 error_propagate(errp
, local_err
);
542 /* write PT_NOTE to vmcore */
543 write_elf32_note(s
, &local_err
);
545 error_propagate(errp
, local_err
);
549 /* write all PT_LOAD to vmcore */
550 write_elf_loads(s
, &local_err
);
552 error_propagate(errp
, local_err
);
556 /* write section to vmcore */
557 if (s
->have_section
) {
558 write_elf_section(s
, 0, &local_err
);
560 error_propagate(errp
, local_err
);
565 /* write notes to vmcore */
566 write_elf32_notes(fd_write_vmcore
, s
, &local_err
);
568 error_propagate(errp
, local_err
);
574 static int get_next_block(DumpState
*s
, GuestPhysBlock
*block
)
577 block
= QTAILQ_NEXT(block
, next
);
584 s
->next_block
= block
;
586 if (block
->target_start
>= s
->begin
+ s
->length
||
587 block
->target_end
<= s
->begin
) {
588 /* This block is out of the range */
592 if (s
->begin
> block
->target_start
) {
593 s
->start
= s
->begin
- block
->target_start
;
601 /* write all memory to vmcore */
602 static void dump_iterate(DumpState
*s
, Error
**errp
)
604 GuestPhysBlock
*block
;
606 Error
*local_err
= NULL
;
609 block
= s
->next_block
;
611 size
= block
->target_end
- block
->target_start
;
614 if (s
->begin
+ s
->length
< block
->target_end
) {
615 size
-= block
->target_end
- (s
->begin
+ s
->length
);
618 write_memory(s
, block
, s
->start
, size
, &local_err
);
620 error_propagate(errp
, local_err
);
624 } while (!get_next_block(s
, block
));
627 static void create_vmcore(DumpState
*s
, Error
**errp
)
629 Error
*local_err
= NULL
;
631 dump_begin(s
, &local_err
);
633 error_propagate(errp
, local_err
);
637 dump_iterate(s
, errp
);
640 static int write_start_flat_header(int fd
)
642 MakedumpfileHeader
*mh
;
645 QEMU_BUILD_BUG_ON(sizeof *mh
> MAX_SIZE_MDF_HEADER
);
646 mh
= g_malloc0(MAX_SIZE_MDF_HEADER
);
648 memcpy(mh
->signature
, MAKEDUMPFILE_SIGNATURE
,
649 MIN(sizeof mh
->signature
, sizeof MAKEDUMPFILE_SIGNATURE
));
651 mh
->type
= cpu_to_be64(TYPE_FLAT_HEADER
);
652 mh
->version
= cpu_to_be64(VERSION_FLAT_HEADER
);
655 written_size
= qemu_write_full(fd
, mh
, MAX_SIZE_MDF_HEADER
);
656 if (written_size
!= MAX_SIZE_MDF_HEADER
) {
664 static int write_end_flat_header(int fd
)
666 MakedumpfileDataHeader mdh
;
668 mdh
.offset
= END_FLAG_FLAT_HEADER
;
669 mdh
.buf_size
= END_FLAG_FLAT_HEADER
;
672 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
673 if (written_size
!= sizeof(mdh
)) {
680 static int write_buffer(int fd
, off_t offset
, const void *buf
, size_t size
)
683 MakedumpfileDataHeader mdh
;
685 mdh
.offset
= cpu_to_be64(offset
);
686 mdh
.buf_size
= cpu_to_be64(size
);
688 written_size
= qemu_write_full(fd
, &mdh
, sizeof(mdh
));
689 if (written_size
!= sizeof(mdh
)) {
693 written_size
= qemu_write_full(fd
, buf
, size
);
694 if (written_size
!= size
) {
701 static int buf_write_note(const void *buf
, size_t size
, void *opaque
)
703 DumpState
*s
= opaque
;
705 /* note_buf is not enough */
706 if (s
->note_buf_offset
+ size
> s
->note_size
) {
710 memcpy(s
->note_buf
+ s
->note_buf_offset
, buf
, size
);
712 s
->note_buf_offset
+= size
;
717 /* write common header, sub header and elf note to vmcore */
718 static void create_header32(DumpState
*s
, Error
**errp
)
720 DiskDumpHeader32
*dh
= NULL
;
721 KdumpSubHeader32
*kh
= NULL
;
724 uint32_t sub_hdr_size
;
725 uint32_t bitmap_blocks
;
727 uint64_t offset_note
;
728 Error
*local_err
= NULL
;
730 /* write common header, the version of kdump-compressed format is 6th */
731 size
= sizeof(DiskDumpHeader32
);
732 dh
= g_malloc0(size
);
734 strncpy(dh
->signature
, KDUMP_SIGNATURE
, strlen(KDUMP_SIGNATURE
));
735 dh
->header_version
= cpu_to_dump32(s
, 6);
736 block_size
= s
->dump_info
.page_size
;
737 dh
->block_size
= cpu_to_dump32(s
, block_size
);
738 sub_hdr_size
= sizeof(struct KdumpSubHeader32
) + s
->note_size
;
739 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
740 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
741 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
742 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
743 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
744 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
745 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
746 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
748 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
749 status
|= DUMP_DH_COMPRESSED_ZLIB
;
752 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
753 status
|= DUMP_DH_COMPRESSED_LZO
;
757 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
758 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
761 dh
->status
= cpu_to_dump32(s
, status
);
763 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
764 error_setg(errp
, "dump: failed to write disk dump header");
768 /* write sub header */
769 size
= sizeof(KdumpSubHeader32
);
770 kh
= g_malloc0(size
);
772 /* 64bit max_mapnr_64 */
773 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
774 kh
->phys_base
= cpu_to_dump32(s
, s
->dump_info
.phys_base
);
775 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
777 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
778 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
779 kh
->note_size
= cpu_to_dump32(s
, s
->note_size
);
781 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
782 block_size
, kh
, size
) < 0) {
783 error_setg(errp
, "dump: failed to write kdump sub header");
788 s
->note_buf
= g_malloc0(s
->note_size
);
789 s
->note_buf_offset
= 0;
791 /* use s->note_buf to store notes temporarily */
792 write_elf32_notes(buf_write_note
, s
, &local_err
);
794 error_propagate(errp
, local_err
);
797 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
799 error_setg(errp
, "dump: failed to write notes");
803 /* get offset of dump_bitmap */
804 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
807 /* get offset of page */
808 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
817 /* write common header, sub header and elf note to vmcore */
818 static void create_header64(DumpState
*s
, Error
**errp
)
820 DiskDumpHeader64
*dh
= NULL
;
821 KdumpSubHeader64
*kh
= NULL
;
824 uint32_t sub_hdr_size
;
825 uint32_t bitmap_blocks
;
827 uint64_t offset_note
;
828 Error
*local_err
= NULL
;
830 /* write common header, the version of kdump-compressed format is 6th */
831 size
= sizeof(DiskDumpHeader64
);
832 dh
= g_malloc0(size
);
834 strncpy(dh
->signature
, KDUMP_SIGNATURE
, strlen(KDUMP_SIGNATURE
));
835 dh
->header_version
= cpu_to_dump32(s
, 6);
836 block_size
= s
->dump_info
.page_size
;
837 dh
->block_size
= cpu_to_dump32(s
, block_size
);
838 sub_hdr_size
= sizeof(struct KdumpSubHeader64
) + s
->note_size
;
839 sub_hdr_size
= DIV_ROUND_UP(sub_hdr_size
, block_size
);
840 dh
->sub_hdr_size
= cpu_to_dump32(s
, sub_hdr_size
);
841 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
842 dh
->max_mapnr
= cpu_to_dump32(s
, MIN(s
->max_mapnr
, UINT_MAX
));
843 dh
->nr_cpus
= cpu_to_dump32(s
, s
->nr_cpus
);
844 bitmap_blocks
= DIV_ROUND_UP(s
->len_dump_bitmap
, block_size
) * 2;
845 dh
->bitmap_blocks
= cpu_to_dump32(s
, bitmap_blocks
);
846 strncpy(dh
->utsname
.machine
, ELF_MACHINE_UNAME
, sizeof(dh
->utsname
.machine
));
848 if (s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) {
849 status
|= DUMP_DH_COMPRESSED_ZLIB
;
852 if (s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) {
853 status
|= DUMP_DH_COMPRESSED_LZO
;
857 if (s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) {
858 status
|= DUMP_DH_COMPRESSED_SNAPPY
;
861 dh
->status
= cpu_to_dump32(s
, status
);
863 if (write_buffer(s
->fd
, 0, dh
, size
) < 0) {
864 error_setg(errp
, "dump: failed to write disk dump header");
868 /* write sub header */
869 size
= sizeof(KdumpSubHeader64
);
870 kh
= g_malloc0(size
);
872 /* 64bit max_mapnr_64 */
873 kh
->max_mapnr_64
= cpu_to_dump64(s
, s
->max_mapnr
);
874 kh
->phys_base
= cpu_to_dump64(s
, s
->dump_info
.phys_base
);
875 kh
->dump_level
= cpu_to_dump32(s
, DUMP_LEVEL
);
877 offset_note
= DISKDUMP_HEADER_BLOCKS
* block_size
+ size
;
878 kh
->offset_note
= cpu_to_dump64(s
, offset_note
);
879 kh
->note_size
= cpu_to_dump64(s
, s
->note_size
);
881 if (write_buffer(s
->fd
, DISKDUMP_HEADER_BLOCKS
*
882 block_size
, kh
, size
) < 0) {
883 error_setg(errp
, "dump: failed to write kdump sub header");
888 s
->note_buf
= g_malloc0(s
->note_size
);
889 s
->note_buf_offset
= 0;
891 /* use s->note_buf to store notes temporarily */
892 write_elf64_notes(buf_write_note
, s
, &local_err
);
894 error_propagate(errp
, local_err
);
898 if (write_buffer(s
->fd
, offset_note
, s
->note_buf
,
900 error_setg(errp
, "dump: failed to write notes");
904 /* get offset of dump_bitmap */
905 s
->offset_dump_bitmap
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
) *
908 /* get offset of page */
909 s
->offset_page
= (DISKDUMP_HEADER_BLOCKS
+ sub_hdr_size
+ bitmap_blocks
) *
918 static void write_dump_header(DumpState
*s
, Error
**errp
)
920 Error
*local_err
= NULL
;
922 if (s
->dump_info
.d_class
== ELFCLASS32
) {
923 create_header32(s
, &local_err
);
925 create_header64(s
, &local_err
);
927 error_propagate(errp
, local_err
);
930 static size_t dump_bitmap_get_bufsize(DumpState
*s
)
932 return s
->dump_info
.page_size
;
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
;
948 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
949 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
951 /* should not set the previous place */
952 assert(last_pfn
<= pfn
);
955 * if the bit needed to be set is not cached in buf, flush the data in buf
957 * making new_offset be bigger than old_offset can also sync remained data
960 old_offset
= bitmap_bufsize
* (last_pfn
/ bits_per_buf
);
961 new_offset
= bitmap_bufsize
* (pfn
/ bits_per_buf
);
963 while (old_offset
< new_offset
) {
964 /* calculate the offset and write dump_bitmap */
965 offset_bitmap1
= s
->offset_dump_bitmap
+ old_offset
;
966 if (write_buffer(s
->fd
, offset_bitmap1
, buf
,
967 bitmap_bufsize
) < 0) {
971 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
972 offset_bitmap2
= s
->offset_dump_bitmap
+ s
->len_dump_bitmap
+
974 if (write_buffer(s
->fd
, offset_bitmap2
, buf
,
975 bitmap_bufsize
) < 0) {
979 memset(buf
, 0, bitmap_bufsize
);
980 old_offset
+= bitmap_bufsize
;
983 /* get the exact place of the bit in the buf, and set it */
984 byte
= (pfn
% bits_per_buf
) / CHAR_BIT
;
985 bit
= (pfn
% bits_per_buf
) % CHAR_BIT
;
987 buf
[byte
] |= 1u << bit
;
989 buf
[byte
] &= ~(1u << bit
);
995 static uint64_t dump_paddr_to_pfn(DumpState
*s
, uint64_t addr
)
997 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
999 return (addr
>> target_page_shift
) - ARCH_PFN_OFFSET
;
1002 static uint64_t dump_pfn_to_paddr(DumpState
*s
, uint64_t pfn
)
1004 int target_page_shift
= ctz32(s
->dump_info
.page_size
);
1006 return (pfn
+ ARCH_PFN_OFFSET
) << target_page_shift
;
1010 * exam every page and return the page frame number and the address of the page.
1011 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1012 * blocks, so block->target_start and block->target_end should be interal
1013 * multiples of the target page size.
1015 static bool get_next_page(GuestPhysBlock
**blockptr
, uint64_t *pfnptr
,
1016 uint8_t **bufptr
, DumpState
*s
)
1018 GuestPhysBlock
*block
= *blockptr
;
1019 hwaddr addr
, target_page_mask
= ~((hwaddr
)s
->dump_info
.page_size
- 1);
1022 /* block == NULL means the start of the iteration */
1024 block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1026 assert((block
->target_start
& ~target_page_mask
) == 0);
1027 assert((block
->target_end
& ~target_page_mask
) == 0);
1028 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1030 *bufptr
= block
->host_addr
;
1035 *pfnptr
= *pfnptr
+ 1;
1036 addr
= dump_pfn_to_paddr(s
, *pfnptr
);
1038 if ((addr
>= block
->target_start
) &&
1039 (addr
+ s
->dump_info
.page_size
<= block
->target_end
)) {
1040 buf
= block
->host_addr
+ (addr
- block
->target_start
);
1042 /* the next page is in the next block */
1043 block
= QTAILQ_NEXT(block
, next
);
1048 assert((block
->target_start
& ~target_page_mask
) == 0);
1049 assert((block
->target_end
& ~target_page_mask
) == 0);
1050 *pfnptr
= dump_paddr_to_pfn(s
, block
->target_start
);
1051 buf
= block
->host_addr
;
1061 static void write_dump_bitmap(DumpState
*s
, Error
**errp
)
1064 uint64_t last_pfn
, pfn
;
1065 void *dump_bitmap_buf
;
1066 size_t num_dumpable
;
1067 GuestPhysBlock
*block_iter
= NULL
;
1068 size_t bitmap_bufsize
= dump_bitmap_get_bufsize(s
);
1069 size_t bits_per_buf
= bitmap_bufsize
* CHAR_BIT
;
1071 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1072 dump_bitmap_buf
= g_malloc0(bitmap_bufsize
);
1078 * exam memory page by page, and set the bit in dump_bitmap corresponded
1079 * to the existing page.
1081 while (get_next_page(&block_iter
, &pfn
, NULL
, s
)) {
1082 ret
= set_dump_bitmap(last_pfn
, pfn
, true, dump_bitmap_buf
, s
);
1084 error_setg(errp
, "dump: failed to set dump_bitmap");
1093 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1094 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1095 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1097 if (num_dumpable
> 0) {
1098 ret
= set_dump_bitmap(last_pfn
, last_pfn
+ bits_per_buf
, false,
1099 dump_bitmap_buf
, s
);
1101 error_setg(errp
, "dump: failed to sync dump_bitmap");
1106 /* number of dumpable pages that will be dumped later */
1107 s
->num_dumpable
= num_dumpable
;
1110 g_free(dump_bitmap_buf
);
1113 static void prepare_data_cache(DataCache
*data_cache
, DumpState
*s
,
1116 data_cache
->fd
= s
->fd
;
1117 data_cache
->data_size
= 0;
1118 data_cache
->buf_size
= 4 * dump_bitmap_get_bufsize(s
);
1119 data_cache
->buf
= g_malloc0(data_cache
->buf_size
);
1120 data_cache
->offset
= offset
;
1123 static int write_cache(DataCache
*dc
, const void *buf
, size_t size
,
1127 * dc->buf_size should not be less than size, otherwise dc will never be
1130 assert(size
<= dc
->buf_size
);
1133 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1134 * otherwise check if the space is enough for caching data in buf, if not,
1135 * write the data in dc->buf to dc->fd and reset dc->buf
1137 if ((!flag_sync
&& dc
->data_size
+ size
> dc
->buf_size
) ||
1138 (flag_sync
&& dc
->data_size
> 0)) {
1139 if (write_buffer(dc
->fd
, dc
->offset
, dc
->buf
, dc
->data_size
) < 0) {
1143 dc
->offset
+= dc
->data_size
;
1148 memcpy(dc
->buf
+ dc
->data_size
, buf
, size
);
1149 dc
->data_size
+= size
;
1155 static void free_data_cache(DataCache
*data_cache
)
1157 g_free(data_cache
->buf
);
1160 static size_t get_len_buf_out(size_t page_size
, uint32_t flag_compress
)
1162 switch (flag_compress
) {
1163 case DUMP_DH_COMPRESSED_ZLIB
:
1164 return compressBound(page_size
);
1166 case DUMP_DH_COMPRESSED_LZO
:
1168 * LZO will expand incompressible data by a little amount. Please check
1169 * the following URL to see the expansion calculation:
1170 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1172 return page_size
+ page_size
/ 16 + 64 + 3;
1174 #ifdef CONFIG_SNAPPY
1175 case DUMP_DH_COMPRESSED_SNAPPY
:
1176 return snappy_max_compressed_length(page_size
);
1183 * check if the page is all 0
1185 static inline bool is_zero_page(const uint8_t *buf
, size_t page_size
)
1187 return buffer_is_zero(buf
, page_size
);
1190 static void write_dump_pages(DumpState
*s
, Error
**errp
)
1193 DataCache page_desc
, page_data
;
1194 size_t len_buf_out
, size_out
;
1196 lzo_bytep wrkmem
= NULL
;
1198 uint8_t *buf_out
= NULL
;
1199 off_t offset_desc
, offset_data
;
1200 PageDescriptor pd
, pd_zero
;
1202 GuestPhysBlock
*block_iter
= NULL
;
1205 /* get offset of page_desc and page_data in dump file */
1206 offset_desc
= s
->offset_page
;
1207 offset_data
= offset_desc
+ sizeof(PageDescriptor
) * s
->num_dumpable
;
1209 prepare_data_cache(&page_desc
, s
, offset_desc
);
1210 prepare_data_cache(&page_data
, s
, offset_data
);
1212 /* prepare buffer to store compressed data */
1213 len_buf_out
= get_len_buf_out(s
->dump_info
.page_size
, s
->flag_compress
);
1214 assert(len_buf_out
!= 0);
1217 wrkmem
= g_malloc(LZO1X_1_MEM_COMPRESS
);
1220 buf_out
= g_malloc(len_buf_out
);
1223 * init zero page's page_desc and page_data, because every zero page
1224 * uses the same page_data
1226 pd_zero
.size
= cpu_to_dump32(s
, s
->dump_info
.page_size
);
1227 pd_zero
.flags
= cpu_to_dump32(s
, 0);
1228 pd_zero
.offset
= cpu_to_dump64(s
, offset_data
);
1229 pd_zero
.page_flags
= cpu_to_dump64(s
, 0);
1230 buf
= g_malloc0(s
->dump_info
.page_size
);
1231 ret
= write_cache(&page_data
, buf
, s
->dump_info
.page_size
, false);
1234 error_setg(errp
, "dump: failed to write page data (zero page)");
1238 offset_data
+= s
->dump_info
.page_size
;
1241 * dump memory to vmcore page by page. zero page will all be resided in the
1242 * first page of page section
1244 while (get_next_page(&block_iter
, &pfn_iter
, &buf
, s
)) {
1245 /* check zero page */
1246 if (is_zero_page(buf
, s
->dump_info
.page_size
)) {
1247 ret
= write_cache(&page_desc
, &pd_zero
, sizeof(PageDescriptor
),
1250 error_setg(errp
, "dump: failed to write page desc");
1255 * not zero page, then:
1256 * 1. compress the page
1257 * 2. write the compressed page into the cache of page_data
1258 * 3. get page desc of the compressed page and write it into the
1259 * cache of page_desc
1261 * only one compression format will be used here, for
1262 * s->flag_compress is set. But when compression fails to work,
1263 * we fall back to save in plaintext.
1265 size_out
= len_buf_out
;
1266 if ((s
->flag_compress
& DUMP_DH_COMPRESSED_ZLIB
) &&
1267 (compress2(buf_out
, (uLongf
*)&size_out
, buf
,
1268 s
->dump_info
.page_size
, Z_BEST_SPEED
) == Z_OK
) &&
1269 (size_out
< s
->dump_info
.page_size
)) {
1270 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_ZLIB
);
1271 pd
.size
= cpu_to_dump32(s
, size_out
);
1273 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1275 error_setg(errp
, "dump: failed to write page data");
1279 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_LZO
) &&
1280 (lzo1x_1_compress(buf
, s
->dump_info
.page_size
, buf_out
,
1281 (lzo_uint
*)&size_out
, wrkmem
) == LZO_E_OK
) &&
1282 (size_out
< s
->dump_info
.page_size
)) {
1283 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_LZO
);
1284 pd
.size
= cpu_to_dump32(s
, size_out
);
1286 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1288 error_setg(errp
, "dump: failed to write page data");
1292 #ifdef CONFIG_SNAPPY
1293 } else if ((s
->flag_compress
& DUMP_DH_COMPRESSED_SNAPPY
) &&
1294 (snappy_compress((char *)buf
, s
->dump_info
.page_size
,
1295 (char *)buf_out
, &size_out
) == SNAPPY_OK
) &&
1296 (size_out
< s
->dump_info
.page_size
)) {
1297 pd
.flags
= cpu_to_dump32(s
, DUMP_DH_COMPRESSED_SNAPPY
);
1298 pd
.size
= cpu_to_dump32(s
, size_out
);
1300 ret
= write_cache(&page_data
, buf_out
, size_out
, false);
1302 error_setg(errp
, "dump: failed to write page data");
1308 * fall back to save in plaintext, size_out should be
1309 * assigned the target's page size
1311 pd
.flags
= cpu_to_dump32(s
, 0);
1312 size_out
= s
->dump_info
.page_size
;
1313 pd
.size
= cpu_to_dump32(s
, size_out
);
1315 ret
= write_cache(&page_data
, buf
,
1316 s
->dump_info
.page_size
, false);
1318 error_setg(errp
, "dump: failed to write page data");
1323 /* get and write page desc here */
1324 pd
.page_flags
= cpu_to_dump64(s
, 0);
1325 pd
.offset
= cpu_to_dump64(s
, offset_data
);
1326 offset_data
+= size_out
;
1328 ret
= write_cache(&page_desc
, &pd
, sizeof(PageDescriptor
), false);
1330 error_setg(errp
, "dump: failed to write page desc");
1334 s
->written_size
+= s
->dump_info
.page_size
;
1337 ret
= write_cache(&page_desc
, NULL
, 0, true);
1339 error_setg(errp
, "dump: failed to sync cache for page_desc");
1342 ret
= write_cache(&page_data
, NULL
, 0, true);
1344 error_setg(errp
, "dump: failed to sync cache for page_data");
1349 free_data_cache(&page_desc
);
1350 free_data_cache(&page_data
);
1359 static void create_kdump_vmcore(DumpState
*s
, Error
**errp
)
1362 Error
*local_err
= NULL
;
1365 * the kdump-compressed format is:
1367 * +------------------------------------------+ 0x0
1368 * | main header (struct disk_dump_header) |
1369 * |------------------------------------------+ block 1
1370 * | sub header (struct kdump_sub_header) |
1371 * |------------------------------------------+ block 2
1372 * | 1st-dump_bitmap |
1373 * |------------------------------------------+ block 2 + X blocks
1374 * | 2nd-dump_bitmap | (aligned by block)
1375 * |------------------------------------------+ block 2 + 2 * X blocks
1376 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1377 * | page desc for pfn 1 (struct page_desc) |
1379 * |------------------------------------------| (not aligned by block)
1380 * | page data (pfn 0) |
1381 * | page data (pfn 1) |
1383 * +------------------------------------------+
1386 ret
= write_start_flat_header(s
->fd
);
1388 error_setg(errp
, "dump: failed to write start flat header");
1392 write_dump_header(s
, &local_err
);
1394 error_propagate(errp
, local_err
);
1398 write_dump_bitmap(s
, &local_err
);
1400 error_propagate(errp
, local_err
);
1404 write_dump_pages(s
, &local_err
);
1406 error_propagate(errp
, local_err
);
1410 ret
= write_end_flat_header(s
->fd
);
1412 error_setg(errp
, "dump: failed to write end flat header");
1417 static ram_addr_t
get_start_block(DumpState
*s
)
1419 GuestPhysBlock
*block
;
1421 if (!s
->has_filter
) {
1422 s
->next_block
= QTAILQ_FIRST(&s
->guest_phys_blocks
.head
);
1426 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1427 if (block
->target_start
>= s
->begin
+ s
->length
||
1428 block
->target_end
<= s
->begin
) {
1429 /* This block is out of the range */
1433 s
->next_block
= block
;
1434 if (s
->begin
> block
->target_start
) {
1435 s
->start
= s
->begin
- block
->target_start
;
1445 static void get_max_mapnr(DumpState
*s
)
1447 GuestPhysBlock
*last_block
;
1449 last_block
= QTAILQ_LAST(&s
->guest_phys_blocks
.head
, GuestPhysBlockHead
);
1450 s
->max_mapnr
= dump_paddr_to_pfn(s
, last_block
->target_end
);
1453 static DumpState dump_state_global
= { .status
= DUMP_STATUS_NONE
};
1455 static void dump_state_prepare(DumpState
*s
)
1457 /* zero the struct, setting status to active */
1458 *s
= (DumpState
) { .status
= DUMP_STATUS_ACTIVE
};
1461 bool dump_in_progress(void)
1463 DumpState
*state
= &dump_state_global
;
1464 return (atomic_read(&state
->status
) == DUMP_STATUS_ACTIVE
);
1467 /* calculate total size of memory to be dumped (taking filter into
1469 static int64_t dump_calculate_size(DumpState
*s
)
1471 GuestPhysBlock
*block
;
1472 int64_t size
= 0, total
= 0, left
= 0, right
= 0;
1474 QTAILQ_FOREACH(block
, &s
->guest_phys_blocks
.head
, next
) {
1475 if (s
->has_filter
) {
1476 /* calculate the overlapped region. */
1477 left
= MAX(s
->begin
, block
->target_start
);
1478 right
= MIN(s
->begin
+ s
->length
, block
->target_end
);
1479 size
= right
- left
;
1480 size
= size
> 0 ? size
: 0;
1482 /* count the whole region in */
1483 size
= (block
->target_end
- block
->target_start
);
1491 static void dump_init(DumpState
*s
, int fd
, bool has_format
,
1492 DumpGuestMemoryFormat format
, bool paging
, bool has_filter
,
1493 int64_t begin
, int64_t length
, Error
**errp
)
1500 s
->has_format
= has_format
;
1502 s
->written_size
= 0;
1504 /* kdump-compressed is conflict with paging and filter */
1505 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1506 assert(!paging
&& !has_filter
);
1509 if (runstate_is_running()) {
1510 vm_stop(RUN_STATE_SAVE_VM
);
1516 /* If we use KVM, we should synchronize the registers before we get dump
1517 * info or physmap info.
1519 cpu_synchronize_all_states();
1526 s
->has_filter
= has_filter
;
1530 memory_mapping_list_init(&s
->list
);
1532 guest_phys_blocks_init(&s
->guest_phys_blocks
);
1533 guest_phys_blocks_append(&s
->guest_phys_blocks
);
1534 s
->total_size
= dump_calculate_size(s
);
1535 #ifdef DEBUG_DUMP_GUEST_MEMORY
1536 fprintf(stderr
, "DUMP: total memory to dump: %lu\n", s
->total_size
);
1539 s
->start
= get_start_block(s
);
1540 if (s
->start
== -1) {
1541 error_setg(errp
, QERR_INVALID_PARAMETER
, "begin");
1545 /* get dump info: endian, class and architecture.
1546 * If the target architecture is not supported, cpu_get_dump_info() will
1549 ret
= cpu_get_dump_info(&s
->dump_info
, &s
->guest_phys_blocks
);
1551 error_setg(errp
, QERR_UNSUPPORTED
);
1555 if (!s
->dump_info
.page_size
) {
1556 s
->dump_info
.page_size
= TARGET_PAGE_SIZE
;
1559 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
1560 s
->dump_info
.d_machine
, nr_cpus
);
1561 if (s
->note_size
< 0) {
1562 error_setg(errp
, QERR_UNSUPPORTED
);
1566 /* get memory mapping */
1568 qemu_get_guest_memory_mapping(&s
->list
, &s
->guest_phys_blocks
, &err
);
1570 error_propagate(errp
, err
);
1574 qemu_get_guest_simple_memory_mapping(&s
->list
, &s
->guest_phys_blocks
);
1577 s
->nr_cpus
= nr_cpus
;
1582 tmp
= DIV_ROUND_UP(DIV_ROUND_UP(s
->max_mapnr
, CHAR_BIT
),
1583 s
->dump_info
.page_size
);
1584 s
->len_dump_bitmap
= tmp
* s
->dump_info
.page_size
;
1586 /* init for kdump-compressed format */
1587 if (has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1589 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
:
1590 s
->flag_compress
= DUMP_DH_COMPRESSED_ZLIB
;
1593 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
:
1595 if (lzo_init() != LZO_E_OK
) {
1596 error_setg(errp
, "failed to initialize the LZO library");
1600 s
->flag_compress
= DUMP_DH_COMPRESSED_LZO
;
1603 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
:
1604 s
->flag_compress
= DUMP_DH_COMPRESSED_SNAPPY
;
1608 s
->flag_compress
= 0;
1614 if (s
->has_filter
) {
1615 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
1619 * calculate phdr_num
1621 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1623 s
->phdr_num
= 1; /* PT_NOTE */
1624 if (s
->list
.num
< UINT16_MAX
- 2) {
1625 s
->phdr_num
+= s
->list
.num
;
1626 s
->have_section
= false;
1628 s
->have_section
= true;
1629 s
->phdr_num
= PN_XNUM
;
1630 s
->sh_info
= 1; /* PT_NOTE */
1632 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1633 if (s
->list
.num
<= UINT32_MAX
- 1) {
1634 s
->sh_info
+= s
->list
.num
;
1636 s
->sh_info
= UINT32_MAX
;
1640 if (s
->dump_info
.d_class
== ELFCLASS64
) {
1641 if (s
->have_section
) {
1642 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1643 sizeof(Elf64_Phdr
) * s
->sh_info
+
1644 sizeof(Elf64_Shdr
) + s
->note_size
;
1646 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
1647 sizeof(Elf64_Phdr
) * s
->phdr_num
+ s
->note_size
;
1650 if (s
->have_section
) {
1651 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1652 sizeof(Elf32_Phdr
) * s
->sh_info
+
1653 sizeof(Elf32_Shdr
) + s
->note_size
;
1655 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
1656 sizeof(Elf32_Phdr
) * s
->phdr_num
+ s
->note_size
;
1666 /* this operation might be time consuming. */
1667 static void dump_process(DumpState
*s
, Error
**errp
)
1669 Error
*local_err
= NULL
;
1670 DumpQueryResult
*result
= NULL
;
1672 if (s
->has_format
&& s
->format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) {
1673 create_kdump_vmcore(s
, &local_err
);
1675 create_vmcore(s
, &local_err
);
1678 /* make sure status is written after written_size updates */
1680 atomic_set(&s
->status
,
1681 (local_err
? DUMP_STATUS_FAILED
: DUMP_STATUS_COMPLETED
));
1683 /* send DUMP_COMPLETED message (unconditionally) */
1684 result
= qmp_query_dump(NULL
);
1685 /* should never fail */
1687 qapi_event_send_dump_completed(result
, !!local_err
, (local_err
? \
1688 error_get_pretty(local_err
) : NULL
),
1690 qapi_free_DumpQueryResult(result
);
1692 error_propagate(errp
, local_err
);
1696 static void *dump_thread(void *data
)
1699 DumpState
*s
= (DumpState
*)data
;
1700 dump_process(s
, &err
);
1705 DumpQueryResult
*qmp_query_dump(Error
**errp
)
1707 DumpQueryResult
*result
= g_new(DumpQueryResult
, 1);
1708 DumpState
*state
= &dump_state_global
;
1709 result
->status
= atomic_read(&state
->status
);
1710 /* make sure we are reading status and written_size in order */
1712 result
->completed
= state
->written_size
;
1713 result
->total
= state
->total_size
;
1717 void qmp_dump_guest_memory(bool paging
, const char *file
,
1718 bool has_detach
, bool detach
,
1719 bool has_begin
, int64_t begin
, bool has_length
,
1720 int64_t length
, bool has_format
,
1721 DumpGuestMemoryFormat format
, Error
**errp
)
1726 Error
*local_err
= NULL
;
1727 bool detach_p
= false;
1729 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1730 error_setg(errp
, "Dump not allowed during incoming migration.");
1734 /* if there is a dump in background, we should wait until the dump
1736 if (dump_in_progress()) {
1737 error_setg(errp
, "There is a dump in process, please wait.");
1742 * kdump-compressed format need the whole memory dumped, so paging or
1743 * filter is not supported here.
1745 if ((has_format
&& format
!= DUMP_GUEST_MEMORY_FORMAT_ELF
) &&
1746 (paging
|| has_begin
|| has_length
)) {
1747 error_setg(errp
, "kdump-compressed format doesn't support paging or "
1751 if (has_begin
&& !has_length
) {
1752 error_setg(errp
, QERR_MISSING_PARAMETER
, "length");
1755 if (!has_begin
&& has_length
) {
1756 error_setg(errp
, QERR_MISSING_PARAMETER
, "begin");
1763 /* check whether lzo/snappy is supported */
1765 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
) {
1766 error_setg(errp
, "kdump-lzo is not available now");
1771 #ifndef CONFIG_SNAPPY
1772 if (has_format
&& format
== DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
) {
1773 error_setg(errp
, "kdump-snappy is not available now");
1779 if (strstart(file
, "fd:", &p
)) {
1780 fd
= monitor_get_fd(cur_mon
, p
, errp
);
1787 if (strstart(file
, "file:", &p
)) {
1788 fd
= qemu_open(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
1790 error_setg_file_open(errp
, errno
, p
);
1796 error_setg(errp
, QERR_INVALID_PARAMETER
, "protocol");
1800 s
= &dump_state_global
;
1801 dump_state_prepare(s
);
1803 dump_init(s
, fd
, has_format
, format
, paging
, has_begin
,
1804 begin
, length
, &local_err
);
1806 error_propagate(errp
, local_err
);
1807 atomic_set(&s
->status
, DUMP_STATUS_FAILED
);
1814 qemu_thread_create(&s
->dump_thread
, "dump_thread", dump_thread
,
1815 s
, QEMU_THREAD_DETACHED
);
1818 dump_process(s
, errp
);
1822 DumpGuestMemoryCapability
*qmp_query_dump_guest_memory_capability(Error
**errp
)
1824 DumpGuestMemoryFormatList
*item
;
1825 DumpGuestMemoryCapability
*cap
=
1826 g_malloc0(sizeof(DumpGuestMemoryCapability
));
1828 /* elf is always available */
1829 item
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1830 cap
->formats
= item
;
1831 item
->value
= DUMP_GUEST_MEMORY_FORMAT_ELF
;
1833 /* kdump-zlib is always available */
1834 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1836 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB
;
1838 /* add new item if kdump-lzo is available */
1840 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1842 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO
;
1845 /* add new item if kdump-snappy is available */
1846 #ifdef CONFIG_SNAPPY
1847 item
->next
= g_malloc0(sizeof(DumpGuestMemoryFormatList
));
1849 item
->value
= DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY
;