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