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"
26 #include "exec/gdbstub.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
)
194 int endian
= s
->dump_info
.d_endian
;
196 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
197 phdr
.p_type
= cpu_convert_to_target32(PT_LOAD
, endian
);
198 phdr
.p_offset
= cpu_convert_to_target64(offset
, endian
);
199 phdr
.p_paddr
= cpu_convert_to_target64(memory_mapping
->phys_addr
, endian
);
201 /* When the memory is not stored into vmcore, offset will be -1 */
204 phdr
.p_filesz
= cpu_convert_to_target64(memory_mapping
->length
, endian
);
206 phdr
.p_memsz
= cpu_convert_to_target64(memory_mapping
->length
, endian
);
207 phdr
.p_vaddr
= cpu_convert_to_target64(memory_mapping
->virt_addr
, endian
);
209 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
211 dump_error(s
, "dump: failed to write program header table.\n");
218 static int write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
219 int phdr_index
, hwaddr offset
)
223 int endian
= s
->dump_info
.d_endian
;
225 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
226 phdr
.p_type
= cpu_convert_to_target32(PT_LOAD
, endian
);
227 phdr
.p_offset
= cpu_convert_to_target32(offset
, endian
);
228 phdr
.p_paddr
= cpu_convert_to_target32(memory_mapping
->phys_addr
, endian
);
230 /* When the memory is not stored into vmcore, offset will be -1 */
233 phdr
.p_filesz
= cpu_convert_to_target32(memory_mapping
->length
, endian
);
235 phdr
.p_memsz
= cpu_convert_to_target32(memory_mapping
->length
, endian
);
236 phdr
.p_vaddr
= cpu_convert_to_target32(memory_mapping
->virt_addr
, endian
);
238 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
240 dump_error(s
, "dump: failed to write program header table.\n");
247 static int write_elf64_note(DumpState
*s
)
250 int endian
= s
->dump_info
.d_endian
;
251 hwaddr begin
= s
->memory_offset
- s
->note_size
;
254 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
255 phdr
.p_type
= cpu_convert_to_target32(PT_NOTE
, endian
);
256 phdr
.p_offset
= cpu_convert_to_target64(begin
, endian
);
258 phdr
.p_filesz
= cpu_convert_to_target64(s
->note_size
, endian
);
259 phdr
.p_memsz
= cpu_convert_to_target64(s
->note_size
, endian
);
262 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
264 dump_error(s
, "dump: failed to write program header table.\n");
271 static int write_elf64_notes(DumpState
*s
)
278 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
279 cpu
= ENV_GET_CPU(env
);
281 ret
= cpu_write_elf64_note(fd_write_vmcore
, env
, id
, s
);
283 dump_error(s
, "dump: failed to write elf notes.\n");
288 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
289 ret
= cpu_write_elf64_qemunote(fd_write_vmcore
, env
, s
);
291 dump_error(s
, "dump: failed to write CPU status.\n");
299 static int write_elf32_note(DumpState
*s
)
301 hwaddr begin
= s
->memory_offset
- s
->note_size
;
303 int endian
= s
->dump_info
.d_endian
;
306 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
307 phdr
.p_type
= cpu_convert_to_target32(PT_NOTE
, endian
);
308 phdr
.p_offset
= cpu_convert_to_target32(begin
, endian
);
310 phdr
.p_filesz
= cpu_convert_to_target32(s
->note_size
, endian
);
311 phdr
.p_memsz
= cpu_convert_to_target32(s
->note_size
, endian
);
314 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
316 dump_error(s
, "dump: failed to write program header table.\n");
323 static int write_elf32_notes(DumpState
*s
)
330 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
331 cpu
= ENV_GET_CPU(env
);
333 ret
= cpu_write_elf32_note(fd_write_vmcore
, env
, id
, s
);
335 dump_error(s
, "dump: failed to write elf notes.\n");
340 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
341 ret
= cpu_write_elf32_qemunote(fd_write_vmcore
, env
, s
);
343 dump_error(s
, "dump: failed to write CPU status.\n");
351 static int write_elf_section(DumpState
*s
, int type
)
355 int endian
= s
->dump_info
.d_endian
;
361 shdr_size
= sizeof(Elf32_Shdr
);
362 memset(&shdr32
, 0, shdr_size
);
363 shdr32
.sh_info
= cpu_convert_to_target32(s
->sh_info
, endian
);
366 shdr_size
= sizeof(Elf64_Shdr
);
367 memset(&shdr64
, 0, shdr_size
);
368 shdr64
.sh_info
= cpu_convert_to_target32(s
->sh_info
, endian
);
372 ret
= fd_write_vmcore(&shdr
, shdr_size
, s
);
374 dump_error(s
, "dump: failed to write section header table.\n");
381 static int write_data(DumpState
*s
, void *buf
, int length
)
385 ret
= fd_write_vmcore(buf
, length
, s
);
387 dump_error(s
, "dump: failed to save memory.\n");
394 /* write the memroy to vmcore. 1 page per I/O. */
395 static int write_memory(DumpState
*s
, RAMBlock
*block
, ram_addr_t start
,
401 for (i
= 0; i
< size
/ TARGET_PAGE_SIZE
; i
++) {
402 ret
= write_data(s
, block
->host
+ start
+ i
* TARGET_PAGE_SIZE
,
409 if ((size
% TARGET_PAGE_SIZE
) != 0) {
410 ret
= write_data(s
, block
->host
+ start
+ i
* TARGET_PAGE_SIZE
,
411 size
% TARGET_PAGE_SIZE
);
420 /* get the memory's offset in the vmcore */
421 static hwaddr
get_offset(hwaddr phys_addr
,
425 hwaddr offset
= s
->memory_offset
;
426 int64_t size_in_block
, start
;
429 if (phys_addr
< s
->begin
|| phys_addr
>= s
->begin
+ s
->length
) {
434 QTAILQ_FOREACH(block
, &ram_list
.blocks
, next
) {
436 if (block
->offset
>= s
->begin
+ s
->length
||
437 block
->offset
+ block
->length
<= s
->begin
) {
438 /* This block is out of the range */
442 if (s
->begin
<= block
->offset
) {
443 start
= block
->offset
;
448 size_in_block
= block
->length
- (start
- block
->offset
);
449 if (s
->begin
+ s
->length
< block
->offset
+ block
->length
) {
450 size_in_block
-= block
->offset
+ block
->length
-
451 (s
->begin
+ s
->length
);
454 start
= block
->offset
;
455 size_in_block
= block
->length
;
458 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
459 return phys_addr
- start
+ offset
;
462 offset
+= size_in_block
;
468 static int write_elf_loads(DumpState
*s
)
471 MemoryMapping
*memory_mapping
;
472 uint32_t phdr_index
= 1;
476 if (s
->have_section
) {
477 max_index
= s
->sh_info
;
479 max_index
= s
->phdr_num
;
482 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
483 offset
= get_offset(memory_mapping
->phys_addr
, s
);
484 if (s
->dump_info
.d_class
== ELFCLASS64
) {
485 ret
= write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
);
487 ret
= write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
);
494 if (phdr_index
>= max_index
) {
502 /* write elf header, PT_NOTE and elf note to vmcore. */
503 static int dump_begin(DumpState
*s
)
508 * the vmcore's format is:
527 * we only know where the memory is saved after we write elf note into
531 /* write elf header to vmcore */
532 if (s
->dump_info
.d_class
== ELFCLASS64
) {
533 ret
= write_elf64_header(s
);
535 ret
= write_elf32_header(s
);
541 if (s
->dump_info
.d_class
== ELFCLASS64
) {
542 /* write PT_NOTE to vmcore */
543 if (write_elf64_note(s
) < 0) {
547 /* write all PT_LOAD to vmcore */
548 if (write_elf_loads(s
) < 0) {
552 /* write section to vmcore */
553 if (s
->have_section
) {
554 if (write_elf_section(s
, 1) < 0) {
559 /* write notes to vmcore */
560 if (write_elf64_notes(s
) < 0) {
565 /* write PT_NOTE to vmcore */
566 if (write_elf32_note(s
) < 0) {
570 /* write all PT_LOAD to vmcore */
571 if (write_elf_loads(s
) < 0) {
575 /* write section to vmcore */
576 if (s
->have_section
) {
577 if (write_elf_section(s
, 0) < 0) {
582 /* write notes to vmcore */
583 if (write_elf32_notes(s
) < 0) {
591 /* write PT_LOAD to vmcore */
592 static int dump_completed(DumpState
*s
)
598 static int get_next_block(DumpState
*s
, RAMBlock
*block
)
601 block
= QTAILQ_NEXT(block
, next
);
610 if (block
->offset
>= s
->begin
+ s
->length
||
611 block
->offset
+ block
->length
<= s
->begin
) {
612 /* This block is out of the range */
616 if (s
->begin
> block
->offset
) {
617 s
->start
= s
->begin
- block
->offset
;
625 /* write all memory to vmcore */
626 static int dump_iterate(DumpState
*s
)
635 size
= block
->length
;
638 if (s
->begin
+ s
->length
< block
->offset
+ block
->length
) {
639 size
-= block
->offset
+ block
->length
- (s
->begin
+ s
->length
);
642 ret
= write_memory(s
, block
, s
->start
, size
);
647 ret
= get_next_block(s
, block
);
655 static int create_vmcore(DumpState
*s
)
664 ret
= dump_iterate(s
);
672 static ram_addr_t
get_start_block(DumpState
*s
)
676 if (!s
->has_filter
) {
677 s
->block
= QTAILQ_FIRST(&ram_list
.blocks
);
681 QTAILQ_FOREACH(block
, &ram_list
.blocks
, next
) {
682 if (block
->offset
>= s
->begin
+ s
->length
||
683 block
->offset
+ block
->length
<= s
->begin
) {
684 /* This block is out of the range */
689 if (s
->begin
> block
->offset
) {
690 s
->start
= s
->begin
- block
->offset
;
700 static int dump_init(DumpState
*s
, int fd
, bool paging
, bool has_filter
,
701 int64_t begin
, int64_t length
, Error
**errp
)
707 if (runstate_is_running()) {
708 vm_stop(RUN_STATE_SAVE_VM
);
716 s
->has_filter
= has_filter
;
719 s
->start
= get_start_block(s
);
720 if (s
->start
== -1) {
721 error_set(errp
, QERR_INVALID_PARAMETER
, "begin");
726 * get dump info: endian, class and architecture.
727 * If the target architecture is not supported, cpu_get_dump_info() will
730 * if we use kvm, we should synchronize the register before we get dump
734 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
735 cpu_synchronize_state(env
);
739 ret
= cpu_get_dump_info(&s
->dump_info
);
741 error_set(errp
, QERR_UNSUPPORTED
);
745 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
746 s
->dump_info
.d_machine
, nr_cpus
);
748 error_set(errp
, QERR_UNSUPPORTED
);
752 /* get memory mapping */
753 memory_mapping_list_init(&s
->list
);
755 qemu_get_guest_memory_mapping(&s
->list
);
757 qemu_get_guest_simple_memory_mapping(&s
->list
);
761 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
767 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
769 s
->phdr_num
= 1; /* PT_NOTE */
770 if (s
->list
.num
< UINT16_MAX
- 2) {
771 s
->phdr_num
+= s
->list
.num
;
772 s
->have_section
= false;
774 s
->have_section
= true;
775 s
->phdr_num
= PN_XNUM
;
776 s
->sh_info
= 1; /* PT_NOTE */
778 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
779 if (s
->list
.num
<= UINT32_MAX
- 1) {
780 s
->sh_info
+= s
->list
.num
;
782 s
->sh_info
= UINT32_MAX
;
786 if (s
->dump_info
.d_class
== ELFCLASS64
) {
787 if (s
->have_section
) {
788 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
789 sizeof(Elf64_Phdr
) * s
->sh_info
+
790 sizeof(Elf64_Shdr
) + s
->note_size
;
792 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
793 sizeof(Elf64_Phdr
) * s
->phdr_num
+ s
->note_size
;
796 if (s
->have_section
) {
797 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
798 sizeof(Elf32_Phdr
) * s
->sh_info
+
799 sizeof(Elf32_Shdr
) + s
->note_size
;
801 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
802 sizeof(Elf32_Phdr
) * s
->phdr_num
+ s
->note_size
;
816 void qmp_dump_guest_memory(bool paging
, const char *file
, bool has_begin
,
817 int64_t begin
, bool has_length
, int64_t length
,
825 if (has_begin
&& !has_length
) {
826 error_set(errp
, QERR_MISSING_PARAMETER
, "length");
829 if (!has_begin
&& has_length
) {
830 error_set(errp
, QERR_MISSING_PARAMETER
, "begin");
835 if (strstart(file
, "fd:", &p
)) {
836 fd
= monitor_get_fd(cur_mon
, p
, errp
);
843 if (strstart(file
, "file:", &p
)) {
844 fd
= qemu_open(p
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
);
846 error_set(errp
, QERR_OPEN_FILE_FAILED
, p
);
852 error_set(errp
, QERR_INVALID_PARAMETER
, "protocol");
856 s
= g_malloc(sizeof(DumpState
));
858 ret
= dump_init(s
, fd
, paging
, has_begin
, begin
, length
, errp
);
864 if (create_vmcore(s
) < 0 && !error_is_set(s
->errp
)) {
865 error_set(errp
, QERR_IO_ERROR
);