blob: e6833e4cc023c9b24a8cdec8782e9a978547f94a [file] [log] [blame]
Wen Congyang783e9b42012-05-07 12:10:47 +08001/*
2 * QEMU dump
3 *
4 * Copyright Fujitsu, Corp. 2011, 2012
5 *
6 * Authors:
7 * Wen Congyang <wency@cn.fujitsu.com>
8 *
Stefan Weil352666e2012-06-10 19:34:04 +00009 * 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.
Wen Congyang783e9b42012-05-07 12:10:47 +080011 *
12 */
13
Peter Maydelld38ea872016-01-29 17:50:05 +000014#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020015#include "qemu/cutils.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080016#include "elf.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010017#include "exec/hwaddr.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010018#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010019#include "sysemu/kvm.h"
20#include "sysemu/dump.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010021#include "sysemu/memory_mapping.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020022#include "sysemu/runstate.h"
Andreas Färber1b3509c2013-06-09 16:48:29 +020023#include "sysemu/cpus.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010024#include "qapi/error.h"
Markus Armbrusterd06b7472019-06-19 22:10:47 +020025#include "qapi/qapi-commands-dump.h"
26#include "qapi/qapi-events-dump.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010027#include "qapi/qmp/qerror.h"
Marc-André Lureau903ef732017-09-11 18:59:25 +020028#include "qemu/error-report.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020029#include "qemu/main-loop.h"
Marc-André Lureau903ef732017-09-11 18:59:25 +020030#include "hw/misc/vmcoreinfo.h"
Peter Xub7bc6b12021-09-22 12:20:09 -040031#include "migration/blocker.h"
Wen Congyang783e9b42012-05-07 12:10:47 +080032
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030033#ifdef TARGET_X86_64
34#include "win_dump.h"
35#endif
36
qiaonuohand12f57e2014-02-18 14:11:35 +080037#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
qiaonuohan4ab23a92014-02-18 14:11:37 +080044#ifndef ELF_MACHINE_UNAME
45#define ELF_MACHINE_UNAME "Unknown"
46#endif
qiaonuohand12f57e2014-02-18 14:11:35 +080047
Marc-André Lureau903ef732017-09-11 18:59:25 +020048#define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
49
Peter Xub7bc6b12021-09-22 12:20:09 -040050static Error *dump_migration_blocker;
51
Marc-André Lureau903ef732017-09-11 18:59:25 +020052#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
Janosch Frank05bbaa502022-03-30 12:36:00 +000057static inline bool dump_is_64bit(DumpState *s)
58{
59 return s->dump_info.d_class == ELFCLASS64;
60}
61
Janosch Frankdddf7252022-08-11 12:10:58 +000062static inline bool dump_has_filter(DumpState *s)
63{
64 return s->filter_area_length > 0;
65}
66
Bharata B Raoacb0ef52014-05-19 19:57:44 +020067uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080068{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020069 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080070 val = cpu_to_le16(val);
71 } else {
72 val = cpu_to_be16(val);
73 }
74
75 return val;
76}
77
Bharata B Raoacb0ef52014-05-19 19:57:44 +020078uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080079{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020080 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080081 val = cpu_to_le32(val);
82 } else {
83 val = cpu_to_be32(val);
84 }
85
86 return val;
87}
88
Bharata B Raoacb0ef52014-05-19 19:57:44 +020089uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080090{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020091 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080092 val = cpu_to_le64(val);
93 } else {
94 val = cpu_to_be64(val);
95 }
96
97 return val;
98}
99
Wen Congyang783e9b42012-05-07 12:10:47 +0800100static int dump_cleanup(DumpState *s)
101{
Laszlo Ersek5ee163e2013-08-06 12:37:09 +0200102 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +0800103 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +0800104 close(s->fd);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200105 g_free(s->guest_note);
Janosch Frank9b722242022-10-17 11:32:10 +0000106 g_array_unref(s->string_table_buf);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200107 s->guest_note = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800108 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +0800109 if (s->detached) {
110 qemu_mutex_lock_iothread();
111 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800112 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +0800113 if (s->detached) {
114 qemu_mutex_unlock_iothread();
115 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800116 }
Peter Xub7bc6b12021-09-22 12:20:09 -0400117 migrate_del_blocker(dump_migration_blocker);
Wen Congyang783e9b42012-05-07 12:10:47 +0800118
Chen Gang29282072014-08-03 23:28:56 +0800119 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800120}
121
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800122static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800123{
124 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300125 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800126
Luiz Capitulino2f616522012-09-21 13:17:55 -0300127 written_size = qemu_write_full(s->fd, buf, size);
128 if (written_size != size) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200129 return -errno;
Wen Congyang783e9b42012-05-07 12:10:47 +0800130 }
131
132 return 0;
133}
134
Janosch Frank670e7692022-08-11 12:11:00 +0000135static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header)
Wen Congyang783e9b42012-05-07 12:10:47 +0800136{
Janosch Frank046bc412022-04-07 09:48:24 +0000137 /*
138 * phnum in the elf header is 16 bit, if we have more segments we
139 * set phnum to PN_XNUM and write the real number of segments to a
140 * special section.
141 */
142 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Wen Congyang783e9b42012-05-07 12:10:47 +0800143
Janosch Frank670e7692022-08-11 12:11:00 +0000144 memset(elf_header, 0, sizeof(Elf64_Ehdr));
145 memcpy(elf_header, ELFMAG, SELFMAG);
146 elf_header->e_ident[EI_CLASS] = ELFCLASS64;
147 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
148 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
149 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
150 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
151 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
152 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
153 elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset);
154 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
155 elf_header->e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank9b722242022-10-17 11:32:10 +0000156 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);
159 elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800160}
161
Janosch Frank670e7692022-08-11 12:11:00 +0000162static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
Wen Congyang783e9b42012-05-07 12:10:47 +0800163{
Janosch Frank046bc412022-04-07 09:48:24 +0000164 /*
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);
Janosch Frank670e7692022-08-11 12:11:00 +0000170
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);
Janosch Frank9b722242022-10-17 11:32:10 +0000183 elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
184 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
185 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
186 elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
Janosch Frank670e7692022-08-11 12:11:00 +0000187}
188
189static void write_elf_header(DumpState *s, Error **errp)
190{
191 Elf32_Ehdr elf32_header;
192 Elf64_Ehdr elf64_header;
193 size_t header_size;
194 void *header_ptr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800195 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800196
Janosch Frank9b722242022-10-17 11:32:10 +0000197 /* The NULL header and the shstrtab are always defined */
198 assert(s->shdr_num >= 2);
Janosch Frank670e7692022-08-11 12:11:00 +0000199 if (dump_is_64bit(s)) {
200 prepare_elf64_header(s, &elf64_header);
201 header_size = sizeof(elf64_header);
202 header_ptr = &elf64_header;
203 } else {
204 prepare_elf32_header(s, &elf32_header);
205 header_size = sizeof(elf32_header);
206 header_ptr = &elf32_header;
Wen Congyang783e9b42012-05-07 12:10:47 +0800207 }
208
Janosch Frank670e7692022-08-11 12:11:00 +0000209 ret = fd_write_vmcore(header_ptr, header_size, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800210 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200211 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800212 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800213}
214
zhanghailiang4c7e2512014-10-09 14:13:11 +0800215static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
216 int phdr_index, hwaddr offset,
217 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800218{
219 Elf64_Phdr phdr;
220 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800221
222 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200223 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
224 phdr.p_offset = cpu_to_dump64(s, offset);
225 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
226 phdr.p_filesz = cpu_to_dump64(s, filesz);
227 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200228 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800229
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200230 assert(memory_mapping->length >= filesz);
231
Wen Congyang783e9b42012-05-07 12:10:47 +0800232 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
233 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200234 error_setg_errno(errp, -ret,
235 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800236 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800237}
238
zhanghailiang4c7e2512014-10-09 14:13:11 +0800239static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
240 int phdr_index, hwaddr offset,
241 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800242{
243 Elf32_Phdr phdr;
244 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800245
246 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200247 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
248 phdr.p_offset = cpu_to_dump32(s, offset);
249 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
250 phdr.p_filesz = cpu_to_dump32(s, filesz);
251 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200252 phdr.p_vaddr =
253 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800254
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200255 assert(memory_mapping->length >= filesz);
256
Wen Congyang783e9b42012-05-07 12:10:47 +0800257 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
258 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200259 error_setg_errno(errp, -ret,
260 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800261 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800262}
263
Janosch Frank2341a942022-08-11 12:11:01 +0000264static void prepare_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800265{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000266 memset(phdr, 0, sizeof(*phdr));
267 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
268 phdr->p_offset = cpu_to_dump64(s, s->note_offset);
269 phdr->p_paddr = 0;
270 phdr->p_filesz = cpu_to_dump64(s, s->note_size);
271 phdr->p_memsz = cpu_to_dump64(s, s->note_size);
272 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800273}
274
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200275static inline int cpu_index(CPUState *cpu)
276{
277 return cpu->cpu_index + 1;
278}
279
Marc-André Lureau903ef732017-09-11 18:59:25 +0200280static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
281 Error **errp)
282{
283 int ret;
284
285 if (s->guest_note) {
286 ret = f(s->guest_note, s->guest_note_size, s);
287 if (ret < 0) {
288 error_setg(errp, "dump: failed to write guest note");
289 }
290 }
291}
292
zhanghailiang4c7e2512014-10-09 14:13:11 +0800293static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
294 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800295{
Andreas Färber0d342822012-12-17 07:12:13 +0100296 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800297 int ret;
298 int id;
299
Andreas Färberbdc44642013-06-24 23:50:24 +0200300 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100301 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800302 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800303 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800304 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800305 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800306 }
307 }
308
Andreas Färberbdc44642013-06-24 23:50:24 +0200309 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800310 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800311 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800312 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800313 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800314 }
315 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200316
317 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800318}
319
Janosch Frank2341a942022-08-11 12:11:01 +0000320static void prepare_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800321{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000322 memset(phdr, 0, sizeof(*phdr));
323 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
324 phdr->p_offset = cpu_to_dump32(s, s->note_offset);
325 phdr->p_paddr = 0;
326 phdr->p_filesz = cpu_to_dump32(s, s->note_size);
327 phdr->p_memsz = cpu_to_dump32(s, s->note_size);
328 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800329}
330
zhanghailiang4c7e2512014-10-09 14:13:11 +0800331static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
332 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800333{
Andreas Färber0d342822012-12-17 07:12:13 +0100334 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800335 int ret;
336 int id;
337
Andreas Färberbdc44642013-06-24 23:50:24 +0200338 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100339 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800340 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800341 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800342 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800343 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800344 }
345 }
346
Andreas Färberbdc44642013-06-24 23:50:24 +0200347 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800348 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800349 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800350 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800351 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800352 }
353 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200354
355 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800356}
357
Janosch Frankbc7d5582022-03-30 12:36:01 +0000358static void write_elf_phdr_note(DumpState *s, Error **errp)
359{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000360 Elf32_Phdr phdr32;
361 Elf64_Phdr phdr64;
362 void *phdr;
363 size_t size;
364 int ret;
365
366 if (dump_is_64bit(s)) {
Janosch Frank2341a942022-08-11 12:11:01 +0000367 prepare_elf64_phdr_note(s, &phdr64);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000368 size = sizeof(phdr64);
369 phdr = &phdr64;
370 } else {
Janosch Frank2341a942022-08-11 12:11:01 +0000371 prepare_elf32_phdr_note(s, &phdr32);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000372 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
Janosch Franke41ed292022-10-17 08:38:13 +0000383static void prepare_elf_section_hdr_zero(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +0800384{
Janosch Franke41ed292022-10-17 08:38:13 +0000385 if (dump_is_64bit(s)) {
386 Elf64_Shdr *shdr64 = s->elf_section_hdrs;
387
388 shdr64->sh_info = cpu_to_dump32(s, s->phdr_num);
389 } else {
390 Elf32_Shdr *shdr32 = s->elf_section_hdrs;
391
392 shdr32->sh_info = cpu_to_dump32(s, s->phdr_num);
393 }
394}
395
Janosch Frank9b722242022-10-17 11:32:10 +0000396static void prepare_elf_section_hdr_string(DumpState *s, void *buff)
397{
398 uint64_t index = s->string_table_buf->len;
399 const char strtab[] = ".shstrtab";
400 Elf32_Shdr shdr32 = {};
401 Elf64_Shdr shdr64 = {};
402 int shdr_size;
403 void *shdr;
404
405 g_array_append_vals(s->string_table_buf, strtab, sizeof(strtab));
406 if (dump_is_64bit(s)) {
407 shdr_size = sizeof(Elf64_Shdr);
408 shdr64.sh_type = SHT_STRTAB;
409 shdr64.sh_offset = s->section_offset + s->elf_section_data_size;
410 shdr64.sh_name = index;
411 shdr64.sh_size = s->string_table_buf->len;
412 shdr = &shdr64;
413 } else {
414 shdr_size = sizeof(Elf32_Shdr);
415 shdr32.sh_type = SHT_STRTAB;
416 shdr32.sh_offset = s->section_offset + s->elf_section_data_size;
417 shdr32.sh_name = index;
418 shdr32.sh_size = s->string_table_buf->len;
419 shdr = &shdr32;
420 }
421 memcpy(buff, shdr, shdr_size);
422}
423
424static bool prepare_elf_section_hdrs(DumpState *s, Error **errp)
Janosch Franke41ed292022-10-17 08:38:13 +0000425{
426 size_t len, sizeof_shdr;
Janosch Frank9b722242022-10-17 11:32:10 +0000427 void *buff_hdr;
Janosch Franke41ed292022-10-17 08:38:13 +0000428
429 /*
430 * Section ordering:
431 * - HDR zero
Janosch Frank9b722242022-10-17 11:32:10 +0000432 * - Arch section hdrs
433 * - String table hdr
Janosch Franke41ed292022-10-17 08:38:13 +0000434 */
435 sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
436 len = sizeof_shdr * s->shdr_num;
437 s->elf_section_hdrs = g_malloc0(len);
Janosch Frank9b722242022-10-17 11:32:10 +0000438 buff_hdr = s->elf_section_hdrs;
Janosch Franke41ed292022-10-17 08:38:13 +0000439
440 /*
441 * The first section header is ALWAYS a special initial section
442 * header.
443 *
444 * The header should be 0 with one exception being that if
445 * phdr_num is PN_XNUM then the sh_info field contains the real
446 * number of segment entries.
447 *
448 * As we zero allocate the buffer we will only need to modify
449 * sh_info for the PN_XNUM case.
450 */
451 if (s->phdr_num >= PN_XNUM) {
452 prepare_elf_section_hdr_zero(s);
453 }
Janosch Frank9b722242022-10-17 11:32:10 +0000454 buff_hdr += sizeof_shdr;
455
456 /* Add architecture defined section headers */
457 if (s->dump_info.arch_sections_write_hdr_fn
458 && s->shdr_num > 2) {
459 buff_hdr += s->dump_info.arch_sections_write_hdr_fn(s, buff_hdr);
460
461 if (s->shdr_num >= SHN_LORESERVE) {
462 error_setg_errno(errp, EINVAL,
463 "dump: too many architecture defined sections");
464 return false;
465 }
466 }
467
468 /*
469 * String table is the last section since strings are added via
470 * arch_sections_write_hdr().
471 */
472 prepare_elf_section_hdr_string(s, buff_hdr);
473 return true;
Janosch Franke41ed292022-10-17 08:38:13 +0000474}
475
476static void write_elf_section_headers(DumpState *s, Error **errp)
477{
478 size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
Wen Congyang783e9b42012-05-07 12:10:47 +0800479 int ret;
480
Janosch Frank9b722242022-10-17 11:32:10 +0000481 if (!prepare_elf_section_hdrs(s, errp)) {
482 return;
483 }
Janosch Franke41ed292022-10-17 08:38:13 +0000484
485 ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s);
486 if (ret < 0) {
487 error_setg_errno(errp, -ret, "dump: failed to write section headers");
Wen Congyang783e9b42012-05-07 12:10:47 +0800488 }
489
Janosch Franke41ed292022-10-17 08:38:13 +0000490 g_free(s->elf_section_hdrs);
Wen Congyang783e9b42012-05-07 12:10:47 +0800491}
492
Janosch Frank9b722242022-10-17 11:32:10 +0000493static void write_elf_sections(DumpState *s, Error **errp)
494{
495 int ret;
496
497 if (s->elf_section_data_size) {
498 /* Write architecture section data */
499 ret = fd_write_vmcore(s->elf_section_data,
500 s->elf_section_data_size, s);
501 if (ret < 0) {
502 error_setg_errno(errp, -ret,
503 "dump: failed to write architecture section data");
504 return;
505 }
506 }
507
508 /* Write string table */
509 ret = fd_write_vmcore(s->string_table_buf->data,
510 s->string_table_buf->len, s);
511 if (ret < 0) {
512 error_setg_errno(errp, -ret, "dump: failed to write string table data");
513 }
514}
515
zhanghailiang4c7e2512014-10-09 14:13:11 +0800516static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800517{
518 int ret;
519
520 ret = fd_write_vmcore(buf, length, s);
521 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200522 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800523 } else {
524 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800525 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800526}
527
zhanghailiang4c7e2512014-10-09 14:13:11 +0800528/* write the memory to vmcore. 1 page per I/O. */
529static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
530 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800531{
Janosch Frank86a518b2022-03-30 12:35:55 +0000532 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800533 int64_t i;
Wen Congyang783e9b42012-05-07 12:10:47 +0800534
Andrew Jones8161bef2016-01-11 20:56:20 +0100535 for (i = 0; i < size / s->dump_info.page_size; i++) {
536 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000537 s->dump_info.page_size, errp);
538 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800539 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800540 }
541 }
542
Andrew Jones8161bef2016-01-11 20:56:20 +0100543 if ((size % s->dump_info.page_size) != 0) {
544 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000545 size % s->dump_info.page_size, errp);
546 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800547 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800548 }
549 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800550}
551
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200552/* get the memory's offset and size in the vmcore */
553static void get_offset_range(hwaddr phys_addr,
554 ram_addr_t mapping_length,
555 DumpState *s,
556 hwaddr *p_offset,
557 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800558{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200559 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200560 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800561 int64_t size_in_block, start;
562
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200563 /* When the memory is not stored into vmcore, offset will be -1 */
564 *p_offset = -1;
565 *p_filesz = 0;
566
Janosch Frankdddf7252022-08-11 12:10:58 +0000567 if (dump_has_filter(s)) {
568 if (phys_addr < s->filter_area_begin ||
569 phys_addr >= s->filter_area_begin + s->filter_area_length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200570 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800571 }
572 }
573
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200574 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000575 if (dump_has_filter(s)) {
576 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
577 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800578 /* This block is out of the range */
579 continue;
580 }
581
Janosch Frankdddf7252022-08-11 12:10:58 +0000582 if (s->filter_area_begin <= block->target_start) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200583 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800584 } else {
Janosch Frankdddf7252022-08-11 12:10:58 +0000585 start = s->filter_area_begin;
Wen Congyang783e9b42012-05-07 12:10:47 +0800586 }
587
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200588 size_in_block = block->target_end - start;
Janosch Frankdddf7252022-08-11 12:10:58 +0000589 if (s->filter_area_begin + s->filter_area_length < block->target_end) {
590 size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800591 }
592 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200593 start = block->target_start;
594 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800595 }
596
597 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200598 *p_offset = phys_addr - start + offset;
599
600 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200601 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200602 * zero-filled in memory at load time; see
603 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
604 */
605 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
606 mapping_length :
607 size_in_block - (phys_addr - start);
608 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800609 }
610
611 offset += size_in_block;
612 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800613}
614
Janosch Frankafae6052022-08-11 12:10:55 +0000615static void write_elf_phdr_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800616{
Janosch Frank86a518b2022-03-30 12:35:55 +0000617 ERRP_GUARD();
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200618 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800619 MemoryMapping *memory_mapping;
620 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800621
622 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200623 get_offset_range(memory_mapping->phys_addr,
624 memory_mapping->length,
625 s, &offset, &filesz);
Janosch Frank05bbaa502022-03-30 12:36:00 +0000626 if (dump_is_64bit(s)) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800627 write_elf64_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000628 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800629 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800630 write_elf32_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000631 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800632 }
633
Janosch Frank86a518b2022-03-30 12:35:55 +0000634 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800635 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800636 }
637
Janosch Frank046bc412022-04-07 09:48:24 +0000638 if (phdr_index >= s->phdr_num) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800639 break;
640 }
641 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800642}
643
Janosch Frankc6812472022-03-30 12:36:03 +0000644static void write_elf_notes(DumpState *s, Error **errp)
645{
646 if (dump_is_64bit(s)) {
647 write_elf64_notes(fd_write_vmcore, s, errp);
648 } else {
649 write_elf32_notes(fd_write_vmcore, s, errp);
650 }
651}
652
Wen Congyang783e9b42012-05-07 12:10:47 +0800653/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800654static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800655{
Janosch Frank86a518b2022-03-30 12:35:55 +0000656 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800657
658 /*
659 * the vmcore's format is:
660 * --------------
661 * | elf header |
662 * --------------
Janosch Frankcb415fd2022-10-17 08:38:14 +0000663 * | sctn_hdr |
664 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800665 * | PT_NOTE |
666 * --------------
667 * | PT_LOAD |
668 * --------------
669 * | ...... |
670 * --------------
671 * | PT_LOAD |
672 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800673 * | elf note |
674 * --------------
675 * | memory |
676 * --------------
677 *
678 * we only know where the memory is saved after we write elf note into
679 * vmcore.
680 */
681
682 /* write elf header to vmcore */
Janosch Frank670e7692022-08-11 12:11:00 +0000683 write_elf_header(s, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000684 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800685 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800686 }
687
Janosch Frankcb415fd2022-10-17 08:38:14 +0000688 /* write section headers to vmcore */
689 write_elf_section_headers(s, errp);
690 if (*errp) {
691 return;
692 }
693
Janosch Frankbc7d5582022-03-30 12:36:01 +0000694 /* write PT_NOTE to vmcore */
695 write_elf_phdr_note(s, errp);
696 if (*errp) {
697 return;
698 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800699
Janosch Frankafae6052022-08-11 12:10:55 +0000700 /* write all PT_LOADs to vmcore */
701 write_elf_phdr_loads(s, errp);
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000702 if (*errp) {
703 return;
704 }
705
Janosch Frankc6812472022-03-30 12:36:03 +0000706 /* write notes to vmcore */
707 write_elf_notes(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800708}
709
Janosch Frank113d8f42022-10-17 08:38:22 +0000710int64_t dump_filtered_memblock_size(GuestPhysBlock *block,
711 int64_t filter_area_start,
712 int64_t filter_area_length)
Wen Congyang783e9b42012-05-07 12:10:47 +0800713{
Janosch Frank1e811302022-08-11 12:10:56 +0000714 int64_t size, left, right;
Wen Congyang783e9b42012-05-07 12:10:47 +0800715
Janosch Frank1e811302022-08-11 12:10:56 +0000716 /* No filter, return full size */
717 if (!filter_area_length) {
718 return block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800719 }
Janosch Frank1e811302022-08-11 12:10:56 +0000720
721 /* calculate the overlapped region. */
722 left = MAX(filter_area_start, block->target_start);
723 right = MIN(filter_area_start + filter_area_length, block->target_end);
724 size = right - left;
725 size = size > 0 ? size : 0;
726
727 return size;
728}
729
Janosch Frank113d8f42022-10-17 08:38:22 +0000730int64_t dump_filtered_memblock_start(GuestPhysBlock *block,
731 int64_t filter_area_start,
732 int64_t filter_area_length)
Janosch Frank1e811302022-08-11 12:10:56 +0000733{
734 if (filter_area_length) {
735 /* return -1 if the block is not within filter area */
736 if (block->target_start >= filter_area_start + filter_area_length ||
737 block->target_end <= filter_area_start) {
738 return -1;
739 }
740
741 if (filter_area_start > block->target_start) {
742 return filter_area_start - block->target_start;
743 }
744 }
745
746 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800747}
748
749/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800750static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800751{
Janosch Frank86a518b2022-03-30 12:35:55 +0000752 ERRP_GUARD();
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200753 GuestPhysBlock *block;
Janosch Frank1e811302022-08-11 12:10:56 +0000754 int64_t memblock_size, memblock_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800755
Janosch Frank1e811302022-08-11 12:10:56 +0000756 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000757 memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000758 if (memblock_start == -1) {
759 continue;
Wen Congyang783e9b42012-05-07 12:10:47 +0800760 }
Janosch Frank1e811302022-08-11 12:10:56 +0000761
Janosch Frankdddf7252022-08-11 12:10:58 +0000762 memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000763
764 /* Write the memory to file */
765 write_memory(s, block, memblock_start, memblock_size, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000766 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800767 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800768 }
Janosch Frank1e811302022-08-11 12:10:56 +0000769 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800770}
771
Janosch Frank9b722242022-10-17 11:32:10 +0000772static void dump_end(DumpState *s, Error **errp)
773{
774 int rc;
Janosch Frank9b722242022-10-17 11:32:10 +0000775
776 if (s->elf_section_data_size) {
777 s->elf_section_data = g_malloc0(s->elf_section_data_size);
778 }
779
780 /* Adds the architecture defined section data to s->elf_section_data */
781 if (s->dump_info.arch_sections_write_fn &&
782 s->elf_section_data_size) {
783 rc = s->dump_info.arch_sections_write_fn(s, s->elf_section_data);
784 if (rc) {
785 error_setg_errno(errp, rc,
786 "dump: failed to get arch section data");
787 g_free(s->elf_section_data);
788 return;
789 }
790 }
791
792 /* write sections to vmcore */
793 write_elf_sections(s, errp);
794}
795
zhanghailiang4c7e2512014-10-09 14:13:11 +0800796static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800797{
Janosch Frank86a518b2022-03-30 12:35:55 +0000798 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800799
Janosch Frank86a518b2022-03-30 12:35:55 +0000800 dump_begin(s, errp);
801 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800802 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800803 }
804
Janosch Frank9b722242022-10-17 11:32:10 +0000805 /* Iterate over memory and dump it to file */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800806 dump_iterate(s, errp);
Janosch Frank9b722242022-10-17 11:32:10 +0000807 if (*errp) {
808 return;
809 }
810
811 /* Write the section data */
812 dump_end(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800813}
814
qiaonuohanfda05382014-02-18 14:11:27 +0800815static int write_start_flat_header(int fd)
816{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200817 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800818 int ret = 0;
819
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200820 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
821 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800822
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200823 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
824 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800825
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200826 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
827 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800828
829 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200830 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800831 if (written_size != MAX_SIZE_MDF_HEADER) {
832 ret = -1;
833 }
834
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200835 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800836 return ret;
837}
838
839static int write_end_flat_header(int fd)
840{
841 MakedumpfileDataHeader mdh;
842
843 mdh.offset = END_FLAG_FLAT_HEADER;
844 mdh.buf_size = END_FLAG_FLAT_HEADER;
845
846 size_t written_size;
847 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
848 if (written_size != sizeof(mdh)) {
849 return -1;
850 }
851
852 return 0;
853}
854
qiaonuohan5d31bab2014-02-18 14:11:28 +0800855static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
856{
857 size_t written_size;
858 MakedumpfileDataHeader mdh;
859
860 mdh.offset = cpu_to_be64(offset);
861 mdh.buf_size = cpu_to_be64(size);
862
863 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
864 if (written_size != sizeof(mdh)) {
865 return -1;
866 }
867
868 written_size = qemu_write_full(fd, buf, size);
869 if (written_size != size) {
870 return -1;
871 }
872
873 return 0;
874}
875
qiaonuohan4835ef72014-02-18 14:11:29 +0800876static int buf_write_note(const void *buf, size_t size, void *opaque)
877{
878 DumpState *s = opaque;
879
880 /* note_buf is not enough */
881 if (s->note_buf_offset + size > s->note_size) {
882 return -1;
883 }
884
885 memcpy(s->note_buf + s->note_buf_offset, buf, size);
886
887 s->note_buf_offset += size;
888
889 return 0;
890}
891
Marc-André Lureau903ef732017-09-11 18:59:25 +0200892/*
893 * This function retrieves various sizes from an elf header.
894 *
895 * @note has to be a valid ELF note. The return sizes are unmodified
896 * (not padded or rounded up to be multiple of 4).
897 */
898static void get_note_sizes(DumpState *s, const void *note,
899 uint64_t *note_head_size,
900 uint64_t *name_size,
901 uint64_t *desc_size)
902{
903 uint64_t note_head_sz;
904 uint64_t name_sz;
905 uint64_t desc_sz;
906
Janosch Frank05bbaa502022-03-30 12:36:00 +0000907 if (dump_is_64bit(s)) {
Marc-André Lureau903ef732017-09-11 18:59:25 +0200908 const Elf64_Nhdr *hdr = note;
909 note_head_sz = sizeof(Elf64_Nhdr);
910 name_sz = tswap64(hdr->n_namesz);
911 desc_sz = tswap64(hdr->n_descsz);
912 } else {
913 const Elf32_Nhdr *hdr = note;
914 note_head_sz = sizeof(Elf32_Nhdr);
915 name_sz = tswap32(hdr->n_namesz);
916 desc_sz = tswap32(hdr->n_descsz);
917 }
918
919 if (note_head_size) {
920 *note_head_size = note_head_sz;
921 }
922 if (name_size) {
923 *name_size = name_sz;
924 }
925 if (desc_size) {
926 *desc_size = desc_sz;
927 }
928}
929
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200930static bool note_name_equal(DumpState *s,
931 const uint8_t *note, const char *name)
932{
933 int len = strlen(name) + 1;
934 uint64_t head_size, name_size;
935
936 get_note_sizes(s, note, &head_size, &name_size, NULL);
937 head_size = ROUND_UP(head_size, 4);
938
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100939 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200940}
941
qiaonuohan298f1162014-02-18 14:11:32 +0800942/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800943static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800944{
Janosch Frank86a518b2022-03-30 12:35:55 +0000945 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800946 DiskDumpHeader32 *dh = NULL;
947 KdumpSubHeader32 *kh = NULL;
948 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800949 uint32_t block_size;
950 uint32_t sub_hdr_size;
951 uint32_t bitmap_blocks;
952 uint32_t status = 0;
953 uint64_t offset_note;
954
955 /* write common header, the version of kdump-compressed format is 6th */
956 size = sizeof(DiskDumpHeader32);
957 dh = g_malloc0(size);
958
Eric Blake84c868f2018-03-27 15:21:51 -0500959 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200960 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100961 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200962 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800963 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
964 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200965 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800966 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200967 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
968 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800969 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200970 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800971 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800972
973 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
974 status |= DUMP_DH_COMPRESSED_ZLIB;
975 }
976#ifdef CONFIG_LZO
977 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
978 status |= DUMP_DH_COMPRESSED_LZO;
979 }
980#endif
981#ifdef CONFIG_SNAPPY
982 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
983 status |= DUMP_DH_COMPRESSED_SNAPPY;
984 }
985#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200986 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800987
988 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800989 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800990 goto out;
991 }
992
993 /* write sub header */
994 size = sizeof(KdumpSubHeader32);
995 kh = g_malloc0(size);
996
997 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200998 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100999 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001000 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +08001001
1002 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +02001003 if (s->guest_note &&
1004 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1005 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1006
1007 get_note_sizes(s, s->guest_note,
1008 &hsize, &name_size, &size_vmcoreinfo_desc);
1009 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1010 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1011 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1012 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
1013 }
1014
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001015 kh->offset_note = cpu_to_dump64(s, offset_note);
1016 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001017
1018 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
1019 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001020 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001021 goto out;
1022 }
1023
1024 /* write note */
1025 s->note_buf = g_malloc0(s->note_size);
1026 s->note_buf_offset = 0;
1027
1028 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001029 write_elf32_notes(buf_write_note, s, errp);
1030 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001031 goto out;
1032 }
qiaonuohan298f1162014-02-18 14:11:32 +08001033 if (write_buffer(s->fd, offset_note, s->note_buf,
1034 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001035 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001036 goto out;
1037 }
1038
1039 /* get offset of dump_bitmap */
1040 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1041 block_size;
1042
1043 /* get offset of page */
1044 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1045 block_size;
1046
1047out:
1048 g_free(dh);
1049 g_free(kh);
1050 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001051}
1052
1053/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +08001054static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001055{
Janosch Frank86a518b2022-03-30 12:35:55 +00001056 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +08001057 DiskDumpHeader64 *dh = NULL;
1058 KdumpSubHeader64 *kh = NULL;
1059 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +08001060 uint32_t block_size;
1061 uint32_t sub_hdr_size;
1062 uint32_t bitmap_blocks;
1063 uint32_t status = 0;
1064 uint64_t offset_note;
1065
1066 /* write common header, the version of kdump-compressed format is 6th */
1067 size = sizeof(DiskDumpHeader64);
1068 dh = g_malloc0(size);
1069
Eric Blake84c868f2018-03-27 15:21:51 -05001070 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001071 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +01001072 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001073 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001074 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
1075 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001076 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001077 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001078 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
1079 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +08001080 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001081 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +08001082 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +08001083
1084 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
1085 status |= DUMP_DH_COMPRESSED_ZLIB;
1086 }
1087#ifdef CONFIG_LZO
1088 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
1089 status |= DUMP_DH_COMPRESSED_LZO;
1090 }
1091#endif
1092#ifdef CONFIG_SNAPPY
1093 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
1094 status |= DUMP_DH_COMPRESSED_SNAPPY;
1095 }
1096#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001097 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +08001098
1099 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001100 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +08001101 goto out;
1102 }
1103
1104 /* write sub header */
1105 size = sizeof(KdumpSubHeader64);
1106 kh = g_malloc0(size);
1107
1108 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001109 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +01001110 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001111 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +08001112
1113 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +02001114 if (s->guest_note &&
1115 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1116 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1117
1118 get_note_sizes(s, s->guest_note,
1119 &hsize, &name_size, &size_vmcoreinfo_desc);
1120 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1121 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1122 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1123 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
1124 }
1125
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001126 kh->offset_note = cpu_to_dump64(s, offset_note);
1127 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001128
1129 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
1130 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001131 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001132 goto out;
1133 }
1134
1135 /* write note */
1136 s->note_buf = g_malloc0(s->note_size);
1137 s->note_buf_offset = 0;
1138
1139 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001140 write_elf64_notes(buf_write_note, s, errp);
1141 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001142 goto out;
1143 }
1144
1145 if (write_buffer(s->fd, offset_note, s->note_buf,
1146 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001147 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001148 goto out;
1149 }
1150
1151 /* get offset of dump_bitmap */
1152 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1153 block_size;
1154
1155 /* get offset of page */
1156 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1157 block_size;
1158
1159out:
1160 g_free(dh);
1161 g_free(kh);
1162 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001163}
1164
zhanghailiang4c7e2512014-10-09 14:13:11 +08001165static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001166{
Janosch Frank05bbaa502022-03-30 12:36:00 +00001167 if (dump_is_64bit(s)) {
Markus Armbruster992861f2020-07-07 18:06:04 +02001168 create_header64(s, errp);
Janosch Frank05bbaa502022-03-30 12:36:00 +00001169 } else {
1170 create_header32(s, errp);
zhanghailiang4c7e2512014-10-09 14:13:11 +08001171 }
qiaonuohan298f1162014-02-18 14:11:32 +08001172}
1173
Andrew Jones8161bef2016-01-11 20:56:20 +01001174static size_t dump_bitmap_get_bufsize(DumpState *s)
1175{
1176 return s->dump_info.page_size;
1177}
1178
qiaonuohand0686c72014-02-18 14:11:33 +08001179/*
1180 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1181 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1182 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1183 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1184 * vmcore, ie. synchronizing un-sync bit into vmcore.
1185 */
1186static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1187 uint8_t *buf, DumpState *s)
1188{
1189 off_t old_offset, new_offset;
1190 off_t offset_bitmap1, offset_bitmap2;
1191 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001192 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1193 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001194
1195 /* should not set the previous place */
1196 assert(last_pfn <= pfn);
1197
1198 /*
1199 * if the bit needed to be set is not cached in buf, flush the data in buf
1200 * to vmcore firstly.
1201 * making new_offset be bigger than old_offset can also sync remained data
1202 * into vmcore.
1203 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001204 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1205 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001206
1207 while (old_offset < new_offset) {
1208 /* calculate the offset and write dump_bitmap */
1209 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1210 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001211 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001212 return -1;
1213 }
1214
1215 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1216 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1217 old_offset;
1218 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001219 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001220 return -1;
1221 }
1222
Andrew Jones8161bef2016-01-11 20:56:20 +01001223 memset(buf, 0, bitmap_bufsize);
1224 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001225 }
1226
1227 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001228 byte = (pfn % bits_per_buf) / CHAR_BIT;
1229 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001230 if (value) {
1231 buf[byte] |= 1u << bit;
1232 } else {
1233 buf[byte] &= ~(1u << bit);
1234 }
1235
1236 return 0;
1237}
1238
Andrew Jones8161bef2016-01-11 20:56:20 +01001239static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1240{
1241 int target_page_shift = ctz32(s->dump_info.page_size);
1242
1243 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1244}
1245
1246static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1247{
1248 int target_page_shift = ctz32(s->dump_info.page_size);
1249
1250 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1251}
1252
qiaonuohand0686c72014-02-18 14:11:33 +08001253/*
Marc-André Lureau94d78842022-09-05 16:06:21 +04001254 * Return the page frame number and the page content in *bufptr. bufptr can be
1255 * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
1256 * memory. This is not necessarily the memory returned.
qiaonuohand0686c72014-02-18 14:11:33 +08001257 */
1258static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1259 uint8_t **bufptr, DumpState *s)
1260{
1261 GuestPhysBlock *block = *blockptr;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001262 uint32_t page_size = s->dump_info.page_size;
1263 uint8_t *buf = NULL, *hbuf;
1264 hwaddr addr;
qiaonuohand0686c72014-02-18 14:11:33 +08001265
1266 /* block == NULL means the start of the iteration */
1267 if (!block) {
1268 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1269 *blockptr = block;
Marc-André Lureau08df3432022-08-25 12:40:12 +04001270 addr = block->target_start;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001271 *pfnptr = dump_paddr_to_pfn(s, addr);
Marc-André Lureau08df3432022-08-25 12:40:12 +04001272 } else {
Marc-André Lureau94d78842022-09-05 16:06:21 +04001273 *pfnptr += 1;
1274 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001275 }
Marc-André Lureau08df3432022-08-25 12:40:12 +04001276 assert(block != NULL);
qiaonuohand0686c72014-02-18 14:11:33 +08001277
Marc-André Lureau94d78842022-09-05 16:06:21 +04001278 while (1) {
1279 if (addr >= block->target_start && addr < block->target_end) {
1280 size_t n = MIN(block->target_end - addr, page_size - addr % page_size);
1281 hbuf = block->host_addr + (addr - block->target_start);
1282 if (!buf) {
1283 if (n == page_size) {
1284 /* this is a whole target page, go for it */
1285 assert(addr % page_size == 0);
1286 buf = hbuf;
1287 break;
1288 } else if (bufptr) {
1289 assert(*bufptr);
1290 buf = *bufptr;
1291 memset(buf, 0, page_size);
1292 } else {
1293 return true;
1294 }
1295 }
1296
1297 memcpy(buf + addr % page_size, hbuf, n);
1298 addr += n;
1299 if (addr % page_size == 0) {
1300 /* we filled up the page */
1301 break;
1302 }
1303 } else {
1304 /* the next page is in the next block */
1305 *blockptr = block = QTAILQ_NEXT(block, next);
1306 if (!block) {
1307 break;
1308 }
1309
1310 addr = block->target_start;
1311 /* are we still in the same page? */
1312 if (dump_paddr_to_pfn(s, addr) != *pfnptr) {
1313 if (buf) {
1314 /* no, but we already filled something earlier, return it */
1315 break;
1316 } else {
1317 /* else continue from there */
1318 *pfnptr = dump_paddr_to_pfn(s, addr);
1319 }
1320 }
qiaonuohand0686c72014-02-18 14:11:33 +08001321 }
qiaonuohand0686c72014-02-18 14:11:33 +08001322 }
1323
1324 if (bufptr) {
1325 *bufptr = buf;
1326 }
1327
Marc-André Lureau94d78842022-09-05 16:06:21 +04001328 return buf != NULL;
qiaonuohand0686c72014-02-18 14:11:33 +08001329}
1330
zhanghailiang4c7e2512014-10-09 14:13:11 +08001331static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001332{
1333 int ret = 0;
1334 uint64_t last_pfn, pfn;
1335 void *dump_bitmap_buf;
1336 size_t num_dumpable;
1337 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001338 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1339 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001340
1341 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001342 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001343
1344 num_dumpable = 0;
1345 last_pfn = 0;
1346
1347 /*
1348 * exam memory page by page, and set the bit in dump_bitmap corresponded
1349 * to the existing page.
1350 */
1351 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1352 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1353 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001354 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001355 goto out;
1356 }
1357
1358 last_pfn = pfn;
1359 num_dumpable++;
1360 }
1361
1362 /*
1363 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001364 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1365 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001366 */
1367 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001368 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001369 dump_bitmap_buf, s);
1370 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001371 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001372 goto out;
1373 }
1374 }
1375
1376 /* number of dumpable pages that will be dumped later */
1377 s->num_dumpable = num_dumpable;
1378
1379out:
1380 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001381}
1382
qiaonuohan64cfba62014-02-18 14:11:34 +08001383static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1384 off_t offset)
1385{
1386 data_cache->fd = s->fd;
1387 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001388 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1389 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001390 data_cache->offset = offset;
1391}
1392
1393static int write_cache(DataCache *dc, const void *buf, size_t size,
1394 bool flag_sync)
1395{
1396 /*
1397 * dc->buf_size should not be less than size, otherwise dc will never be
1398 * enough
1399 */
1400 assert(size <= dc->buf_size);
1401
1402 /*
1403 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1404 * otherwise check if the space is enough for caching data in buf, if not,
1405 * write the data in dc->buf to dc->fd and reset dc->buf
1406 */
1407 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1408 (flag_sync && dc->data_size > 0)) {
1409 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1410 return -1;
1411 }
1412
1413 dc->offset += dc->data_size;
1414 dc->data_size = 0;
1415 }
1416
1417 if (!flag_sync) {
1418 memcpy(dc->buf + dc->data_size, buf, size);
1419 dc->data_size += size;
1420 }
1421
1422 return 0;
1423}
1424
1425static void free_data_cache(DataCache *data_cache)
1426{
1427 g_free(data_cache->buf);
1428}
1429
qiaonuohand12f57e2014-02-18 14:11:35 +08001430static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1431{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001432 switch (flag_compress) {
1433 case DUMP_DH_COMPRESSED_ZLIB:
1434 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001435
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001436 case DUMP_DH_COMPRESSED_LZO:
1437 /*
1438 * LZO will expand incompressible data by a little amount. Please check
1439 * the following URL to see the expansion calculation:
1440 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1441 */
1442 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001443
1444#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001445 case DUMP_DH_COMPRESSED_SNAPPY:
1446 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001447#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001448 }
1449 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001450}
1451
zhanghailiang4c7e2512014-10-09 14:13:11 +08001452static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001453{
1454 int ret = 0;
1455 DataCache page_desc, page_data;
1456 size_t len_buf_out, size_out;
1457#ifdef CONFIG_LZO
1458 lzo_bytep wrkmem = NULL;
1459#endif
1460 uint8_t *buf_out = NULL;
1461 off_t offset_desc, offset_data;
1462 PageDescriptor pd, pd_zero;
1463 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001464 GuestPhysBlock *block_iter = NULL;
1465 uint64_t pfn_iter;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001466 g_autofree uint8_t *page = NULL;
qiaonuohand12f57e2014-02-18 14:11:35 +08001467
1468 /* get offset of page_desc and page_data in dump file */
1469 offset_desc = s->offset_page;
1470 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1471
1472 prepare_data_cache(&page_desc, s, offset_desc);
1473 prepare_data_cache(&page_data, s, offset_data);
1474
1475 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001476 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001477 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001478
1479#ifdef CONFIG_LZO
1480 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1481#endif
1482
1483 buf_out = g_malloc(len_buf_out);
1484
1485 /*
1486 * init zero page's page_desc and page_data, because every zero page
1487 * uses the same page_data
1488 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001489 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001490 pd_zero.flags = cpu_to_dump32(s, 0);
1491 pd_zero.offset = cpu_to_dump64(s, offset_data);
1492 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001493 buf = g_malloc0(s->dump_info.page_size);
1494 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001495 g_free(buf);
1496 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001497 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001498 goto out;
1499 }
1500
Andrew Jones8161bef2016-01-11 20:56:20 +01001501 offset_data += s->dump_info.page_size;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001502 page = g_malloc(s->dump_info.page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001503
1504 /*
1505 * dump memory to vmcore page by page. zero page will all be resided in the
1506 * first page of page section
1507 */
Marc-André Lureau94d78842022-09-05 16:06:21 +04001508 for (buf = page; get_next_page(&block_iter, &pfn_iter, &buf, s); buf = page) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001509 /* check zero page */
Juan Quintelaf13f22b2021-11-18 15:57:44 +01001510 if (buffer_is_zero(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001511 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1512 false);
1513 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001514 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001515 goto out;
1516 }
1517 } else {
1518 /*
1519 * not zero page, then:
1520 * 1. compress the page
1521 * 2. write the compressed page into the cache of page_data
1522 * 3. get page desc of the compressed page and write it into the
1523 * cache of page_desc
1524 *
1525 * only one compression format will be used here, for
1526 * s->flag_compress is set. But when compression fails to work,
1527 * we fall back to save in plaintext.
1528 */
1529 size_out = len_buf_out;
1530 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001531 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001532 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1533 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001534 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1535 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001536
1537 ret = write_cache(&page_data, buf_out, size_out, false);
1538 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001539 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001540 goto out;
1541 }
1542#ifdef CONFIG_LZO
1543 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001544 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001545 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001546 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001547 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1548 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001549
1550 ret = write_cache(&page_data, buf_out, size_out, false);
1551 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001552 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001553 goto out;
1554 }
1555#endif
1556#ifdef CONFIG_SNAPPY
1557 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001558 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001559 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001560 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001561 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1562 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001563
1564 ret = write_cache(&page_data, buf_out, size_out, false);
1565 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001566 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001567 goto out;
1568 }
1569#endif
1570 } else {
1571 /*
1572 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001573 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001574 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001575 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001576 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001577 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001578
Andrew Jones8161bef2016-01-11 20:56:20 +01001579 ret = write_cache(&page_data, buf,
1580 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001581 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001582 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001583 goto out;
1584 }
1585 }
1586
1587 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001588 pd.page_flags = cpu_to_dump64(s, 0);
1589 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001590 offset_data += size_out;
1591
1592 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1593 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001594 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001595 goto out;
1596 }
1597 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001598 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001599 }
1600
1601 ret = write_cache(&page_desc, NULL, 0, true);
1602 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001603 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001604 goto out;
1605 }
1606 ret = write_cache(&page_data, NULL, 0, true);
1607 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001608 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001609 goto out;
1610 }
1611
1612out:
1613 free_data_cache(&page_desc);
1614 free_data_cache(&page_data);
1615
1616#ifdef CONFIG_LZO
1617 g_free(wrkmem);
1618#endif
1619
1620 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001621}
1622
zhanghailiang4c7e2512014-10-09 14:13:11 +08001623static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001624{
Janosch Frank86a518b2022-03-30 12:35:55 +00001625 ERRP_GUARD();
qiaonuohanb53ccc32014-02-18 14:11:36 +08001626 int ret;
1627
1628 /*
1629 * the kdump-compressed format is:
1630 * File offset
1631 * +------------------------------------------+ 0x0
1632 * | main header (struct disk_dump_header) |
1633 * |------------------------------------------+ block 1
1634 * | sub header (struct kdump_sub_header) |
1635 * |------------------------------------------+ block 2
1636 * | 1st-dump_bitmap |
1637 * |------------------------------------------+ block 2 + X blocks
1638 * | 2nd-dump_bitmap | (aligned by block)
1639 * |------------------------------------------+ block 2 + 2 * X blocks
1640 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1641 * | page desc for pfn 1 (struct page_desc) |
1642 * | : |
1643 * |------------------------------------------| (not aligned by block)
1644 * | page data (pfn 0) |
1645 * | page data (pfn 1) |
1646 * | : |
1647 * +------------------------------------------+
1648 */
1649
1650 ret = write_start_flat_header(s->fd);
1651 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001652 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001653 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001654 }
1655
Janosch Frank86a518b2022-03-30 12:35:55 +00001656 write_dump_header(s, errp);
1657 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001658 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001659 }
1660
Janosch Frank86a518b2022-03-30 12:35:55 +00001661 write_dump_bitmap(s, errp);
1662 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001663 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001664 }
1665
Janosch Frank86a518b2022-03-30 12:35:55 +00001666 write_dump_pages(s, errp);
1667 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001668 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001669 }
1670
1671 ret = write_end_flat_header(s->fd);
1672 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001673 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001674 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001675 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001676}
1677
Janosch Frank0c2994a2022-08-11 12:10:57 +00001678static int validate_start_block(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +08001679{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001680 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001681
Janosch Frankdddf7252022-08-11 12:10:58 +00001682 if (!dump_has_filter(s)) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001683 return 0;
1684 }
1685
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001686 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frank0c2994a2022-08-11 12:10:57 +00001687 /* This block is out of the range */
Janosch Frankdddf7252022-08-11 12:10:58 +00001688 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
1689 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001690 continue;
1691 }
Janosch Frank0c2994a2022-08-11 12:10:57 +00001692 return 0;
1693 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001694
1695 return -1;
1696}
1697
qiaonuohan7aad2482014-02-18 14:11:31 +08001698static void get_max_mapnr(DumpState *s)
1699{
1700 GuestPhysBlock *last_block;
1701
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001702 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001703 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001704}
1705
Peter Xubaf28f52016-02-18 13:16:48 +08001706static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1707
1708static void dump_state_prepare(DumpState *s)
1709{
1710 /* zero the struct, setting status to active */
1711 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1712}
1713
Marc-André Lureau544803c2022-03-23 19:57:31 +04001714bool qemu_system_dump_in_progress(void)
Peter Xu65d64f32016-02-18 13:16:49 +08001715{
1716 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001717 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001718}
1719
Janosch Frankc370d532022-08-11 12:10:59 +00001720/*
1721 * calculate total size of memory to be dumped (taking filter into
1722 * account.)
1723 */
Peter Xu2264c2c2016-02-18 13:16:53 +08001724static int64_t dump_calculate_size(DumpState *s)
1725{
1726 GuestPhysBlock *block;
Janosch Frankc370d532022-08-11 12:10:59 +00001727 int64_t total = 0;
Peter Xu2264c2c2016-02-18 13:16:53 +08001728
1729 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankc370d532022-08-11 12:10:59 +00001730 total += dump_filtered_memblock_size(block,
1731 s->filter_area_begin,
1732 s->filter_area_length);
Peter Xu2264c2c2016-02-18 13:16:53 +08001733 }
1734
1735 return total;
1736}
1737
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001738static void vmcoreinfo_update_phys_base(DumpState *s)
1739{
1740 uint64_t size, note_head_size, name_size, phys_base;
1741 char **lines;
1742 uint8_t *vmci;
1743 size_t i;
1744
1745 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1746 return;
1747 }
1748
1749 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1750 note_head_size = ROUND_UP(note_head_size, 4);
1751
1752 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1753 *(vmci + size) = '\0';
1754
1755 lines = g_strsplit((char *)vmci, "\n", -1);
1756 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001757 const char *prefix = NULL;
1758
1759 if (s->dump_info.d_machine == EM_X86_64) {
1760 prefix = "NUMBER(phys_base)=";
1761 } else if (s->dump_info.d_machine == EM_AARCH64) {
1762 prefix = "NUMBER(PHYS_OFFSET)=";
1763 }
1764
1765 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1766 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001767 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001768 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001769 } else {
1770 s->dump_info.phys_base = phys_base;
1771 }
1772 break;
1773 }
1774 }
1775
1776 g_strfreev(lines);
1777}
1778
zhanghailiang4c7e2512014-10-09 14:13:11 +08001779static void dump_init(DumpState *s, int fd, bool has_format,
1780 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1781 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001782{
Janosch Frank86a518b2022-03-30 12:35:55 +00001783 ERRP_GUARD();
Marc-André Lureau903ef732017-09-11 18:59:25 +02001784 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001785 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001786 int nr_cpus;
1787 int ret;
1788
Peter Xuca1fc8c2016-02-18 13:16:50 +08001789 s->has_format = has_format;
1790 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001791 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001792
qiaonuohanb53ccc32014-02-18 14:11:36 +08001793 /* kdump-compressed is conflict with paging and filter */
1794 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1795 assert(!paging && !has_filter);
1796 }
1797
Wen Congyang783e9b42012-05-07 12:10:47 +08001798 if (runstate_is_running()) {
1799 vm_stop(RUN_STATE_SAVE_VM);
1800 s->resume = true;
1801 } else {
1802 s->resume = false;
1803 }
1804
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001805 /* If we use KVM, we should synchronize the registers before we get dump
1806 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001807 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001808 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001809 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001810 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001811 nr_cpus++;
1812 }
1813
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001814 s->fd = fd;
Janosch Frankdddf7252022-08-11 12:10:58 +00001815 if (has_filter && !length) {
1816 error_setg(errp, QERR_INVALID_PARAMETER, "length");
1817 goto cleanup;
1818 }
1819 s->filter_area_begin = begin;
1820 s->filter_area_length = length;
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001821
Janosch Frank9b722242022-10-17 11:32:10 +00001822 /* First index is 0, it's the special null name */
1823 s->string_table_buf = g_array_new(FALSE, TRUE, 1);
1824 /*
1825 * Allocate the null name, due to the clearing option set to true
1826 * it will be 0.
1827 */
1828 g_array_set_size(s->string_table_buf, 1);
1829
Chen Gang29282072014-08-03 23:28:56 +08001830 memory_mapping_list_init(&s->list);
1831
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001832 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001833 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001834 s->total_size = dump_calculate_size(s);
1835#ifdef DEBUG_DUMP_GUEST_MEMORY
1836 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1837#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001838
Cornelia Huckd1e69942017-09-13 16:20:35 +02001839 /* it does not make sense to dump non-existent memory */
1840 if (!s->total_size) {
1841 error_setg(errp, "dump: no guest memory to dump");
1842 goto cleanup;
1843 }
1844
Janosch Frank0c2994a2022-08-11 12:10:57 +00001845 /* Is the filter filtering everything? */
1846 if (validate_start_block(s) == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001847 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001848 goto cleanup;
1849 }
1850
1851 /* get dump info: endian, class and architecture.
1852 * If the target architecture is not supported, cpu_get_dump_info() will
1853 * return -1.
1854 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001855 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001856 if (ret < 0) {
Markus Armbrusterf969c622023-02-07 08:51:05 +01001857 error_setg(errp,
1858 "dumping guest memory is not supported on this target");
Wen Congyang783e9b42012-05-07 12:10:47 +08001859 goto cleanup;
1860 }
1861
Andrew Jones8161bef2016-01-11 20:56:20 +01001862 if (!s->dump_info.page_size) {
1863 s->dump_info.page_size = TARGET_PAGE_SIZE;
1864 }
1865
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001866 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1867 s->dump_info.d_machine, nr_cpus);
Aneesh Kumar K.Vbb6b6842013-10-01 21:49:32 +05301868 if (s->note_size < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001869 error_setg(errp, QERR_UNSUPPORTED);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001870 goto cleanup;
1871 }
1872
Marc-André Lureau903ef732017-09-11 18:59:25 +02001873 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001874 * The goal of this block is to (a) update the previously guessed
1875 * phys_base, (b) copy the guest note out of the guest.
1876 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001877 */
1878 if (vmci) {
1879 uint64_t addr, note_head_size, name_size, desc_size;
1880 uint32_t size;
1881 uint16_t format;
1882
Janosch Frank05bbaa502022-03-30 12:36:00 +00001883 note_head_size = dump_is_64bit(s) ?
1884 sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001885
1886 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1887 size = le32_to_cpu(vmci->vmcoreinfo.size);
1888 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1889 if (!vmci->has_vmcoreinfo) {
1890 warn_report("guest note is not present");
1891 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1892 warn_report("guest note size is invalid: %" PRIu32, size);
Marc-André Lureau5be5df72018-08-17 17:59:10 +02001893 } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
Marc-André Lureau903ef732017-09-11 18:59:25 +02001894 warn_report("guest note format is unsupported: %" PRIu16, format);
1895 } else {
1896 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1897 cpu_physical_memory_read(addr, s->guest_note, size);
1898
1899 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1900 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1901 desc_size);
1902 if (name_size > MAX_GUEST_NOTE_SIZE ||
1903 desc_size > MAX_GUEST_NOTE_SIZE ||
1904 s->guest_note_size > size) {
1905 warn_report("Invalid guest note header");
1906 g_free(s->guest_note);
1907 s->guest_note = NULL;
1908 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001909 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001910 s->note_size += s->guest_note_size;
1911 }
1912 }
1913 }
1914
Wen Congyang783e9b42012-05-07 12:10:47 +08001915 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001916 if (paging) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001917 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1918 if (*errp) {
Andreas Färber11ed09c2013-05-29 21:54:03 +02001919 goto cleanup;
1920 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001921 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001922 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001923 }
1924
qiaonuohan7aad2482014-02-18 14:11:31 +08001925 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001926
1927 get_max_mapnr(s);
1928
1929 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001930 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1931 s->dump_info.page_size);
1932 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001933
qiaonuohanb53ccc32014-02-18 14:11:36 +08001934 /* init for kdump-compressed format */
1935 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1936 switch (format) {
1937 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1938 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1939 break;
1940
1941 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001942#ifdef CONFIG_LZO
1943 if (lzo_init() != LZO_E_OK) {
1944 error_setg(errp, "failed to initialize the LZO library");
1945 goto cleanup;
1946 }
1947#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001948 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1949 break;
1950
1951 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1952 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1953 break;
1954
1955 default:
1956 s->flag_compress = 0;
1957 }
1958
zhanghailiang4c7e2512014-10-09 14:13:11 +08001959 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001960 }
1961
Janosch Frankdddf7252022-08-11 12:10:58 +00001962 if (dump_has_filter(s)) {
1963 memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +08001964 }
1965
1966 /*
Janosch Frank9b722242022-10-17 11:32:10 +00001967 * The first section header is always a special one in which most
1968 * fields are 0. The section header string table is also always
1969 * set.
Wen Congyang783e9b42012-05-07 12:10:47 +08001970 */
Janosch Frank9b722242022-10-17 11:32:10 +00001971 s->shdr_num = 2;
Wen Congyang783e9b42012-05-07 12:10:47 +08001972
Janosch Frank9b722242022-10-17 11:32:10 +00001973 /*
1974 * Adds the number of architecture sections to shdr_num and sets
1975 * elf_section_data_size so we know the offsets and sizes of all
1976 * parts.
1977 */
1978 if (s->dump_info.arch_sections_add_fn) {
1979 s->dump_info.arch_sections_add_fn(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001980 }
1981
Janosch Frank9b722242022-10-17 11:32:10 +00001982 /*
1983 * calculate shdr_num so we know the offsets and sizes of all
1984 * parts.
1985 * Calculate phdr_num
1986 *
1987 * The absolute maximum amount of phdrs is UINT32_MAX - 1 as
1988 * sh_info is 32 bit. There's special handling once we go over
1989 * UINT16_MAX - 1 but that is handled in the ehdr and section
1990 * code.
1991 */
1992 s->phdr_num = 1; /* Reserve PT_NOTE */
1993 if (s->list.num <= UINT32_MAX - 1) {
1994 s->phdr_num += s->list.num;
1995 } else {
1996 s->phdr_num = UINT32_MAX;
1997 }
1998
1999 /*
2000 * Now that the number of section and program headers is known we
2001 * can calculate the offsets of the headers and data.
2002 */
Janosch Frank05bbaa502022-03-30 12:36:00 +00002003 if (dump_is_64bit(s)) {
Janosch Frankcb415fd2022-10-17 08:38:14 +00002004 s->shdr_offset = sizeof(Elf64_Ehdr);
2005 s->phdr_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
2006 s->note_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08002007 } else {
Janosch Frankcb415fd2022-10-17 08:38:14 +00002008 s->shdr_offset = sizeof(Elf32_Ehdr);
2009 s->phdr_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
2010 s->note_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08002011 }
Janosch Frank13fd4172022-10-17 08:38:16 +00002012 s->memory_offset = s->note_offset + s->note_size;
2013 s->section_offset = s->memory_offset + s->total_size;
Wen Congyang783e9b42012-05-07 12:10:47 +08002014
zhanghailiang4c7e2512014-10-09 14:13:11 +08002015 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08002016
2017cleanup:
Chen Gang29282072014-08-03 23:28:56 +08002018 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002019}
2020
Peter Xuca1fc8c2016-02-18 13:16:50 +08002021/* this operation might be time consuming. */
2022static void dump_process(DumpState *s, Error **errp)
2023{
Janosch Frank86a518b2022-03-30 12:35:55 +00002024 ERRP_GUARD();
Peter Xud42a0d12016-02-18 13:16:56 +08002025 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08002026
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002027 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
2028#ifdef TARGET_X86_64
Janosch Frank86a518b2022-03-30 12:35:55 +00002029 create_win_dump(s, errp);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002030#endif
2031 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Janosch Frank86a518b2022-03-30 12:35:55 +00002032 create_kdump_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08002033 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +00002034 create_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08002035 }
2036
Peter Xu39ba2ea2016-02-18 13:16:54 +08002037 /* make sure status is written after written_size updates */
2038 smp_wmb();
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002039 qatomic_set(&s->status,
Janosch Frank86a518b2022-03-30 12:35:55 +00002040 (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08002041
Peter Xud42a0d12016-02-18 13:16:56 +08002042 /* send DUMP_COMPLETED message (unconditionally) */
2043 result = qmp_query_dump(NULL);
2044 /* should never fail */
2045 assert(result);
Markus Armbrusterd4f8bdc2022-11-04 17:06:55 +01002046 qapi_event_send_dump_completed(result,
2047 *errp ? error_get_pretty(*errp) : NULL);
Peter Xud42a0d12016-02-18 13:16:56 +08002048 qapi_free_DumpQueryResult(result);
2049
Peter Xuca1fc8c2016-02-18 13:16:50 +08002050 dump_cleanup(s);
2051}
2052
Peter Xu1fbeff72016-02-18 13:16:52 +08002053static void *dump_thread(void *data)
2054{
Peter Xu1fbeff72016-02-18 13:16:52 +08002055 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03002056 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08002057 return NULL;
2058}
2059
Peter Xu39ba2ea2016-02-18 13:16:54 +08002060DumpQueryResult *qmp_query_dump(Error **errp)
2061{
2062 DumpQueryResult *result = g_new(DumpQueryResult, 1);
2063 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002064 result->status = qatomic_read(&state->status);
Peter Xu39ba2ea2016-02-18 13:16:54 +08002065 /* make sure we are reading status and written_size in order */
2066 smp_rmb();
2067 result->completed = state->written_size;
2068 result->total = state->total_size;
2069 return result;
2070}
2071
Peter Xu228de9c2016-02-18 13:16:47 +08002072void qmp_dump_guest_memory(bool paging, const char *file,
2073 bool has_detach, bool detach,
2074 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08002075 int64_t length, bool has_format,
2076 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08002077{
Janosch Frank86a518b2022-03-30 12:35:55 +00002078 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +08002079 const char *p;
2080 int fd = -1;
2081 DumpState *s;
Peter Xu1fbeff72016-02-18 13:16:52 +08002082 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08002083
Peter Xu63e27f22016-02-18 13:16:51 +08002084 if (runstate_check(RUN_STATE_INMIGRATE)) {
2085 error_setg(errp, "Dump not allowed during incoming migration.");
2086 return;
2087 }
2088
Peter Xu65d64f32016-02-18 13:16:49 +08002089 /* if there is a dump in background, we should wait until the dump
2090 * finished */
Marc-André Lureau544803c2022-03-23 19:57:31 +04002091 if (qemu_system_dump_in_progress()) {
Peter Xu65d64f32016-02-18 13:16:49 +08002092 error_setg(errp, "There is a dump in process, please wait.");
2093 return;
2094 }
2095
qiaonuohanb53ccc32014-02-18 14:11:36 +08002096 /*
2097 * kdump-compressed format need the whole memory dumped, so paging or
2098 * filter is not supported here.
2099 */
2100 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
2101 (paging || has_begin || has_length)) {
2102 error_setg(errp, "kdump-compressed format doesn't support paging or "
2103 "filter");
2104 return;
2105 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002106 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002107 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08002108 return;
2109 }
2110 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002111 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08002112 return;
2113 }
Peter Xu1fbeff72016-02-18 13:16:52 +08002114 if (has_detach) {
2115 detach_p = detach;
2116 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002117
qiaonuohanb53ccc32014-02-18 14:11:36 +08002118 /* check whether lzo/snappy is supported */
2119#ifndef CONFIG_LZO
2120 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
2121 error_setg(errp, "kdump-lzo is not available now");
2122 return;
2123 }
2124#endif
2125
2126#ifndef CONFIG_SNAPPY
2127 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
2128 error_setg(errp, "kdump-snappy is not available now");
2129 return;
2130 }
2131#endif
2132
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002133#ifndef TARGET_X86_64
2134 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
2135 error_setg(errp, "Windows dump is only available for x86-64");
2136 return;
2137 }
2138#endif
2139
Wen Congyang783e9b42012-05-07 12:10:47 +08002140#if !defined(WIN32)
2141 if (strstart(file, "fd:", &p)) {
Kevin Wolf947e4742020-10-05 17:58:44 +02002142 fd = monitor_get_fd(monitor_cur(), p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08002143 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08002144 return;
2145 }
2146 }
2147#endif
2148
2149 if (strstart(file, "file:", &p)) {
Daniel P. Berrangé448058a2020-07-21 13:25:21 +01002150 fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
Wen Congyang783e9b42012-05-07 12:10:47 +08002151 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04002152 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08002153 return;
2154 }
2155 }
2156
2157 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002158 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08002159 return;
2160 }
2161
Peter Xub7bc6b12021-09-22 12:20:09 -04002162 if (!dump_migration_blocker) {
2163 error_setg(&dump_migration_blocker,
2164 "Live migration disabled: dump-guest-memory in progress");
2165 }
2166
2167 /*
2168 * Allows even for -only-migratable, but forbid migration during the
2169 * process of dump guest memory.
2170 */
2171 if (migrate_add_blocker_internal(dump_migration_blocker, errp)) {
2172 /* Remember to release the fd before passing it over to dump state */
2173 close(fd);
2174 return;
2175 }
2176
Peter Xubaf28f52016-02-18 13:16:48 +08002177 s = &dump_state_global;
2178 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002179
zhanghailiang4c7e2512014-10-09 14:13:11 +08002180 dump_init(s, fd, has_format, format, paging, has_begin,
Janosch Frank86a518b2022-03-30 12:35:55 +00002181 begin, length, errp);
2182 if (*errp) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002183 qatomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08002184 return;
2185 }
2186
Peter Xu1fbeff72016-02-18 13:16:52 +08002187 if (detach_p) {
2188 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08002189 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08002190 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2191 s, QEMU_THREAD_DETACHED);
2192 } else {
2193 /* sync dump */
2194 dump_process(s, errp);
2195 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002196}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002197
2198DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2199{
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002200 DumpGuestMemoryCapability *cap =
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002201 g_new0(DumpGuestMemoryCapability, 1);
Eric Blake95b3a8c2021-01-13 16:10:13 -06002202 DumpGuestMemoryFormatList **tail = &cap->formats;
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002203
2204 /* elf is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002205 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002206
2207 /* kdump-zlib is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002208 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002209
2210 /* add new item if kdump-lzo is available */
2211#ifdef CONFIG_LZO
Eric Blake95b3a8c2021-01-13 16:10:13 -06002212 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002213#endif
2214
2215 /* add new item if kdump-snappy is available */
2216#ifdef CONFIG_SNAPPY
Eric Blake95b3a8c2021-01-13 16:10:13 -06002217 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002218#endif
2219
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002220 /* Windows dump is available only if target is x86_64 */
2221#ifdef TARGET_X86_64
Eric Blake95b3a8c2021-01-13 16:10:13 -06002222 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002223#endif
2224
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002225 return cap;
2226}