nbd: Fix trace message for disconnect
[qemu/ar7.git] / dump.c
blobd9090a24cce6f2d4491f93124fec0f61b7d7704e
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/osdep.h"
15 #include "qemu/cutils.h"
16 #include "elf.h"
17 #include "cpu.h"
18 #include "exec/cpu-all.h"
19 #include "exec/hwaddr.h"
20 #include "monitor/monitor.h"
21 #include "sysemu/kvm.h"
22 #include "sysemu/dump.h"
23 #include "sysemu/sysemu.h"
24 #include "sysemu/memory_mapping.h"
25 #include "sysemu/cpus.h"
26 #include "qapi/qmp/qerror.h"
27 #include "qmp-commands.h"
28 #include "qapi-event.h"
30 #include <zlib.h>
31 #ifdef CONFIG_LZO
32 #include <lzo/lzo1x.h>
33 #endif
34 #ifdef CONFIG_SNAPPY
35 #include <snappy-c.h>
36 #endif
37 #ifndef ELF_MACHINE_UNAME
38 #define ELF_MACHINE_UNAME "Unknown"
39 #endif
41 uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
43 if (s->dump_info.d_endian == ELFDATA2LSB) {
44 val = cpu_to_le16(val);
45 } else {
46 val = cpu_to_be16(val);
49 return val;
52 uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
54 if (s->dump_info.d_endian == ELFDATA2LSB) {
55 val = cpu_to_le32(val);
56 } else {
57 val = cpu_to_be32(val);
60 return val;
63 uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
65 if (s->dump_info.d_endian == ELFDATA2LSB) {
66 val = cpu_to_le64(val);
67 } else {
68 val = cpu_to_be64(val);
71 return val;
74 static int dump_cleanup(DumpState *s)
76 guest_phys_blocks_free(&s->guest_phys_blocks);
77 memory_mapping_list_free(&s->list);
78 close(s->fd);
79 if (s->resume) {
80 if (s->detached) {
81 qemu_mutex_lock_iothread();
83 vm_start();
84 if (s->detached) {
85 qemu_mutex_unlock_iothread();
89 return 0;
92 static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
94 DumpState *s = opaque;
95 size_t written_size;
97 written_size = qemu_write_full(s->fd, buf, size);
98 if (written_size != size) {
99 return -1;
102 return 0;
105 static void write_elf64_header(DumpState *s, Error **errp)
107 Elf64_Ehdr elf_header;
108 int ret;
110 memset(&elf_header, 0, sizeof(Elf64_Ehdr));
111 memcpy(&elf_header, ELFMAG, SELFMAG);
112 elf_header.e_ident[EI_CLASS] = ELFCLASS64;
113 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
114 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
115 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
116 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
117 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
118 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
119 elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
120 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
121 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
122 if (s->have_section) {
123 uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
125 elf_header.e_shoff = cpu_to_dump64(s, shoff);
126 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
127 elf_header.e_shnum = cpu_to_dump16(s, 1);
130 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
131 if (ret < 0) {
132 error_setg(errp, "dump: failed to write elf header");
136 static void write_elf32_header(DumpState *s, Error **errp)
138 Elf32_Ehdr elf_header;
139 int ret;
141 memset(&elf_header, 0, sizeof(Elf32_Ehdr));
142 memcpy(&elf_header, ELFMAG, SELFMAG);
143 elf_header.e_ident[EI_CLASS] = ELFCLASS32;
144 elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
145 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
146 elf_header.e_type = cpu_to_dump16(s, ET_CORE);
147 elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
148 elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
149 elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
150 elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
151 elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
152 elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
153 if (s->have_section) {
154 uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
156 elf_header.e_shoff = cpu_to_dump32(s, shoff);
157 elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
158 elf_header.e_shnum = cpu_to_dump16(s, 1);
161 ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
162 if (ret < 0) {
163 error_setg(errp, "dump: failed to write elf header");
167 static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
168 int phdr_index, hwaddr offset,
169 hwaddr filesz, Error **errp)
171 Elf64_Phdr phdr;
172 int ret;
174 memset(&phdr, 0, sizeof(Elf64_Phdr));
175 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
176 phdr.p_offset = cpu_to_dump64(s, offset);
177 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
178 phdr.p_filesz = cpu_to_dump64(s, filesz);
179 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
180 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr);
182 assert(memory_mapping->length >= filesz);
184 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
185 if (ret < 0) {
186 error_setg(errp, "dump: failed to write program header table");
190 static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
191 int phdr_index, hwaddr offset,
192 hwaddr filesz, Error **errp)
194 Elf32_Phdr phdr;
195 int ret;
197 memset(&phdr, 0, sizeof(Elf32_Phdr));
198 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
199 phdr.p_offset = cpu_to_dump32(s, offset);
200 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
201 phdr.p_filesz = cpu_to_dump32(s, filesz);
202 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
203 phdr.p_vaddr = cpu_to_dump32(s, memory_mapping->virt_addr);
205 assert(memory_mapping->length >= filesz);
207 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
208 if (ret < 0) {
209 error_setg(errp, "dump: failed to write program header table");
213 static void write_elf64_note(DumpState *s, Error **errp)
215 Elf64_Phdr phdr;
216 hwaddr begin = s->memory_offset - s->note_size;
217 int ret;
219 memset(&phdr, 0, sizeof(Elf64_Phdr));
220 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
221 phdr.p_offset = cpu_to_dump64(s, begin);
222 phdr.p_paddr = 0;
223 phdr.p_filesz = cpu_to_dump64(s, s->note_size);
224 phdr.p_memsz = cpu_to_dump64(s, s->note_size);
225 phdr.p_vaddr = 0;
227 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
228 if (ret < 0) {
229 error_setg(errp, "dump: failed to write program header table");
233 static inline int cpu_index(CPUState *cpu)
235 return cpu->cpu_index + 1;
238 static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
239 Error **errp)
241 CPUState *cpu;
242 int ret;
243 int id;
245 CPU_FOREACH(cpu) {
246 id = cpu_index(cpu);
247 ret = cpu_write_elf64_note(f, cpu, id, s);
248 if (ret < 0) {
249 error_setg(errp, "dump: failed to write elf notes");
250 return;
254 CPU_FOREACH(cpu) {
255 ret = cpu_write_elf64_qemunote(f, cpu, s);
256 if (ret < 0) {
257 error_setg(errp, "dump: failed to write CPU status");
258 return;
263 static void write_elf32_note(DumpState *s, Error **errp)
265 hwaddr begin = s->memory_offset - s->note_size;
266 Elf32_Phdr phdr;
267 int ret;
269 memset(&phdr, 0, sizeof(Elf32_Phdr));
270 phdr.p_type = cpu_to_dump32(s, PT_NOTE);
271 phdr.p_offset = cpu_to_dump32(s, begin);
272 phdr.p_paddr = 0;
273 phdr.p_filesz = cpu_to_dump32(s, s->note_size);
274 phdr.p_memsz = cpu_to_dump32(s, s->note_size);
275 phdr.p_vaddr = 0;
277 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
278 if (ret < 0) {
279 error_setg(errp, "dump: failed to write program header table");
283 static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
284 Error **errp)
286 CPUState *cpu;
287 int ret;
288 int id;
290 CPU_FOREACH(cpu) {
291 id = cpu_index(cpu);
292 ret = cpu_write_elf32_note(f, cpu, id, s);
293 if (ret < 0) {
294 error_setg(errp, "dump: failed to write elf notes");
295 return;
299 CPU_FOREACH(cpu) {
300 ret = cpu_write_elf32_qemunote(f, cpu, s);
301 if (ret < 0) {
302 error_setg(errp, "dump: failed to write CPU status");
303 return;
308 static void write_elf_section(DumpState *s, int type, Error **errp)
310 Elf32_Shdr shdr32;
311 Elf64_Shdr shdr64;
312 int shdr_size;
313 void *shdr;
314 int ret;
316 if (type == 0) {
317 shdr_size = sizeof(Elf32_Shdr);
318 memset(&shdr32, 0, shdr_size);
319 shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
320 shdr = &shdr32;
321 } else {
322 shdr_size = sizeof(Elf64_Shdr);
323 memset(&shdr64, 0, shdr_size);
324 shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
325 shdr = &shdr64;
328 ret = fd_write_vmcore(&shdr, shdr_size, s);
329 if (ret < 0) {
330 error_setg(errp, "dump: failed to write section header table");
334 static void write_data(DumpState *s, void *buf, int length, Error **errp)
336 int ret;
338 ret = fd_write_vmcore(buf, length, s);
339 if (ret < 0) {
340 error_setg(errp, "dump: failed to save memory");
341 } else {
342 s->written_size += length;
346 /* write the memory to vmcore. 1 page per I/O. */
347 static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
348 int64_t size, Error **errp)
350 int64_t i;
351 Error *local_err = NULL;
353 for (i = 0; i < size / s->dump_info.page_size; i++) {
354 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
355 s->dump_info.page_size, &local_err);
356 if (local_err) {
357 error_propagate(errp, local_err);
358 return;
362 if ((size % s->dump_info.page_size) != 0) {
363 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
364 size % s->dump_info.page_size, &local_err);
365 if (local_err) {
366 error_propagate(errp, local_err);
367 return;
372 /* get the memory's offset and size in the vmcore */
373 static void get_offset_range(hwaddr phys_addr,
374 ram_addr_t mapping_length,
375 DumpState *s,
376 hwaddr *p_offset,
377 hwaddr *p_filesz)
379 GuestPhysBlock *block;
380 hwaddr offset = s->memory_offset;
381 int64_t size_in_block, start;
383 /* When the memory is not stored into vmcore, offset will be -1 */
384 *p_offset = -1;
385 *p_filesz = 0;
387 if (s->has_filter) {
388 if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
389 return;
393 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
394 if (s->has_filter) {
395 if (block->target_start >= s->begin + s->length ||
396 block->target_end <= s->begin) {
397 /* This block is out of the range */
398 continue;
401 if (s->begin <= block->target_start) {
402 start = block->target_start;
403 } else {
404 start = s->begin;
407 size_in_block = block->target_end - start;
408 if (s->begin + s->length < block->target_end) {
409 size_in_block -= block->target_end - (s->begin + s->length);
411 } else {
412 start = block->target_start;
413 size_in_block = block->target_end - block->target_start;
416 if (phys_addr >= start && phys_addr < start + size_in_block) {
417 *p_offset = phys_addr - start + offset;
419 /* The offset range mapped from the vmcore file must not spill over
420 * the GuestPhysBlock, clamp it. The rest of the mapping will be
421 * zero-filled in memory at load time; see
422 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
424 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
425 mapping_length :
426 size_in_block - (phys_addr - start);
427 return;
430 offset += size_in_block;
434 static void write_elf_loads(DumpState *s, Error **errp)
436 hwaddr offset, filesz;
437 MemoryMapping *memory_mapping;
438 uint32_t phdr_index = 1;
439 uint32_t max_index;
440 Error *local_err = NULL;
442 if (s->have_section) {
443 max_index = s->sh_info;
444 } else {
445 max_index = s->phdr_num;
448 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
449 get_offset_range(memory_mapping->phys_addr,
450 memory_mapping->length,
451 s, &offset, &filesz);
452 if (s->dump_info.d_class == ELFCLASS64) {
453 write_elf64_load(s, memory_mapping, phdr_index++, offset,
454 filesz, &local_err);
455 } else {
456 write_elf32_load(s, memory_mapping, phdr_index++, offset,
457 filesz, &local_err);
460 if (local_err) {
461 error_propagate(errp, local_err);
462 return;
465 if (phdr_index >= max_index) {
466 break;
471 /* write elf header, PT_NOTE and elf note to vmcore. */
472 static void dump_begin(DumpState *s, Error **errp)
474 Error *local_err = NULL;
477 * the vmcore's format is:
478 * --------------
479 * | elf header |
480 * --------------
481 * | PT_NOTE |
482 * --------------
483 * | PT_LOAD |
484 * --------------
485 * | ...... |
486 * --------------
487 * | PT_LOAD |
488 * --------------
489 * | sec_hdr |
490 * --------------
491 * | elf note |
492 * --------------
493 * | memory |
494 * --------------
496 * we only know where the memory is saved after we write elf note into
497 * vmcore.
500 /* write elf header to vmcore */
501 if (s->dump_info.d_class == ELFCLASS64) {
502 write_elf64_header(s, &local_err);
503 } else {
504 write_elf32_header(s, &local_err);
506 if (local_err) {
507 error_propagate(errp, local_err);
508 return;
511 if (s->dump_info.d_class == ELFCLASS64) {
512 /* write PT_NOTE to vmcore */
513 write_elf64_note(s, &local_err);
514 if (local_err) {
515 error_propagate(errp, local_err);
516 return;
519 /* write all PT_LOAD to vmcore */
520 write_elf_loads(s, &local_err);
521 if (local_err) {
522 error_propagate(errp, local_err);
523 return;
526 /* write section to vmcore */
527 if (s->have_section) {
528 write_elf_section(s, 1, &local_err);
529 if (local_err) {
530 error_propagate(errp, local_err);
531 return;
535 /* write notes to vmcore */
536 write_elf64_notes(fd_write_vmcore, s, &local_err);
537 if (local_err) {
538 error_propagate(errp, local_err);
539 return;
541 } else {
542 /* write PT_NOTE to vmcore */
543 write_elf32_note(s, &local_err);
544 if (local_err) {
545 error_propagate(errp, local_err);
546 return;
549 /* write all PT_LOAD to vmcore */
550 write_elf_loads(s, &local_err);
551 if (local_err) {
552 error_propagate(errp, local_err);
553 return;
556 /* write section to vmcore */
557 if (s->have_section) {
558 write_elf_section(s, 0, &local_err);
559 if (local_err) {
560 error_propagate(errp, local_err);
561 return;
565 /* write notes to vmcore */
566 write_elf32_notes(fd_write_vmcore, s, &local_err);
567 if (local_err) {
568 error_propagate(errp, local_err);
569 return;
574 static int get_next_block(DumpState *s, GuestPhysBlock *block)
576 while (1) {
577 block = QTAILQ_NEXT(block, next);
578 if (!block) {
579 /* no more block */
580 return 1;
583 s->start = 0;
584 s->next_block = block;
585 if (s->has_filter) {
586 if (block->target_start >= s->begin + s->length ||
587 block->target_end <= s->begin) {
588 /* This block is out of the range */
589 continue;
592 if (s->begin > block->target_start) {
593 s->start = s->begin - block->target_start;
597 return 0;
601 /* write all memory to vmcore */
602 static void dump_iterate(DumpState *s, Error **errp)
604 GuestPhysBlock *block;
605 int64_t size;
606 Error *local_err = NULL;
608 do {
609 block = s->next_block;
611 size = block->target_end - block->target_start;
612 if (s->has_filter) {
613 size -= s->start;
614 if (s->begin + s->length < block->target_end) {
615 size -= block->target_end - (s->begin + s->length);
618 write_memory(s, block, s->start, size, &local_err);
619 if (local_err) {
620 error_propagate(errp, local_err);
621 return;
624 } while (!get_next_block(s, block));
627 static void create_vmcore(DumpState *s, Error **errp)
629 Error *local_err = NULL;
631 dump_begin(s, &local_err);
632 if (local_err) {
633 error_propagate(errp, local_err);
634 return;
637 dump_iterate(s, errp);
640 static int write_start_flat_header(int fd)
642 MakedumpfileHeader *mh;
643 int ret = 0;
645 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
646 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
648 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
649 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
651 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
652 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
654 size_t written_size;
655 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
656 if (written_size != MAX_SIZE_MDF_HEADER) {
657 ret = -1;
660 g_free(mh);
661 return ret;
664 static int write_end_flat_header(int fd)
666 MakedumpfileDataHeader mdh;
668 mdh.offset = END_FLAG_FLAT_HEADER;
669 mdh.buf_size = END_FLAG_FLAT_HEADER;
671 size_t written_size;
672 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
673 if (written_size != sizeof(mdh)) {
674 return -1;
677 return 0;
680 static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
682 size_t written_size;
683 MakedumpfileDataHeader mdh;
685 mdh.offset = cpu_to_be64(offset);
686 mdh.buf_size = cpu_to_be64(size);
688 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
689 if (written_size != sizeof(mdh)) {
690 return -1;
693 written_size = qemu_write_full(fd, buf, size);
694 if (written_size != size) {
695 return -1;
698 return 0;
701 static int buf_write_note(const void *buf, size_t size, void *opaque)
703 DumpState *s = opaque;
705 /* note_buf is not enough */
706 if (s->note_buf_offset + size > s->note_size) {
707 return -1;
710 memcpy(s->note_buf + s->note_buf_offset, buf, size);
712 s->note_buf_offset += size;
714 return 0;
717 /* write common header, sub header and elf note to vmcore */
718 static void create_header32(DumpState *s, Error **errp)
720 DiskDumpHeader32 *dh = NULL;
721 KdumpSubHeader32 *kh = NULL;
722 size_t size;
723 uint32_t block_size;
724 uint32_t sub_hdr_size;
725 uint32_t bitmap_blocks;
726 uint32_t status = 0;
727 uint64_t offset_note;
728 Error *local_err = NULL;
730 /* write common header, the version of kdump-compressed format is 6th */
731 size = sizeof(DiskDumpHeader32);
732 dh = g_malloc0(size);
734 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
735 dh->header_version = cpu_to_dump32(s, 6);
736 block_size = s->dump_info.page_size;
737 dh->block_size = cpu_to_dump32(s, block_size);
738 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
739 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
740 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
741 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
742 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
743 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
744 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
745 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
746 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
748 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
749 status |= DUMP_DH_COMPRESSED_ZLIB;
751 #ifdef CONFIG_LZO
752 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
753 status |= DUMP_DH_COMPRESSED_LZO;
755 #endif
756 #ifdef CONFIG_SNAPPY
757 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
758 status |= DUMP_DH_COMPRESSED_SNAPPY;
760 #endif
761 dh->status = cpu_to_dump32(s, status);
763 if (write_buffer(s->fd, 0, dh, size) < 0) {
764 error_setg(errp, "dump: failed to write disk dump header");
765 goto out;
768 /* write sub header */
769 size = sizeof(KdumpSubHeader32);
770 kh = g_malloc0(size);
772 /* 64bit max_mapnr_64 */
773 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
774 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
775 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
777 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
778 kh->offset_note = cpu_to_dump64(s, offset_note);
779 kh->note_size = cpu_to_dump32(s, s->note_size);
781 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
782 block_size, kh, size) < 0) {
783 error_setg(errp, "dump: failed to write kdump sub header");
784 goto out;
787 /* write note */
788 s->note_buf = g_malloc0(s->note_size);
789 s->note_buf_offset = 0;
791 /* use s->note_buf to store notes temporarily */
792 write_elf32_notes(buf_write_note, s, &local_err);
793 if (local_err) {
794 error_propagate(errp, local_err);
795 goto out;
797 if (write_buffer(s->fd, offset_note, s->note_buf,
798 s->note_size) < 0) {
799 error_setg(errp, "dump: failed to write notes");
800 goto out;
803 /* get offset of dump_bitmap */
804 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
805 block_size;
807 /* get offset of page */
808 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
809 block_size;
811 out:
812 g_free(dh);
813 g_free(kh);
814 g_free(s->note_buf);
817 /* write common header, sub header and elf note to vmcore */
818 static void create_header64(DumpState *s, Error **errp)
820 DiskDumpHeader64 *dh = NULL;
821 KdumpSubHeader64 *kh = NULL;
822 size_t size;
823 uint32_t block_size;
824 uint32_t sub_hdr_size;
825 uint32_t bitmap_blocks;
826 uint32_t status = 0;
827 uint64_t offset_note;
828 Error *local_err = NULL;
830 /* write common header, the version of kdump-compressed format is 6th */
831 size = sizeof(DiskDumpHeader64);
832 dh = g_malloc0(size);
834 strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
835 dh->header_version = cpu_to_dump32(s, 6);
836 block_size = s->dump_info.page_size;
837 dh->block_size = cpu_to_dump32(s, block_size);
838 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
839 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
840 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
841 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
842 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
843 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
844 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
845 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
846 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
848 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
849 status |= DUMP_DH_COMPRESSED_ZLIB;
851 #ifdef CONFIG_LZO
852 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
853 status |= DUMP_DH_COMPRESSED_LZO;
855 #endif
856 #ifdef CONFIG_SNAPPY
857 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
858 status |= DUMP_DH_COMPRESSED_SNAPPY;
860 #endif
861 dh->status = cpu_to_dump32(s, status);
863 if (write_buffer(s->fd, 0, dh, size) < 0) {
864 error_setg(errp, "dump: failed to write disk dump header");
865 goto out;
868 /* write sub header */
869 size = sizeof(KdumpSubHeader64);
870 kh = g_malloc0(size);
872 /* 64bit max_mapnr_64 */
873 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
874 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
875 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
877 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
878 kh->offset_note = cpu_to_dump64(s, offset_note);
879 kh->note_size = cpu_to_dump64(s, s->note_size);
881 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
882 block_size, kh, size) < 0) {
883 error_setg(errp, "dump: failed to write kdump sub header");
884 goto out;
887 /* write note */
888 s->note_buf = g_malloc0(s->note_size);
889 s->note_buf_offset = 0;
891 /* use s->note_buf to store notes temporarily */
892 write_elf64_notes(buf_write_note, s, &local_err);
893 if (local_err) {
894 error_propagate(errp, local_err);
895 goto out;
898 if (write_buffer(s->fd, offset_note, s->note_buf,
899 s->note_size) < 0) {
900 error_setg(errp, "dump: failed to write notes");
901 goto out;
904 /* get offset of dump_bitmap */
905 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
906 block_size;
908 /* get offset of page */
909 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
910 block_size;
912 out:
913 g_free(dh);
914 g_free(kh);
915 g_free(s->note_buf);
918 static void write_dump_header(DumpState *s, Error **errp)
920 Error *local_err = NULL;
922 if (s->dump_info.d_class == ELFCLASS32) {
923 create_header32(s, &local_err);
924 } else {
925 create_header64(s, &local_err);
927 error_propagate(errp, local_err);
930 static size_t dump_bitmap_get_bufsize(DumpState *s)
932 return s->dump_info.page_size;
936 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
937 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
938 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
939 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
940 * vmcore, ie. synchronizing un-sync bit into vmcore.
942 static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
943 uint8_t *buf, DumpState *s)
945 off_t old_offset, new_offset;
946 off_t offset_bitmap1, offset_bitmap2;
947 uint32_t byte, bit;
948 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
949 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
951 /* should not set the previous place */
952 assert(last_pfn <= pfn);
955 * if the bit needed to be set is not cached in buf, flush the data in buf
956 * to vmcore firstly.
957 * making new_offset be bigger than old_offset can also sync remained data
958 * into vmcore.
960 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
961 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
963 while (old_offset < new_offset) {
964 /* calculate the offset and write dump_bitmap */
965 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
966 if (write_buffer(s->fd, offset_bitmap1, buf,
967 bitmap_bufsize) < 0) {
968 return -1;
971 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
972 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
973 old_offset;
974 if (write_buffer(s->fd, offset_bitmap2, buf,
975 bitmap_bufsize) < 0) {
976 return -1;
979 memset(buf, 0, bitmap_bufsize);
980 old_offset += bitmap_bufsize;
983 /* get the exact place of the bit in the buf, and set it */
984 byte = (pfn % bits_per_buf) / CHAR_BIT;
985 bit = (pfn % bits_per_buf) % CHAR_BIT;
986 if (value) {
987 buf[byte] |= 1u << bit;
988 } else {
989 buf[byte] &= ~(1u << bit);
992 return 0;
995 static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
997 int target_page_shift = ctz32(s->dump_info.page_size);
999 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1002 static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1004 int target_page_shift = ctz32(s->dump_info.page_size);
1006 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1010 * exam every page and return the page frame number and the address of the page.
1011 * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1012 * blocks, so block->target_start and block->target_end should be interal
1013 * multiples of the target page size.
1015 static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1016 uint8_t **bufptr, DumpState *s)
1018 GuestPhysBlock *block = *blockptr;
1019 hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
1020 uint8_t *buf;
1022 /* block == NULL means the start of the iteration */
1023 if (!block) {
1024 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1025 *blockptr = block;
1026 assert((block->target_start & ~target_page_mask) == 0);
1027 assert((block->target_end & ~target_page_mask) == 0);
1028 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
1029 if (bufptr) {
1030 *bufptr = block->host_addr;
1032 return true;
1035 *pfnptr = *pfnptr + 1;
1036 addr = dump_pfn_to_paddr(s, *pfnptr);
1038 if ((addr >= block->target_start) &&
1039 (addr + s->dump_info.page_size <= block->target_end)) {
1040 buf = block->host_addr + (addr - block->target_start);
1041 } else {
1042 /* the next page is in the next block */
1043 block = QTAILQ_NEXT(block, next);
1044 *blockptr = block;
1045 if (!block) {
1046 return false;
1048 assert((block->target_start & ~target_page_mask) == 0);
1049 assert((block->target_end & ~target_page_mask) == 0);
1050 *pfnptr = dump_paddr_to_pfn(s, block->target_start);
1051 buf = block->host_addr;
1054 if (bufptr) {
1055 *bufptr = buf;
1058 return true;
1061 static void write_dump_bitmap(DumpState *s, Error **errp)
1063 int ret = 0;
1064 uint64_t last_pfn, pfn;
1065 void *dump_bitmap_buf;
1066 size_t num_dumpable;
1067 GuestPhysBlock *block_iter = NULL;
1068 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1069 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1071 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1072 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
1074 num_dumpable = 0;
1075 last_pfn = 0;
1078 * exam memory page by page, and set the bit in dump_bitmap corresponded
1079 * to the existing page.
1081 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1082 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1083 if (ret < 0) {
1084 error_setg(errp, "dump: failed to set dump_bitmap");
1085 goto out;
1088 last_pfn = pfn;
1089 num_dumpable++;
1093 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1094 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1095 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1097 if (num_dumpable > 0) {
1098 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
1099 dump_bitmap_buf, s);
1100 if (ret < 0) {
1101 error_setg(errp, "dump: failed to sync dump_bitmap");
1102 goto out;
1106 /* number of dumpable pages that will be dumped later */
1107 s->num_dumpable = num_dumpable;
1109 out:
1110 g_free(dump_bitmap_buf);
1113 static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1114 off_t offset)
1116 data_cache->fd = s->fd;
1117 data_cache->data_size = 0;
1118 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1119 data_cache->buf = g_malloc0(data_cache->buf_size);
1120 data_cache->offset = offset;
1123 static int write_cache(DataCache *dc, const void *buf, size_t size,
1124 bool flag_sync)
1127 * dc->buf_size should not be less than size, otherwise dc will never be
1128 * enough
1130 assert(size <= dc->buf_size);
1133 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1134 * otherwise check if the space is enough for caching data in buf, if not,
1135 * write the data in dc->buf to dc->fd and reset dc->buf
1137 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1138 (flag_sync && dc->data_size > 0)) {
1139 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1140 return -1;
1143 dc->offset += dc->data_size;
1144 dc->data_size = 0;
1147 if (!flag_sync) {
1148 memcpy(dc->buf + dc->data_size, buf, size);
1149 dc->data_size += size;
1152 return 0;
1155 static void free_data_cache(DataCache *data_cache)
1157 g_free(data_cache->buf);
1160 static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1162 switch (flag_compress) {
1163 case DUMP_DH_COMPRESSED_ZLIB:
1164 return compressBound(page_size);
1166 case DUMP_DH_COMPRESSED_LZO:
1168 * LZO will expand incompressible data by a little amount. Please check
1169 * the following URL to see the expansion calculation:
1170 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1172 return page_size + page_size / 16 + 64 + 3;
1174 #ifdef CONFIG_SNAPPY
1175 case DUMP_DH_COMPRESSED_SNAPPY:
1176 return snappy_max_compressed_length(page_size);
1177 #endif
1179 return 0;
1183 * check if the page is all 0
1185 static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1187 return buffer_is_zero(buf, page_size);
1190 static void write_dump_pages(DumpState *s, Error **errp)
1192 int ret = 0;
1193 DataCache page_desc, page_data;
1194 size_t len_buf_out, size_out;
1195 #ifdef CONFIG_LZO
1196 lzo_bytep wrkmem = NULL;
1197 #endif
1198 uint8_t *buf_out = NULL;
1199 off_t offset_desc, offset_data;
1200 PageDescriptor pd, pd_zero;
1201 uint8_t *buf;
1202 GuestPhysBlock *block_iter = NULL;
1203 uint64_t pfn_iter;
1205 /* get offset of page_desc and page_data in dump file */
1206 offset_desc = s->offset_page;
1207 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1209 prepare_data_cache(&page_desc, s, offset_desc);
1210 prepare_data_cache(&page_data, s, offset_data);
1212 /* prepare buffer to store compressed data */
1213 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
1214 assert(len_buf_out != 0);
1216 #ifdef CONFIG_LZO
1217 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1218 #endif
1220 buf_out = g_malloc(len_buf_out);
1223 * init zero page's page_desc and page_data, because every zero page
1224 * uses the same page_data
1226 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
1227 pd_zero.flags = cpu_to_dump32(s, 0);
1228 pd_zero.offset = cpu_to_dump64(s, offset_data);
1229 pd_zero.page_flags = cpu_to_dump64(s, 0);
1230 buf = g_malloc0(s->dump_info.page_size);
1231 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
1232 g_free(buf);
1233 if (ret < 0) {
1234 error_setg(errp, "dump: failed to write page data (zero page)");
1235 goto out;
1238 offset_data += s->dump_info.page_size;
1241 * dump memory to vmcore page by page. zero page will all be resided in the
1242 * first page of page section
1244 while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1245 /* check zero page */
1246 if (is_zero_page(buf, s->dump_info.page_size)) {
1247 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1248 false);
1249 if (ret < 0) {
1250 error_setg(errp, "dump: failed to write page desc");
1251 goto out;
1253 } else {
1255 * not zero page, then:
1256 * 1. compress the page
1257 * 2. write the compressed page into the cache of page_data
1258 * 3. get page desc of the compressed page and write it into the
1259 * cache of page_desc
1261 * only one compression format will be used here, for
1262 * s->flag_compress is set. But when compression fails to work,
1263 * we fall back to save in plaintext.
1265 size_out = len_buf_out;
1266 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
1267 (compress2(buf_out, (uLongf *)&size_out, buf,
1268 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1269 (size_out < s->dump_info.page_size)) {
1270 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1271 pd.size = cpu_to_dump32(s, size_out);
1273 ret = write_cache(&page_data, buf_out, size_out, false);
1274 if (ret < 0) {
1275 error_setg(errp, "dump: failed to write page data");
1276 goto out;
1278 #ifdef CONFIG_LZO
1279 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
1280 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
1281 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
1282 (size_out < s->dump_info.page_size)) {
1283 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1284 pd.size = cpu_to_dump32(s, size_out);
1286 ret = write_cache(&page_data, buf_out, size_out, false);
1287 if (ret < 0) {
1288 error_setg(errp, "dump: failed to write page data");
1289 goto out;
1291 #endif
1292 #ifdef CONFIG_SNAPPY
1293 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
1294 (snappy_compress((char *)buf, s->dump_info.page_size,
1295 (char *)buf_out, &size_out) == SNAPPY_OK) &&
1296 (size_out < s->dump_info.page_size)) {
1297 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1298 pd.size = cpu_to_dump32(s, size_out);
1300 ret = write_cache(&page_data, buf_out, size_out, false);
1301 if (ret < 0) {
1302 error_setg(errp, "dump: failed to write page data");
1303 goto out;
1305 #endif
1306 } else {
1308 * fall back to save in plaintext, size_out should be
1309 * assigned the target's page size
1311 pd.flags = cpu_to_dump32(s, 0);
1312 size_out = s->dump_info.page_size;
1313 pd.size = cpu_to_dump32(s, size_out);
1315 ret = write_cache(&page_data, buf,
1316 s->dump_info.page_size, false);
1317 if (ret < 0) {
1318 error_setg(errp, "dump: failed to write page data");
1319 goto out;
1323 /* get and write page desc here */
1324 pd.page_flags = cpu_to_dump64(s, 0);
1325 pd.offset = cpu_to_dump64(s, offset_data);
1326 offset_data += size_out;
1328 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1329 if (ret < 0) {
1330 error_setg(errp, "dump: failed to write page desc");
1331 goto out;
1334 s->written_size += s->dump_info.page_size;
1337 ret = write_cache(&page_desc, NULL, 0, true);
1338 if (ret < 0) {
1339 error_setg(errp, "dump: failed to sync cache for page_desc");
1340 goto out;
1342 ret = write_cache(&page_data, NULL, 0, true);
1343 if (ret < 0) {
1344 error_setg(errp, "dump: failed to sync cache for page_data");
1345 goto out;
1348 out:
1349 free_data_cache(&page_desc);
1350 free_data_cache(&page_data);
1352 #ifdef CONFIG_LZO
1353 g_free(wrkmem);
1354 #endif
1356 g_free(buf_out);
1359 static void create_kdump_vmcore(DumpState *s, Error **errp)
1361 int ret;
1362 Error *local_err = NULL;
1365 * the kdump-compressed format is:
1366 * File offset
1367 * +------------------------------------------+ 0x0
1368 * | main header (struct disk_dump_header) |
1369 * |------------------------------------------+ block 1
1370 * | sub header (struct kdump_sub_header) |
1371 * |------------------------------------------+ block 2
1372 * | 1st-dump_bitmap |
1373 * |------------------------------------------+ block 2 + X blocks
1374 * | 2nd-dump_bitmap | (aligned by block)
1375 * |------------------------------------------+ block 2 + 2 * X blocks
1376 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1377 * | page desc for pfn 1 (struct page_desc) |
1378 * | : |
1379 * |------------------------------------------| (not aligned by block)
1380 * | page data (pfn 0) |
1381 * | page data (pfn 1) |
1382 * | : |
1383 * +------------------------------------------+
1386 ret = write_start_flat_header(s->fd);
1387 if (ret < 0) {
1388 error_setg(errp, "dump: failed to write start flat header");
1389 return;
1392 write_dump_header(s, &local_err);
1393 if (local_err) {
1394 error_propagate(errp, local_err);
1395 return;
1398 write_dump_bitmap(s, &local_err);
1399 if (local_err) {
1400 error_propagate(errp, local_err);
1401 return;
1404 write_dump_pages(s, &local_err);
1405 if (local_err) {
1406 error_propagate(errp, local_err);
1407 return;
1410 ret = write_end_flat_header(s->fd);
1411 if (ret < 0) {
1412 error_setg(errp, "dump: failed to write end flat header");
1413 return;
1417 static ram_addr_t get_start_block(DumpState *s)
1419 GuestPhysBlock *block;
1421 if (!s->has_filter) {
1422 s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1423 return 0;
1426 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1427 if (block->target_start >= s->begin + s->length ||
1428 block->target_end <= s->begin) {
1429 /* This block is out of the range */
1430 continue;
1433 s->next_block = block;
1434 if (s->begin > block->target_start) {
1435 s->start = s->begin - block->target_start;
1436 } else {
1437 s->start = 0;
1439 return s->start;
1442 return -1;
1445 static void get_max_mapnr(DumpState *s)
1447 GuestPhysBlock *last_block;
1449 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head, GuestPhysBlockHead);
1450 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
1453 static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1455 static void dump_state_prepare(DumpState *s)
1457 /* zero the struct, setting status to active */
1458 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1461 bool dump_in_progress(void)
1463 DumpState *state = &dump_state_global;
1464 return (atomic_read(&state->status) == DUMP_STATUS_ACTIVE);
1467 /* calculate total size of memory to be dumped (taking filter into
1468 * acoount.) */
1469 static int64_t dump_calculate_size(DumpState *s)
1471 GuestPhysBlock *block;
1472 int64_t size = 0, total = 0, left = 0, right = 0;
1474 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1475 if (s->has_filter) {
1476 /* calculate the overlapped region. */
1477 left = MAX(s->begin, block->target_start);
1478 right = MIN(s->begin + s->length, block->target_end);
1479 size = right - left;
1480 size = size > 0 ? size : 0;
1481 } else {
1482 /* count the whole region in */
1483 size = (block->target_end - block->target_start);
1485 total += size;
1488 return total;
1491 static void dump_init(DumpState *s, int fd, bool has_format,
1492 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1493 int64_t begin, int64_t length, Error **errp)
1495 CPUState *cpu;
1496 int nr_cpus;
1497 Error *err = NULL;
1498 int ret;
1500 s->has_format = has_format;
1501 s->format = format;
1502 s->written_size = 0;
1504 /* kdump-compressed is conflict with paging and filter */
1505 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1506 assert(!paging && !has_filter);
1509 if (runstate_is_running()) {
1510 vm_stop(RUN_STATE_SAVE_VM);
1511 s->resume = true;
1512 } else {
1513 s->resume = false;
1516 /* If we use KVM, we should synchronize the registers before we get dump
1517 * info or physmap info.
1519 cpu_synchronize_all_states();
1520 nr_cpus = 0;
1521 CPU_FOREACH(cpu) {
1522 nr_cpus++;
1525 s->fd = fd;
1526 s->has_filter = has_filter;
1527 s->begin = begin;
1528 s->length = length;
1530 memory_mapping_list_init(&s->list);
1532 guest_phys_blocks_init(&s->guest_phys_blocks);
1533 guest_phys_blocks_append(&s->guest_phys_blocks);
1534 s->total_size = dump_calculate_size(s);
1535 #ifdef DEBUG_DUMP_GUEST_MEMORY
1536 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1537 #endif
1539 s->start = get_start_block(s);
1540 if (s->start == -1) {
1541 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
1542 goto cleanup;
1545 /* get dump info: endian, class and architecture.
1546 * If the target architecture is not supported, cpu_get_dump_info() will
1547 * return -1.
1549 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
1550 if (ret < 0) {
1551 error_setg(errp, QERR_UNSUPPORTED);
1552 goto cleanup;
1555 if (!s->dump_info.page_size) {
1556 s->dump_info.page_size = TARGET_PAGE_SIZE;
1559 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1560 s->dump_info.d_machine, nr_cpus);
1561 if (s->note_size < 0) {
1562 error_setg(errp, QERR_UNSUPPORTED);
1563 goto cleanup;
1566 /* get memory mapping */
1567 if (paging) {
1568 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
1569 if (err != NULL) {
1570 error_propagate(errp, err);
1571 goto cleanup;
1573 } else {
1574 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
1577 s->nr_cpus = nr_cpus;
1579 get_max_mapnr(s);
1581 uint64_t tmp;
1582 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1583 s->dump_info.page_size);
1584 s->len_dump_bitmap = tmp * s->dump_info.page_size;
1586 /* init for kdump-compressed format */
1587 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1588 switch (format) {
1589 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1590 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1591 break;
1593 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
1594 #ifdef CONFIG_LZO
1595 if (lzo_init() != LZO_E_OK) {
1596 error_setg(errp, "failed to initialize the LZO library");
1597 goto cleanup;
1599 #endif
1600 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1601 break;
1603 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1604 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1605 break;
1607 default:
1608 s->flag_compress = 0;
1611 return;
1614 if (s->has_filter) {
1615 memory_mapping_filter(&s->list, s->begin, s->length);
1619 * calculate phdr_num
1621 * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1623 s->phdr_num = 1; /* PT_NOTE */
1624 if (s->list.num < UINT16_MAX - 2) {
1625 s->phdr_num += s->list.num;
1626 s->have_section = false;
1627 } else {
1628 s->have_section = true;
1629 s->phdr_num = PN_XNUM;
1630 s->sh_info = 1; /* PT_NOTE */
1632 /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1633 if (s->list.num <= UINT32_MAX - 1) {
1634 s->sh_info += s->list.num;
1635 } else {
1636 s->sh_info = UINT32_MAX;
1640 if (s->dump_info.d_class == ELFCLASS64) {
1641 if (s->have_section) {
1642 s->memory_offset = sizeof(Elf64_Ehdr) +
1643 sizeof(Elf64_Phdr) * s->sh_info +
1644 sizeof(Elf64_Shdr) + s->note_size;
1645 } else {
1646 s->memory_offset = sizeof(Elf64_Ehdr) +
1647 sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1649 } else {
1650 if (s->have_section) {
1651 s->memory_offset = sizeof(Elf32_Ehdr) +
1652 sizeof(Elf32_Phdr) * s->sh_info +
1653 sizeof(Elf32_Shdr) + s->note_size;
1654 } else {
1655 s->memory_offset = sizeof(Elf32_Ehdr) +
1656 sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1660 return;
1662 cleanup:
1663 dump_cleanup(s);
1666 /* this operation might be time consuming. */
1667 static void dump_process(DumpState *s, Error **errp)
1669 Error *local_err = NULL;
1670 DumpQueryResult *result = NULL;
1672 if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1673 create_kdump_vmcore(s, &local_err);
1674 } else {
1675 create_vmcore(s, &local_err);
1678 /* make sure status is written after written_size updates */
1679 smp_wmb();
1680 atomic_set(&s->status,
1681 (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
1683 /* send DUMP_COMPLETED message (unconditionally) */
1684 result = qmp_query_dump(NULL);
1685 /* should never fail */
1686 assert(result);
1687 qapi_event_send_dump_completed(result, !!local_err, (local_err ? \
1688 error_get_pretty(local_err) : NULL),
1689 &error_abort);
1690 qapi_free_DumpQueryResult(result);
1692 error_propagate(errp, local_err);
1693 dump_cleanup(s);
1696 static void *dump_thread(void *data)
1698 Error *err = NULL;
1699 DumpState *s = (DumpState *)data;
1700 dump_process(s, &err);
1701 error_free(err);
1702 return NULL;
1705 DumpQueryResult *qmp_query_dump(Error **errp)
1707 DumpQueryResult *result = g_new(DumpQueryResult, 1);
1708 DumpState *state = &dump_state_global;
1709 result->status = atomic_read(&state->status);
1710 /* make sure we are reading status and written_size in order */
1711 smp_rmb();
1712 result->completed = state->written_size;
1713 result->total = state->total_size;
1714 return result;
1717 void qmp_dump_guest_memory(bool paging, const char *file,
1718 bool has_detach, bool detach,
1719 bool has_begin, int64_t begin, bool has_length,
1720 int64_t length, bool has_format,
1721 DumpGuestMemoryFormat format, Error **errp)
1723 const char *p;
1724 int fd = -1;
1725 DumpState *s;
1726 Error *local_err = NULL;
1727 bool detach_p = false;
1729 if (runstate_check(RUN_STATE_INMIGRATE)) {
1730 error_setg(errp, "Dump not allowed during incoming migration.");
1731 return;
1734 /* if there is a dump in background, we should wait until the dump
1735 * finished */
1736 if (dump_in_progress()) {
1737 error_setg(errp, "There is a dump in process, please wait.");
1738 return;
1742 * kdump-compressed format need the whole memory dumped, so paging or
1743 * filter is not supported here.
1745 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1746 (paging || has_begin || has_length)) {
1747 error_setg(errp, "kdump-compressed format doesn't support paging or "
1748 "filter");
1749 return;
1751 if (has_begin && !has_length) {
1752 error_setg(errp, QERR_MISSING_PARAMETER, "length");
1753 return;
1755 if (!has_begin && has_length) {
1756 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
1757 return;
1759 if (has_detach) {
1760 detach_p = detach;
1763 /* check whether lzo/snappy is supported */
1764 #ifndef CONFIG_LZO
1765 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1766 error_setg(errp, "kdump-lzo is not available now");
1767 return;
1769 #endif
1771 #ifndef CONFIG_SNAPPY
1772 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1773 error_setg(errp, "kdump-snappy is not available now");
1774 return;
1776 #endif
1778 #if !defined(WIN32)
1779 if (strstart(file, "fd:", &p)) {
1780 fd = monitor_get_fd(cur_mon, p, errp);
1781 if (fd == -1) {
1782 return;
1785 #endif
1787 if (strstart(file, "file:", &p)) {
1788 fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
1789 if (fd < 0) {
1790 error_setg_file_open(errp, errno, p);
1791 return;
1795 if (fd == -1) {
1796 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
1797 return;
1800 s = &dump_state_global;
1801 dump_state_prepare(s);
1803 dump_init(s, fd, has_format, format, paging, has_begin,
1804 begin, length, &local_err);
1805 if (local_err) {
1806 error_propagate(errp, local_err);
1807 atomic_set(&s->status, DUMP_STATUS_FAILED);
1808 return;
1811 if (detach_p) {
1812 /* detached dump */
1813 s->detached = true;
1814 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
1815 s, QEMU_THREAD_DETACHED);
1816 } else {
1817 /* sync dump */
1818 dump_process(s, errp);
1822 DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
1824 DumpGuestMemoryFormatList *item;
1825 DumpGuestMemoryCapability *cap =
1826 g_malloc0(sizeof(DumpGuestMemoryCapability));
1828 /* elf is always available */
1829 item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1830 cap->formats = item;
1831 item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
1833 /* kdump-zlib is always available */
1834 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1835 item = item->next;
1836 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1838 /* add new item if kdump-lzo is available */
1839 #ifdef CONFIG_LZO
1840 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1841 item = item->next;
1842 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1843 #endif
1845 /* add new item if kdump-snappy is available */
1846 #ifdef CONFIG_SNAPPY
1847 item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
1848 item = item->next;
1849 item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1850 #endif
1852 return cap;