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