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