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