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