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 "qapi/error.h"
25 #include "qmp-commands.h"
27 static uint16_t cpu_convert_to_target16(uint16_t val
, int endian
)
29 if (endian
== ELFDATA2LSB
) {
30 val
= cpu_to_le16(val
);
32 val
= cpu_to_be16(val
);
38 static uint32_t cpu_convert_to_target32(uint32_t val
, int endian
)
40 if (endian
== ELFDATA2LSB
) {
41 val
= cpu_to_le32(val
);
43 val
= cpu_to_be32(val
);
49 static uint64_t cpu_convert_to_target64(uint64_t val
, int endian
)
51 if (endian
== ELFDATA2LSB
) {
52 val
= cpu_to_le64(val
);
54 val
= cpu_to_be64(val
);
60 typedef struct DumpState
{
61 ArchDumpInfo dump_info
;
62 MemoryMappingList list
;
79 static int dump_cleanup(DumpState
*s
)
83 memory_mapping_list_free(&s
->list
);
94 static void dump_error(DumpState
*s
, const char *reason
)
99 static int fd_write_vmcore(void *buf
, size_t size
, void *opaque
)
101 DumpState
*s
= opaque
;
104 written_size
= qemu_write_full(s
->fd
, buf
, size
);
105 if (written_size
!= size
) {
112 static int write_elf64_header(DumpState
*s
)
114 Elf64_Ehdr elf_header
;
116 int endian
= s
->dump_info
.d_endian
;
118 memset(&elf_header
, 0, sizeof(Elf64_Ehdr
));
119 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
120 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS64
;
121 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
122 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
123 elf_header
.e_type
= cpu_convert_to_target16(ET_CORE
, endian
);
124 elf_header
.e_machine
= cpu_convert_to_target16(s
->dump_info
.d_machine
,
126 elf_header
.e_version
= cpu_convert_to_target32(EV_CURRENT
, endian
);
127 elf_header
.e_ehsize
= cpu_convert_to_target16(sizeof(elf_header
), endian
);
128 elf_header
.e_phoff
= cpu_convert_to_target64(sizeof(Elf64_Ehdr
), endian
);
129 elf_header
.e_phentsize
= cpu_convert_to_target16(sizeof(Elf64_Phdr
),
131 elf_header
.e_phnum
= cpu_convert_to_target16(s
->phdr_num
, endian
);
132 if (s
->have_section
) {
133 uint64_t shoff
= sizeof(Elf64_Ehdr
) + sizeof(Elf64_Phdr
) * s
->sh_info
;
135 elf_header
.e_shoff
= cpu_convert_to_target64(shoff
, endian
);
136 elf_header
.e_shentsize
= cpu_convert_to_target16(sizeof(Elf64_Shdr
),
138 elf_header
.e_shnum
= cpu_convert_to_target16(1, endian
);
141 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
143 dump_error(s
, "dump: failed to write elf header.\n");
150 static int write_elf32_header(DumpState
*s
)
152 Elf32_Ehdr elf_header
;
154 int endian
= s
->dump_info
.d_endian
;
156 memset(&elf_header
, 0, sizeof(Elf32_Ehdr
));
157 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
158 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS32
;
159 elf_header
.e_ident
[EI_DATA
] = endian
;
160 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
161 elf_header
.e_type
= cpu_convert_to_target16(ET_CORE
, endian
);
162 elf_header
.e_machine
= cpu_convert_to_target16(s
->dump_info
.d_machine
,
164 elf_header
.e_version
= cpu_convert_to_target32(EV_CURRENT
, endian
);
165 elf_header
.e_ehsize
= cpu_convert_to_target16(sizeof(elf_header
), endian
);
166 elf_header
.e_phoff
= cpu_convert_to_target32(sizeof(Elf32_Ehdr
), endian
);
167 elf_header
.e_phentsize
= cpu_convert_to_target16(sizeof(Elf32_Phdr
),
169 elf_header
.e_phnum
= cpu_convert_to_target16(s
->phdr_num
, endian
);
170 if (s
->have_section
) {
171 uint32_t shoff
= sizeof(Elf32_Ehdr
) + sizeof(Elf32_Phdr
) * s
->sh_info
;
173 elf_header
.e_shoff
= cpu_convert_to_target32(shoff
, endian
);
174 elf_header
.e_shentsize
= cpu_convert_to_target16(sizeof(Elf32_Shdr
),
176 elf_header
.e_shnum
= cpu_convert_to_target16(1, endian
);
179 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
181 dump_error(s
, "dump: failed to write elf header.\n");
188 static int write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
189 int phdr_index
, hwaddr offset
)
193 int endian
= s
->dump_info
.d_endian
;
195 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
196 phdr
.p_type
= cpu_convert_to_target32(PT_LOAD
, endian
);
197 phdr
.p_offset
= cpu_convert_to_target64(offset
, endian
);
198 phdr
.p_paddr
= cpu_convert_to_target64(memory_mapping
->phys_addr
, endian
);
200 /* When the memory is not stored into vmcore, offset will be -1 */
203 phdr
.p_filesz
= cpu_convert_to_target64(memory_mapping
->length
, endian
);
205 phdr
.p_memsz
= cpu_convert_to_target64(memory_mapping
->length
, endian
);
206 phdr
.p_vaddr
= cpu_convert_to_target64(memory_mapping
->virt_addr
, endian
);
208 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
210 dump_error(s
, "dump: failed to write program header table.\n");
217 static int write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
218 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
);
229 /* When the memory is not stored into vmcore, offset will be -1 */
232 phdr
.p_filesz
= cpu_convert_to_target32(memory_mapping
->length
, endian
);
234 phdr
.p_memsz
= cpu_convert_to_target32(memory_mapping
->length
, endian
);
235 phdr
.p_vaddr
= cpu_convert_to_target32(memory_mapping
->virt_addr
, endian
);
237 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
239 dump_error(s
, "dump: failed to write program header table.\n");
246 static int write_elf64_note(DumpState
*s
)
249 int endian
= s
->dump_info
.d_endian
;
250 hwaddr begin
= s
->memory_offset
- s
->note_size
;
253 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
254 phdr
.p_type
= cpu_convert_to_target32(PT_NOTE
, endian
);
255 phdr
.p_offset
= cpu_convert_to_target64(begin
, endian
);
257 phdr
.p_filesz
= cpu_convert_to_target64(s
->note_size
, endian
);
258 phdr
.p_memsz
= cpu_convert_to_target64(s
->note_size
, endian
);
261 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
263 dump_error(s
, "dump: failed to write program header table.\n");
270 static inline int cpu_index(CPUState
*cpu
)
272 return cpu
->cpu_index
+ 1;
275 static int write_elf64_notes(DumpState
*s
)
282 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
283 cpu
= ENV_GET_CPU(env
);
285 ret
= cpu_write_elf64_note(fd_write_vmcore
, env
, id
, s
);
287 dump_error(s
, "dump: failed to write elf notes.\n");
292 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
293 ret
= cpu_write_elf64_qemunote(fd_write_vmcore
, env
, s
);
295 dump_error(s
, "dump: failed to write CPU status.\n");
303 static int write_elf32_note(DumpState
*s
)
305 hwaddr begin
= s
->memory_offset
- s
->note_size
;
307 int endian
= s
->dump_info
.d_endian
;
310 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
311 phdr
.p_type
= cpu_convert_to_target32(PT_NOTE
, endian
);
312 phdr
.p_offset
= cpu_convert_to_target32(begin
, endian
);
314 phdr
.p_filesz
= cpu_convert_to_target32(s
->note_size
, endian
);
315 phdr
.p_memsz
= cpu_convert_to_target32(s
->note_size
, endian
);
318 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
320 dump_error(s
, "dump: failed to write program header table.\n");
327 static int write_elf32_notes(DumpState
*s
)
334 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
335 cpu
= ENV_GET_CPU(env
);
337 ret
= cpu_write_elf32_note(fd_write_vmcore
, env
, id
, s
);
339 dump_error(s
, "dump: failed to write elf notes.\n");
344 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
345 ret
= cpu_write_elf32_qemunote(fd_write_vmcore
, env
, s
);
347 dump_error(s
, "dump: failed to write CPU status.\n");
355 static int write_elf_section(DumpState
*s
, int type
)
359 int endian
= s
->dump_info
.d_endian
;
365 shdr_size
= sizeof(Elf32_Shdr
);
366 memset(&shdr32
, 0, shdr_size
);
367 shdr32
.sh_info
= cpu_convert_to_target32(s
->sh_info
, endian
);
370 shdr_size
= sizeof(Elf64_Shdr
);
371 memset(&shdr64
, 0, shdr_size
);
372 shdr64
.sh_info
= cpu_convert_to_target32(s
->sh_info
, endian
);
376 ret
= fd_write_vmcore(&shdr
, shdr_size
, s
);
378 dump_error(s
, "dump: failed to write section header table.\n");
385 static int write_data(DumpState
*s
, void *buf
, int length
)
389 ret
= fd_write_vmcore(buf
, length
, s
);
391 dump_error(s
, "dump: failed to save memory.\n");
398 /* write the memroy to vmcore. 1 page per I/O. */
399 static int write_memory(DumpState
*s
, RAMBlock
*block
, ram_addr_t start
,
405 for (i
= 0; i
< size
/ TARGET_PAGE_SIZE
; i
++) {
406 ret
= write_data(s
, block
->host
+ start
+ i
* TARGET_PAGE_SIZE
,
413 if ((size
% TARGET_PAGE_SIZE
) != 0) {
414 ret
= write_data(s
, block
->host
+ start
+ i
* TARGET_PAGE_SIZE
,
415 size
% TARGET_PAGE_SIZE
);
424 /* get the memory's offset in the vmcore */
425 static hwaddr
get_offset(hwaddr phys_addr
,
429 hwaddr offset
= s
->memory_offset
;
430 int64_t size_in_block
, start
;
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 return phys_addr
- start
+ offset
;
466 offset
+= size_in_block
;
472 static int write_elf_loads(DumpState
*s
)
475 MemoryMapping
*memory_mapping
;
476 uint32_t phdr_index
= 1;
480 if (s
->have_section
) {
481 max_index
= s
->sh_info
;
483 max_index
= s
->phdr_num
;
486 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
487 offset
= get_offset(memory_mapping
->phys_addr
, s
);
488 if (s
->dump_info
.d_class
== ELFCLASS64
) {
489 ret
= write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
);
491 ret
= write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
);
498 if (phdr_index
>= max_index
) {
506 /* write elf header, PT_NOTE and elf note to vmcore. */
507 static int dump_begin(DumpState
*s
)
512 * the vmcore's format is:
531 * we only know where the memory is saved after we write elf note into
535 /* write elf header to vmcore */
536 if (s
->dump_info
.d_class
== ELFCLASS64
) {
537 ret
= write_elf64_header(s
);
539 ret
= write_elf32_header(s
);
545 if (s
->dump_info
.d_class
== ELFCLASS64
) {
546 /* write PT_NOTE to vmcore */
547 if (write_elf64_note(s
) < 0) {
551 /* write all PT_LOAD to vmcore */
552 if (write_elf_loads(s
) < 0) {
556 /* write section to vmcore */
557 if (s
->have_section
) {
558 if (write_elf_section(s
, 1) < 0) {
563 /* write notes to vmcore */
564 if (write_elf64_notes(s
) < 0) {
569 /* write PT_NOTE to vmcore */
570 if (write_elf32_note(s
) < 0) {
574 /* write all PT_LOAD to vmcore */
575 if (write_elf_loads(s
) < 0) {
579 /* write section to vmcore */
580 if (s
->have_section
) {
581 if (write_elf_section(s
, 0) < 0) {
586 /* write notes to vmcore */
587 if (write_elf32_notes(s
) < 0) {
595 /* write PT_LOAD to vmcore */
596 static int dump_completed(DumpState
*s
)
602 static int get_next_block(DumpState
*s
, RAMBlock
*block
)
605 block
= QTAILQ_NEXT(block
, next
);
614 if (block
->offset
>= s
->begin
+ s
->length
||
615 block
->offset
+ block
->length
<= s
->begin
) {
616 /* This block is out of the range */
620 if (s
->begin
> block
->offset
) {
621 s
->start
= s
->begin
- block
->offset
;
629 /* write all memory to vmcore */
630 static int dump_iterate(DumpState
*s
)
639 size
= block
->length
;
642 if (s
->begin
+ s
->length
< block
->offset
+ block
->length
) {
643 size
-= block
->offset
+ block
->length
- (s
->begin
+ s
->length
);
646 ret
= write_memory(s
, block
, s
->start
, size
);
651 ret
= get_next_block(s
, block
);
659 static int create_vmcore(DumpState
*s
)
668 ret
= dump_iterate(s
);
676 static ram_addr_t
get_start_block(DumpState
*s
)
680 if (!s
->has_filter
) {
681 s
->block
= QTAILQ_FIRST(&ram_list
.blocks
);
685 QTAILQ_FOREACH(block
, &ram_list
.blocks
, next
) {
686 if (block
->offset
>= s
->begin
+ s
->length
||
687 block
->offset
+ block
->length
<= s
->begin
) {
688 /* This block is out of the range */
693 if (s
->begin
> block
->offset
) {
694 s
->start
= s
->begin
- block
->offset
;
704 static int dump_init(DumpState
*s
, int fd
, bool paging
, bool has_filter
,
705 int64_t begin
, int64_t length
, Error
**errp
)
711 if (runstate_is_running()) {
712 vm_stop(RUN_STATE_SAVE_VM
);
720 s
->has_filter
= has_filter
;
723 s
->start
= get_start_block(s
);
724 if (s
->start
== -1) {
725 error_set(errp
, QERR_INVALID_PARAMETER
, "begin");
730 * get dump info: endian, class and architecture.
731 * If the target architecture is not supported, cpu_get_dump_info() will
734 * if we use kvm, we should synchronize the register before we get dump
738 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
739 cpu_synchronize_state(env
);
743 ret
= cpu_get_dump_info(&s
->dump_info
);
745 error_set(errp
, QERR_UNSUPPORTED
);
749 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
750 s
->dump_info
.d_machine
, nr_cpus
);
752 error_set(errp
, QERR_UNSUPPORTED
);
756 /* get memory mapping */
757 memory_mapping_list_init(&s
->list
);
759 qemu_get_guest_memory_mapping(&s
->list
);
761 qemu_get_guest_simple_memory_mapping(&s
->list
);
765 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
771 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
773 s
->phdr_num
= 1; /* PT_NOTE */
774 if (s
->list
.num
< UINT16_MAX
- 2) {
775 s
->phdr_num
+= s
->list
.num
;
776 s
->have_section
= false;
778 s
->have_section
= true;
779 s
->phdr_num
= PN_XNUM
;
780 s
->sh_info
= 1; /* PT_NOTE */
782 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
783 if (s
->list
.num
<= UINT32_MAX
- 1) {
784 s
->sh_info
+= s
->list
.num
;
786 s
->sh_info
= UINT32_MAX
;
790 if (s
->dump_info
.d_class
== ELFCLASS64
) {
791 if (s
->have_section
) {
792 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
793 sizeof(Elf64_Phdr
) * s
->sh_info
+
794 sizeof(Elf64_Shdr
) + s
->note_size
;
796 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
797 sizeof(Elf64_Phdr
) * s
->phdr_num
+ s
->note_size
;
800 if (s
->have_section
) {
801 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
802 sizeof(Elf32_Phdr
) * s
->sh_info
+
803 sizeof(Elf32_Shdr
) + s
->note_size
;
805 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
806 sizeof(Elf32_Phdr
) * s
->phdr_num
+ s
->note_size
;
820 void qmp_dump_guest_memory(bool paging
, const char *file
, bool has_begin
,
821 int64_t begin
, bool has_length
, int64_t length
,
829 if (has_begin
&& !has_length
) {
830 error_set(errp
, QERR_MISSING_PARAMETER
, "length");
833 if (!has_begin
&& has_length
) {
834 error_set(errp
, QERR_MISSING_PARAMETER
, "begin");
839 if (strstart(file
, "fd:", &p
)) {
840 fd
= monitor_get_fd(cur_mon
, p
, errp
);
847 if (strstart(file
, "file:", &p
)) {
848 fd
= qemu_open(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
850 error_set(errp
, QERR_OPEN_FILE_FAILED
, p
);
856 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
860 s
= g_malloc(sizeof(DumpState
));
862 ret
= dump_init(s
, fd
, paging
, has_begin
, begin
, length
, errp
);
868 if (create_vmcore(s
) < 0 && !error_is_set(s
->errp
)) {
869 error_set(errp
, QERR_IO_ERROR
);