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