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