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"
28 static uint16_t cpu_convert_to_target16(uint16_t val
, int endian
)
30 if (endian
== ELFDATA2LSB
) {
31 val
= cpu_to_le16(val
);
33 val
= cpu_to_be16(val
);
39 static uint32_t cpu_convert_to_target32(uint32_t val
, int endian
)
41 if (endian
== ELFDATA2LSB
) {
42 val
= cpu_to_le32(val
);
44 val
= cpu_to_be32(val
);
50 static uint64_t cpu_convert_to_target64(uint64_t val
, int endian
)
52 if (endian
== ELFDATA2LSB
) {
53 val
= cpu_to_le64(val
);
55 val
= cpu_to_be64(val
);
61 typedef struct DumpState
{
62 ArchDumpInfo dump_info
;
63 MemoryMappingList list
;
80 static int dump_cleanup(DumpState
*s
)
84 memory_mapping_list_free(&s
->list
);
95 static void dump_error(DumpState
*s
, const char *reason
)
100 static int fd_write_vmcore(void *buf
, size_t size
, void *opaque
)
102 DumpState
*s
= opaque
;
105 written_size
= qemu_write_full(s
->fd
, buf
, size
);
106 if (written_size
!= size
) {
113 static int write_elf64_header(DumpState
*s
)
115 Elf64_Ehdr elf_header
;
117 int endian
= s
->dump_info
.d_endian
;
119 memset(&elf_header
, 0, sizeof(Elf64_Ehdr
));
120 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
121 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS64
;
122 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
123 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
124 elf_header
.e_type
= cpu_convert_to_target16(ET_CORE
, endian
);
125 elf_header
.e_machine
= cpu_convert_to_target16(s
->dump_info
.d_machine
,
127 elf_header
.e_version
= cpu_convert_to_target32(EV_CURRENT
, endian
);
128 elf_header
.e_ehsize
= cpu_convert_to_target16(sizeof(elf_header
), endian
);
129 elf_header
.e_phoff
= cpu_convert_to_target64(sizeof(Elf64_Ehdr
), endian
);
130 elf_header
.e_phentsize
= cpu_convert_to_target16(sizeof(Elf64_Phdr
),
132 elf_header
.e_phnum
= cpu_convert_to_target16(s
->phdr_num
, endian
);
133 if (s
->have_section
) {
134 uint64_t shoff
= sizeof(Elf64_Ehdr
) + sizeof(Elf64_Phdr
) * s
->sh_info
;
136 elf_header
.e_shoff
= cpu_convert_to_target64(shoff
, endian
);
137 elf_header
.e_shentsize
= cpu_convert_to_target16(sizeof(Elf64_Shdr
),
139 elf_header
.e_shnum
= cpu_convert_to_target16(1, endian
);
142 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
144 dump_error(s
, "dump: failed to write elf header.\n");
151 static int write_elf32_header(DumpState
*s
)
153 Elf32_Ehdr elf_header
;
155 int endian
= s
->dump_info
.d_endian
;
157 memset(&elf_header
, 0, sizeof(Elf32_Ehdr
));
158 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
159 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS32
;
160 elf_header
.e_ident
[EI_DATA
] = endian
;
161 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
162 elf_header
.e_type
= cpu_convert_to_target16(ET_CORE
, endian
);
163 elf_header
.e_machine
= cpu_convert_to_target16(s
->dump_info
.d_machine
,
165 elf_header
.e_version
= cpu_convert_to_target32(EV_CURRENT
, endian
);
166 elf_header
.e_ehsize
= cpu_convert_to_target16(sizeof(elf_header
), endian
);
167 elf_header
.e_phoff
= cpu_convert_to_target32(sizeof(Elf32_Ehdr
), endian
);
168 elf_header
.e_phentsize
= cpu_convert_to_target16(sizeof(Elf32_Phdr
),
170 elf_header
.e_phnum
= cpu_convert_to_target16(s
->phdr_num
, endian
);
171 if (s
->have_section
) {
172 uint32_t shoff
= sizeof(Elf32_Ehdr
) + sizeof(Elf32_Phdr
) * s
->sh_info
;
174 elf_header
.e_shoff
= cpu_convert_to_target32(shoff
, endian
);
175 elf_header
.e_shentsize
= cpu_convert_to_target16(sizeof(Elf32_Shdr
),
177 elf_header
.e_shnum
= cpu_convert_to_target16(1, endian
);
180 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
182 dump_error(s
, "dump: failed to write elf header.\n");
189 static int write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
190 int phdr_index
, hwaddr offset
,
195 int endian
= s
->dump_info
.d_endian
;
197 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
198 phdr
.p_type
= cpu_convert_to_target32(PT_LOAD
, endian
);
199 phdr
.p_offset
= cpu_convert_to_target64(offset
, endian
);
200 phdr
.p_paddr
= cpu_convert_to_target64(memory_mapping
->phys_addr
, endian
);
201 phdr
.p_filesz
= cpu_convert_to_target64(filesz
, endian
);
202 phdr
.p_memsz
= cpu_convert_to_target64(memory_mapping
->length
, endian
);
203 phdr
.p_vaddr
= cpu_convert_to_target64(memory_mapping
->virt_addr
, endian
);
205 assert(memory_mapping
->length
>= filesz
);
207 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
209 dump_error(s
, "dump: failed to write program header table.\n");
216 static int write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
217 int phdr_index
, hwaddr offset
,
222 int endian
= s
->dump_info
.d_endian
;
224 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
225 phdr
.p_type
= cpu_convert_to_target32(PT_LOAD
, endian
);
226 phdr
.p_offset
= cpu_convert_to_target32(offset
, endian
);
227 phdr
.p_paddr
= cpu_convert_to_target32(memory_mapping
->phys_addr
, endian
);
228 phdr
.p_filesz
= cpu_convert_to_target32(filesz
, endian
);
229 phdr
.p_memsz
= cpu_convert_to_target32(memory_mapping
->length
, endian
);
230 phdr
.p_vaddr
= cpu_convert_to_target32(memory_mapping
->virt_addr
, endian
);
232 assert(memory_mapping
->length
>= filesz
);
234 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
236 dump_error(s
, "dump: failed to write program header table.\n");
243 static int write_elf64_note(DumpState
*s
)
246 int endian
= s
->dump_info
.d_endian
;
247 hwaddr begin
= s
->memory_offset
- s
->note_size
;
250 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
251 phdr
.p_type
= cpu_convert_to_target32(PT_NOTE
, endian
);
252 phdr
.p_offset
= cpu_convert_to_target64(begin
, endian
);
254 phdr
.p_filesz
= cpu_convert_to_target64(s
->note_size
, endian
);
255 phdr
.p_memsz
= cpu_convert_to_target64(s
->note_size
, endian
);
258 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
260 dump_error(s
, "dump: failed to write program header table.\n");
267 static inline int cpu_index(CPUState
*cpu
)
269 return cpu
->cpu_index
+ 1;
272 static int write_elf64_notes(DumpState
*s
)
278 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
280 ret
= cpu_write_elf64_note(fd_write_vmcore
, cpu
, id
, s
);
282 dump_error(s
, "dump: failed to write elf notes.\n");
287 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
288 ret
= cpu_write_elf64_qemunote(fd_write_vmcore
, cpu
, s
);
290 dump_error(s
, "dump: failed to write CPU status.\n");
298 static int write_elf32_note(DumpState
*s
)
300 hwaddr begin
= s
->memory_offset
- s
->note_size
;
302 int endian
= s
->dump_info
.d_endian
;
305 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
306 phdr
.p_type
= cpu_convert_to_target32(PT_NOTE
, endian
);
307 phdr
.p_offset
= cpu_convert_to_target32(begin
, endian
);
309 phdr
.p_filesz
= cpu_convert_to_target32(s
->note_size
, endian
);
310 phdr
.p_memsz
= cpu_convert_to_target32(s
->note_size
, endian
);
313 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
315 dump_error(s
, "dump: failed to write program header table.\n");
322 static int write_elf32_notes(DumpState
*s
)
328 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
330 ret
= cpu_write_elf32_note(fd_write_vmcore
, cpu
, id
, s
);
332 dump_error(s
, "dump: failed to write elf notes.\n");
337 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
338 ret
= cpu_write_elf32_qemunote(fd_write_vmcore
, cpu
, s
);
340 dump_error(s
, "dump: failed to write CPU status.\n");
348 static int write_elf_section(DumpState
*s
, int type
)
352 int endian
= s
->dump_info
.d_endian
;
358 shdr_size
= sizeof(Elf32_Shdr
);
359 memset(&shdr32
, 0, shdr_size
);
360 shdr32
.sh_info
= cpu_convert_to_target32(s
->sh_info
, endian
);
363 shdr_size
= sizeof(Elf64_Shdr
);
364 memset(&shdr64
, 0, shdr_size
);
365 shdr64
.sh_info
= cpu_convert_to_target32(s
->sh_info
, endian
);
369 ret
= fd_write_vmcore(&shdr
, shdr_size
, s
);
371 dump_error(s
, "dump: failed to write section header table.\n");
378 static int write_data(DumpState
*s
, void *buf
, int length
)
382 ret
= fd_write_vmcore(buf
, length
, s
);
384 dump_error(s
, "dump: failed to save memory.\n");
391 /* write the memroy to vmcore. 1 page per I/O. */
392 static int write_memory(DumpState
*s
, RAMBlock
*block
, ram_addr_t start
,
398 for (i
= 0; i
< size
/ TARGET_PAGE_SIZE
; i
++) {
399 ret
= write_data(s
, block
->host
+ start
+ i
* TARGET_PAGE_SIZE
,
406 if ((size
% TARGET_PAGE_SIZE
) != 0) {
407 ret
= write_data(s
, block
->host
+ start
+ i
* TARGET_PAGE_SIZE
,
408 size
% TARGET_PAGE_SIZE
);
417 /* get the memory's offset and size in the vmcore */
418 static void get_offset_range(hwaddr phys_addr
,
419 ram_addr_t mapping_length
,
425 hwaddr offset
= s
->memory_offset
;
426 int64_t size_in_block
, start
;
428 /* When the memory is not stored into vmcore, offset will be -1 */
433 if (phys_addr
< s
->begin
|| phys_addr
>= s
->begin
+ s
->length
) {
438 QTAILQ_FOREACH(block
, &ram_list
.blocks
, next
) {
440 if (block
->offset
>= s
->begin
+ s
->length
||
441 block
->offset
+ block
->length
<= s
->begin
) {
442 /* This block is out of the range */
446 if (s
->begin
<= block
->offset
) {
447 start
= block
->offset
;
452 size_in_block
= block
->length
- (start
- block
->offset
);
453 if (s
->begin
+ s
->length
< block
->offset
+ block
->length
) {
454 size_in_block
-= block
->offset
+ block
->length
-
455 (s
->begin
+ s
->length
);
458 start
= block
->offset
;
459 size_in_block
= block
->length
;
462 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
463 *p_offset
= phys_addr
- start
+ offset
;
465 /* The offset range mapped from the vmcore file must not spill over
466 * the RAMBlock, clamp it. The rest of the mapping will be
467 * zero-filled in memory at load time; see
468 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
470 *p_filesz
= phys_addr
+ mapping_length
<= start
+ size_in_block
?
472 size_in_block
- (phys_addr
- start
);
476 offset
+= size_in_block
;
480 static int write_elf_loads(DumpState
*s
)
482 hwaddr offset
, filesz
;
483 MemoryMapping
*memory_mapping
;
484 uint32_t phdr_index
= 1;
488 if (s
->have_section
) {
489 max_index
= s
->sh_info
;
491 max_index
= s
->phdr_num
;
494 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
495 get_offset_range(memory_mapping
->phys_addr
,
496 memory_mapping
->length
,
497 s
, &offset
, &filesz
);
498 if (s
->dump_info
.d_class
== ELFCLASS64
) {
499 ret
= write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
,
502 ret
= write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
,
510 if (phdr_index
>= max_index
) {
518 /* write elf header, PT_NOTE and elf note to vmcore. */
519 static int dump_begin(DumpState
*s
)
524 * the vmcore's format is:
543 * we only know where the memory is saved after we write elf note into
547 /* write elf header to vmcore */
548 if (s
->dump_info
.d_class
== ELFCLASS64
) {
549 ret
= write_elf64_header(s
);
551 ret
= write_elf32_header(s
);
557 if (s
->dump_info
.d_class
== ELFCLASS64
) {
558 /* write PT_NOTE to vmcore */
559 if (write_elf64_note(s
) < 0) {
563 /* write all PT_LOAD to vmcore */
564 if (write_elf_loads(s
) < 0) {
568 /* write section to vmcore */
569 if (s
->have_section
) {
570 if (write_elf_section(s
, 1) < 0) {
575 /* write notes to vmcore */
576 if (write_elf64_notes(s
) < 0) {
581 /* write PT_NOTE to vmcore */
582 if (write_elf32_note(s
) < 0) {
586 /* write all PT_LOAD to vmcore */
587 if (write_elf_loads(s
) < 0) {
591 /* write section to vmcore */
592 if (s
->have_section
) {
593 if (write_elf_section(s
, 0) < 0) {
598 /* write notes to vmcore */
599 if (write_elf32_notes(s
) < 0) {
607 /* write PT_LOAD to vmcore */
608 static int dump_completed(DumpState
*s
)
614 static int get_next_block(DumpState
*s
, RAMBlock
*block
)
617 block
= QTAILQ_NEXT(block
, next
);
626 if (block
->offset
>= s
->begin
+ s
->length
||
627 block
->offset
+ block
->length
<= s
->begin
) {
628 /* This block is out of the range */
632 if (s
->begin
> block
->offset
) {
633 s
->start
= s
->begin
- block
->offset
;
641 /* write all memory to vmcore */
642 static int dump_iterate(DumpState
*s
)
651 size
= block
->length
;
654 if (s
->begin
+ s
->length
< block
->offset
+ block
->length
) {
655 size
-= block
->offset
+ block
->length
- (s
->begin
+ s
->length
);
658 ret
= write_memory(s
, block
, s
->start
, size
);
663 ret
= get_next_block(s
, block
);
671 static int create_vmcore(DumpState
*s
)
680 ret
= dump_iterate(s
);
688 static ram_addr_t
get_start_block(DumpState
*s
)
692 if (!s
->has_filter
) {
693 s
->block
= QTAILQ_FIRST(&ram_list
.blocks
);
697 QTAILQ_FOREACH(block
, &ram_list
.blocks
, next
) {
698 if (block
->offset
>= s
->begin
+ s
->length
||
699 block
->offset
+ block
->length
<= s
->begin
) {
700 /* This block is out of the range */
705 if (s
->begin
> block
->offset
) {
706 s
->start
= s
->begin
- block
->offset
;
716 static int dump_init(DumpState
*s
, int fd
, bool paging
, bool has_filter
,
717 int64_t begin
, int64_t length
, Error
**errp
)
724 if (runstate_is_running()) {
725 vm_stop(RUN_STATE_SAVE_VM
);
733 s
->has_filter
= has_filter
;
736 s
->start
= get_start_block(s
);
737 if (s
->start
== -1) {
738 error_set(errp
, QERR_INVALID_PARAMETER
, "begin");
743 * get dump info: endian, class and architecture.
744 * If the target architecture is not supported, cpu_get_dump_info() will
747 * If we use KVM, we should synchronize the registers before we get dump
750 cpu_synchronize_all_states();
752 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
756 ret
= cpu_get_dump_info(&s
->dump_info
);
758 error_set(errp
, QERR_UNSUPPORTED
);
762 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
763 s
->dump_info
.d_machine
, nr_cpus
);
765 error_set(errp
, QERR_UNSUPPORTED
);
769 /* get memory mapping */
770 memory_mapping_list_init(&s
->list
);
772 qemu_get_guest_memory_mapping(&s
->list
, &err
);
774 error_propagate(errp
, err
);
778 qemu_get_guest_simple_memory_mapping(&s
->list
);
782 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
788 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
790 s
->phdr_num
= 1; /* PT_NOTE */
791 if (s
->list
.num
< UINT16_MAX
- 2) {
792 s
->phdr_num
+= s
->list
.num
;
793 s
->have_section
= false;
795 s
->have_section
= true;
796 s
->phdr_num
= PN_XNUM
;
797 s
->sh_info
= 1; /* PT_NOTE */
799 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
800 if (s
->list
.num
<= UINT32_MAX
- 1) {
801 s
->sh_info
+= s
->list
.num
;
803 s
->sh_info
= UINT32_MAX
;
807 if (s
->dump_info
.d_class
== ELFCLASS64
) {
808 if (s
->have_section
) {
809 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
810 sizeof(Elf64_Phdr
) * s
->sh_info
+
811 sizeof(Elf64_Shdr
) + s
->note_size
;
813 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
814 sizeof(Elf64_Phdr
) * s
->phdr_num
+ s
->note_size
;
817 if (s
->have_section
) {
818 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
819 sizeof(Elf32_Phdr
) * s
->sh_info
+
820 sizeof(Elf32_Shdr
) + s
->note_size
;
822 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
823 sizeof(Elf32_Phdr
) * s
->phdr_num
+ s
->note_size
;
837 void qmp_dump_guest_memory(bool paging
, const char *file
, bool has_begin
,
838 int64_t begin
, bool has_length
, int64_t length
,
846 if (has_begin
&& !has_length
) {
847 error_set(errp
, QERR_MISSING_PARAMETER
, "length");
850 if (!has_begin
&& has_length
) {
851 error_set(errp
, QERR_MISSING_PARAMETER
, "begin");
856 if (strstart(file
, "fd:", &p
)) {
857 fd
= monitor_get_fd(cur_mon
, p
, errp
);
864 if (strstart(file
, "file:", &p
)) {
865 fd
= qemu_open(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
867 error_setg_file_open(errp
, errno
, p
);
873 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
877 s
= g_malloc(sizeof(DumpState
));
879 ret
= dump_init(s
, fd
, paging
, has_begin
, begin
, length
, errp
);
885 if (create_vmcore(s
) < 0 && !error_is_set(s
->errp
)) {
886 error_set(errp
, QERR_IO_ERROR
);