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"
23 #include "memory_mapping.h"
25 #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
;
69 target_phys_addr_t memory_offset
;
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
;
106 /* The fd may be passed from user, and it can be non-blocked */
108 writen_size
= qemu_write_full(fd
, buf
, size
);
109 if (writen_size
!= size
&& errno
!= EAGAIN
) {
120 static int write_elf64_header(DumpState
*s
)
122 Elf64_Ehdr elf_header
;
124 int endian
= s
->dump_info
.d_endian
;
126 memset(&elf_header
, 0, sizeof(Elf64_Ehdr
));
127 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
128 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS64
;
129 elf_header
.e_ident
[EI_DATA
] = s
->dump_info
.d_endian
;
130 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
131 elf_header
.e_type
= cpu_convert_to_target16(ET_CORE
, endian
);
132 elf_header
.e_machine
= cpu_convert_to_target16(s
->dump_info
.d_machine
,
134 elf_header
.e_version
= cpu_convert_to_target32(EV_CURRENT
, endian
);
135 elf_header
.e_ehsize
= cpu_convert_to_target16(sizeof(elf_header
), endian
);
136 elf_header
.e_phoff
= cpu_convert_to_target64(sizeof(Elf64_Ehdr
), endian
);
137 elf_header
.e_phentsize
= cpu_convert_to_target16(sizeof(Elf64_Phdr
),
139 elf_header
.e_phnum
= cpu_convert_to_target16(s
->phdr_num
, endian
);
140 if (s
->have_section
) {
141 uint64_t shoff
= sizeof(Elf64_Ehdr
) + sizeof(Elf64_Phdr
) * s
->sh_info
;
143 elf_header
.e_shoff
= cpu_convert_to_target64(shoff
, endian
);
144 elf_header
.e_shentsize
= cpu_convert_to_target16(sizeof(Elf64_Shdr
),
146 elf_header
.e_shnum
= cpu_convert_to_target16(1, endian
);
149 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
151 dump_error(s
, "dump: failed to write elf header.\n");
158 static int write_elf32_header(DumpState
*s
)
160 Elf32_Ehdr elf_header
;
162 int endian
= s
->dump_info
.d_endian
;
164 memset(&elf_header
, 0, sizeof(Elf32_Ehdr
));
165 memcpy(&elf_header
, ELFMAG
, SELFMAG
);
166 elf_header
.e_ident
[EI_CLASS
] = ELFCLASS32
;
167 elf_header
.e_ident
[EI_DATA
] = endian
;
168 elf_header
.e_ident
[EI_VERSION
] = EV_CURRENT
;
169 elf_header
.e_type
= cpu_convert_to_target16(ET_CORE
, endian
);
170 elf_header
.e_machine
= cpu_convert_to_target16(s
->dump_info
.d_machine
,
172 elf_header
.e_version
= cpu_convert_to_target32(EV_CURRENT
, endian
);
173 elf_header
.e_ehsize
= cpu_convert_to_target16(sizeof(elf_header
), endian
);
174 elf_header
.e_phoff
= cpu_convert_to_target32(sizeof(Elf32_Ehdr
), endian
);
175 elf_header
.e_phentsize
= cpu_convert_to_target16(sizeof(Elf32_Phdr
),
177 elf_header
.e_phnum
= cpu_convert_to_target16(s
->phdr_num
, endian
);
178 if (s
->have_section
) {
179 uint32_t shoff
= sizeof(Elf32_Ehdr
) + sizeof(Elf32_Phdr
) * s
->sh_info
;
181 elf_header
.e_shoff
= cpu_convert_to_target32(shoff
, endian
);
182 elf_header
.e_shentsize
= cpu_convert_to_target16(sizeof(Elf32_Shdr
),
184 elf_header
.e_shnum
= cpu_convert_to_target16(1, endian
);
187 ret
= fd_write_vmcore(&elf_header
, sizeof(elf_header
), s
);
189 dump_error(s
, "dump: failed to write elf header.\n");
196 static int write_elf64_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
197 int phdr_index
, target_phys_addr_t offset
)
201 int endian
= s
->dump_info
.d_endian
;
203 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
204 phdr
.p_type
= cpu_convert_to_target32(PT_LOAD
, endian
);
205 phdr
.p_offset
= cpu_convert_to_target64(offset
, endian
);
206 phdr
.p_paddr
= cpu_convert_to_target64(memory_mapping
->phys_addr
, endian
);
208 /* When the memory is not stored into vmcore, offset will be -1 */
211 phdr
.p_filesz
= cpu_convert_to_target64(memory_mapping
->length
, endian
);
213 phdr
.p_memsz
= cpu_convert_to_target64(memory_mapping
->length
, endian
);
214 phdr
.p_vaddr
= cpu_convert_to_target64(memory_mapping
->virt_addr
, endian
);
216 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
218 dump_error(s
, "dump: failed to write program header table.\n");
225 static int write_elf32_load(DumpState
*s
, MemoryMapping
*memory_mapping
,
226 int phdr_index
, target_phys_addr_t offset
)
230 int endian
= s
->dump_info
.d_endian
;
232 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
233 phdr
.p_type
= cpu_convert_to_target32(PT_LOAD
, endian
);
234 phdr
.p_offset
= cpu_convert_to_target32(offset
, endian
);
235 phdr
.p_paddr
= cpu_convert_to_target32(memory_mapping
->phys_addr
, endian
);
237 /* When the memory is not stored into vmcore, offset will be -1 */
240 phdr
.p_filesz
= cpu_convert_to_target32(memory_mapping
->length
, endian
);
242 phdr
.p_memsz
= cpu_convert_to_target32(memory_mapping
->length
, endian
);
243 phdr
.p_vaddr
= cpu_convert_to_target32(memory_mapping
->virt_addr
, endian
);
245 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
247 dump_error(s
, "dump: failed to write program header table.\n");
254 static int write_elf64_note(DumpState
*s
)
257 int endian
= s
->dump_info
.d_endian
;
258 target_phys_addr_t begin
= s
->memory_offset
- s
->note_size
;
261 memset(&phdr
, 0, sizeof(Elf64_Phdr
));
262 phdr
.p_type
= cpu_convert_to_target32(PT_NOTE
, endian
);
263 phdr
.p_offset
= cpu_convert_to_target64(begin
, endian
);
265 phdr
.p_filesz
= cpu_convert_to_target64(s
->note_size
, endian
);
266 phdr
.p_memsz
= cpu_convert_to_target64(s
->note_size
, endian
);
269 ret
= fd_write_vmcore(&phdr
, sizeof(Elf64_Phdr
), s
);
271 dump_error(s
, "dump: failed to write program header table.\n");
278 static int write_elf64_notes(DumpState
*s
)
284 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
286 ret
= cpu_write_elf64_note(fd_write_vmcore
, env
, id
, s
);
288 dump_error(s
, "dump: failed to write elf notes.\n");
293 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
294 ret
= cpu_write_elf64_qemunote(fd_write_vmcore
, env
, s
);
296 dump_error(s
, "dump: failed to write CPU status.\n");
304 static int write_elf32_note(DumpState
*s
)
306 target_phys_addr_t begin
= s
->memory_offset
- s
->note_size
;
308 int endian
= s
->dump_info
.d_endian
;
311 memset(&phdr
, 0, sizeof(Elf32_Phdr
));
312 phdr
.p_type
= cpu_convert_to_target32(PT_NOTE
, endian
);
313 phdr
.p_offset
= cpu_convert_to_target32(begin
, endian
);
315 phdr
.p_filesz
= cpu_convert_to_target32(s
->note_size
, endian
);
316 phdr
.p_memsz
= cpu_convert_to_target32(s
->note_size
, endian
);
319 ret
= fd_write_vmcore(&phdr
, sizeof(Elf32_Phdr
), s
);
321 dump_error(s
, "dump: failed to write program header table.\n");
328 static int write_elf32_notes(DumpState
*s
)
334 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
336 ret
= cpu_write_elf32_note(fd_write_vmcore
, env
, id
, s
);
338 dump_error(s
, "dump: failed to write elf notes.\n");
343 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
344 ret
= cpu_write_elf32_qemunote(fd_write_vmcore
, env
, s
);
346 dump_error(s
, "dump: failed to write CPU status.\n");
354 static int write_elf_section(DumpState
*s
, int type
)
358 int endian
= s
->dump_info
.d_endian
;
364 shdr_size
= sizeof(Elf32_Shdr
);
365 memset(&shdr32
, 0, shdr_size
);
366 shdr32
.sh_info
= cpu_convert_to_target32(s
->sh_info
, endian
);
369 shdr_size
= sizeof(Elf64_Shdr
);
370 memset(&shdr64
, 0, shdr_size
);
371 shdr64
.sh_info
= cpu_convert_to_target32(s
->sh_info
, endian
);
375 ret
= fd_write_vmcore(&shdr
, shdr_size
, s
);
377 dump_error(s
, "dump: failed to write section header table.\n");
384 static int write_data(DumpState
*s
, void *buf
, int length
)
388 ret
= fd_write_vmcore(buf
, length
, s
);
390 dump_error(s
, "dump: failed to save memory.\n");
397 /* write the memroy to vmcore. 1 page per I/O. */
398 static int write_memory(DumpState
*s
, RAMBlock
*block
, ram_addr_t start
,
404 for (i
= 0; i
< size
/ TARGET_PAGE_SIZE
; i
++) {
405 ret
= write_data(s
, block
->host
+ start
+ i
* TARGET_PAGE_SIZE
,
412 if ((size
% TARGET_PAGE_SIZE
) != 0) {
413 ret
= write_data(s
, block
->host
+ start
+ i
* TARGET_PAGE_SIZE
,
414 size
% TARGET_PAGE_SIZE
);
423 /* get the memory's offset in the vmcore */
424 static target_phys_addr_t
get_offset(target_phys_addr_t phys_addr
,
428 target_phys_addr_t offset
= s
->memory_offset
;
429 int64_t size_in_block
, start
;
432 if (phys_addr
< s
->begin
|| phys_addr
>= s
->begin
+ s
->length
) {
437 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
439 if (block
->offset
>= s
->begin
+ s
->length
||
440 block
->offset
+ block
->length
<= s
->begin
) {
441 /* This block is out of the range */
445 if (s
->begin
<= block
->offset
) {
446 start
= block
->offset
;
451 size_in_block
= block
->length
- (start
- block
->offset
);
452 if (s
->begin
+ s
->length
< block
->offset
+ block
->length
) {
453 size_in_block
-= block
->offset
+ block
->length
-
454 (s
->begin
+ s
->length
);
457 start
= block
->offset
;
458 size_in_block
= block
->length
;
461 if (phys_addr
>= start
&& phys_addr
< start
+ size_in_block
) {
462 return phys_addr
- start
+ offset
;
465 offset
+= size_in_block
;
471 static int write_elf_loads(DumpState
*s
)
473 target_phys_addr_t offset
;
474 MemoryMapping
*memory_mapping
;
475 uint32_t phdr_index
= 1;
479 if (s
->have_section
) {
480 max_index
= s
->sh_info
;
482 max_index
= s
->phdr_num
;
485 QTAILQ_FOREACH(memory_mapping
, &s
->list
.head
, next
) {
486 offset
= get_offset(memory_mapping
->phys_addr
, s
);
487 if (s
->dump_info
.d_class
== ELFCLASS64
) {
488 ret
= write_elf64_load(s
, memory_mapping
, phdr_index
++, offset
);
490 ret
= write_elf32_load(s
, memory_mapping
, phdr_index
++, offset
);
497 if (phdr_index
>= max_index
) {
505 /* write elf header, PT_NOTE and elf note to vmcore. */
506 static int dump_begin(DumpState
*s
)
511 * the vmcore's format is:
530 * we only know where the memory is saved after we write elf note into
534 /* write elf header to vmcore */
535 if (s
->dump_info
.d_class
== ELFCLASS64
) {
536 ret
= write_elf64_header(s
);
538 ret
= write_elf32_header(s
);
544 if (s
->dump_info
.d_class
== ELFCLASS64
) {
545 /* write PT_NOTE to vmcore */
546 if (write_elf64_note(s
) < 0) {
550 /* write all PT_LOAD to vmcore */
551 if (write_elf_loads(s
) < 0) {
555 /* write section to vmcore */
556 if (s
->have_section
) {
557 if (write_elf_section(s
, 1) < 0) {
562 /* write notes to vmcore */
563 if (write_elf64_notes(s
) < 0) {
568 /* write PT_NOTE to vmcore */
569 if (write_elf32_note(s
) < 0) {
573 /* write all PT_LOAD to vmcore */
574 if (write_elf_loads(s
) < 0) {
578 /* write section to vmcore */
579 if (s
->have_section
) {
580 if (write_elf_section(s
, 0) < 0) {
585 /* write notes to vmcore */
586 if (write_elf32_notes(s
) < 0) {
594 /* write PT_LOAD to vmcore */
595 static int dump_completed(DumpState
*s
)
601 static int get_next_block(DumpState
*s
, RAMBlock
*block
)
604 block
= QLIST_NEXT(block
, next
);
613 if (block
->offset
>= s
->begin
+ s
->length
||
614 block
->offset
+ block
->length
<= s
->begin
) {
615 /* This block is out of the range */
619 if (s
->begin
> block
->offset
) {
620 s
->start
= s
->begin
- block
->offset
;
628 /* write all memory to vmcore */
629 static int dump_iterate(DumpState
*s
)
638 size
= block
->length
;
641 if (s
->begin
+ s
->length
< block
->offset
+ block
->length
) {
642 size
-= block
->offset
+ block
->length
- (s
->begin
+ s
->length
);
645 ret
= write_memory(s
, block
, s
->start
, size
);
650 ret
= get_next_block(s
, block
);
658 static int create_vmcore(DumpState
*s
)
667 ret
= dump_iterate(s
);
675 static ram_addr_t
get_start_block(DumpState
*s
)
679 if (!s
->has_filter
) {
680 s
->block
= QLIST_FIRST(&ram_list
.blocks
);
684 QLIST_FOREACH(block
, &ram_list
.blocks
, next
) {
685 if (block
->offset
>= s
->begin
+ s
->length
||
686 block
->offset
+ block
->length
<= s
->begin
) {
687 /* This block is out of the range */
692 if (s
->begin
> block
->offset
) {
693 s
->start
= s
->begin
- block
->offset
;
703 static int dump_init(DumpState
*s
, int fd
, bool paging
, bool has_filter
,
704 int64_t begin
, int64_t length
, Error
**errp
)
710 if (runstate_is_running()) {
711 vm_stop(RUN_STATE_SAVE_VM
);
719 s
->has_filter
= has_filter
;
722 s
->start
= get_start_block(s
);
723 if (s
->start
== -1) {
724 error_set(errp
, QERR_INVALID_PARAMETER
, "begin");
729 * get dump info: endian, class and architecture.
730 * If the target architecture is not supported, cpu_get_dump_info() will
733 * if we use kvm, we should synchronize the register before we get dump
737 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
738 cpu_synchronize_state(env
);
742 ret
= cpu_get_dump_info(&s
->dump_info
);
744 error_set(errp
, QERR_UNSUPPORTED
);
748 s
->note_size
= cpu_get_note_size(s
->dump_info
.d_class
,
749 s
->dump_info
.d_machine
, nr_cpus
);
751 error_set(errp
, QERR_UNSUPPORTED
);
755 /* get memory mapping */
756 memory_mapping_list_init(&s
->list
);
758 qemu_get_guest_memory_mapping(&s
->list
);
760 qemu_get_guest_simple_memory_mapping(&s
->list
);
764 memory_mapping_filter(&s
->list
, s
->begin
, s
->length
);
770 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
772 s
->phdr_num
= 1; /* PT_NOTE */
773 if (s
->list
.num
< UINT16_MAX
- 2) {
774 s
->phdr_num
+= s
->list
.num
;
775 s
->have_section
= false;
777 s
->have_section
= true;
778 s
->phdr_num
= PN_XNUM
;
779 s
->sh_info
= 1; /* PT_NOTE */
781 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
782 if (s
->list
.num
<= UINT32_MAX
- 1) {
783 s
->sh_info
+= s
->list
.num
;
785 s
->sh_info
= UINT32_MAX
;
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
);