cpu: Move CPU_COMMON_THREAD into CPUState
[qemu/agraf.git] / dump.c
blob2bf8d8d99423a364e6916c766733f97828afb0d5
1 /*
2 * QEMU dump
4 * Copyright Fujitsu, Corp. 2011, 2012
6 * Authors:
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"
15 #include "elf.h"
16 #include "cpu.h"
17 #include "cpu-all.h"
18 #include "targphys.h"
19 #include "monitor.h"
20 #include "kvm.h"
21 #include "dump.h"
22 #include "sysemu.h"
23 #include "memory_mapping.h"
24 #include "error.h"
25 #include "qmp-commands.h"
26 #include "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);
32 } else {
33 val = cpu_to_be16(val);
36 return val;
39 static uint32_t cpu_convert_to_target32(uint32_t val, int endian)
41 if (endian == ELFDATA2LSB) {
42 val = cpu_to_le32(val);
43 } else {
44 val = cpu_to_be32(val);
47 return val;
50 static uint64_t cpu_convert_to_target64(uint64_t val, int endian)
52 if (endian == ELFDATA2LSB) {
53 val = cpu_to_le64(val);
54 } else {
55 val = cpu_to_be64(val);
58 return val;
61 typedef struct DumpState {
62 ArchDumpInfo dump_info;
63 MemoryMappingList list;
64 uint16_t phdr_num;
65 uint32_t sh_info;
66 bool have_section;
67 bool resume;
68 size_t note_size;
69 target_phys_addr_t memory_offset;
70 int fd;
72 RAMBlock *block;
73 ram_addr_t start;
74 bool has_filter;
75 int64_t begin;
76 int64_t length;
77 Error **errp;
78 } DumpState;
80 static int dump_cleanup(DumpState *s)
82 int ret = 0;
84 memory_mapping_list_free(&s->list);
85 if (s->fd != -1) {
86 close(s->fd);
88 if (s->resume) {
89 vm_start();
92 return ret;
95 static void dump_error(DumpState *s, const char *reason)
97 dump_cleanup(s);
100 static int fd_write_vmcore(void *buf, size_t size, void *opaque)
102 DumpState *s = opaque;
103 int fd = s->fd;
104 size_t writen_size;
106 /* The fd may be passed from user, and it can be non-blocked */
107 while (size) {
108 writen_size = qemu_write_full(fd, buf, size);
109 if (writen_size != size && errno != EAGAIN) {
110 return -1;
113 buf += writen_size;
114 size -= writen_size;
117 return 0;
120 static int write_elf64_header(DumpState *s)
122 Elf64_Ehdr elf_header;
123 int ret;
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,
133 endian);
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),
138 endian);
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),
145 endian);
146 elf_header.e_shnum = cpu_convert_to_target16(1, endian);
149 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
150 if (ret < 0) {
151 dump_error(s, "dump: failed to write elf header.\n");
152 return -1;
155 return 0;
158 static int write_elf32_header(DumpState *s)
160 Elf32_Ehdr elf_header;
161 int ret;
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,
171 endian);
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),
176 endian);
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),
183 endian);
184 elf_header.e_shnum = cpu_convert_to_target16(1, endian);
187 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
188 if (ret < 0) {
189 dump_error(s, "dump: failed to write elf header.\n");
190 return -1;
193 return 0;
196 static int write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
197 int phdr_index, target_phys_addr_t offset)
199 Elf64_Phdr phdr;
200 int ret;
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);
207 if (offset == -1) {
208 /* When the memory is not stored into vmcore, offset will be -1 */
209 phdr.p_filesz = 0;
210 } else {
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);
217 if (ret < 0) {
218 dump_error(s, "dump: failed to write program header table.\n");
219 return -1;
222 return 0;
225 static int write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
226 int phdr_index, target_phys_addr_t offset)
228 Elf32_Phdr phdr;
229 int ret;
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);
236 if (offset == -1) {
237 /* When the memory is not stored into vmcore, offset will be -1 */
238 phdr.p_filesz = 0;
239 } else {
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);
246 if (ret < 0) {
247 dump_error(s, "dump: failed to write program header table.\n");
248 return -1;
251 return 0;
254 static int write_elf64_note(DumpState *s)
256 Elf64_Phdr phdr;
257 int endian = s->dump_info.d_endian;
258 target_phys_addr_t begin = s->memory_offset - s->note_size;
259 int ret;
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);
264 phdr.p_paddr = 0;
265 phdr.p_filesz = cpu_convert_to_target64(s->note_size, endian);
266 phdr.p_memsz = cpu_convert_to_target64(s->note_size, endian);
267 phdr.p_vaddr = 0;
269 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
270 if (ret < 0) {
271 dump_error(s, "dump: failed to write program header table.\n");
272 return -1;
275 return 0;
278 static int write_elf64_notes(DumpState *s)
280 CPUArchState *env;
281 int ret;
282 int id;
284 for (env = first_cpu; env != NULL; env = env->next_cpu) {
285 id = cpu_index(env);
286 ret = cpu_write_elf64_note(fd_write_vmcore, env, id, s);
287 if (ret < 0) {
288 dump_error(s, "dump: failed to write elf notes.\n");
289 return -1;
293 for (env = first_cpu; env != NULL; env = env->next_cpu) {
294 ret = cpu_write_elf64_qemunote(fd_write_vmcore, env, s);
295 if (ret < 0) {
296 dump_error(s, "dump: failed to write CPU status.\n");
297 return -1;
301 return 0;
304 static int write_elf32_note(DumpState *s)
306 target_phys_addr_t begin = s->memory_offset - s->note_size;
307 Elf32_Phdr phdr;
308 int endian = s->dump_info.d_endian;
309 int ret;
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);
314 phdr.p_paddr = 0;
315 phdr.p_filesz = cpu_convert_to_target32(s->note_size, endian);
316 phdr.p_memsz = cpu_convert_to_target32(s->note_size, endian);
317 phdr.p_vaddr = 0;
319 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
320 if (ret < 0) {
321 dump_error(s, "dump: failed to write program header table.\n");
322 return -1;
325 return 0;
328 static int write_elf32_notes(DumpState *s)
330 CPUArchState *env;
331 int ret;
332 int id;
334 for (env = first_cpu; env != NULL; env = env->next_cpu) {
335 id = cpu_index(env);
336 ret = cpu_write_elf32_note(fd_write_vmcore, env, id, s);
337 if (ret < 0) {
338 dump_error(s, "dump: failed to write elf notes.\n");
339 return -1;
343 for (env = first_cpu; env != NULL; env = env->next_cpu) {
344 ret = cpu_write_elf32_qemunote(fd_write_vmcore, env, s);
345 if (ret < 0) {
346 dump_error(s, "dump: failed to write CPU status.\n");
347 return -1;
351 return 0;
354 static int write_elf_section(DumpState *s, int type)
356 Elf32_Shdr shdr32;
357 Elf64_Shdr shdr64;
358 int endian = s->dump_info.d_endian;
359 int shdr_size;
360 void *shdr;
361 int ret;
363 if (type == 0) {
364 shdr_size = sizeof(Elf32_Shdr);
365 memset(&shdr32, 0, shdr_size);
366 shdr32.sh_info = cpu_convert_to_target32(s->sh_info, endian);
367 shdr = &shdr32;
368 } else {
369 shdr_size = sizeof(Elf64_Shdr);
370 memset(&shdr64, 0, shdr_size);
371 shdr64.sh_info = cpu_convert_to_target32(s->sh_info, endian);
372 shdr = &shdr64;
375 ret = fd_write_vmcore(&shdr, shdr_size, s);
376 if (ret < 0) {
377 dump_error(s, "dump: failed to write section header table.\n");
378 return -1;
381 return 0;
384 static int write_data(DumpState *s, void *buf, int length)
386 int ret;
388 ret = fd_write_vmcore(buf, length, s);
389 if (ret < 0) {
390 dump_error(s, "dump: failed to save memory.\n");
391 return -1;
394 return 0;
397 /* write the memroy to vmcore. 1 page per I/O. */
398 static int write_memory(DumpState *s, RAMBlock *block, ram_addr_t start,
399 int64_t size)
401 int64_t i;
402 int ret;
404 for (i = 0; i < size / TARGET_PAGE_SIZE; i++) {
405 ret = write_data(s, block->host + start + i * TARGET_PAGE_SIZE,
406 TARGET_PAGE_SIZE);
407 if (ret < 0) {
408 return ret;
412 if ((size % TARGET_PAGE_SIZE) != 0) {
413 ret = write_data(s, block->host + start + i * TARGET_PAGE_SIZE,
414 size % TARGET_PAGE_SIZE);
415 if (ret < 0) {
416 return ret;
420 return 0;
423 /* get the memory's offset in the vmcore */
424 static target_phys_addr_t get_offset(target_phys_addr_t phys_addr,
425 DumpState *s)
427 RAMBlock *block;
428 target_phys_addr_t offset = s->memory_offset;
429 int64_t size_in_block, start;
431 if (s->has_filter) {
432 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
433 return -1;
437 QLIST_FOREACH(block, &ram_list.blocks, next) {
438 if (s->has_filter) {
439 if (block->offset >= s->begin + s->length ||
440 block->offset + block->length <= s->begin) {
441 /* This block is out of the range */
442 continue;
445 if (s->begin <= block->offset) {
446 start = block->offset;
447 } else {
448 start = s->begin;
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);
456 } else {
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;
468 return -1;
471 static int write_elf_loads(DumpState *s)
473 target_phys_addr_t offset;
474 MemoryMapping *memory_mapping;
475 uint32_t phdr_index = 1;
476 int ret;
477 uint32_t max_index;
479 if (s->have_section) {
480 max_index = s->sh_info;
481 } else {
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);
489 } else {
490 ret = write_elf32_load(s, memory_mapping, phdr_index++, offset);
493 if (ret < 0) {
494 return -1;
497 if (phdr_index >= max_index) {
498 break;
502 return 0;
505 /* write elf header, PT_NOTE and elf note to vmcore. */
506 static int dump_begin(DumpState *s)
508 int ret;
511 * the vmcore's format is:
512 * --------------
513 * | elf header |
514 * --------------
515 * | PT_NOTE |
516 * --------------
517 * | PT_LOAD |
518 * --------------
519 * | ...... |
520 * --------------
521 * | PT_LOAD |
522 * --------------
523 * | sec_hdr |
524 * --------------
525 * | elf note |
526 * --------------
527 * | memory |
528 * --------------
530 * we only know where the memory is saved after we write elf note into
531 * vmcore.
534 /* write elf header to vmcore */
535 if (s->dump_info.d_class == ELFCLASS64) {
536 ret = write_elf64_header(s);
537 } else {
538 ret = write_elf32_header(s);
540 if (ret < 0) {
541 return -1;
544 if (s->dump_info.d_class == ELFCLASS64) {
545 /* write PT_NOTE to vmcore */
546 if (write_elf64_note(s) < 0) {
547 return -1;
550 /* write all PT_LOAD to vmcore */
551 if (write_elf_loads(s) < 0) {
552 return -1;
555 /* write section to vmcore */
556 if (s->have_section) {
557 if (write_elf_section(s, 1) < 0) {
558 return -1;
562 /* write notes to vmcore */
563 if (write_elf64_notes(s) < 0) {
564 return -1;
567 } else {
568 /* write PT_NOTE to vmcore */
569 if (write_elf32_note(s) < 0) {
570 return -1;
573 /* write all PT_LOAD to vmcore */
574 if (write_elf_loads(s) < 0) {
575 return -1;
578 /* write section to vmcore */
579 if (s->have_section) {
580 if (write_elf_section(s, 0) < 0) {
581 return -1;
585 /* write notes to vmcore */
586 if (write_elf32_notes(s) < 0) {
587 return -1;
591 return 0;
594 /* write PT_LOAD to vmcore */
595 static int dump_completed(DumpState *s)
597 dump_cleanup(s);
598 return 0;
601 static int get_next_block(DumpState *s, RAMBlock *block)
603 while (1) {
604 block = QLIST_NEXT(block, next);
605 if (!block) {
606 /* no more block */
607 return 1;
610 s->start = 0;
611 s->block = block;
612 if (s->has_filter) {
613 if (block->offset >= s->begin + s->length ||
614 block->offset + block->length <= s->begin) {
615 /* This block is out of the range */
616 continue;
619 if (s->begin > block->offset) {
620 s->start = s->begin - block->offset;
624 return 0;
628 /* write all memory to vmcore */
629 static int dump_iterate(DumpState *s)
631 RAMBlock *block;
632 int64_t size;
633 int ret;
635 while (1) {
636 block = s->block;
638 size = block->length;
639 if (s->has_filter) {
640 size -= s->start;
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);
646 if (ret == -1) {
647 return ret;
650 ret = get_next_block(s, block);
651 if (ret == 1) {
652 dump_completed(s);
653 return 0;
658 static int create_vmcore(DumpState *s)
660 int ret;
662 ret = dump_begin(s);
663 if (ret < 0) {
664 return -1;
667 ret = dump_iterate(s);
668 if (ret < 0) {
669 return -1;
672 return 0;
675 static ram_addr_t get_start_block(DumpState *s)
677 RAMBlock *block;
679 if (!s->has_filter) {
680 s->block = QLIST_FIRST(&ram_list.blocks);
681 return 0;
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 */
688 continue;
691 s->block = block;
692 if (s->begin > block->offset) {
693 s->start = s->begin - block->offset;
694 } else {
695 s->start = 0;
697 return s->start;
700 return -1;
703 static int dump_init(DumpState *s, int fd, bool paging, bool has_filter,
704 int64_t begin, int64_t length, Error **errp)
706 CPUArchState *env;
707 int nr_cpus;
708 int ret;
710 if (runstate_is_running()) {
711 vm_stop(RUN_STATE_SAVE_VM);
712 s->resume = true;
713 } else {
714 s->resume = false;
717 s->errp = errp;
718 s->fd = fd;
719 s->has_filter = has_filter;
720 s->begin = begin;
721 s->length = length;
722 s->start = get_start_block(s);
723 if (s->start == -1) {
724 error_set(errp, QERR_INVALID_PARAMETER, "begin");
725 goto cleanup;
729 * get dump info: endian, class and architecture.
730 * If the target architecture is not supported, cpu_get_dump_info() will
731 * return -1.
733 * if we use kvm, we should synchronize the register before we get dump
734 * info.
736 nr_cpus = 0;
737 for (env = first_cpu; env != NULL; env = env->next_cpu) {
738 cpu_synchronize_state(env);
739 nr_cpus++;
742 ret = cpu_get_dump_info(&s->dump_info);
743 if (ret < 0) {
744 error_set(errp, QERR_UNSUPPORTED);
745 goto cleanup;
748 s->note_size = cpu_get_note_size(s->dump_info.d_class,
749 s->dump_info.d_machine, nr_cpus);
750 if (ret < 0) {
751 error_set(errp, QERR_UNSUPPORTED);
752 goto cleanup;
755 /* get memory mapping */
756 memory_mapping_list_init(&s->list);
757 if (paging) {
758 qemu_get_guest_memory_mapping(&s->list);
759 } else {
760 qemu_get_guest_simple_memory_mapping(&s->list);
763 if (s->has_filter) {
764 memory_mapping_filter(&s->list, s->begin, s->length);
768 * calculate phdr_num
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;
776 } else {
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;
784 } else {
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;
794 } else {
795 s->memory_offset = sizeof(Elf64_Ehdr) +
796 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
798 } else {
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;
803 } else {
804 s->memory_offset = sizeof(Elf32_Ehdr) +
805 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
809 return 0;
811 cleanup:
812 if (s->resume) {
813 vm_start();
816 return -1;
819 void qmp_dump_guest_memory(bool paging, const char *file, bool has_begin,
820 int64_t begin, bool has_length, int64_t length,
821 Error **errp)
823 const char *p;
824 int fd = -1;
825 DumpState *s;
826 int ret;
828 if (has_begin && !has_length) {
829 error_set(errp, QERR_MISSING_PARAMETER, "length");
830 return;
832 if (!has_begin && has_length) {
833 error_set(errp, QERR_MISSING_PARAMETER, "begin");
834 return;
837 #if !defined(WIN32)
838 if (strstart(file, "fd:", &p)) {
839 fd = monitor_get_fd(cur_mon, p);
840 if (fd == -1) {
841 error_set(errp, QERR_FD_NOT_FOUND, p);
842 return;
845 #endif
847 if (strstart(file, "file:", &p)) {
848 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
849 if (fd < 0) {
850 error_set(errp, QERR_OPEN_FILE_FAILED, p);
851 return;
855 if (fd == -1) {
856 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
857 return;
860 s = g_malloc(sizeof(DumpState));
862 ret = dump_init(s, fd, paging, has_begin, begin, length, errp);
863 if (ret < 0) {
864 g_free(s);
865 return;
868 if (create_vmcore(s) < 0 && !error_is_set(s->errp)) {
869 error_set(errp, QERR_IO_ERROR);
872 g_free(s);