xref: /openbmc/qemu/dump/dump.c (revision 5cc8767d)
1 /*
2  * QEMU dump
3  *
4  * Copyright Fujitsu, Corp. 2011, 2012
5  *
6  * Authors:
7  *     Wen Congyang <wency@cn.fujitsu.com>
8  *
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.
11  *
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qemu-common.h"
16 #include "qemu/cutils.h"
17 #include "elf.h"
18 #include "cpu.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/error.h"
27 #include "qapi/qapi-commands-dump.h"
28 #include "qapi/qapi-events-dump.h"
29 #include "qapi/qmp/qerror.h"
30 #include "qemu/error-report.h"
31 #include "hw/misc/vmcoreinfo.h"
32 
33 #ifdef TARGET_X86_64
34 #include "win_dump.h"
35 #endif
36 
37 #include <zlib.h>
38 #ifdef CONFIG_LZO
39 #include <lzo/lzo1x.h>
40 #endif
41 #ifdef CONFIG_SNAPPY
42 #include <snappy-c.h>
43 #endif
44 #ifndef ELF_MACHINE_UNAME
45 #define ELF_MACHINE_UNAME "Unknown"
46 #endif
47 
48 #define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
49 
50 #define ELF_NOTE_SIZE(hdr_size, name_size, desc_size)   \
51     ((DIV_ROUND_UP((hdr_size), 4) +                     \
52       DIV_ROUND_UP((name_size), 4) +                    \
53       DIV_ROUND_UP((desc_size), 4)) * 4)
54 
55 uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
56 {
57     if (s->dump_info.d_endian == ELFDATA2LSB) {
58         val = cpu_to_le16(val);
59     } else {
60         val = cpu_to_be16(val);
61     }
62 
63     return val;
64 }
65 
66 uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
67 {
68     if (s->dump_info.d_endian == ELFDATA2LSB) {
69         val = cpu_to_le32(val);
70     } else {
71         val = cpu_to_be32(val);
72     }
73 
74     return val;
75 }
76 
77 uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
78 {
79     if (s->dump_info.d_endian == ELFDATA2LSB) {
80         val = cpu_to_le64(val);
81     } else {
82         val = cpu_to_be64(val);
83     }
84 
85     return val;
86 }
87 
88 static int dump_cleanup(DumpState *s)
89 {
90     guest_phys_blocks_free(&s->guest_phys_blocks);
91     memory_mapping_list_free(&s->list);
92     close(s->fd);
93     g_free(s->guest_note);
94     s->guest_note = NULL;
95     if (s->resume) {
96         if (s->detached) {
97             qemu_mutex_lock_iothread();
98         }
99         vm_start();
100         if (s->detached) {
101             qemu_mutex_unlock_iothread();
102         }
103     }
104 
105     return 0;
106 }
107 
108 static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
109 {
110     DumpState *s = opaque;
111     size_t written_size;
112 
113     written_size = qemu_write_full(s->fd, buf, size);
114     if (written_size != size) {
115         return -errno;
116     }
117 
118     return 0;
119 }
120 
121 static void write_elf64_header(DumpState *s, Error **errp)
122 {
123     Elf64_Ehdr elf_header;
124     int ret;
125 
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_to_dump16(s, ET_CORE);
132     elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
133     elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
134     elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
135     elf_header.e_phoff = cpu_to_dump64(s, sizeof(Elf64_Ehdr));
136     elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
137     elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
138     if (s->have_section) {
139         uint64_t shoff = sizeof(Elf64_Ehdr) + sizeof(Elf64_Phdr) * s->sh_info;
140 
141         elf_header.e_shoff = cpu_to_dump64(s, shoff);
142         elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
143         elf_header.e_shnum = cpu_to_dump16(s, 1);
144     }
145 
146     ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
147     if (ret < 0) {
148         error_setg_errno(errp, -ret, "dump: failed to write elf header");
149     }
150 }
151 
152 static void write_elf32_header(DumpState *s, Error **errp)
153 {
154     Elf32_Ehdr elf_header;
155     int ret;
156 
157     memset(&elf_header, 0, sizeof(Elf32_Ehdr));
158     memcpy(&elf_header, ELFMAG, SELFMAG);
159     elf_header.e_ident[EI_CLASS] = ELFCLASS32;
160     elf_header.e_ident[EI_DATA] = s->dump_info.d_endian;
161     elf_header.e_ident[EI_VERSION] = EV_CURRENT;
162     elf_header.e_type = cpu_to_dump16(s, ET_CORE);
163     elf_header.e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
164     elf_header.e_version = cpu_to_dump32(s, EV_CURRENT);
165     elf_header.e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
166     elf_header.e_phoff = cpu_to_dump32(s, sizeof(Elf32_Ehdr));
167     elf_header.e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
168     elf_header.e_phnum = cpu_to_dump16(s, s->phdr_num);
169     if (s->have_section) {
170         uint32_t shoff = sizeof(Elf32_Ehdr) + sizeof(Elf32_Phdr) * s->sh_info;
171 
172         elf_header.e_shoff = cpu_to_dump32(s, shoff);
173         elf_header.e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
174         elf_header.e_shnum = cpu_to_dump16(s, 1);
175     }
176 
177     ret = fd_write_vmcore(&elf_header, sizeof(elf_header), s);
178     if (ret < 0) {
179         error_setg_errno(errp, -ret, "dump: failed to write elf header");
180     }
181 }
182 
183 static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
184                              int phdr_index, hwaddr offset,
185                              hwaddr filesz, Error **errp)
186 {
187     Elf64_Phdr phdr;
188     int ret;
189 
190     memset(&phdr, 0, sizeof(Elf64_Phdr));
191     phdr.p_type = cpu_to_dump32(s, PT_LOAD);
192     phdr.p_offset = cpu_to_dump64(s, offset);
193     phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
194     phdr.p_filesz = cpu_to_dump64(s, filesz);
195     phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
196     phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
197 
198     assert(memory_mapping->length >= filesz);
199 
200     ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
201     if (ret < 0) {
202         error_setg_errno(errp, -ret,
203                          "dump: failed to write program header table");
204     }
205 }
206 
207 static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
208                              int phdr_index, hwaddr offset,
209                              hwaddr filesz, Error **errp)
210 {
211     Elf32_Phdr phdr;
212     int ret;
213 
214     memset(&phdr, 0, sizeof(Elf32_Phdr));
215     phdr.p_type = cpu_to_dump32(s, PT_LOAD);
216     phdr.p_offset = cpu_to_dump32(s, offset);
217     phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
218     phdr.p_filesz = cpu_to_dump32(s, filesz);
219     phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
220     phdr.p_vaddr =
221         cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
222 
223     assert(memory_mapping->length >= filesz);
224 
225     ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
226     if (ret < 0) {
227         error_setg_errno(errp, -ret,
228                          "dump: failed to write program header table");
229     }
230 }
231 
232 static void write_elf64_note(DumpState *s, Error **errp)
233 {
234     Elf64_Phdr phdr;
235     hwaddr begin = s->memory_offset - s->note_size;
236     int ret;
237 
238     memset(&phdr, 0, sizeof(Elf64_Phdr));
239     phdr.p_type = cpu_to_dump32(s, PT_NOTE);
240     phdr.p_offset = cpu_to_dump64(s, begin);
241     phdr.p_paddr = 0;
242     phdr.p_filesz = cpu_to_dump64(s, s->note_size);
243     phdr.p_memsz = cpu_to_dump64(s, s->note_size);
244     phdr.p_vaddr = 0;
245 
246     ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
247     if (ret < 0) {
248         error_setg_errno(errp, -ret,
249                          "dump: failed to write program header table");
250     }
251 }
252 
253 static inline int cpu_index(CPUState *cpu)
254 {
255     return cpu->cpu_index + 1;
256 }
257 
258 static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
259                              Error **errp)
260 {
261     int ret;
262 
263     if (s->guest_note) {
264         ret = f(s->guest_note, s->guest_note_size, s);
265         if (ret < 0) {
266             error_setg(errp, "dump: failed to write guest note");
267         }
268     }
269 }
270 
271 static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
272                               Error **errp)
273 {
274     CPUState *cpu;
275     int ret;
276     int id;
277 
278     CPU_FOREACH(cpu) {
279         id = cpu_index(cpu);
280         ret = cpu_write_elf64_note(f, cpu, id, s);
281         if (ret < 0) {
282             error_setg(errp, "dump: failed to write elf notes");
283             return;
284         }
285     }
286 
287     CPU_FOREACH(cpu) {
288         ret = cpu_write_elf64_qemunote(f, cpu, s);
289         if (ret < 0) {
290             error_setg(errp, "dump: failed to write CPU status");
291             return;
292         }
293     }
294 
295     write_guest_note(f, s, errp);
296 }
297 
298 static void write_elf32_note(DumpState *s, Error **errp)
299 {
300     hwaddr begin = s->memory_offset - s->note_size;
301     Elf32_Phdr phdr;
302     int ret;
303 
304     memset(&phdr, 0, sizeof(Elf32_Phdr));
305     phdr.p_type = cpu_to_dump32(s, PT_NOTE);
306     phdr.p_offset = cpu_to_dump32(s, begin);
307     phdr.p_paddr = 0;
308     phdr.p_filesz = cpu_to_dump32(s, s->note_size);
309     phdr.p_memsz = cpu_to_dump32(s, s->note_size);
310     phdr.p_vaddr = 0;
311 
312     ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
313     if (ret < 0) {
314         error_setg_errno(errp, -ret,
315                          "dump: failed to write program header table");
316     }
317 }
318 
319 static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
320                               Error **errp)
321 {
322     CPUState *cpu;
323     int ret;
324     int id;
325 
326     CPU_FOREACH(cpu) {
327         id = cpu_index(cpu);
328         ret = cpu_write_elf32_note(f, cpu, id, s);
329         if (ret < 0) {
330             error_setg(errp, "dump: failed to write elf notes");
331             return;
332         }
333     }
334 
335     CPU_FOREACH(cpu) {
336         ret = cpu_write_elf32_qemunote(f, cpu, s);
337         if (ret < 0) {
338             error_setg(errp, "dump: failed to write CPU status");
339             return;
340         }
341     }
342 
343     write_guest_note(f, s, errp);
344 }
345 
346 static void write_elf_section(DumpState *s, int type, Error **errp)
347 {
348     Elf32_Shdr shdr32;
349     Elf64_Shdr shdr64;
350     int shdr_size;
351     void *shdr;
352     int ret;
353 
354     if (type == 0) {
355         shdr_size = sizeof(Elf32_Shdr);
356         memset(&shdr32, 0, shdr_size);
357         shdr32.sh_info = cpu_to_dump32(s, s->sh_info);
358         shdr = &shdr32;
359     } else {
360         shdr_size = sizeof(Elf64_Shdr);
361         memset(&shdr64, 0, shdr_size);
362         shdr64.sh_info = cpu_to_dump32(s, s->sh_info);
363         shdr = &shdr64;
364     }
365 
366     ret = fd_write_vmcore(&shdr, shdr_size, s);
367     if (ret < 0) {
368         error_setg_errno(errp, -ret,
369                          "dump: failed to write section header table");
370     }
371 }
372 
373 static void write_data(DumpState *s, void *buf, int length, Error **errp)
374 {
375     int ret;
376 
377     ret = fd_write_vmcore(buf, length, s);
378     if (ret < 0) {
379         error_setg_errno(errp, -ret, "dump: failed to save memory");
380     } else {
381         s->written_size += length;
382     }
383 }
384 
385 /* write the memory to vmcore. 1 page per I/O. */
386 static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
387                          int64_t size, Error **errp)
388 {
389     int64_t i;
390     Error *local_err = NULL;
391 
392     for (i = 0; i < size / s->dump_info.page_size; i++) {
393         write_data(s, block->host_addr + start + i * s->dump_info.page_size,
394                    s->dump_info.page_size, &local_err);
395         if (local_err) {
396             error_propagate(errp, local_err);
397             return;
398         }
399     }
400 
401     if ((size % s->dump_info.page_size) != 0) {
402         write_data(s, block->host_addr + start + i * s->dump_info.page_size,
403                    size % s->dump_info.page_size, &local_err);
404         if (local_err) {
405             error_propagate(errp, local_err);
406             return;
407         }
408     }
409 }
410 
411 /* get the memory's offset and size in the vmcore */
412 static void get_offset_range(hwaddr phys_addr,
413                              ram_addr_t mapping_length,
414                              DumpState *s,
415                              hwaddr *p_offset,
416                              hwaddr *p_filesz)
417 {
418     GuestPhysBlock *block;
419     hwaddr offset = s->memory_offset;
420     int64_t size_in_block, start;
421 
422     /* When the memory is not stored into vmcore, offset will be -1 */
423     *p_offset = -1;
424     *p_filesz = 0;
425 
426     if (s->has_filter) {
427         if (phys_addr < s->begin || phys_addr >= s->begin + s->length) {
428             return;
429         }
430     }
431 
432     QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
433         if (s->has_filter) {
434             if (block->target_start >= s->begin + s->length ||
435                 block->target_end <= s->begin) {
436                 /* This block is out of the range */
437                 continue;
438             }
439 
440             if (s->begin <= block->target_start) {
441                 start = block->target_start;
442             } else {
443                 start = s->begin;
444             }
445 
446             size_in_block = block->target_end - start;
447             if (s->begin + s->length < block->target_end) {
448                 size_in_block -= block->target_end - (s->begin + s->length);
449             }
450         } else {
451             start = block->target_start;
452             size_in_block = block->target_end - block->target_start;
453         }
454 
455         if (phys_addr >= start && phys_addr < start + size_in_block) {
456             *p_offset = phys_addr - start + offset;
457 
458             /* The offset range mapped from the vmcore file must not spill over
459              * the GuestPhysBlock, clamp it. The rest of the mapping will be
460              * zero-filled in memory at load time; see
461              * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
462              */
463             *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
464                         mapping_length :
465                         size_in_block - (phys_addr - start);
466             return;
467         }
468 
469         offset += size_in_block;
470     }
471 }
472 
473 static void write_elf_loads(DumpState *s, Error **errp)
474 {
475     hwaddr offset, filesz;
476     MemoryMapping *memory_mapping;
477     uint32_t phdr_index = 1;
478     uint32_t max_index;
479     Error *local_err = NULL;
480 
481     if (s->have_section) {
482         max_index = s->sh_info;
483     } else {
484         max_index = s->phdr_num;
485     }
486 
487     QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
488         get_offset_range(memory_mapping->phys_addr,
489                          memory_mapping->length,
490                          s, &offset, &filesz);
491         if (s->dump_info.d_class == ELFCLASS64) {
492             write_elf64_load(s, memory_mapping, phdr_index++, offset,
493                              filesz, &local_err);
494         } else {
495             write_elf32_load(s, memory_mapping, phdr_index++, offset,
496                              filesz, &local_err);
497         }
498 
499         if (local_err) {
500             error_propagate(errp, local_err);
501             return;
502         }
503 
504         if (phdr_index >= max_index) {
505             break;
506         }
507     }
508 }
509 
510 /* write elf header, PT_NOTE and elf note to vmcore. */
511 static void dump_begin(DumpState *s, Error **errp)
512 {
513     Error *local_err = NULL;
514 
515     /*
516      * the vmcore's format is:
517      *   --------------
518      *   |  elf header |
519      *   --------------
520      *   |  PT_NOTE    |
521      *   --------------
522      *   |  PT_LOAD    |
523      *   --------------
524      *   |  ......     |
525      *   --------------
526      *   |  PT_LOAD    |
527      *   --------------
528      *   |  sec_hdr    |
529      *   --------------
530      *   |  elf note   |
531      *   --------------
532      *   |  memory     |
533      *   --------------
534      *
535      * we only know where the memory is saved after we write elf note into
536      * vmcore.
537      */
538 
539     /* write elf header to vmcore */
540     if (s->dump_info.d_class == ELFCLASS64) {
541         write_elf64_header(s, &local_err);
542     } else {
543         write_elf32_header(s, &local_err);
544     }
545     if (local_err) {
546         error_propagate(errp, local_err);
547         return;
548     }
549 
550     if (s->dump_info.d_class == ELFCLASS64) {
551         /* write PT_NOTE to vmcore */
552         write_elf64_note(s, &local_err);
553         if (local_err) {
554             error_propagate(errp, local_err);
555             return;
556         }
557 
558         /* write all PT_LOAD to vmcore */
559         write_elf_loads(s, &local_err);
560         if (local_err) {
561             error_propagate(errp, local_err);
562             return;
563         }
564 
565         /* write section to vmcore */
566         if (s->have_section) {
567             write_elf_section(s, 1, &local_err);
568             if (local_err) {
569                 error_propagate(errp, local_err);
570                 return;
571             }
572         }
573 
574         /* write notes to vmcore */
575         write_elf64_notes(fd_write_vmcore, s, &local_err);
576         if (local_err) {
577             error_propagate(errp, local_err);
578             return;
579         }
580     } else {
581         /* write PT_NOTE to vmcore */
582         write_elf32_note(s, &local_err);
583         if (local_err) {
584             error_propagate(errp, local_err);
585             return;
586         }
587 
588         /* write all PT_LOAD to vmcore */
589         write_elf_loads(s, &local_err);
590         if (local_err) {
591             error_propagate(errp, local_err);
592             return;
593         }
594 
595         /* write section to vmcore */
596         if (s->have_section) {
597             write_elf_section(s, 0, &local_err);
598             if (local_err) {
599                 error_propagate(errp, local_err);
600                 return;
601             }
602         }
603 
604         /* write notes to vmcore */
605         write_elf32_notes(fd_write_vmcore, s, &local_err);
606         if (local_err) {
607             error_propagate(errp, local_err);
608             return;
609         }
610     }
611 }
612 
613 static int get_next_block(DumpState *s, GuestPhysBlock *block)
614 {
615     while (1) {
616         block = QTAILQ_NEXT(block, next);
617         if (!block) {
618             /* no more block */
619             return 1;
620         }
621 
622         s->start = 0;
623         s->next_block = block;
624         if (s->has_filter) {
625             if (block->target_start >= s->begin + s->length ||
626                 block->target_end <= s->begin) {
627                 /* This block is out of the range */
628                 continue;
629             }
630 
631             if (s->begin > block->target_start) {
632                 s->start = s->begin - block->target_start;
633             }
634         }
635 
636         return 0;
637     }
638 }
639 
640 /* write all memory to vmcore */
641 static void dump_iterate(DumpState *s, Error **errp)
642 {
643     GuestPhysBlock *block;
644     int64_t size;
645     Error *local_err = NULL;
646 
647     do {
648         block = s->next_block;
649 
650         size = block->target_end - block->target_start;
651         if (s->has_filter) {
652             size -= s->start;
653             if (s->begin + s->length < block->target_end) {
654                 size -= block->target_end - (s->begin + s->length);
655             }
656         }
657         write_memory(s, block, s->start, size, &local_err);
658         if (local_err) {
659             error_propagate(errp, local_err);
660             return;
661         }
662 
663     } while (!get_next_block(s, block));
664 }
665 
666 static void create_vmcore(DumpState *s, Error **errp)
667 {
668     Error *local_err = NULL;
669 
670     dump_begin(s, &local_err);
671     if (local_err) {
672         error_propagate(errp, local_err);
673         return;
674     }
675 
676     dump_iterate(s, errp);
677 }
678 
679 static int write_start_flat_header(int fd)
680 {
681     MakedumpfileHeader *mh;
682     int ret = 0;
683 
684     QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
685     mh = g_malloc0(MAX_SIZE_MDF_HEADER);
686 
687     memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
688            MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
689 
690     mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
691     mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
692 
693     size_t written_size;
694     written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
695     if (written_size != MAX_SIZE_MDF_HEADER) {
696         ret = -1;
697     }
698 
699     g_free(mh);
700     return ret;
701 }
702 
703 static int write_end_flat_header(int fd)
704 {
705     MakedumpfileDataHeader mdh;
706 
707     mdh.offset = END_FLAG_FLAT_HEADER;
708     mdh.buf_size = END_FLAG_FLAT_HEADER;
709 
710     size_t written_size;
711     written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
712     if (written_size != sizeof(mdh)) {
713         return -1;
714     }
715 
716     return 0;
717 }
718 
719 static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
720 {
721     size_t written_size;
722     MakedumpfileDataHeader mdh;
723 
724     mdh.offset = cpu_to_be64(offset);
725     mdh.buf_size = cpu_to_be64(size);
726 
727     written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
728     if (written_size != sizeof(mdh)) {
729         return -1;
730     }
731 
732     written_size = qemu_write_full(fd, buf, size);
733     if (written_size != size) {
734         return -1;
735     }
736 
737     return 0;
738 }
739 
740 static int buf_write_note(const void *buf, size_t size, void *opaque)
741 {
742     DumpState *s = opaque;
743 
744     /* note_buf is not enough */
745     if (s->note_buf_offset + size > s->note_size) {
746         return -1;
747     }
748 
749     memcpy(s->note_buf + s->note_buf_offset, buf, size);
750 
751     s->note_buf_offset += size;
752 
753     return 0;
754 }
755 
756 /*
757  * This function retrieves various sizes from an elf header.
758  *
759  * @note has to be a valid ELF note. The return sizes are unmodified
760  * (not padded or rounded up to be multiple of 4).
761  */
762 static void get_note_sizes(DumpState *s, const void *note,
763                            uint64_t *note_head_size,
764                            uint64_t *name_size,
765                            uint64_t *desc_size)
766 {
767     uint64_t note_head_sz;
768     uint64_t name_sz;
769     uint64_t desc_sz;
770 
771     if (s->dump_info.d_class == ELFCLASS64) {
772         const Elf64_Nhdr *hdr = note;
773         note_head_sz = sizeof(Elf64_Nhdr);
774         name_sz = tswap64(hdr->n_namesz);
775         desc_sz = tswap64(hdr->n_descsz);
776     } else {
777         const Elf32_Nhdr *hdr = note;
778         note_head_sz = sizeof(Elf32_Nhdr);
779         name_sz = tswap32(hdr->n_namesz);
780         desc_sz = tswap32(hdr->n_descsz);
781     }
782 
783     if (note_head_size) {
784         *note_head_size = note_head_sz;
785     }
786     if (name_size) {
787         *name_size = name_sz;
788     }
789     if (desc_size) {
790         *desc_size = desc_sz;
791     }
792 }
793 
794 static bool note_name_equal(DumpState *s,
795                             const uint8_t *note, const char *name)
796 {
797     int len = strlen(name) + 1;
798     uint64_t head_size, name_size;
799 
800     get_note_sizes(s, note, &head_size, &name_size, NULL);
801     head_size = ROUND_UP(head_size, 4);
802 
803     return name_size == len && memcmp(note + head_size, name, len) == 0;
804 }
805 
806 /* write common header, sub header and elf note to vmcore */
807 static void create_header32(DumpState *s, Error **errp)
808 {
809     DiskDumpHeader32 *dh = NULL;
810     KdumpSubHeader32 *kh = NULL;
811     size_t size;
812     uint32_t block_size;
813     uint32_t sub_hdr_size;
814     uint32_t bitmap_blocks;
815     uint32_t status = 0;
816     uint64_t offset_note;
817     Error *local_err = NULL;
818 
819     /* write common header, the version of kdump-compressed format is 6th */
820     size = sizeof(DiskDumpHeader32);
821     dh = g_malloc0(size);
822 
823     memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
824     dh->header_version = cpu_to_dump32(s, 6);
825     block_size = s->dump_info.page_size;
826     dh->block_size = cpu_to_dump32(s, block_size);
827     sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
828     sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
829     dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
830     /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
831     dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
832     dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
833     bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
834     dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
835     strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
836 
837     if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
838         status |= DUMP_DH_COMPRESSED_ZLIB;
839     }
840 #ifdef CONFIG_LZO
841     if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
842         status |= DUMP_DH_COMPRESSED_LZO;
843     }
844 #endif
845 #ifdef CONFIG_SNAPPY
846     if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
847         status |= DUMP_DH_COMPRESSED_SNAPPY;
848     }
849 #endif
850     dh->status = cpu_to_dump32(s, status);
851 
852     if (write_buffer(s->fd, 0, dh, size) < 0) {
853         error_setg(errp, "dump: failed to write disk dump header");
854         goto out;
855     }
856 
857     /* write sub header */
858     size = sizeof(KdumpSubHeader32);
859     kh = g_malloc0(size);
860 
861     /* 64bit max_mapnr_64 */
862     kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
863     kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
864     kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
865 
866     offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
867     if (s->guest_note &&
868         note_name_equal(s, s->guest_note, "VMCOREINFO")) {
869         uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
870 
871         get_note_sizes(s, s->guest_note,
872                        &hsize, &name_size, &size_vmcoreinfo_desc);
873         offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
874             (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
875         kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
876         kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
877     }
878 
879     kh->offset_note = cpu_to_dump64(s, offset_note);
880     kh->note_size = cpu_to_dump32(s, s->note_size);
881 
882     if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
883                      block_size, kh, size) < 0) {
884         error_setg(errp, "dump: failed to write kdump sub header");
885         goto out;
886     }
887 
888     /* write note */
889     s->note_buf = g_malloc0(s->note_size);
890     s->note_buf_offset = 0;
891 
892     /* use s->note_buf to store notes temporarily */
893     write_elf32_notes(buf_write_note, s, &local_err);
894     if (local_err) {
895         error_propagate(errp, local_err);
896         goto out;
897     }
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;
902     }
903 
904     /* get offset of dump_bitmap */
905     s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
906                              block_size;
907 
908     /* get offset of page */
909     s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
910                      block_size;
911 
912 out:
913     g_free(dh);
914     g_free(kh);
915     g_free(s->note_buf);
916 }
917 
918 /* write common header, sub header and elf note to vmcore */
919 static void create_header64(DumpState *s, Error **errp)
920 {
921     DiskDumpHeader64 *dh = NULL;
922     KdumpSubHeader64 *kh = NULL;
923     size_t size;
924     uint32_t block_size;
925     uint32_t sub_hdr_size;
926     uint32_t bitmap_blocks;
927     uint32_t status = 0;
928     uint64_t offset_note;
929     Error *local_err = NULL;
930 
931     /* write common header, the version of kdump-compressed format is 6th */
932     size = sizeof(DiskDumpHeader64);
933     dh = g_malloc0(size);
934 
935     memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
936     dh->header_version = cpu_to_dump32(s, 6);
937     block_size = s->dump_info.page_size;
938     dh->block_size = cpu_to_dump32(s, block_size);
939     sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
940     sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
941     dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
942     /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
943     dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
944     dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
945     bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
946     dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
947     strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
948 
949     if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
950         status |= DUMP_DH_COMPRESSED_ZLIB;
951     }
952 #ifdef CONFIG_LZO
953     if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
954         status |= DUMP_DH_COMPRESSED_LZO;
955     }
956 #endif
957 #ifdef CONFIG_SNAPPY
958     if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
959         status |= DUMP_DH_COMPRESSED_SNAPPY;
960     }
961 #endif
962     dh->status = cpu_to_dump32(s, status);
963 
964     if (write_buffer(s->fd, 0, dh, size) < 0) {
965         error_setg(errp, "dump: failed to write disk dump header");
966         goto out;
967     }
968 
969     /* write sub header */
970     size = sizeof(KdumpSubHeader64);
971     kh = g_malloc0(size);
972 
973     /* 64bit max_mapnr_64 */
974     kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
975     kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
976     kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
977 
978     offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
979     if (s->guest_note &&
980         note_name_equal(s, s->guest_note, "VMCOREINFO")) {
981         uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
982 
983         get_note_sizes(s, s->guest_note,
984                        &hsize, &name_size, &size_vmcoreinfo_desc);
985         offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
986             (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
987         kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
988         kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
989     }
990 
991     kh->offset_note = cpu_to_dump64(s, offset_note);
992     kh->note_size = cpu_to_dump64(s, s->note_size);
993 
994     if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
995                      block_size, kh, size) < 0) {
996         error_setg(errp, "dump: failed to write kdump sub header");
997         goto out;
998     }
999 
1000     /* write note */
1001     s->note_buf = g_malloc0(s->note_size);
1002     s->note_buf_offset = 0;
1003 
1004     /* use s->note_buf to store notes temporarily */
1005     write_elf64_notes(buf_write_note, s, &local_err);
1006     if (local_err) {
1007         error_propagate(errp, local_err);
1008         goto out;
1009     }
1010 
1011     if (write_buffer(s->fd, offset_note, s->note_buf,
1012                      s->note_size) < 0) {
1013         error_setg(errp, "dump: failed to write notes");
1014         goto out;
1015     }
1016 
1017     /* get offset of dump_bitmap */
1018     s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1019                              block_size;
1020 
1021     /* get offset of page */
1022     s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1023                      block_size;
1024 
1025 out:
1026     g_free(dh);
1027     g_free(kh);
1028     g_free(s->note_buf);
1029 }
1030 
1031 static void write_dump_header(DumpState *s, Error **errp)
1032 {
1033      Error *local_err = NULL;
1034 
1035     if (s->dump_info.d_class == ELFCLASS32) {
1036         create_header32(s, &local_err);
1037     } else {
1038         create_header64(s, &local_err);
1039     }
1040     error_propagate(errp, local_err);
1041 }
1042 
1043 static size_t dump_bitmap_get_bufsize(DumpState *s)
1044 {
1045     return s->dump_info.page_size;
1046 }
1047 
1048 /*
1049  * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1050  * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1051  * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1052  * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1053  * vmcore, ie. synchronizing un-sync bit into vmcore.
1054  */
1055 static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1056                            uint8_t *buf, DumpState *s)
1057 {
1058     off_t old_offset, new_offset;
1059     off_t offset_bitmap1, offset_bitmap2;
1060     uint32_t byte, bit;
1061     size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1062     size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1063 
1064     /* should not set the previous place */
1065     assert(last_pfn <= pfn);
1066 
1067     /*
1068      * if the bit needed to be set is not cached in buf, flush the data in buf
1069      * to vmcore firstly.
1070      * making new_offset be bigger than old_offset can also sync remained data
1071      * into vmcore.
1072      */
1073     old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1074     new_offset = bitmap_bufsize * (pfn / bits_per_buf);
1075 
1076     while (old_offset < new_offset) {
1077         /* calculate the offset and write dump_bitmap */
1078         offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1079         if (write_buffer(s->fd, offset_bitmap1, buf,
1080                          bitmap_bufsize) < 0) {
1081             return -1;
1082         }
1083 
1084         /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1085         offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1086                          old_offset;
1087         if (write_buffer(s->fd, offset_bitmap2, buf,
1088                          bitmap_bufsize) < 0) {
1089             return -1;
1090         }
1091 
1092         memset(buf, 0, bitmap_bufsize);
1093         old_offset += bitmap_bufsize;
1094     }
1095 
1096     /* get the exact place of the bit in the buf, and set it */
1097     byte = (pfn % bits_per_buf) / CHAR_BIT;
1098     bit = (pfn % bits_per_buf) % CHAR_BIT;
1099     if (value) {
1100         buf[byte] |= 1u << bit;
1101     } else {
1102         buf[byte] &= ~(1u << bit);
1103     }
1104 
1105     return 0;
1106 }
1107 
1108 static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1109 {
1110     int target_page_shift = ctz32(s->dump_info.page_size);
1111 
1112     return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1113 }
1114 
1115 static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1116 {
1117     int target_page_shift = ctz32(s->dump_info.page_size);
1118 
1119     return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1120 }
1121 
1122 /*
1123  * exam every page and return the page frame number and the address of the page.
1124  * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
1125  * blocks, so block->target_start and block->target_end should be interal
1126  * multiples of the target page size.
1127  */
1128 static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1129                           uint8_t **bufptr, DumpState *s)
1130 {
1131     GuestPhysBlock *block = *blockptr;
1132     hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
1133     uint8_t *buf;
1134 
1135     /* block == NULL means the start of the iteration */
1136     if (!block) {
1137         block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1138         *blockptr = block;
1139         assert((block->target_start & ~target_page_mask) == 0);
1140         assert((block->target_end & ~target_page_mask) == 0);
1141         *pfnptr = dump_paddr_to_pfn(s, block->target_start);
1142         if (bufptr) {
1143             *bufptr = block->host_addr;
1144         }
1145         return true;
1146     }
1147 
1148     *pfnptr = *pfnptr + 1;
1149     addr = dump_pfn_to_paddr(s, *pfnptr);
1150 
1151     if ((addr >= block->target_start) &&
1152         (addr + s->dump_info.page_size <= block->target_end)) {
1153         buf = block->host_addr + (addr - block->target_start);
1154     } else {
1155         /* the next page is in the next block */
1156         block = QTAILQ_NEXT(block, next);
1157         *blockptr = block;
1158         if (!block) {
1159             return false;
1160         }
1161         assert((block->target_start & ~target_page_mask) == 0);
1162         assert((block->target_end & ~target_page_mask) == 0);
1163         *pfnptr = dump_paddr_to_pfn(s, block->target_start);
1164         buf = block->host_addr;
1165     }
1166 
1167     if (bufptr) {
1168         *bufptr = buf;
1169     }
1170 
1171     return true;
1172 }
1173 
1174 static void write_dump_bitmap(DumpState *s, Error **errp)
1175 {
1176     int ret = 0;
1177     uint64_t last_pfn, pfn;
1178     void *dump_bitmap_buf;
1179     size_t num_dumpable;
1180     GuestPhysBlock *block_iter = NULL;
1181     size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1182     size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
1183 
1184     /* dump_bitmap_buf is used to store dump_bitmap temporarily */
1185     dump_bitmap_buf = g_malloc0(bitmap_bufsize);
1186 
1187     num_dumpable = 0;
1188     last_pfn = 0;
1189 
1190     /*
1191      * exam memory page by page, and set the bit in dump_bitmap corresponded
1192      * to the existing page.
1193      */
1194     while (get_next_page(&block_iter, &pfn, NULL, s)) {
1195         ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1196         if (ret < 0) {
1197             error_setg(errp, "dump: failed to set dump_bitmap");
1198             goto out;
1199         }
1200 
1201         last_pfn = pfn;
1202         num_dumpable++;
1203     }
1204 
1205     /*
1206      * set_dump_bitmap will always leave the recently set bit un-sync. Here we
1207      * set the remaining bits from last_pfn to the end of the bitmap buffer to
1208      * 0. With those set, the un-sync bit will be synchronized into the vmcore.
1209      */
1210     if (num_dumpable > 0) {
1211         ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
1212                               dump_bitmap_buf, s);
1213         if (ret < 0) {
1214             error_setg(errp, "dump: failed to sync dump_bitmap");
1215             goto out;
1216         }
1217     }
1218 
1219     /* number of dumpable pages that will be dumped later */
1220     s->num_dumpable = num_dumpable;
1221 
1222 out:
1223     g_free(dump_bitmap_buf);
1224 }
1225 
1226 static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1227                                off_t offset)
1228 {
1229     data_cache->fd = s->fd;
1230     data_cache->data_size = 0;
1231     data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1232     data_cache->buf = g_malloc0(data_cache->buf_size);
1233     data_cache->offset = offset;
1234 }
1235 
1236 static int write_cache(DataCache *dc, const void *buf, size_t size,
1237                        bool flag_sync)
1238 {
1239     /*
1240      * dc->buf_size should not be less than size, otherwise dc will never be
1241      * enough
1242      */
1243     assert(size <= dc->buf_size);
1244 
1245     /*
1246      * if flag_sync is set, synchronize data in dc->buf into vmcore.
1247      * otherwise check if the space is enough for caching data in buf, if not,
1248      * write the data in dc->buf to dc->fd and reset dc->buf
1249      */
1250     if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1251         (flag_sync && dc->data_size > 0)) {
1252         if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1253             return -1;
1254         }
1255 
1256         dc->offset += dc->data_size;
1257         dc->data_size = 0;
1258     }
1259 
1260     if (!flag_sync) {
1261         memcpy(dc->buf + dc->data_size, buf, size);
1262         dc->data_size += size;
1263     }
1264 
1265     return 0;
1266 }
1267 
1268 static void free_data_cache(DataCache *data_cache)
1269 {
1270     g_free(data_cache->buf);
1271 }
1272 
1273 static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1274 {
1275     switch (flag_compress) {
1276     case DUMP_DH_COMPRESSED_ZLIB:
1277         return compressBound(page_size);
1278 
1279     case DUMP_DH_COMPRESSED_LZO:
1280         /*
1281          * LZO will expand incompressible data by a little amount. Please check
1282          * the following URL to see the expansion calculation:
1283          * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1284          */
1285         return page_size + page_size / 16 + 64 + 3;
1286 
1287 #ifdef CONFIG_SNAPPY
1288     case DUMP_DH_COMPRESSED_SNAPPY:
1289         return snappy_max_compressed_length(page_size);
1290 #endif
1291     }
1292     return 0;
1293 }
1294 
1295 /*
1296  * check if the page is all 0
1297  */
1298 static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
1299 {
1300     return buffer_is_zero(buf, page_size);
1301 }
1302 
1303 static void write_dump_pages(DumpState *s, Error **errp)
1304 {
1305     int ret = 0;
1306     DataCache page_desc, page_data;
1307     size_t len_buf_out, size_out;
1308 #ifdef CONFIG_LZO
1309     lzo_bytep wrkmem = NULL;
1310 #endif
1311     uint8_t *buf_out = NULL;
1312     off_t offset_desc, offset_data;
1313     PageDescriptor pd, pd_zero;
1314     uint8_t *buf;
1315     GuestPhysBlock *block_iter = NULL;
1316     uint64_t pfn_iter;
1317 
1318     /* get offset of page_desc and page_data in dump file */
1319     offset_desc = s->offset_page;
1320     offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1321 
1322     prepare_data_cache(&page_desc, s, offset_desc);
1323     prepare_data_cache(&page_data, s, offset_data);
1324 
1325     /* prepare buffer to store compressed data */
1326     len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
1327     assert(len_buf_out != 0);
1328 
1329 #ifdef CONFIG_LZO
1330     wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1331 #endif
1332 
1333     buf_out = g_malloc(len_buf_out);
1334 
1335     /*
1336      * init zero page's page_desc and page_data, because every zero page
1337      * uses the same page_data
1338      */
1339     pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
1340     pd_zero.flags = cpu_to_dump32(s, 0);
1341     pd_zero.offset = cpu_to_dump64(s, offset_data);
1342     pd_zero.page_flags = cpu_to_dump64(s, 0);
1343     buf = g_malloc0(s->dump_info.page_size);
1344     ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
1345     g_free(buf);
1346     if (ret < 0) {
1347         error_setg(errp, "dump: failed to write page data (zero page)");
1348         goto out;
1349     }
1350 
1351     offset_data += s->dump_info.page_size;
1352 
1353     /*
1354      * dump memory to vmcore page by page. zero page will all be resided in the
1355      * first page of page section
1356      */
1357     while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
1358         /* check zero page */
1359         if (is_zero_page(buf, s->dump_info.page_size)) {
1360             ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1361                               false);
1362             if (ret < 0) {
1363                 error_setg(errp, "dump: failed to write page desc");
1364                 goto out;
1365             }
1366         } else {
1367             /*
1368              * not zero page, then:
1369              * 1. compress the page
1370              * 2. write the compressed page into the cache of page_data
1371              * 3. get page desc of the compressed page and write it into the
1372              *    cache of page_desc
1373              *
1374              * only one compression format will be used here, for
1375              * s->flag_compress is set. But when compression fails to work,
1376              * we fall back to save in plaintext.
1377              */
1378              size_out = len_buf_out;
1379              if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
1380                     (compress2(buf_out, (uLongf *)&size_out, buf,
1381                                s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1382                     (size_out < s->dump_info.page_size)) {
1383                 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1384                 pd.size  = cpu_to_dump32(s, size_out);
1385 
1386                 ret = write_cache(&page_data, buf_out, size_out, false);
1387                 if (ret < 0) {
1388                     error_setg(errp, "dump: failed to write page data");
1389                     goto out;
1390                 }
1391 #ifdef CONFIG_LZO
1392             } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
1393                     (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
1394                     (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
1395                     (size_out < s->dump_info.page_size)) {
1396                 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1397                 pd.size  = cpu_to_dump32(s, size_out);
1398 
1399                 ret = write_cache(&page_data, buf_out, size_out, false);
1400                 if (ret < 0) {
1401                     error_setg(errp, "dump: failed to write page data");
1402                     goto out;
1403                 }
1404 #endif
1405 #ifdef CONFIG_SNAPPY
1406             } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
1407                     (snappy_compress((char *)buf, s->dump_info.page_size,
1408                     (char *)buf_out, &size_out) == SNAPPY_OK) &&
1409                     (size_out < s->dump_info.page_size)) {
1410                 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1411                 pd.size  = cpu_to_dump32(s, size_out);
1412 
1413                 ret = write_cache(&page_data, buf_out, size_out, false);
1414                 if (ret < 0) {
1415                     error_setg(errp, "dump: failed to write page data");
1416                     goto out;
1417                 }
1418 #endif
1419             } else {
1420                 /*
1421                  * fall back to save in plaintext, size_out should be
1422                  * assigned the target's page size
1423                  */
1424                 pd.flags = cpu_to_dump32(s, 0);
1425                 size_out = s->dump_info.page_size;
1426                 pd.size = cpu_to_dump32(s, size_out);
1427 
1428                 ret = write_cache(&page_data, buf,
1429                                   s->dump_info.page_size, false);
1430                 if (ret < 0) {
1431                     error_setg(errp, "dump: failed to write page data");
1432                     goto out;
1433                 }
1434             }
1435 
1436             /* get and write page desc here */
1437             pd.page_flags = cpu_to_dump64(s, 0);
1438             pd.offset = cpu_to_dump64(s, offset_data);
1439             offset_data += size_out;
1440 
1441             ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1442             if (ret < 0) {
1443                 error_setg(errp, "dump: failed to write page desc");
1444                 goto out;
1445             }
1446         }
1447         s->written_size += s->dump_info.page_size;
1448     }
1449 
1450     ret = write_cache(&page_desc, NULL, 0, true);
1451     if (ret < 0) {
1452         error_setg(errp, "dump: failed to sync cache for page_desc");
1453         goto out;
1454     }
1455     ret = write_cache(&page_data, NULL, 0, true);
1456     if (ret < 0) {
1457         error_setg(errp, "dump: failed to sync cache for page_data");
1458         goto out;
1459     }
1460 
1461 out:
1462     free_data_cache(&page_desc);
1463     free_data_cache(&page_data);
1464 
1465 #ifdef CONFIG_LZO
1466     g_free(wrkmem);
1467 #endif
1468 
1469     g_free(buf_out);
1470 }
1471 
1472 static void create_kdump_vmcore(DumpState *s, Error **errp)
1473 {
1474     int ret;
1475     Error *local_err = NULL;
1476 
1477     /*
1478      * the kdump-compressed format is:
1479      *                                               File offset
1480      *  +------------------------------------------+ 0x0
1481      *  |    main header (struct disk_dump_header) |
1482      *  |------------------------------------------+ block 1
1483      *  |    sub header (struct kdump_sub_header)  |
1484      *  |------------------------------------------+ block 2
1485      *  |            1st-dump_bitmap               |
1486      *  |------------------------------------------+ block 2 + X blocks
1487      *  |            2nd-dump_bitmap               | (aligned by block)
1488      *  |------------------------------------------+ block 2 + 2 * X blocks
1489      *  |  page desc for pfn 0 (struct page_desc)  | (aligned by block)
1490      *  |  page desc for pfn 1 (struct page_desc)  |
1491      *  |                    :                     |
1492      *  |------------------------------------------| (not aligned by block)
1493      *  |         page data (pfn 0)                |
1494      *  |         page data (pfn 1)                |
1495      *  |                    :                     |
1496      *  +------------------------------------------+
1497      */
1498 
1499     ret = write_start_flat_header(s->fd);
1500     if (ret < 0) {
1501         error_setg(errp, "dump: failed to write start flat header");
1502         return;
1503     }
1504 
1505     write_dump_header(s, &local_err);
1506     if (local_err) {
1507         error_propagate(errp, local_err);
1508         return;
1509     }
1510 
1511     write_dump_bitmap(s, &local_err);
1512     if (local_err) {
1513         error_propagate(errp, local_err);
1514         return;
1515     }
1516 
1517     write_dump_pages(s, &local_err);
1518     if (local_err) {
1519         error_propagate(errp, local_err);
1520         return;
1521     }
1522 
1523     ret = write_end_flat_header(s->fd);
1524     if (ret < 0) {
1525         error_setg(errp, "dump: failed to write end flat header");
1526         return;
1527     }
1528 }
1529 
1530 static ram_addr_t get_start_block(DumpState *s)
1531 {
1532     GuestPhysBlock *block;
1533 
1534     if (!s->has_filter) {
1535         s->next_block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1536         return 0;
1537     }
1538 
1539     QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1540         if (block->target_start >= s->begin + s->length ||
1541             block->target_end <= s->begin) {
1542             /* This block is out of the range */
1543             continue;
1544         }
1545 
1546         s->next_block = block;
1547         if (s->begin > block->target_start) {
1548             s->start = s->begin - block->target_start;
1549         } else {
1550             s->start = 0;
1551         }
1552         return s->start;
1553     }
1554 
1555     return -1;
1556 }
1557 
1558 static void get_max_mapnr(DumpState *s)
1559 {
1560     GuestPhysBlock *last_block;
1561 
1562     last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
1563     s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
1564 }
1565 
1566 static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1567 
1568 static void dump_state_prepare(DumpState *s)
1569 {
1570     /* zero the struct, setting status to active */
1571     *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1572 }
1573 
1574 bool dump_in_progress(void)
1575 {
1576     DumpState *state = &dump_state_global;
1577     return (atomic_read(&state->status) == DUMP_STATUS_ACTIVE);
1578 }
1579 
1580 /* calculate total size of memory to be dumped (taking filter into
1581  * acoount.) */
1582 static int64_t dump_calculate_size(DumpState *s)
1583 {
1584     GuestPhysBlock *block;
1585     int64_t size = 0, total = 0, left = 0, right = 0;
1586 
1587     QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
1588         if (s->has_filter) {
1589             /* calculate the overlapped region. */
1590             left = MAX(s->begin, block->target_start);
1591             right = MIN(s->begin + s->length, block->target_end);
1592             size = right - left;
1593             size = size > 0 ? size : 0;
1594         } else {
1595             /* count the whole region in */
1596             size = (block->target_end - block->target_start);
1597         }
1598         total += size;
1599     }
1600 
1601     return total;
1602 }
1603 
1604 static void vmcoreinfo_update_phys_base(DumpState *s)
1605 {
1606     uint64_t size, note_head_size, name_size, phys_base;
1607     char **lines;
1608     uint8_t *vmci;
1609     size_t i;
1610 
1611     if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1612         return;
1613     }
1614 
1615     get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1616     note_head_size = ROUND_UP(note_head_size, 4);
1617 
1618     vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1619     *(vmci + size) = '\0';
1620 
1621     lines = g_strsplit((char *)vmci, "\n", -1);
1622     for (i = 0; lines[i]; i++) {
1623         const char *prefix = NULL;
1624 
1625         if (s->dump_info.d_machine == EM_X86_64) {
1626             prefix = "NUMBER(phys_base)=";
1627         } else if (s->dump_info.d_machine == EM_AARCH64) {
1628             prefix = "NUMBER(PHYS_OFFSET)=";
1629         }
1630 
1631         if (prefix && g_str_has_prefix(lines[i], prefix)) {
1632             if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
1633                               &phys_base) < 0) {
1634                 warn_report("Failed to read %s", prefix);
1635             } else {
1636                 s->dump_info.phys_base = phys_base;
1637             }
1638             break;
1639         }
1640     }
1641 
1642     g_strfreev(lines);
1643 }
1644 
1645 static void dump_init(DumpState *s, int fd, bool has_format,
1646                       DumpGuestMemoryFormat format, bool paging, bool has_filter,
1647                       int64_t begin, int64_t length, Error **errp)
1648 {
1649     VMCoreInfoState *vmci = vmcoreinfo_find();
1650     CPUState *cpu;
1651     int nr_cpus;
1652     Error *err = NULL;
1653     int ret;
1654 
1655     s->has_format = has_format;
1656     s->format = format;
1657     s->written_size = 0;
1658 
1659     /* kdump-compressed is conflict with paging and filter */
1660     if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1661         assert(!paging && !has_filter);
1662     }
1663 
1664     if (runstate_is_running()) {
1665         vm_stop(RUN_STATE_SAVE_VM);
1666         s->resume = true;
1667     } else {
1668         s->resume = false;
1669     }
1670 
1671     /* If we use KVM, we should synchronize the registers before we get dump
1672      * info or physmap info.
1673      */
1674     cpu_synchronize_all_states();
1675     nr_cpus = 0;
1676     CPU_FOREACH(cpu) {
1677         nr_cpus++;
1678     }
1679 
1680     s->fd = fd;
1681     s->has_filter = has_filter;
1682     s->begin = begin;
1683     s->length = length;
1684 
1685     memory_mapping_list_init(&s->list);
1686 
1687     guest_phys_blocks_init(&s->guest_phys_blocks);
1688     guest_phys_blocks_append(&s->guest_phys_blocks);
1689     s->total_size = dump_calculate_size(s);
1690 #ifdef DEBUG_DUMP_GUEST_MEMORY
1691     fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1692 #endif
1693 
1694     /* it does not make sense to dump non-existent memory */
1695     if (!s->total_size) {
1696         error_setg(errp, "dump: no guest memory to dump");
1697         goto cleanup;
1698     }
1699 
1700     s->start = get_start_block(s);
1701     if (s->start == -1) {
1702         error_setg(errp, QERR_INVALID_PARAMETER, "begin");
1703         goto cleanup;
1704     }
1705 
1706     /* get dump info: endian, class and architecture.
1707      * If the target architecture is not supported, cpu_get_dump_info() will
1708      * return -1.
1709      */
1710     ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
1711     if (ret < 0) {
1712         error_setg(errp, QERR_UNSUPPORTED);
1713         goto cleanup;
1714     }
1715 
1716     if (!s->dump_info.page_size) {
1717         s->dump_info.page_size = TARGET_PAGE_SIZE;
1718     }
1719 
1720     s->note_size = cpu_get_note_size(s->dump_info.d_class,
1721                                      s->dump_info.d_machine, nr_cpus);
1722     if (s->note_size < 0) {
1723         error_setg(errp, QERR_UNSUPPORTED);
1724         goto cleanup;
1725     }
1726 
1727     /*
1728      * The goal of this block is to (a) update the previously guessed
1729      * phys_base, (b) copy the guest note out of the guest.
1730      * Failure to do so is not fatal for dumping.
1731      */
1732     if (vmci) {
1733         uint64_t addr, note_head_size, name_size, desc_size;
1734         uint32_t size;
1735         uint16_t format;
1736 
1737         note_head_size = s->dump_info.d_class == ELFCLASS32 ?
1738             sizeof(Elf32_Nhdr) : sizeof(Elf64_Nhdr);
1739 
1740         format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1741         size = le32_to_cpu(vmci->vmcoreinfo.size);
1742         addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1743         if (!vmci->has_vmcoreinfo) {
1744             warn_report("guest note is not present");
1745         } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1746             warn_report("guest note size is invalid: %" PRIu32, size);
1747         } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
1748             warn_report("guest note format is unsupported: %" PRIu16, format);
1749         } else {
1750             s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1751             cpu_physical_memory_read(addr, s->guest_note, size);
1752 
1753             get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1754             s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1755                                                desc_size);
1756             if (name_size > MAX_GUEST_NOTE_SIZE ||
1757                 desc_size > MAX_GUEST_NOTE_SIZE ||
1758                 s->guest_note_size > size) {
1759                 warn_report("Invalid guest note header");
1760                 g_free(s->guest_note);
1761                 s->guest_note = NULL;
1762             } else {
1763                 vmcoreinfo_update_phys_base(s);
1764                 s->note_size += s->guest_note_size;
1765             }
1766         }
1767     }
1768 
1769     /* get memory mapping */
1770     if (paging) {
1771         qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, &err);
1772         if (err != NULL) {
1773             error_propagate(errp, err);
1774             goto cleanup;
1775         }
1776     } else {
1777         qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
1778     }
1779 
1780     s->nr_cpus = nr_cpus;
1781 
1782     get_max_mapnr(s);
1783 
1784     uint64_t tmp;
1785     tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1786                        s->dump_info.page_size);
1787     s->len_dump_bitmap = tmp * s->dump_info.page_size;
1788 
1789     /* init for kdump-compressed format */
1790     if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1791         switch (format) {
1792         case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1793             s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1794             break;
1795 
1796         case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
1797 #ifdef CONFIG_LZO
1798             if (lzo_init() != LZO_E_OK) {
1799                 error_setg(errp, "failed to initialize the LZO library");
1800                 goto cleanup;
1801             }
1802 #endif
1803             s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1804             break;
1805 
1806         case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1807             s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1808             break;
1809 
1810         default:
1811             s->flag_compress = 0;
1812         }
1813 
1814         return;
1815     }
1816 
1817     if (s->has_filter) {
1818         memory_mapping_filter(&s->list, s->begin, s->length);
1819     }
1820 
1821     /*
1822      * calculate phdr_num
1823      *
1824      * the type of ehdr->e_phnum is uint16_t, so we should avoid overflow
1825      */
1826     s->phdr_num = 1; /* PT_NOTE */
1827     if (s->list.num < UINT16_MAX - 2) {
1828         s->phdr_num += s->list.num;
1829         s->have_section = false;
1830     } else {
1831         s->have_section = true;
1832         s->phdr_num = PN_XNUM;
1833         s->sh_info = 1; /* PT_NOTE */
1834 
1835         /* the type of shdr->sh_info is uint32_t, so we should avoid overflow */
1836         if (s->list.num <= UINT32_MAX - 1) {
1837             s->sh_info += s->list.num;
1838         } else {
1839             s->sh_info = UINT32_MAX;
1840         }
1841     }
1842 
1843     if (s->dump_info.d_class == ELFCLASS64) {
1844         if (s->have_section) {
1845             s->memory_offset = sizeof(Elf64_Ehdr) +
1846                                sizeof(Elf64_Phdr) * s->sh_info +
1847                                sizeof(Elf64_Shdr) + s->note_size;
1848         } else {
1849             s->memory_offset = sizeof(Elf64_Ehdr) +
1850                                sizeof(Elf64_Phdr) * s->phdr_num + s->note_size;
1851         }
1852     } else {
1853         if (s->have_section) {
1854             s->memory_offset = sizeof(Elf32_Ehdr) +
1855                                sizeof(Elf32_Phdr) * s->sh_info +
1856                                sizeof(Elf32_Shdr) + s->note_size;
1857         } else {
1858             s->memory_offset = sizeof(Elf32_Ehdr) +
1859                                sizeof(Elf32_Phdr) * s->phdr_num + s->note_size;
1860         }
1861     }
1862 
1863     return;
1864 
1865 cleanup:
1866     dump_cleanup(s);
1867 }
1868 
1869 /* this operation might be time consuming. */
1870 static void dump_process(DumpState *s, Error **errp)
1871 {
1872     Error *local_err = NULL;
1873     DumpQueryResult *result = NULL;
1874 
1875     if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1876 #ifdef TARGET_X86_64
1877         create_win_dump(s, &local_err);
1878 #endif
1879     } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1880         create_kdump_vmcore(s, &local_err);
1881     } else {
1882         create_vmcore(s, &local_err);
1883     }
1884 
1885     /* make sure status is written after written_size updates */
1886     smp_wmb();
1887     atomic_set(&s->status,
1888                (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
1889 
1890     /* send DUMP_COMPLETED message (unconditionally) */
1891     result = qmp_query_dump(NULL);
1892     /* should never fail */
1893     assert(result);
1894     qapi_event_send_dump_completed(result, !!local_err, (local_err ? \
1895                                    error_get_pretty(local_err) : NULL));
1896     qapi_free_DumpQueryResult(result);
1897 
1898     error_propagate(errp, local_err);
1899     dump_cleanup(s);
1900 }
1901 
1902 static void *dump_thread(void *data)
1903 {
1904     DumpState *s = (DumpState *)data;
1905     dump_process(s, NULL);
1906     return NULL;
1907 }
1908 
1909 DumpQueryResult *qmp_query_dump(Error **errp)
1910 {
1911     DumpQueryResult *result = g_new(DumpQueryResult, 1);
1912     DumpState *state = &dump_state_global;
1913     result->status = atomic_read(&state->status);
1914     /* make sure we are reading status and written_size in order */
1915     smp_rmb();
1916     result->completed = state->written_size;
1917     result->total = state->total_size;
1918     return result;
1919 }
1920 
1921 void qmp_dump_guest_memory(bool paging, const char *file,
1922                            bool has_detach, bool detach,
1923                            bool has_begin, int64_t begin, bool has_length,
1924                            int64_t length, bool has_format,
1925                            DumpGuestMemoryFormat format, Error **errp)
1926 {
1927     const char *p;
1928     int fd = -1;
1929     DumpState *s;
1930     Error *local_err = NULL;
1931     bool detach_p = false;
1932 
1933     if (runstate_check(RUN_STATE_INMIGRATE)) {
1934         error_setg(errp, "Dump not allowed during incoming migration.");
1935         return;
1936     }
1937 
1938     /* if there is a dump in background, we should wait until the dump
1939      * finished */
1940     if (dump_in_progress()) {
1941         error_setg(errp, "There is a dump in process, please wait.");
1942         return;
1943     }
1944 
1945     /*
1946      * kdump-compressed format need the whole memory dumped, so paging or
1947      * filter is not supported here.
1948      */
1949     if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
1950         (paging || has_begin || has_length)) {
1951         error_setg(errp, "kdump-compressed format doesn't support paging or "
1952                          "filter");
1953         return;
1954     }
1955     if (has_begin && !has_length) {
1956         error_setg(errp, QERR_MISSING_PARAMETER, "length");
1957         return;
1958     }
1959     if (!has_begin && has_length) {
1960         error_setg(errp, QERR_MISSING_PARAMETER, "begin");
1961         return;
1962     }
1963     if (has_detach) {
1964         detach_p = detach;
1965     }
1966 
1967     /* check whether lzo/snappy is supported */
1968 #ifndef CONFIG_LZO
1969     if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
1970         error_setg(errp, "kdump-lzo is not available now");
1971         return;
1972     }
1973 #endif
1974 
1975 #ifndef CONFIG_SNAPPY
1976     if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
1977         error_setg(errp, "kdump-snappy is not available now");
1978         return;
1979     }
1980 #endif
1981 
1982 #ifndef TARGET_X86_64
1983     if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
1984         error_setg(errp, "Windows dump is only available for x86-64");
1985         return;
1986     }
1987 #endif
1988 
1989 #if !defined(WIN32)
1990     if (strstart(file, "fd:", &p)) {
1991         fd = monitor_get_fd(cur_mon, p, errp);
1992         if (fd == -1) {
1993             return;
1994         }
1995     }
1996 #endif
1997 
1998     if  (strstart(file, "file:", &p)) {
1999         fd = qemu_open(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
2000         if (fd < 0) {
2001             error_setg_file_open(errp, errno, p);
2002             return;
2003         }
2004     }
2005 
2006     if (fd == -1) {
2007         error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
2008         return;
2009     }
2010 
2011     s = &dump_state_global;
2012     dump_state_prepare(s);
2013 
2014     dump_init(s, fd, has_format, format, paging, has_begin,
2015               begin, length, &local_err);
2016     if (local_err) {
2017         error_propagate(errp, local_err);
2018         atomic_set(&s->status, DUMP_STATUS_FAILED);
2019         return;
2020     }
2021 
2022     if (detach_p) {
2023         /* detached dump */
2024         s->detached = true;
2025         qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2026                            s, QEMU_THREAD_DETACHED);
2027     } else {
2028         /* sync dump */
2029         dump_process(s, errp);
2030     }
2031 }
2032 
2033 DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2034 {
2035     DumpGuestMemoryFormatList *item;
2036     DumpGuestMemoryCapability *cap =
2037                                   g_malloc0(sizeof(DumpGuestMemoryCapability));
2038 
2039     /* elf is always available */
2040     item = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2041     cap->formats = item;
2042     item->value = DUMP_GUEST_MEMORY_FORMAT_ELF;
2043 
2044     /* kdump-zlib is always available */
2045     item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2046     item = item->next;
2047     item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2048 
2049     /* add new item if kdump-lzo is available */
2050 #ifdef CONFIG_LZO
2051     item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2052     item = item->next;
2053     item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2054 #endif
2055 
2056     /* add new item if kdump-snappy is available */
2057 #ifdef CONFIG_SNAPPY
2058     item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2059     item = item->next;
2060     item->value = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2061 #endif
2062 
2063     /* Windows dump is available only if target is x86_64 */
2064 #ifdef TARGET_X86_64
2065     item->next = g_malloc0(sizeof(DumpGuestMemoryFormatList));
2066     item = item->next;
2067     item->value = DUMP_GUEST_MEMORY_FORMAT_WIN_DMP;
2068 #endif
2069 
2070     return cap;
2071 }
2072