blob: fa650980d851ae8eea13b85e4f4a99068e44f823 [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"
Philippe Mathieu-Daudéac978772023-02-23 23:56:46 +010017#include "qemu/bswap.h"
Philippe Mathieu-Daudéc5d40b22023-02-23 23:38:59 +010018#include "exec/target_page.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010019#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010020#include "sysemu/dump.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020021#include "sysemu/runstate.h"
Andreas Färber1b3509c2013-06-09 16:48:29 +020022#include "sysemu/cpus.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010023#include "qapi/error.h"
Markus Armbrusterd06b7472019-06-19 22:10:47 +020024#include "qapi/qapi-commands-dump.h"
25#include "qapi/qapi-events-dump.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010026#include "qapi/qmp/qerror.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020027#include "qemu/main-loop.h"
Marc-André Lureau903ef732017-09-11 18:59:25 +020028#include "hw/misc/vmcoreinfo.h"
Peter Xub7bc6b12021-09-22 12:20:09 -040029#include "migration/blocker.h"
Philippe Mathieu-Daudéac978772023-02-23 23:56:46 +010030#include "hw/core/cpu.h"
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030031#include "win_dump.h"
Viktor Prutyanov2da91b52018-05-17 19:23:39 +030032
qiaonuohand12f57e2014-02-18 14:11:35 +080033#include <zlib.h>
34#ifdef CONFIG_LZO
35#include <lzo/lzo1x.h>
36#endif
37#ifdef CONFIG_SNAPPY
38#include <snappy-c.h>
39#endif
qiaonuohan4ab23a92014-02-18 14:11:37 +080040#ifndef ELF_MACHINE_UNAME
41#define ELF_MACHINE_UNAME "Unknown"
42#endif
qiaonuohand12f57e2014-02-18 14:11:35 +080043
Marc-André Lureau903ef732017-09-11 18:59:25 +020044#define MAX_GUEST_NOTE_SIZE (1 << 20) /* 1MB should be enough */
45
Peter Xub7bc6b12021-09-22 12:20:09 -040046static Error *dump_migration_blocker;
47
Marc-André Lureau903ef732017-09-11 18:59:25 +020048#define ELF_NOTE_SIZE(hdr_size, name_size, desc_size) \
49 ((DIV_ROUND_UP((hdr_size), 4) + \
50 DIV_ROUND_UP((name_size), 4) + \
51 DIV_ROUND_UP((desc_size), 4)) * 4)
52
Janosch Frank05bbaa502022-03-30 12:36:00 +000053static inline bool dump_is_64bit(DumpState *s)
54{
55 return s->dump_info.d_class == ELFCLASS64;
56}
57
Janosch Frankdddf7252022-08-11 12:10:58 +000058static inline bool dump_has_filter(DumpState *s)
59{
60 return s->filter_area_length > 0;
61}
62
Bharata B Raoacb0ef52014-05-19 19:57:44 +020063uint16_t cpu_to_dump16(DumpState *s, uint16_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080064{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020065 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080066 val = cpu_to_le16(val);
67 } else {
68 val = cpu_to_be16(val);
69 }
70
71 return val;
72}
73
Bharata B Raoacb0ef52014-05-19 19:57:44 +020074uint32_t cpu_to_dump32(DumpState *s, uint32_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080075{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020076 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080077 val = cpu_to_le32(val);
78 } else {
79 val = cpu_to_be32(val);
80 }
81
82 return val;
83}
84
Bharata B Raoacb0ef52014-05-19 19:57:44 +020085uint64_t cpu_to_dump64(DumpState *s, uint64_t val)
Wen Congyang783e9b42012-05-07 12:10:47 +080086{
Bharata B Raoacb0ef52014-05-19 19:57:44 +020087 if (s->dump_info.d_endian == ELFDATA2LSB) {
Wen Congyang783e9b42012-05-07 12:10:47 +080088 val = cpu_to_le64(val);
89 } else {
90 val = cpu_to_be64(val);
91 }
92
93 return val;
94}
95
Wen Congyang783e9b42012-05-07 12:10:47 +080096static int dump_cleanup(DumpState *s)
97{
Laszlo Ersek5ee163e2013-08-06 12:37:09 +020098 guest_phys_blocks_free(&s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +080099 memory_mapping_list_free(&s->list);
Chen Gang29282072014-08-03 23:28:56 +0800100 close(s->fd);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200101 g_free(s->guest_note);
Janosch Frank9b722242022-10-17 11:32:10 +0000102 g_array_unref(s->string_table_buf);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200103 s->guest_note = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +0800104 if (s->resume) {
Fam Zheng6796b402017-05-03 15:28:19 +0800105 if (s->detached) {
106 qemu_mutex_lock_iothread();
107 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800108 vm_start();
Fam Zheng6796b402017-05-03 15:28:19 +0800109 if (s->detached) {
110 qemu_mutex_unlock_iothread();
111 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800112 }
Peter Xub7bc6b12021-09-22 12:20:09 -0400113 migrate_del_blocker(dump_migration_blocker);
Wen Congyang783e9b42012-05-07 12:10:47 +0800114
Chen Gang29282072014-08-03 23:28:56 +0800115 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800116}
117
qiaonuohanb5ba1cc2014-02-18 14:11:25 +0800118static int fd_write_vmcore(const void *buf, size_t size, void *opaque)
Wen Congyang783e9b42012-05-07 12:10:47 +0800119{
120 DumpState *s = opaque;
Luiz Capitulino2f616522012-09-21 13:17:55 -0300121 size_t written_size;
Wen Congyang783e9b42012-05-07 12:10:47 +0800122
Luiz Capitulino2f616522012-09-21 13:17:55 -0300123 written_size = qemu_write_full(s->fd, buf, size);
124 if (written_size != size) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200125 return -errno;
Wen Congyang783e9b42012-05-07 12:10:47 +0800126 }
127
128 return 0;
129}
130
Janosch Frank670e7692022-08-11 12:11:00 +0000131static void prepare_elf64_header(DumpState *s, Elf64_Ehdr *elf_header)
Wen Congyang783e9b42012-05-07 12:10:47 +0800132{
Janosch Frank046bc412022-04-07 09:48:24 +0000133 /*
134 * phnum in the elf header is 16 bit, if we have more segments we
135 * set phnum to PN_XNUM and write the real number of segments to a
136 * special section.
137 */
138 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Wen Congyang783e9b42012-05-07 12:10:47 +0800139
Janosch Frank670e7692022-08-11 12:11:00 +0000140 memset(elf_header, 0, sizeof(Elf64_Ehdr));
141 memcpy(elf_header, ELFMAG, SELFMAG);
142 elf_header->e_ident[EI_CLASS] = ELFCLASS64;
143 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
144 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
145 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
146 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
147 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
148 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
149 elf_header->e_phoff = cpu_to_dump64(s, s->phdr_offset);
150 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf64_Phdr));
151 elf_header->e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank9b722242022-10-17 11:32:10 +0000152 elf_header->e_shoff = cpu_to_dump64(s, s->shdr_offset);
153 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf64_Shdr));
154 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
155 elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
Wen Congyang783e9b42012-05-07 12:10:47 +0800156}
157
Janosch Frank670e7692022-08-11 12:11:00 +0000158static void prepare_elf32_header(DumpState *s, Elf32_Ehdr *elf_header)
Wen Congyang783e9b42012-05-07 12:10:47 +0800159{
Janosch Frank046bc412022-04-07 09:48:24 +0000160 /*
161 * phnum in the elf header is 16 bit, if we have more segments we
162 * set phnum to PN_XNUM and write the real number of segments to a
163 * special section.
164 */
165 uint16_t phnum = MIN(s->phdr_num, PN_XNUM);
Janosch Frank670e7692022-08-11 12:11:00 +0000166
167 memset(elf_header, 0, sizeof(Elf32_Ehdr));
168 memcpy(elf_header, ELFMAG, SELFMAG);
169 elf_header->e_ident[EI_CLASS] = ELFCLASS32;
170 elf_header->e_ident[EI_DATA] = s->dump_info.d_endian;
171 elf_header->e_ident[EI_VERSION] = EV_CURRENT;
172 elf_header->e_type = cpu_to_dump16(s, ET_CORE);
173 elf_header->e_machine = cpu_to_dump16(s, s->dump_info.d_machine);
174 elf_header->e_version = cpu_to_dump32(s, EV_CURRENT);
175 elf_header->e_ehsize = cpu_to_dump16(s, sizeof(elf_header));
176 elf_header->e_phoff = cpu_to_dump32(s, s->phdr_offset);
177 elf_header->e_phentsize = cpu_to_dump16(s, sizeof(Elf32_Phdr));
178 elf_header->e_phnum = cpu_to_dump16(s, phnum);
Janosch Frank9b722242022-10-17 11:32:10 +0000179 elf_header->e_shoff = cpu_to_dump32(s, s->shdr_offset);
180 elf_header->e_shentsize = cpu_to_dump16(s, sizeof(Elf32_Shdr));
181 elf_header->e_shnum = cpu_to_dump16(s, s->shdr_num);
182 elf_header->e_shstrndx = cpu_to_dump16(s, s->shdr_num - 1);
Janosch Frank670e7692022-08-11 12:11:00 +0000183}
184
185static void write_elf_header(DumpState *s, Error **errp)
186{
187 Elf32_Ehdr elf32_header;
188 Elf64_Ehdr elf64_header;
189 size_t header_size;
190 void *header_ptr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800191 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800192
Janosch Frank9b722242022-10-17 11:32:10 +0000193 /* The NULL header and the shstrtab are always defined */
194 assert(s->shdr_num >= 2);
Janosch Frank670e7692022-08-11 12:11:00 +0000195 if (dump_is_64bit(s)) {
196 prepare_elf64_header(s, &elf64_header);
197 header_size = sizeof(elf64_header);
198 header_ptr = &elf64_header;
199 } else {
200 prepare_elf32_header(s, &elf32_header);
201 header_size = sizeof(elf32_header);
202 header_ptr = &elf32_header;
Wen Congyang783e9b42012-05-07 12:10:47 +0800203 }
204
Janosch Frank670e7692022-08-11 12:11:00 +0000205 ret = fd_write_vmcore(header_ptr, header_size, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800206 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200207 error_setg_errno(errp, -ret, "dump: failed to write elf header");
Wen Congyang783e9b42012-05-07 12:10:47 +0800208 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800209}
210
zhanghailiang4c7e2512014-10-09 14:13:11 +0800211static void write_elf64_load(DumpState *s, MemoryMapping *memory_mapping,
212 int phdr_index, hwaddr offset,
213 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800214{
215 Elf64_Phdr phdr;
216 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800217
218 memset(&phdr, 0, sizeof(Elf64_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200219 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
220 phdr.p_offset = cpu_to_dump64(s, offset);
221 phdr.p_paddr = cpu_to_dump64(s, memory_mapping->phys_addr);
222 phdr.p_filesz = cpu_to_dump64(s, filesz);
223 phdr.p_memsz = cpu_to_dump64(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200224 phdr.p_vaddr = cpu_to_dump64(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800225
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200226 assert(memory_mapping->length >= filesz);
227
Wen Congyang783e9b42012-05-07 12:10:47 +0800228 ret = fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s);
229 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200230 error_setg_errno(errp, -ret,
231 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800232 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800233}
234
zhanghailiang4c7e2512014-10-09 14:13:11 +0800235static void write_elf32_load(DumpState *s, MemoryMapping *memory_mapping,
236 int phdr_index, hwaddr offset,
237 hwaddr filesz, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800238{
239 Elf32_Phdr phdr;
240 int ret;
Wen Congyang783e9b42012-05-07 12:10:47 +0800241
242 memset(&phdr, 0, sizeof(Elf32_Phdr));
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200243 phdr.p_type = cpu_to_dump32(s, PT_LOAD);
244 phdr.p_offset = cpu_to_dump32(s, offset);
245 phdr.p_paddr = cpu_to_dump32(s, memory_mapping->phys_addr);
246 phdr.p_filesz = cpu_to_dump32(s, filesz);
247 phdr.p_memsz = cpu_to_dump32(s, memory_mapping->length);
Jon Dorone17bebd2019-01-09 10:22:03 +0200248 phdr.p_vaddr =
249 cpu_to_dump32(s, memory_mapping->virt_addr) ?: phdr.p_paddr;
Wen Congyang783e9b42012-05-07 12:10:47 +0800250
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200251 assert(memory_mapping->length >= filesz);
252
Wen Congyang783e9b42012-05-07 12:10:47 +0800253 ret = fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s);
254 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200255 error_setg_errno(errp, -ret,
256 "dump: failed to write program header table");
Wen Congyang783e9b42012-05-07 12:10:47 +0800257 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800258}
259
Janosch Frank2341a942022-08-11 12:11:01 +0000260static void prepare_elf64_phdr_note(DumpState *s, Elf64_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800261{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000262 memset(phdr, 0, sizeof(*phdr));
263 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
264 phdr->p_offset = cpu_to_dump64(s, s->note_offset);
265 phdr->p_paddr = 0;
266 phdr->p_filesz = cpu_to_dump64(s, s->note_size);
267 phdr->p_memsz = cpu_to_dump64(s, s->note_size);
268 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800269}
270
Paolo Bonzini0bc3cd62013-04-08 17:29:59 +0200271static inline int cpu_index(CPUState *cpu)
272{
273 return cpu->cpu_index + 1;
274}
275
Marc-André Lureau903ef732017-09-11 18:59:25 +0200276static void write_guest_note(WriteCoreDumpFunction f, DumpState *s,
277 Error **errp)
278{
279 int ret;
280
281 if (s->guest_note) {
282 ret = f(s->guest_note, s->guest_note_size, s);
283 if (ret < 0) {
284 error_setg(errp, "dump: failed to write guest note");
285 }
286 }
287}
288
zhanghailiang4c7e2512014-10-09 14:13:11 +0800289static void write_elf64_notes(WriteCoreDumpFunction f, DumpState *s,
290 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800291{
Andreas Färber0d342822012-12-17 07:12:13 +0100292 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800293 int ret;
294 int id;
295
Andreas Färberbdc44642013-06-24 23:50:24 +0200296 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100297 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800298 ret = cpu_write_elf64_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800299 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800300 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800301 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800302 }
303 }
304
Andreas Färberbdc44642013-06-24 23:50:24 +0200305 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800306 ret = cpu_write_elf64_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800307 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800308 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800309 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800310 }
311 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200312
313 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800314}
315
Janosch Frank2341a942022-08-11 12:11:01 +0000316static void prepare_elf32_phdr_note(DumpState *s, Elf32_Phdr *phdr)
Wen Congyang783e9b42012-05-07 12:10:47 +0800317{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000318 memset(phdr, 0, sizeof(*phdr));
319 phdr->p_type = cpu_to_dump32(s, PT_NOTE);
320 phdr->p_offset = cpu_to_dump32(s, s->note_offset);
321 phdr->p_paddr = 0;
322 phdr->p_filesz = cpu_to_dump32(s, s->note_size);
323 phdr->p_memsz = cpu_to_dump32(s, s->note_size);
324 phdr->p_vaddr = 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800325}
326
zhanghailiang4c7e2512014-10-09 14:13:11 +0800327static void write_elf32_notes(WriteCoreDumpFunction f, DumpState *s,
328 Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800329{
Andreas Färber0d342822012-12-17 07:12:13 +0100330 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +0800331 int ret;
332 int id;
333
Andreas Färberbdc44642013-06-24 23:50:24 +0200334 CPU_FOREACH(cpu) {
Andreas Färber0d342822012-12-17 07:12:13 +0100335 id = cpu_index(cpu);
qiaonuohan6a519912014-02-18 14:11:26 +0800336 ret = cpu_write_elf32_note(f, cpu, id, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800337 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800338 error_setg(errp, "dump: failed to write elf notes");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800339 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800340 }
341 }
342
Andreas Färberbdc44642013-06-24 23:50:24 +0200343 CPU_FOREACH(cpu) {
qiaonuohan6a519912014-02-18 14:11:26 +0800344 ret = cpu_write_elf32_qemunote(f, cpu, s);
Wen Congyang783e9b42012-05-07 12:10:47 +0800345 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800346 error_setg(errp, "dump: failed to write CPU status");
zhanghailiang4c7e2512014-10-09 14:13:11 +0800347 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800348 }
349 }
Marc-André Lureau903ef732017-09-11 18:59:25 +0200350
351 write_guest_note(f, s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800352}
353
Janosch Frankbc7d5582022-03-30 12:36:01 +0000354static void write_elf_phdr_note(DumpState *s, Error **errp)
355{
Janosch Frankbc7d5582022-03-30 12:36:01 +0000356 Elf32_Phdr phdr32;
357 Elf64_Phdr phdr64;
358 void *phdr;
359 size_t size;
360 int ret;
361
362 if (dump_is_64bit(s)) {
Janosch Frank2341a942022-08-11 12:11:01 +0000363 prepare_elf64_phdr_note(s, &phdr64);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000364 size = sizeof(phdr64);
365 phdr = &phdr64;
366 } else {
Janosch Frank2341a942022-08-11 12:11:01 +0000367 prepare_elf32_phdr_note(s, &phdr32);
Janosch Frankbc7d5582022-03-30 12:36:01 +0000368 size = sizeof(phdr32);
369 phdr = &phdr32;
370 }
371
372 ret = fd_write_vmcore(phdr, size, s);
373 if (ret < 0) {
374 error_setg_errno(errp, -ret,
375 "dump: failed to write program header table");
376 }
377}
378
Janosch Franke41ed292022-10-17 08:38:13 +0000379static void prepare_elf_section_hdr_zero(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +0800380{
Janosch Franke41ed292022-10-17 08:38:13 +0000381 if (dump_is_64bit(s)) {
382 Elf64_Shdr *shdr64 = s->elf_section_hdrs;
383
384 shdr64->sh_info = cpu_to_dump32(s, s->phdr_num);
385 } else {
386 Elf32_Shdr *shdr32 = s->elf_section_hdrs;
387
388 shdr32->sh_info = cpu_to_dump32(s, s->phdr_num);
389 }
390}
391
Janosch Frank9b722242022-10-17 11:32:10 +0000392static void prepare_elf_section_hdr_string(DumpState *s, void *buff)
393{
394 uint64_t index = s->string_table_buf->len;
395 const char strtab[] = ".shstrtab";
396 Elf32_Shdr shdr32 = {};
397 Elf64_Shdr shdr64 = {};
398 int shdr_size;
399 void *shdr;
400
401 g_array_append_vals(s->string_table_buf, strtab, sizeof(strtab));
402 if (dump_is_64bit(s)) {
403 shdr_size = sizeof(Elf64_Shdr);
404 shdr64.sh_type = SHT_STRTAB;
405 shdr64.sh_offset = s->section_offset + s->elf_section_data_size;
406 shdr64.sh_name = index;
407 shdr64.sh_size = s->string_table_buf->len;
408 shdr = &shdr64;
409 } else {
410 shdr_size = sizeof(Elf32_Shdr);
411 shdr32.sh_type = SHT_STRTAB;
412 shdr32.sh_offset = s->section_offset + s->elf_section_data_size;
413 shdr32.sh_name = index;
414 shdr32.sh_size = s->string_table_buf->len;
415 shdr = &shdr32;
416 }
417 memcpy(buff, shdr, shdr_size);
418}
419
420static bool prepare_elf_section_hdrs(DumpState *s, Error **errp)
Janosch Franke41ed292022-10-17 08:38:13 +0000421{
422 size_t len, sizeof_shdr;
Janosch Frank9b722242022-10-17 11:32:10 +0000423 void *buff_hdr;
Janosch Franke41ed292022-10-17 08:38:13 +0000424
425 /*
426 * Section ordering:
427 * - HDR zero
Janosch Frank9b722242022-10-17 11:32:10 +0000428 * - Arch section hdrs
429 * - String table hdr
Janosch Franke41ed292022-10-17 08:38:13 +0000430 */
431 sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
432 len = sizeof_shdr * s->shdr_num;
433 s->elf_section_hdrs = g_malloc0(len);
Janosch Frank9b722242022-10-17 11:32:10 +0000434 buff_hdr = s->elf_section_hdrs;
Janosch Franke41ed292022-10-17 08:38:13 +0000435
436 /*
437 * The first section header is ALWAYS a special initial section
438 * header.
439 *
440 * The header should be 0 with one exception being that if
441 * phdr_num is PN_XNUM then the sh_info field contains the real
442 * number of segment entries.
443 *
444 * As we zero allocate the buffer we will only need to modify
445 * sh_info for the PN_XNUM case.
446 */
447 if (s->phdr_num >= PN_XNUM) {
448 prepare_elf_section_hdr_zero(s);
449 }
Janosch Frank9b722242022-10-17 11:32:10 +0000450 buff_hdr += sizeof_shdr;
451
452 /* Add architecture defined section headers */
453 if (s->dump_info.arch_sections_write_hdr_fn
454 && s->shdr_num > 2) {
455 buff_hdr += s->dump_info.arch_sections_write_hdr_fn(s, buff_hdr);
456
457 if (s->shdr_num >= SHN_LORESERVE) {
458 error_setg_errno(errp, EINVAL,
459 "dump: too many architecture defined sections");
460 return false;
461 }
462 }
463
464 /*
465 * String table is the last section since strings are added via
466 * arch_sections_write_hdr().
467 */
468 prepare_elf_section_hdr_string(s, buff_hdr);
469 return true;
Janosch Franke41ed292022-10-17 08:38:13 +0000470}
471
472static void write_elf_section_headers(DumpState *s, Error **errp)
473{
474 size_t sizeof_shdr = dump_is_64bit(s) ? sizeof(Elf64_Shdr) : sizeof(Elf32_Shdr);
Wen Congyang783e9b42012-05-07 12:10:47 +0800475 int ret;
476
Janosch Frank9b722242022-10-17 11:32:10 +0000477 if (!prepare_elf_section_hdrs(s, errp)) {
478 return;
479 }
Janosch Franke41ed292022-10-17 08:38:13 +0000480
481 ret = fd_write_vmcore(s->elf_section_hdrs, s->shdr_num * sizeof_shdr, s);
482 if (ret < 0) {
483 error_setg_errno(errp, -ret, "dump: failed to write section headers");
Wen Congyang783e9b42012-05-07 12:10:47 +0800484 }
485
Janosch Franke41ed292022-10-17 08:38:13 +0000486 g_free(s->elf_section_hdrs);
Wen Congyang783e9b42012-05-07 12:10:47 +0800487}
488
Janosch Frank9b722242022-10-17 11:32:10 +0000489static void write_elf_sections(DumpState *s, Error **errp)
490{
491 int ret;
492
493 if (s->elf_section_data_size) {
494 /* Write architecture section data */
495 ret = fd_write_vmcore(s->elf_section_data,
496 s->elf_section_data_size, s);
497 if (ret < 0) {
498 error_setg_errno(errp, -ret,
499 "dump: failed to write architecture section data");
500 return;
501 }
502 }
503
504 /* Write string table */
505 ret = fd_write_vmcore(s->string_table_buf->data,
506 s->string_table_buf->len, s);
507 if (ret < 0) {
508 error_setg_errno(errp, -ret, "dump: failed to write string table data");
509 }
510}
511
zhanghailiang4c7e2512014-10-09 14:13:11 +0800512static void write_data(DumpState *s, void *buf, int length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800513{
514 int ret;
515
516 ret = fd_write_vmcore(buf, length, s);
517 if (ret < 0) {
Yasmin Beatriz0c336592018-02-12 12:25:06 -0200518 error_setg_errno(errp, -ret, "dump: failed to save memory");
Peter Xu2264c2c2016-02-18 13:16:53 +0800519 } else {
520 s->written_size += length;
Wen Congyang783e9b42012-05-07 12:10:47 +0800521 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800522}
523
zhanghailiang4c7e2512014-10-09 14:13:11 +0800524/* write the memory to vmcore. 1 page per I/O. */
525static void write_memory(DumpState *s, GuestPhysBlock *block, ram_addr_t start,
526 int64_t size, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800527{
Janosch Frank86a518b2022-03-30 12:35:55 +0000528 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800529 int64_t i;
Wen Congyang783e9b42012-05-07 12:10:47 +0800530
Andrew Jones8161bef2016-01-11 20:56:20 +0100531 for (i = 0; i < size / s->dump_info.page_size; i++) {
532 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000533 s->dump_info.page_size, errp);
534 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800535 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800536 }
537 }
538
Andrew Jones8161bef2016-01-11 20:56:20 +0100539 if ((size % s->dump_info.page_size) != 0) {
540 write_data(s, block->host_addr + start + i * s->dump_info.page_size,
Janosch Frank86a518b2022-03-30 12:35:55 +0000541 size % s->dump_info.page_size, errp);
542 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800543 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800544 }
545 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800546}
547
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200548/* get the memory's offset and size in the vmcore */
549static void get_offset_range(hwaddr phys_addr,
550 ram_addr_t mapping_length,
551 DumpState *s,
552 hwaddr *p_offset,
553 hwaddr *p_filesz)
Wen Congyang783e9b42012-05-07 12:10:47 +0800554{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200555 GuestPhysBlock *block;
Avi Kivitya8170e52012-10-23 12:30:10 +0200556 hwaddr offset = s->memory_offset;
Wen Congyang783e9b42012-05-07 12:10:47 +0800557 int64_t size_in_block, start;
558
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200559 /* When the memory is not stored into vmcore, offset will be -1 */
560 *p_offset = -1;
561 *p_filesz = 0;
562
Janosch Frankdddf7252022-08-11 12:10:58 +0000563 if (dump_has_filter(s)) {
564 if (phys_addr < s->filter_area_begin ||
565 phys_addr >= s->filter_area_begin + s->filter_area_length) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200566 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800567 }
568 }
569
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200570 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000571 if (dump_has_filter(s)) {
572 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
573 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800574 /* This block is out of the range */
575 continue;
576 }
577
Janosch Frankdddf7252022-08-11 12:10:58 +0000578 if (s->filter_area_begin <= block->target_start) {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200579 start = block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800580 } else {
Janosch Frankdddf7252022-08-11 12:10:58 +0000581 start = s->filter_area_begin;
Wen Congyang783e9b42012-05-07 12:10:47 +0800582 }
583
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200584 size_in_block = block->target_end - start;
Janosch Frankdddf7252022-08-11 12:10:58 +0000585 if (s->filter_area_begin + s->filter_area_length < block->target_end) {
586 size_in_block -= block->target_end - (s->filter_area_begin + s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +0800587 }
588 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200589 start = block->target_start;
590 size_in_block = block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800591 }
592
593 if (phys_addr >= start && phys_addr < start + size_in_block) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200594 *p_offset = phys_addr - start + offset;
595
596 /* The offset range mapped from the vmcore file must not spill over
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200597 * the GuestPhysBlock, clamp it. The rest of the mapping will be
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200598 * zero-filled in memory at load time; see
599 * <http://refspecs.linuxbase.org/elf/gabi4+/ch5.pheader.html>.
600 */
601 *p_filesz = phys_addr + mapping_length <= start + size_in_block ?
602 mapping_length :
603 size_in_block - (phys_addr - start);
604 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800605 }
606
607 offset += size_in_block;
608 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800609}
610
Janosch Frankafae6052022-08-11 12:10:55 +0000611static void write_elf_phdr_loads(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800612{
Janosch Frank86a518b2022-03-30 12:35:55 +0000613 ERRP_GUARD();
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200614 hwaddr offset, filesz;
Wen Congyang783e9b42012-05-07 12:10:47 +0800615 MemoryMapping *memory_mapping;
616 uint32_t phdr_index = 1;
Wen Congyang783e9b42012-05-07 12:10:47 +0800617
618 QTAILQ_FOREACH(memory_mapping, &s->list.head, next) {
Laszlo Ersek2cac2602013-08-06 12:37:08 +0200619 get_offset_range(memory_mapping->phys_addr,
620 memory_mapping->length,
621 s, &offset, &filesz);
Janosch Frank05bbaa502022-03-30 12:36:00 +0000622 if (dump_is_64bit(s)) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800623 write_elf64_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000624 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800625 } else {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800626 write_elf32_load(s, memory_mapping, phdr_index++, offset,
Janosch Frank86a518b2022-03-30 12:35:55 +0000627 filesz, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800628 }
629
Janosch Frank86a518b2022-03-30 12:35:55 +0000630 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800631 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800632 }
633
Janosch Frank046bc412022-04-07 09:48:24 +0000634 if (phdr_index >= s->phdr_num) {
Wen Congyang783e9b42012-05-07 12:10:47 +0800635 break;
636 }
637 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800638}
639
Janosch Frankc6812472022-03-30 12:36:03 +0000640static void write_elf_notes(DumpState *s, Error **errp)
641{
642 if (dump_is_64bit(s)) {
643 write_elf64_notes(fd_write_vmcore, s, errp);
644 } else {
645 write_elf32_notes(fd_write_vmcore, s, errp);
646 }
647}
648
Wen Congyang783e9b42012-05-07 12:10:47 +0800649/* write elf header, PT_NOTE and elf note to vmcore. */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800650static void dump_begin(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800651{
Janosch Frank86a518b2022-03-30 12:35:55 +0000652 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800653
654 /*
655 * the vmcore's format is:
656 * --------------
657 * | elf header |
658 * --------------
Janosch Frankcb415fd2022-10-17 08:38:14 +0000659 * | sctn_hdr |
660 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800661 * | PT_NOTE |
662 * --------------
663 * | PT_LOAD |
664 * --------------
665 * | ...... |
666 * --------------
667 * | PT_LOAD |
668 * --------------
Wen Congyang783e9b42012-05-07 12:10:47 +0800669 * | elf note |
670 * --------------
671 * | memory |
672 * --------------
673 *
674 * we only know where the memory is saved after we write elf note into
675 * vmcore.
676 */
677
678 /* write elf header to vmcore */
Janosch Frank670e7692022-08-11 12:11:00 +0000679 write_elf_header(s, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000680 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800681 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800682 }
683
Janosch Frankcb415fd2022-10-17 08:38:14 +0000684 /* write section headers to vmcore */
685 write_elf_section_headers(s, errp);
686 if (*errp) {
687 return;
688 }
689
Janosch Frankbc7d5582022-03-30 12:36:01 +0000690 /* write PT_NOTE to vmcore */
691 write_elf_phdr_note(s, errp);
692 if (*errp) {
693 return;
694 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800695
Janosch Frankafae6052022-08-11 12:10:55 +0000696 /* write all PT_LOADs to vmcore */
697 write_elf_phdr_loads(s, errp);
Janosch Frank5ff2e5a2022-03-30 12:36:02 +0000698 if (*errp) {
699 return;
700 }
701
Janosch Frankc6812472022-03-30 12:36:03 +0000702 /* write notes to vmcore */
703 write_elf_notes(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800704}
705
Janosch Frank113d8f42022-10-17 08:38:22 +0000706int64_t dump_filtered_memblock_size(GuestPhysBlock *block,
707 int64_t filter_area_start,
708 int64_t filter_area_length)
Wen Congyang783e9b42012-05-07 12:10:47 +0800709{
Janosch Frank1e811302022-08-11 12:10:56 +0000710 int64_t size, left, right;
Wen Congyang783e9b42012-05-07 12:10:47 +0800711
Janosch Frank1e811302022-08-11 12:10:56 +0000712 /* No filter, return full size */
713 if (!filter_area_length) {
714 return block->target_end - block->target_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800715 }
Janosch Frank1e811302022-08-11 12:10:56 +0000716
717 /* calculate the overlapped region. */
718 left = MAX(filter_area_start, block->target_start);
719 right = MIN(filter_area_start + filter_area_length, block->target_end);
720 size = right - left;
721 size = size > 0 ? size : 0;
722
723 return size;
724}
725
Janosch Frank113d8f42022-10-17 08:38:22 +0000726int64_t dump_filtered_memblock_start(GuestPhysBlock *block,
727 int64_t filter_area_start,
728 int64_t filter_area_length)
Janosch Frank1e811302022-08-11 12:10:56 +0000729{
730 if (filter_area_length) {
731 /* return -1 if the block is not within filter area */
732 if (block->target_start >= filter_area_start + filter_area_length ||
733 block->target_end <= filter_area_start) {
734 return -1;
735 }
736
737 if (filter_area_start > block->target_start) {
738 return filter_area_start - block->target_start;
739 }
740 }
741
742 return 0;
Wen Congyang783e9b42012-05-07 12:10:47 +0800743}
744
745/* write all memory to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800746static void dump_iterate(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800747{
Janosch Frank86a518b2022-03-30 12:35:55 +0000748 ERRP_GUARD();
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200749 GuestPhysBlock *block;
Janosch Frank1e811302022-08-11 12:10:56 +0000750 int64_t memblock_size, memblock_start;
Wen Congyang783e9b42012-05-07 12:10:47 +0800751
Janosch Frank1e811302022-08-11 12:10:56 +0000752 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankdddf7252022-08-11 12:10:58 +0000753 memblock_start = dump_filtered_memblock_start(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000754 if (memblock_start == -1) {
755 continue;
Wen Congyang783e9b42012-05-07 12:10:47 +0800756 }
Janosch Frank1e811302022-08-11 12:10:56 +0000757
Janosch Frankdddf7252022-08-11 12:10:58 +0000758 memblock_size = dump_filtered_memblock_size(block, s->filter_area_begin, s->filter_area_length);
Janosch Frank1e811302022-08-11 12:10:56 +0000759
760 /* Write the memory to file */
761 write_memory(s, block, memblock_start, memblock_size, errp);
Janosch Frank86a518b2022-03-30 12:35:55 +0000762 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800763 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800764 }
Janosch Frank1e811302022-08-11 12:10:56 +0000765 }
Wen Congyang783e9b42012-05-07 12:10:47 +0800766}
767
Janosch Frank9b722242022-10-17 11:32:10 +0000768static void dump_end(DumpState *s, Error **errp)
769{
770 int rc;
Janosch Frank9b722242022-10-17 11:32:10 +0000771
772 if (s->elf_section_data_size) {
773 s->elf_section_data = g_malloc0(s->elf_section_data_size);
774 }
775
776 /* Adds the architecture defined section data to s->elf_section_data */
777 if (s->dump_info.arch_sections_write_fn &&
778 s->elf_section_data_size) {
779 rc = s->dump_info.arch_sections_write_fn(s, s->elf_section_data);
780 if (rc) {
781 error_setg_errno(errp, rc,
782 "dump: failed to get arch section data");
783 g_free(s->elf_section_data);
784 return;
785 }
786 }
787
788 /* write sections to vmcore */
789 write_elf_sections(s, errp);
790}
791
zhanghailiang4c7e2512014-10-09 14:13:11 +0800792static void create_vmcore(DumpState *s, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +0800793{
Janosch Frank86a518b2022-03-30 12:35:55 +0000794 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +0800795
Janosch Frank86a518b2022-03-30 12:35:55 +0000796 dump_begin(s, errp);
797 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +0800798 return;
Wen Congyang783e9b42012-05-07 12:10:47 +0800799 }
800
Janosch Frank9b722242022-10-17 11:32:10 +0000801 /* Iterate over memory and dump it to file */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800802 dump_iterate(s, errp);
Janosch Frank9b722242022-10-17 11:32:10 +0000803 if (*errp) {
804 return;
805 }
806
807 /* Write the section data */
808 dump_end(s, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +0800809}
810
qiaonuohanfda05382014-02-18 14:11:27 +0800811static int write_start_flat_header(int fd)
812{
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200813 MakedumpfileHeader *mh;
qiaonuohanfda05382014-02-18 14:11:27 +0800814 int ret = 0;
815
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200816 QEMU_BUILD_BUG_ON(sizeof *mh > MAX_SIZE_MDF_HEADER);
817 mh = g_malloc0(MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800818
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200819 memcpy(mh->signature, MAKEDUMPFILE_SIGNATURE,
820 MIN(sizeof mh->signature, sizeof MAKEDUMPFILE_SIGNATURE));
qiaonuohanfda05382014-02-18 14:11:27 +0800821
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200822 mh->type = cpu_to_be64(TYPE_FLAT_HEADER);
823 mh->version = cpu_to_be64(VERSION_FLAT_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800824
825 size_t written_size;
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200826 written_size = qemu_write_full(fd, mh, MAX_SIZE_MDF_HEADER);
qiaonuohanfda05382014-02-18 14:11:27 +0800827 if (written_size != MAX_SIZE_MDF_HEADER) {
828 ret = -1;
829 }
830
Laszlo Ersek92ba1402014-05-20 13:39:42 +0200831 g_free(mh);
qiaonuohanfda05382014-02-18 14:11:27 +0800832 return ret;
833}
834
835static int write_end_flat_header(int fd)
836{
837 MakedumpfileDataHeader mdh;
838
839 mdh.offset = END_FLAG_FLAT_HEADER;
840 mdh.buf_size = END_FLAG_FLAT_HEADER;
841
842 size_t written_size;
843 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
844 if (written_size != sizeof(mdh)) {
845 return -1;
846 }
847
848 return 0;
849}
850
qiaonuohan5d31bab2014-02-18 14:11:28 +0800851static int write_buffer(int fd, off_t offset, const void *buf, size_t size)
852{
853 size_t written_size;
854 MakedumpfileDataHeader mdh;
855
856 mdh.offset = cpu_to_be64(offset);
857 mdh.buf_size = cpu_to_be64(size);
858
859 written_size = qemu_write_full(fd, &mdh, sizeof(mdh));
860 if (written_size != sizeof(mdh)) {
861 return -1;
862 }
863
864 written_size = qemu_write_full(fd, buf, size);
865 if (written_size != size) {
866 return -1;
867 }
868
869 return 0;
870}
871
qiaonuohan4835ef72014-02-18 14:11:29 +0800872static int buf_write_note(const void *buf, size_t size, void *opaque)
873{
874 DumpState *s = opaque;
875
876 /* note_buf is not enough */
877 if (s->note_buf_offset + size > s->note_size) {
878 return -1;
879 }
880
881 memcpy(s->note_buf + s->note_buf_offset, buf, size);
882
883 s->note_buf_offset += size;
884
885 return 0;
886}
887
Marc-André Lureau903ef732017-09-11 18:59:25 +0200888/*
889 * This function retrieves various sizes from an elf header.
890 *
891 * @note has to be a valid ELF note. The return sizes are unmodified
892 * (not padded or rounded up to be multiple of 4).
893 */
894static void get_note_sizes(DumpState *s, const void *note,
895 uint64_t *note_head_size,
896 uint64_t *name_size,
897 uint64_t *desc_size)
898{
899 uint64_t note_head_sz;
900 uint64_t name_sz;
901 uint64_t desc_sz;
902
Janosch Frank05bbaa502022-03-30 12:36:00 +0000903 if (dump_is_64bit(s)) {
Marc-André Lureau903ef732017-09-11 18:59:25 +0200904 const Elf64_Nhdr *hdr = note;
905 note_head_sz = sizeof(Elf64_Nhdr);
Philippe Mathieu-Daudébb509d92022-12-16 08:28:32 +0100906 name_sz = cpu_to_dump64(s, hdr->n_namesz);
907 desc_sz = cpu_to_dump64(s, hdr->n_descsz);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200908 } else {
909 const Elf32_Nhdr *hdr = note;
910 note_head_sz = sizeof(Elf32_Nhdr);
Philippe Mathieu-Daudébb509d92022-12-16 08:28:32 +0100911 name_sz = cpu_to_dump32(s, hdr->n_namesz);
912 desc_sz = cpu_to_dump32(s, hdr->n_descsz);
Marc-André Lureau903ef732017-09-11 18:59:25 +0200913 }
914
915 if (note_head_size) {
916 *note_head_size = note_head_sz;
917 }
918 if (name_size) {
919 *name_size = name_sz;
920 }
921 if (desc_size) {
922 *desc_size = desc_sz;
923 }
924}
925
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200926static bool note_name_equal(DumpState *s,
927 const uint8_t *note, const char *name)
928{
929 int len = strlen(name) + 1;
930 uint64_t head_size, name_size;
931
932 get_note_sizes(s, note, &head_size, &name_size, NULL);
933 head_size = ROUND_UP(head_size, 4);
934
Marc-André Lureauc983ca82017-12-12 15:53:59 +0100935 return name_size == len && memcmp(note + head_size, name, len) == 0;
Marc-André Lureaud9feb512017-09-11 18:59:26 +0200936}
937
qiaonuohan298f1162014-02-18 14:11:32 +0800938/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +0800939static void create_header32(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +0800940{
Janosch Frank86a518b2022-03-30 12:35:55 +0000941 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +0800942 DiskDumpHeader32 *dh = NULL;
943 KdumpSubHeader32 *kh = NULL;
944 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +0800945 uint32_t block_size;
946 uint32_t sub_hdr_size;
947 uint32_t bitmap_blocks;
948 uint32_t status = 0;
949 uint64_t offset_note;
950
951 /* write common header, the version of kdump-compressed format is 6th */
952 size = sizeof(DiskDumpHeader32);
953 dh = g_malloc0(size);
954
Eric Blake84c868f2018-03-27 15:21:51 -0500955 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200956 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +0100957 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200958 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800959 sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
960 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200961 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +0800962 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200963 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
964 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +0800965 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200966 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +0800967 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +0800968
969 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
970 status |= DUMP_DH_COMPRESSED_ZLIB;
971 }
972#ifdef CONFIG_LZO
973 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
974 status |= DUMP_DH_COMPRESSED_LZO;
975 }
976#endif
977#ifdef CONFIG_SNAPPY
978 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
979 status |= DUMP_DH_COMPRESSED_SNAPPY;
980 }
981#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200982 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +0800983
984 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +0800985 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +0800986 goto out;
987 }
988
989 /* write sub header */
990 size = sizeof(KdumpSubHeader32);
991 kh = g_malloc0(size);
992
993 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200994 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +0100995 kh->phys_base = cpu_to_dump32(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +0200996 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +0800997
998 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +0200999 if (s->guest_note &&
1000 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1001 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1002
1003 get_note_sizes(s, s->guest_note,
1004 &hsize, &name_size, &size_vmcoreinfo_desc);
1005 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1006 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1007 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1008 kh->size_vmcoreinfo = cpu_to_dump32(s, size_vmcoreinfo_desc);
1009 }
1010
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001011 kh->offset_note = cpu_to_dump64(s, offset_note);
1012 kh->note_size = cpu_to_dump32(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001013
1014 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
1015 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001016 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001017 goto out;
1018 }
1019
1020 /* write note */
1021 s->note_buf = g_malloc0(s->note_size);
1022 s->note_buf_offset = 0;
1023
1024 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001025 write_elf32_notes(buf_write_note, s, errp);
1026 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001027 goto out;
1028 }
qiaonuohan298f1162014-02-18 14:11:32 +08001029 if (write_buffer(s->fd, offset_note, s->note_buf,
1030 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001031 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001032 goto out;
1033 }
1034
1035 /* get offset of dump_bitmap */
1036 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1037 block_size;
1038
1039 /* get offset of page */
1040 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1041 block_size;
1042
1043out:
1044 g_free(dh);
1045 g_free(kh);
1046 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001047}
1048
1049/* write common header, sub header and elf note to vmcore */
zhanghailiang4c7e2512014-10-09 14:13:11 +08001050static void create_header64(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001051{
Janosch Frank86a518b2022-03-30 12:35:55 +00001052 ERRP_GUARD();
qiaonuohan298f1162014-02-18 14:11:32 +08001053 DiskDumpHeader64 *dh = NULL;
1054 KdumpSubHeader64 *kh = NULL;
1055 size_t size;
qiaonuohan298f1162014-02-18 14:11:32 +08001056 uint32_t block_size;
1057 uint32_t sub_hdr_size;
1058 uint32_t bitmap_blocks;
1059 uint32_t status = 0;
1060 uint64_t offset_note;
1061
1062 /* write common header, the version of kdump-compressed format is 6th */
1063 size = sizeof(DiskDumpHeader64);
1064 dh = g_malloc0(size);
1065
Eric Blake84c868f2018-03-27 15:21:51 -05001066 memcpy(dh->signature, KDUMP_SIGNATURE, SIG_LEN);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001067 dh->header_version = cpu_to_dump32(s, 6);
Andrew Jones8161bef2016-01-11 20:56:20 +01001068 block_size = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001069 dh->block_size = cpu_to_dump32(s, block_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001070 sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
1071 sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001072 dh->sub_hdr_size = cpu_to_dump32(s, sub_hdr_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001073 /* dh->max_mapnr may be truncated, full 64bit is in kh.max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001074 dh->max_mapnr = cpu_to_dump32(s, MIN(s->max_mapnr, UINT_MAX));
1075 dh->nr_cpus = cpu_to_dump32(s, s->nr_cpus);
qiaonuohan298f1162014-02-18 14:11:32 +08001076 bitmap_blocks = DIV_ROUND_UP(s->len_dump_bitmap, block_size) * 2;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001077 dh->bitmap_blocks = cpu_to_dump32(s, bitmap_blocks);
qiaonuohan4ab23a92014-02-18 14:11:37 +08001078 strncpy(dh->utsname.machine, ELF_MACHINE_UNAME, sizeof(dh->utsname.machine));
qiaonuohan298f1162014-02-18 14:11:32 +08001079
1080 if (s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) {
1081 status |= DUMP_DH_COMPRESSED_ZLIB;
1082 }
1083#ifdef CONFIG_LZO
1084 if (s->flag_compress & DUMP_DH_COMPRESSED_LZO) {
1085 status |= DUMP_DH_COMPRESSED_LZO;
1086 }
1087#endif
1088#ifdef CONFIG_SNAPPY
1089 if (s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) {
1090 status |= DUMP_DH_COMPRESSED_SNAPPY;
1091 }
1092#endif
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001093 dh->status = cpu_to_dump32(s, status);
qiaonuohan298f1162014-02-18 14:11:32 +08001094
1095 if (write_buffer(s->fd, 0, dh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001096 error_setg(errp, "dump: failed to write disk dump header");
qiaonuohan298f1162014-02-18 14:11:32 +08001097 goto out;
1098 }
1099
1100 /* write sub header */
1101 size = sizeof(KdumpSubHeader64);
1102 kh = g_malloc0(size);
1103
1104 /* 64bit max_mapnr_64 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001105 kh->max_mapnr_64 = cpu_to_dump64(s, s->max_mapnr);
Andrew Jonesb6e05aa2016-01-11 20:56:21 +01001106 kh->phys_base = cpu_to_dump64(s, s->dump_info.phys_base);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001107 kh->dump_level = cpu_to_dump32(s, DUMP_LEVEL);
qiaonuohan298f1162014-02-18 14:11:32 +08001108
1109 offset_note = DISKDUMP_HEADER_BLOCKS * block_size + size;
Marc-André Lureau9ada5752017-09-11 18:59:27 +02001110 if (s->guest_note &&
1111 note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1112 uint64_t hsize, name_size, size_vmcoreinfo_desc, offset_vmcoreinfo;
1113
1114 get_note_sizes(s, s->guest_note,
1115 &hsize, &name_size, &size_vmcoreinfo_desc);
1116 offset_vmcoreinfo = offset_note + s->note_size - s->guest_note_size +
1117 (DIV_ROUND_UP(hsize, 4) + DIV_ROUND_UP(name_size, 4)) * 4;
1118 kh->offset_vmcoreinfo = cpu_to_dump64(s, offset_vmcoreinfo);
1119 kh->size_vmcoreinfo = cpu_to_dump64(s, size_vmcoreinfo_desc);
1120 }
1121
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001122 kh->offset_note = cpu_to_dump64(s, offset_note);
1123 kh->note_size = cpu_to_dump64(s, s->note_size);
qiaonuohan298f1162014-02-18 14:11:32 +08001124
1125 if (write_buffer(s->fd, DISKDUMP_HEADER_BLOCKS *
1126 block_size, kh, size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001127 error_setg(errp, "dump: failed to write kdump sub header");
qiaonuohan298f1162014-02-18 14:11:32 +08001128 goto out;
1129 }
1130
1131 /* write note */
1132 s->note_buf = g_malloc0(s->note_size);
1133 s->note_buf_offset = 0;
1134
1135 /* use s->note_buf to store notes temporarily */
Janosch Frank86a518b2022-03-30 12:35:55 +00001136 write_elf64_notes(buf_write_note, s, errp);
1137 if (*errp) {
qiaonuohan298f1162014-02-18 14:11:32 +08001138 goto out;
1139 }
1140
1141 if (write_buffer(s->fd, offset_note, s->note_buf,
1142 s->note_size) < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001143 error_setg(errp, "dump: failed to write notes");
qiaonuohan298f1162014-02-18 14:11:32 +08001144 goto out;
1145 }
1146
1147 /* get offset of dump_bitmap */
1148 s->offset_dump_bitmap = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size) *
1149 block_size;
1150
1151 /* get offset of page */
1152 s->offset_page = (DISKDUMP_HEADER_BLOCKS + sub_hdr_size + bitmap_blocks) *
1153 block_size;
1154
1155out:
1156 g_free(dh);
1157 g_free(kh);
1158 g_free(s->note_buf);
qiaonuohan298f1162014-02-18 14:11:32 +08001159}
1160
zhanghailiang4c7e2512014-10-09 14:13:11 +08001161static void write_dump_header(DumpState *s, Error **errp)
qiaonuohan298f1162014-02-18 14:11:32 +08001162{
Janosch Frank05bbaa502022-03-30 12:36:00 +00001163 if (dump_is_64bit(s)) {
Markus Armbruster992861f2020-07-07 18:06:04 +02001164 create_header64(s, errp);
Janosch Frank05bbaa502022-03-30 12:36:00 +00001165 } else {
1166 create_header32(s, errp);
zhanghailiang4c7e2512014-10-09 14:13:11 +08001167 }
qiaonuohan298f1162014-02-18 14:11:32 +08001168}
1169
Andrew Jones8161bef2016-01-11 20:56:20 +01001170static size_t dump_bitmap_get_bufsize(DumpState *s)
1171{
1172 return s->dump_info.page_size;
1173}
1174
qiaonuohand0686c72014-02-18 14:11:33 +08001175/*
1176 * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be
1177 * rewritten, so if need to set the first bit, set last_pfn and pfn to 0.
1178 * set_dump_bitmap will always leave the recently set bit un-sync. And setting
1179 * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into
1180 * vmcore, ie. synchronizing un-sync bit into vmcore.
1181 */
1182static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value,
1183 uint8_t *buf, DumpState *s)
1184{
1185 off_t old_offset, new_offset;
1186 off_t offset_bitmap1, offset_bitmap2;
1187 uint32_t byte, bit;
Andrew Jones8161bef2016-01-11 20:56:20 +01001188 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1189 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001190
1191 /* should not set the previous place */
1192 assert(last_pfn <= pfn);
1193
1194 /*
1195 * if the bit needed to be set is not cached in buf, flush the data in buf
1196 * to vmcore firstly.
1197 * making new_offset be bigger than old_offset can also sync remained data
1198 * into vmcore.
1199 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001200 old_offset = bitmap_bufsize * (last_pfn / bits_per_buf);
1201 new_offset = bitmap_bufsize * (pfn / bits_per_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001202
1203 while (old_offset < new_offset) {
1204 /* calculate the offset and write dump_bitmap */
1205 offset_bitmap1 = s->offset_dump_bitmap + old_offset;
1206 if (write_buffer(s->fd, offset_bitmap1, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001207 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001208 return -1;
1209 }
1210
1211 /* dump level 1 is chosen, so 1st and 2nd bitmap are same */
1212 offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap +
1213 old_offset;
1214 if (write_buffer(s->fd, offset_bitmap2, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001215 bitmap_bufsize) < 0) {
qiaonuohand0686c72014-02-18 14:11:33 +08001216 return -1;
1217 }
1218
Andrew Jones8161bef2016-01-11 20:56:20 +01001219 memset(buf, 0, bitmap_bufsize);
1220 old_offset += bitmap_bufsize;
qiaonuohand0686c72014-02-18 14:11:33 +08001221 }
1222
1223 /* get the exact place of the bit in the buf, and set it */
Andrew Jones8161bef2016-01-11 20:56:20 +01001224 byte = (pfn % bits_per_buf) / CHAR_BIT;
1225 bit = (pfn % bits_per_buf) % CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001226 if (value) {
1227 buf[byte] |= 1u << bit;
1228 } else {
1229 buf[byte] &= ~(1u << bit);
1230 }
1231
1232 return 0;
1233}
1234
Andrew Jones8161bef2016-01-11 20:56:20 +01001235static uint64_t dump_paddr_to_pfn(DumpState *s, uint64_t addr)
1236{
1237 int target_page_shift = ctz32(s->dump_info.page_size);
1238
1239 return (addr >> target_page_shift) - ARCH_PFN_OFFSET;
1240}
1241
1242static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
1243{
1244 int target_page_shift = ctz32(s->dump_info.page_size);
1245
1246 return (pfn + ARCH_PFN_OFFSET) << target_page_shift;
1247}
1248
qiaonuohand0686c72014-02-18 14:11:33 +08001249/*
Marc-André Lureau94d78842022-09-05 16:06:21 +04001250 * Return the page frame number and the page content in *bufptr. bufptr can be
1251 * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
1252 * memory. This is not necessarily the memory returned.
qiaonuohand0686c72014-02-18 14:11:33 +08001253 */
1254static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
1255 uint8_t **bufptr, DumpState *s)
1256{
1257 GuestPhysBlock *block = *blockptr;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001258 uint32_t page_size = s->dump_info.page_size;
1259 uint8_t *buf = NULL, *hbuf;
1260 hwaddr addr;
qiaonuohand0686c72014-02-18 14:11:33 +08001261
1262 /* block == NULL means the start of the iteration */
1263 if (!block) {
1264 block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
1265 *blockptr = block;
Marc-André Lureau08df3432022-08-25 12:40:12 +04001266 addr = block->target_start;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001267 *pfnptr = dump_paddr_to_pfn(s, addr);
Marc-André Lureau08df3432022-08-25 12:40:12 +04001268 } else {
Marc-André Lureau94d78842022-09-05 16:06:21 +04001269 *pfnptr += 1;
1270 addr = dump_pfn_to_paddr(s, *pfnptr);
qiaonuohand0686c72014-02-18 14:11:33 +08001271 }
Marc-André Lureau08df3432022-08-25 12:40:12 +04001272 assert(block != NULL);
qiaonuohand0686c72014-02-18 14:11:33 +08001273
Marc-André Lureau94d78842022-09-05 16:06:21 +04001274 while (1) {
1275 if (addr >= block->target_start && addr < block->target_end) {
1276 size_t n = MIN(block->target_end - addr, page_size - addr % page_size);
1277 hbuf = block->host_addr + (addr - block->target_start);
1278 if (!buf) {
1279 if (n == page_size) {
1280 /* this is a whole target page, go for it */
1281 assert(addr % page_size == 0);
1282 buf = hbuf;
1283 break;
1284 } else if (bufptr) {
1285 assert(*bufptr);
1286 buf = *bufptr;
1287 memset(buf, 0, page_size);
1288 } else {
1289 return true;
1290 }
1291 }
1292
1293 memcpy(buf + addr % page_size, hbuf, n);
1294 addr += n;
1295 if (addr % page_size == 0) {
1296 /* we filled up the page */
1297 break;
1298 }
1299 } else {
1300 /* the next page is in the next block */
1301 *blockptr = block = QTAILQ_NEXT(block, next);
1302 if (!block) {
1303 break;
1304 }
1305
1306 addr = block->target_start;
1307 /* are we still in the same page? */
1308 if (dump_paddr_to_pfn(s, addr) != *pfnptr) {
1309 if (buf) {
1310 /* no, but we already filled something earlier, return it */
1311 break;
1312 } else {
1313 /* else continue from there */
1314 *pfnptr = dump_paddr_to_pfn(s, addr);
1315 }
1316 }
qiaonuohand0686c72014-02-18 14:11:33 +08001317 }
qiaonuohand0686c72014-02-18 14:11:33 +08001318 }
1319
1320 if (bufptr) {
1321 *bufptr = buf;
1322 }
1323
Marc-André Lureau94d78842022-09-05 16:06:21 +04001324 return buf != NULL;
qiaonuohand0686c72014-02-18 14:11:33 +08001325}
1326
zhanghailiang4c7e2512014-10-09 14:13:11 +08001327static void write_dump_bitmap(DumpState *s, Error **errp)
qiaonuohand0686c72014-02-18 14:11:33 +08001328{
1329 int ret = 0;
1330 uint64_t last_pfn, pfn;
1331 void *dump_bitmap_buf;
1332 size_t num_dumpable;
1333 GuestPhysBlock *block_iter = NULL;
Andrew Jones8161bef2016-01-11 20:56:20 +01001334 size_t bitmap_bufsize = dump_bitmap_get_bufsize(s);
1335 size_t bits_per_buf = bitmap_bufsize * CHAR_BIT;
qiaonuohand0686c72014-02-18 14:11:33 +08001336
1337 /* dump_bitmap_buf is used to store dump_bitmap temporarily */
Andrew Jones8161bef2016-01-11 20:56:20 +01001338 dump_bitmap_buf = g_malloc0(bitmap_bufsize);
qiaonuohand0686c72014-02-18 14:11:33 +08001339
1340 num_dumpable = 0;
1341 last_pfn = 0;
1342
1343 /*
1344 * exam memory page by page, and set the bit in dump_bitmap corresponded
1345 * to the existing page.
1346 */
1347 while (get_next_page(&block_iter, &pfn, NULL, s)) {
1348 ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s);
1349 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001350 error_setg(errp, "dump: failed to set dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001351 goto out;
1352 }
1353
1354 last_pfn = pfn;
1355 num_dumpable++;
1356 }
1357
1358 /*
1359 * set_dump_bitmap will always leave the recently set bit un-sync. Here we
Andrew Jones8161bef2016-01-11 20:56:20 +01001360 * set the remaining bits from last_pfn to the end of the bitmap buffer to
1361 * 0. With those set, the un-sync bit will be synchronized into the vmcore.
qiaonuohand0686c72014-02-18 14:11:33 +08001362 */
1363 if (num_dumpable > 0) {
Andrew Jones8161bef2016-01-11 20:56:20 +01001364 ret = set_dump_bitmap(last_pfn, last_pfn + bits_per_buf, false,
qiaonuohand0686c72014-02-18 14:11:33 +08001365 dump_bitmap_buf, s);
1366 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001367 error_setg(errp, "dump: failed to sync dump_bitmap");
qiaonuohand0686c72014-02-18 14:11:33 +08001368 goto out;
1369 }
1370 }
1371
1372 /* number of dumpable pages that will be dumped later */
1373 s->num_dumpable = num_dumpable;
1374
1375out:
1376 g_free(dump_bitmap_buf);
qiaonuohand0686c72014-02-18 14:11:33 +08001377}
1378
qiaonuohan64cfba62014-02-18 14:11:34 +08001379static void prepare_data_cache(DataCache *data_cache, DumpState *s,
1380 off_t offset)
1381{
1382 data_cache->fd = s->fd;
1383 data_cache->data_size = 0;
Andrew Jones8161bef2016-01-11 20:56:20 +01001384 data_cache->buf_size = 4 * dump_bitmap_get_bufsize(s);
1385 data_cache->buf = g_malloc0(data_cache->buf_size);
qiaonuohan64cfba62014-02-18 14:11:34 +08001386 data_cache->offset = offset;
1387}
1388
1389static int write_cache(DataCache *dc, const void *buf, size_t size,
1390 bool flag_sync)
1391{
1392 /*
1393 * dc->buf_size should not be less than size, otherwise dc will never be
1394 * enough
1395 */
1396 assert(size <= dc->buf_size);
1397
1398 /*
1399 * if flag_sync is set, synchronize data in dc->buf into vmcore.
1400 * otherwise check if the space is enough for caching data in buf, if not,
1401 * write the data in dc->buf to dc->fd and reset dc->buf
1402 */
1403 if ((!flag_sync && dc->data_size + size > dc->buf_size) ||
1404 (flag_sync && dc->data_size > 0)) {
1405 if (write_buffer(dc->fd, dc->offset, dc->buf, dc->data_size) < 0) {
1406 return -1;
1407 }
1408
1409 dc->offset += dc->data_size;
1410 dc->data_size = 0;
1411 }
1412
1413 if (!flag_sync) {
1414 memcpy(dc->buf + dc->data_size, buf, size);
1415 dc->data_size += size;
1416 }
1417
1418 return 0;
1419}
1420
1421static void free_data_cache(DataCache *data_cache)
1422{
1423 g_free(data_cache->buf);
1424}
1425
qiaonuohand12f57e2014-02-18 14:11:35 +08001426static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
1427{
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001428 switch (flag_compress) {
1429 case DUMP_DH_COMPRESSED_ZLIB:
1430 return compressBound(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001431
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001432 case DUMP_DH_COMPRESSED_LZO:
1433 /*
1434 * LZO will expand incompressible data by a little amount. Please check
1435 * the following URL to see the expansion calculation:
1436 * http://www.oberhumer.com/opensource/lzo/lzofaq.php
1437 */
1438 return page_size + page_size / 16 + 64 + 3;
qiaonuohand12f57e2014-02-18 14:11:35 +08001439
1440#ifdef CONFIG_SNAPPY
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001441 case DUMP_DH_COMPRESSED_SNAPPY:
1442 return snappy_max_compressed_length(page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001443#endif
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001444 }
1445 return 0;
qiaonuohand12f57e2014-02-18 14:11:35 +08001446}
1447
zhanghailiang4c7e2512014-10-09 14:13:11 +08001448static void write_dump_pages(DumpState *s, Error **errp)
qiaonuohand12f57e2014-02-18 14:11:35 +08001449{
1450 int ret = 0;
1451 DataCache page_desc, page_data;
1452 size_t len_buf_out, size_out;
1453#ifdef CONFIG_LZO
1454 lzo_bytep wrkmem = NULL;
1455#endif
1456 uint8_t *buf_out = NULL;
1457 off_t offset_desc, offset_data;
1458 PageDescriptor pd, pd_zero;
1459 uint8_t *buf;
qiaonuohand12f57e2014-02-18 14:11:35 +08001460 GuestPhysBlock *block_iter = NULL;
1461 uint64_t pfn_iter;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001462 g_autofree uint8_t *page = NULL;
qiaonuohand12f57e2014-02-18 14:11:35 +08001463
1464 /* get offset of page_desc and page_data in dump file */
1465 offset_desc = s->offset_page;
1466 offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
1467
1468 prepare_data_cache(&page_desc, s, offset_desc);
1469 prepare_data_cache(&page_data, s, offset_data);
1470
1471 /* prepare buffer to store compressed data */
Andrew Jones8161bef2016-01-11 20:56:20 +01001472 len_buf_out = get_len_buf_out(s->dump_info.page_size, s->flag_compress);
Laszlo Ersekb87ef352014-05-20 13:42:53 +02001473 assert(len_buf_out != 0);
qiaonuohand12f57e2014-02-18 14:11:35 +08001474
1475#ifdef CONFIG_LZO
1476 wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
1477#endif
1478
1479 buf_out = g_malloc(len_buf_out);
1480
1481 /*
1482 * init zero page's page_desc and page_data, because every zero page
1483 * uses the same page_data
1484 */
Andrew Jones8161bef2016-01-11 20:56:20 +01001485 pd_zero.size = cpu_to_dump32(s, s->dump_info.page_size);
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001486 pd_zero.flags = cpu_to_dump32(s, 0);
1487 pd_zero.offset = cpu_to_dump64(s, offset_data);
1488 pd_zero.page_flags = cpu_to_dump64(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001489 buf = g_malloc0(s->dump_info.page_size);
1490 ret = write_cache(&page_data, buf, s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001491 g_free(buf);
1492 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001493 error_setg(errp, "dump: failed to write page data (zero page)");
qiaonuohand12f57e2014-02-18 14:11:35 +08001494 goto out;
1495 }
1496
Andrew Jones8161bef2016-01-11 20:56:20 +01001497 offset_data += s->dump_info.page_size;
Marc-André Lureau94d78842022-09-05 16:06:21 +04001498 page = g_malloc(s->dump_info.page_size);
qiaonuohand12f57e2014-02-18 14:11:35 +08001499
1500 /*
1501 * dump memory to vmcore page by page. zero page will all be resided in the
1502 * first page of page section
1503 */
Marc-André Lureau94d78842022-09-05 16:06:21 +04001504 for (buf = page; get_next_page(&block_iter, &pfn_iter, &buf, s); buf = page) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001505 /* check zero page */
Juan Quintelaf13f22b2021-11-18 15:57:44 +01001506 if (buffer_is_zero(buf, s->dump_info.page_size)) {
qiaonuohand12f57e2014-02-18 14:11:35 +08001507 ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
1508 false);
1509 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001510 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001511 goto out;
1512 }
1513 } else {
1514 /*
1515 * not zero page, then:
1516 * 1. compress the page
1517 * 2. write the compressed page into the cache of page_data
1518 * 3. get page desc of the compressed page and write it into the
1519 * cache of page_desc
1520 *
1521 * only one compression format will be used here, for
1522 * s->flag_compress is set. But when compression fails to work,
1523 * we fall back to save in plaintext.
1524 */
1525 size_out = len_buf_out;
1526 if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001527 (compress2(buf_out, (uLongf *)&size_out, buf,
Andrew Jones8161bef2016-01-11 20:56:20 +01001528 s->dump_info.page_size, Z_BEST_SPEED) == Z_OK) &&
1529 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001530 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_ZLIB);
1531 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001532
1533 ret = write_cache(&page_data, buf_out, size_out, false);
1534 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001535 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001536 goto out;
1537 }
1538#ifdef CONFIG_LZO
1539 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001540 (lzo1x_1_compress(buf, s->dump_info.page_size, buf_out,
qiaonuohand12f57e2014-02-18 14:11:35 +08001541 (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001542 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001543 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_LZO);
1544 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001545
1546 ret = write_cache(&page_data, buf_out, size_out, false);
1547 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001548 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001549 goto out;
1550 }
1551#endif
1552#ifdef CONFIG_SNAPPY
1553 } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001554 (snappy_compress((char *)buf, s->dump_info.page_size,
qiaonuohand12f57e2014-02-18 14:11:35 +08001555 (char *)buf_out, &size_out) == SNAPPY_OK) &&
Andrew Jones8161bef2016-01-11 20:56:20 +01001556 (size_out < s->dump_info.page_size)) {
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001557 pd.flags = cpu_to_dump32(s, DUMP_DH_COMPRESSED_SNAPPY);
1558 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001559
1560 ret = write_cache(&page_data, buf_out, size_out, false);
1561 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001562 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001563 goto out;
1564 }
1565#endif
1566 } else {
1567 /*
1568 * fall back to save in plaintext, size_out should be
Andrew Jones8161bef2016-01-11 20:56:20 +01001569 * assigned the target's page size
qiaonuohand12f57e2014-02-18 14:11:35 +08001570 */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001571 pd.flags = cpu_to_dump32(s, 0);
Andrew Jones8161bef2016-01-11 20:56:20 +01001572 size_out = s->dump_info.page_size;
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001573 pd.size = cpu_to_dump32(s, size_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001574
Andrew Jones8161bef2016-01-11 20:56:20 +01001575 ret = write_cache(&page_data, buf,
1576 s->dump_info.page_size, false);
qiaonuohand12f57e2014-02-18 14:11:35 +08001577 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001578 error_setg(errp, "dump: failed to write page data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001579 goto out;
1580 }
1581 }
1582
1583 /* get and write page desc here */
Bharata B Raoacb0ef52014-05-19 19:57:44 +02001584 pd.page_flags = cpu_to_dump64(s, 0);
1585 pd.offset = cpu_to_dump64(s, offset_data);
qiaonuohand12f57e2014-02-18 14:11:35 +08001586 offset_data += size_out;
1587
1588 ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
1589 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001590 error_setg(errp, "dump: failed to write page desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001591 goto out;
1592 }
1593 }
Peter Xu2264c2c2016-02-18 13:16:53 +08001594 s->written_size += s->dump_info.page_size;
qiaonuohand12f57e2014-02-18 14:11:35 +08001595 }
1596
1597 ret = write_cache(&page_desc, NULL, 0, true);
1598 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001599 error_setg(errp, "dump: failed to sync cache for page_desc");
qiaonuohand12f57e2014-02-18 14:11:35 +08001600 goto out;
1601 }
1602 ret = write_cache(&page_data, NULL, 0, true);
1603 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001604 error_setg(errp, "dump: failed to sync cache for page_data");
qiaonuohand12f57e2014-02-18 14:11:35 +08001605 goto out;
1606 }
1607
1608out:
1609 free_data_cache(&page_desc);
1610 free_data_cache(&page_data);
1611
1612#ifdef CONFIG_LZO
1613 g_free(wrkmem);
1614#endif
1615
1616 g_free(buf_out);
qiaonuohand12f57e2014-02-18 14:11:35 +08001617}
1618
zhanghailiang4c7e2512014-10-09 14:13:11 +08001619static void create_kdump_vmcore(DumpState *s, Error **errp)
qiaonuohanb53ccc32014-02-18 14:11:36 +08001620{
Janosch Frank86a518b2022-03-30 12:35:55 +00001621 ERRP_GUARD();
qiaonuohanb53ccc32014-02-18 14:11:36 +08001622 int ret;
1623
1624 /*
1625 * the kdump-compressed format is:
1626 * File offset
1627 * +------------------------------------------+ 0x0
1628 * | main header (struct disk_dump_header) |
1629 * |------------------------------------------+ block 1
1630 * | sub header (struct kdump_sub_header) |
1631 * |------------------------------------------+ block 2
1632 * | 1st-dump_bitmap |
1633 * |------------------------------------------+ block 2 + X blocks
1634 * | 2nd-dump_bitmap | (aligned by block)
1635 * |------------------------------------------+ block 2 + 2 * X blocks
1636 * | page desc for pfn 0 (struct page_desc) | (aligned by block)
1637 * | page desc for pfn 1 (struct page_desc) |
1638 * | : |
1639 * |------------------------------------------| (not aligned by block)
1640 * | page data (pfn 0) |
1641 * | page data (pfn 1) |
1642 * | : |
1643 * +------------------------------------------+
1644 */
1645
1646 ret = write_start_flat_header(s->fd);
1647 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001648 error_setg(errp, "dump: failed to write start flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001649 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001650 }
1651
Janosch Frank86a518b2022-03-30 12:35:55 +00001652 write_dump_header(s, errp);
1653 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001654 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001655 }
1656
Janosch Frank86a518b2022-03-30 12:35:55 +00001657 write_dump_bitmap(s, errp);
1658 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001659 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001660 }
1661
Janosch Frank86a518b2022-03-30 12:35:55 +00001662 write_dump_pages(s, errp);
1663 if (*errp) {
zhanghailiang4c7e2512014-10-09 14:13:11 +08001664 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001665 }
1666
1667 ret = write_end_flat_header(s->fd);
1668 if (ret < 0) {
Peter Xue3517a52016-02-18 13:16:46 +08001669 error_setg(errp, "dump: failed to write end flat header");
zhanghailiang4c7e2512014-10-09 14:13:11 +08001670 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001671 }
qiaonuohanb53ccc32014-02-18 14:11:36 +08001672}
1673
Janosch Frank0c2994a2022-08-11 12:10:57 +00001674static int validate_start_block(DumpState *s)
Wen Congyang783e9b42012-05-07 12:10:47 +08001675{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001676 GuestPhysBlock *block;
Wen Congyang783e9b42012-05-07 12:10:47 +08001677
Janosch Frankdddf7252022-08-11 12:10:58 +00001678 if (!dump_has_filter(s)) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001679 return 0;
1680 }
1681
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001682 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frank0c2994a2022-08-11 12:10:57 +00001683 /* This block is out of the range */
Janosch Frankdddf7252022-08-11 12:10:58 +00001684 if (block->target_start >= s->filter_area_begin + s->filter_area_length ||
1685 block->target_end <= s->filter_area_begin) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001686 continue;
1687 }
Janosch Frank0c2994a2022-08-11 12:10:57 +00001688 return 0;
1689 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001690
1691 return -1;
1692}
1693
qiaonuohan7aad2482014-02-18 14:11:31 +08001694static void get_max_mapnr(DumpState *s)
1695{
1696 GuestPhysBlock *last_block;
1697
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01001698 last_block = QTAILQ_LAST(&s->guest_phys_blocks.head);
Andrew Jones8161bef2016-01-11 20:56:20 +01001699 s->max_mapnr = dump_paddr_to_pfn(s, last_block->target_end);
qiaonuohan7aad2482014-02-18 14:11:31 +08001700}
1701
Peter Xubaf28f52016-02-18 13:16:48 +08001702static DumpState dump_state_global = { .status = DUMP_STATUS_NONE };
1703
1704static void dump_state_prepare(DumpState *s)
1705{
1706 /* zero the struct, setting status to active */
1707 *s = (DumpState) { .status = DUMP_STATUS_ACTIVE };
1708}
1709
Marc-André Lureau544803c2022-03-23 19:57:31 +04001710bool qemu_system_dump_in_progress(void)
Peter Xu65d64f32016-02-18 13:16:49 +08001711{
1712 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001713 return (qatomic_read(&state->status) == DUMP_STATUS_ACTIVE);
Peter Xu65d64f32016-02-18 13:16:49 +08001714}
1715
Janosch Frankc370d532022-08-11 12:10:59 +00001716/*
1717 * calculate total size of memory to be dumped (taking filter into
1718 * account.)
1719 */
Peter Xu2264c2c2016-02-18 13:16:53 +08001720static int64_t dump_calculate_size(DumpState *s)
1721{
1722 GuestPhysBlock *block;
Janosch Frankc370d532022-08-11 12:10:59 +00001723 int64_t total = 0;
Peter Xu2264c2c2016-02-18 13:16:53 +08001724
1725 QTAILQ_FOREACH(block, &s->guest_phys_blocks.head, next) {
Janosch Frankc370d532022-08-11 12:10:59 +00001726 total += dump_filtered_memblock_size(block,
1727 s->filter_area_begin,
1728 s->filter_area_length);
Peter Xu2264c2c2016-02-18 13:16:53 +08001729 }
1730
1731 return total;
1732}
1733
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001734static void vmcoreinfo_update_phys_base(DumpState *s)
1735{
1736 uint64_t size, note_head_size, name_size, phys_base;
1737 char **lines;
1738 uint8_t *vmci;
1739 size_t i;
1740
1741 if (!note_name_equal(s, s->guest_note, "VMCOREINFO")) {
1742 return;
1743 }
1744
1745 get_note_sizes(s, s->guest_note, &note_head_size, &name_size, &size);
1746 note_head_size = ROUND_UP(note_head_size, 4);
1747
1748 vmci = s->guest_note + note_head_size + ROUND_UP(name_size, 4);
1749 *(vmci + size) = '\0';
1750
1751 lines = g_strsplit((char *)vmci, "\n", -1);
1752 for (i = 0; lines[i]; i++) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001753 const char *prefix = NULL;
1754
1755 if (s->dump_info.d_machine == EM_X86_64) {
1756 prefix = "NUMBER(phys_base)=";
1757 } else if (s->dump_info.d_machine == EM_AARCH64) {
1758 prefix = "NUMBER(PHYS_OFFSET)=";
1759 }
1760
1761 if (prefix && g_str_has_prefix(lines[i], prefix)) {
1762 if (qemu_strtou64(lines[i] + strlen(prefix), NULL, 16,
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001763 &phys_base) < 0) {
Wei Huang68cbecf2018-03-09 12:03:23 -05001764 warn_report("Failed to read %s", prefix);
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001765 } else {
1766 s->dump_info.phys_base = phys_base;
1767 }
1768 break;
1769 }
1770 }
1771
1772 g_strfreev(lines);
1773}
1774
zhanghailiang4c7e2512014-10-09 14:13:11 +08001775static void dump_init(DumpState *s, int fd, bool has_format,
1776 DumpGuestMemoryFormat format, bool paging, bool has_filter,
1777 int64_t begin, int64_t length, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08001778{
Janosch Frank86a518b2022-03-30 12:35:55 +00001779 ERRP_GUARD();
Marc-André Lureau903ef732017-09-11 18:59:25 +02001780 VMCoreInfoState *vmci = vmcoreinfo_find();
Andreas Färber182735e2013-05-29 22:29:20 +02001781 CPUState *cpu;
Wen Congyang783e9b42012-05-07 12:10:47 +08001782 int nr_cpus;
1783 int ret;
1784
Peter Xuca1fc8c2016-02-18 13:16:50 +08001785 s->has_format = has_format;
1786 s->format = format;
Peter Xu2264c2c2016-02-18 13:16:53 +08001787 s->written_size = 0;
Peter Xuca1fc8c2016-02-18 13:16:50 +08001788
qiaonuohanb53ccc32014-02-18 14:11:36 +08001789 /* kdump-compressed is conflict with paging and filter */
1790 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1791 assert(!paging && !has_filter);
1792 }
1793
Wen Congyang783e9b42012-05-07 12:10:47 +08001794 if (runstate_is_running()) {
1795 vm_stop(RUN_STATE_SAVE_VM);
1796 s->resume = true;
1797 } else {
1798 s->resume = false;
1799 }
1800
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001801 /* If we use KVM, we should synchronize the registers before we get dump
1802 * info or physmap info.
Wen Congyang783e9b42012-05-07 12:10:47 +08001803 */
Andreas Färber1b3509c2013-06-09 16:48:29 +02001804 cpu_synchronize_all_states();
Wen Congyang783e9b42012-05-07 12:10:47 +08001805 nr_cpus = 0;
Andreas Färberbdc44642013-06-24 23:50:24 +02001806 CPU_FOREACH(cpu) {
Wen Congyang783e9b42012-05-07 12:10:47 +08001807 nr_cpus++;
1808 }
1809
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001810 s->fd = fd;
Janosch Frankdddf7252022-08-11 12:10:58 +00001811 if (has_filter && !length) {
1812 error_setg(errp, QERR_INVALID_PARAMETER, "length");
1813 goto cleanup;
1814 }
1815 s->filter_area_begin = begin;
1816 s->filter_area_length = length;
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001817
Janosch Frank9b722242022-10-17 11:32:10 +00001818 /* First index is 0, it's the special null name */
1819 s->string_table_buf = g_array_new(FALSE, TRUE, 1);
1820 /*
1821 * Allocate the null name, due to the clearing option set to true
1822 * it will be 0.
1823 */
1824 g_array_set_size(s->string_table_buf, 1);
1825
Chen Gang29282072014-08-03 23:28:56 +08001826 memory_mapping_list_init(&s->list);
1827
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001828 guest_phys_blocks_init(&s->guest_phys_blocks);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +02001829 guest_phys_blocks_append(&s->guest_phys_blocks);
Peter Xu2264c2c2016-02-18 13:16:53 +08001830 s->total_size = dump_calculate_size(s);
1831#ifdef DEBUG_DUMP_GUEST_MEMORY
1832 fprintf(stderr, "DUMP: total memory to dump: %lu\n", s->total_size);
1833#endif
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001834
Cornelia Huckd1e69942017-09-13 16:20:35 +02001835 /* it does not make sense to dump non-existent memory */
1836 if (!s->total_size) {
1837 error_setg(errp, "dump: no guest memory to dump");
1838 goto cleanup;
1839 }
1840
Janosch Frank0c2994a2022-08-11 12:10:57 +00001841 /* Is the filter filtering everything? */
1842 if (validate_start_block(s) == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001843 error_setg(errp, QERR_INVALID_PARAMETER, "begin");
Laszlo Ersek5ee163e2013-08-06 12:37:09 +02001844 goto cleanup;
1845 }
1846
1847 /* get dump info: endian, class and architecture.
1848 * If the target architecture is not supported, cpu_get_dump_info() will
1849 * return -1.
1850 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001851 ret = cpu_get_dump_info(&s->dump_info, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001852 if (ret < 0) {
Markus Armbrusterf969c622023-02-07 08:51:05 +01001853 error_setg(errp,
1854 "dumping guest memory is not supported on this target");
Wen Congyang783e9b42012-05-07 12:10:47 +08001855 goto cleanup;
1856 }
1857
Andrew Jones8161bef2016-01-11 20:56:20 +01001858 if (!s->dump_info.page_size) {
Philippe Mathieu-Daudéc5d40b22023-02-23 23:38:59 +01001859 s->dump_info.page_size = qemu_target_page_size();
Andrew Jones8161bef2016-01-11 20:56:20 +01001860 }
1861
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001862 s->note_size = cpu_get_note_size(s->dump_info.d_class,
1863 s->dump_info.d_machine, nr_cpus);
Markus Armbrusterf1a46972023-02-07 08:51:06 +01001864 assert(s->note_size >= 0);
Paolo Bonzini4720bd02012-06-07 08:48:09 +02001865
Marc-André Lureau903ef732017-09-11 18:59:25 +02001866 /*
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001867 * The goal of this block is to (a) update the previously guessed
1868 * phys_base, (b) copy the guest note out of the guest.
1869 * Failure to do so is not fatal for dumping.
Marc-André Lureau903ef732017-09-11 18:59:25 +02001870 */
1871 if (vmci) {
1872 uint64_t addr, note_head_size, name_size, desc_size;
1873 uint32_t size;
1874 uint16_t format;
1875
Janosch Frank05bbaa502022-03-30 12:36:00 +00001876 note_head_size = dump_is_64bit(s) ?
1877 sizeof(Elf64_Nhdr) : sizeof(Elf32_Nhdr);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001878
1879 format = le16_to_cpu(vmci->vmcoreinfo.guest_format);
1880 size = le32_to_cpu(vmci->vmcoreinfo.size);
1881 addr = le64_to_cpu(vmci->vmcoreinfo.paddr);
1882 if (!vmci->has_vmcoreinfo) {
1883 warn_report("guest note is not present");
1884 } else if (size < note_head_size || size > MAX_GUEST_NOTE_SIZE) {
1885 warn_report("guest note size is invalid: %" PRIu32, size);
Marc-André Lureau5be5df72018-08-17 17:59:10 +02001886 } else if (format != FW_CFG_VMCOREINFO_FORMAT_ELF) {
Marc-André Lureau903ef732017-09-11 18:59:25 +02001887 warn_report("guest note format is unsupported: %" PRIu16, format);
1888 } else {
1889 s->guest_note = g_malloc(size + 1); /* +1 for adding \0 */
1890 cpu_physical_memory_read(addr, s->guest_note, size);
1891
1892 get_note_sizes(s, s->guest_note, NULL, &name_size, &desc_size);
1893 s->guest_note_size = ELF_NOTE_SIZE(note_head_size, name_size,
1894 desc_size);
1895 if (name_size > MAX_GUEST_NOTE_SIZE ||
1896 desc_size > MAX_GUEST_NOTE_SIZE ||
1897 s->guest_note_size > size) {
1898 warn_report("Invalid guest note header");
1899 g_free(s->guest_note);
1900 s->guest_note = NULL;
1901 } else {
Marc-André Lureaud9feb512017-09-11 18:59:26 +02001902 vmcoreinfo_update_phys_base(s);
Marc-André Lureau903ef732017-09-11 18:59:25 +02001903 s->note_size += s->guest_note_size;
1904 }
1905 }
1906 }
1907
Wen Congyang783e9b42012-05-07 12:10:47 +08001908 /* get memory mapping */
Wen Congyang783e9b42012-05-07 12:10:47 +08001909 if (paging) {
Janosch Frank86a518b2022-03-30 12:35:55 +00001910 qemu_get_guest_memory_mapping(&s->list, &s->guest_phys_blocks, errp);
1911 if (*errp) {
Andreas Färber11ed09c2013-05-29 21:54:03 +02001912 goto cleanup;
1913 }
Wen Congyang783e9b42012-05-07 12:10:47 +08001914 } else {
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +02001915 qemu_get_guest_simple_memory_mapping(&s->list, &s->guest_phys_blocks);
Wen Congyang783e9b42012-05-07 12:10:47 +08001916 }
1917
qiaonuohan7aad2482014-02-18 14:11:31 +08001918 s->nr_cpus = nr_cpus;
qiaonuohan7aad2482014-02-18 14:11:31 +08001919
1920 get_max_mapnr(s);
1921
1922 uint64_t tmp;
Andrew Jones8161bef2016-01-11 20:56:20 +01001923 tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT),
1924 s->dump_info.page_size);
1925 s->len_dump_bitmap = tmp * s->dump_info.page_size;
qiaonuohan7aad2482014-02-18 14:11:31 +08001926
qiaonuohanb53ccc32014-02-18 14:11:36 +08001927 /* init for kdump-compressed format */
1928 if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
1929 switch (format) {
1930 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB:
1931 s->flag_compress = DUMP_DH_COMPRESSED_ZLIB;
1932 break;
1933
1934 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO:
Laszlo Ersekc998acb2014-05-20 13:42:22 +02001935#ifdef CONFIG_LZO
1936 if (lzo_init() != LZO_E_OK) {
1937 error_setg(errp, "failed to initialize the LZO library");
1938 goto cleanup;
1939 }
1940#endif
qiaonuohanb53ccc32014-02-18 14:11:36 +08001941 s->flag_compress = DUMP_DH_COMPRESSED_LZO;
1942 break;
1943
1944 case DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY:
1945 s->flag_compress = DUMP_DH_COMPRESSED_SNAPPY;
1946 break;
1947
1948 default:
1949 s->flag_compress = 0;
1950 }
1951
zhanghailiang4c7e2512014-10-09 14:13:11 +08001952 return;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001953 }
1954
Janosch Frankdddf7252022-08-11 12:10:58 +00001955 if (dump_has_filter(s)) {
1956 memory_mapping_filter(&s->list, s->filter_area_begin, s->filter_area_length);
Wen Congyang783e9b42012-05-07 12:10:47 +08001957 }
1958
1959 /*
Janosch Frank9b722242022-10-17 11:32:10 +00001960 * The first section header is always a special one in which most
1961 * fields are 0. The section header string table is also always
1962 * set.
Wen Congyang783e9b42012-05-07 12:10:47 +08001963 */
Janosch Frank9b722242022-10-17 11:32:10 +00001964 s->shdr_num = 2;
Wen Congyang783e9b42012-05-07 12:10:47 +08001965
Janosch Frank9b722242022-10-17 11:32:10 +00001966 /*
1967 * Adds the number of architecture sections to shdr_num and sets
1968 * elf_section_data_size so we know the offsets and sizes of all
1969 * parts.
1970 */
1971 if (s->dump_info.arch_sections_add_fn) {
1972 s->dump_info.arch_sections_add_fn(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08001973 }
1974
Janosch Frank9b722242022-10-17 11:32:10 +00001975 /*
1976 * calculate shdr_num so we know the offsets and sizes of all
1977 * parts.
1978 * Calculate phdr_num
1979 *
1980 * The absolute maximum amount of phdrs is UINT32_MAX - 1 as
1981 * sh_info is 32 bit. There's special handling once we go over
1982 * UINT16_MAX - 1 but that is handled in the ehdr and section
1983 * code.
1984 */
1985 s->phdr_num = 1; /* Reserve PT_NOTE */
1986 if (s->list.num <= UINT32_MAX - 1) {
1987 s->phdr_num += s->list.num;
1988 } else {
1989 s->phdr_num = UINT32_MAX;
1990 }
1991
1992 /*
1993 * Now that the number of section and program headers is known we
1994 * can calculate the offsets of the headers and data.
1995 */
Janosch Frank05bbaa502022-03-30 12:36:00 +00001996 if (dump_is_64bit(s)) {
Janosch Frankcb415fd2022-10-17 08:38:14 +00001997 s->shdr_offset = sizeof(Elf64_Ehdr);
1998 s->phdr_offset = s->shdr_offset + sizeof(Elf64_Shdr) * s->shdr_num;
1999 s->note_offset = s->phdr_offset + sizeof(Elf64_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08002000 } else {
Janosch Frankcb415fd2022-10-17 08:38:14 +00002001 s->shdr_offset = sizeof(Elf32_Ehdr);
2002 s->phdr_offset = s->shdr_offset + sizeof(Elf32_Shdr) * s->shdr_num;
2003 s->note_offset = s->phdr_offset + sizeof(Elf32_Phdr) * s->phdr_num;
Wen Congyang783e9b42012-05-07 12:10:47 +08002004 }
Janosch Frank13fd4172022-10-17 08:38:16 +00002005 s->memory_offset = s->note_offset + s->note_size;
2006 s->section_offset = s->memory_offset + s->total_size;
Wen Congyang783e9b42012-05-07 12:10:47 +08002007
zhanghailiang4c7e2512014-10-09 14:13:11 +08002008 return;
Wen Congyang783e9b42012-05-07 12:10:47 +08002009
2010cleanup:
Chen Gang29282072014-08-03 23:28:56 +08002011 dump_cleanup(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002012}
2013
Peter Xuca1fc8c2016-02-18 13:16:50 +08002014/* this operation might be time consuming. */
2015static void dump_process(DumpState *s, Error **errp)
2016{
Janosch Frank86a518b2022-03-30 12:35:55 +00002017 ERRP_GUARD();
Peter Xud42a0d12016-02-18 13:16:56 +08002018 DumpQueryResult *result = NULL;
Peter Xuca1fc8c2016-02-18 13:16:50 +08002019
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002020 if (s->has_format && s->format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP) {
2021#ifdef TARGET_X86_64
Janosch Frank86a518b2022-03-30 12:35:55 +00002022 create_win_dump(s, errp);
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002023#endif
2024 } else if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
Janosch Frank86a518b2022-03-30 12:35:55 +00002025 create_kdump_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08002026 } else {
Janosch Frank86a518b2022-03-30 12:35:55 +00002027 create_vmcore(s, errp);
Peter Xuca1fc8c2016-02-18 13:16:50 +08002028 }
2029
Peter Xu39ba2ea2016-02-18 13:16:54 +08002030 /* make sure status is written after written_size updates */
2031 smp_wmb();
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002032 qatomic_set(&s->status,
Janosch Frank86a518b2022-03-30 12:35:55 +00002033 (*errp ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
Peter Xuca1fc8c2016-02-18 13:16:50 +08002034
Peter Xud42a0d12016-02-18 13:16:56 +08002035 /* send DUMP_COMPLETED message (unconditionally) */
2036 result = qmp_query_dump(NULL);
2037 /* should never fail */
2038 assert(result);
Markus Armbrusterd4f8bdc2022-11-04 17:06:55 +01002039 qapi_event_send_dump_completed(result,
2040 *errp ? error_get_pretty(*errp) : NULL);
Peter Xud42a0d12016-02-18 13:16:56 +08002041 qapi_free_DumpQueryResult(result);
2042
Peter Xuca1fc8c2016-02-18 13:16:50 +08002043 dump_cleanup(s);
2044}
2045
Peter Xu1fbeff72016-02-18 13:16:52 +08002046static void *dump_thread(void *data)
2047{
Peter Xu1fbeff72016-02-18 13:16:52 +08002048 DumpState *s = (DumpState *)data;
Alberto Garciac3a8fe32017-08-29 15:08:36 +03002049 dump_process(s, NULL);
Peter Xu1fbeff72016-02-18 13:16:52 +08002050 return NULL;
2051}
2052
Peter Xu39ba2ea2016-02-18 13:16:54 +08002053DumpQueryResult *qmp_query_dump(Error **errp)
2054{
2055 DumpQueryResult *result = g_new(DumpQueryResult, 1);
2056 DumpState *state = &dump_state_global;
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002057 result->status = qatomic_read(&state->status);
Peter Xu39ba2ea2016-02-18 13:16:54 +08002058 /* make sure we are reading status and written_size in order */
2059 smp_rmb();
2060 result->completed = state->written_size;
2061 result->total = state->total_size;
2062 return result;
2063}
2064
Peter Xu228de9c2016-02-18 13:16:47 +08002065void qmp_dump_guest_memory(bool paging, const char *file,
2066 bool has_detach, bool detach,
2067 bool has_begin, int64_t begin, bool has_length,
qiaonuohanb53ccc32014-02-18 14:11:36 +08002068 int64_t length, bool has_format,
2069 DumpGuestMemoryFormat format, Error **errp)
Wen Congyang783e9b42012-05-07 12:10:47 +08002070{
Janosch Frank86a518b2022-03-30 12:35:55 +00002071 ERRP_GUARD();
Wen Congyang783e9b42012-05-07 12:10:47 +08002072 const char *p;
2073 int fd = -1;
2074 DumpState *s;
Peter Xu1fbeff72016-02-18 13:16:52 +08002075 bool detach_p = false;
Wen Congyang783e9b42012-05-07 12:10:47 +08002076
Peter Xu63e27f22016-02-18 13:16:51 +08002077 if (runstate_check(RUN_STATE_INMIGRATE)) {
2078 error_setg(errp, "Dump not allowed during incoming migration.");
2079 return;
2080 }
2081
Peter Xu65d64f32016-02-18 13:16:49 +08002082 /* if there is a dump in background, we should wait until the dump
2083 * finished */
Marc-André Lureau544803c2022-03-23 19:57:31 +04002084 if (qemu_system_dump_in_progress()) {
Peter Xu65d64f32016-02-18 13:16:49 +08002085 error_setg(errp, "There is a dump in process, please wait.");
2086 return;
2087 }
2088
qiaonuohanb53ccc32014-02-18 14:11:36 +08002089 /*
2090 * kdump-compressed format need the whole memory dumped, so paging or
2091 * filter is not supported here.
2092 */
2093 if ((has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) &&
2094 (paging || has_begin || has_length)) {
2095 error_setg(errp, "kdump-compressed format doesn't support paging or "
2096 "filter");
2097 return;
2098 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002099 if (has_begin && !has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002100 error_setg(errp, QERR_MISSING_PARAMETER, "length");
Wen Congyang783e9b42012-05-07 12:10:47 +08002101 return;
2102 }
2103 if (!has_begin && has_length) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002104 error_setg(errp, QERR_MISSING_PARAMETER, "begin");
Wen Congyang783e9b42012-05-07 12:10:47 +08002105 return;
2106 }
Peter Xu1fbeff72016-02-18 13:16:52 +08002107 if (has_detach) {
2108 detach_p = detach;
2109 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002110
qiaonuohanb53ccc32014-02-18 14:11:36 +08002111 /* check whether lzo/snappy is supported */
2112#ifndef CONFIG_LZO
2113 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO) {
2114 error_setg(errp, "kdump-lzo is not available now");
2115 return;
2116 }
2117#endif
2118
2119#ifndef CONFIG_SNAPPY
2120 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY) {
2121 error_setg(errp, "kdump-snappy is not available now");
2122 return;
2123 }
2124#endif
2125
Philippe Mathieu-Daudéefc31462023-02-23 23:58:16 +01002126 if (has_format && format == DUMP_GUEST_MEMORY_FORMAT_WIN_DMP
2127 && !win_dump_available(errp)) {
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002128 return;
2129 }
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002130
Wen Congyang783e9b42012-05-07 12:10:47 +08002131#if !defined(WIN32)
2132 if (strstart(file, "fd:", &p)) {
Kevin Wolf947e4742020-10-05 17:58:44 +02002133 fd = monitor_get_fd(monitor_cur(), p, errp);
Wen Congyang783e9b42012-05-07 12:10:47 +08002134 if (fd == -1) {
Wen Congyang783e9b42012-05-07 12:10:47 +08002135 return;
2136 }
2137 }
2138#endif
2139
2140 if (strstart(file, "file:", &p)) {
Daniel P. Berrangé448058a2020-07-21 13:25:21 +01002141 fd = qemu_open_old(p, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR);
Wen Congyang783e9b42012-05-07 12:10:47 +08002142 if (fd < 0) {
Luiz Capitulino75817662013-06-07 14:36:01 -04002143 error_setg_file_open(errp, errno, p);
Wen Congyang783e9b42012-05-07 12:10:47 +08002144 return;
2145 }
2146 }
2147
2148 if (fd == -1) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01002149 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Wen Congyang783e9b42012-05-07 12:10:47 +08002150 return;
2151 }
2152
Peter Xub7bc6b12021-09-22 12:20:09 -04002153 if (!dump_migration_blocker) {
2154 error_setg(&dump_migration_blocker,
2155 "Live migration disabled: dump-guest-memory in progress");
2156 }
2157
2158 /*
2159 * Allows even for -only-migratable, but forbid migration during the
2160 * process of dump guest memory.
2161 */
2162 if (migrate_add_blocker_internal(dump_migration_blocker, errp)) {
2163 /* Remember to release the fd before passing it over to dump state */
2164 close(fd);
2165 return;
2166 }
2167
Peter Xubaf28f52016-02-18 13:16:48 +08002168 s = &dump_state_global;
2169 dump_state_prepare(s);
Wen Congyang783e9b42012-05-07 12:10:47 +08002170
zhanghailiang4c7e2512014-10-09 14:13:11 +08002171 dump_init(s, fd, has_format, format, paging, has_begin,
Janosch Frank86a518b2022-03-30 12:35:55 +00002172 begin, length, errp);
2173 if (*errp) {
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01002174 qatomic_set(&s->status, DUMP_STATUS_FAILED);
Wen Congyang783e9b42012-05-07 12:10:47 +08002175 return;
2176 }
2177
Peter Xu1fbeff72016-02-18 13:16:52 +08002178 if (detach_p) {
2179 /* detached dump */
Fam Zheng6796b402017-05-03 15:28:19 +08002180 s->detached = true;
Peter Xu1fbeff72016-02-18 13:16:52 +08002181 qemu_thread_create(&s->dump_thread, "dump_thread", dump_thread,
2182 s, QEMU_THREAD_DETACHED);
2183 } else {
2184 /* sync dump */
2185 dump_process(s, errp);
2186 }
Wen Congyang783e9b42012-05-07 12:10:47 +08002187}
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002188
2189DumpGuestMemoryCapability *qmp_query_dump_guest_memory_capability(Error **errp)
2190{
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002191 DumpGuestMemoryCapability *cap =
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002192 g_new0(DumpGuestMemoryCapability, 1);
Eric Blake95b3a8c2021-01-13 16:10:13 -06002193 DumpGuestMemoryFormatList **tail = &cap->formats;
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002194
2195 /* elf is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002196 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_ELF);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002197
2198 /* kdump-zlib is always available */
Eric Blake95b3a8c2021-01-13 16:10:13 -06002199 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002200
2201 /* add new item if kdump-lzo is available */
2202#ifdef CONFIG_LZO
Eric Blake95b3a8c2021-01-13 16:10:13 -06002203 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002204#endif
2205
2206 /* add new item if kdump-snappy is available */
2207#ifdef CONFIG_SNAPPY
Eric Blake95b3a8c2021-01-13 16:10:13 -06002208 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY);
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002209#endif
2210
Philippe Mathieu-Daudéefc31462023-02-23 23:58:16 +01002211 if (win_dump_available(NULL)) {
2212 QAPI_LIST_APPEND(tail, DUMP_GUEST_MEMORY_FORMAT_WIN_DMP);
2213 }
Viktor Prutyanov2da91b52018-05-17 19:23:39 +03002214
qiaonuohan7d6dc7f2014-02-18 14:11:38 +08002215 return cap;
2216}