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