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. See
10 * the COPYING file in the top-level directory.
14 #include "qemu-common.h"
17 #include <sys/procfs.h>
27 #include "memory_mapping.h"
29 #include "qmp-commands.h"
32 #if defined(CONFIG_HAVE_CORE_DUMP)
33 static uint16_t cpu_convert_to_target16(uint16_t val
, int endian
)
35 if (endian
== ELFDATA2LSB
) {
36 val
= cpu_to_le16(val
);
38 val
= cpu_to_be16(val
);
44 static uint32_t cpu_convert_to_target32(uint32_t val
, int endian
)
46 if (endian
== ELFDATA2LSB
) {
47 val
= cpu_to_le32(val
);
49 val
= cpu_to_be32(val
);
55 static uint64_t cpu_convert_to_target64(uint64_t val
, int endian
)
57 if (endian
== ELFDATA2LSB
) {
58 val
= cpu_to_le64(val
);
60 val
= cpu_to_be64(val
);
66 typedef struct DumpState
{
67 ArchDumpInfo dump_info
;
68 MemoryMappingList list
;
74 target_phys_addr_t memory_offset
;
85 static int dump_cleanup(DumpState
*s
)
89 memory_mapping_list_free(&s
->list
);
100 static void dump_error(DumpState
*s
, const char *reason
)
105 static int fd_write_vmcore(void *buf
, size_t size
, void *opaque
)
107 DumpState
*s
= opaque
;
111 /* The fd may be passed from user, and it can be non-blocked */
113 writen_size
= qemu_write_full(fd
, buf
, size
);
114 if (writen_size
!= size
&& errno
!= EAGAIN
) {
125 static int write_elf64_header(DumpState
*s
)
127 Elf64_Ehdr elf_header
;
129 int endian
= s
->dump_info
.d_endian
;
131 memset(&elf_header
, 0, sizeof(Elf64_Ehdr
));
132 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
133 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS64
;
134 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
135 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
136 elf_header
.e_type
= cpu_convert_to_target16(ET_CORE
, endian
);
137 elf_header
.e_machine
= cpu_convert_to_target16(s
->dump_info
.d_machine
,
139 elf_header
.e_version
= cpu_convert_to_target32(EV_CURRENT
, endian
);
140 elf_header
.e_ehsize
= cpu_convert_to_target16(sizeof(elf_header
), endian
);
141 elf_header
.e_phoff
= cpu_convert_to_target64(sizeof(Elf64_Ehdr
), endian
);
142 elf_header
.e_phentsize
= cpu_convert_to_target16(sizeof(Elf64_Phdr
),
144 elf_header
.e_phnum
= cpu_convert_to_target16(s
->phdr_num
, endian
);
145 if (s
->have_section
) {
146 uint64_t shoff
= sizeof(Elf64_Ehdr
) + sizeof(Elf64_Phdr
) * s
->sh_info
;
148 elf_header
.e_shoff
= cpu_convert_to_target64(shoff
, endian
);
149 elf_header
.e_shentsize
= cpu_convert_to_target16(sizeof(Elf64_Shdr
),
151 elf_header
.e_shnum
= cpu_convert_to_target16(1, endian
);
154 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
156 dump_error(s
, "dump: failed to write elf header.\n");
163 static int write_elf32_header(DumpState
*s
)
165 Elf32_Ehdr elf_header
;
167 int endian
= s
->dump_info
.d_endian
;
169 memset(&elf_header
, 0, sizeof(Elf32_Ehdr
));
170 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
171 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS32
;
172 elf_header
.e_ident
[EI_DATA
] = endian
;
173 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
174 elf_header
.e_type
= cpu_convert_to_target16(ET_CORE
, endian
);
175 elf_header
.e_machine
= cpu_convert_to_target16(s
->dump_info
.d_machine
,
177 elf_header
.e_version
= cpu_convert_to_target32(EV_CURRENT
, endian
);
178 elf_header
.e_ehsize
= cpu_convert_to_target16(sizeof(elf_header
), endian
);
179 elf_header
.e_phoff
= cpu_convert_to_target32(sizeof(Elf32_Ehdr
), endian
);
180 elf_header
.e_phentsize
= cpu_convert_to_target16(sizeof(Elf32_Phdr
),
182 elf_header
.e_phnum
= cpu_convert_to_target16(s
->phdr_num
, endian
);
183 if (s
->have_section
) {
184 uint32_t shoff
= sizeof(Elf32_Ehdr
) + sizeof(Elf32_Phdr
) * s
->sh_info
;
186 elf_header
.e_shoff
= cpu_convert_to_target32(shoff
, endian
);
187 elf_header
.e_shentsize
= cpu_convert_to_target16(sizeof(Elf32_Shdr
),
189 elf_header
.e_shnum
= cpu_convert_to_target16(1, endian
);
192 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
194 dump_error(s
, "dump: failed to write elf header.\n");
201 static int write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
202 int phdr_index
, target_phys_addr_t offset
)
206 int endian
= s
->dump_info
.d_endian
;
208 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
209 phdr
.p_type
= cpu_convert_to_target32(PT_LOAD
, endian
);
210 phdr
.p_offset
= cpu_convert_to_target64(offset
, endian
);
211 phdr
.p_paddr
= cpu_convert_to_target64(memory_mapping
->phys_addr
, endian
);
213 /* When the memory is not stored into vmcore, offset will be -1 */
216 phdr
.p_filesz
= cpu_convert_to_target64(memory_mapping
->length
, endian
);
218 phdr
.p_memsz
= cpu_convert_to_target64(memory_mapping
->length
, endian
);
219 phdr
.p_vaddr
= cpu_convert_to_target64(memory_mapping
->virt_addr
, endian
);
221 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
223 dump_error(s
, "dump: failed to write program header table.\n");
230 static int write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
231 int phdr_index
, target_phys_addr_t offset
)
235 int endian
= s
->dump_info
.d_endian
;
237 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
238 phdr
.p_type
= cpu_convert_to_target32(PT_LOAD
, endian
);
239 phdr
.p_offset
= cpu_convert_to_target32(offset
, endian
);
240 phdr
.p_paddr
= cpu_convert_to_target32(memory_mapping
->phys_addr
, endian
);
242 /* When the memory is not stored into vmcore, offset will be -1 */
245 phdr
.p_filesz
= cpu_convert_to_target32(memory_mapping
->length
, endian
);
247 phdr
.p_memsz
= cpu_convert_to_target32(memory_mapping
->length
, endian
);
248 phdr
.p_vaddr
= cpu_convert_to_target32(memory_mapping
->virt_addr
, endian
);
250 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
252 dump_error(s
, "dump: failed to write program header table.\n");
259 static int write_elf64_note(DumpState
*s
)
262 int endian
= s
->dump_info
.d_endian
;
263 target_phys_addr_t begin
= s
->memory_offset
- s
->note_size
;
266 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
267 phdr
.p_type
= cpu_convert_to_target32(PT_NOTE
, endian
);
268 phdr
.p_offset
= cpu_convert_to_target64(begin
, endian
);
270 phdr
.p_filesz
= cpu_convert_to_target64(s
->note_size
, endian
);
271 phdr
.p_memsz
= cpu_convert_to_target64(s
->note_size
, endian
);
274 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
276 dump_error(s
, "dump: failed to write program header table.\n");
283 static int write_elf64_notes(DumpState
*s
)
289 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
291 ret
= cpu_write_elf64_note(fd_write_vmcore
, env
, id
, s
);
293 dump_error(s
, "dump: failed to write elf notes.\n");
298 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
299 ret
= cpu_write_elf64_qemunote(fd_write_vmcore
, env
, s
);
301 dump_error(s
, "dump: failed to write CPU status.\n");
309 static int write_elf32_note(DumpState
*s
)
311 target_phys_addr_t begin
= s
->memory_offset
- s
->note_size
;
313 int endian
= s
->dump_info
.d_endian
;
316 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
317 phdr
.p_type
= cpu_convert_to_target32(PT_NOTE
, endian
);
318 phdr
.p_offset
= cpu_convert_to_target32(begin
, endian
);
320 phdr
.p_filesz
= cpu_convert_to_target32(s
->note_size
, endian
);
321 phdr
.p_memsz
= cpu_convert_to_target32(s
->note_size
, endian
);
324 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
326 dump_error(s
, "dump: failed to write program header table.\n");
333 static int write_elf32_notes(DumpState
*s
)
339 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
341 ret
= cpu_write_elf32_note(fd_write_vmcore
, env
, id
, s
);
343 dump_error(s
, "dump: failed to write elf notes.\n");
348 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
349 ret
= cpu_write_elf32_qemunote(fd_write_vmcore
, env
, s
);
351 dump_error(s
, "dump: failed to write CPU status.\n");
359 static int write_elf_section(DumpState
*s
, int type
)
363 int endian
= s
->dump_info
.d_endian
;
369 shdr_size
= sizeof(Elf32_Shdr
);
370 memset(&shdr32
, 0, shdr_size
);
371 shdr32
.sh_info
= cpu_convert_to_target32(s
->sh_info
, endian
);
374 shdr_size
= sizeof(Elf64_Shdr
);
375 memset(&shdr64
, 0, shdr_size
);
376 shdr64
.sh_info
= cpu_convert_to_target32(s
->sh_info
, endian
);
380 ret
= fd_write_vmcore(&shdr
, shdr_size
, s
);
382 dump_error(s
, "dump: failed to write section header table.\n");
389 static int write_data(DumpState
*s
, void *buf
, int length
)
393 ret
= fd_write_vmcore(buf
, length
, s
);
395 dump_error(s
, "dump: failed to save memory.\n");
402 /* write the memroy to vmcore. 1 page per I/O. */
403 static int write_memory(DumpState
*s
, RAMBlock
*block
, ram_addr_t start
,
409 for (i
= 0; i
< size
/ TARGET_PAGE_SIZE
; i
++) {
410 ret
= write_data(s
, block
->host
+ start
+ i
* TARGET_PAGE_SIZE
,
417 if ((size
% TARGET_PAGE_SIZE
) != 0) {
418 ret
= write_data(s
, block
->host
+ start
+ i
* TARGET_PAGE_SIZE
,
419 size
% TARGET_PAGE_SIZE
);
428 /* get the memory's offset in the vmcore */
429 static target_phys_addr_t
get_offset(target_phys_addr_t phys_addr
,
433 target_phys_addr_t offset
= s
->memory_offset
;
434 int64_t size_in_block
, start
;
437 if (phys_addr
< s
->begin
|| phys_addr
>= s
->begin
+ s
->length
) {
442 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
444 if (block
->offset
>= s
->begin
+ s
->length
||
445 block
->offset
+ block
->length
<= s
->begin
) {
446 /* This block is out of the range */
450 if (s
->begin
<= block
->offset
) {
451 start
= block
->offset
;
456 size_in_block
= block
->length
- (start
- block
->offset
);
457 if (s
->begin
+ s
->length
< block
->offset
+ block
->length
) {
458 size_in_block
-= block
->offset
+ block
->length
-
459 (s
->begin
+ s
->length
);
462 start
= block
->offset
;
463 size_in_block
= block
->length
;
466 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
467 return phys_addr
- start
+ offset
;
470 offset
+= size_in_block
;
476 static int write_elf_loads(DumpState
*s
)
478 target_phys_addr_t offset
;
479 MemoryMapping
*memory_mapping
;
480 uint32_t phdr_index
= 1;
484 if (s
->have_section
) {
485 max_index
= s
->sh_info
;
487 max_index
= s
->phdr_num
;
490 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
491 offset
= get_offset(memory_mapping
->phys_addr
, s
);
492 if (s
->dump_info
.d_class
== ELFCLASS64
) {
493 ret
= write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
);
495 ret
= write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
);
502 if (phdr_index
>= max_index
) {
510 /* write elf header, PT_NOTE and elf note to vmcore. */
511 static int dump_begin(DumpState
*s
)
516 * the vmcore's format is:
535 * we only know where the memory is saved after we write elf note into
539 /* write elf header to vmcore */
540 if (s
->dump_info
.d_class
== ELFCLASS64
) {
541 ret
= write_elf64_header(s
);
543 ret
= write_elf32_header(s
);
549 if (s
->dump_info
.d_class
== ELFCLASS64
) {
550 /* write PT_NOTE to vmcore */
551 if (write_elf64_note(s
) < 0) {
555 /* write all PT_LOAD to vmcore */
556 if (write_elf_loads(s
) < 0) {
560 /* write section to vmcore */
561 if (s
->have_section
) {
562 if (write_elf_section(s
, 1) < 0) {
567 /* write notes to vmcore */
568 if (write_elf64_notes(s
) < 0) {
573 /* write PT_NOTE to vmcore */
574 if (write_elf32_note(s
) < 0) {
578 /* write all PT_LOAD to vmcore */
579 if (write_elf_loads(s
) < 0) {
583 /* write section to vmcore */
584 if (s
->have_section
) {
585 if (write_elf_section(s
, 0) < 0) {
590 /* write notes to vmcore */
591 if (write_elf32_notes(s
) < 0) {
599 /* write PT_LOAD to vmcore */
600 static int dump_completed(DumpState
*s
)
606 static int get_next_block(DumpState
*s
, RAMBlock
*block
)
609 block
= QLIST_NEXT(block
, next
);
618 if (block
->offset
>= s
->begin
+ s
->length
||
619 block
->offset
+ block
->length
<= s
->begin
) {
620 /* This block is out of the range */
624 if (s
->begin
> block
->offset
) {
625 s
->start
= s
->begin
- block
->offset
;
633 /* write all memory to vmcore */
634 static int dump_iterate(DumpState
*s
)
643 size
= block
->length
;
646 if (s
->begin
+ s
->length
< block
->offset
+ block
->length
) {
647 size
-= block
->offset
+ block
->length
- (s
->begin
+ s
->length
);
650 ret
= write_memory(s
, block
, s
->start
, size
);
655 ret
= get_next_block(s
, block
);
663 static int create_vmcore(DumpState
*s
)
672 ret
= dump_iterate(s
);
680 static ram_addr_t
get_start_block(DumpState
*s
)
684 if (!s
->has_filter
) {
685 s
->block
= QLIST_FIRST(&ram_list
.blocks
);
689 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
690 if (block
->offset
>= s
->begin
+ s
->length
||
691 block
->offset
+ block
->length
<= s
->begin
) {
692 /* This block is out of the range */
697 if (s
->begin
> block
->offset
) {
698 s
->start
= s
->begin
- block
->offset
;
708 static int dump_init(DumpState
*s
, int fd
, bool paging
, bool has_filter
,
709 int64_t begin
, int64_t length
, Error
**errp
)
715 if (runstate_is_running()) {
716 vm_stop(RUN_STATE_SAVE_VM
);
724 s
->has_filter
= has_filter
;
727 s
->start
= get_start_block(s
);
728 if (s
->start
== -1) {
729 error_set(errp
, QERR_INVALID_PARAMETER
, "begin");
734 * get dump info: endian, class and architecture.
735 * If the target architecture is not supported, cpu_get_dump_info() will
738 * if we use kvm, we should synchronize the register before we get dump
742 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
743 cpu_synchronize_state(env
);
747 ret
= cpu_get_dump_info(&s
->dump_info
);
749 error_set(errp
, QERR_UNSUPPORTED
);
753 /* get memory mapping */
754 memory_mapping_list_init(&s
->list
);
756 qemu_get_guest_memory_mapping(&s
->list
);
758 qemu_get_guest_simple_memory_mapping(&s
->list
);
762 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
768 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
770 s
->phdr_num
= 1; /* PT_NOTE */
771 if (s
->list
.num
< UINT16_MAX
- 2) {
772 s
->phdr_num
+= s
->list
.num
;
773 s
->have_section
= false;
775 s
->have_section
= true;
776 s
->phdr_num
= PN_XNUM
;
777 s
->sh_info
= 1; /* PT_NOTE */
779 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
780 if (s
->list
.num
<= UINT32_MAX
- 1) {
781 s
->sh_info
+= s
->list
.num
;
783 s
->sh_info
= UINT32_MAX
;
787 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
788 s
->dump_info
.d_machine
, nr_cpus
);
789 if (s
->dump_info
.d_class
== ELFCLASS64
) {
790 if (s
->have_section
) {
791 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
792 sizeof(Elf64_Phdr
) * s
->sh_info
+
793 sizeof(Elf64_Shdr
) + s
->note_size
;
795 s
->memory_offset
= sizeof(Elf64_Ehdr
) +
796 sizeof(Elf64_Phdr
) * s
->phdr_num
+ s
->note_size
;
799 if (s
->have_section
) {
800 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
801 sizeof(Elf32_Phdr
) * s
->sh_info
+
802 sizeof(Elf32_Shdr
) + s
->note_size
;
804 s
->memory_offset
= sizeof(Elf32_Ehdr
) +
805 sizeof(Elf32_Phdr
) * s
->phdr_num
+ s
->note_size
;
819 void qmp_dump_guest_memory(bool paging
, const char *file
, bool has_begin
,
820 int64_t begin
, bool has_length
, int64_t length
,
828 if (has_begin
&& !has_length
) {
829 error_set(errp
, QERR_MISSING_PARAMETER
, "length");
832 if (!has_begin
&& has_length
) {
833 error_set(errp
, QERR_MISSING_PARAMETER
, "begin");
838 if (strstart(file
, "fd:", &p
)) {
839 fd
= monitor_get_fd(cur_mon
, p
);
841 error_set(errp
, QERR_FD_NOT_FOUND
, p
);
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
);
876 /* we need this function in hmp.c */
877 void qmp_dump_guest_memory(bool paging
, const char *file
, bool has_begin
,
878 int64_t begin
, bool has_length
, int64_t length
,
881 error_set(errp
, QERR_UNSUPPORTED
);