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